forked from grahamedgecombe/icicle
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrv32_decode.sv
294 lines (256 loc) · 7.59 KB
/
rv32_decode.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
`ifndef RV32_DECODE
`define RV32_DECODE
`include "rv32_control.sv"
`include "rv32_csrs.sv"
`include "rv32_regs.sv"
module rv32_decode (
input clk,
input reset,
`ifdef RISCV_FORMAL
/* debug control in */
input intr_in,
/* debug data in */
input [31:0] next_pc_in,
/* debug control out */
output logic intr_out,
/* debug data out */
output logic [31:0] next_pc_out,
output logic [31:0] instr_out,
`endif
/* control in (from hazard) */
input stall_in,
input flush_in,
input writeback_flush_in,
/* control in (from fetch) */
input valid_in,
input exception_in,
input [3:0] exception_cause_in,
input branch_predicted_taken_in,
/* control in (from writeback) */
input [4:0] rd_in,
input rd_write_in,
/* data in */
input [31:0] pc_in,
input [31:0] instr_in,
/* data in (from writeback) */
input [31:0] rd_value_in,
/* control out (to hazard) */
output logic [4:0] rs1_unreg_out,
output logic rs1_read_unreg_out,
output logic [4:0] rs2_unreg_out,
output logic rs2_read_unreg_out,
output logic mem_fence_unreg_out,
/* control out */
output logic branch_predicted_taken_out,
output logic valid_out,
output logic exception_out,
output logic [3:0] exception_cause_out,
output logic [4:0] rs1_out,
output logic [4:0] rs2_out,
output logic [2:0] alu_op_out,
output logic alu_sub_sra_out,
output logic [1:0] alu_src1_out,
output logic [1:0] alu_src2_out,
output logic mem_read_out,
output logic mem_write_out,
output logic [1:0] mem_width_out,
output logic mem_zero_extend_out,
output logic mem_fence_out,
output logic csr_read_out,
output logic csr_write_out,
output logic [1:0] csr_write_op_out,
output logic csr_src_out,
output logic [1:0] branch_op_out,
output logic branch_pc_src_out,
output logic ecall_out,
output logic ebreak_out,
output logic mret_out,
output logic [4:0] rd_out,
output logic rd_write_out,
/* data out */
output logic [31:0] pc_out,
output logic [31:0] rs1_value_out,
output logic [31:0] rs2_value_out,
output logic [31:0] imm_value_out,
output logic [11:0] csr_out
);
logic [4:0] rs2;
logic [4:0] rs1;
logic [4:0] rd;
assign rs2 = instr_in[24:20];
assign rs1 = instr_in[19:15];
assign rd = instr_in[11:7];
assign rs1_unreg_out = rs1;
assign rs2_unreg_out = rs2;
rv32_regs regs (
.clk(clk),
.stall_in(stall_in),
.writeback_flush_in(writeback_flush_in),
/* control in */
.rs1_in(rs1),
.rs2_in(rs2),
.rd_in(rd_in),
.rd_write_in(rd_write_in),
/* data in */
.rd_value_in(rd_value_in),
/* data out */
.rs1_value_out(rs1_value_out),
.rs2_value_out(rs2_value_out)
);
logic valid;
logic rs1_read;
logic rs2_read;
logic [2:0] imm;
logic [2:0] alu_op;
logic alu_sub_sra;
logic [1:0] alu_src1;
logic [1:0] alu_src2;
logic mem_read;
logic mem_write;
logic [1:0] mem_width;
logic mem_zero_extend;
logic mem_fence;
logic csr_read;
logic csr_write;
logic [1:0] csr_write_op;
logic csr_src;
logic [1:0] branch_op;
logic branch_pc_src;
logic ecall;
logic ebreak;
logic mret;
logic rd_write;
assign rs1_read_unreg_out = rs1_read;
assign rs2_read_unreg_out = rs2_read;
assign mem_fence_unreg_out = mem_fence;
rv32_control_unit control_unit (
/* data in */
.instr_in(instr_in),
/* control in */
.rs1_in(rs1),
.rd_in(rd),
/* control out */
.valid_out(valid),
.rs1_read_out(rs1_read),
.rs2_read_out(rs2_read),
.imm_out(imm),
.alu_op_out(alu_op),
.alu_sub_sra_out(alu_sub_sra),
.alu_src1_out(alu_src1),
.alu_src2_out(alu_src2),
.mem_read_out(mem_read),
.mem_write_out(mem_write),
.mem_width_out(mem_width),
.mem_zero_extend_out(mem_zero_extend),
.mem_fence_out(mem_fence),
.csr_read_out(csr_read),
.csr_write_out(csr_write),
.csr_write_op_out(csr_write_op),
.csr_src_out(csr_src),
.branch_op_out(branch_op),
.branch_pc_src_out(branch_pc_src),
.ecall_out(ecall),
.ebreak_out(ebreak),
.mret_out(mret),
.rd_write_out(rd_write)
);
logic [31:0] imm_value;
rv32_imm_mux imm_mux (
/* control in */
.imm_in(imm),
/* data in */
.instr_in(instr_in),
/* data out */
.imm_value_out(imm_value)
);
logic [11:0] csr;
assign csr = instr_in[31:20];
always_ff @(posedge clk) begin
if (!stall_in) begin
`ifdef RISCV_FORMAL
intr_out <= intr_in;
next_pc_out <= next_pc_in;
instr_out <= instr_in;
`endif
branch_predicted_taken_out <= branch_predicted_taken_in;
valid_out <= valid_in && valid;
if (!exception_in && valid_in && !valid) begin
exception_out <= 1;
exception_cause_out <= `RV32_MCAUSE_INSTR_ILLEGAL_EXCEPTION;
end else begin
exception_out <= exception_in;
exception_cause_out <= exception_cause_in;
end
rs1_out <= rs1;
rs2_out <= rs2;
alu_op_out <= alu_op;
alu_sub_sra_out <= alu_sub_sra;
alu_src1_out <= alu_src1;
alu_src2_out <= alu_src2;
mem_read_out <= mem_read;
mem_write_out <= mem_write;
mem_width_out <= mem_width;
mem_zero_extend_out <= mem_zero_extend;
mem_fence_out <= mem_fence;
csr_read_out <= csr_read;
csr_write_out <= csr_write;
csr_write_op_out <= csr_write_op;
csr_src_out <= csr_src;
branch_op_out <= branch_op;
branch_pc_src_out <= branch_pc_src;
ecall_out <= ecall;
ebreak_out <= ebreak;
mret_out <= mret;
rd_out <= rd;
rd_write_out <= rd_write;
pc_out <= pc_in;
imm_value_out <= imm_value;
csr_out <= csr;
if (flush_in) begin
branch_predicted_taken_out <= 0;
valid_out <= 0;
exception_out <= 0;
mem_read_out <= 0;
mem_write_out <= 0;
csr_read_out <= 0;
csr_write_out <= 0;
branch_op_out <= `RV32_BRANCH_OP_NEVER;
ecall_out <= 0;
ebreak_out <= 0;
mret_out <= 0;
rd_write_out <= 0;
end
end
if (reset) begin
branch_predicted_taken_out <= 0;
valid_out <= 0;
exception_out <= 0;
rs1_out <= 0;
rs2_out <= 0;
alu_op_out <= 0;
alu_sub_sra_out <= 0;
alu_src1_out <= 0;
alu_src2_out <= 0;
mem_read_out <= 0;
mem_write_out <= 0;
mem_width_out <= 0;
mem_zero_extend_out <= 0;
mem_fence_out <= 0;
csr_read_out <= 0;
csr_write_out <= 0;
csr_write_op_out <= 0;
csr_src_out <= 0;
branch_op_out <= 0;
branch_pc_src_out <= 0;
ecall_out <= 0;
ebreak_out <= 0;
mret_out <= 0;
rd_out <= 0;
rd_write_out <= 0;
pc_out <= 0;
imm_value_out <= 0;
csr_out <= 0;
end
end
endmodule
`endif