-
Notifications
You must be signed in to change notification settings - Fork 1
/
riscv_alu_basic.sv
321 lines (261 loc) · 11.3 KB
/
riscv_alu_basic.sv
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Copyright 2018 ETH Zurich and University of Bologna.
// Copyright and related rights are licensed under the Solderpad Hardware
// License, Version 0.51 (the "License"); you may not use this file except in
// compliance with the License. You may obtain a copy of the License at
// http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
// or agreed to in writing, software, hardware and materials distributed under
// this License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
////////////////////////////////////////////////////////////////////////////////
// Engineer: Matthias Baer - [email protected] //
// //
// Additional contributions by: //
// Igor Loi - [email protected] //
// Andreas Traber - [email protected] //
// Lukas Mueller - [email protected] //
// Davide Schiavone - [email protected] //
// //
// Design Name: ALU //
// Project Name: RI5CY //
// Language: SystemVerilog //
// //
// Description: Minimal version of the ALU to be used with shared DSP //
// //
////////////////////////////////////////////////////////////////////////////////
import riscv_defines::*;
module riscv_alu_basic
(
input logic clk,
input logic rst_n,
input logic [ALU_OP_WIDTH-1:0] operator_i,
input logic [31:0] operand_a_i,
input logic [31:0] operand_b_i,
input logic [31:0] operand_c_i,
input logic [ 1:0] vector_mode_i,
input logic [ 4:0] bmask_a_i,
input logic [ 4:0] bmask_b_i,
input logic [ 1:0] imm_vec_ext_i,
output logic [31:0] result_o,
output logic comparison_result_o,
output logic ready_o,
input logic ex_ready_i
);
logic [31:0] operand_a_rev;
logic [31:0] operand_a_neg;
logic [31:0] operand_a_neg_rev;
assign operand_a_neg = ~operand_a_i;
// bit reverse operand_a for left shifts and bit counting
generate
genvar k;
for(k = 0; k < 32; k++)
begin
assign operand_a_rev[k] = operand_a_i[31-k];
end
endgenerate
// bit reverse operand_a_neg for left shifts and bit counting
generate
genvar m;
for(m = 0; m < 32; m++)
begin
assign operand_a_neg_rev[m] = operand_a_neg[31-m];
end
endgenerate
logic [31:0] operand_b_neg;
assign operand_b_neg = ~operand_b_i;
logic [31:0] bmask;
//////////////////////////////////////////////////////////////////////////////////////////
// ____ _ _ _ _ _ _ _ _ //
// | _ \ __ _ _ __| |_(_) |_(_) ___ _ __ ___ __| | / \ __| | __| | ___ _ __ //
// | |_) / _` | '__| __| | __| |/ _ \| '_ \ / _ \/ _` | / _ \ / _` |/ _` |/ _ \ '__| //
// | __/ (_| | | | |_| | |_| | (_) | | | | __/ (_| | / ___ \ (_| | (_| | __/ | //
// |_| \__,_|_| \__|_|\__|_|\___/|_| |_|\___|\__,_| /_/ \_\__,_|\__,_|\___|_| //
// //
//////////////////////////////////////////////////////////////////////////////////////////
logic adder_op_b_negate;
logic [31:0] adder_op_a, adder_op_b;
logic [35:0] adder_in_a, adder_in_b;
logic [31:0] adder_result;
logic [35:0] adder_result_expanded;
assign adder_op_b_negate = (operator_i == ALU_SUB) || (operator_i == ALU_SUBR) ||
(operator_i == ALU_SUBU) || (operator_i == ALU_SUBR);
// prepare operand a
assign adder_op_a = (operator_i == ALU_ABS) ? operand_a_neg : operand_a_i;
// prepare operand b
assign adder_op_b = adder_op_b_negate ? operand_b_neg : operand_b_i;
assign adder_result = adder_op_a + adder_op_b + adder_op_b_negate;
////////////////////////////////////////
// ____ _ _ ___ _____ _____ //
// / ___|| | | |_ _| ___|_ _| //
// \___ \| |_| || || |_ | | //
// ___) | _ || || _| | | //
// |____/|_| |_|___|_| |_| //
// //
////////////////////////////////////////
logic shift_left; // should we shift left
logic shift_arithmetic;
logic [31:0] shift_amt_left; // amount of shift, if to the left
logic [31:0] shift_amt; // amount of shift, to the right
logic [31:0] shift_amt_int; // amount of shift, used for the actual shifters
logic [31:0] shift_op_a; // input of the shifter
logic [32:0] shift_op_a_ext; // sign extension
logic [31:0] shift_result;
logic [31:0] shift_right_result;
logic [31:0] shift_left_result;
assign shift_amt = operand_b_i;
// by reversing the bits of the input, we also have to reverse the order of shift amounts
assign shift_amt_left[31: 0] = shift_amt[31: 0];
// ALU_FL1 and ALU_CBL are used for the bit counting ops later
assign shift_left = (operator_i == ALU_SLL);
assign shift_arithmetic = (operator_i == ALU_SRA);
// choose the bit reversed or the normal input for shift operand a
assign shift_op_a = shift_left ? operand_a_rev : operand_a_i;
assign shift_amt_int = shift_left ? shift_amt_left : shift_amt;
assign shift_op_a_ext = shift_arithmetic ? {shift_op_a[31], shift_op_a} : {1'b0, shift_op_a};
assign shift_right_result = $signed(shift_op_a_ext) >>> shift_amt_int[4:0];
// bit reverse the shift_right_result for left shifts
genvar j;
generate
for(j = 0; j < 32; j++)
begin
assign shift_left_result[j] = shift_right_result[31-j];
end
endgenerate
assign shift_result = shift_left ? shift_left_result : shift_right_result;
//////////////////////////////////////////////////////////////////
// ____ ___ __ __ ____ _ ____ ___ ____ ___ _ _ //
// / ___/ _ \| \/ | _ \ / \ | _ \|_ _/ ___| / _ \| \ | | //
// | | | | | | |\/| | |_) / _ \ | |_) || |\___ \| | | | \| | //
// | |__| |_| | | | | __/ ___ \| _ < | | ___) | |_| | |\ | //
// \____\___/|_| |_|_| /_/ \_\_| \_\___|____/ \___/|_| \_| //
// //
//////////////////////////////////////////////////////////////////
logic [3:0] is_equal;
logic [3:0] is_greater; // handles both signed and unsigned forms
// 8-bit vector comparisons, basic building blocks
logic [3:0] cmp_signed;
logic [3:0] is_equal_vec;
logic [3:0] is_greater_vec;
always_comb
begin
cmp_signed = 4'b0;
unique case (operator_i)
ALU_GTS,
ALU_GES,
ALU_LTS,
ALU_LES,
ALU_SLTS,
ALU_SLETS,
ALU_MIN,
ALU_MAX,
ALU_ABS,
ALU_CLIP,
ALU_CLIPU: begin
case (vector_mode_i)
VEC_MODE8: cmp_signed[3:0] = 4'b1111;
VEC_MODE16: cmp_signed[3:0] = 4'b1010;
default: cmp_signed[3:0] = 4'b1000;
endcase
end
default:;
endcase
end
// generate vector equal and greater than signals, cmp_signed decides if the
// comparison is done signed or unsigned
genvar i;
generate
for(i = 0; i < 4; i++)
begin
assign is_equal_vec[i] = (operand_a_i[8*i+7:8*i] == operand_b_i[8*i+7:i*8]);
assign is_greater_vec[i] = $signed({operand_a_i[8*i+7] & cmp_signed[i], operand_a_i[8*i+7:8*i]})
>
$signed({operand_b_i[8*i+7] & cmp_signed[i], operand_b_i[8*i+7:i*8]});
end
endgenerate
// generate the real equal and greater than signals that take the vector
// mode into account
always_comb
begin
// 32-bit mode
is_equal[3:0] = {4{is_equal_vec[3] & is_equal_vec[2] & is_equal_vec[1] & is_equal_vec[0]}};
is_greater[3:0] = {4{is_greater_vec[3] | (is_equal_vec[3] & (is_greater_vec[2]
| (is_equal_vec[2] & (is_greater_vec[1]
| (is_equal_vec[1] & (is_greater_vec[0]))))))}};
case(vector_mode_i)
VEC_MODE16:
begin
is_equal[1:0] = {2{is_equal_vec[0] & is_equal_vec[1]}};
is_equal[3:2] = {2{is_equal_vec[2] & is_equal_vec[3]}};
is_greater[1:0] = {2{is_greater_vec[1] | (is_equal_vec[1] & is_greater_vec[0])}};
is_greater[3:2] = {2{is_greater_vec[3] | (is_equal_vec[3] & is_greater_vec[2])}};
end
VEC_MODE8:
begin
is_equal[3:0] = is_equal_vec[3:0];
is_greater[3:0] = is_greater_vec[3:0];
end
default:; // see default assignment
endcase
end
// generate comparison result
logic [3:0] cmp_result;
always_comb
begin
cmp_result = is_equal;
unique case (operator_i)
ALU_EQ: cmp_result = is_equal;
ALU_NE: cmp_result = ~is_equal;
ALU_GTS, ALU_GTU: cmp_result = is_greater;
ALU_GES, ALU_GEU: cmp_result = is_greater | is_equal;
ALU_LTS, ALU_SLTS,
ALU_LTU, ALU_SLTU: cmp_result = ~(is_greater | is_equal);
ALU_SLETS,
ALU_SLETU,
ALU_LES, ALU_LEU: cmp_result = ~is_greater;
default: ;
endcase
end
assign comparison_result_o = cmp_result[3];
////////////////////////////////////////////////////////
// ____ _ _ __ __ //
// | _ \ ___ ___ _ _| | |_ | \/ |_ ___ __ //
// | |_) / _ \/ __| | | | | __| | |\/| | | | \ \/ / //
// | _ < __/\__ \ |_| | | |_ | | | | |_| |> < //
// |_| \_\___||___/\__,_|_|\__| |_| |_|\__,_/_/\_\ //
// //
////////////////////////////////////////////////////////
always_comb
begin
result_o = 'x;
unique case (operator_i)
// Standard Operations
ALU_AND: result_o = operand_a_i & operand_b_i;
ALU_OR: result_o = operand_a_i | operand_b_i;
ALU_XOR: result_o = operand_a_i ^ operand_b_i;
// Shift Operations
ALU_ADD,
ALU_SUB: result_o = adder_result;
ALU_SLL,
ALU_SRL, ALU_SRA: result_o = shift_result;
// Comparison Operations
ALU_EQ, ALU_NE,
ALU_GTU, ALU_GEU,
ALU_LTU, ALU_LEU,
ALU_GTS, ALU_GES,
ALU_LTS, ALU_LES: begin
result_o[31:24] = {8{cmp_result[3]}};
result_o[23:16] = {8{cmp_result[2]}};
result_o[15: 8] = {8{cmp_result[1]}};
result_o[ 7: 0] = {8{cmp_result[0]}};
end
ALU_SLTS, ALU_SLTU,
ALU_SLETS, ALU_SLETU: result_o = {31'b0, comparison_result_o};
default:
`ifndef SYNTHESIS
$warning("instruction not supported in basic alu")// default case to suppress unique warning
`endif
;
endcase
end
assign ready_o = 1'b1;
endmodule