forked from openhwgroup/cv32e40x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cv32e40x_cs_registers.sv
1909 lines (1608 loc) · 75 KB
/
cv32e40x_cs_registers.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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: Sven Stucki - svstucki@student.ethz.ch //
// //
// Additional contributions by: //
// Andreas Traber - atraber@iis.ee.ethz.ch //
// Michael Gautschi - gautschi@iis.ee.ethz.ch //
// Davide Schiavone - pschiavo@iis.ee.ethz.ch //
// Andrea Bettati - andrea.bettati@studenti.unipr.it //
// Øystein Knauserud - oystein.knauserud@silabs.com //
// Øivind Ekelund - oivind.ekelund@silabs.com //
// //
// Design Name: Control and Status Registers //
// Project Name: RI5CY //
// Language: SystemVerilog //
// //
// Description: Control and Status Registers (CSRs) //
// //
////////////////////////////////////////////////////////////////////////////////
module cv32e40x_cs_registers import cv32e40x_pkg::*;
#(
parameter rv32_e RV32 = RV32I,
parameter a_ext_e A_EXT = A_NONE,
parameter m_ext_e M_EXT = M,
parameter bit X_EXT = 0,
parameter logic [31:0] X_MISA = 32'h00000000,
parameter logic [1:0] X_ECS_XS = 2'b00, // todo:XIF implement related mstatus bitfields (but only if X_EXT = 1)
parameter bit ZC_EXT = 0,
parameter bit CLIC = 0,
parameter int unsigned CLIC_ID_WIDTH = 5,
parameter int unsigned NUM_MHPMCOUNTERS = 1,
parameter bit DEBUG = 1,
parameter int DBG_NUM_TRIGGERS = 1, // todo: implement support for DBG_NUM_TRIGGERS != 1
parameter int unsigned MTVT_ADDR_WIDTH = 26
)
(
// Clock and Reset
input logic clk,
input logic rst_n,
// Configuration
input logic [31:0] mhartid_i,
input logic [3:0] mimpid_patch_i,
input logic [31:0] mtvec_addr_i,
input logic csr_mtvec_init_i,
// CSRs
output dcsr_t dcsr_o,
output logic [31:0] dpc_o,
output logic [JVT_ADDR_WIDTH-1:0] jvt_addr_o,
output logic [5:0] jvt_mode_o,
output mcause_t mcause_o,
output logic [63:0] mcycle_o,
output logic [31:0] mepc_o,
output logic [31:0] mie_o,
output mintstatus_t mintstatus_o,
output logic [7:0] mintthresh_th_o,
output mstatus_t mstatus_o,
output logic [24:0] mtvec_addr_o,
output logic [1:0] mtvec_mode_o,
output logic [MTVT_ADDR_WIDTH-1:0] mtvt_addr_o,
output privlvl_t priv_lvl_o,
// ID/EX pipeline
input id_ex_pipe_t id_ex_pipe_i,
output logic csr_illegal_o,
// EX/WB pipeline
input ex_wb_pipe_t ex_wb_pipe_i,
// From controller_fsm
input ctrl_fsm_t ctrl_fsm_i,
// To controller_bypass
output logic csr_counter_read_o,
output logic csr_mnxti_read_o,
// Interface to CSRs (SRAM like)
output logic [31:0] csr_rdata_o,
// Interrupts
input logic [31:0] mip_i,
input logic mnxti_irq_pending_i,
input logic [CLIC_ID_WIDTH-1:0] mnxti_irq_id_i,
input logic [7:0] mnxti_irq_level_i,
output logic clic_pa_valid_o, // CSR read data is an address to a function pointer
output logic [31:0] clic_pa_o, // Address to CLIC function pointer
output logic csr_irq_enable_write_o, // An irq enable write is being performed in WB
// Time input
input logic [63:0] time_i,
// CSR write strobes
output logic csr_wr_in_wb_flush_o,
// Debug
input logic [31:0] pc_if_i,
input logic ptr_in_if_i,
input privlvl_t priv_lvl_if_i,
output logic [31:0] trigger_match_if_o,
output logic [31:0] trigger_match_ex_o,
output logic etrigger_wb_o,
input logic lsu_valid_ex_i,
input logic [31:0] lsu_addr_ex_i,
input logic lsu_we_ex_i,
input logic [3:0] lsu_be_ex_i,
input lsu_atomic_e lsu_atomic_ex_i
);
localparam logic [31:0] CORE_MISA =
(32'(A_EXT == A) << 0) | // A - Atomic Instructions extension
(32'(1) << 2) | // C - Compressed extension
(32'(RV32 == RV32E) << 4) | // E - RV32E/64E base ISA
(32'(RV32 == RV32I) << 8) | // I - RV32I/64I/128I base ISA
(32'(M_EXT == M) << 12) | // M - Integer Multiply/Divide extension
(32'(0) << 20) | // U - User mode implemented
(32'(1) << 23) | // X - Non-standard extensions present
(32'(MXL) << 30); // M-XLEN
localparam logic [31:0] MISA_VALUE = CORE_MISA | (X_EXT ? X_MISA : 32'h0000_0000);
localparam logic [31:0] CSR_MTVT_MASK = {{MTVT_ADDR_WIDTH{1'b1}}, {(32-MTVT_ADDR_WIDTH){1'b0}}};
// CSR update logic
logic [31:0] csr_wdata_int;
logic [31:0] csr_rdata_int;
logic csr_we_int;
csr_opcode_e csr_op;
csr_num_e csr_waddr;
csr_num_e csr_raddr;
logic [31:0] csr_wdata;
logic csr_en_gated;
logic illegal_csr_read; // Current CSR cannot be read
logic illegal_csr_write; // Current CSR cannot be written
logic instr_valid; // Local instr_valid
logic unused_signals;
// Interrupt control signals
logic [31:0] mepc_q, mepc_n, mepc_rdata;
logic mepc_we;
// Trigger
// Trigger CSR write enables are decoded in cs_registers, all other (WARL behavior, write data and trigger matches)
// are handled within cv32e40x_debug_triggers
logic [31:0] tselect_rdata;
logic tselect_we;
logic [31:0] tdata1_rdata;
logic tdata1_we;
logic [31:0] tdata2_rdata;
logic tdata2_we;
logic [31:0] tinfo_rdata;
logic tinfo_we;
// Debug
dcsr_t dcsr_q, dcsr_n, dcsr_rdata;
logic dcsr_we;
logic [31:0] dpc_q, dpc_n, dpc_rdata;
logic dpc_we;
logic [31:0] dscratch0_q, dscratch0_n, dscratch0_rdata;
logic dscratch0_we;
logic [31:0] dscratch1_q, dscratch1_n, dscratch1_rdata;
logic dscratch1_we;
logic [31:0] mscratch_q, mscratch_n, mscratch_rdata;
logic mscratch_we;
jvt_t jvt_q, jvt_n, jvt_rdata;
logic jvt_we;
mstatus_t mstatus_q, mstatus_n, mstatus_rdata;
logic mstatus_we;
logic [31:0] mstatush_n, mstatush_rdata; // No CSR module instance
logic mstatush_we; // Not used in RTL (used by RVFI)
logic [31:0] misa_n, misa_rdata; // No CSR module instance
logic misa_we; // Not used in RTL (used by RVFI)
mcause_t mcause_q, mcause_n, mcause_rdata;
logic mcause_we;
mtvec_t mtvec_q, mtvec_n, mtvec_rdata;
logic mtvec_we;
mtvt_t mtvt_q, mtvt_n, mtvt_rdata;
logic mtvt_we;
logic [31:0] mnxti_n, mnxti_rdata; // No CSR module instance
logic mnxti_we;
mintstatus_t mintstatus_q, mintstatus_n, mintstatus_rdata;
logic mintstatus_we;
logic [31:0] mintthresh_q, mintthresh_n, mintthresh_rdata;
logic mintthresh_we;
logic [31:0] mscratchcsw_n, mscratchcsw_rdata;
logic mscratchcsw_we;
logic [31:0] mscratchcswl_n, mscratchcswl_rdata;
logic mscratchcswl_we;
logic [31:0] mip_n, mip_rdata; // No CSR module instance
logic mip_we; // Not used in RTL (used by RVFI)
logic [31:0] mie_q, mie_n, mie_rdata; // Bits are masked according to IRQ_MASK
logic mie_we;
logic [31:0] mvendorid_n, mvendorid_rdata; // No CSR module instance
logic mvendorid_we; // Always 0 (MRO), not used in RTL (used by RVFI)
logic [31:0] marchid_n, marchid_rdata; // No CSR module instance
logic marchid_we; // Always 0 (MRO), not used in RTL (used by RVFI)
logic [31:0] mimpid_n, mimpid_rdata; // No CSR module instance
logic mimpid_we; // Always 0 (MRO), not used in RTL (used by RVFI)
logic [31:0] mhartid_n, mhartid_rdata; // No CSR module instance
logic mhartid_we; // Always 0 (MRO), not used in RTL (used by RVFI)
logic [31:0] mconfigptr_n, mconfigptr_rdata; // No CSR module instance
logic mconfigptr_we; // Always 0 (MRO), not used in RTL (used by RVFI)
logic [31:0] mtval_n, mtval_rdata; // No CSR module instance
logic mtval_we; // Not used in RTL (used by RVFI)
privlvl_t priv_lvl_n, priv_lvl_q, priv_lvl_rdata; // todo: Use the priv_* signals as much as possible as in the 40S
logic priv_lvl_we;
logic [1:0] priv_lvl_q_int;
// Detect JVT writes (requires pipeline flush)
logic csr_wr_in_wb;
logic jvt_wr_in_wb;
// Special we signal for aliased writes between mstatus and mcause.
// Only used for explicit writes to either mcause or mstatus, to signal that
// mpp and mpie of the aliased CSR should be written.
// All implicit writes (upon taking exceptions etc) handle the aliasing by also
// writing mcause.mpp/mpie to mstatus and vice versa.
logic mcause_alias_we;
logic mstatus_alias_we;
// Performance Counter Signals
logic [31:0] [63:0] mhpmcounter_q; // Performance counters
logic [31:0] [63:0] mhpmcounter_n; // Performance counters next value
logic [31:0] [63:0] mhpmcounter_rdata; // Performance counters next value
logic [31:0] [1:0] mhpmcounter_we; // Performance counters write enable
logic [31:0] [31:0] mhpmevent_q, mhpmevent_n, mhpmevent_rdata; // Event enable
logic [31:0] mcountinhibit_q, mcountinhibit_n, mcountinhibit_rdata; // Performance counter inhibit
logic [NUM_HPM_EVENTS-1:0] hpm_events; // Events for performance counters
logic [31:0] [63:0] mhpmcounter_increment; // Increment of mhpmcounter_q
logic [31:0] mhpmcounter_write_lower; // Write 32 lower bits of mhpmcounter_q
logic [31:0] mhpmcounter_write_upper; // Write 32 upper bits mhpmcounter_q
logic [31:0] mhpmcounter_write_increment; // Write increment of mhpmcounter_q
// Signal used for RVFI to set rmask, not used internally
logic mscratchcsw_in_wb;
logic mscratchcswl_in_wb;
logic mnxti_in_wb;
// Local instr_valid for write portion (WB)
// Not factoring in ctrl_fsm_i.halt_limited_wb. This signal is only set during SLEEP mode, and while in SLEEP
// there cannot be any CSR instruction in WB.
assign instr_valid = ex_wb_pipe_i.instr_valid && !ctrl_fsm_i.kill_wb && !ctrl_fsm_i.halt_wb;
// CSR access. Read in EX, write in WB
// Setting csr_raddr to zero in case of unused csr to save power (alu_operand_b toggles a lot)
assign csr_raddr = csr_num_e'((id_ex_pipe_i.csr_en && id_ex_pipe_i.instr_valid) ? id_ex_pipe_i.alu_operand_b[11:0] : 12'b0);
// Not suppressing csr_waddr to zero when unused since its source are dedicated flipflops and would not save power as for raddr
assign csr_waddr = csr_num_e'(ex_wb_pipe_i.csr_addr);
assign csr_wdata = ex_wb_pipe_i.csr_wdata;
assign csr_op = ex_wb_pipe_i.csr_op;
// CSR write operations in WB, actual csr_we_int may still become 1'b0 in case of CSR_OP_READ
assign csr_en_gated = ex_wb_pipe_i.csr_en && instr_valid;
////////////////////////////////////////
// Determine if CSR access is illegal //
// Both read and write validity is //
// checked in the first (EX) stage //
// Invalid writes will suppress ex_wb //
// signals and avoid writing in WB //
////////////////////////////////////////
assign illegal_csr_write = (id_ex_pipe_i.csr_op != CSR_OP_READ) &&
(id_ex_pipe_i.csr_en) &&
(csr_raddr[11:10] == 2'b11); // Priv spec section 2.1
assign csr_illegal_o = (id_ex_pipe_i.instr_valid && id_ex_pipe_i.csr_en) ? illegal_csr_write || illegal_csr_read : 1'b0;
////////////////////////////////////////////
// ____ ____ ____ ____ //
// / ___/ ___|| _ \ | _ \ ___ __ _ //
// | | \___ \| |_) | | |_) / _ \/ _` | //
// | |___ ___) | _ < | _ < __/ (_| | //
// \____|____/|_| \_\ |_| \_\___|\__, | //
// |___/ //
////////////////////////////////////////////
////////////////////////////////////////////
// CSR read logic
always_comb
begin
illegal_csr_read = 1'b0;
csr_counter_read_o = 1'b0;
csr_mnxti_read_o = 1'b0;
case (csr_raddr)
// jvt: Jump vector table
CSR_JVT: begin
if (ZC_EXT) begin
csr_rdata_int = jvt_rdata;
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
// mstatus
CSR_MSTATUS: csr_rdata_int = mstatus_rdata;
// misa
CSR_MISA: csr_rdata_int = misa_rdata;
// mie: machine interrupt enable
CSR_MIE: begin
csr_rdata_int = mie_rdata;
end
// mtvec: machine trap-handler base address
CSR_MTVEC: csr_rdata_int = mtvec_rdata;
// mtvt: machine trap-handler vector table base address
CSR_MTVT: begin
if (CLIC) begin
csr_rdata_int = mtvt_rdata;
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
// mstatush
CSR_MSTATUSH: csr_rdata_int = mstatush_rdata;
CSR_MCOUNTINHIBIT: csr_rdata_int = mcountinhibit_rdata;
CSR_MHPMEVENT3,
CSR_MHPMEVENT4, CSR_MHPMEVENT5, CSR_MHPMEVENT6, CSR_MHPMEVENT7,
CSR_MHPMEVENT8, CSR_MHPMEVENT9, CSR_MHPMEVENT10, CSR_MHPMEVENT11,
CSR_MHPMEVENT12, CSR_MHPMEVENT13, CSR_MHPMEVENT14, CSR_MHPMEVENT15,
CSR_MHPMEVENT16, CSR_MHPMEVENT17, CSR_MHPMEVENT18, CSR_MHPMEVENT19,
CSR_MHPMEVENT20, CSR_MHPMEVENT21, CSR_MHPMEVENT22, CSR_MHPMEVENT23,
CSR_MHPMEVENT24, CSR_MHPMEVENT25, CSR_MHPMEVENT26, CSR_MHPMEVENT27,
CSR_MHPMEVENT28, CSR_MHPMEVENT29, CSR_MHPMEVENT30, CSR_MHPMEVENT31:
csr_rdata_int = mhpmevent_rdata[csr_raddr[4:0]];
// mscratch: machine scratch
CSR_MSCRATCH: csr_rdata_int = mscratch_rdata;
// mepc: exception program counter
CSR_MEPC: csr_rdata_int = mepc_rdata;
// mcause: exception cause
CSR_MCAUSE: csr_rdata_int = mcause_rdata;
// mtval
CSR_MTVAL: csr_rdata_int = mtval_rdata;
// mip: interrupt pending
CSR_MIP: csr_rdata_int = mip_rdata;
// mnxti: Next Interrupt Handler Address and Interrupt Enable
CSR_MNXTI: begin
if (CLIC) begin
// The data read here is what will be used in the read-modify-write portion of the CSR access.
// For mnxti, this is actually mstatus. The value written back to the GPR will be the address of
// the function pointer to the interrupt handler. This is muxed in the WB stage.
csr_rdata_int = mstatus_rdata;
csr_mnxti_read_o = 1'b1;
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
// mintstatus: Interrupt Status
CSR_MINTSTATUS: begin
if (CLIC) begin
csr_rdata_int = mintstatus_rdata;
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
// mintthresh: Interrupt-Level Threshold
CSR_MINTTHRESH: begin
if (CLIC) begin
csr_rdata_int = mintthresh_rdata;
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
// mscratchcsw: Scratch Swap for Multiple Privilege Modes
CSR_MSCRATCHCSW: begin
if (CLIC) begin
// CLIC spec 13.2
// Depending on mstatus.MPP, we return either mscratch_rdata or rs1 to rd.
// Safe to use mstatus_rdata here (EX timing), as there is a generic stall of the ID stage
// whenever a CSR instruction follows another CSR instruction. Alternative implementation using
// a local forward of mstatus_rdata is identical (SEC).
if (mstatus_rdata.mpp != PRIV_LVL_M) begin
// Return mscratch for writing to GPR
csr_rdata_int = mscratch_rdata;
end else begin
// return rs1 for writing to GPR
csr_rdata_int = id_ex_pipe_i.alu_operand_a;
end
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
// mscratchcswl: Scratch Swap for Interrupt Levels
CSR_MSCRATCHCSWL: begin
if (CLIC) begin
// CLIC spec 14.1
// Depending on mcause.pil and mintstatus.mil, either mscratch or rs1 is returned to rd.
// Safe to use mcause_rdata and mintstatus_rdata here (EX timing), as there is a generic stall of the ID stage
// whenever a CSR instruction follows another CSR instruction. Alternative implementation using
// a local forward of mcause_rdata and mintstatus_rdata is identical (SEC).
if ((mcause_rdata.mpil == '0) != (mintstatus_rdata.mil == 0)) begin
// Return mscratch for writing to GPR
csr_rdata_int = mscratch_rdata;
end else begin
// return rs1 for writing to GPR
csr_rdata_int = id_ex_pipe_i.alu_operand_a;
end
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
CSR_TSELECT: begin
if (DBG_NUM_TRIGGERS > 0) begin
csr_rdata_int = tselect_rdata;
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
CSR_TDATA1: begin
if (DBG_NUM_TRIGGERS > 0) begin
csr_rdata_int = tdata1_rdata;
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
CSR_TDATA2: begin
if (DBG_NUM_TRIGGERS > 0) begin
csr_rdata_int = tdata2_rdata;
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
CSR_TINFO: begin
if (DBG_NUM_TRIGGERS > 0) begin
csr_rdata_int = tinfo_rdata;
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
CSR_DCSR: begin
if (DEBUG) begin
csr_rdata_int = dcsr_rdata;
illegal_csr_read = !ctrl_fsm_i.debug_mode;
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
CSR_DPC: begin
if (DEBUG) begin
csr_rdata_int = dpc_rdata;
illegal_csr_read = !ctrl_fsm_i.debug_mode;
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
CSR_DSCRATCH0: begin
if (DEBUG) begin
csr_rdata_int = dscratch0_rdata;
illegal_csr_read = !ctrl_fsm_i.debug_mode;
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
CSR_DSCRATCH1: begin
if (DEBUG) begin
csr_rdata_int = dscratch1_rdata;
illegal_csr_read = !ctrl_fsm_i.debug_mode;
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
CSR_TIME: begin
csr_rdata_int = time_i[31:0];
end
CSR_TIMEH: begin
csr_rdata_int = time_i[63:32];
end
// Hardware Performance Monitor
CSR_MCYCLE,
CSR_MINSTRET,
CSR_MHPMCOUNTER3,
CSR_MHPMCOUNTER4, CSR_MHPMCOUNTER5, CSR_MHPMCOUNTER6, CSR_MHPMCOUNTER7,
CSR_MHPMCOUNTER8, CSR_MHPMCOUNTER9, CSR_MHPMCOUNTER10, CSR_MHPMCOUNTER11,
CSR_MHPMCOUNTER12, CSR_MHPMCOUNTER13, CSR_MHPMCOUNTER14, CSR_MHPMCOUNTER15,
CSR_MHPMCOUNTER16, CSR_MHPMCOUNTER17, CSR_MHPMCOUNTER18, CSR_MHPMCOUNTER19,
CSR_MHPMCOUNTER20, CSR_MHPMCOUNTER21, CSR_MHPMCOUNTER22, CSR_MHPMCOUNTER23,
CSR_MHPMCOUNTER24, CSR_MHPMCOUNTER25, CSR_MHPMCOUNTER26, CSR_MHPMCOUNTER27,
CSR_MHPMCOUNTER28, CSR_MHPMCOUNTER29, CSR_MHPMCOUNTER30, CSR_MHPMCOUNTER31,
CSR_CYCLE,
CSR_INSTRET,
CSR_HPMCOUNTER3,
CSR_HPMCOUNTER4, CSR_HPMCOUNTER5, CSR_HPMCOUNTER6, CSR_HPMCOUNTER7,
CSR_HPMCOUNTER8, CSR_HPMCOUNTER9, CSR_HPMCOUNTER10, CSR_HPMCOUNTER11,
CSR_HPMCOUNTER12, CSR_HPMCOUNTER13, CSR_HPMCOUNTER14, CSR_HPMCOUNTER15,
CSR_HPMCOUNTER16, CSR_HPMCOUNTER17, CSR_HPMCOUNTER18, CSR_HPMCOUNTER19,
CSR_HPMCOUNTER20, CSR_HPMCOUNTER21, CSR_HPMCOUNTER22, CSR_HPMCOUNTER23,
CSR_HPMCOUNTER24, CSR_HPMCOUNTER25, CSR_HPMCOUNTER26, CSR_HPMCOUNTER27,
CSR_HPMCOUNTER28, CSR_HPMCOUNTER29, CSR_HPMCOUNTER30, CSR_HPMCOUNTER31: begin
csr_rdata_int = mhpmcounter_rdata[csr_raddr[4:0]][31:0];
csr_counter_read_o = 1'b1;
end
CSR_MCYCLEH,
CSR_MINSTRETH,
CSR_MHPMCOUNTER3H,
CSR_MHPMCOUNTER4H, CSR_MHPMCOUNTER5H, CSR_MHPMCOUNTER6H, CSR_MHPMCOUNTER7H,
CSR_MHPMCOUNTER8H, CSR_MHPMCOUNTER9H, CSR_MHPMCOUNTER10H, CSR_MHPMCOUNTER11H,
CSR_MHPMCOUNTER12H, CSR_MHPMCOUNTER13H, CSR_MHPMCOUNTER14H, CSR_MHPMCOUNTER15H,
CSR_MHPMCOUNTER16H, CSR_MHPMCOUNTER17H, CSR_MHPMCOUNTER18H, CSR_MHPMCOUNTER19H,
CSR_MHPMCOUNTER20H, CSR_MHPMCOUNTER21H, CSR_MHPMCOUNTER22H, CSR_MHPMCOUNTER23H,
CSR_MHPMCOUNTER24H, CSR_MHPMCOUNTER25H, CSR_MHPMCOUNTER26H, CSR_MHPMCOUNTER27H,
CSR_MHPMCOUNTER28H, CSR_MHPMCOUNTER29H, CSR_MHPMCOUNTER30H, CSR_MHPMCOUNTER31H,
CSR_CYCLEH,
CSR_INSTRETH,
CSR_HPMCOUNTER3H,
CSR_HPMCOUNTER4H, CSR_HPMCOUNTER5H, CSR_HPMCOUNTER6H, CSR_HPMCOUNTER7H,
CSR_HPMCOUNTER8H, CSR_HPMCOUNTER9H, CSR_HPMCOUNTER10H, CSR_HPMCOUNTER11H,
CSR_HPMCOUNTER12H, CSR_HPMCOUNTER13H, CSR_HPMCOUNTER14H, CSR_HPMCOUNTER15H,
CSR_HPMCOUNTER16H, CSR_HPMCOUNTER17H, CSR_HPMCOUNTER18H, CSR_HPMCOUNTER19H,
CSR_HPMCOUNTER20H, CSR_HPMCOUNTER21H, CSR_HPMCOUNTER22H, CSR_HPMCOUNTER23H,
CSR_HPMCOUNTER24H, CSR_HPMCOUNTER25H, CSR_HPMCOUNTER26H, CSR_HPMCOUNTER27H,
CSR_HPMCOUNTER28H, CSR_HPMCOUNTER29H, CSR_HPMCOUNTER30H, CSR_HPMCOUNTER31H: begin
csr_rdata_int = mhpmcounter_rdata[csr_raddr[4:0]][63:32];
csr_counter_read_o = 1'b1;
end
// mvendorid: Machine Vendor ID
CSR_MVENDORID: csr_rdata_int = mvendorid_rdata;
// marchid: Machine Architecture ID
CSR_MARCHID: csr_rdata_int = marchid_rdata;
// mimpid: implementation id
CSR_MIMPID: csr_rdata_int = mimpid_rdata;
// mhartid: unique hardware thread id
CSR_MHARTID: csr_rdata_int = mhartid_rdata;
// mconfigptr: Pointer to configuration data structure
CSR_MCONFIGPTR: csr_rdata_int = mconfigptr_rdata;
default: begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
endcase
end
////////////////////////////////////////////
// CSR write logic
always_comb
begin
jvt_n = csr_next_value(csr_wdata_int, CSR_JVT_MASK, JVT_RESET_VAL);
jvt_we = 1'b0;
mscratch_n = csr_next_value(csr_wdata_int, CSR_MSCRATCH_MASK, MSCRATCH_RESET_VAL);
mscratch_we = 1'b0;
mepc_n = csr_next_value(csr_wdata_int, CSR_MEPC_MASK, MEPC_RESET_VAL);
mepc_we = 1'b0;
dpc_n = csr_next_value(csr_wdata_int, CSR_DPC_MASK, DPC_RESET_VAL);
dpc_we = 1'b0;
dcsr_n = csr_next_value(dcsr_t'{
xdebugver : dcsr_rdata.xdebugver,
ebreakm : csr_wdata_int[15],
ebreaku : dcsr_ebreaku_resolve(dcsr_rdata.ebreaku, csr_wdata_int[DCSR_EBREAKU_BIT]),
stepie : csr_wdata_int[11],
stopcount : csr_wdata_int[10],
mprven : dcsr_rdata.mprven,
step : csr_wdata_int[2],
prv : dcsr_prv_resolve(dcsr_rdata.prv, csr_wdata_int[DCSR_PRV_BIT_HIGH:DCSR_PRV_BIT_LOW]),
cause : dcsr_rdata.cause,
default : 'd0
},
CSR_DCSR_MASK, DCSR_RESET_VAL);
dcsr_we = 1'b0;
dscratch0_n = csr_next_value(csr_wdata_int, CSR_DSCRATCH0_MASK, DSCRATCH0_RESET_VAL);
dscratch0_we = 1'b0;
dscratch1_n = csr_next_value(csr_wdata_int, CSR_DSCRATCH1_MASK, DSCRATCH1_RESET_VAL);
dscratch1_we = 1'b0;
tselect_we = 1'b0;
tdata1_we = 1'b0;
tdata2_we = 1'b0;
tinfo_we = 1'b0;
// TODO:XIF add support for SD/XS/FS/VS
mstatus_n = csr_next_value(mstatus_t'{
tw: 1'b0,
mprv: mstatus_mprv_resolve(mstatus_rdata.mprv, csr_wdata_int[MSTATUS_MPRV_BIT]),
mpp: mstatus_mpp_resolve(mstatus_rdata.mpp, csr_wdata_int[MSTATUS_MPP_BIT_HIGH:MSTATUS_MPP_BIT_LOW]),
mpie: csr_wdata_int[MSTATUS_MPIE_BIT],
mie: csr_wdata_int[MSTATUS_MIE_BIT],
default: 'b0
},
CSR_MSTATUS_MASK, MSTATUS_RESET_VAL);
mstatus_we = 1'b0;
mstatush_n = mstatush_rdata; // Read only
mstatush_we = 1'b0;
mstatus_alias_we = 1'b0;
misa_n = misa_rdata; // Read only
misa_we = 1'b0;
priv_lvl_n = priv_lvl_rdata;
priv_lvl_we = 1'b0;
if (CLIC) begin
mtvec_n = csr_next_value(mtvec_t'{
addr: csr_mtvec_init_i ? mtvec_addr_i[31:7] : csr_wdata_int[31:7],
zero0: mtvec_rdata.zero0,
submode: mtvec_rdata.submode,
mode: csr_mtvec_init_i ? mtvec_rdata.mode : mtvec_mode_clic_resolve(mtvec_rdata.mode, csr_wdata_int[MTVEC_MODE_BIT_HIGH:MTVEC_MODE_BIT_LOW])
},
CSR_CLIC_MTVEC_MASK, MTVEC_CLIC_RESET_VAL);
mtvec_we = csr_mtvec_init_i;
mtvt_n = csr_next_value({csr_wdata_int[31:(32-MTVT_ADDR_WIDTH)], {(32-MTVT_ADDR_WIDTH){1'b0}}}, CSR_MTVT_MASK, MTVT_RESET_VAL);
mtvt_we = 1'b0;
mnxti_we = 1'b0;
mintstatus_n = mintstatus_rdata; // Read only
mintstatus_we = 1'b0;
mintthresh_n = csr_next_value(csr_wdata_int, CSR_MINTTHRESH_MASK, MINTTHRESH_RESET_VAL);
mintthresh_we = 1'b0;
mscratchcsw_n = mscratch_n; // mscratchcsw operates conditionally on mscratch
mscratchcsw_we = 1'b0;
mscratchcswl_n = mscratch_n; // mscratchcsw operates conditionally on mscratch
mscratchcswl_we = 1'b0;
mie_n = '0;
mie_we = 1'b0;
mip_n = mip_rdata; // Read only;
mip_we = 1'b0;
mcause_n = csr_next_value(mcause_t'{
irq: csr_wdata_int[31],
minhv: csr_wdata_int[30],
mpp: mcause_mpp_resolve(mcause_rdata.mpp, csr_wdata_int[MCAUSE_MPP_BIT_HIGH:MCAUSE_MPP_BIT_LOW]),
mpie: csr_wdata_int[MCAUSE_MPIE_BIT],
mpil: csr_wdata_int[23:16],
exception_code: csr_wdata_int[10:0],
default: 'b0
},
CSR_CLIC_MCAUSE_MASK, MCAUSE_CLIC_RESET_VAL);
mcause_we = 1'b0;
mcause_alias_we = 1'b0;
end else begin // !CLIC
mtvec_n = csr_next_value(mtvec_t'{
addr: csr_mtvec_init_i ? mtvec_addr_i[31:7] : csr_wdata_int[31:7],
zero0: mtvec_rdata.zero0,
submode: mtvec_rdata.submode,
mode: csr_mtvec_init_i ? mtvec_rdata.mode : mtvec_mode_clint_resolve(mtvec_rdata.mode, csr_wdata_int[MTVEC_MODE_BIT_HIGH:MTVEC_MODE_BIT_LOW])
},
CSR_BASIC_MTVEC_MASK, MTVEC_BASIC_RESET_VAL);
mtvec_we = csr_mtvec_init_i;
mtvt_n = '0;
mtvt_we = 1'b0;
mnxti_we = 1'b0;
mintstatus_n = '0;
mintstatus_we = 1'b0;
mintthresh_n = '0;
mintthresh_we = 1'b0;
mscratchcsw_n = '0;
mscratchcsw_we = 1'b0;
mscratchcswl_n = '0;
mscratchcswl_we = 1'b0;
mie_n = csr_next_value(csr_wdata_int, IRQ_MASK, MIE_BASIC_RESET_VAL);
mie_we = 1'b0;
mip_n = mip_rdata; // Read only;
mip_we = 1'b0;
mcause_n = csr_next_value(mcause_t'{
irq: csr_wdata_int[31],
exception_code: csr_wdata_int[10:0],
default: 'b0
},
CSR_BASIC_MCAUSE_MASK, MCAUSE_BASIC_RESET_VAL);
mcause_we = 1'b0;
mcause_alias_we = 1'b0;
end
mtval_n = mtval_rdata; // Read-only
mtval_we = 1'b0;
// Read-only CSRS
mhartid_n = mhartid_rdata; // Read-only
mhartid_we = 1'b0; // Always 0
mimpid_n = mimpid_rdata; // Read-only
mimpid_we = 1'b0; // Always 0
mconfigptr_n = mconfigptr_rdata; // Read-only
mconfigptr_we = 1'b0; // Always 0
mvendorid_n = mvendorid_rdata; // Read-only
mvendorid_we = 1'b0; // Always 0
marchid_n = marchid_rdata; // Read-only
marchid_we = 1'b0; // Always 0
if (csr_we_int) begin
case (csr_waddr)
// jvt: Jump vector table
CSR_JVT: begin
if (ZC_EXT) begin
jvt_we = 1'b1;
end
end
// mstatus
CSR_MSTATUS: begin
mstatus_we = 1'b1;
// CLIC mode is assumed when CLIC = 1
// For CLIC, a write to mstatus.mpp or mstatus.mpie will write to the
// corresponding bits in mstatus as well.
if (CLIC) begin
mcause_alias_we = 1'b1;
end
end
CSR_MISA: begin
misa_we = 1'b1;
end
// mie: machine interrupt enable
CSR_MIE: begin
mie_we = 1'b1;
end
// mtvec: machine trap-handler base address
CSR_MTVEC: begin
mtvec_we = 1'b1;
end
// mtvt: machine trap-handler vector table base address
CSR_MTVT: begin
if (CLIC) begin
mtvt_we = 1'b1;
end
end
CSR_MSTATUSH: begin
mstatush_we = 1'b1;
end
// mscratch: machine scratch
CSR_MSCRATCH: begin
mscratch_we = 1'b1;
end
// mepc: exception program counter
CSR_MEPC: begin
mepc_we = 1'b1;
end
// mcause
CSR_MCAUSE: begin
mcause_we = 1'b1;
// CLIC mode is assumed when CLIC = 1
// For CLIC, a write to mcause.mpp or mcause.mpie will write to the
// corresponding bits in mstatus as well.
if (CLIC) begin
mstatus_alias_we = 1'b1;
end
end
CSR_MTVAL: begin
mtval_we = 1'b1;
end
// mip: machine interrupt pending
CSR_MIP: begin
mip_we = 1'b1;
end
CSR_MNXTI: begin
if (CLIC) begin
mnxti_we = 1'b1;
// Writes to mnxti also writes to mstatus (uses mstatus in the RMW operation)
// Also writing to mcause to ensure we can assert mstatus_we == mcause_we and similar for mpp/mpie.
mstatus_we = 1'b1;
mcause_we = 1'b1;
// Writes to mintstatus.mil and mcause depend on the current state of
// clic interrupts AND the type of CSR instruction used.
// Mcause is written unconditionally for aliasing purposes, but the mcause_n
// is modified to reflect the side effects in case mnxti_irq_pending_i i set.
// Side effects occur when there is an actual write to the mstatus CSR
// This is already coded into the csr_we_int/mnxti_we
if (mnxti_irq_pending_i) begin
mintstatus_we = 1'b1;
end
end
end
CSR_MINTTHRESH: begin
if (CLIC) begin
mintthresh_we = 1'b1;
end
end
CSR_MSCRATCHCSW: begin
if (CLIC) begin
// mscratchcsw operates on mscratch
// Writing only when mstatus.mpp != PRIV_LVL_M
if (mstatus_rdata.mpp != PRIV_LVL_M) begin
mscratchcsw_we = 1'b1;
mscratch_we = 1'b1;
end
end
end
CSR_MSCRATCHCSWL: begin
if (CLIC) begin
// mscratchcswl operates on mscratch
if ((mcause_rdata.mpil == '0) != (mintstatus_rdata.mil == '0)) begin
mscratchcswl_we = 1'b1;
mscratch_we = 1'b1;
end
end
end
CSR_TSELECT: begin
tselect_we = 1'b1;
end
CSR_TDATA1: begin
if (ctrl_fsm_i.debug_mode) begin
tdata1_we = 1'b1;
end
end
CSR_TDATA2: begin
if (ctrl_fsm_i.debug_mode) begin
tdata2_we = 1'b1;
end
end
CSR_TINFO: begin
tinfo_we = 1'b1;
end
CSR_DCSR: begin
dcsr_we = 1'b1;
end
CSR_DPC: begin
dpc_we = 1'b1;
end
CSR_DSCRATCH0: begin
dscratch0_we = 1'b1;
end
CSR_DSCRATCH1: begin
dscratch1_we = 1'b1;
end
default:;
endcase
end
// CSR side effects from other CSRs
// CLIC mode is assumed when CLIC = 1
if (CLIC) begin
if (mnxti_we) begin
// Mstatus is written as part of an mnxti access
// Make sure we alias the mpp/mpie to mcause
mcause_n = mcause_rdata;
mcause_n.mpie = mstatus_n.mpie;
mcause_n.mpp = mstatus_n.mpp;
// mintstatus and mcause are updated if an actual mstatus write happens and
// a higher level non-shv interrupt is pending.
// This is already decoded into the respective _we signals below.
if (mintstatus_we) begin
mintstatus_n.mil = mnxti_irq_level_i;
end
if (mnxti_irq_pending_i) begin
mcause_n.irq = 1'b1;
mcause_n.exception_code = {1'b0, 10'(mnxti_irq_id_i)};
end
end else if (mstatus_alias_we) begin
// In CLIC mode, writes to mcause.mpp/mpie is aliased to mstatus.mpp/mpie
// All other mstatus bits are preserved
mstatus_n = mstatus_rdata; // Preserve all fields
// Write mpie and mpp as aliased through mcause
mstatus_n.mpie = mcause_n.mpie;
mstatus_n.mpp = mcause_n.mpp;
mstatus_we = 1'b1;
end else if (mcause_alias_we) begin