-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalu.v
76 lines (71 loc) · 1.16 KB
/
alu.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
`timescale 1ns / 1ps
module alu(opcode, a, b, result, zero);
input [3:0] opcode;
input [31:0] a;
input [31:0] b;
output reg [31:0] result;
output reg zero;
reg [31:0] y;
integer i;
always @(opcode, a, b)
begin
case(opcode)
4'b0000: // ADD
begin
result <= a + b;
end
4'b0001: // SUB: a - b
begin
result <= a + (~b + 1);
end
4'b0010: // AND
begin
result <= a & b;
end
4'b0011: // OR
begin
result <= a | b;
end
4'b0100: // XOR
begin
result <= a ^ b;
end
4'b0101: // LUI
begin
result[31:16] <= a;
result[15:0] <= 16'b0000000000000000;
end
4'b0110: // SLL
begin
y = a;
for(i = b ; i > 0 ; i = i - 1) begin
y = {y[30:0], 1'b0};
end
result <= y;
end
4'b0111: // SRL
begin
y = a;
for(i = b ; i > 0 ; i = i - 1) begin
y = {1'b0, y[31:1]};
end
result <= y;
end
4'b1000: // SRA
begin
y = a;
for(i = b ; i > 0 ; i = i - 1) begin
y = {y[31], y[31:1]};
end
result <= y;
end
endcase
end
always @(result) begin
if (result == 0) begin
zero <= 1;
end else begin
zero <= 0;
end
end
endmodule