-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathalu.v
159 lines (129 loc) · 4.55 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
Authored 2018-2019, Ryan Voo.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the following
disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials
provided with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
`include "../include/rv32i-defines.v"
`include "../include/sail-core-defines.v"
/*
* Description:
*
* This module implements the ALU for the RV32I.
*/
/*
* Not all instructions are fed to the ALU. As a result, the ALUctl
* field is only unique across the instructions that are actually
* fed to the ALU.
*/
module alu(ALUctl, A, B, ALUOut, Branch_Enable);
input [6:0] ALUctl;
input [31:0] A;
input [31:0] B;
output reg [31:0] ALUOut;
output reg Branch_Enable;
/*
* This uses Yosys's support for nonzero initial values:
*
* https://github.com/YosysHQ/yosys/commit/0793f1b196df536975a044a4ce53025c81d00c7f
*
* Rather than using this simulation construct (`initial`),
* the design should instead use a reset signal going to
* modules in the design.
*/
initial begin
ALUOut = 32'b0;
Branch_Enable = 1'b0;
end
always @(ALUctl, A, B) begin
case (ALUctl[3:0])
/*
* AND (the fields also match ANDI and LUI)
*/
`kSAIL_MICROARCHITECTURE_ALUCTL_3to0_AND: ALUOut = A & B;
/*
* OR (the fields also match ORI)
*/
`kSAIL_MICROARCHITECTURE_ALUCTL_3to0_OR: ALUOut = A | B;
/*
* ADD (the fields also match AUIPC, all loads, all stores, and ADDI)
*/
`kSAIL_MICROARCHITECTURE_ALUCTL_3to0_ADD: ALUOut = A + B;
/*
* SUBTRACT (the fields also matches all branches)
*/
`kSAIL_MICROARCHITECTURE_ALUCTL_3to0_SUB: ALUOut = A - B;
/*
* SLT (the fields also matches all the other SLT variants)
*/
`kSAIL_MICROARCHITECTURE_ALUCTL_3to0_SLT: ALUOut = $signed(A) < $signed(B) ? 32'b1 : 32'b0;
/*
* SRL (the fields also matches the other SRL variants)
*/
`kSAIL_MICROARCHITECTURE_ALUCTL_3to0_SRL: ALUOut = A >> B[4:0];
/*
* SRA (the fields also matches the other SRA variants)
*/
`kSAIL_MICROARCHITECTURE_ALUCTL_3to0_SRA: ALUOut = $signed(A) >>> B[4:0];
/*
* SLL (the fields also match the other SLL variants)
*/
`kSAIL_MICROARCHITECTURE_ALUCTL_3to0_SLL: ALUOut = A << B[4:0];
/*
* XOR (the fields also match other XOR variants)
*/
`kSAIL_MICROARCHITECTURE_ALUCTL_3to0_XOR: ALUOut = A ^ B;
/*
* CSRRW only
*/
`kSAIL_MICROARCHITECTURE_ALUCTL_3to0_CSRRW: ALUOut = A;
/*
* CSRRS only
*/
`kSAIL_MICROARCHITECTURE_ALUCTL_3to0_CSRRS: ALUOut = A | B;
/*
* CSRRC only
*/
`kSAIL_MICROARCHITECTURE_ALUCTL_3to0_CSRRC: ALUOut = (~A) & B;
/*
* Should never happen.
*/
default: ALUOut = 0;
endcase
end
always @(ALUctl, ALUOut, A, B) begin
case (ALUctl[6:4])
`kSAIL_MICROARCHITECTURE_ALUCTL_6to4_BEQ: Branch_Enable = (ALUOut == 0);
`kSAIL_MICROARCHITECTURE_ALUCTL_6to4_BNE: Branch_Enable = !(ALUOut == 0);
`kSAIL_MICROARCHITECTURE_ALUCTL_6to4_BLT: Branch_Enable = ($signed(A) < $signed(B));
`kSAIL_MICROARCHITECTURE_ALUCTL_6to4_BGE: Branch_Enable = ($signed(A) >= $signed(B));
`kSAIL_MICROARCHITECTURE_ALUCTL_6to4_BLTU: Branch_Enable = ($unsigned(A) < $unsigned(B));
`kSAIL_MICROARCHITECTURE_ALUCTL_6to4_BGEU: Branch_Enable = ($unsigned(A) >= $unsigned(B));
default: Branch_Enable = 1'b0;
endcase
end
endmodule