-
Notifications
You must be signed in to change notification settings - Fork 0
/
ALU.v
83 lines (76 loc) · 3 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
77
78
79
80
81
82
83
`timescale 1ns / 1ps
module ALU(clk, a, b, alufn, otp, zero, overflow);
// Inputs
input wire clk;
input wire [31:0] a;
input wire [31:0] b;
input wire [5:0] alufn; // 6-bit operation code
// Outputs
output reg [31:0] otp;
output reg zero; // Set if the output of the ALU is 0
output reg overflow; // Overflow flag
// Internal wires for temporary results and flags
wire [31:0] tempAuRes, tempLuRes, tempSuRes;
wire tempAuZero, tempAuOverflow;
wire tempLuZero, tempLuOverflow;
wire tempSuZero, tempSuOverflow;
// ALU operation selection based on the operation code (alufn)
always @(a or b or alufn)
begin
casex(alufn)
// Arithmetic operations
6'b0000xx: begin
if (alufn[1:0] == 2'b00) begin // ADD
otp = a + b;
zero = (otp == 0) ? 1 : 0;
overflow = ((a >= 0 && b >= 0 && otp < 0) || (a < 0 && b < 0 && otp >= 0)) ? 1 : 0;
end
else if (alufn[1:0] == 2'b01) begin // SUB
otp = a - b;
zero = (otp == 0) ? 1 : 0;
overflow = ((a >= 0 && b < 0 && otp < 0) || (a < 0 && b >= 0 && otp > 0)) ? 1 : 0;
end
else if (alufn[1:0] == 2'b10) begin // MUL
otp = a * b;
zero = (otp == 0) ? 1 : 0;
overflow = 0;
end
end
// Logical operations
6'b0001xx: begin
case (alufn[1:0])
2'b00: otp = a & b; // AND
2'b01: otp = a | b; // OR
2'b10: otp = a ^ b; // XOR
endcase
zero = (otp == 0) ? 1 : 0;
overflow = 0;
end
// Shift and comparison operations
6'b0010xx: begin
case (alufn[1:0])
2'b00: otp = a << b; // SHIFTLEFT
2'b01: otp = a >> b; // SHIFTRIGHT
2'b11: otp = (a < b) ? 1 : 0; // SLT
endcase
zero = (otp == 0) ? 1 : 0;
overflow = 0;
end
default: otp = {32{1'b0}}; // Default case
endcase
$display("a%h - b%h - otp %h alufn %b", a, b, otp, alufn);
end
endmodule
// Separate modules for ArithmeticUnit, LogicalUnit, and ShiftUnit
module ArithmeticUnit(a, b, alufn, otp, zero, overflow);
// Similar structure as the corresponding section in the ALU module
// Handles arithmetic operations
endmodule
module LogicalUnit(a, b, otp, alufn, zero, overflow);
// Similar structure as the corresponding section in the ALU module
// Handles logical operations
endmodule
module ShiftUnit(a, b, otp, alufn, zero, overflow);
// Similar structure as the corresponding section in the ALU module
// Handles shift and comparison operations
endmodule