-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcsr.sv
392 lines (364 loc) · 13.5 KB
/
csr.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
/**
* File : csr.sv
* License : MIT license <Check LICENSE>
* Author : Anderson Ignacio da Silva (aignacio) <anderson@aignacio.com>
* Date : 23.01.2022
* Last Modified Date: 22.05.2022
*/
module csr
import amba_axi_pkg::*;
import amba_ahb_pkg::*;
import nox_utils_pkg::*;
#(
parameter int SUPPORT_DEBUG = 1,
parameter int MTVEC_DEFAULT_VAL = 'h1000 // 4KB
)(
input clk,
input rst,
input stall_i,
input s_csr_t csr_i,
input rdata_t rs1_data_i,
input imm_t imm_i,
output rdata_t csr_rd_o,
// Interrupts [async trap] & Exceptions [sync trap]
input pc_t pc_addr_i,
input pc_t pc_lsu_i,
input s_irq_t irq_i,
input will_jump_i,
input eval_trap_i,
input s_trap_info_t dec_trap_i,
input s_trap_info_t instr_addr_mis_i,
input s_trap_info_t fetch_trap_i,
input ecall_i,
input ebreak_i,
input mret_i,
input wfi_i,
input s_trap_lsu_info_t lsu_trap_i,
output s_trap_info_t trap_o
);
typedef struct packed {
csr_t op;
logic rs1_is_x0;
imm_t imm;
rdata_t rs1;
rdata_t csr_rd;
rdata_t mask;
} s_wr_csr_t;
logic mcause_interrupt;
pc_t mtvec_base_addr;
logic mtvec_vectored;
rdata_t trap_offset;
logic dbg_irq_mtime;
logic dbg_irq_msoft;
logic dbg_irq_mext;
logic traps_can_happen_wo_exec;
mcause_int_t async_int;
rdata_ext_t csr_minstret_ff, next_minstret,
csr_cycle_ff, next_cycle,
csr_time_ff, next_time;
rdata_t csr_mstatus_ff, next_mstatus,
csr_mie_ff, next_mie,
csr_mtvec_ff, next_mtvec,
csr_mscratch_ff, next_mscratch,
csr_mepc_ff, next_mepc,
csr_mcause_ff, next_mcause,
csr_mtval_ff, next_mtval,
csr_mip_ff, next_mip;
s_wr_csr_t csr_wr_args;
s_trap_info_t trap_ff, next_trap;
logic [2:0] irq_vec;
function automatic rdata_t wr_csr_val(s_wr_csr_t wr_arg);
rdata_t wr_val;
case (wr_arg.op)
RV_CSR_RW: wr_val = wr_arg.rs1;
RV_CSR_RS: wr_val = wr_arg.csr_rd | wr_arg.rs1;
RV_CSR_RC: wr_val = wr_arg.csr_rd & ~wr_arg.rs1;
RV_CSR_RWI: wr_val = wr_arg.imm;
RV_CSR_RSI: wr_val = wr_arg.csr_rd | wr_arg.imm;
RV_CSR_RCI: wr_val = wr_arg.csr_rd & ~wr_arg.imm;
default: wr_val = wr_arg.csr_rd;
endcase
if ((wr_arg.op != RV_CSR_RW) && (wr_arg.op != RV_CSR_RWI)) begin
wr_val = wr_arg.rs1_is_x0 ? wr_arg.csr_rd : wr_val;
end
wr_val = (wr_val & wr_arg.mask);
wr_val = stall_i ? wr_arg.csr_rd : wr_val;
return wr_val;
endfunction
always_comb begin : rd_wr_csr
// Output is combo cause there's a mux in the exe stg
csr_rd_o = rdata_t'('0);
//next_minstret = csr_minstret_ff;
next_cycle = csr_cycle_ff + 'd1;
next_time = csr_time_ff;
next_mstatus = csr_mstatus_ff;
next_mie = csr_mie_ff;
next_mtvec = csr_mtvec_ff;
next_mscratch = csr_mscratch_ff;
next_mepc = csr_mepc_ff;
next_mcause = csr_mcause_ff;
next_mtval = csr_mtval_ff;
next_mip = csr_mip_ff;
csr_wr_args.op = csr_i.op;
csr_wr_args.rs1_is_x0 = csr_i.rs1_is_x0;
csr_wr_args.imm = imm_i;
csr_wr_args.rs1 = rs1_data_i;
csr_wr_args.csr_rd = '0;
csr_wr_args.mask = '1;
case(csr_i.addr)
RV_CSR_MSTATUS: begin
csr_wr_args.mask = 'h807FF9BB;
csr_rd_o = csr_mstatus_ff;
csr_wr_args.csr_rd = csr_rd_o;
next_mstatus = wr_csr_val(csr_wr_args);
end
RV_CSR_MIE: begin
csr_wr_args.mask = 'h888;
csr_rd_o = csr_mie_ff;
csr_wr_args.csr_rd = csr_rd_o;
next_mie = wr_csr_val(csr_wr_args);
end
RV_CSR_MTVEC: begin
csr_wr_args.mask = 'hFFFF_FFFD;
csr_rd_o = csr_mtvec_ff;
csr_wr_args.csr_rd = csr_rd_o;
next_mtvec = wr_csr_val(csr_wr_args);
end
RV_CSR_MSCRATCH: begin
csr_rd_o = csr_mscratch_ff;
csr_wr_args.csr_rd = csr_rd_o;
next_mscratch = wr_csr_val(csr_wr_args);
end
RV_CSR_MEPC: begin
csr_wr_args.mask = 'hFFFF_FFFC;
csr_rd_o = csr_mepc_ff;
csr_wr_args.csr_rd = csr_rd_o;
next_mepc = wr_csr_val(csr_wr_args);
end
RV_CSR_MCAUSE: begin
csr_rd_o = csr_mcause_ff;
end
RV_CSR_MTVAL: begin
csr_rd_o = csr_mtval_ff;
csr_wr_args.csr_rd = csr_rd_o;
next_mtval = wr_csr_val(csr_wr_args);
end
RV_CSR_MIP: begin
csr_wr_args.mask = 'h8;
csr_rd_o = csr_mip_ff;
csr_wr_args.csr_rd = csr_rd_o;
next_mip = wr_csr_val(csr_wr_args);
end
//RV_CSR_MCYCLE: csr_rd_o = csr_cycle_ff[31:0];
//RV_CSR_MCYCLEH: csr_rd_o = csr_cycle_ff[63:32];
//RV_CSR_MINSTRET: csr_rd_o = csr_minstret_ff[31:0];
//RV_CSR_MINSTRETH: csr_rd_o = csr_minstret_ff[63:32];
RV_CSR_CYCLE: csr_rd_o = csr_cycle_ff[31:0];
RV_CSR_CYCLEH: csr_rd_o = csr_cycle_ff[63:32];
//RV_CSR_INSTRET: csr_rd_o = csr_minstret_ff[31:0];
//RV_CSR_INSTRETH: csr_rd_o = csr_minstret_ff[63:32];
//RV_CSR_TIME: csr_rd_o = csr_time_ff[31:0];
//RV_CSR_TIMEH: csr_rd_o = csr_time_ff[63:32];
RV_CSR_MISA: csr_rd_o = `M_ISA_ID;
//RV_CSR_MVENDORID: csr_rd_o = `M_VENDOR_ID;
//RV_CSR_MARCHID: csr_rd_o = `M_ARCH_ID;
//RV_CSR_MIMPLID: csr_rd_o = `M_IMPL_ID;
RV_CSR_MHARTID: csr_rd_o = `M_HART_ID;
default: csr_rd_o = rdata_t'('0);
endcase
next_trap = s_trap_info_t'('0);
dbg_irq_mtime = 'b0;
dbg_irq_msoft = 'b0;
dbg_irq_mext = 'b0;
// Trap control
// Priority decoder:
// 1) IRQS [async traps]
// 2) Exceptions [sync traps]
// Acording to the priv. spec, these two bits needs to be
// cleared by HW, mtip (mtimer) as soon as we reflect mtimecmp change
// and meip (external) as soon as the IRQ controller turns it off
next_mip[`RV_MIE_MTIP] = irq_i.timer_irq;
next_mip[`RV_MIE_MEIP] = irq_i.ext_irq;
priority case(1)
(csr_mstatus_ff[`RV_MST_MIE] &&
irq_i.ext_irq &&
csr_mie_ff[`RV_MIE_MEIP]): begin
//next_mip[`RV_MIE_MEIP] = 'b1;
next_mepc = pc_addr_i;
next_mcause = 'h8000_000B;
next_mtval = rdata_t'('h0);
next_trap.active = 'b1;
dbg_irq_mext = 'b1;
end
(csr_mstatus_ff[`RV_MST_MIE] &&
irq_i.sw_irq &&
csr_mie_ff[`RV_MIE_MSIP]): begin
next_mip[`RV_MIE_MSIP] = 'b1;
next_mepc = pc_addr_i;
next_mcause = 'h8000_0003;
next_mtval = rdata_t'('h0);
next_trap.active = 'b1;
dbg_irq_msoft = 'b1;
end
(csr_mstatus_ff[`RV_MST_MIE] &&
irq_i.timer_irq &&
csr_mie_ff[`RV_MIE_MTIP]): begin
//next_mip[`RV_MIE_MTIP] = 'b1;
next_mepc = pc_addr_i;
next_mcause = 'h8000_0007;
next_mtval = rdata_t'('h0);
next_trap.active = 'b1;
dbg_irq_mtime = 'b1;
end
fetch_trap_i.active: begin // TODO: test this feature
next_mepc = pc_addr_i;
next_mcause = 'd1;
next_mtval = fetch_trap_i.mtval;
next_trap.active = 'b1;
end
(dec_trap_i.active && ~will_jump_i): begin
next_mepc = dec_trap_i.pc_addr;
next_mcause = 'd2;
next_mtval = dec_trap_i.mtval;
next_trap.active = 'b1;
end
instr_addr_mis_i.active: begin
next_mepc = pc_addr_i;
next_mcause = 'd0;
next_mtval = instr_addr_mis_i.mtval;
next_trap.active = 'b1;
end
ecall_i: begin
next_mepc = pc_addr_i;
next_mcause = 'd11;
next_mtval = rdata_t'('h0);
next_trap.active = 'b1;
end
ebreak_i: begin
next_mepc = pc_addr_i;
next_mcause = 'd3;
//next_mtval = pc_addr_i;
next_mtval = '0;
next_trap.active = 'b1;
end
mret_i: begin // Fake trap to return program
next_mtval = rdata_t'('h0);
next_trap.active = 'b1;
end
lsu_trap_i.ld_mis.active: begin // TODO: test this feature
next_mepc = pc_lsu_i;
next_mcause = 'd4;
next_mtval = pc_lsu_i;
next_trap.active = 'b1;
end
lsu_trap_i.ld.active: begin // TODO: test this feature
next_mepc = pc_lsu_i;
next_mcause = 'd5;
next_mtval = pc_lsu_i;
next_trap.active = 'b1;
end
lsu_trap_i.st_mis.active: begin // TODO: test this feature
next_mepc = pc_lsu_i;
next_mcause = 'd6;
next_mtval = pc_lsu_i;
next_trap.active = 'b1;
end
lsu_trap_i.st.active: begin // TODO: test this feature
next_mepc = pc_lsu_i;
next_mcause = 'd7;
next_mtval = pc_lsu_i;
next_trap.active = 'b1;
end
// Added the below statement due to error while synthesizing on vivado
default: next_trap = s_trap_info_t'('0);
endcase
irq_vec = {dbg_irq_mtime, dbg_irq_msoft, dbg_irq_mtime};
// In case we have one of the following traps
// we don't need to wait till it's in the exec
// stage to evaluate
traps_can_happen_wo_exec = (fetch_trap_i.active ||
lsu_trap_i.st.active ||
lsu_trap_i.ld.active ||
lsu_trap_i.st_mis.active ||
lsu_trap_i.ld_mis.active);
if (~traps_can_happen_wo_exec) begin
if (~eval_trap_i && ~wfi_i) begin
next_trap.active = 'b0;
end
end
// Define trap address
mtvec_base_addr = {csr_mtvec_ff[31:2],2'h0};
mtvec_vectored = csr_mtvec_ff[0];
mcause_interrupt = next_mcause[31];
async_int = mcause_int_t'(next_mcause[3:0]);
trap_offset = 'h0;
next_trap.pc_addr = mtvec_base_addr;
// Vectored mode and MCAUSE is async interrupt
if (mtvec_vectored && mcause_interrupt) begin
case(async_int)
RV_M_SW_INT: trap_offset = 'h0c;
RV_M_TIMER_INT: trap_offset = 'h1c;
RV_M_EXT_INT: trap_offset = 'h2c;
default: trap_offset = 'h0;
endcase
end
if (next_trap.active && ~mret_i) begin
// bkp mstatus[MIE]
//To support nested traps, each privilege mode x has a two-level stack of interrupt-enable bits and privilege modes.
//xPIE holds the value of the interrupt-enable bit active prior to the trap, and x PP holds the previous privilege mode.
//The x PP fields can only hold privilege modes up to x, so MPP is two bits wide, SPP is one bit wide, and
//UPP is implicitly zero. When a trap is taken from privilege mode y into privilege mode x, xPIE is set to the value
//of xIE; xIE is set to 0; and xPP is set to y.
next_mstatus[`RV_MST_MPIE] = csr_mstatus_ff[`RV_MST_MIE];
next_mstatus[`RV_MST_MIE] = 'b0;
if (wfi_i && (|irq_vec)) begin
// In this case, ISA says:
//...If an enabled interrupt is present or later becomes present while the hart is stalled, the interrupt exception
//will be taken on the following instruction, i.e., execution resumes in the trap handler and mepc = pc + 4.
next_mepc = next_mepc + 'd4;
end
end
if (mret_i) begin
next_trap.pc_addr = csr_mepc_ff;
//The MRET, SRET, or URET instructions are used to return from traps in M-mode, S-mode, or U-mode respectively.
//When executing an x RET instruction, supposing x PP holds the value y, x IE is set to xPIE; the privilege mode
//is changed to y; xPIE is set to 1; and xPP is set to U (or M if user-mode is not supported).
next_mstatus[`RV_MST_MIE] = csr_mstatus_ff[`RV_MST_MPIE];
next_mstatus[`RV_MST_MPIE] = 'b1;
end
else begin
next_trap.pc_addr = mtvec_base_addr+trap_offset;
end
trap_o = trap_ff;
end
`CLK_PROC(clk, rst) begin
`RST_TYPE(rst) begin
csr_mstatus_ff <= 'h1880;
csr_mie_ff <= `OP_RST_L;
csr_mtvec_ff <= MTVEC_DEFAULT_VAL;
csr_mscratch_ff <= `OP_RST_L;
csr_mepc_ff <= `OP_RST_L;
csr_mcause_ff <= `OP_RST_L;
csr_mtval_ff <= `OP_RST_L;
csr_mip_ff <= `OP_RST_L;
csr_cycle_ff <= `OP_RST_L;
//csr_time_ff <= `OP_RST_L;
//csr_minstret_ff <= `OP_RST_L;
trap_ff <= `OP_RST_L;
end
else begin
csr_mstatus_ff <= next_mstatus;
csr_mie_ff <= next_mie;
csr_mtvec_ff <= next_mtvec;
csr_mscratch_ff <= next_mscratch;
csr_mepc_ff <= next_mepc;
csr_mcause_ff <= next_mcause;
csr_mtval_ff <= next_mtval;
csr_mip_ff <= next_mip;
csr_cycle_ff <= next_cycle;
//csr_time_ff <= next_time;
//csr_minstret_ff <= next_minstret;
trap_ff <= next_trap;
end
end
endmodule