-
Notifications
You must be signed in to change notification settings - Fork 0
/
ALU.v
74 lines (63 loc) · 2.17 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
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 2023/01/20 14:32:35
// Design Name:
// Module Name: ALU
// Project Name:
// Target Devices:
// Tool Versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module ALU(
input [2:0] funct,
input [31:0] op1,
input [31:0] op2,
input [3:0] ALUcntl,
output [31:0] ALUResult
);
/* signed operand 1 */
wire signed [31:0] signed_op1;
/* ALU operation results */
wire [31:0] and_result;
wire [31:0] or_result;
wire [31:0] xor_result;
wire [31:0] lsl_result;
wire [31:0] rsl_result;
wire [31:0] rsa_result;
wire [31:0] add_result;
wire [31:0] sub_result;
/* Carry */
wire Cout;
/* Branch wires */
wire signed [31:0] signed_ALUResult;
assign signed_op1 = op1;
assign and_result = op1 & op2;
assign or_result = op1 | op2;
assign xor_result = op1 ^ op2;
assign lsl_result = op1 << op2;
assign rsl_result = op1 >> op2;
assign rsa_result = signed_op1 >>> op2;
assign add_result = op1 + op2;
assign {Cout, sub_result} = {1'b0, op1} + ~{1'b0, op2} + 1'b1;
assign ALUResult = (ALUcntl == 4'b0000) ? and_result :
(ALUcntl == 4'b0001) ? or_result :
(ALUcntl == 4'b0010) ? xor_result :
(ALUcntl == 4'b0011) ? lsl_result :
(ALUcntl == 4'b0100) ? rsl_result :
(ALUcntl == 4'b0101) ? rsa_result :
(ALUcntl == 4'b0110) ? add_result :
(ALUcntl == 4'b0111) ? (funct == 3'b010) ? (sub_result[31] == 0) ? 32'h0000_0000 : 32'h0000_0001 :
(funct == 3'b011) ? (Cout == 0) ? 32'h0000_0000 : 32'h0000_0001 :
sub_result :
32'hxxxx_xxxx;
endmodule