-
Notifications
You must be signed in to change notification settings - Fork 22
/
cv32e40s_cs_registers.sv
2958 lines (2496 loc) · 111 KB
/
cv32e40s_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 lowRISC contributors.
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0
// 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 cv32e40s_cs_registers import cv32e40s_pkg::*;
#(
parameter LIB = 0,
parameter rv32_e RV32 = RV32I,
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: implement related mstatus bitfields (but only if X_EXT = 1)
parameter bit ZC_EXT = 0,
parameter bit SMCLIC = 0,
parameter int SMCLIC_ID_WIDTH = 5,
parameter int SMCLIC_INTTHRESHBITS = 8,
parameter int NUM_MHPMCOUNTERS = 1,
parameter int PMP_NUM_REGIONS = 0,
parameter int PMP_GRANULARITY = 0,
parameter pmpncfg_t PMP_PMPNCFG_RV[PMP_NUM_REGIONS-1:0] = '{default:PMPNCFG_DEFAULT},
parameter logic [31:0] PMP_PMPADDR_RV[PMP_NUM_REGIONS-1:0] = '{default:32'h0},
parameter mseccfg_t PMP_MSECCFG_RV = MSECCFG_DEFAULT,
parameter lfsr_cfg_t LFSR0_CFG = LFSR_CFG_DEFAULT,
parameter lfsr_cfg_t LFSR1_CFG = LFSR_CFG_DEFAULT,
parameter lfsr_cfg_t LFSR2_CFG = LFSR_CFG_DEFAULT,
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,
input logic scan_cg_en_i,
// 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_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 logic [31:0] mstateen0_o,
output privlvl_t priv_lvl_o,
output privlvlctrl_t priv_lvl_if_ctrl_o,
output privlvl_t priv_lvl_lsu_o,
output privlvl_t priv_lvl_clic_ptr_o,
// IF/ID pipeline
input logic sys_en_id_i,
input logic sys_mret_id_i,
// 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 [SMCLIC_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
// PMP
output pmp_csr_t csr_pmp_o,
// Xsecure
output logic csr_err_o,
output logic lfsr_lockup_o,
input logic lfsr_shift_if_i,
input logic lfsr_shift_id_i,
output xsecure_ctrl_t xsecure_ctrl_o,
// 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 trigger_match_if_o,
output logic 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
);
localparam bit PMP = SECURE;
localparam PMP_ADDR_WIDTH = (PMP_GRANULARITY > 0) ? 33 - PMP_GRANULARITY : 32;
localparam bit USER = SECURE;
localparam logic [31:0] CORE_MISA =
(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'(USER) << 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);
// Set mask for minththresh based on number of bits implemented (SMCLIC_INTTHRESHBITS)
localparam CSR_MINTTHRESH_MASK = ((2 ** SMCLIC_INTTHRESHBITS )-1) << (8 - SMCLIC_INTTHRESHBITS);
// 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] tdata3_rdata;
logic tdata3_we;
logic [31:0] tinfo_rdata;
logic tinfo_we;
logic [31:0] tcontrol_rdata;
logic tcontrol_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] mclicbase_n, mclicbase_rdata; // No CSR module instance
logic mclicbase_we; // Not used in RTL (used by RVFI)
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)
logic [31:0] mcounteren_n, mcounteren_rdata; // No CSR module instance
logic mcounteren_we; // Not used in RTL (used by RVFI)
pmpncfg_t pmpncfg_n[PMP_MAX_REGIONS];
pmpncfg_t pmpncfg_n_int[PMP_MAX_REGIONS];
pmpncfg_t pmpncfg_q[PMP_MAX_REGIONS];
pmpncfg_t pmpncfg_rdata[PMP_MAX_REGIONS];
logic [PMP_MAX_REGIONS-1:0] pmpncfg_we;
logic [PMP_MAX_REGIONS-1:0] pmpncfg_locked;
logic [PMP_MAX_REGIONS-1:0] pmpaddr_locked;
logic [PMP_MAX_REGIONS-1:0] pmpncfg_wr_addr_match;
logic [PMP_MAX_REGIONS-1:0] pmpncfg_warl_ignore_wr;
logic [PMP_MAX_REGIONS-1:0] pmpaddr_wr_addr_match;
logic [PMP_ADDR_WIDTH-1:0] pmp_addr_n[PMP_MAX_REGIONS]; // Value written is not necessarily the value read
logic [PMP_ADDR_WIDTH-1:0] pmp_addr_q[PMP_MAX_REGIONS];
logic [31:0] pmp_addr_n_r[PMP_MAX_REGIONS]; // Not used in RTL (used by RVFI) (next read value)
logic [PMP_MAX_REGIONS-1:0] pmp_addr_n_r_unused;
logic [31:0] pmp_addr_rdata[PMP_MAX_REGIONS];
logic [PMP_MAX_REGIONS-1:0] pmp_addr_we;
mseccfg_t pmp_mseccfg_n;
mseccfg_t pmp_mseccfg_q;
mseccfg_t pmp_mseccfg_rdata;
logic pmp_mseccfg_we;
mseccfg_t pmp_mseccfgh_n; // No CSR module instance
mseccfg_t pmp_mseccfgh_rdata;
logic pmp_mseccfgh_we; // Not used in RTL (used by RVFI)
logic [31:0] menvcfgh_n, menvcfgh_rdata; // No CSR module instance
logic menvcfgh_we; // Not used in RTL (used by RVFI)
logic [31:0] menvcfg_n, menvcfg_rdata; // No CSR module instance
logic menvcfg_we; // Not used in RTL (used by RVFI)
cpuctrl_t cpuctrl_n, cpuctrl_q, cpuctrl_rdata;
logic cpuctrl_we;
logic [31:0] secureseed0_n, secureseed1_n, secureseed2_n; // No CSR module instance
logic secureseed0_we, secureseed1_we, secureseed2_we;
logic [31:0] secureseed0_rdata, secureseed1_rdata, secureseed2_rdata;
privlvl_t priv_lvl_n, priv_lvl_q, priv_lvl_rdata;
logic priv_lvl_we;
logic [1:0] priv_lvl_q_int;
logic [31:0] mstateen0_n, mstateen0_q, mstateen0_rdata;
logic mstateen0_we;
logic [31:0] mstateen1_n, mstateen1_rdata; // No CSR module instance
logic mstateen1_we; // Not used in RTL (used by RVFI)
logic [31:0] mstateen2_n, mstateen2_rdata; // No CSR module instance
logic mstateen2_we; // Not used in RTL (used by RVFI)
logic [31:0] mstateen3_n, mstateen3_rdata; // No CSR module instance
logic mstateen3_we; // Not used in RTL (used by RVFI)
logic [31:0] mstateen0h_n, mstateen0h_rdata; // No CSR module instance
logic mstateen0h_we; // Not used in RTL (used by RVFI)
logic [31:0] mstateen1h_n, mstateen1h_rdata; // No CSR module instance
logic mstateen1h_we; // Not used in RTL (used by RVFI)
logic [31:0] mstateen2h_n, mstateen2h_rdata; // No CSR module instance
logic mstateen2h_we; // Not used in RTL (used by RVFI)
logic [31:0] mstateen3h_n, mstateen3h_rdata; // No CSR module instance
logic mstateen3h_we;
// 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
logic illegal_csr_write_priv, illegal_csr_read_priv;
logic illegal_jvt_access;
logic csr_wr_in_wb;
logic xsecure_csr_wr_in_wb;
logic pmp_csr_wr_in_wb;
// Detect JVT writes (requires pipeline flush)
logic jvt_wr_in_wb;
logic mscratch_rd_error;
logic mstatus_rd_error;
logic mtvec_rd_error;
logic [PMP_MAX_REGIONS-1:0] pmpncfg_rd_error;
logic [PMP_MAX_REGIONS-1:0] pmp_addr_rd_error;
logic pmp_mseccfg_rd_error;
logic pmp_rd_error;
logic cpuctrl_rd_error;
logic dcsr_rd_error;
logic mie_rd_error;
logic mepc_rd_error;
logic mtvt_rd_error;
logic mintstatus_rd_error;
logic mintthresh_rd_error;
logic jvt_rd_error;
logic priv_lvl_rd_error;
logic mstateen0_rd_error;
logic dpc_rd_error; // Not used
logic dscratch0_rd_error; // Not used
logic dscratch1_rd_error; // Not used
logic mcause_rd_error; // Not used
// 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;
// Combine all CSR read error outputs
assign csr_err_o = mscratch_rd_error ||
mstatus_rd_error ||
mtvec_rd_error ||
pmp_rd_error ||
cpuctrl_rd_error ||
dcsr_rd_error ||
mie_rd_error ||
mepc_rd_error ||
mtvt_rd_error ||
mintstatus_rd_error ||
mintthresh_rd_error ||
jvt_rd_error ||
priv_lvl_rd_error ||
mstateen0_rd_error;
////////////////////////////////////////
// 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 //
////////////////////////////////////////
// Bits [9:8] in csr_addr indicate priviledge level needed to access CSR's.
// The exception is access to perfomance counters from user mode, which is configured through mcounteren.
assign illegal_csr_write_priv = csr_raddr[9:8] > id_ex_pipe_i.priv_lvl;
assign illegal_csr_read_priv = (csr_raddr[9:8] > id_ex_pipe_i.priv_lvl);
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) || illegal_csr_write_priv); // Priv spec section 2.1
// Any access to JVT is only legal from machine mode, or from user mode when mstateen[2] is 1.
assign illegal_jvt_access = ((csr_raddr == CSR_JVT) && ((id_ex_pipe_i.priv_lvl < PRIV_LVL_M) && !mstateen0_rdata[2]));
assign csr_illegal_o = (id_ex_pipe_i.instr_valid && id_ex_pipe_i.csr_en) ? illegal_csr_write || illegal_csr_read || illegal_csr_read_priv || illegal_jvt_access : 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;
// mcounteren: machine counter enable
CSR_MCOUNTEREN: begin
if (USER) begin
csr_rdata_int = mcounteren_rdata;
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
// mtvt: machine trap-handler vector table base address
CSR_MTVT: begin
if (SMCLIC) 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 (SMCLIC) 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 (SMCLIC) 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 (SMCLIC) 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 (SMCLIC) 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 (SMCLIC) 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
// mclicbase: CLIC Base
CSR_MCLICBASE: begin
if (SMCLIC) begin
csr_rdata_int = mclicbase_rdata;
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_TDATA3: begin
if (DBG_NUM_TRIGGERS > 0) begin
csr_rdata_int = tdata3_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_TCONTROL: begin
if (DBG_NUM_TRIGGERS > 0) begin
csr_rdata_int = tcontrol_rdata;
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
CSR_DCSR: begin
csr_rdata_int = dcsr_rdata;
illegal_csr_read = !ctrl_fsm_i.debug_mode;
end
CSR_DPC: begin
csr_rdata_int = dpc_rdata;
illegal_csr_read = !ctrl_fsm_i.debug_mode;
end
CSR_DSCRATCH0: begin
csr_rdata_int = dscratch0_rdata;
illegal_csr_read = !ctrl_fsm_i.debug_mode;
end
CSR_DSCRATCH1: begin
csr_rdata_int = dscratch1_rdata;
illegal_csr_read = !ctrl_fsm_i.debug_mode;
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: 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: 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;
CSR_PMPCFG0, CSR_PMPCFG1, CSR_PMPCFG2, CSR_PMPCFG3, CSR_PMPCFG4, CSR_PMPCFG5, CSR_PMPCFG6, CSR_PMPCFG7,
CSR_PMPCFG8, CSR_PMPCFG9, CSR_PMPCFG10, CSR_PMPCFG11, CSR_PMPCFG12, CSR_PMPCFG13, CSR_PMPCFG14, CSR_PMPCFG15: begin
if (PMP) begin
csr_rdata_int = {pmpncfg_rdata[6'(csr_raddr[3:0]*4 + 3)], pmpncfg_rdata[6'(csr_raddr[3:0]*4 + 2)],
pmpncfg_rdata[6'(csr_raddr[3:0]*4 + 1)], pmpncfg_rdata[6'(csr_raddr[3:0]*4 + 0)]};
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
CSR_PMPADDR0, CSR_PMPADDR1, CSR_PMPADDR2, CSR_PMPADDR3, CSR_PMPADDR4, CSR_PMPADDR5, CSR_PMPADDR6, CSR_PMPADDR7,
CSR_PMPADDR8, CSR_PMPADDR9, CSR_PMPADDR10, CSR_PMPADDR11, CSR_PMPADDR12, CSR_PMPADDR13, CSR_PMPADDR14, CSR_PMPADDR15: begin
if (PMP) begin
csr_rdata_int = pmp_addr_rdata[6'(16*0 + csr_raddr[3:0])];
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
CSR_PMPADDR16, CSR_PMPADDR17, CSR_PMPADDR18, CSR_PMPADDR19, CSR_PMPADDR20, CSR_PMPADDR21, CSR_PMPADDR22, CSR_PMPADDR23,
CSR_PMPADDR24, CSR_PMPADDR25, CSR_PMPADDR26, CSR_PMPADDR27, CSR_PMPADDR28, CSR_PMPADDR29, CSR_PMPADDR30, CSR_PMPADDR31: begin
if (PMP) begin
csr_rdata_int = pmp_addr_rdata[6'(16*1 + csr_raddr[3:0])];
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
CSR_PMPADDR32, CSR_PMPADDR33, CSR_PMPADDR34, CSR_PMPADDR35, CSR_PMPADDR36, CSR_PMPADDR37, CSR_PMPADDR38, CSR_PMPADDR39,
CSR_PMPADDR40, CSR_PMPADDR41, CSR_PMPADDR42, CSR_PMPADDR43, CSR_PMPADDR44, CSR_PMPADDR45, CSR_PMPADDR46, CSR_PMPADDR47: begin
if (PMP) begin
csr_rdata_int = pmp_addr_rdata[6'(16*2 + csr_raddr[3:0])];
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
CSR_PMPADDR48, CSR_PMPADDR49, CSR_PMPADDR50, CSR_PMPADDR51, CSR_PMPADDR52, CSR_PMPADDR53, CSR_PMPADDR54, CSR_PMPADDR55,
CSR_PMPADDR56, CSR_PMPADDR57, CSR_PMPADDR58, CSR_PMPADDR59, CSR_PMPADDR60, CSR_PMPADDR61, CSR_PMPADDR62, CSR_PMPADDR63: begin
if (PMP) begin
csr_rdata_int = pmp_addr_rdata[6'(16*3 + csr_raddr[3:0])];
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
CSR_MSECCFG: begin
if (PMP) begin
csr_rdata_int = pmp_mseccfg_rdata;
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
CSR_MSECCFGH: begin
if (PMP) begin
csr_rdata_int = pmp_mseccfgh_rdata;
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
CSR_MENVCFG: begin
if (USER) begin
csr_rdata_int = menvcfg_rdata;
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
CSR_MENVCFGH: begin
if (USER) begin
csr_rdata_int = menvcfgh_rdata;
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
CSR_SECURESEED0: begin
if (SECURE) begin
csr_rdata_int = secureseed0_rdata;
end else begin
// Cause illegal CSR access
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
CSR_SECURESEED1: begin
if (SECURE) begin
csr_rdata_int = secureseed1_rdata;
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
CSR_SECURESEED2: begin
if (SECURE) begin
csr_rdata_int = secureseed2_rdata;
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
CSR_CPUCTRL: begin
if (SECURE) begin
csr_rdata_int = cpuctrl_rdata;
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
CSR_MSTATEEN0: begin
if (USER) begin
csr_rdata_int = mstateen0_rdata;
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
CSR_MSTATEEN1: begin
if (USER) begin
csr_rdata_int = mstateen1_rdata;
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
CSR_MSTATEEN2: begin
if (USER) begin
csr_rdata_int = mstateen2_rdata;
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
CSR_MSTATEEN3: begin
if (USER) begin
csr_rdata_int = mstateen3_rdata;
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
CSR_MSTATEEN0H: begin
if (USER) begin
csr_rdata_int = mstateen0h_rdata;
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
CSR_MSTATEEN1H: begin
if (USER) begin
csr_rdata_int = mstateen1h_rdata;
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
CSR_MSTATEEN2H: begin
if (USER) begin
csr_rdata_int = mstateen2h_rdata;
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
CSR_MSTATEEN3H: begin
if (USER) begin
csr_rdata_int = mstateen3h_rdata;
end else begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
end
default: begin
csr_rdata_int = '0;
illegal_csr_read = 1'b1;
end
endcase
end
////////////////////////////////////////////
// CSR write logic
always_comb
begin
jvt_n = csr_wdata_int & CSR_JVT_MASK;
jvt_we = 1'b0;
mscratch_n = csr_wdata_int;
mscratch_we = 1'b0;
mepc_n = csr_wdata_int & CSR_MEPC_MASK;
mepc_we = 1'b0;
dpc_n = csr_wdata_int & CSR_DPC_MASK;
dpc_we = 1'b0;
dcsr_n = '{
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]),