-
Notifications
You must be signed in to change notification settings - Fork 11
/
ALU32bit.v
184 lines (140 loc) · 3.93 KB
/
ALU32bit.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//32 bit sayılarla Arithmetic işlemler yapan modül.
//Tüm instructionların yapması gerekenler burada halledildi.
module ALU32bit(ALU_result, sig_branch, opcode, rs_content, rt_content, shamt, ALU_control, immediate);
input [5:0] ALU_control, opcode;
input [4:0] shamt; // shamt
input [15:0] immediate;
input [31:0] rs_content, rt_content; //inputs
output reg sig_branch;
output reg [31:0] ALU_result; //output
integer i; //for loop
// temp for sra command
reg signed [31:0] temp, signed_rs, signed_rt;
reg [31:0] signExtend, zeroExtend;
always @ (ALU_control, rs_content, rt_content, shamt, immediate) begin
// signed value assigment
signed_rs = rs_content;
signed_rt = rt_content;
// R-type instruction
if(opcode == 6'h0) begin
case(ALU_control)
6'h20 : //add
ALU_result = signed_rs + signed_rt;
6'h21 : //addu
ALU_result = rs_content + rt_content;
6'h22 : //sub
ALU_result = signed_rs - signed_rt;
6'h23 : //subu
ALU_result = rs_content - rt_content;
6'h24 : //and
ALU_result = rs_content & rt_content;
6'h25 : //or
ALU_result = rs_content | rt_content;
6'h27 : //nor
ALU_result = ~(rs_content | rt_content);
6'h03 : //sra
begin
temp = rt_content;
for(i = 0; i < shamt; i = i + 1) begin
temp = {temp[31],temp[31:1]}; //add the lsb for msb
end
ALU_result = temp;
end
6'h02 : //srl
ALU_result = (rt_content >> shamt);
6'h00 : //sll
ALU_result = (rt_content << shamt);
6'h2b : //sltu
begin
if(rs_content < rt_content) begin
ALU_result = 1;
end else begin
ALU_result = 0;
end
end
6'h2a : //slt
begin
if(signed_rs < signed_rt) begin
ALU_result = 1;
end else begin
ALU_result = 0;
end
end
endcase //case
end // if
// I type
else begin
signExtend = {{16{immediate[15]}}, immediate};
zeroExtend = {{16{1'b0}}, immediate};
case(opcode)
6'h8 : // addi
ALU_result = signed_rs + signExtend;
6'h9 : // addiu
ALU_result = rs_content + signExtend;
6'b010010 : // andi
ALU_result = rs_content & zeroExtend;
6'h4 : // beq
begin
// if the result is zero, they are equal go branch!
ALU_result = signed_rs - signed_rt;
if(ALU_result == 0) begin
sig_branch = 1'b1;
end
else begin
sig_branch = 1'b0;
end
end
6'h5 : // bne
begin
// if the result is not zero, they are not equal go branch!
ALU_result = signed_rs - signed_rt;
if(ALU_result != 0) begin
sig_branch = 1'b1;
ALU_result = 1'b0;
end
else begin
sig_branch = 1'b0;
end
end
6'b010101 : // lui
ALU_result = {immediate, {16{1'b0}}};
6'b010011 : // ori
ALU_result = rs_content | zeroExtend;
6'b001010 : // slti
begin
if(signed_rs < $signed(signExtend)) begin
ALU_result = 1;
end else begin
ALU_result = 0;
end
end
6'b001011 : // sltiu
begin
if(rs_content < signExtend) begin
ALU_result = 1;
end else begin
ALU_result = 0;
end
end
6'h28 : // sb
ALU_result = signed_rs + signExtend;
6'h29 : // sh
ALU_result = signed_rs + signExtend;
6'h2b : // sw
ALU_result = signed_rs + signExtend;
6'h23 : // lw
ALU_result = signed_rs + signExtend;
6'h24 : // lbu
ALU_result = signed_rs + signExtend;
6'h25 : // lhu
ALU_result = signed_rs + signExtend;
6'h30 : // ll
ALU_result = signed_rs + signExtend;
endcase
end
end
initial begin
$monitor("opcode: %6b, Rs content: %32b, rt content: %32b, signExtendImm = %32b, result: %32b\n",
opcode, rs_content, rt_content, signExtend, ALU_result);
end
endmodule