forked from grahamedgecombe/icicle
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrv32_mem.sv
395 lines (348 loc) · 11.9 KB
/
rv32_mem.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
`ifndef RV32_MEM
`define RV32_MEM
`include "rv32_branch.sv"
`include "rv32_csrs.sv"
`define RV32_MEM_WIDTH_WORD 2'b00
`define RV32_MEM_WIDTH_HALF 2'b01
`define RV32_MEM_WIDTH_BYTE 2'b10
module rv32_mem (
input clk,
input reset,
`ifdef RISCV_FORMAL
/* debug control in */
input intr_in,
input [4:0] rs1_in,
input [4:0] rs2_in,
/* debug data in */
input [31:0] next_pc_in,
input [31:0] instr_in,
/* debug control out */
output logic intr_out,
output logic trap_out,
output logic [4:0] rs1_out,
output logic [4:0] rs2_out,
output logic [3:0] read_mask_out,
output logic [3:0] write_mask_out,
/* debug data out */
output logic [31:0] pc_out,
output logic [31:0] next_pc_out,
output logic [31:0] instr_out,
output logic [31:0] rs1_value_out,
output logic [31:0] rs2_value_out,
output logic [31:0] address_out,
output logic [31:0] read_value_out,
output logic [31:0] write_value_out,
`endif
/* control in (from hazard) */
input stall_in,
input flush_in,
input writeback_flush_in,
/* control in */
input branch_predicted_taken_in,
input branch_misaligned_in,
input valid_in,
input exception_in,
input [3:0] exception_cause_in,
input read_in,
input write_in,
input [1:0] width_in,
input zero_extend_in,
input csr_read_in,
input csr_write_in,
input [1:0] csr_write_op_in,
input csr_src_in,
input [1:0] branch_op_in,
input ecall_in,
input ebreak_in,
input mret_in,
input [4:0] rd_in,
input rd_write_in,
/* control in (from data memory bus) */
input data_fault_in,
/* data in */
input [31:0] pc_in,
input [31:0] result_in,
input [31:0] rs1_value_in,
input [31:0] rs2_value_in,
input [31:0] imm_value_in,
input [31:0] branch_pc_in,
input [11:0] csr_in,
/* data in (from data memory bus) */
input [31:0] data_read_value_in,
/* control out */
output logic valid_out,
output logic trap_unreg_out,
output logic branch_mispredicted_out,
output logic [4:0] rd_out,
output logic rd_write_out,
/* control out (to data memory bus) */
output logic data_read_out,
output logic data_write_out,
output logic [3:0] data_write_mask_out,
/* data out */
output logic [31:0] rd_value_out,
output logic [31:0] trap_pc_out,
output logic [31:0] branch_pc_out,
/* data out (to data memory bus) */
output logic [31:0] data_address_out,
output logic [31:0] data_write_value_out,
/* data out (to timer) */
output logic [63:0] cycle_out
);
logic branch_mispredicted;
logic branch_taken;
/* branch unit */
rv32_branch_unit branch_unit (
/* control in */
.predicted_taken_in(branch_predicted_taken_in),
.op_in(branch_op_in),
/* data in */
.result_in(result_in),
/* control out */
.taken_out(branch_taken),
.mispredicted_out(branch_mispredicted)
);
assign branch_pc_out = branch_pc_in;
assign branch_mispredicted_out = branch_mispredicted && !flush_in;
/* memory access unit */
logic mem_misaligned;
logic [31:0] read_value;
logic [3:0] read_mask;
assign data_read_out = read_in && !mem_misaligned;
assign data_write_out = write_in && !mem_misaligned;
assign data_address_out = {result_in[31:2], 2'b0};
always_comb begin
/* alignment check */
case (width_in)
`RV32_MEM_WIDTH_WORD: mem_misaligned = result_in[1:0] != 0;
`RV32_MEM_WIDTH_HALF: mem_misaligned = result_in[0] != 0;
`RV32_MEM_WIDTH_BYTE: mem_misaligned = 0;
default: mem_misaligned = 1'bx;
endcase
/* write port */
if (write_in) begin
case (width_in)
`RV32_MEM_WIDTH_WORD: begin
data_write_value_out = rs2_value_in;
data_write_mask_out = 4'b1111;
end
`RV32_MEM_WIDTH_HALF: begin
case (result_in[1])
1'b0: begin
data_write_value_out = {16'bx, rs2_value_in[15:0]};
data_write_mask_out = 4'b0011;
end
1'b1: begin
data_write_value_out = {rs2_value_in[15:0], 16'bx};
data_write_mask_out = 4'b1100;
end
endcase
end
`RV32_MEM_WIDTH_BYTE: begin
case (result_in[1:0])
2'b00: begin
data_write_value_out = {24'bx, rs2_value_in[7:0]};
data_write_mask_out = 4'b0001;
end
2'b01: begin
data_write_value_out = {16'bx, rs2_value_in[7:0], 8'bx};
data_write_mask_out = 4'b0010;
end
2'b10: begin
data_write_value_out = {8'bx, rs2_value_in[7:0], 16'bx};
data_write_mask_out = 4'b0100;
end
2'b11: begin
data_write_value_out = {rs2_value_in[7:0], 24'bx};
data_write_mask_out = 4'b1000;
end
endcase
end
default: begin
data_write_value_out = 32'bx;
data_write_mask_out = 4'bx;
end
endcase
end else begin
data_write_value_out = 32'bx;
data_write_mask_out = 4'b0;
end
/* read port */
if (read_in) begin
case (width_in)
`RV32_MEM_WIDTH_WORD: begin
read_value = data_read_value_in;
read_mask = 4'b1111;
end
`RV32_MEM_WIDTH_HALF: begin
case (result_in[1])
1'b0: begin
read_value = {{16{zero_extend_in ? 1'b0 : data_read_value_in[15]}}, data_read_value_in[15:0]};
read_mask = 4'b0011;
end
1'b1: begin
read_value = {{16{zero_extend_in ? 1'b0 : data_read_value_in[31]}}, data_read_value_in[31:16]};
read_mask = 4'b1100;
end
endcase
end
`RV32_MEM_WIDTH_BYTE: begin
case (result_in[1:0])
2'b00: begin
read_value = {{24{zero_extend_in ? 1'b0 : data_read_value_in[7]}}, data_read_value_in[7:0]};
read_mask = 4'b0001;
end
2'b01: begin
read_value = {{24{zero_extend_in ? 1'b0 : data_read_value_in[15]}}, data_read_value_in[15:8]};
read_mask = 4'b0010;
end
2'b10: begin
read_value = {{24{zero_extend_in ? 1'b0 : data_read_value_in[23]}}, data_read_value_in[23:16]};
read_mask = 4'b0100;
end
2'b11: begin
read_value = {{24{zero_extend_in ? 1'b0 : data_read_value_in[31]}}, data_read_value_in[31:24]};
read_mask = 4'b1000;
end
endcase
end
default: begin
read_value = 32'bx;
read_mask = 4'bx;
end
endcase
end else begin
read_value = 32'bx;
read_mask = 4'b0;
end
end
/* traps */
logic exception;
logic [3:0] exception_cause;
logic mem_exception;
always_comb begin
exception = 0;
exception_cause = 4'bx;
mem_exception = 0;
if (exception_in) begin
exception = 1;
exception_cause = exception_cause_in;
end else if (branch_taken && branch_misaligned_in) begin
exception = 1;
exception_cause = `RV32_MCAUSE_INSTR_MISALIGNED_EXCEPTION;
mem_exception = 1;
end else if (read_in && mem_misaligned) begin
exception = 1;
exception_cause = `RV32_MCAUSE_LOAD_MISALIGNED_EXCEPTION;
mem_exception = 1;
end else if (write_in && mem_misaligned) begin
exception = 1;
exception_cause = `RV32_MCAUSE_STORE_MISALIGNED_EXCEPTION;
mem_exception = 1;
end else if (read_in && data_fault_in) begin
exception = 1;
exception_cause = `RV32_MCAUSE_LOAD_FAULT_EXCEPTION;
mem_exception = 1;
end else if (write_in && data_fault_in) begin
exception = 1;
exception_cause = `RV32_MCAUSE_STORE_FAULT_EXCEPTION;
mem_exception = 1;
end else if (ecall_in) begin
exception = 1;
exception_cause = `RV32_MCAUSE_MACHINE_ECALL_EXCEPTION;
end else if (ebreak_in) begin
exception = 1;
exception_cause = `RV32_MCAUSE_BREAKPOINT_EXCEPTION;
end
end
/* csr file */
logic [31:0] csr_read_value;
logic [63:0] cycle;
rv32_csrs csrs (
.clk(clk),
.reset(reset),
.stall_in(stall_in),
.flush_in(flush_in),
.writeback_flush_in(writeback_flush_in),
/* control in */
.exception_in(exception),
.exception_cause_in(exception_cause),
.read_in(csr_read_in),
.write_in(csr_write_in),
.write_op_in(csr_write_op_in),
.src_in(csr_src_in),
.mret_in(mret_in),
/* control in (from writeback) */
.instr_retired_in(valid_out),
/* data in */
.pc_in(pc_in),
.rs1_value_in(rs1_value_in),
.imm_value_in(imm_value_in),
.csr_in(csr_in),
/* control out */
.trap_out(trap_unreg_out),
/* data out */
.read_value_out(csr_read_value),
.trap_pc_out(trap_pc_out),
/* data out (to timer) */
.cycle_out(cycle_out)
);
logic [31:0] next_pc;
`ifdef RISCV_FORMAL
always_comb begin
if (trap_unreg_out)
next_pc = trap_pc_out;
else if (branch_mispredicted)
next_pc = branch_pc_in;
else
next_pc = next_pc_in;
end
`endif
always_ff @(posedge clk) begin
if (!stall_in) begin
`ifdef RISCV_FORMAL
intr_out <= intr_in;
pc_out <= pc_in;
next_pc_out <= next_pc;
trap_out <= trap_unreg_out;
rs1_out <= rs1_in;
rs2_out <= rs2_in;
instr_out <= instr_in;
rs1_value_out <= rs1_value_in;
rs2_value_out <= rs2_value_in;
address_out <= data_address_out;
read_mask_out <= read_mask;
write_mask_out <= data_write_mask_out;
read_value_out <= data_read_value_in;
write_value_out <= data_write_value_out;
`endif
valid_out <= valid_in;
rd_out <= rd_in;
rd_write_out <= rd_write_in;
if (read_in)
rd_value_out <= read_value;
else if (csr_read_in)
rd_value_out <= csr_read_value;
else
rd_value_out <= result_in;
`ifdef RISCV_FORMAL
if (flush_in)
trap_out <= 0;
`endif
if (flush_in || mem_exception) begin
valid_out <= 0;
rd_write_out <= 0;
end
end
if (reset) begin
`ifdef RISCV_FORMAL
trap_out <= 0;
`endif
valid_out <= 0;
rd_out <= 0;
rd_write_out <= 0;
rd_value_out <= 0;
end
end
endmodule
`endif