-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathALU.v
More file actions
53 lines (51 loc) · 1.04 KB
/
ALU.v
File metadata and controls
53 lines (51 loc) · 1.04 KB
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
module ALU #(parameter Width = 32)(
input [3:0] controlsignal, input [Width-1:0] A1,A2,
output reg [Width-1:0] Y,
output zero,
output reg overflow,carry);
//wire [63:0] B;
always @(*)
begin
if (controlsignal == 4'b0000)
Y = A1 & A2;
else if (controlsignal == 4'b0001)
Y = A1 | A2;
else if(controlsignal == 4'b0010)
begin
{carry,Y} = A1 + A2;
overflow=(A1[31] & A2[31] & ~Y[31]) |((~A1[31] & ~A2[31] & Y[31]));
end
else if(controlsignal == 4'b0110)
begin
{carry,Y} = A1 - A2;
overflow=(A1[31] & A2[31] & ~Y[31]) |((~A1[31] & ~A2[31] & Y[31]));
end
else if(controlsignal == 4'b0011)
Y = A1 << A2;
else if(controlsignal == 4'b0100)
begin
//A1 = ~A1 + 1;
//A2 = ~A2 + 1;
if((~A1+1) < (~A2+1))
Y = 1;
else
Y = 0;
end
else if(controlsignal == 4'b0101)
begin
if(A1 < A2)
Y = 1;
else
Y = 0;
end
else if(controlsignal == 4'b0111)
Y = A1 ^ A2;
else if(controlsignal == 4'b1000)
Y = A1 >> A2;
else if(controlsignal == 4'b1010)
Y = A1 >>> A2;
else
Y = {Width{1'bx}};
end
assign zero = (Y == 0) ? 1 : 0;
endmodule