-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathALU.sv
More file actions
112 lines (101 loc) · 3.55 KB
/
Copy pathALU.sv
File metadata and controls
112 lines (101 loc) · 3.55 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
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
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 2024/05/01 10:31:41
// Design Name:
// Module Name: ALU
// Project Name:
// Target Devices:
// Tool Versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
`include "defines.svh"
module ALU #(
parameter DATAWIDTH = 32
)(
input logic [DATAWIDTH-1:0] A,
input logic [DATAWIDTH-1:0] B,
input logic [3:0] ALUControl,
output logic [DATAWIDTH-1:0] Result,
output logic isTrue
);
always_comb begin
Result = 0;
case(ALUControl)
// 算术运算
`ALU_ADD: Result = A + B;
`ALU_SUB, `ALU_BEQ, `ALU_BNE: Result = A - B;
// 移位操作
`ALU_SLL: Result = A << B[4:0]; // 逻辑左移
`ALU_SRL: Result = A >> B[4:0]; // 逻辑右移
`ALU_SRA: Result = $signed(A) >>> B[4:0]; // 算术右移
// 逻辑运算
`ALU_XOR: Result = A ^ B;
`ALU_OR: Result = A | B;
`ALU_AND: Result = A & B;
// 比较运算
`ALU_SLT, `ALU_BLT, `ALU_BGE: begin // 有符号比较
Result = ($signed(A) < $signed(B)) ? 1 : 0;
end
`ALU_SLTU, `ALU_BLTU, `ALU_BGEU: begin // 无符号比较
Result = (A < B) ? 1 : 0;
end
default: begin
Result = 32'h0;
end
endcase
end
always_comb begin
isTrue = 1'b0; // 默认值
case(ALUControl)
`ALU_BEQ: begin
isTrue = (A == B);
$display("Entered ALU_BEQ case: A = %h, B = %h, isTrue = %b", A, B, isTrue);
end
`ALU_BNE: begin
isTrue = (A != B);
$display("Entered ALU_BNE case: A = %h, B = %h, isTrue = %b", A, B, isTrue);
end
`ALU_BLT: begin
isTrue = ($signed(A) < $signed(B));
$display("Entered ALU_BLT case: A = %h, B = %h, isTrue = %b", A, B, isTrue);
end
`ALU_BLTU: begin
isTrue = (A < B);
$display("Entered ALU_BLTU case: A = %h, B = %h, isTrue = %b", A, B, isTrue);
$display("***** ALU_BLTU: A < B check: A = %h, B = %h", A, B);
end
`ALU_BGE: begin
isTrue = ($signed(A) >= $signed(B));
$display("Entered ALU_BGE case: A = %h, B = %h, isTrue = %b", A, B, isTrue);
end
`ALU_BGEU: begin
isTrue = (A >= B);
$display("Entered ALU_BGEU case: A = %h, B = %h, isTrue = %b", A, B, isTrue);
end
default: begin
isTrue = 1'b0;
$display("Entered default case: A = %h, B = %h, isTrue = %b", A, B, isTrue);
$display("&&& Default case entered!");
end
endcase
end
// 输出最终的 isTrue
always@(* ) begin
$display(" ");
$display("------------In ALU------------");
$display("ALUControl = %h", ALUControl);
$display("ALU Compare: A = %h, B = %h, Result = %h", A, B, Result);
$display("A < B = %b", (A < B));
$display("ALU isTrue = %b", isTrue);
end
endmodule