-
Notifications
You must be signed in to change notification settings - Fork 1
/
alu.sv
63 lines (61 loc) · 1.2 KB
/
alu.sv
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
module alu (input logic [31:0] src1, src2, input logic [3:0] aluc, output logic [31:0] out, output logic zero,
output logic cout, overflow, sign);
always_comb begin
case (aluc)
4'b0000: begin
{cout, out} = {1'b0, src1} + {1'b0, src2};
end
4'b0001: begin
{cout, out} = {1'b0, src1} + {1'b0, ~src2} + 9'b1;
end
4'b0010: begin
out = src1 & src2;
cout = 0;
end
4'b0011: begin
out = src1 | src2;
cout = 0;
end
4'b0100: begin // sra
out = $signed(src1) >>> $unsigned(src2[4:0]);
cout = 0;
end
4'b0101: begin
if ($signed(src1) < $signed(src2))
out = 1;
else
out = 0;
cout = 0;
end
4'b0110: begin // srl
out = src1 >> src2[4:0];
cout = 0;
end
4'b0111: begin // sll
out = src1 << src2[4:0];
cout = 0;
end
4'b1000: begin // sltu
if($unsigned(src1) < $unsigned(src2))
out = 1;
else
out = 0;
cout = 0;
end
4'b1001: begin // xor
out = src1 ^ src2;
cout = 0;
end
default: begin
out = 0;
cout = 0;
end
endcase
if (out==0)
zero = 1;
else
zero = 0;
sign = out[31];
overflow = (~((src1[31] ^ src2[31]) ^ aluc[0])) & (src1[31] ^ sign) & ~aluc[1];
end
endmodule