OpenCores
URL https://opencores.org/ocsvn/neorv32/neorv32/trunk

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [rtl/] [core/] [neorv32_cpu_control.vhd] - Blame information for rev 60

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 zero_gravi
-- #################################################################################################
2
-- # << NEORV32 - CPU Control >>                                                                   #
3
-- # ********************************************************************************************* #
4 31 zero_gravi
-- # CPU operation is split into a fetch engine (responsible for fetching instruction data), an    #
5
-- # issue engine (for recoding compressed instructions and for constructing 32-bit instruction    #
6
-- # words) and an execute engine (responsible for actually executing the instructions), a trap    #
7 44 zero_gravi
-- # handling controller and the RISC-V status and control register set (CSRs) including the       #
8
-- # hardware performance monitor counters.                                                        #
9 2 zero_gravi
-- # ********************************************************************************************* #
10
-- # BSD 3-Clause License                                                                          #
11
-- #                                                                                               #
12 42 zero_gravi
-- # Copyright (c) 2021, Stephan Nolting. All rights reserved.                                     #
13 2 zero_gravi
-- #                                                                                               #
14
-- # Redistribution and use in source and binary forms, with or without modification, are          #
15
-- # permitted provided that the following conditions are met:                                     #
16
-- #                                                                                               #
17
-- # 1. Redistributions of source code must retain the above copyright notice, this list of        #
18
-- #    conditions and the following disclaimer.                                                   #
19
-- #                                                                                               #
20
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
21
-- #    conditions and the following disclaimer in the documentation and/or other materials        #
22
-- #    provided with the distribution.                                                            #
23
-- #                                                                                               #
24
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
25
-- #    endorse or promote products derived from this software without specific prior written      #
26
-- #    permission.                                                                                #
27
-- #                                                                                               #
28
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
29
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
30
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
31
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
32
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
33
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
34
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
35
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
36
-- # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
37
-- # ********************************************************************************************* #
38
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
39
-- #################################################################################################
40
 
41
library ieee;
42
use ieee.std_logic_1164.all;
43
use ieee.numeric_std.all;
44
 
45
library neorv32;
46
use neorv32.neorv32_package.all;
47
 
48
entity neorv32_cpu_control is
49
  generic (
50
    -- General --
51 49 zero_gravi
    HW_THREAD_ID                 : natural := 0;     -- hardware thread id (32-bit)
52 59 zero_gravi
    CPU_BOOT_ADDR                : std_ulogic_vector(31 downto 0) := x"00000000"; -- cpu boot address
53
    CPU_DEBUG_ADDR               : std_ulogic_vector(31 downto 0) := x"00000000"; -- cpu debug mode start address
54 2 zero_gravi
    -- RISC-V CPU Extensions --
55 39 zero_gravi
    CPU_EXTENSION_RISCV_A        : boolean := false; -- implement atomic extension?
56 12 zero_gravi
    CPU_EXTENSION_RISCV_C        : boolean := false; -- implement compressed extension?
57
    CPU_EXTENSION_RISCV_E        : boolean := false; -- implement embedded RF extension?
58
    CPU_EXTENSION_RISCV_M        : boolean := false; -- implement muld/div extension?
59 15 zero_gravi
    CPU_EXTENSION_RISCV_U        : boolean := false; -- implement user mode extension?
60 53 zero_gravi
    CPU_EXTENSION_RISCV_Zfinx    : boolean := false; -- implement 32-bit floating-point extension (using INT reg!)
61 12 zero_gravi
    CPU_EXTENSION_RISCV_Zicsr    : boolean := true;  -- implement CSR system?
62 49 zero_gravi
    CPU_EXTENSION_RISCV_Zifencei : boolean := false; -- implement instruction stream sync.?
63 59 zero_gravi
    CPU_EXTENSION_RISCV_DEBUG    : boolean := false; -- implement CPU debug mode?
64 56 zero_gravi
    -- Extension Options --
65
    CPU_CNT_WIDTH                : natural := 64; -- total width of CPU cycle and instret counters (0..64)
66 15 zero_gravi
    -- Physical memory protection (PMP) --
67 42 zero_gravi
    PMP_NUM_REGIONS              : natural := 0;       -- number of regions (0..64)
68
    PMP_MIN_GRANULARITY          : natural := 64*1024; -- minimal region granularity in bytes, has to be a power of 2, min 8 bytes
69
    -- Hardware Performance Monitors (HPM) --
70 56 zero_gravi
    HPM_NUM_CNTS                 : natural := 0;     -- number of implemented HPM counters (0..29)
71 60 zero_gravi
    HPM_CNT_WIDTH                : natural := 40     -- total size of HPM counters (0..64)
72 2 zero_gravi
  );
73
  port (
74
    -- global control --
75
    clk_i         : in  std_ulogic; -- global clock, rising edge
76
    rstn_i        : in  std_ulogic; -- global reset, low-active, async
77
    ctrl_o        : out std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
78
    -- status input --
79
    alu_wait_i    : in  std_ulogic; -- wait for ALU
80 12 zero_gravi
    bus_i_wait_i  : in  std_ulogic; -- wait for bus
81
    bus_d_wait_i  : in  std_ulogic; -- wait for bus
82 57 zero_gravi
    excl_state_i  : in  std_ulogic; -- atomic/exclusive access lock status
83 2 zero_gravi
    -- data input --
84
    instr_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- instruction
85
    cmp_i         : in  std_ulogic_vector(1 downto 0); -- comparator status
86 36 zero_gravi
    alu_add_i     : in  std_ulogic_vector(data_width_c-1 downto 0); -- ALU address result
87
    rs1_i         : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 1
88 2 zero_gravi
    -- data output --
89
    imm_o         : out std_ulogic_vector(data_width_c-1 downto 0); -- immediate
90 6 zero_gravi
    fetch_pc_o    : out std_ulogic_vector(data_width_c-1 downto 0); -- PC for instruction fetch
91
    curr_pc_o     : out std_ulogic_vector(data_width_c-1 downto 0); -- current PC (corresponding to current instruction)
92 2 zero_gravi
    csr_rdata_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- CSR read data
93 52 zero_gravi
    -- FPU interface --
94
    fpu_rm_o      : out std_ulogic_vector(02 downto 0); -- rounding mode
95
    fpu_flags_i   : in  std_ulogic_vector(04 downto 0); -- exception flags
96 59 zero_gravi
    -- debug mode (halt) request --
97
    db_halt_req_i : in  std_ulogic;
98 58 zero_gravi
    -- non-maskable interrupt --
99
    nm_irq_i      : in  std_ulogic;
100 14 zero_gravi
    -- interrupts (risc-v compliant) --
101
    msw_irq_i     : in  std_ulogic; -- machine software interrupt
102
    mext_irq_i    : in  std_ulogic; -- machine external interrupt
103 2 zero_gravi
    mtime_irq_i   : in  std_ulogic; -- machine timer interrupt
104 14 zero_gravi
    -- fast interrupts (custom) --
105 48 zero_gravi
    firq_i        : in  std_ulogic_vector(15 downto 0);
106
    firq_ack_o    : out std_ulogic_vector(15 downto 0);
107 11 zero_gravi
    -- system time input from MTIME --
108
    time_i        : in  std_ulogic_vector(63 downto 0); -- current system time
109 15 zero_gravi
    -- physical memory protection --
110 23 zero_gravi
    pmp_addr_o    : out pmp_addr_if_t; -- addresses
111
    pmp_ctrl_o    : out pmp_ctrl_if_t; -- configs
112 2 zero_gravi
    -- bus access exceptions --
113
    mar_i         : in  std_ulogic_vector(data_width_c-1 downto 0);  -- memory address register
114
    ma_instr_i    : in  std_ulogic; -- misaligned instruction address
115
    ma_load_i     : in  std_ulogic; -- misaligned load data address
116
    ma_store_i    : in  std_ulogic; -- misaligned store data address
117
    be_instr_i    : in  std_ulogic; -- bus error on instruction access
118
    be_load_i     : in  std_ulogic; -- bus error on load data access
119 12 zero_gravi
    be_store_i    : in  std_ulogic  -- bus error on store data access
120 2 zero_gravi
  );
121
end neorv32_cpu_control;
122
 
123
architecture neorv32_cpu_control_rtl of neorv32_cpu_control is
124
 
125 56 zero_gravi
  -- CPU core counter ([m]cycle, [m]instret) width - high/low parts --
126
  constant cpu_cnt_lo_width_c : natural := natural(cond_sel_int_f(boolean(CPU_CNT_WIDTH < 32), CPU_CNT_WIDTH, 32));
127
  constant cpu_cnt_hi_width_c : natural := natural(cond_sel_int_f(boolean(CPU_CNT_WIDTH > 32), CPU_CNT_WIDTH-32, 0));
128
 
129
  -- HPM counter width - high/low parts --
130
  constant hpm_cnt_lo_width_c : natural := natural(cond_sel_int_f(boolean(HPM_CNT_WIDTH < 32), HPM_CNT_WIDTH, 32));
131
  constant hpm_cnt_hi_width_c : natural := natural(cond_sel_int_f(boolean(HPM_CNT_WIDTH > 32), HPM_CNT_WIDTH-32, 0));
132
 
133 57 zero_gravi
  -- instruction fetch engine --
134
  type fetch_engine_state_t is (IFETCH_REQUEST, IFETCH_ISSUE);
135 6 zero_gravi
  type fetch_engine_t is record
136 31 zero_gravi
    state       : fetch_engine_state_t;
137
    state_nxt   : fetch_engine_state_t;
138 42 zero_gravi
    state_prev  : fetch_engine_state_t;
139 57 zero_gravi
    restart     : std_ulogic;
140
    restart_nxt : std_ulogic;
141 31 zero_gravi
    pc          : std_ulogic_vector(data_width_c-1 downto 0);
142
    pc_nxt      : std_ulogic_vector(data_width_c-1 downto 0);
143
    reset       : std_ulogic;
144
    bus_err_ack : std_ulogic;
145 6 zero_gravi
  end record;
146
  signal fetch_engine : fetch_engine_t;
147 2 zero_gravi
 
148 57 zero_gravi
  -- instruction prefetch buffer (IPB, real FIFO) --
149 31 zero_gravi
  type ipb_data_fifo_t is array (0 to ipb_entries_c-1) of std_ulogic_vector(2+31 downto 0);
150 6 zero_gravi
  type ipb_t is record
151 31 zero_gravi
    wdata : std_ulogic_vector(2+31 downto 0); -- write status (bus_error, align_error) + 32-bit instruction data
152
    we    : std_ulogic; -- trigger write
153
    free  : std_ulogic; -- free entry available?
154
    clear : std_ulogic; -- clear all entries
155 20 zero_gravi
    --
156 31 zero_gravi
    rdata : std_ulogic_vector(2+31 downto 0); -- read data: status (bus_error, align_error) + 32-bit instruction data
157
    re    : std_ulogic; -- read enable
158
    avail : std_ulogic; -- data available?
159 20 zero_gravi
    --
160 31 zero_gravi
    w_pnt : std_ulogic_vector(index_size_f(ipb_entries_c) downto 0); -- write pointer
161
    r_pnt : std_ulogic_vector(index_size_f(ipb_entries_c) downto 0); -- read pointer
162 34 zero_gravi
    match : std_ulogic;
163 31 zero_gravi
    empty : std_ulogic;
164
    full  : std_ulogic;
165 20 zero_gravi
    --
166 31 zero_gravi
    data  : ipb_data_fifo_t; -- fifo memory
167 6 zero_gravi
  end record;
168
  signal ipb : ipb_t;
169 2 zero_gravi
 
170 31 zero_gravi
  -- pre-decoder --
171
  signal ci_instr16 : std_ulogic_vector(15 downto 0);
172
  signal ci_instr32 : std_ulogic_vector(31 downto 0);
173
  signal ci_illegal : std_ulogic;
174
 
175 57 zero_gravi
  -- instruction issue engine --
176 31 zero_gravi
  type issue_engine_state_t is (ISSUE_ACTIVE, ISSUE_REALIGN);
177
  type issue_engine_t is record
178
    state     : issue_engine_state_t;
179
    state_nxt : issue_engine_state_t;
180
    align     : std_ulogic;
181
    align_nxt : std_ulogic;
182
    buf       : std_ulogic_vector(2+15 downto 0);
183
    buf_nxt   : std_ulogic_vector(2+15 downto 0);
184
  end record;
185
  signal issue_engine : issue_engine_t;
186
 
187 37 zero_gravi
  -- instruction issue interface --
188
  type cmd_issue_t is record
189
    data  : std_ulogic_vector(35 downto 0); -- 4-bit status + 32-bit instruction
190
    valid : std_ulogic; -- data word is valid when set
191 31 zero_gravi
  end record;
192 37 zero_gravi
  signal cmd_issue : cmd_issue_t;
193 31 zero_gravi
 
194 44 zero_gravi
  -- instruction decoding helper logic --
195
  type decode_aux_t is record
196 60 zero_gravi
    alu_immediate : std_ulogic;
197
    rs1_is_r0     : std_ulogic;
198
    is_atomic_lr  : std_ulogic;
199
    is_atomic_sc  : std_ulogic;
200
    is_float_op   : std_ulogic;
201
    sys_env_cmd   : std_ulogic_vector(11 downto 0);
202 44 zero_gravi
  end record;
203
  signal decode_aux : decode_aux_t;
204
 
205 6 zero_gravi
  -- instruction execution engine --
206 53 zero_gravi
  type execute_engine_state_t is (SYS_WAIT, DISPATCH, TRAP_ENTER, TRAP_EXIT, TRAP_EXECUTE, EXECUTE, ALU_WAIT, BRANCH,
207 57 zero_gravi
                                  FENCE_OP,LOADSTORE_0, LOADSTORE_1, LOADSTORE_2, SYS_ENV, CSR_ACCESS);
208 6 zero_gravi
  type execute_engine_t is record
209
    state        : execute_engine_state_t;
210
    state_nxt    : execute_engine_state_t;
211 42 zero_gravi
    state_prev   : execute_engine_state_t;
212 39 zero_gravi
    --
213 6 zero_gravi
    i_reg        : std_ulogic_vector(31 downto 0);
214
    i_reg_nxt    : std_ulogic_vector(31 downto 0);
215 33 zero_gravi
    i_reg_last   : std_ulogic_vector(31 downto 0); -- last executed instruction
216 39 zero_gravi
    --
217 6 zero_gravi
    is_ci        : std_ulogic; -- current instruction is de-compressed instruction
218
    is_ci_nxt    : std_ulogic;
219 29 zero_gravi
    is_cp_op     : std_ulogic; -- current instruction is a co-processor operation
220
    is_cp_op_nxt : std_ulogic;
221 39 zero_gravi
    --
222 57 zero_gravi
    branch_taken : std_ulogic; -- branch condition fulfilled
223 6 zero_gravi
    pc           : std_ulogic_vector(data_width_c-1 downto 0); -- actual PC, corresponding to current executed instruction
224 49 zero_gravi
    pc_mux_sel   : std_ulogic; -- source select for PC update
225 39 zero_gravi
    pc_we        : std_ulogic; -- PC update enabled
226 6 zero_gravi
    next_pc      : std_ulogic_vector(data_width_c-1 downto 0); -- next PC, corresponding to next instruction to be executed
227 49 zero_gravi
    next_pc_inc  : std_ulogic_vector(data_width_c-1 downto 0); -- increment to get next PC
228 6 zero_gravi
    last_pc      : std_ulogic_vector(data_width_c-1 downto 0); -- PC of last executed instruction
229 39 zero_gravi
    --
230 11 zero_gravi
    sleep        : std_ulogic; -- CPU in sleep mode
231 39 zero_gravi
    sleep_nxt    : std_ulogic;
232 49 zero_gravi
    branched     : std_ulogic; -- instruction fetch was reset
233
    branched_nxt : std_ulogic;
234 6 zero_gravi
  end record;
235
  signal execute_engine : execute_engine_t;
236 2 zero_gravi
 
237 6 zero_gravi
  -- trap controller --
238
  type trap_ctrl_t is record
239
    exc_buf       : std_ulogic_vector(exception_width_c-1 downto 0);
240
    exc_fire      : std_ulogic; -- set if there is a valid source in the exception buffer
241
    irq_buf       : std_ulogic_vector(interrupt_width_c-1 downto 0);
242
    irq_fire      : std_ulogic; -- set if there is a valid source in the interrupt buffer
243
    exc_ack       : std_ulogic; -- acknowledge all exceptions
244
    irq_ack       : std_ulogic_vector(interrupt_width_c-1 downto 0); -- acknowledge specific interrupt
245
    irq_ack_nxt   : std_ulogic_vector(interrupt_width_c-1 downto 0);
246 59 zero_gravi
    cause         : std_ulogic_vector(6 downto 0); -- trap ID for mcause CSR
247
    cause_nxt     : std_ulogic_vector(6 downto 0);
248
    db_irq_fire   : std_ulogic; -- set if there is a valid IRQ source in the "enter debug mode" trap buffer
249
    db_irq_en     : std_ulogic; -- set if IRQs are allowed in debu mode
250 6 zero_gravi
    --
251
    env_start     : std_ulogic; -- start trap handler env
252
    env_start_ack : std_ulogic; -- start of trap handler acknowledged
253
    env_end       : std_ulogic; -- end trap handler env
254
    --
255
    instr_be      : std_ulogic; -- instruction fetch bus error
256
    instr_ma      : std_ulogic; -- instruction fetch misaligned address
257
    instr_il      : std_ulogic; -- illegal instruction
258
    env_call      : std_ulogic;
259
    break_point   : std_ulogic;
260
  end record;
261
  signal trap_ctrl : trap_ctrl_t;
262
 
263 40 zero_gravi
  -- CPU main control bus --
264 6 zero_gravi
  signal ctrl_nxt, ctrl : std_ulogic_vector(ctrl_width_c-1 downto 0);
265 2 zero_gravi
 
266 40 zero_gravi
  -- fast instruction fetch access --
267 6 zero_gravi
  signal bus_fast_ir : std_ulogic;
268 2 zero_gravi
 
269 6 zero_gravi
  -- RISC-V control and status registers (CSRs) --
270 42 zero_gravi
  type pmp_ctrl_t     is array (0 to PMP_NUM_REGIONS-1) of std_ulogic_vector(7 downto 0);
271
  type pmp_addr_t     is array (0 to PMP_NUM_REGIONS-1) of std_ulogic_vector(data_width_c-1 downto 0);
272
  type pmp_ctrl_rd_t  is array (0 to 63) of std_ulogic_vector(7 downto 0);
273
  type mhpmevent_t    is array (0 to HPM_NUM_CNTS-1) of std_ulogic_vector(hpmcnt_event_size_c-1 downto 0);
274 56 zero_gravi
  type mhpmcnt_t      is array (0 to HPM_NUM_CNTS-1) of std_ulogic_vector(32 downto 0); -- 32-bit, plus 1-bit overflow
275
  type mhpmcnth_t     is array (0 to HPM_NUM_CNTS-1) of std_ulogic_vector(31 downto 0); -- 32-bit
276
  type mhpmcnt_rd_t   is array (0 to 29) of std_ulogic_vector(31 downto 0);
277 42 zero_gravi
  type mhpmcnth_rd_t  is array (0 to 29) of std_ulogic_vector(31 downto 0);
278 6 zero_gravi
  type csr_t is record
279 42 zero_gravi
    addr              : std_ulogic_vector(11 downto 0); -- csr address
280
    we                : std_ulogic; -- csr write enable
281
    we_nxt            : std_ulogic;
282
    re                : std_ulogic; -- csr read enable
283
    re_nxt            : std_ulogic;
284
    wdata             : std_ulogic_vector(data_width_c-1 downto 0); -- csr write data
285
    rdata             : std_ulogic_vector(data_width_c-1 downto 0); -- csr read data
286 29 zero_gravi
    --
287 42 zero_gravi
    mstatus_mie       : std_ulogic; -- mstatus.MIE: global IRQ enable (R/W)
288
    mstatus_mpie      : std_ulogic; -- mstatus.MPIE: previous global IRQ enable (R/W)
289
    mstatus_mpp       : std_ulogic_vector(1 downto 0); -- mstatus.MPP: machine previous privilege mode
290 29 zero_gravi
    --
291 42 zero_gravi
    mie_msie          : std_ulogic; -- mie.MSIE: machine software interrupt enable (R/W)
292
    mie_meie          : std_ulogic; -- mie.MEIE: machine external interrupt enable (R/W)
293
    mie_mtie          : std_ulogic; -- mie.MEIE: machine timer interrupt enable (R/W)
294 48 zero_gravi
    mie_firqe         : std_ulogic_vector(15 downto 0); -- mie.firq*e: fast interrupt enabled (R/W)
295 29 zero_gravi
    --
296 42 zero_gravi
    mcounteren_cy     : std_ulogic; -- mcounteren.cy: allow cycle[h] access from user-mode
297
    mcounteren_tm     : std_ulogic; -- mcounteren.tm: allow time[h] access from user-mode
298
    mcounteren_ir     : std_ulogic; -- mcounteren.ir: allow instret[h] access from user-mode
299
    mcounteren_hpm    : std_ulogic_vector(HPM_NUM_CNTS-1 downto 0); -- mcounteren.hpmx: allow mhpmcounterx[h] access from user-mode
300 29 zero_gravi
    --
301 42 zero_gravi
    mcountinhibit_cy  : std_ulogic; -- mcounterinhibit.cy: enable auto-increment for [m]cycle[h]
302
    mcountinhibit_ir  : std_ulogic; -- mcounterinhibit.ir: enable auto-increment for [m]instret[h]
303
    mcountinhibit_hpm : std_ulogic_vector(HPM_NUM_CNTS-1 downto 0); -- mcounterinhibit.hpm3: enable auto-increment for mhpmcounterx[h]
304 40 zero_gravi
    --
305 42 zero_gravi
    privilege         : std_ulogic_vector(1 downto 0); -- hart's current privilege mode
306 59 zero_gravi
    privilege_rd      : std_ulogic_vector(1 downto 0); -- hart's current privilege mode (effective)
307 42 zero_gravi
    priv_m_mode       : std_ulogic; -- CPU in M-mode
308
    priv_u_mode       : std_ulogic; -- CPU in u-mode
309 41 zero_gravi
    --
310 42 zero_gravi
    mepc              : std_ulogic_vector(data_width_c-1 downto 0); -- mepc: machine exception pc (R/W)
311 49 zero_gravi
    mcause            : std_ulogic_vector(5 downto 0); -- mcause: machine trap cause (R/W)
312 42 zero_gravi
    mtvec             : std_ulogic_vector(data_width_c-1 downto 0); -- mtvec: machine trap-handler base address (R/W), bit 1:0 == 00
313 49 zero_gravi
    mtval             : std_ulogic_vector(data_width_c-1 downto 0); -- mtval: machine bad address or instruction (R/W)
314 42 zero_gravi
    --
315
    mhpmevent         : mhpmevent_t; -- mhpmevent*: machine performance-monitoring event selector (R/W)
316
    --
317
    mscratch          : std_ulogic_vector(data_width_c-1 downto 0); -- mscratch: scratch register (R/W)
318 56 zero_gravi
    --
319 42 zero_gravi
    mcycle            : std_ulogic_vector(32 downto 0); -- mcycle (R/W), plus carry bit
320 60 zero_gravi
    mcycle_msb        : std_ulogic; -- counter low-to-high-word overflow
321
    mcycleh           : std_ulogic_vector(31 downto 0); -- mcycleh (R/W)
322 42 zero_gravi
    minstret          : std_ulogic_vector(32 downto 0); -- minstret (R/W), plus carry bit
323 60 zero_gravi
    minstret_msb      : std_ulogic; -- counter low-to-high-word overflow
324 42 zero_gravi
    minstreth         : std_ulogic_vector(31 downto 0); -- minstreth (R/W)
325
    --
326
    mhpmcounter       : mhpmcnt_t; -- mhpmcounter* (R/W), plus carry bit
327 60 zero_gravi
    mhpmcounter_msb   : std_ulogic_vector(HPM_NUM_CNTS-1 downto 0); -- counter low-to-high-word overflow
328 42 zero_gravi
    mhpmcounterh      : mhpmcnth_t; -- mhpmcounter*h (R/W)
329
    mhpmcounter_rd    : mhpmcnt_rd_t; -- mhpmcounter* (R/W): actual read data
330
    mhpmcounterh_rd   : mhpmcnth_rd_t; -- mhpmcounter*h (R/W): actual read data
331
    --
332
    pmpcfg            : pmp_ctrl_t; -- physical memory protection - configuration registers
333
    pmpcfg_rd         : pmp_ctrl_rd_t; -- physical memory protection - actual read data
334
    pmpaddr           : pmp_addr_t; -- physical memory protection - address registers
335 52 zero_gravi
    --
336
    frm               : std_ulogic_vector(02 downto 0); -- frm (R/W): FPU rounding mode
337
    fflags            : std_ulogic_vector(04 downto 0); -- fflags (R/W): FPU exception flags
338 59 zero_gravi
    --
339
    dcsr_ebreakm      : std_ulogic; -- dcsr.ebreakm (R/W): behavior of ebreak instruction on m-mode
340
    dcsr_ebreaku      : std_ulogic; -- dcsr.ebreaku (R/W): behavior of ebreak instruction on u-mode
341
    dcsr_step         : std_ulogic; -- dcsr.step (R/W): single-step mode
342
    dcsr_prv          : std_ulogic_vector(01 downto 0); -- dcsr.prv (R/W): current privilege level when entering debug mode
343
    dcsr_cause        : std_ulogic_vector(02 downto 0); -- dcsr.cause (R/-): why was debug mode entered
344
    dcsr_rd           : std_ulogic_vector(data_width_c-1 downto 0); -- dcsr (R/(W)): debug mode control and status register
345
    dpc               : std_ulogic_vector(data_width_c-1 downto 0); -- dpc (R/W): debug mode program counter
346
    dscratch0         : std_ulogic_vector(data_width_c-1 downto 0); -- dscratch0 (R/W): debug mode scratch register 0
347 6 zero_gravi
  end record;
348
  signal csr : csr_t;
349 2 zero_gravi
 
350 59 zero_gravi
  -- debug mode controller --
351
  type debug_ctrl_state_t is (DEBUG_OFFLINE, DEBUG_PENDING, DEBUG_ONLINE, DEBUG_EXIT);
352
  type debug_ctrl_t is record
353
    state        : debug_ctrl_state_t;
354
    -- decoded state --
355
    running      : std_ulogic; -- debug mode active
356
    pending      : std_ulogic; -- waiting to start debug mode
357
    -- entering triggers --
358
    trig_break   : std_ulogic; -- ebreak instruction
359
    trig_halt    : std_ulogic; -- external request
360
    trig_step    : std_ulogic; -- single-stepping mode
361
    -- leave debug mode --
362
    dret         : std_ulogic; -- executed DRET instruction
363
    -- misc --
364
    ext_halt_req : std_ulogic_vector(1 downto 0); -- rising edge detector for external halt request
365
  end record;
366
  signal debug_ctrl : debug_ctrl_t;
367
 
368 42 zero_gravi
  -- (hpm) counter events --
369
  signal cnt_event, cnt_event_nxt : std_ulogic_vector(hpmcnt_event_size_c-1 downto 0);
370
  signal hpmcnt_trigger           : std_ulogic_vector(HPM_NUM_CNTS-1 downto 0);
371
 
372 6 zero_gravi
  -- illegal instruction check --
373 36 zero_gravi
  signal illegal_opcode_lsbs : std_ulogic; -- if opcode != rv32
374 2 zero_gravi
  signal illegal_instruction : std_ulogic;
375
  signal illegal_register    : std_ulogic; -- only for E-extension
376
  signal illegal_compressed  : std_ulogic; -- only fir C-extension
377
 
378 15 zero_gravi
  -- access (privilege) check --
379
  signal csr_acc_valid : std_ulogic; -- valid CSR access (implemented and valid access rights)
380
 
381 2 zero_gravi
begin
382
 
383 6 zero_gravi
-- ****************************************************************************************************************************
384 56 zero_gravi
-- Instruction Fetch (always fetch 32-bit-aligned 32-bit chunks of data)
385 6 zero_gravi
-- ****************************************************************************************************************************
386
 
387
  -- Fetch Engine FSM Sync ------------------------------------------------------------------
388
  -- -------------------------------------------------------------------------------------------
389 31 zero_gravi
  fetch_engine_fsm_sync: process(rstn_i, clk_i)
390 6 zero_gravi
  begin
391
    if (rstn_i = '0') then
392 57 zero_gravi
      fetch_engine.state      <= IFETCH_REQUEST;
393
      fetch_engine.state_prev <= IFETCH_REQUEST;
394
      fetch_engine.restart    <= '1';
395 56 zero_gravi
      fetch_engine.pc         <= (others => def_rst_val_c);
396 6 zero_gravi
    elsif rising_edge(clk_i) then
397 57 zero_gravi
      fetch_engine.state      <= fetch_engine.state_nxt;
398
      fetch_engine.state_prev <= fetch_engine.state;
399
      fetch_engine.restart    <= fetch_engine.restart_nxt;
400
      if (fetch_engine.restart = '1') then
401
        fetch_engine.pc <= execute_engine.pc(data_width_c-1 downto 1) & '0'; -- initialize with "real" application PC
402 6 zero_gravi
      else
403 57 zero_gravi
        fetch_engine.pc <= fetch_engine.pc_nxt;
404 6 zero_gravi
      end if;
405
    end if;
406
  end process fetch_engine_fsm_sync;
407
 
408 12 zero_gravi
  -- PC output --
409 31 zero_gravi
  fetch_pc_o <= fetch_engine.pc(data_width_c-1 downto 1) & '0'; -- half-word aligned
410 6 zero_gravi
 
411 12 zero_gravi
 
412 6 zero_gravi
  -- Fetch Engine FSM Comb ------------------------------------------------------------------
413
  -- -------------------------------------------------------------------------------------------
414 31 zero_gravi
  fetch_engine_fsm_comb: process(fetch_engine, execute_engine, ipb, instr_i, bus_i_wait_i, be_instr_i, ma_instr_i)
415 6 zero_gravi
  begin
416
    -- arbiter defaults --
417 31 zero_gravi
    bus_fast_ir              <= '0';
418
    fetch_engine.state_nxt   <= fetch_engine.state;
419
    fetch_engine.pc_nxt      <= fetch_engine.pc;
420
    fetch_engine.bus_err_ack <= '0';
421 57 zero_gravi
    fetch_engine.restart_nxt <= fetch_engine.restart or fetch_engine.reset;
422 6 zero_gravi
 
423
    -- instruction prefetch buffer interface --
424
    ipb.we    <= '0';
425 31 zero_gravi
    ipb.wdata <= be_instr_i & ma_instr_i & instr_i(31 downto 0); -- store exception info and instruction word
426 57 zero_gravi
    ipb.clear <= fetch_engine.restart;
427 6 zero_gravi
 
428
    -- state machine --
429
    case fetch_engine.state is
430
 
431 57 zero_gravi
      when IFETCH_REQUEST => -- request new 32-bit-aligned instruction word
432 6 zero_gravi
      -- ------------------------------------------------------------
433 57 zero_gravi
        if (ipb.free = '1') and (fetch_engine.restart = '0') then -- free entry in buffer AND no reset request?
434 31 zero_gravi
          bus_fast_ir            <= '1'; -- fast instruction fetch request
435
          fetch_engine.state_nxt <= IFETCH_ISSUE;
436
        end if;
437 57 zero_gravi
        if (fetch_engine.restart = '1') then -- reset request?
438
          fetch_engine.restart_nxt <= '0';
439
        end if;
440 6 zero_gravi
 
441 31 zero_gravi
      when IFETCH_ISSUE => -- store instruction data to prefetch buffer
442 6 zero_gravi
      -- ------------------------------------------------------------
443 41 zero_gravi
        fetch_engine.bus_err_ack <= be_instr_i or ma_instr_i; -- ACK bus/alignment errors
444 12 zero_gravi
        if (bus_i_wait_i = '0') or (be_instr_i = '1') or (ma_instr_i = '1') then -- wait for bus response
445 57 zero_gravi
          fetch_engine.pc_nxt <= std_ulogic_vector(unsigned(fetch_engine.pc) + 4);
446
          ipb.we              <= not fetch_engine.restart; -- write to IPB if not being reset
447
          if (fetch_engine.restart = '1') then -- reset request?
448
            fetch_engine.restart_nxt <= '0';
449
          end if;
450 39 zero_gravi
          fetch_engine.state_nxt <= IFETCH_REQUEST;
451 6 zero_gravi
        end if;
452 11 zero_gravi
 
453 6 zero_gravi
      when others => -- undefined
454
      -- ------------------------------------------------------------
455 57 zero_gravi
        fetch_engine.state_nxt <= IFETCH_REQUEST;
456 6 zero_gravi
 
457
    end case;
458
  end process fetch_engine_fsm_comb;
459
 
460
 
461
-- ****************************************************************************************************************************
462
-- Instruction Prefetch Buffer
463
-- ****************************************************************************************************************************
464
 
465 20 zero_gravi
  -- Instruction Prefetch Buffer (FIFO) -----------------------------------------------------
466 6 zero_gravi
  -- -------------------------------------------------------------------------------------------
467 56 zero_gravi
  instr_prefetch_buffer_ctrl: process(rstn_i, clk_i)
468 6 zero_gravi
  begin
469 56 zero_gravi
    if (rstn_i = '0') then
470
      ipb.w_pnt <= (others => def_rst_val_c);
471
      ipb.r_pnt <= (others => def_rst_val_c);
472
    elsif rising_edge(clk_i) then
473 20 zero_gravi
      -- write port --
474 6 zero_gravi
      if (ipb.clear = '1') then
475 20 zero_gravi
        ipb.w_pnt <= (others => '0');
476 6 zero_gravi
      elsif (ipb.we = '1') then
477 20 zero_gravi
        ipb.w_pnt <= std_ulogic_vector(unsigned(ipb.w_pnt) + 1);
478
      end if;
479 36 zero_gravi
      -- read port --
480 20 zero_gravi
      if (ipb.clear = '1') then
481
        ipb.r_pnt <= (others => '0');
482 6 zero_gravi
      elsif (ipb.re = '1') then
483 20 zero_gravi
        ipb.r_pnt <= std_ulogic_vector(unsigned(ipb.r_pnt) + 1);
484 6 zero_gravi
      end if;
485 20 zero_gravi
    end if;
486 56 zero_gravi
  end process instr_prefetch_buffer_ctrl;
487 20 zero_gravi
 
488 56 zero_gravi
  instr_prefetch_buffer_data: process(clk_i)
489
  begin
490
    if rising_edge(clk_i) then
491
      if (ipb.we = '1') then -- write access
492
        ipb.data(to_integer(unsigned(ipb.w_pnt(ipb.w_pnt'left-1 downto 0)))) <= ipb.wdata;
493
      end if;
494
    end if;
495
  end process instr_prefetch_buffer_data;
496
 
497 20 zero_gravi
  -- async read --
498 31 zero_gravi
  ipb.rdata <= ipb.data(to_integer(unsigned(ipb.r_pnt(ipb.r_pnt'left-1 downto 0))));
499 20 zero_gravi
 
500 6 zero_gravi
  -- status --
501 40 zero_gravi
  ipb.match <= '1' when (ipb.r_pnt(ipb.r_pnt'left-1 downto 0) = ipb.w_pnt(ipb.w_pnt'left-1 downto 0))  else '0';
502 34 zero_gravi
  ipb.full  <= '1' when (ipb.r_pnt(ipb.r_pnt'left) /= ipb.w_pnt(ipb.w_pnt'left)) and (ipb.match = '1') else '0';
503
  ipb.empty <= '1' when (ipb.r_pnt(ipb.r_pnt'left)  = ipb.w_pnt(ipb.w_pnt'left)) and (ipb.match = '1') else '0';
504 20 zero_gravi
  ipb.free  <= not ipb.full;
505
  ipb.avail <= not ipb.empty;
506 6 zero_gravi
 
507
 
508
-- ****************************************************************************************************************************
509 31 zero_gravi
-- Instruction Issue (recoding of compressed instructions and 32-bit instruction word construction)
510
-- ****************************************************************************************************************************
511
 
512
  -- Issue Engine FSM Sync ------------------------------------------------------------------
513
  -- -------------------------------------------------------------------------------------------
514
  issue_engine_fsm_sync: process(rstn_i, clk_i)
515
  begin
516
    if (rstn_i = '0') then
517
      issue_engine.state <= ISSUE_ACTIVE;
518 40 zero_gravi
      issue_engine.align <= CPU_BOOT_ADDR(1); -- 32- or 16-bit boundary
519 56 zero_gravi
      issue_engine.buf   <= (others => def_rst_val_c);
520 31 zero_gravi
    elsif rising_edge(clk_i) then
521
      if (ipb.clear = '1') then
522
        if (CPU_EXTENSION_RISCV_C = true) then
523
          if (execute_engine.pc(1) = '1') then -- branch to unaligned address?
524
            issue_engine.state <= ISSUE_REALIGN;
525
            issue_engine.align <= '1'; -- aligned on 16-bit boundary
526
          else
527
            issue_engine.state <= issue_engine.state_nxt;
528
            issue_engine.align <= '0'; -- aligned on 32-bit boundary
529
          end if;
530
        else
531
          issue_engine.state <= issue_engine.state_nxt;
532
          issue_engine.align <= '0'; -- always aligned on 32-bit boundaries
533
        end if;
534
      else
535
        issue_engine.state <= issue_engine.state_nxt;
536
        issue_engine.align <= issue_engine.align_nxt;
537
      end if;
538
      issue_engine.buf <= issue_engine.buf_nxt;
539
    end if;
540
  end process issue_engine_fsm_sync;
541
 
542
 
543
  -- Issue Engine FSM Comb ------------------------------------------------------------------
544
  -- -------------------------------------------------------------------------------------------
545 37 zero_gravi
  issue_engine_fsm_comb: process(issue_engine, ipb, execute_engine, ci_illegal, ci_instr32)
546 31 zero_gravi
  begin
547
    -- arbiter defaults --
548
    issue_engine.state_nxt <= issue_engine.state;
549
    issue_engine.align_nxt <= issue_engine.align;
550
    issue_engine.buf_nxt   <= issue_engine.buf;
551
 
552
    -- instruction prefetch buffer interface defaults --
553
    ipb.re <= '0';
554
 
555 37 zero_gravi
    -- instruction issue interface defaults --
556
    -- cmd_issue.data = <illegal_compressed_instruction> & <bus_error & alignment_error> & <is_compressed_instrucion> & <32-bit_instruction_word>
557
    cmd_issue.data  <= '0' & ipb.rdata(33 downto 32) & '0' & ipb.rdata(31 downto 0);
558
    cmd_issue.valid <= '0';
559 31 zero_gravi
 
560
    -- state machine --
561
    case issue_engine.state is
562
 
563
      when ISSUE_ACTIVE => -- issue instruction if available
564
      -- ------------------------------------------------------------
565
        if (ipb.avail = '1') then -- instructions available?
566
 
567
          if (issue_engine.align = '0') or (CPU_EXTENSION_RISCV_C = false) then -- begin check in LOW instruction half-word
568 41 zero_gravi
            if (execute_engine.state = DISPATCH) then -- ready to issue new command?
569 39 zero_gravi
              cmd_issue.valid      <= '1';
570 31 zero_gravi
              issue_engine.buf_nxt <= ipb.rdata(33 downto 32) & ipb.rdata(31 downto 16); -- store high half-word - we might need it for an unaligned uncompressed instruction
571
              if (ipb.rdata(1 downto 0) = "11") or (CPU_EXTENSION_RISCV_C = false) then -- uncompressed and "aligned"
572 37 zero_gravi
                ipb.re <= '1';
573
                cmd_issue.data <= '0' & ipb.rdata(33 downto 32) & '0' & ipb.rdata(31 downto 0);
574 31 zero_gravi
              else -- compressed
575 37 zero_gravi
                ipb.re <= '1';
576
                cmd_issue.data <= ci_illegal & ipb.rdata(33 downto 32) & '1' & ci_instr32;
577 31 zero_gravi
                issue_engine.align_nxt <= '1';
578
              end if;
579
            end if;
580
 
581
          else -- begin check in HIGH instruction half-word
582 41 zero_gravi
            if (execute_engine.state = DISPATCH) then -- ready to issue new command?
583 39 zero_gravi
              cmd_issue.valid      <= '1';
584 31 zero_gravi
              issue_engine.buf_nxt <= ipb.rdata(33 downto 32) & ipb.rdata(31 downto 16); -- store high half-word - we might need it for an unaligned uncompressed instruction
585
              if (issue_engine.buf(1 downto 0) = "11") then -- uncompressed and "unaligned"
586 37 zero_gravi
                ipb.re <= '1';
587
                cmd_issue.data <= '0' & issue_engine.buf(17 downto 16) & '0' & (ipb.rdata(15 downto 0) & issue_engine.buf(15 downto 0));
588 31 zero_gravi
              else -- compressed
589 36 zero_gravi
                -- do not read from ipb here!
590 37 zero_gravi
                cmd_issue.data <= ci_illegal & ipb.rdata(33 downto 32) & '1' & ci_instr32;
591 31 zero_gravi
                issue_engine.align_nxt <= '0';
592
              end if;
593
            end if;
594
          end if;
595
        end if;
596
 
597
      when ISSUE_REALIGN => -- re-align input fifos after a branch to an unaligned address
598
      -- ------------------------------------------------------------
599
        issue_engine.buf_nxt <= ipb.rdata(33 downto 32) & ipb.rdata(31 downto 16);
600
        if (ipb.avail = '1') then -- instructions available?
601
          ipb.re <= '1';
602
          issue_engine.state_nxt <= ISSUE_ACTIVE;
603
        end if;
604
 
605
      when others => -- undefined
606
      -- ------------------------------------------------------------
607
        issue_engine.state_nxt <= ISSUE_ACTIVE;
608
 
609
    end case;
610
  end process issue_engine_fsm_comb;
611
 
612 41 zero_gravi
  -- 16-bit instructions: half-word select --
613 31 zero_gravi
  ci_instr16 <= ipb.rdata(15 downto 0) when (issue_engine.align = '0') else issue_engine.buf(15 downto 0);
614
 
615
 
616
  -- Compressed Instructions Recoding -------------------------------------------------------
617
  -- -------------------------------------------------------------------------------------------
618
  neorv32_cpu_decompressor_inst_true:
619
  if (CPU_EXTENSION_RISCV_C = true) generate
620
    neorv32_cpu_decompressor_inst: neorv32_cpu_decompressor
621
    port map (
622
      -- instruction input --
623
      ci_instr16_i => ci_instr16, -- compressed instruction input
624
      -- instruction output --
625
      ci_illegal_o => ci_illegal, -- is an illegal compressed instruction
626
      ci_instr32_o => ci_instr32  -- 32-bit decompressed instruction
627
    );
628
  end generate;
629
 
630
  neorv32_cpu_decompressor_inst_false:
631
  if (CPU_EXTENSION_RISCV_C = false) generate
632
    ci_instr32 <= (others => '0');
633
    ci_illegal <= '0';
634
  end generate;
635
 
636
 
637
-- ****************************************************************************************************************************
638 6 zero_gravi
-- Instruction Execution
639
-- ****************************************************************************************************************************
640
 
641 2 zero_gravi
  -- Immediate Generator --------------------------------------------------------------------
642
  -- -------------------------------------------------------------------------------------------
643 56 zero_gravi
  imm_gen: process(execute_engine.i_reg, rstn_i, clk_i)
644 37 zero_gravi
    variable opcode_v : std_ulogic_vector(6 downto 0);
645 2 zero_gravi
  begin
646 38 zero_gravi
    opcode_v := execute_engine.i_reg(instr_opcode_msb_c downto instr_opcode_lsb_c+2) & "11";
647 56 zero_gravi
    if (rstn_i = '0') then
648
      imm_o <= (others => def_rst_val_c);
649
    elsif rising_edge(clk_i) then
650 49 zero_gravi
      if (execute_engine.state = BRANCH) then -- next_PC as immediate for jump-and-link operations (=return address) via ALU.MOV_B
651 39 zero_gravi
        imm_o <= execute_engine.next_pc;
652 49 zero_gravi
      else -- "normal" immediate from instruction word
653
        case opcode_v is -- save some bits here, the two LSBs are always "11" for rv32
654 53 zero_gravi
          when opcode_store_c => -- S-immediate
655 39 zero_gravi
            imm_o(31 downto 11) <= (others => execute_engine.i_reg(31)); -- sign extension
656
            imm_o(10 downto 05) <= execute_engine.i_reg(30 downto 25);
657
            imm_o(04 downto 01) <= execute_engine.i_reg(11 downto 08);
658
            imm_o(00)           <= execute_engine.i_reg(07);
659
          when opcode_branch_c => -- B-immediate
660
            imm_o(31 downto 12) <= (others => execute_engine.i_reg(31)); -- sign extension
661
            imm_o(11)           <= execute_engine.i_reg(07);
662
            imm_o(10 downto 05) <= execute_engine.i_reg(30 downto 25);
663
            imm_o(04 downto 01) <= execute_engine.i_reg(11 downto 08);
664
            imm_o(00)           <= '0';
665
          when opcode_lui_c | opcode_auipc_c => -- U-immediate
666
            imm_o(31 downto 20) <= execute_engine.i_reg(31 downto 20);
667
            imm_o(19 downto 12) <= execute_engine.i_reg(19 downto 12);
668
            imm_o(11 downto 00) <= (others => '0');
669
          when opcode_jal_c => -- J-immediate
670
            imm_o(31 downto 20) <= (others => execute_engine.i_reg(31)); -- sign extension
671
            imm_o(19 downto 12) <= execute_engine.i_reg(19 downto 12);
672
            imm_o(11)           <= execute_engine.i_reg(20);
673
            imm_o(10 downto 05) <= execute_engine.i_reg(30 downto 25);
674
            imm_o(04 downto 01) <= execute_engine.i_reg(24 downto 21);
675
            imm_o(00)           <= '0';
676
          when opcode_atomic_c => -- atomic memory access
677 40 zero_gravi
            imm_o               <= (others => '0'); -- effective address is addr = reg + 0 = reg
678 39 zero_gravi
          when others => -- I-immediate
679
            imm_o(31 downto 11) <= (others => execute_engine.i_reg(31)); -- sign extension
680
            imm_o(10 downto 05) <= execute_engine.i_reg(30 downto 25);
681
            imm_o(04 downto 01) <= execute_engine.i_reg(24 downto 21);
682
            imm_o(00)           <= execute_engine.i_reg(20);
683
        end case;
684
      end if;
685 2 zero_gravi
    end if;
686
  end process imm_gen;
687
 
688
 
689
  -- Branch Condition Check -----------------------------------------------------------------
690
  -- -------------------------------------------------------------------------------------------
691 6 zero_gravi
  branch_check: process(execute_engine.i_reg, cmp_i)
692 2 zero_gravi
  begin
693 6 zero_gravi
    case execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) is
694 2 zero_gravi
      when funct3_beq_c => -- branch if equal
695 47 zero_gravi
        execute_engine.branch_taken <= cmp_i(cmp_equal_c);
696 2 zero_gravi
      when funct3_bne_c => -- branch if not equal
697 47 zero_gravi
        execute_engine.branch_taken <= not cmp_i(cmp_equal_c);
698 2 zero_gravi
      when funct3_blt_c | funct3_bltu_c => -- branch if less (signed/unsigned)
699 47 zero_gravi
        execute_engine.branch_taken <= cmp_i(cmp_less_c);
700 2 zero_gravi
      when funct3_bge_c | funct3_bgeu_c => -- branch if greater or equal (signed/unsigned)
701 47 zero_gravi
        execute_engine.branch_taken <= not cmp_i(cmp_less_c);
702 2 zero_gravi
      when others => -- undefined
703 6 zero_gravi
        execute_engine.branch_taken <= '0';
704 2 zero_gravi
    end case;
705
  end process branch_check;
706
 
707
 
708 6 zero_gravi
  -- Execute Engine FSM Sync ----------------------------------------------------------------
709 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
710 56 zero_gravi
  execute_engine_fsm_sync: process(rstn_i, clk_i)
711 2 zero_gravi
  begin
712
    if (rstn_i = '0') then
713 56 zero_gravi
      -- registers that DO require a specific reset state --
714 49 zero_gravi
      execute_engine.pc       <= CPU_BOOT_ADDR(data_width_c-1 downto 1) & '0';
715
      execute_engine.state    <= SYS_WAIT;
716
      execute_engine.sleep    <= '0';
717
      execute_engine.branched <= '1'; -- reset is a branch from "somewhere"
718 57 zero_gravi
      -- no dedicated RESET required --
719 56 zero_gravi
      execute_engine.state_prev <= SYS_WAIT;
720
      execute_engine.i_reg      <= (others => def_rst_val_c);
721
      execute_engine.is_ci      <= def_rst_val_c;
722
      execute_engine.is_cp_op   <= def_rst_val_c;
723
      execute_engine.last_pc    <= (others => def_rst_val_c);
724
      execute_engine.i_reg_last <= (others => def_rst_val_c);
725
      execute_engine.next_pc    <= (others => def_rst_val_c);
726
      ctrl                      <= (others => def_rst_val_c);
727
      --
728
      ctrl(ctrl_bus_rd_c)       <= '0';
729
      ctrl(ctrl_bus_wr_c)       <= '0';
730 2 zero_gravi
    elsif rising_edge(clk_i) then
731 39 zero_gravi
      -- PC update --
732
      if (execute_engine.pc_we = '1') then
733 49 zero_gravi
        if (execute_engine.pc_mux_sel = '0') then
734 58 zero_gravi
          execute_engine.pc <= execute_engine.next_pc(data_width_c-1 downto 1) & '0'; -- normal (linear) increment OR trap enter/exit
735 49 zero_gravi
        else
736
          execute_engine.pc <= alu_add_i(data_width_c-1 downto 1) & '0'; -- jump/taken_branch
737
        end if;
738 39 zero_gravi
      end if;
739
      --
740 49 zero_gravi
      execute_engine.state    <= execute_engine.state_nxt;
741
      execute_engine.sleep    <= execute_engine.sleep_nxt;
742
      execute_engine.branched <= execute_engine.branched_nxt;
743 56 zero_gravi
      --
744 42 zero_gravi
      execute_engine.state_prev <= execute_engine.state;
745
      execute_engine.i_reg      <= execute_engine.i_reg_nxt;
746
      execute_engine.is_ci      <= execute_engine.is_ci_nxt;
747
      execute_engine.is_cp_op   <= execute_engine.is_cp_op_nxt;
748 59 zero_gravi
 
749 49 zero_gravi
      -- PC & IR of "last executed" instruction --
750 40 zero_gravi
      if (execute_engine.state = EXECUTE) then
751
        execute_engine.last_pc    <= execute_engine.pc;
752 39 zero_gravi
        execute_engine.i_reg_last <= execute_engine.i_reg;
753
      end if;
754 59 zero_gravi
 
755 49 zero_gravi
      -- next PC --
756
      case execute_engine.state is
757 59 zero_gravi
        when TRAP_ENTER =>
758
          if (CPU_EXTENSION_RISCV_DEBUG = false) then -- normal trapping
759
            execute_engine.next_pc <= csr.mtvec(data_width_c-1 downto 1) & '0'; -- trap enter
760
          else -- DEBUG MODE enabled
761
            if (trap_ctrl.cause(5) = '1') then -- trap cause: debug mode (re-)entry
762
              execute_engine.next_pc <= CPU_DEBUG_ADDR; -- debug mode enter; start at "parking loop" <normal_entry>
763
            elsif (debug_ctrl.running = '1') then -- any other exception INSIDE debug mode
764
              execute_engine.next_pc <= std_ulogic_vector(unsigned(CPU_DEBUG_ADDR) + 4); -- execute at "parking loop" <exception_entry>
765
            else -- normal trapping
766
              execute_engine.next_pc <= csr.mtvec(data_width_c-1 downto 1) & '0'; -- trap enter
767
            end if;
768
          end if;
769
        when TRAP_EXIT =>
770
          if (CPU_EXTENSION_RISCV_DEBUG = false) or (debug_ctrl.running = '0') then -- normal end of trap
771
            execute_engine.next_pc <= csr.mepc(data_width_c-1 downto 1) & '0'; -- trap exit
772
          else -- DEBUG MODE exiting
773
            execute_engine.next_pc <= csr.dpc(data_width_c-1 downto 1) & '0'; -- debug mode exit
774
          end if;
775
        when EXECUTE =>
776
          execute_engine.next_pc <= std_ulogic_vector(unsigned(execute_engine.pc) + unsigned(execute_engine.next_pc_inc)); -- next linear PC
777
        when others =>
778
          NULL;
779 49 zero_gravi
      end case;
780 59 zero_gravi
 
781 39 zero_gravi
      -- main control bus --
782 6 zero_gravi
      ctrl <= ctrl_nxt;
783 2 zero_gravi
    end if;
784 6 zero_gravi
  end process execute_engine_fsm_sync;
785 2 zero_gravi
 
786 56 zero_gravi
 
787 49 zero_gravi
  -- PC increment for next linear instruction (+2 for compressed instr., +4 otherwise) --
788
  execute_engine.next_pc_inc <= x"00000004" when ((execute_engine.is_ci = '0') or (CPU_EXTENSION_RISCV_C = false)) else x"00000002";
789 41 zero_gravi
 
790 20 zero_gravi
  -- PC output --
791 39 zero_gravi
  curr_pc_o <= execute_engine.pc(data_width_c-1 downto 1) & '0'; -- PC for ALU ops
792 6 zero_gravi
 
793 49 zero_gravi
  -- CSR access address --
794
  csr.addr <= execute_engine.i_reg(instr_csr_id_msb_c downto instr_csr_id_lsb_c);
795 20 zero_gravi
 
796 49 zero_gravi
 
797 6 zero_gravi
  -- CPU Control Bus Output -----------------------------------------------------------------
798
  -- -------------------------------------------------------------------------------------------
799 60 zero_gravi
  ctrl_output: process(ctrl, fetch_engine, trap_ctrl, bus_fast_ir, execute_engine, csr, debug_ctrl)
800 2 zero_gravi
  begin
801 36 zero_gravi
    -- signals from execute engine --
802 2 zero_gravi
    ctrl_o <= ctrl;
803 36 zero_gravi
    -- current privilege level --
804 59 zero_gravi
    ctrl_o(ctrl_priv_lvl_msb_c downto ctrl_priv_lvl_lsb_c) <= csr.privilege_rd;
805 36 zero_gravi
    -- register addresses --
806 40 zero_gravi
    ctrl_o(ctrl_rf_rs1_adr4_c downto ctrl_rf_rs1_adr0_c) <= execute_engine.i_reg(instr_rs1_msb_c downto instr_rs1_lsb_c);
807
    ctrl_o(ctrl_rf_rs2_adr4_c downto ctrl_rf_rs2_adr0_c) <= execute_engine.i_reg(instr_rs2_msb_c downto instr_rs2_lsb_c);
808
    ctrl_o(ctrl_rf_rd_adr4_c  downto ctrl_rf_rd_adr0_c)  <= execute_engine.i_reg(instr_rd_msb_c  downto instr_rd_lsb_c);
809 12 zero_gravi
    -- fast bus access requests --
810 36 zero_gravi
    ctrl_o(ctrl_bus_if_c) <= bus_fast_ir;
811 12 zero_gravi
    -- bus error control --
812 47 zero_gravi
    ctrl_o(ctrl_bus_ierr_ack_c) <= fetch_engine.bus_err_ack; -- instruction fetch bus access error ACK
813
    ctrl_o(ctrl_bus_derr_ack_c) <= trap_ctrl.env_start_ack; -- data access bus error access ACK
814
    -- memory access size / sign --
815
    ctrl_o(ctrl_bus_unsigned_c) <= execute_engine.i_reg(instr_funct3_msb_c); -- unsigned LOAD (LBU, LHU)
816
    ctrl_o(ctrl_bus_size_msb_c downto ctrl_bus_size_lsb_c) <= execute_engine.i_reg(instr_funct3_lsb_c+1 downto instr_funct3_lsb_c); -- mem transfer size
817
    -- alu.shifter --
818
    ctrl_o(ctrl_alu_shift_dir_c) <= execute_engine.i_reg(instr_funct3_msb_c); -- shift direction (left/right)
819
    ctrl_o(ctrl_alu_shift_ar_c)  <= execute_engine.i_reg(30); -- is arithmetic shift
820 36 zero_gravi
    -- instruction's function blocks (for co-processors) --
821 44 zero_gravi
    ctrl_o(ctrl_ir_opcode7_6_c  downto ctrl_ir_opcode7_0_c) <= execute_engine.i_reg(instr_opcode_msb_c  downto instr_opcode_lsb_c);
822 36 zero_gravi
    ctrl_o(ctrl_ir_funct12_11_c downto ctrl_ir_funct12_0_c) <= execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c);
823
    ctrl_o(ctrl_ir_funct3_2_c   downto ctrl_ir_funct3_0_c)  <= execute_engine.i_reg(instr_funct3_msb_c  downto instr_funct3_lsb_c);
824 47 zero_gravi
    -- cpu status --
825 60 zero_gravi
    ctrl_o(ctrl_sleep_c) <= execute_engine.sleep; -- cpu is in sleep mode
826
    ctrl_o(ctrl_trap_c)  <= trap_ctrl.env_start_ack; -- cpu is starting a trap handler
827 59 zero_gravi
    if (CPU_EXTENSION_RISCV_DEBUG = true) then
828
      ctrl_o(ctrl_debug_running_c) <= debug_ctrl.running; -- cpu is currently in debug mode
829
    else
830
      ctrl_o(ctrl_debug_running_c) <= '0';
831
    end if;
832 6 zero_gravi
  end process ctrl_output;
833 2 zero_gravi
 
834
 
835 44 zero_gravi
  -- Decoding Helper Logic ------------------------------------------------------------------
836
  -- -------------------------------------------------------------------------------------------
837
  decode_helper: process(execute_engine)
838 49 zero_gravi
    variable sys_env_cmd_mask_v : std_ulogic_vector(11 downto 0);
839 44 zero_gravi
  begin
840
    -- defaults --
841 60 zero_gravi
    decode_aux.alu_immediate <= '0';
842
    decode_aux.rs1_is_r0     <= '0';
843
    decode_aux.is_atomic_lr  <= '0';
844
    decode_aux.is_atomic_sc  <= '0';
845
    decode_aux.is_float_op   <= '0';
846 44 zero_gravi
 
847
    -- is immediate ALU operation? --
848
    decode_aux.alu_immediate <= not execute_engine.i_reg(instr_opcode_msb_c-1);
849
 
850
    -- is rs1 == r0? --
851 60 zero_gravi
    decode_aux.rs1_is_r0 <= not or_reduce_f(execute_engine.i_reg(instr_rs1_msb_c downto instr_rs1_lsb_c));
852 44 zero_gravi
 
853
    -- is atomic load-reservate/store-conditional? --
854 52 zero_gravi
    if (CPU_EXTENSION_RISCV_A = true) and (execute_engine.i_reg(instr_opcode_lsb_c+3 downto instr_opcode_lsb_c+2) = "11") then -- valid atomic sub-opcode
855 44 zero_gravi
      decode_aux.is_atomic_lr <= not execute_engine.i_reg(instr_funct5_lsb_c);
856
      decode_aux.is_atomic_sc <=     execute_engine.i_reg(instr_funct5_lsb_c);
857
    end if;
858
 
859 53 zero_gravi
    -- floating-point operations (Zfinx) --
860
    if ((execute_engine.i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c+3) = "0000")) or -- FADD.S / FSUB.S
861 52 zero_gravi
       ((execute_engine.i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c+2) = "00010")) or -- FMUL.S
862 53 zero_gravi
       ((execute_engine.i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c+2) = "11100") and (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = "001")) or -- FCLASS.S
863 52 zero_gravi
       ((execute_engine.i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c+2) = "00100") and (execute_engine.i_reg(instr_funct3_msb_c) = '0')) or -- FSGNJ[N/X].S
864
       ((execute_engine.i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c+2) = "00101") and (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_msb_c-1) = "00")) or -- FMIN.S / FMAX.S
865
       ((execute_engine.i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c+2) = "10100") and (execute_engine.i_reg(instr_funct3_msb_c) = '0')) or -- FEQ.S / FLT.S / FLE.S
866 53 zero_gravi
       ((execute_engine.i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c+2) = "11010") and (execute_engine.i_reg(instr_funct12_lsb_c+4 downto instr_funct12_lsb_c+1) = "0000")) or -- FCVT.S.W*
867 52 zero_gravi
       ((execute_engine.i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c+2) = "11000") and (execute_engine.i_reg(instr_funct12_lsb_c+4 downto instr_funct12_lsb_c+1) = "0000")) then -- FCVT.W*.S
868 53 zero_gravi
      decode_aux.is_float_op <= '1';
869 52 zero_gravi
    end if;
870
 
871 49 zero_gravi
    -- system/environment instructions --
872 59 zero_gravi
    sys_env_cmd_mask_v := funct12_ecall_c or funct12_ebreak_c or funct12_mret_c or funct12_wfi_c or funct12_dret_c; -- sum-up set bits
873 60 zero_gravi
    decode_aux.sys_env_cmd <= execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) and sys_env_cmd_mask_v; -- set unused bits to always-zero
874 44 zero_gravi
  end process decode_helper;
875
 
876
 
877 6 zero_gravi
  -- Execute Engine FSM Comb ----------------------------------------------------------------
878
  -- -------------------------------------------------------------------------------------------
879 59 zero_gravi
  execute_engine_fsm_comb: process(execute_engine, debug_ctrl, decode_aux, fetch_engine, cmd_issue, trap_ctrl, csr, ctrl, csr_acc_valid,
880 57 zero_gravi
                                   alu_wait_i, bus_d_wait_i, ma_load_i, be_load_i, ma_store_i, be_store_i, excl_state_i)
881 44 zero_gravi
    variable opcode_v : std_ulogic_vector(6 downto 0);
882 2 zero_gravi
  begin
883
    -- arbiter defaults --
884 29 zero_gravi
    execute_engine.state_nxt    <= execute_engine.state;
885
    execute_engine.i_reg_nxt    <= execute_engine.i_reg;
886
    execute_engine.is_cp_op_nxt <= execute_engine.is_cp_op;
887
    execute_engine.is_ci_nxt    <= execute_engine.is_ci;
888
    execute_engine.sleep_nxt    <= execute_engine.sleep;
889 49 zero_gravi
    execute_engine.branched_nxt <= execute_engine.branched;
890 39 zero_gravi
    --
891 49 zero_gravi
    execute_engine.pc_mux_sel   <= '0';
892 39 zero_gravi
    execute_engine.pc_we        <= '0';
893 2 zero_gravi
 
894 6 zero_gravi
    -- instruction dispatch --
895 37 zero_gravi
    fetch_engine.reset          <= '0';
896 2 zero_gravi
 
897 6 zero_gravi
    -- trap environment control --
898 37 zero_gravi
    trap_ctrl.env_start_ack     <= '0';
899
    trap_ctrl.env_end           <= '0';
900 6 zero_gravi
 
901 59 zero_gravi
    -- leave debug mode --
902
    debug_ctrl.dret             <= '0';
903
 
904 2 zero_gravi
    -- exception trigger --
905 37 zero_gravi
    trap_ctrl.instr_be          <= '0';
906
    trap_ctrl.instr_ma          <= '0';
907
    trap_ctrl.env_call          <= '0';
908
    trap_ctrl.break_point       <= '0';
909
    illegal_compressed          <= '0';
910 2 zero_gravi
 
911 6 zero_gravi
    -- CSR access --
912 37 zero_gravi
    csr.we_nxt                  <= '0';
913
    csr.re_nxt                  <= '0';
914 6 zero_gravi
 
915 39 zero_gravi
    -- CONTROL DEFAULTS --
916 36 zero_gravi
    ctrl_nxt <= (others => '0'); -- default: all off
917 47 zero_gravi
    -- ALU main control --
918
    ctrl_nxt(ctrl_alu_addsub_c) <= '0'; -- ADD(I)
919
    ctrl_nxt(ctrl_alu_func1_c  downto ctrl_alu_func0_c) <= alu_func_cmd_arith_c; -- default ALU function select: arithmetic
920
    ctrl_nxt(ctrl_alu_arith_c) <= alu_arith_cmd_addsub_c; -- default ALU arithmetic operation: ADDSUB
921
    -- ALU sign control --
922 6 zero_gravi
    if (execute_engine.i_reg(instr_opcode_lsb_c+4) = '1') then -- ALU ops
923 36 zero_gravi
      ctrl_nxt(ctrl_alu_unsigned_c) <= execute_engine.i_reg(instr_funct3_lsb_c+0); -- unsigned ALU operation? (SLTIU, SLTU)
924 2 zero_gravi
    else -- branches
925 36 zero_gravi
      ctrl_nxt(ctrl_alu_unsigned_c) <= execute_engine.i_reg(instr_funct3_lsb_c+1); -- unsigned branches? (BLTU, BGEU)
926 2 zero_gravi
    end if;
927 57 zero_gravi
    -- Atomic store-conditional instruction (evaluate lock status) --
928
    if (CPU_EXTENSION_RISCV_A = true) then
929
      ctrl_nxt(ctrl_bus_ch_lock_c) <= decode_aux.is_atomic_sc;
930
    else
931
      ctrl_nxt(ctrl_bus_ch_lock_c) <= '0';
932
    end if;
933 2 zero_gravi
 
934
 
935 6 zero_gravi
    -- state machine --
936
    case execute_engine.state is
937 2 zero_gravi
 
938 44 zero_gravi
      when SYS_WAIT => -- System delay cycle (used to wait for side effects to kick in) [and to init r0 with zero if it is a physical register]
939 2 zero_gravi
      -- ------------------------------------------------------------
940 26 zero_gravi
        -- set reg_file's r0 to zero --
941 25 zero_gravi
        if (rf_r0_is_reg_c = true) then -- is r0 implemented as physical register, which has to be set to zero?
942 49 zero_gravi
          ctrl_nxt(ctrl_alu_func1_c downto ctrl_alu_func0_c) <= alu_func_cmd_copro_c; -- hacky! CSR read-access CP selected without a valid CSR-read -> results zero
943
          ctrl_nxt(ctrl_cp_id_msb_c downto ctrl_cp_id_lsb_c) <= cp_sel_csr_rd_c; -- use CSR-READ CP
944
          ctrl_nxt(ctrl_rf_r0_we_c)                          <= '1'; -- force RF write access and force rd=r0
945 25 zero_gravi
        end if;
946
        --
947 6 zero_gravi
        execute_engine.state_nxt <= DISPATCH;
948 2 zero_gravi
 
949 39 zero_gravi
 
950 37 zero_gravi
      when DISPATCH => -- Get new command from instruction issue engine
951 25 zero_gravi
      -- ------------------------------------------------------------
952 52 zero_gravi
        -- housekeeping --
953 58 zero_gravi
        execute_engine.is_cp_op_nxt <= '0'; -- no co-processor operation yet
954 49 zero_gravi
        -- PC update --
955
        execute_engine.pc_mux_sel <= '0'; -- linear next PC
956 40 zero_gravi
        -- IR update --
957 49 zero_gravi
        execute_engine.is_ci_nxt <= cmd_issue.data(32); -- flag to indicate a de-compressed instruction
958
        execute_engine.i_reg_nxt <= cmd_issue.data(31 downto 0);
959 40 zero_gravi
        --
960 37 zero_gravi
        if (cmd_issue.valid = '1') then -- instruction available?
961 49 zero_gravi
          -- PC update --
962
          execute_engine.branched_nxt <= '0';
963
          execute_engine.pc_we        <= not execute_engine.branched; -- update PC with linear next_pc if there was no actual branch
964 40 zero_gravi
          -- IR update - exceptions --
965
          trap_ctrl.instr_ma <= cmd_issue.data(33); -- misaligned instruction fetch address
966
          trap_ctrl.instr_be <= cmd_issue.data(34); -- bus access fault during instruction fetch
967
          illegal_compressed <= cmd_issue.data(35); -- invalid decompressed instruction
968
          -- any reason to go to trap state? --
969 37 zero_gravi
          if (execute_engine.sleep = '1') or (trap_ctrl.env_start = '1') or (trap_ctrl.exc_fire = '1') or ((cmd_issue.data(33) or cmd_issue.data(34)) = '1') then
970 49 zero_gravi
            execute_engine.state_nxt <= TRAP_ENTER;
971 13 zero_gravi
          else
972 14 zero_gravi
            execute_engine.state_nxt <= EXECUTE;
973 13 zero_gravi
          end if;
974
        end if;
975 2 zero_gravi
 
976 39 zero_gravi
 
977 59 zero_gravi
      when TRAP_ENTER => -- Start trap environment - get TVEC, stay here for sleep mode
978 2 zero_gravi
      -- ------------------------------------------------------------
979 34 zero_gravi
        if (trap_ctrl.env_start = '1') then -- trap triggered?
980
          trap_ctrl.env_start_ack   <= '1';
981 49 zero_gravi
          execute_engine.state_nxt  <= TRAP_EXECUTE;
982 2 zero_gravi
        end if;
983
 
984 59 zero_gravi
      when TRAP_EXIT => -- Return from trap environment - get EPC
985 49 zero_gravi
      -- ------------------------------------------------------------
986
        trap_ctrl.env_end        <= '1';
987
        execute_engine.state_nxt <= TRAP_EXECUTE;
988 39 zero_gravi
 
989 60 zero_gravi
      when TRAP_EXECUTE => -- Start trap environment -> jump to TVEC / return from trap environment -> jump to EPC
990 49 zero_gravi
      -- ------------------------------------------------------------
991 60 zero_gravi
        execute_engine.pc_mux_sel <= '0'; -- next_PC
992 49 zero_gravi
        fetch_engine.reset        <= '1';
993
        execute_engine.pc_we      <= '1';
994
        execute_engine.sleep_nxt  <= '0'; -- disable sleep mode
995
        execute_engine.state_nxt  <= SYS_WAIT;
996
 
997
 
998 60 zero_gravi
      when EXECUTE => -- Decode and execute instruction (control has to be here for exactly 1 cycle in any case!)
999 2 zero_gravi
      -- ------------------------------------------------------------
1000 36 zero_gravi
        opcode_v := execute_engine.i_reg(instr_opcode_msb_c downto instr_opcode_lsb_c+2) & "11"; -- save some bits here, LSBs are always 11 for rv32
1001
        case opcode_v is
1002 2 zero_gravi
 
1003 60 zero_gravi
          when opcode_alu_c | opcode_alui_c => -- (register/immediate) ALU operation
1004 2 zero_gravi
          -- ------------------------------------------------------------
1005 49 zero_gravi
            ctrl_nxt(ctrl_alu_opa_mux_c) <= '0'; -- use RS1 as ALU.OPA
1006
            ctrl_nxt(ctrl_alu_opb_mux_c) <= decode_aux.alu_immediate; -- use IMM as ALU.OPB for immediate operations
1007
            ctrl_nxt(ctrl_rf_in_mux_c)   <= '0'; -- RF input = ALU result
1008 25 zero_gravi
 
1009 60 zero_gravi
            -- ALU arithmetic operation type --
1010 39 zero_gravi
            if (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_slt_c) or
1011
               (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sltu_c) then
1012
              ctrl_nxt(ctrl_alu_arith_c) <= alu_arith_cmd_slt_c;
1013 29 zero_gravi
            else
1014 39 zero_gravi
              ctrl_nxt(ctrl_alu_arith_c) <= alu_arith_cmd_addsub_c;
1015 25 zero_gravi
            end if;
1016
 
1017 29 zero_gravi
            -- ADD/SUB --
1018 44 zero_gravi
            if ((decode_aux.alu_immediate = '0') and (execute_engine.i_reg(instr_funct7_msb_c-1) = '1')) or -- not an immediate op and funct7.6 set => SUB
1019 29 zero_gravi
               (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_slt_c) or -- SLT operation
1020
               (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sltu_c) then -- SLTU operation
1021
              ctrl_nxt(ctrl_alu_addsub_c) <= '1'; -- SUB/SLT
1022
            else
1023
              ctrl_nxt(ctrl_alu_addsub_c) <= '0'; -- ADD(I)
1024
            end if;
1025
 
1026 39 zero_gravi
            -- ALU logic operation --
1027
            case execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) is -- actual ALU.logic operation (re-coding)
1028
              when funct3_xor_c => ctrl_nxt(ctrl_alu_logic1_c downto ctrl_alu_logic0_c) <= alu_logic_cmd_xor_c; -- XOR(I)
1029
              when funct3_or_c  => ctrl_nxt(ctrl_alu_logic1_c downto ctrl_alu_logic0_c) <= alu_logic_cmd_or_c;  -- OR(I)
1030 40 zero_gravi
              when others       => ctrl_nxt(ctrl_alu_logic1_c downto ctrl_alu_logic0_c) <= alu_logic_cmd_and_c; -- AND(I)
1031 39 zero_gravi
            end case;
1032
 
1033 44 zero_gravi
            -- co-processor MULDIV operation? --
1034
            if (CPU_EXTENSION_RISCV_M = true) and (execute_engine.i_reg(instr_opcode_lsb_c+5) = opcode_alu_c(5)) and (execute_engine.i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c) = "0000001") then -- MULDIV CP op?
1035
              ctrl_nxt(ctrl_cp_id_msb_c downto ctrl_cp_id_lsb_c) <= cp_sel_muldiv_c; -- use MULDIV CP
1036 39 zero_gravi
              execute_engine.is_cp_op_nxt                        <= '1'; -- this is a CP operation
1037
              ctrl_nxt(ctrl_alu_func1_c downto ctrl_alu_func0_c) <= alu_func_cmd_copro_c;
1038 44 zero_gravi
            -- ALU operation, function select --
1039 39 zero_gravi
            else
1040
              execute_engine.is_cp_op_nxt <= '0'; -- no CP operation
1041
              case execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) is -- actual ALU.func operation (re-coding)
1042
                when funct3_xor_c | funct3_or_c | funct3_and_c => ctrl_nxt(ctrl_alu_func1_c downto ctrl_alu_func0_c) <= alu_func_cmd_logic_c;
1043
                when funct3_sll_c | funct3_sr_c                => ctrl_nxt(ctrl_alu_func1_c downto ctrl_alu_func0_c) <= alu_func_cmd_shift_c;
1044
                when others                                    => ctrl_nxt(ctrl_alu_func1_c downto ctrl_alu_func0_c) <= alu_func_cmd_arith_c;
1045
              end case;
1046
            end if;
1047
 
1048 59 zero_gravi
            -- multi cycle ALU operation? --
1049 25 zero_gravi
            if (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sll_c) or -- SLL shift operation?
1050
               (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sr_c) or -- SR shift operation?
1051 60 zero_gravi
               ((CPU_EXTENSION_RISCV_M = true) and (execute_engine.i_reg(instr_opcode_lsb_c+5) = opcode_alu_c(5)) and (execute_engine.i_reg(instr_funct7_lsb_c) = '1')) then -- MULDIV CP op?
1052 6 zero_gravi
              execute_engine.state_nxt <= ALU_WAIT;
1053 26 zero_gravi
            else -- single cycle ALU operation
1054 2 zero_gravi
              ctrl_nxt(ctrl_rf_wb_en_c) <= '1'; -- valid RF write-back
1055 6 zero_gravi
              execute_engine.state_nxt <= DISPATCH;
1056 2 zero_gravi
            end if;
1057
 
1058 25 zero_gravi
          when opcode_lui_c | opcode_auipc_c => -- load upper immediate / add upper immediate to PC
1059 2 zero_gravi
          -- ------------------------------------------------------------
1060 27 zero_gravi
            ctrl_nxt(ctrl_alu_opa_mux_c) <= '1'; -- ALU.OPA = PC (for AUIPC only)
1061
            ctrl_nxt(ctrl_alu_opb_mux_c) <= '1'; -- use IMM as ALU.OPB
1062 39 zero_gravi
            ctrl_nxt(ctrl_alu_arith_c)   <= alu_arith_cmd_addsub_c; -- actual ALU operation = ADD
1063
            ctrl_nxt(ctrl_alu_logic1_c downto ctrl_alu_logic0_c) <= alu_logic_cmd_movb_c; -- MOVB
1064 25 zero_gravi
            if (execute_engine.i_reg(instr_opcode_lsb_c+5) = opcode_lui_c(5)) then -- LUI
1065 39 zero_gravi
              ctrl_nxt(ctrl_alu_func1_c downto ctrl_alu_func0_c) <= alu_func_cmd_logic_c; -- actual ALU operation = MOVB
1066 27 zero_gravi
            else -- AUIPC
1067 39 zero_gravi
              ctrl_nxt(ctrl_alu_func1_c downto ctrl_alu_func0_c) <= alu_func_cmd_arith_c; -- actual ALU operation = ADD
1068 2 zero_gravi
            end if;
1069 49 zero_gravi
            ctrl_nxt(ctrl_rf_in_mux_c) <= '0'; -- RF input = ALU result
1070
            ctrl_nxt(ctrl_rf_wb_en_c)  <= '1'; -- valid RF write-back
1071
            execute_engine.state_nxt   <= DISPATCH;
1072 2 zero_gravi
 
1073 53 zero_gravi
          when opcode_load_c | opcode_store_c | opcode_atomic_c => -- load/store / atomic memory access
1074 2 zero_gravi
          -- ------------------------------------------------------------
1075 57 zero_gravi
            ctrl_nxt(ctrl_alu_opa_mux_c)<= '0'; -- use RS1 as ALU.OPA
1076
            ctrl_nxt(ctrl_alu_opb_mux_c)<= '1'; -- use IMM as ALU.OPB
1077
            ctrl_nxt(ctrl_bus_mo_we_c)  <= '1'; -- write to MAR and MDO (MDO only relevant for store)
1078 39 zero_gravi
            --
1079 52 zero_gravi
            if (CPU_EXTENSION_RISCV_A = false) or -- atomic extension disabled
1080 53 zero_gravi
               (execute_engine.i_reg(instr_opcode_lsb_c+3 downto instr_opcode_lsb_c+2) = "00") then  -- normal integerload/store
1081 39 zero_gravi
              execute_engine.state_nxt <= LOADSTORE_0;
1082
            else -- atomic operation
1083
              if (execute_engine.i_reg(instr_funct5_msb_c downto instr_funct5_lsb_c) = funct5_a_sc_c) or -- store-conditional
1084
                 (execute_engine.i_reg(instr_funct5_msb_c downto instr_funct5_lsb_c) = funct5_a_lr_c) then -- load-reservate
1085
                execute_engine.state_nxt <= LOADSTORE_0;
1086
              else -- unimplemented (atomic) instruction
1087
                execute_engine.state_nxt <= SYS_WAIT;
1088
              end if;
1089
            end if;
1090 2 zero_gravi
 
1091 29 zero_gravi
          when opcode_branch_c | opcode_jal_c | opcode_jalr_c => -- branch / jump and link (with register)
1092 2 zero_gravi
          -- ------------------------------------------------------------
1093 49 zero_gravi
            -- target address (ALU.ADD) operands --
1094 29 zero_gravi
            if (execute_engine.i_reg(instr_opcode_lsb_c+3 downto instr_opcode_lsb_c+2) = opcode_jalr_c(3 downto 2)) then -- JALR
1095
              ctrl_nxt(ctrl_alu_opa_mux_c) <= '0'; -- use RS1 as ALU.OPA (branch target address base)
1096 49 zero_gravi
            else -- JAL
1097 29 zero_gravi
              ctrl_nxt(ctrl_alu_opa_mux_c) <= '1'; -- use PC as ALU.OPA (branch target address base)
1098 2 zero_gravi
            end if;
1099 29 zero_gravi
            ctrl_nxt(ctrl_alu_opb_mux_c) <= '1'; -- use IMM as ALU.OPB (branch target address offset)
1100 49 zero_gravi
            execute_engine.state_nxt     <= BRANCH;
1101 2 zero_gravi
 
1102 8 zero_gravi
          when opcode_fence_c => -- fence operations
1103
          -- ------------------------------------------------------------
1104 39 zero_gravi
            execute_engine.state_nxt <= FENCE_OP;
1105 8 zero_gravi
 
1106 2 zero_gravi
          when opcode_syscsr_c => -- system/csr access
1107
          -- ------------------------------------------------------------
1108 45 zero_gravi
            if (CPU_EXTENSION_RISCV_Zicsr = true) then
1109
              csr.re_nxt <= csr_acc_valid; -- always read CSR if valid access, only relevant for CSR-instructions
1110 49 zero_gravi
              ctrl_nxt(ctrl_alu_func1_c downto ctrl_alu_func0_c) <= alu_func_cmd_copro_c; -- only relevant for CSR-instructions
1111
              ctrl_nxt(ctrl_cp_id_msb_c downto ctrl_cp_id_lsb_c) <= cp_sel_csr_rd_c; -- use CSR-READ CP, only relevant for CSR-instructions
1112 45 zero_gravi
              if (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_env_c) then -- system/environment
1113
                execute_engine.state_nxt <= SYS_ENV;
1114
              else -- CSR access
1115
                execute_engine.state_nxt <= CSR_ACCESS;
1116
              end if;
1117
            else
1118
              execute_engine.state_nxt <= SYS_WAIT;
1119 2 zero_gravi
            end if;
1120
 
1121 53 zero_gravi
          when opcode_fop_c => -- floating-point operations
1122 52 zero_gravi
          -- ------------------------------------------------------------
1123 55 zero_gravi
            if (CPU_EXTENSION_RISCV_Zfinx = true) and (decode_aux.is_float_op = '1') then
1124 52 zero_gravi
              ctrl_nxt(ctrl_cp_id_msb_c downto ctrl_cp_id_lsb_c) <= cp_sel_fpu_c; -- use FPU CP
1125
              execute_engine.is_cp_op_nxt                        <= '1'; -- this is a CP operation
1126
              ctrl_nxt(ctrl_alu_func1_c downto ctrl_alu_func0_c) <= alu_func_cmd_copro_c;
1127
              execute_engine.state_nxt                           <= ALU_WAIT;
1128 53 zero_gravi
            else
1129
              execute_engine.state_nxt <= SYS_WAIT;
1130 52 zero_gravi
            end if;
1131
 
1132 2 zero_gravi
          when others => -- undefined
1133
          -- ------------------------------------------------------------
1134 39 zero_gravi
            execute_engine.state_nxt <= SYS_WAIT;
1135 2 zero_gravi
 
1136
        end case;
1137
 
1138 39 zero_gravi
 
1139
      when SYS_ENV => -- system environment operation - execution
1140 2 zero_gravi
      -- ------------------------------------------------------------
1141 49 zero_gravi
        execute_engine.state_nxt <= SYS_WAIT;
1142
        case decode_aux.sys_env_cmd is -- use a simplified input here (with permanent zeros)
1143
          when funct12_ecall_c  => trap_ctrl.env_call       <= '1'; -- ECALL
1144
          when funct12_ebreak_c => trap_ctrl.break_point    <= '1'; -- EBREAK
1145
          when funct12_mret_c   => execute_engine.state_nxt <= TRAP_EXIT; -- MRET
1146 60 zero_gravi
          when funct12_wfi_c => -- WFI
1147 59 zero_gravi
            if (CPU_EXTENSION_RISCV_DEBUG = true) and (debug_ctrl.running = '1') then
1148
              NULL; -- just a NOP when in debug mode
1149
            else
1150
              execute_engine.sleep_nxt <= '1'; -- WFI (normal)
1151
            end if;
1152
          when funct12_dret_c => -- DRET
1153
            if (CPU_EXTENSION_RISCV_DEBUG = true) then
1154
              execute_engine.state_nxt <= TRAP_EXIT;
1155
              debug_ctrl.dret <= '1';
1156
            else
1157
              NULL;
1158
            end if;
1159 60 zero_gravi
          when others => NULL; -- undefined
1160 39 zero_gravi
        end case;
1161
 
1162
 
1163
      when CSR_ACCESS => -- read & write status and control register (CSR)
1164
      -- ------------------------------------------------------------
1165 27 zero_gravi
        -- CSR write access --
1166 6 zero_gravi
        case execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) is
1167 25 zero_gravi
          when funct3_csrrw_c | funct3_csrrwi_c => -- CSRRW(I)
1168 15 zero_gravi
            csr.we_nxt <= csr_acc_valid; -- always write CSR if valid access
1169 27 zero_gravi
          when funct3_csrrs_c | funct3_csrrsi_c | funct3_csrrc_c | funct3_csrrci_c => -- CSRRS(I) / CSRRC(I)
1170 44 zero_gravi
            csr.we_nxt <= (not decode_aux.rs1_is_r0) and csr_acc_valid; -- write CSR if rs1/imm is not zero and if valid access
1171 29 zero_gravi
          when others => -- invalid
1172 27 zero_gravi
            csr.we_nxt <= '0';
1173 2 zero_gravi
        end case;
1174 27 zero_gravi
        -- register file write back --
1175 49 zero_gravi
        ctrl_nxt(ctrl_alu_func1_c downto ctrl_alu_func0_c) <= alu_func_cmd_copro_c;
1176
        ctrl_nxt(ctrl_rf_in_mux_c)                         <= '0'; -- RF input = ALU result
1177
        ctrl_nxt(ctrl_rf_wb_en_c)                          <= '1'; -- valid RF write-back
1178
        execute_engine.state_nxt                           <= DISPATCH;
1179 2 zero_gravi
 
1180 39 zero_gravi
 
1181 19 zero_gravi
      when ALU_WAIT => -- wait for multi-cycle ALU operation (shifter or CP) to finish
1182 2 zero_gravi
      -- ------------------------------------------------------------
1183 49 zero_gravi
        ctrl_nxt(ctrl_rf_in_mux_c) <= '0'; -- RF input = ALU result
1184 44 zero_gravi
        -- cp access or alu.shift? --
1185 29 zero_gravi
        if (execute_engine.is_cp_op = '1') then
1186 39 zero_gravi
          ctrl_nxt(ctrl_alu_func1_c downto ctrl_alu_func0_c) <= alu_func_cmd_copro_c;
1187 29 zero_gravi
        else
1188 39 zero_gravi
          ctrl_nxt(ctrl_alu_func1_c downto ctrl_alu_func0_c) <= alu_func_cmd_shift_c;
1189 19 zero_gravi
        end if;
1190
        -- wait for result --
1191 6 zero_gravi
        if (alu_wait_i = '0') then
1192 56 zero_gravi
          ctrl_nxt(ctrl_rf_wb_en_c) <= '1'; -- valid RF write-back
1193
          execute_engine.state_nxt  <= DISPATCH;
1194 2 zero_gravi
        end if;
1195
 
1196 39 zero_gravi
 
1197 6 zero_gravi
      when BRANCH => -- update PC for taken branches and jumps
1198
      -- ------------------------------------------------------------
1199 39 zero_gravi
        -- get and store return address (only relevant for jump-and-link operations) --
1200
        ctrl_nxt(ctrl_alu_opb_mux_c)                         <= '1'; -- use IMM as ALU.OPB (next_pc from immediate generator = return address)
1201
        ctrl_nxt(ctrl_alu_logic1_c downto ctrl_alu_logic0_c) <= alu_logic_cmd_movb_c; -- MOVB
1202
        ctrl_nxt(ctrl_alu_func1_c downto ctrl_alu_func0_c)   <= alu_func_cmd_logic_c; -- actual ALU operation = MOVB
1203 49 zero_gravi
        ctrl_nxt(ctrl_rf_in_mux_c)                           <= '0'; -- RF input = ALU result
1204 40 zero_gravi
        ctrl_nxt(ctrl_rf_wb_en_c)                            <= execute_engine.i_reg(instr_opcode_lsb_c+2); -- valid RF write-back? (is jump-and-link?)
1205 39 zero_gravi
        -- destination address --
1206 49 zero_gravi
        execute_engine.pc_mux_sel <= '1'; -- alu.add = branch/jump destination
1207 40 zero_gravi
        if (execute_engine.i_reg(instr_opcode_lsb_c+2) = '1') or (execute_engine.branch_taken = '1') then -- JAL/JALR or taken branch
1208 49 zero_gravi
          execute_engine.pc_we        <= '1'; -- update PC
1209
          execute_engine.branched_nxt <= '1'; -- this is an actual branch
1210
          fetch_engine.reset          <= '1'; -- trigger new instruction fetch from modified PC
1211
          execute_engine.state_nxt    <= SYS_WAIT;
1212 11 zero_gravi
        else
1213
          execute_engine.state_nxt <= DISPATCH;
1214 6 zero_gravi
        end if;
1215
 
1216 39 zero_gravi
 
1217
      when FENCE_OP => -- fence operations - execution
1218
      -- ------------------------------------------------------------
1219 47 zero_gravi
        execute_engine.state_nxt <= SYS_WAIT;
1220 39 zero_gravi
        -- FENCE.I --
1221 47 zero_gravi
        if (CPU_EXTENSION_RISCV_Zifencei = true) then
1222 49 zero_gravi
          execute_engine.pc_mux_sel <= '0'; -- linear next PC = start *new* instruction fetch with next instruction (only relevant for fence.i)
1223 47 zero_gravi
          if (execute_engine.i_reg(instr_funct3_lsb_c) = funct3_fencei_c(0)) then
1224 49 zero_gravi
            execute_engine.pc_we        <= '1'; -- update PC
1225
            execute_engine.branched_nxt <= '1'; -- this is an actual branch
1226
            fetch_engine.reset          <= '1'; -- trigger new instruction fetch from modified PC
1227 47 zero_gravi
            ctrl_nxt(ctrl_bus_fencei_c) <= '1';
1228
          end if;
1229 39 zero_gravi
        end if;
1230
        -- FENCE --
1231
        if (execute_engine.i_reg(instr_funct3_lsb_c) = funct3_fence_c(0)) then
1232
          ctrl_nxt(ctrl_bus_fence_c) <= '1';
1233
        end if;
1234
 
1235
 
1236 12 zero_gravi
      when LOADSTORE_0 => -- trigger memory request
1237 6 zero_gravi
      -- ------------------------------------------------------------
1238 57 zero_gravi
        ctrl_nxt(ctrl_bus_lock_c) <= decode_aux.is_atomic_lr; -- atomic.LR: set lock
1239 44 zero_gravi
        if (execute_engine.i_reg(instr_opcode_msb_c-1) = '0') or (decode_aux.is_atomic_lr = '1') then -- normal load or atomic load-reservate
1240 57 zero_gravi
          ctrl_nxt(ctrl_bus_rd_c)  <= '1'; -- read request
1241 39 zero_gravi
        else -- store
1242 57 zero_gravi
          if (CPU_EXTENSION_RISCV_A = true) and (decode_aux.is_atomic_sc = '1') then -- evaluate lock state
1243
            if (excl_state_i = '1') then -- lock is still ok - perform write access
1244
              ctrl_nxt(ctrl_bus_wr_c) <= '1'; -- write request
1245
            end if;
1246
          else
1247
            ctrl_nxt(ctrl_bus_wr_c) <= '1'; -- (normal) write request
1248
          end if;
1249 12 zero_gravi
        end if;
1250
        execute_engine.state_nxt <= LOADSTORE_1;
1251 6 zero_gravi
 
1252 39 zero_gravi
 
1253 12 zero_gravi
      when LOADSTORE_1 => -- memory latency
1254 6 zero_gravi
      -- ------------------------------------------------------------
1255 39 zero_gravi
        ctrl_nxt(ctrl_bus_mi_we_c) <= '1'; -- write input data to MDI (only relevant for LOAD)
1256 57 zero_gravi
        execute_engine.state_nxt   <= LOADSTORE_2;
1257 6 zero_gravi
 
1258 39 zero_gravi
 
1259 12 zero_gravi
      when LOADSTORE_2 => -- wait for bus transaction to finish
1260 6 zero_gravi
      -- ------------------------------------------------------------
1261 57 zero_gravi
        ctrl_nxt(ctrl_bus_mi_we_c) <= '1'; -- keep writing input data to MDI (only relevant for load (and SC.W) operations)
1262 53 zero_gravi
        ctrl_nxt(ctrl_rf_in_mux_c) <= '1'; -- RF input = memory input (only relevant for LOADs)
1263 39 zero_gravi
        -- wait for memory response --
1264 37 zero_gravi
        if ((ma_load_i or be_load_i or ma_store_i or be_store_i) = '1') then -- abort if exception
1265 53 zero_gravi
          execute_engine.state_nxt <= DISPATCH;
1266 26 zero_gravi
        elsif (bus_d_wait_i = '0') then -- wait for bus to finish transaction
1267 57 zero_gravi
          -- remove atomic lock if this is NOT the LR.W instruction used to SET the lock --
1268
          if (CPU_EXTENSION_RISCV_A = true) and (decode_aux.is_atomic_lr = '0') then -- execute and evaluate atomic store-conditional
1269
            ctrl_nxt(ctrl_bus_de_lock_c) <= '1';
1270
          end if;
1271
          -- data write-back --
1272
          if (execute_engine.i_reg(instr_opcode_msb_c-1) = '0') or -- normal load
1273
             (decode_aux.is_atomic_lr = '1') or -- atomic load-reservate
1274
             (decode_aux.is_atomic_sc = '1') then -- atomic store-conditional
1275 53 zero_gravi
            ctrl_nxt(ctrl_rf_wb_en_c) <= '1';
1276 6 zero_gravi
          end if;
1277
          execute_engine.state_nxt <= DISPATCH;
1278
        end if;
1279
 
1280 39 zero_gravi
 
1281 2 zero_gravi
      when others => -- undefined
1282
      -- ------------------------------------------------------------
1283 7 zero_gravi
        execute_engine.state_nxt <= SYS_WAIT;
1284 2 zero_gravi
 
1285
    end case;
1286 6 zero_gravi
  end process execute_engine_fsm_comb;
1287 2 zero_gravi
 
1288
 
1289 15 zero_gravi
-- ****************************************************************************************************************************
1290
-- Invalid Instruction / CSR access check
1291
-- ****************************************************************************************************************************
1292
 
1293 49 zero_gravi
  -- CSR Access Check -----------------------------------------------------------------------
1294 15 zero_gravi
  -- -------------------------------------------------------------------------------------------
1295 59 zero_gravi
  csr_access_check: process(execute_engine.i_reg, csr, debug_ctrl)
1296 42 zero_gravi
    variable csr_wacc_v           : std_ulogic; -- to check access to read-only CSRs
1297 56 zero_gravi
    variable csr_mcounteren_hpm_v : std_ulogic_vector(31 downto 0); -- max 29 HPM counters, plus 3 LSB-aligned dummy bits
1298 15 zero_gravi
  begin
1299 58 zero_gravi
    -- is this CSR instruction really going to write to a CSR? --
1300 30 zero_gravi
    if (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrw_c) or
1301
       (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrwi_c) then
1302
      csr_wacc_v := '1'; -- always write CSR
1303 58 zero_gravi
    else -- clear/set
1304 60 zero_gravi
      csr_wacc_v := or_reduce_f(execute_engine.i_reg(instr_rs1_msb_c downto instr_rs1_lsb_c)); -- write allowed if rs1/uimm5 != 0
1305 30 zero_gravi
    end if;
1306
 
1307 42 zero_gravi
    -- low privilege level access to hpm counters? --
1308
    csr_mcounteren_hpm_v := (others => '0');
1309 56 zero_gravi
    if (CPU_EXTENSION_RISCV_U = true) and (HPM_NUM_CNTS /= 0) then
1310
      csr_mcounteren_hpm_v(3+(HPM_NUM_CNTS-1) downto 3+0) := csr.mcounteren_hpm(HPM_NUM_CNTS-1 downto 0);
1311 52 zero_gravi
    else -- 'mcounteren' CSR is hardwired to zero if user mode is not implemented
1312
      csr_mcounteren_hpm_v := (others => '0');
1313 51 zero_gravi
    end if;
1314 42 zero_gravi
 
1315 15 zero_gravi
    -- check CSR access --
1316 58 zero_gravi
    csr_acc_valid <= '0'; -- default = invalid access
1317 41 zero_gravi
    case csr.addr is
1318 56 zero_gravi
 
1319 58 zero_gravi
      -- floating-point CSRs --
1320 56 zero_gravi
      when csr_fflags_c | csr_frm_c | csr_fcsr_c =>
1321 58 zero_gravi
        if (CPU_EXTENSION_RISCV_Zfinx = true) then
1322
          csr_acc_valid <= '1'; -- full access for everyone if Zfinx extension is implemented
1323
        else
1324
          NULL;
1325
        end if;
1326 56 zero_gravi
 
1327 60 zero_gravi
      -- machine trap setup & handling --
1328
      when csr_mstatus_c | csr_misa_c | csr_mie_c | csr_mtvec_c | csr_mcounteren_c | csr_mscratch_c | csr_mepc_c | csr_mcause_c =>
1329 56 zero_gravi
        csr_acc_valid <= csr.priv_m_mode; -- M-mode only, NOTE: MISA is read-only in the NEORV32 but we do not cause an exception here for compatibility
1330 60 zero_gravi
      when csr_mip_c | csr_mtval_c => -- NOTE: MIP and MTVAL are read-only in the NEORV32!
1331 58 zero_gravi
        csr_acc_valid <= (not csr_wacc_v) and csr.priv_m_mode; -- M-mode only, read-only
1332 56 zero_gravi
 
1333 60 zero_gravi
      -- physical memory protection - address & configuration --
1334 42 zero_gravi
      when csr_pmpaddr0_c  | csr_pmpaddr1_c  | csr_pmpaddr2_c  | csr_pmpaddr3_c  | csr_pmpaddr4_c  | csr_pmpaddr5_c  | csr_pmpaddr6_c  | csr_pmpaddr7_c  |
1335
           csr_pmpaddr8_c  | csr_pmpaddr9_c  | csr_pmpaddr10_c | csr_pmpaddr11_c | csr_pmpaddr12_c | csr_pmpaddr13_c | csr_pmpaddr14_c | csr_pmpaddr15_c |
1336
           csr_pmpaddr16_c | csr_pmpaddr17_c | csr_pmpaddr18_c | csr_pmpaddr19_c | csr_pmpaddr20_c | csr_pmpaddr21_c | csr_pmpaddr22_c | csr_pmpaddr23_c |
1337
           csr_pmpaddr24_c | csr_pmpaddr25_c | csr_pmpaddr26_c | csr_pmpaddr27_c | csr_pmpaddr28_c | csr_pmpaddr29_c | csr_pmpaddr30_c | csr_pmpaddr31_c |
1338
           csr_pmpaddr32_c | csr_pmpaddr33_c | csr_pmpaddr34_c | csr_pmpaddr35_c | csr_pmpaddr36_c | csr_pmpaddr37_c | csr_pmpaddr38_c | csr_pmpaddr39_c |
1339
           csr_pmpaddr40_c | csr_pmpaddr41_c | csr_pmpaddr42_c | csr_pmpaddr43_c | csr_pmpaddr44_c | csr_pmpaddr45_c | csr_pmpaddr46_c | csr_pmpaddr47_c |
1340
           csr_pmpaddr48_c | csr_pmpaddr49_c | csr_pmpaddr50_c | csr_pmpaddr51_c | csr_pmpaddr52_c | csr_pmpaddr53_c | csr_pmpaddr54_c | csr_pmpaddr55_c |
1341 60 zero_gravi
           csr_pmpaddr56_c | csr_pmpaddr57_c | csr_pmpaddr58_c | csr_pmpaddr59_c | csr_pmpaddr60_c | csr_pmpaddr61_c | csr_pmpaddr62_c | csr_pmpaddr63_c |
1342
           csr_pmpcfg0_c | csr_pmpcfg1_c | csr_pmpcfg2_c  | csr_pmpcfg3_c  | csr_pmpcfg4_c  | csr_pmpcfg5_c  | csr_pmpcfg6_c  | csr_pmpcfg7_c |
1343
           csr_pmpcfg8_c | csr_pmpcfg9_c | csr_pmpcfg10_c | csr_pmpcfg11_c | csr_pmpcfg12_c | csr_pmpcfg13_c | csr_pmpcfg14_c | csr_pmpcfg15_c =>
1344 58 zero_gravi
        if (PMP_NUM_REGIONS > 0) then
1345
          csr_acc_valid <= csr.priv_m_mode; -- M-mode only
1346
        else
1347
          NULL;
1348
        end if;
1349 56 zero_gravi
 
1350
      -- machine counters/timers --
1351 58 zero_gravi
      when csr_mcycle_c | csr_minstret_c =>
1352
        csr_acc_valid <= csr.priv_m_mode and bool_to_ulogic_f(boolean(cpu_cnt_lo_width_c > 0)); -- M-mode only, access valid if really implemented
1353
      when csr_mcycleh_c | csr_minstreth_c =>
1354
        csr_acc_valid <= csr.priv_m_mode and bool_to_ulogic_f(boolean(cpu_cnt_hi_width_c > 0)); -- M-mode only, access valid if really implemented
1355 56 zero_gravi
 
1356
      when csr_mhpmcounter3_c   | csr_mhpmcounter4_c   | csr_mhpmcounter5_c   | csr_mhpmcounter6_c   | csr_mhpmcounter7_c   | csr_mhpmcounter8_c   | -- LOW
1357
           csr_mhpmcounter9_c   | csr_mhpmcounter10_c  | csr_mhpmcounter11_c  | csr_mhpmcounter12_c  | csr_mhpmcounter13_c  | csr_mhpmcounter14_c  |
1358
           csr_mhpmcounter15_c  | csr_mhpmcounter16_c  | csr_mhpmcounter17_c  | csr_mhpmcounter18_c  | csr_mhpmcounter19_c  | csr_mhpmcounter20_c  |
1359
           csr_mhpmcounter21_c  | csr_mhpmcounter22_c  | csr_mhpmcounter23_c  | csr_mhpmcounter24_c  | csr_mhpmcounter25_c  | csr_mhpmcounter26_c  |
1360
           csr_mhpmcounter27_c  | csr_mhpmcounter28_c  | csr_mhpmcounter29_c  | csr_mhpmcounter30_c  | csr_mhpmcounter31_c  |
1361
           csr_mhpmcounter3h_c  | csr_mhpmcounter4h_c  | csr_mhpmcounter5h_c  | csr_mhpmcounter6h_c  | csr_mhpmcounter7h_c  | csr_mhpmcounter8h_c  | -- HIGH
1362
           csr_mhpmcounter9h_c  | csr_mhpmcounter10h_c | csr_mhpmcounter11h_c | csr_mhpmcounter12h_c | csr_mhpmcounter13h_c | csr_mhpmcounter14h_c |
1363
           csr_mhpmcounter15h_c | csr_mhpmcounter16h_c | csr_mhpmcounter17h_c | csr_mhpmcounter18h_c | csr_mhpmcounter19h_c | csr_mhpmcounter20h_c |
1364
           csr_mhpmcounter21h_c | csr_mhpmcounter22h_c | csr_mhpmcounter23h_c | csr_mhpmcounter24h_c | csr_mhpmcounter25h_c | csr_mhpmcounter26h_c |
1365
           csr_mhpmcounter27h_c | csr_mhpmcounter28h_c | csr_mhpmcounter29h_c | csr_mhpmcounter30h_c | csr_mhpmcounter31h_c =>
1366 58 zero_gravi
        if (HPM_NUM_CNTS > 0) then
1367
          csr_acc_valid <= csr.priv_m_mode; -- M-mode only
1368
        else
1369
          NULL;
1370
        end if;
1371 56 zero_gravi
 
1372
      -- user counters/timers --
1373
      when csr_cycle_c =>
1374
        csr_acc_valid <= (not csr_wacc_v) and (csr.priv_m_mode or csr.mcounteren_cy) and bool_to_ulogic_f(boolean(cpu_cnt_lo_width_c > 0)); -- M-mode, U-mode if authorized, read-only, access if implemented
1375
      when csr_cycleh_c =>
1376
        csr_acc_valid <= (not csr_wacc_v) and (csr.priv_m_mode or csr.mcounteren_cy) and bool_to_ulogic_f(boolean(cpu_cnt_hi_width_c > 0)); -- M-mode, U-mode if authorized, read-only, access if implemented
1377
      when csr_instret_c =>
1378
        csr_acc_valid <= (not csr_wacc_v) and (csr.priv_m_mode or csr.mcounteren_ir) and bool_to_ulogic_f(boolean(cpu_cnt_lo_width_c > 0)); -- M-mode, U-mode if authorized, read-only, access if implemented
1379
      when csr_instreth_c =>
1380
        csr_acc_valid <= (not csr_wacc_v) and (csr.priv_m_mode or csr.mcounteren_ir) and bool_to_ulogic_f(boolean(cpu_cnt_hi_width_c > 0)); -- M-mode, U-mode if authorized, read-only, access if implemented
1381
 
1382
      when csr_time_c | csr_timeh_c =>
1383
        csr_acc_valid <= (not csr_wacc_v) and (csr.priv_m_mode or csr.mcounteren_tm); -- M-mode, U-mode if authorized, read-only
1384
 
1385
      when csr_hpmcounter3_c   | csr_hpmcounter4_c   | csr_hpmcounter5_c   | csr_hpmcounter6_c   | csr_hpmcounter7_c   | csr_hpmcounter8_c   | -- LOW
1386
           csr_hpmcounter9_c   | csr_hpmcounter10_c  | csr_hpmcounter11_c  | csr_hpmcounter12_c  | csr_hpmcounter13_c  | csr_hpmcounter14_c  |
1387
           csr_hpmcounter15_c  | csr_hpmcounter16_c  | csr_hpmcounter17_c  | csr_hpmcounter18_c  | csr_hpmcounter19_c  | csr_hpmcounter20_c  |
1388
           csr_hpmcounter21_c  | csr_hpmcounter22_c  | csr_hpmcounter23_c  | csr_hpmcounter24_c  | csr_hpmcounter25_c  | csr_hpmcounter26_c  |
1389
           csr_hpmcounter27_c  | csr_hpmcounter28_c  | csr_hpmcounter29_c  | csr_hpmcounter30_c  | csr_hpmcounter31_c  |
1390
           csr_hpmcounter3h_c  | csr_hpmcounter4h_c  | csr_hpmcounter5h_c  | csr_hpmcounter6h_c  | csr_hpmcounter7h_c  | csr_hpmcounter8h_c  | -- HIGH
1391
           csr_hpmcounter9h_c  | csr_hpmcounter10h_c | csr_hpmcounter11h_c | csr_hpmcounter12h_c | csr_hpmcounter13h_c | csr_hpmcounter14h_c |
1392
           csr_hpmcounter15h_c | csr_hpmcounter16h_c | csr_hpmcounter17h_c | csr_hpmcounter18h_c | csr_hpmcounter19h_c | csr_hpmcounter20h_c |
1393
           csr_hpmcounter21h_c | csr_hpmcounter22h_c | csr_hpmcounter23h_c | csr_hpmcounter24h_c | csr_hpmcounter25h_c | csr_hpmcounter26h_c |
1394
           csr_hpmcounter27h_c | csr_hpmcounter28h_c | csr_hpmcounter29h_c | csr_hpmcounter30h_c | csr_hpmcounter31h_c =>
1395 58 zero_gravi
        if (HPM_NUM_CNTS > 0) then
1396
          csr_acc_valid <= (not csr_wacc_v) and (csr.priv_m_mode or csr_mcounteren_hpm_v(to_integer(unsigned(csr.addr(4 downto 0))))); -- M-mode, U-mode if authorized, read-only
1397
        else
1398
          NULL;
1399
        end if;
1400 56 zero_gravi
 
1401
      -- machine counter setup --
1402
      when csr_mcountinhibit_c =>
1403
        csr_acc_valid <= csr.priv_m_mode; -- M-mode only
1404
 
1405 42 zero_gravi
      when csr_mhpmevent3_c  | csr_mhpmevent4_c  | csr_mhpmevent5_c  | csr_mhpmevent6_c  | csr_mhpmevent7_c  | csr_mhpmevent8_c  |
1406
           csr_mhpmevent9_c  | csr_mhpmevent10_c | csr_mhpmevent11_c | csr_mhpmevent12_c | csr_mhpmevent13_c | csr_mhpmevent14_c |
1407
           csr_mhpmevent15_c | csr_mhpmevent16_c | csr_mhpmevent17_c | csr_mhpmevent18_c | csr_mhpmevent19_c | csr_mhpmevent20_c |
1408
           csr_mhpmevent21_c | csr_mhpmevent22_c | csr_mhpmevent23_c | csr_mhpmevent24_c | csr_mhpmevent25_c | csr_mhpmevent26_c |
1409
           csr_mhpmevent27_c | csr_mhpmevent28_c | csr_mhpmevent29_c | csr_mhpmevent30_c | csr_mhpmevent31_c =>
1410 58 zero_gravi
        if (HPM_NUM_CNTS > 0) then
1411
          csr_acc_valid <= csr.priv_m_mode; -- M-mode only
1412
        else
1413
          NULL;
1414
        end if;
1415 42 zero_gravi
 
1416 58 zero_gravi
      -- machine information registers & custom (NEORV32-specific) read-only CSRs --
1417
      when csr_mvendorid_c | csr_marchid_c | csr_mimpid_c | csr_mhartid_c | csr_mzext_c =>
1418 56 zero_gravi
        csr_acc_valid <= (not csr_wacc_v) and csr.priv_m_mode; -- M-mode only, read-only
1419 58 zero_gravi
 
1420 59 zero_gravi
      -- debug mode CSRs --
1421
      when csr_dcsr_c | csr_dpc_c | csr_dscratch0_c =>
1422
        if (CPU_EXTENSION_RISCV_DEBUG = true) then
1423 60 zero_gravi
          csr_acc_valid <= debug_ctrl.running; -- access in only in debug-mode
1424 59 zero_gravi
        else
1425
          NULL;
1426
        end if;
1427
 
1428 56 zero_gravi
      -- undefined / not implemented --
1429
      when others =>
1430 58 zero_gravi
        NULL; -- invalid access
1431 15 zero_gravi
    end case;
1432 49 zero_gravi
  end process csr_access_check;
1433 15 zero_gravi
 
1434
 
1435 2 zero_gravi
  -- Illegal Instruction Check --------------------------------------------------------------
1436
  -- -------------------------------------------------------------------------------------------
1437 59 zero_gravi
  illegal_instruction_check: process(execute_engine, decode_aux, csr_acc_valid, debug_ctrl)
1438 36 zero_gravi
    variable opcode_v : std_ulogic_vector(6 downto 0);
1439 2 zero_gravi
  begin
1440 11 zero_gravi
    -- illegal instructions are checked in the EXECUTE stage
1441 36 zero_gravi
    -- the execute engine should not commit any illegal instruction
1442 6 zero_gravi
    if (execute_engine.state = EXECUTE) then
1443 2 zero_gravi
      -- defaults --
1444
      illegal_instruction <= '0';
1445
      illegal_register    <= '0';
1446
 
1447 36 zero_gravi
      -- check opcode for rv32 --
1448
      if (execute_engine.i_reg(instr_opcode_lsb_c+1 downto instr_opcode_lsb_c) = "11") then
1449
        illegal_opcode_lsbs <= '0';
1450
      else
1451
        illegal_opcode_lsbs <= '1';
1452
      end if;
1453
 
1454 2 zero_gravi
      -- check instructions --
1455 60 zero_gravi
      opcode_v := execute_engine.i_reg(instr_opcode_msb_c downto instr_opcode_lsb_c+2) & "11"; -- save some bits here, LSBs are always 11 for rv32
1456 36 zero_gravi
      case opcode_v is
1457 2 zero_gravi
 
1458 59 zero_gravi
 
1459
        when opcode_lui_c | opcode_auipc_c | opcode_jal_c => -- check sufficient LUI, UIPC, JAL (only check actual OPCODE)
1460 52 zero_gravi
        -- ------------------------------------------------------------
1461 2 zero_gravi
          illegal_instruction <= '0';
1462 23 zero_gravi
          -- illegal E-CPU register? --
1463
          if (CPU_EXTENSION_RISCV_E = true) and (execute_engine.i_reg(instr_rd_msb_c) = '1') then
1464
            illegal_register <= '1';
1465
          end if;
1466 2 zero_gravi
 
1467 44 zero_gravi
        when opcode_alu_c => -- check ALU.funct3 & ALU.funct7
1468 52 zero_gravi
        -- ------------------------------------------------------------
1469 44 zero_gravi
          if (execute_engine.i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c) = "0000001") then -- MULDIV
1470
            if (CPU_EXTENSION_RISCV_M = false) then -- not implemented
1471
              illegal_instruction <= '1';
1472
            end if;
1473
          elsif ((execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_subadd_c) or
1474
                 (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sr_c)) and -- ADD/SUB or SRA/SRL check
1475
                ((execute_engine.i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c) /= "0000000") and
1476
                 (execute_engine.i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c) /= "0100000")) then -- ADD/SUB or SRA/SRL select
1477
            illegal_instruction <= '1';
1478
          else
1479
            illegal_instruction <= '0';
1480
          end if;
1481
          -- illegal E-CPU register? --
1482
          if (CPU_EXTENSION_RISCV_E = true) and
1483
             ((execute_engine.i_reg(instr_rs2_msb_c) = '1') or (execute_engine.i_reg(instr_rs1_msb_c) = '1') or (execute_engine.i_reg(instr_rd_msb_c) = '1')) then
1484
            illegal_register <= '1';
1485
          end if;
1486
 
1487
        when opcode_alui_c => -- check ALUI.funct7
1488 52 zero_gravi
        -- ------------------------------------------------------------
1489 60 zero_gravi
          if ((execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sll_c) and
1490 6 zero_gravi
              (execute_engine.i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c) /= "0000000")) or -- shift logical left
1491
             ((execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sr_c) and
1492
              ((execute_engine.i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c) /= "0000000") and
1493
               (execute_engine.i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c) /= "0100000"))) then -- shift right
1494 2 zero_gravi
            illegal_instruction <= '1';
1495
          else
1496
            illegal_instruction <= '0';
1497
          end if;
1498 23 zero_gravi
          -- illegal E-CPU register? --
1499
          if (CPU_EXTENSION_RISCV_E = true) and ((execute_engine.i_reg(instr_rs1_msb_c) = '1') or (execute_engine.i_reg(instr_rd_msb_c) = '1')) then
1500
            illegal_register <= '1';
1501
          end if;
1502 39 zero_gravi
 
1503 44 zero_gravi
        when opcode_load_c => -- check LOAD.funct3
1504 52 zero_gravi
        -- ------------------------------------------------------------
1505 6 zero_gravi
          if (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_lb_c) or
1506
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_lh_c) or
1507
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_lw_c) or
1508
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_lbu_c) or
1509
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_lhu_c) then
1510 2 zero_gravi
            illegal_instruction <= '0';
1511
          else
1512
            illegal_instruction <= '1';
1513
          end if;
1514 23 zero_gravi
          -- illegal E-CPU register? --
1515
          if (CPU_EXTENSION_RISCV_E = true) and ((execute_engine.i_reg(instr_rs1_msb_c) = '1') or (execute_engine.i_reg(instr_rd_msb_c) = '1')) then
1516
            illegal_register <= '1';
1517
          end if;
1518 39 zero_gravi
 
1519 44 zero_gravi
        when opcode_store_c => -- check STORE.funct3
1520 52 zero_gravi
        -- ------------------------------------------------------------
1521 6 zero_gravi
          if (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sb_c) or
1522
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sh_c) or
1523
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sw_c) then
1524 2 zero_gravi
            illegal_instruction <= '0';
1525
          else
1526
            illegal_instruction <= '1';
1527
          end if;
1528 23 zero_gravi
          -- illegal E-CPU register? --
1529
          if (CPU_EXTENSION_RISCV_E = true) and ((execute_engine.i_reg(instr_rs2_msb_c) = '1') or (execute_engine.i_reg(instr_rs1_msb_c) = '1')) then
1530
            illegal_register <= '1';
1531
          end if;
1532 2 zero_gravi
 
1533 44 zero_gravi
        when opcode_branch_c => -- check BRANCH.funct3
1534 52 zero_gravi
        -- ------------------------------------------------------------
1535 6 zero_gravi
          if (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_beq_c) or
1536
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_bne_c) or
1537
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_blt_c) or
1538
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_bge_c) or
1539
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_bltu_c) or
1540
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_bgeu_c) then
1541 2 zero_gravi
            illegal_instruction <= '0';
1542
          else
1543
            illegal_instruction <= '1';
1544
          end if;
1545 23 zero_gravi
          -- illegal E-CPU register? --
1546
          if (CPU_EXTENSION_RISCV_E = true) and ((execute_engine.i_reg(instr_rs2_msb_c) = '1') or (execute_engine.i_reg(instr_rs1_msb_c) = '1')) then
1547
            illegal_register <= '1';
1548
          end if;
1549 2 zero_gravi
 
1550 44 zero_gravi
        when opcode_jalr_c => -- check JALR.funct3
1551 52 zero_gravi
        -- ------------------------------------------------------------
1552 6 zero_gravi
          if (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = "000") then
1553 2 zero_gravi
            illegal_instruction <= '0';
1554
          else
1555
            illegal_instruction <= '1';
1556
          end if;
1557 23 zero_gravi
          -- illegal E-CPU register? --
1558
          if (CPU_EXTENSION_RISCV_E = true) and ((execute_engine.i_reg(instr_rs1_msb_c) = '1') or (execute_engine.i_reg(instr_rd_msb_c) = '1')) then
1559
            illegal_register <= '1';
1560
          end if;
1561 2 zero_gravi
 
1562 52 zero_gravi
        when opcode_fence_c => -- fence instructions
1563
        -- ------------------------------------------------------------
1564 59 zero_gravi
          if (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_fencei_c) then -- FENCE.I -- NO trap if not implemented
1565 8 zero_gravi
            illegal_instruction <= '0';
1566
          elsif (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_fence_c) then -- FENCE
1567
            illegal_instruction <= '0';
1568
          else
1569
            illegal_instruction <= '1';
1570
          end if;
1571
 
1572 52 zero_gravi
        when opcode_syscsr_c => -- check system instructions
1573
        -- ------------------------------------------------------------
1574 2 zero_gravi
          -- CSR access --
1575 6 zero_gravi
          if (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrw_c) or
1576
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrs_c) or
1577
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrc_c) or
1578
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrwi_c) or
1579
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrsi_c) or
1580
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrci_c) then
1581 15 zero_gravi
            -- valid CSR access? --
1582
            if (csr_acc_valid = '1') then
1583 2 zero_gravi
              illegal_instruction <= '0';
1584
            else
1585
              illegal_instruction <= '1';
1586
            end if;
1587 23 zero_gravi
            -- illegal E-CPU register? --
1588
            if (CPU_EXTENSION_RISCV_E = true) then
1589
              if (execute_engine.i_reg(instr_funct3_msb_c) = '0') then -- reg-reg CSR
1590
                illegal_register <= execute_engine.i_reg(instr_rs1_msb_c) or execute_engine.i_reg(instr_rd_msb_c);
1591
              else -- reg-imm CSR
1592
                illegal_register <= execute_engine.i_reg(instr_rd_msb_c);
1593
              end if;
1594
            end if;
1595 2 zero_gravi
 
1596 60 zero_gravi
          -- ecall, ebreak, mret, wfi, dret --
1597 6 zero_gravi
          elsif (execute_engine.i_reg(instr_rd_msb_c  downto instr_rd_lsb_c)  = "00000") and
1598
                (execute_engine.i_reg(instr_rs1_msb_c downto instr_rs1_lsb_c) = "00000") then
1599 13 zero_gravi
            if (execute_engine.i_reg(instr_funct12_msb_c  downto instr_funct12_lsb_c) = funct12_ecall_c)  or -- ECALL
1600 11 zero_gravi
               (execute_engine.i_reg(instr_funct12_msb_c  downto instr_funct12_lsb_c) = funct12_ebreak_c) or -- EBREAK 
1601 13 zero_gravi
               (execute_engine.i_reg(instr_funct12_msb_c  downto instr_funct12_lsb_c) = funct12_mret_c)   or -- MRET
1602 60 zero_gravi
               ((execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = (funct12_dret_c)) and (CPU_EXTENSION_RISCV_DEBUG = true) and (debug_ctrl.running = '1')) or -- DRET
1603 13 zero_gravi
               (execute_engine.i_reg(instr_funct12_msb_c  downto instr_funct12_lsb_c) = funct12_wfi_c) then  -- WFI
1604 2 zero_gravi
              illegal_instruction <= '0';
1605
            else
1606
              illegal_instruction <= '1';
1607
            end if;
1608
          else
1609
            illegal_instruction <= '1';
1610
          end if;
1611
 
1612 52 zero_gravi
        when opcode_atomic_c => -- atomic instructions
1613
        -- ------------------------------------------------------------
1614 39 zero_gravi
          if (CPU_EXTENSION_RISCV_A = true) and -- atomic memory operations (A extension) enabled
1615
             ((execute_engine.i_reg(instr_funct5_msb_c downto instr_funct5_lsb_c) = funct5_a_lr_c) or -- LR
1616
              (execute_engine.i_reg(instr_funct5_msb_c downto instr_funct5_lsb_c) = funct5_a_sc_c)) then -- SC
1617
            illegal_instruction <= '0';
1618
          else
1619
            illegal_instruction <= '1';
1620
          end if;
1621
 
1622 53 zero_gravi
        when opcode_fop_c => -- floating point operations - single/dual operands
1623 52 zero_gravi
        -- ------------------------------------------------------------
1624 53 zero_gravi
          if (CPU_EXTENSION_RISCV_Zfinx = true) and -- F extension enabled
1625
             (execute_engine.i_reg(instr_funct7_lsb_c+1 downto instr_funct7_lsb_c) = float_single_c) and -- single-precision operations only
1626
             (decode_aux.is_float_op = '1') then -- is correct/supported floating-point instruction
1627 52 zero_gravi
            illegal_instruction <= '0';
1628
          else
1629
            illegal_instruction <= '1';
1630
          end if;
1631
 
1632 36 zero_gravi
        when others => -- undefined instruction -> illegal!
1633 52 zero_gravi
        -- ------------------------------------------------------------
1634 36 zero_gravi
          illegal_instruction <= '1';
1635 2 zero_gravi
 
1636
      end case;
1637
    else
1638 36 zero_gravi
      illegal_opcode_lsbs <= '0';
1639 2 zero_gravi
      illegal_instruction <= '0';
1640
      illegal_register    <= '0';
1641
    end if;
1642
  end process illegal_instruction_check;
1643
 
1644
  -- any illegal condition? --
1645 59 zero_gravi
  -- ignore illegal register condition in debug mode
1646
  trap_ctrl.instr_il <= illegal_instruction or illegal_opcode_lsbs or (illegal_register and (not debug_ctrl.running)) or illegal_compressed;
1647 2 zero_gravi
 
1648
 
1649 6 zero_gravi
-- ****************************************************************************************************************************
1650 38 zero_gravi
-- Exception and Interrupt (= Trap) Control
1651 6 zero_gravi
-- ****************************************************************************************************************************
1652 2 zero_gravi
 
1653 6 zero_gravi
  -- Trap Controller ------------------------------------------------------------------------
1654 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
1655 6 zero_gravi
  trap_controller: process(rstn_i, clk_i)
1656 40 zero_gravi
    variable mode_m_v, mode_u_v : std_ulogic;
1657 2 zero_gravi
  begin
1658
    if (rstn_i = '0') then
1659 6 zero_gravi
      trap_ctrl.exc_buf   <= (others => '0');
1660 59 zero_gravi
      trap_ctrl.exc_buf(exception_db_break_c) <= '0'; -- enter debug mode
1661 56 zero_gravi
      trap_ctrl.irq_buf   <= (others => def_rst_val_c);
1662 59 zero_gravi
      trap_ctrl.irq_buf(interrupt_nm_irq_c)   <= '0'; -- NMI
1663
      trap_ctrl.irq_buf(interrupt_db_halt_c)  <= '0'; -- enter debug mode
1664
      trap_ctrl.irq_buf(interrupt_db_step_c)  <= '0'; -- enter debug mode
1665 6 zero_gravi
      trap_ctrl.exc_ack   <= '0';
1666
      trap_ctrl.irq_ack   <= (others => '0');
1667 47 zero_gravi
      trap_ctrl.env_start <= '0';
1668 56 zero_gravi
      trap_ctrl.cause     <= (others => def_rst_val_c);
1669 2 zero_gravi
    elsif rising_edge(clk_i) then
1670
      if (CPU_EXTENSION_RISCV_Zicsr = true) then
1671 59 zero_gravi
 
1672 2 zero_gravi
        -- exception buffer: misaligned load/store/instruction address
1673 59 zero_gravi
        trap_ctrl.exc_buf(exception_lalign_c) <= (trap_ctrl.exc_buf(exception_lalign_c) or ma_load_i)          and (not trap_ctrl.exc_ack);
1674
        trap_ctrl.exc_buf(exception_salign_c) <= (trap_ctrl.exc_buf(exception_salign_c) or ma_store_i)         and (not trap_ctrl.exc_ack);
1675
        trap_ctrl.exc_buf(exception_ialign_c) <= (trap_ctrl.exc_buf(exception_ialign_c) or trap_ctrl.instr_ma) and (not trap_ctrl.exc_ack);
1676
 
1677 2 zero_gravi
        -- exception buffer: load/store/instruction bus access error
1678 59 zero_gravi
        trap_ctrl.exc_buf(exception_laccess_c) <= (trap_ctrl.exc_buf(exception_laccess_c) or be_load_i)          and (not trap_ctrl.exc_ack);
1679
        trap_ctrl.exc_buf(exception_saccess_c) <= (trap_ctrl.exc_buf(exception_saccess_c) or be_store_i)         and (not trap_ctrl.exc_ack);
1680
        trap_ctrl.exc_buf(exception_iaccess_c) <= (trap_ctrl.exc_buf(exception_iaccess_c) or trap_ctrl.instr_be) and (not trap_ctrl.exc_ack);
1681
 
1682 2 zero_gravi
        -- exception buffer: illegal instruction / env call / break point
1683 40 zero_gravi
        trap_ctrl.exc_buf(exception_m_envcall_c) <= (trap_ctrl.exc_buf(exception_m_envcall_c) or (trap_ctrl.env_call and csr.priv_m_mode)) and (not trap_ctrl.exc_ack);
1684
        trap_ctrl.exc_buf(exception_u_envcall_c) <= (trap_ctrl.exc_buf(exception_u_envcall_c) or (trap_ctrl.env_call and csr.priv_u_mode)) and (not trap_ctrl.exc_ack);
1685
        trap_ctrl.exc_buf(exception_iillegal_c)  <= (trap_ctrl.exc_buf(exception_iillegal_c)  or trap_ctrl.instr_il)                       and (not trap_ctrl.exc_ack);
1686 59 zero_gravi
        if (CPU_EXTENSION_RISCV_DEBUG = true) then
1687
          trap_ctrl.exc_buf(exception_break_c) <= (trap_ctrl.exc_buf(exception_break_c) or
1688
            (
1689
              (trap_ctrl.break_point and csr.priv_m_mode and (not csr.dcsr_ebreakm) and (not debug_ctrl.running)) or -- enable break to machine-trap-handler when in machine mode on "ebreak"
1690
              (trap_ctrl.break_point and csr.priv_u_mode and (not csr.dcsr_ebreaku) and (not debug_ctrl.running))    -- enable break to machine-trap-handler when in user mode on "ebreak"
1691
            )
1692
          ) and (not trap_ctrl.exc_ack);
1693
        else
1694
          trap_ctrl.exc_buf(exception_break_c) <= (trap_ctrl.exc_buf(exception_break_c) or trap_ctrl.break_point) and (not trap_ctrl.exc_ack);
1695
        end if;
1696
 
1697
        -- enter debug mode --
1698
        if (CPU_EXTENSION_RISCV_DEBUG = true) then
1699
          trap_ctrl.exc_buf(exception_db_break_c) <= (trap_ctrl.exc_buf(exception_db_break_c) or debug_ctrl.trig_break) and (not trap_ctrl.exc_ack);
1700
          trap_ctrl.irq_buf(interrupt_db_halt_c)  <= (trap_ctrl.irq_buf(interrupt_db_halt_c)  or debug_ctrl.trig_halt)  and (not trap_ctrl.irq_ack(interrupt_db_halt_c));
1701
          trap_ctrl.irq_buf(interrupt_db_step_c)  <= (trap_ctrl.irq_buf(interrupt_db_step_c)  or debug_ctrl.trig_step)  and (not trap_ctrl.irq_ack(interrupt_db_step_c));
1702
        else
1703
          trap_ctrl.exc_buf(exception_db_break_c) <= '0';
1704
          trap_ctrl.irq_buf(interrupt_db_halt_c)  <= '0';
1705
          trap_ctrl.irq_buf(interrupt_db_step_c)  <= '0';
1706
        end if;
1707
 
1708 58 zero_gravi
        -- interrupt buffer: non-maskable interrupt
1709
        trap_ctrl.irq_buf(interrupt_nm_irq_c)    <= (trap_ctrl.irq_buf(interrupt_nm_irq_c) or nm_irq_i) and (not trap_ctrl.irq_ack(interrupt_nm_irq_c));
1710 18 zero_gravi
        -- interrupt buffer: machine software/external/timer interrupt
1711 58 zero_gravi
        trap_ctrl.irq_buf(interrupt_msw_irq_c)   <= csr.mie_msie and (trap_ctrl.irq_buf(interrupt_msw_irq_c)   or msw_irq_i)   and (not trap_ctrl.irq_ack(interrupt_msw_irq_c));
1712
        trap_ctrl.irq_buf(interrupt_mext_irq_c)  <= csr.mie_meie and (trap_ctrl.irq_buf(interrupt_mext_irq_c)  or mext_irq_i)  and (not trap_ctrl.irq_ack(interrupt_mext_irq_c));
1713
        trap_ctrl.irq_buf(interrupt_mtime_irq_c) <= csr.mie_mtie and (trap_ctrl.irq_buf(interrupt_mtime_irq_c) or mtime_irq_i) and (not trap_ctrl.irq_ack(interrupt_mtime_irq_c));
1714 48 zero_gravi
        -- interrupt buffer: NEORV32-specific fast interrupts
1715
        for i in 0 to 15 loop
1716 58 zero_gravi
          trap_ctrl.irq_buf(interrupt_firq_0_c+i) <= csr.mie_firqe(i) and (trap_ctrl.irq_buf(interrupt_firq_0_c+i) or firq_i(i)) and (not trap_ctrl.irq_ack(interrupt_firq_0_c+i));
1717 48 zero_gravi
        end loop;
1718 59 zero_gravi
 
1719 6 zero_gravi
        -- trap control --
1720
        if (trap_ctrl.env_start = '0') then -- no started trap handler
1721 49 zero_gravi
          if (trap_ctrl.exc_fire = '1') or ((trap_ctrl.irq_fire = '1') and -- trap triggered!
1722
             ((execute_engine.state = EXECUTE) or (execute_engine.state = TRAP_ENTER))) then -- fire IRQs in EXECUTE or TRAP state only to continue execution even on permanent IRQ
1723 13 zero_gravi
            trap_ctrl.cause     <= trap_ctrl.cause_nxt;   -- capture source ID for program (for mcause csr)
1724 58 zero_gravi
            trap_ctrl.exc_ack   <= '1';                   -- clear exception
1725 42 zero_gravi
            trap_ctrl.irq_ack   <= trap_ctrl.irq_ack_nxt; -- clear interrupt with interrupt ACK mask
1726 13 zero_gravi
            trap_ctrl.env_start <= '1';                   -- now execute engine can start trap handler
1727 2 zero_gravi
          end if;
1728 6 zero_gravi
        else -- trap waiting to get started
1729
          if (trap_ctrl.env_start_ack = '1') then -- start of trap handler acknowledged by execution engine
1730
            trap_ctrl.exc_ack   <= '0';
1731
            trap_ctrl.irq_ack   <= (others => '0');
1732
            trap_ctrl.env_start <= '0';
1733 2 zero_gravi
          end if;
1734
        end if;
1735
      end if;
1736
    end if;
1737 6 zero_gravi
  end process trap_controller;
1738 2 zero_gravi
 
1739
  -- any exception/interrupt? --
1740 60 zero_gravi
  trap_ctrl.exc_fire <= or_reduce_f(trap_ctrl.exc_buf); -- exceptions/faults CANNOT be masked
1741
  trap_ctrl.irq_fire <= (or_reduce_f(trap_ctrl.irq_buf) and csr.mstatus_mie and trap_ctrl.db_irq_en) or trap_ctrl.db_irq_fire; -- interrupts CAN be masked
1742 2 zero_gravi
 
1743 59 zero_gravi
  -- debug mode (entry) interrupts --
1744
  trap_ctrl.db_irq_en <= '1' when (CPU_EXTENSION_RISCV_DEBUG = false) else
1745 60 zero_gravi
                         '0' when (debug_ctrl.running = '1') or (csr.dcsr_step = '1') else '1'; -- no interrupts when IN debug mode or IN single-step mode
1746 59 zero_gravi
  trap_ctrl.db_irq_fire <= (trap_ctrl.irq_buf(interrupt_db_step_c) or trap_ctrl.irq_buf(interrupt_db_halt_c)) when (CPU_EXTENSION_RISCV_DEBUG = true) else '0'; -- "NMI" for debug mode entry
1747
 
1748 47 zero_gravi
  -- acknowledge mask output --
1749 48 zero_gravi
  firq_ack_o <= trap_ctrl.irq_ack(interrupt_firq_15_c downto interrupt_firq_0_c);
1750 40 zero_gravi
 
1751 47 zero_gravi
 
1752 42 zero_gravi
  -- Trap Priority Encoder ------------------------------------------------------------------
1753 6 zero_gravi
  -- -------------------------------------------------------------------------------------------
1754
  trap_priority: process(trap_ctrl)
1755 2 zero_gravi
  begin
1756
    -- defaults --
1757 59 zero_gravi
    trap_ctrl.cause_nxt   <= (others => '0');
1758 6 zero_gravi
    trap_ctrl.irq_ack_nxt <= (others => '0');
1759 2 zero_gravi
 
1760 58 zero_gravi
    -- ----------------------------------------------------------------------------------------
1761 59 zero_gravi
    -- enter debug mode requests; basically, these are standard interrupt that have some
1762
    -- special handling - they have the highest priority in order to go to debug when requested
1763
    -- even if other traps are pending right now; the <trap_ctrl.cause_nxt> value will be
1764
    -- written to csr.dcsr_cause instead of mcause
1765
    -- ----------------------------------------------------------------------------------------
1766
 
1767
    -- break instruction --
1768
    if (CPU_EXTENSION_RISCV_DEBUG = true) and (trap_ctrl.exc_buf(exception_db_break_c) = '1') then
1769
      trap_ctrl.cause_nxt <= trap_db_break_c;
1770
 
1771
    -- external halt request --
1772
    elsif (CPU_EXTENSION_RISCV_DEBUG = true) and (trap_ctrl.irq_buf(interrupt_db_halt_c) = '1') then
1773
      trap_ctrl.cause_nxt <= trap_db_halt_c;
1774
      trap_ctrl.irq_ack_nxt(interrupt_db_halt_c) <= '1';
1775
 
1776
 
1777
    -- ----------------------------------------------------------------------------------------
1778 38 zero_gravi
    -- the following traps are caused by *asynchronous* exceptions (= interrupts)
1779 12 zero_gravi
    -- here we do need a specific acknowledge mask since several sources can trigger at once
1780 58 zero_gravi
    -- ----------------------------------------------------------------------------------------
1781 9 zero_gravi
 
1782 58 zero_gravi
    -- interrupt: 1.0 non-maskable interrupt --
1783 59 zero_gravi
    elsif (trap_ctrl.irq_buf(interrupt_nm_irq_c) = '1') then
1784 58 zero_gravi
      trap_ctrl.cause_nxt <= trap_nmi_c;
1785
      trap_ctrl.irq_ack_nxt(interrupt_nm_irq_c) <= '1';
1786
 
1787
 
1788 2 zero_gravi
    -- interrupt: 1.11 machine external interrupt --
1789 58 zero_gravi
    elsif (trap_ctrl.irq_buf(interrupt_mext_irq_c) = '1') then
1790 12 zero_gravi
      trap_ctrl.cause_nxt <= trap_mei_c;
1791 6 zero_gravi
      trap_ctrl.irq_ack_nxt(interrupt_mext_irq_c) <= '1';
1792 2 zero_gravi
 
1793 40 zero_gravi
    -- interrupt: 1.3 machine SW interrupt --
1794
    elsif (trap_ctrl.irq_buf(interrupt_msw_irq_c) = '1') then
1795
      trap_ctrl.cause_nxt <= trap_msi_c;
1796
      trap_ctrl.irq_ack_nxt(interrupt_msw_irq_c) <= '1';
1797
 
1798 2 zero_gravi
    -- interrupt: 1.7 machine timer interrupt --
1799 6 zero_gravi
    elsif (trap_ctrl.irq_buf(interrupt_mtime_irq_c) = '1') then
1800 12 zero_gravi
      trap_ctrl.cause_nxt <= trap_mti_c;
1801 6 zero_gravi
      trap_ctrl.irq_ack_nxt(interrupt_mtime_irq_c) <= '1';
1802 2 zero_gravi
 
1803
 
1804 14 zero_gravi
    -- interrupt: 1.16 fast interrupt channel 0 --
1805
    elsif (trap_ctrl.irq_buf(interrupt_firq_0_c) = '1') then
1806
      trap_ctrl.cause_nxt <= trap_firq0_c;
1807
      trap_ctrl.irq_ack_nxt(interrupt_firq_0_c) <= '1';
1808
 
1809
    -- interrupt: 1.17 fast interrupt channel 1 --
1810
    elsif (trap_ctrl.irq_buf(interrupt_firq_1_c) = '1') then
1811
      trap_ctrl.cause_nxt <= trap_firq1_c;
1812
      trap_ctrl.irq_ack_nxt(interrupt_firq_1_c) <= '1';
1813
 
1814
    -- interrupt: 1.18 fast interrupt channel 2 --
1815
    elsif (trap_ctrl.irq_buf(interrupt_firq_2_c) = '1') then
1816
      trap_ctrl.cause_nxt <= trap_firq2_c;
1817
      trap_ctrl.irq_ack_nxt(interrupt_firq_2_c) <= '1';
1818
 
1819
    -- interrupt: 1.19 fast interrupt channel 3 --
1820
    elsif (trap_ctrl.irq_buf(interrupt_firq_3_c) = '1') then
1821
      trap_ctrl.cause_nxt <= trap_firq3_c;
1822
      trap_ctrl.irq_ack_nxt(interrupt_firq_3_c) <= '1';
1823
 
1824 47 zero_gravi
    -- interrupt: 1.20 fast interrupt channel 4 --
1825
    elsif (trap_ctrl.irq_buf(interrupt_firq_4_c) = '1') then
1826
      trap_ctrl.cause_nxt <= trap_firq4_c;
1827
      trap_ctrl.irq_ack_nxt(interrupt_firq_4_c) <= '1';
1828 14 zero_gravi
 
1829 47 zero_gravi
    -- interrupt: 1.21 fast interrupt channel 5 --
1830
    elsif (trap_ctrl.irq_buf(interrupt_firq_5_c) = '1') then
1831
      trap_ctrl.cause_nxt <= trap_firq5_c;
1832
      trap_ctrl.irq_ack_nxt(interrupt_firq_5_c) <= '1';
1833
 
1834
    -- interrupt: 1.22 fast interrupt channel 6 --
1835
    elsif (trap_ctrl.irq_buf(interrupt_firq_6_c) = '1') then
1836
      trap_ctrl.cause_nxt <= trap_firq6_c;
1837
      trap_ctrl.irq_ack_nxt(interrupt_firq_6_c) <= '1';
1838
 
1839
    -- interrupt: 1.23 fast interrupt channel 7 --
1840
    elsif (trap_ctrl.irq_buf(interrupt_firq_7_c) = '1') then
1841
      trap_ctrl.cause_nxt <= trap_firq7_c;
1842
      trap_ctrl.irq_ack_nxt(interrupt_firq_7_c) <= '1';
1843
 
1844 48 zero_gravi
    -- interrupt: 1.24 fast interrupt channel 8 --
1845
    elsif (trap_ctrl.irq_buf(interrupt_firq_8_c) = '1') then
1846
      trap_ctrl.cause_nxt <= trap_firq8_c;
1847
      trap_ctrl.irq_ack_nxt(interrupt_firq_8_c) <= '1';
1848 47 zero_gravi
 
1849 48 zero_gravi
    -- interrupt: 1.25 fast interrupt channel 9 --
1850
    elsif (trap_ctrl.irq_buf(interrupt_firq_9_c) = '1') then
1851
      trap_ctrl.cause_nxt <= trap_firq9_c;
1852
      trap_ctrl.irq_ack_nxt(interrupt_firq_9_c) <= '1';
1853
 
1854
    -- interrupt: 1.26 fast interrupt channel 10 --
1855
    elsif (trap_ctrl.irq_buf(interrupt_firq_10_c) = '1') then
1856
      trap_ctrl.cause_nxt <= trap_firq10_c;
1857
      trap_ctrl.irq_ack_nxt(interrupt_firq_10_c) <= '1';
1858
 
1859
    -- interrupt: 1.27 fast interrupt channel 11 --
1860
    elsif (trap_ctrl.irq_buf(interrupt_firq_11_c) = '1') then
1861
      trap_ctrl.cause_nxt <= trap_firq11_c;
1862
      trap_ctrl.irq_ack_nxt(interrupt_firq_11_c) <= '1';
1863
 
1864
    -- interrupt: 1.28 fast interrupt channel 12 --
1865
    elsif (trap_ctrl.irq_buf(interrupt_firq_12_c) = '1') then
1866
      trap_ctrl.cause_nxt <= trap_firq12_c;
1867
      trap_ctrl.irq_ack_nxt(interrupt_firq_12_c) <= '1';
1868
 
1869
    -- interrupt: 1.29 fast interrupt channel 13 --
1870
    elsif (trap_ctrl.irq_buf(interrupt_firq_13_c) = '1') then
1871
      trap_ctrl.cause_nxt <= trap_firq13_c;
1872
      trap_ctrl.irq_ack_nxt(interrupt_firq_13_c) <= '1';
1873
 
1874
    -- interrupt: 1.30 fast interrupt channel 14 --
1875
    elsif (trap_ctrl.irq_buf(interrupt_firq_14_c) = '1') then
1876
      trap_ctrl.cause_nxt <= trap_firq14_c;
1877
      trap_ctrl.irq_ack_nxt(interrupt_firq_14_c) <= '1';
1878
 
1879
    -- interrupt: 1.31 fast interrupt channel 15 --
1880
    elsif (trap_ctrl.irq_buf(interrupt_firq_15_c) = '1') then
1881
      trap_ctrl.cause_nxt <= trap_firq15_c;
1882
      trap_ctrl.irq_ack_nxt(interrupt_firq_15_c) <= '1';
1883
 
1884
 
1885 58 zero_gravi
    -- ----------------------------------------------------------------------------------------
1886 42 zero_gravi
    -- the following traps are caused by *synchronous* exceptions (= 'classic' exceptions)
1887 12 zero_gravi
    -- here we do not need a specific acknowledge mask since only one exception (the one
1888 38 zero_gravi
    -- with highest priority) is evaluated at once
1889 58 zero_gravi
    -- ----------------------------------------------------------------------------------------
1890 4 zero_gravi
 
1891 38 zero_gravi
    -- exception: 0.1 instruction access fault --
1892 6 zero_gravi
    elsif (trap_ctrl.exc_buf(exception_iaccess_c) = '1') then
1893 12 zero_gravi
      trap_ctrl.cause_nxt <= trap_iba_c;
1894 2 zero_gravi
 
1895 38 zero_gravi
    -- exception: 0.2 illegal instruction --
1896 6 zero_gravi
    elsif (trap_ctrl.exc_buf(exception_iillegal_c) = '1') then
1897 12 zero_gravi
      trap_ctrl.cause_nxt <= trap_iil_c;
1898 2 zero_gravi
 
1899 38 zero_gravi
    -- exception: 0.0 instruction address misaligned --
1900 12 zero_gravi
    elsif (trap_ctrl.exc_buf(exception_ialign_c) = '1') then
1901
      trap_ctrl.cause_nxt <= trap_ima_c;
1902 2 zero_gravi
 
1903 12 zero_gravi
 
1904 38 zero_gravi
    -- exception: 0.11 environment call from M-mode --
1905 6 zero_gravi
    elsif (trap_ctrl.exc_buf(exception_m_envcall_c) = '1') then
1906 14 zero_gravi
      trap_ctrl.cause_nxt <= trap_menv_c;
1907 2 zero_gravi
 
1908 40 zero_gravi
    -- exception: 0.8 environment call from U-mode --
1909
    elsif (trap_ctrl.exc_buf(exception_u_envcall_c) = '1') then
1910
      trap_ctrl.cause_nxt <= trap_uenv_c;
1911
 
1912 38 zero_gravi
    -- exception: 0.3 breakpoint --
1913 6 zero_gravi
    elsif (trap_ctrl.exc_buf(exception_break_c) = '1') then
1914 12 zero_gravi
      trap_ctrl.cause_nxt <= trap_brk_c;
1915 2 zero_gravi
 
1916
 
1917 38 zero_gravi
    -- exception: 0.6 store address misaligned -
1918 6 zero_gravi
    elsif (trap_ctrl.exc_buf(exception_salign_c) = '1') then
1919 12 zero_gravi
      trap_ctrl.cause_nxt <= trap_sma_c;
1920 2 zero_gravi
 
1921 38 zero_gravi
    -- exception: 0.4 load address misaligned --
1922 6 zero_gravi
    elsif (trap_ctrl.exc_buf(exception_lalign_c) = '1') then
1923 12 zero_gravi
      trap_ctrl.cause_nxt <= trap_lma_c;
1924 2 zero_gravi
 
1925 38 zero_gravi
    -- exception: 0.7 store access fault --
1926 6 zero_gravi
    elsif (trap_ctrl.exc_buf(exception_saccess_c) = '1') then
1927 12 zero_gravi
      trap_ctrl.cause_nxt <= trap_sbe_c;
1928 2 zero_gravi
 
1929 38 zero_gravi
    -- exception: 0.5 load access fault --
1930 6 zero_gravi
    elsif (trap_ctrl.exc_buf(exception_laccess_c) = '1') then
1931 12 zero_gravi
      trap_ctrl.cause_nxt <= trap_lbe_c;
1932 59 zero_gravi
 
1933
 
1934
    -- ----------------------------------------------------------------------------------------
1935
    -- re-enter debug mode during single-stepping; this debug mode entry trap has the lowest
1936
    -- priority to let "normal" traps kick in during single stepping
1937
    -- ----------------------------------------------------------------------------------------
1938
 
1939
    -- single stepping --
1940
    elsif (CPU_EXTENSION_RISCV_DEBUG = true) and (trap_ctrl.irq_buf(interrupt_db_step_c) = '1') then
1941
      trap_ctrl.cause_nxt <= trap_db_step_c;
1942
      trap_ctrl.irq_ack_nxt(interrupt_db_step_c) <= '1';
1943 2 zero_gravi
    end if;
1944 6 zero_gravi
  end process trap_priority;
1945
 
1946 2 zero_gravi
 
1947 6 zero_gravi
-- ****************************************************************************************************************************
1948
-- Control and Status Registers (CSRs)
1949
-- ****************************************************************************************************************************
1950 2 zero_gravi
 
1951 27 zero_gravi
  -- Control and Status Registers Write Data ------------------------------------------------
1952
  -- -------------------------------------------------------------------------------------------
1953 36 zero_gravi
  csr_write_data: process(execute_engine.i_reg, csr.rdata, rs1_i)
1954
    variable csr_operand_v : std_ulogic_vector(data_width_c-1 downto 0);
1955 27 zero_gravi
  begin
1956 36 zero_gravi
    -- CSR operand source --
1957
    if (execute_engine.i_reg(instr_funct3_msb_c) = '1') then -- immediate
1958
      csr_operand_v := (others => '0');
1959 38 zero_gravi
      csr_operand_v(4 downto 0) := execute_engine.i_reg(19 downto 15); -- uimm5
1960 36 zero_gravi
    else -- register
1961
      csr_operand_v := rs1_i;
1962
    end if;
1963 40 zero_gravi
    -- tiny ALU for CSR write operations --
1964 27 zero_gravi
    case execute_engine.i_reg(instr_funct3_lsb_c+1 downto instr_funct3_lsb_c) is
1965 36 zero_gravi
      when "10"   => csr.wdata <= csr.rdata or csr_operand_v; -- CSRRS(I)
1966
      when "11"   => csr.wdata <= csr.rdata and (not csr_operand_v); -- CSRRC(I)
1967
      when others => csr.wdata <= csr_operand_v; -- CSRRW(I)
1968 27 zero_gravi
    end case;
1969
  end process csr_write_data;
1970
 
1971
 
1972 52 zero_gravi
  -- Control and Status Registers - Write Access --------------------------------------------
1973 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
1974
  csr_write_access: process(rstn_i, clk_i)
1975
  begin
1976 59 zero_gravi
    -- NOTE: If <dedicated_reset_c> = true then <def_rst_val_c> evaluates to '-'. Register that reset to <def_rst_val_c> do
1977
    -- NOT actually have a real reset by default (def_rst_val_c = '-') and have to be explicitly initialized by software!
1978 56 zero_gravi
    -- see: https://forums.xilinx.com/t5/General-Technical-Discussion/quot-Don-t-care-quot-reset-value/td-p/412845
1979 2 zero_gravi
    if (rstn_i = '0') then
1980 40 zero_gravi
      csr.we           <= '0';
1981 11 zero_gravi
      --
1982 6 zero_gravi
      csr.mstatus_mie  <= '0';
1983
      csr.mstatus_mpie <= '0';
1984 56 zero_gravi
      csr.mstatus_mpp  <= (others => '0');
1985 29 zero_gravi
      csr.privilege    <= priv_mode_m_c; -- start in MACHINE mode
1986 56 zero_gravi
      csr.mie_msie     <= def_rst_val_c;
1987
      csr.mie_meie     <= def_rst_val_c;
1988
      csr.mie_mtie     <= def_rst_val_c;
1989
      csr.mie_firqe    <= (others => def_rst_val_c);
1990
      csr.mtvec        <= (others => def_rst_val_c);
1991
      csr.mscratch     <= x"19880704";
1992
      csr.mepc         <= (others => def_rst_val_c);
1993
      csr.mcause       <= (others => def_rst_val_c);
1994
      csr.mtval        <= (others => def_rst_val_c);
1995 42 zero_gravi
      --
1996 52 zero_gravi
      csr.pmpcfg  <= (others => (others => '0'));
1997 56 zero_gravi
      csr.pmpaddr <= (others => (others => def_rst_val_c));
1998 34 zero_gravi
      --
1999 56 zero_gravi
      csr.mhpmevent <= (others => (others => def_rst_val_c));
2000 41 zero_gravi
      --
2001 56 zero_gravi
      csr.mcounteren_cy  <= def_rst_val_c;
2002
      csr.mcounteren_tm  <= def_rst_val_c;
2003
      csr.mcounteren_ir  <= def_rst_val_c;
2004
      csr.mcounteren_hpm <= (others => def_rst_val_c);
2005 42 zero_gravi
      --
2006 56 zero_gravi
      csr.mcountinhibit_cy  <= def_rst_val_c;
2007
      csr.mcountinhibit_ir  <= def_rst_val_c;
2008
      csr.mcountinhibit_hpm <= (others => def_rst_val_c);
2009 52 zero_gravi
      --
2010 56 zero_gravi
      csr.fflags <= (others => def_rst_val_c);
2011
      csr.frm    <= (others => def_rst_val_c);
2012 59 zero_gravi
      --
2013
      csr.dcsr_ebreakm <= '0';
2014
      csr.dcsr_ebreaku <= '0';
2015
      csr.dcsr_step    <= '0';
2016
      csr.dcsr_prv     <= (others => def_rst_val_c);
2017
      csr.dcsr_cause   <= (others => def_rst_val_c);
2018
      csr.dpc          <= (others => def_rst_val_c);
2019
      csr.dscratch0    <= (others => def_rst_val_c);
2020 49 zero_gravi
 
2021 2 zero_gravi
    elsif rising_edge(clk_i) then
2022 29 zero_gravi
      -- write access? --
2023
      csr.we <= csr.we_nxt;
2024 56 zero_gravi
 
2025 36 zero_gravi
      if (CPU_EXTENSION_RISCV_Zicsr = true) then
2026
        -- --------------------------------------------------------------------------------
2027
        -- CSR access by application software
2028
        -- --------------------------------------------------------------------------------
2029
        if (csr.we = '1') then -- manual update
2030 52 zero_gravi
 
2031
          -- user floating-point CSRs --
2032
          -- --------------------------------------------------------------------
2033 56 zero_gravi
          if (CPU_EXTENSION_RISCV_Zfinx = true) then -- floating point CSR class
2034
            if (csr.addr(11 downto 4) = csr_class_float_c) and (csr.addr(3 downto 2) = csr_fcsr_c(3 downto 2)) then
2035
              case csr.addr(1 downto 0) is
2036
                when "01" => -- R/W: fflags - floating-point (FPU) exception flags
2037
                  csr.fflags <= csr.wdata(4 downto 0);
2038
                when "10" => -- R/W: frm - floating-point (FPU) rounding mode
2039
                  csr.frm    <= csr.wdata(2 downto 0);
2040
                when "11" => -- R/W: fcsr - floating-point (FPU) control/status (frm + fflags)
2041
                  csr.frm    <= csr.wdata(7 downto 5);
2042
                  csr.fflags <= csr.wdata(4 downto 0);
2043
                when others => NULL;
2044
              end case;
2045 52 zero_gravi
            end if;
2046
          end if;
2047
 
2048
          -- machine trap setup --
2049
          -- --------------------------------------------------------------------
2050 59 zero_gravi
          if (csr.addr(11 downto 4) = csr_class_setup_c) then -- trap setup CSR class
2051 52 zero_gravi
            -- R/W: mstatus - machine status register --
2052
            if (csr.addr(3 downto 0) = csr_mstatus_c(3 downto 0)) then
2053 36 zero_gravi
              csr.mstatus_mie  <= csr.wdata(03);
2054
              csr.mstatus_mpie <= csr.wdata(07);
2055
              if (CPU_EXTENSION_RISCV_U = true) then -- user mode implemented
2056
                csr.mstatus_mpp(0) <= csr.wdata(11) or csr.wdata(12);
2057
                csr.mstatus_mpp(1) <= csr.wdata(11) or csr.wdata(12);
2058 40 zero_gravi
              else -- only machine mode is available
2059
                csr.mstatus_mpp <= priv_mode_m_c;
2060 36 zero_gravi
              end if;
2061 52 zero_gravi
            end if;
2062
            -- R/W: mie - machine interrupt enable register --
2063
            if (csr.addr(3 downto 0) = csr_mie_c(3 downto 0)) then
2064 29 zero_gravi
              csr.mie_msie <= csr.wdata(03); -- machine SW IRQ enable
2065
              csr.mie_mtie <= csr.wdata(07); -- machine TIMER IRQ enable
2066
              csr.mie_meie <= csr.wdata(11); -- machine EXT IRQ enable
2067 48 zero_gravi
              for i in 0 to 15 loop -- fast interrupt channels 0..15
2068
                csr.mie_firqe(i) <= csr.wdata(16+i);
2069
              end loop; -- i
2070 52 zero_gravi
            end if;
2071
            -- R/W: mtvec - machine trap-handler base address (for ALL exceptions) --
2072
            if (csr.addr(3 downto 0) = csr_mtvec_c(3 downto 0)) then
2073 29 zero_gravi
              csr.mtvec <= csr.wdata(data_width_c-1 downto 2) & "00"; -- mtvec.MODE=0
2074 52 zero_gravi
            end if;
2075
            -- R/W: machine counter enable register --
2076 56 zero_gravi
            if (CPU_EXTENSION_RISCV_U = true) then -- this CSR is hardwired to zero if user mode is not implemented
2077
              if (csr.addr(3 downto 0) = csr_mcounteren_c(3 downto 0)) then
2078 51 zero_gravi
                csr.mcounteren_cy  <= csr.wdata(0); -- enable user-level access to cycle[h]
2079
                csr.mcounteren_tm  <= csr.wdata(1); -- enable user-level access to time[h]
2080
                csr.mcounteren_ir  <= csr.wdata(2); -- enable user-level access to instret[h]
2081
                csr.mcounteren_hpm <= csr.wdata(csr.mcounteren_hpm'left+3 downto 3); -- enable user-level access to hpmcounterx[h]
2082
              end if;
2083 52 zero_gravi
            end if;
2084
          end if;
2085 29 zero_gravi
 
2086 52 zero_gravi
          -- machine trap handling --
2087
          -- --------------------------------------------------------------------
2088
          if (csr.addr(11 downto 4) = csr_class_trap_c) then -- machine trap handling CSR class
2089
            -- R/W: mscratch - machine scratch register --
2090
            if (csr.addr(3 downto 0) = csr_mscratch_c(3 downto 0)) then
2091 36 zero_gravi
              csr.mscratch <= csr.wdata;
2092 52 zero_gravi
            end if;
2093
            -- R/W: mepc - machine exception program counter --
2094
            if (csr.addr(3 downto 0) = csr_mepc_c(3 downto 0)) then
2095 36 zero_gravi
              csr.mepc <= csr.wdata(data_width_c-1 downto 1) & '0';
2096 52 zero_gravi
            end if;
2097
            -- R/W: mcause - machine trap cause --
2098
            if (csr.addr(3 downto 0) = csr_mcause_c(3 downto 0)) then
2099 36 zero_gravi
              csr.mcause(csr.mcause'left) <= csr.wdata(31); -- 1: interrupt, 0: exception
2100
              csr.mcause(4 downto 0)      <= csr.wdata(4 downto 0); -- identifier
2101 52 zero_gravi
            end if;
2102
          end if;
2103 29 zero_gravi
 
2104 52 zero_gravi
          -- physical memory protection: R/W: pmpcfg* - PMP configuration registers --
2105
          -- --------------------------------------------------------------------
2106 56 zero_gravi
          if (PMP_NUM_REGIONS > 0) then
2107
            if (csr.addr(11 downto 4) = csr_class_pmpcfg_c) then -- pmp configuration CSR class
2108 52 zero_gravi
              for i in 0 to PMP_NUM_REGIONS-1 loop
2109
                if (csr.addr(3 downto 0) = std_ulogic_vector(to_unsigned(i, 4))) then
2110
                  if (csr.pmpcfg(i)(7) = '0') then -- unlocked pmpcfg access
2111
                    csr.pmpcfg(i)(0) <= csr.wdata((i mod 4)*8+0); -- R (rights.read)
2112
                    csr.pmpcfg(i)(1) <= csr.wdata((i mod 4)*8+1); -- W (rights.write)
2113
                    csr.pmpcfg(i)(2) <= csr.wdata((i mod 4)*8+2); -- X (rights.execute)
2114
                    csr.pmpcfg(i)(3) <= csr.wdata((i mod 4)*8+3) and csr.wdata((i mod 4)*8+4); -- A_L
2115
                    csr.pmpcfg(i)(4) <= csr.wdata((i mod 4)*8+3) and csr.wdata((i mod 4)*8+4); -- A_H - NAPOT/OFF only
2116
                    csr.pmpcfg(i)(5) <= '0'; -- reserved
2117
                    csr.pmpcfg(i)(6) <= '0'; -- reserved
2118
                    csr.pmpcfg(i)(7) <= csr.wdata((i mod 4)*8+7); -- L (locked / rights also enforced in m-mode)
2119 36 zero_gravi
                  end if;
2120 52 zero_gravi
                end if;
2121
              end loop; -- i (PMP regions)
2122
            end if;
2123
          end if;
2124 4 zero_gravi
 
2125 52 zero_gravi
          -- physical memory protection: R/W: pmpaddr* - PMP address registers --
2126
          -- --------------------------------------------------------------------
2127 56 zero_gravi
          if (PMP_NUM_REGIONS > 0) then
2128
            if (csr.addr(11 downto 4) =  csr_pmpaddr0_c(11 downto 4)) or (csr.addr(11 downto 4) = csr_pmpaddr16_c(11 downto 4)) or
2129
               (csr.addr(11 downto 4) = csr_pmpaddr32_c(11 downto 4)) or (csr.addr(11 downto 4) = csr_pmpaddr48_c(11 downto 4)) then
2130 52 zero_gravi
              for i in 0 to PMP_NUM_REGIONS-1 loop
2131
                if (csr.addr(6 downto 0) = std_ulogic_vector(unsigned(csr_pmpaddr0_c(6 downto 0)) + i)) and (csr.pmpcfg(i)(7) = '0') then -- unlocked pmpaddr access
2132
                  csr.pmpaddr(i) <= csr.wdata;
2133
                  csr.pmpaddr(i)(index_size_f(PMP_MIN_GRANULARITY)-4 downto 0) <= (others => '1');
2134
                end if;
2135
              end loop; -- i (PMP regions)
2136
            end if;
2137
          end if;
2138 2 zero_gravi
 
2139 52 zero_gravi
          -- machine counter setup --
2140
          -- --------------------------------------------------------------------
2141 56 zero_gravi
          if (csr.addr(11 downto 5) = csr_cnt_setup_c) then -- counter configuration CSR class
2142
            -- R/W: mcountinhibit - machine counter-inhibit register --
2143
            if (csr.addr(4 downto 0) = csr_mcountinhibit_c(4 downto 0)) then
2144
              csr.mcountinhibit_cy  <= csr.wdata(0); -- enable auto-increment of [m]cycle[h] counter
2145
              csr.mcountinhibit_ir  <= csr.wdata(2); -- enable auto-increment of [m]instret[h] counter
2146
              csr.mcountinhibit_hpm <= csr.wdata(csr.mcountinhibit_hpm'left+3 downto 3); -- enable auto-increment of [m]hpmcounter*[h] counter
2147
            end if;
2148
            -- machine performance-monitoring event selector --
2149 52 zero_gravi
            if (HPM_NUM_CNTS > 0) then
2150
              for i in 0 to HPM_NUM_CNTS-1 loop
2151
                if (csr.addr(4 downto 0) = std_ulogic_vector(to_unsigned(i+3, 5))) then
2152
                  csr.mhpmevent(i) <= csr.wdata(csr.mhpmevent(i)'left downto 0);
2153
                end if;
2154 56 zero_gravi
                csr.mhpmevent(i)(hpmcnt_event_never_c) <= '0'; -- would be used for "TIME"
2155 52 zero_gravi
              end loop; -- i (CSRs)
2156
            end if;
2157
          end if;
2158 42 zero_gravi
 
2159 59 zero_gravi
          -- debug mode CSRs --
2160
          -- --------------------------------------------------------------------
2161
          if (CPU_EXTENSION_RISCV_DEBUG = true) then
2162
            if (csr.addr(11 downto 2) = csr_class_debug_c) then -- debug CSR class
2163
              -- R/W: dcsr - debug mode control and status register --
2164
              if (csr.addr(1 downto 0) = csr_dcsr_c(1 downto 0)) then
2165
                csr.dcsr_ebreakm <= csr.wdata(15);
2166
                csr.dcsr_step    <= csr.wdata(2);
2167
                if (CPU_EXTENSION_RISCV_U = true) then -- user mode implemented
2168
                  csr.dcsr_ebreaku <= csr.wdata(12);
2169
                  csr.dcsr_prv(0)  <= csr.wdata(1) or csr.wdata(0);
2170
                  csr.dcsr_prv(1)  <= csr.wdata(1) or csr.wdata(0);
2171
                else -- only machine mode is available
2172
                  csr.dcsr_prv <= priv_mode_m_c;
2173
                end if;
2174
              end if;
2175
              -- R/W: dpc - debug mode program counter --
2176
              if (csr.addr(1 downto 0) = csr_dpc_c(1 downto 0)) then
2177
                csr.dpc <= csr.wdata;
2178
              end if;
2179
              -- R/W: dscratch0 - debug mode scratch register 0 --
2180
              if (csr.addr(1 downto 0) = csr_dscratch0_c(1 downto 0)) then
2181
                csr.dscratch0 <= csr.wdata;
2182
              end if;
2183
            end if;
2184
          end if;
2185 29 zero_gravi
 
2186 59 zero_gravi
 
2187 36 zero_gravi
        -- --------------------------------------------------------------------------------
2188
        -- CSR access by hardware
2189
        -- --------------------------------------------------------------------------------
2190
        else
2191
 
2192 52 zero_gravi
          -- floating-point (FPU) exception flags --
2193
          -- --------------------------------------------------------------------
2194 55 zero_gravi
          if (CPU_EXTENSION_RISCV_Zfinx = true) then
2195 52 zero_gravi
            csr.fflags <= csr.fflags or fpu_flags_i; -- accumulate flags ("accrued exception flags")
2196
          end if;
2197
 
2198 59 zero_gravi
          -- mcause, mepc, mtval: write machine trap cause, PC and trap value register --
2199 36 zero_gravi
          -- --------------------------------------------------------------------
2200
          if (trap_ctrl.env_start_ack = '1') then -- trap handler starting?
2201 59 zero_gravi
            if (CPU_EXTENSION_RISCV_DEBUG = false) or ((trap_ctrl.cause(5) = '0') and -- update mtval/mepc/mcause only when NOT ENTRY debug mode exception
2202
                                                       (debug_ctrl.running = '0')) then -- and NOT IN debug mode
2203
 
2204
              -- trap cause ID code --
2205
              csr.mcause(csr.mcause'left) <= trap_ctrl.cause(trap_ctrl.cause'left); -- 1: interrupt, 0: exception
2206
              csr.mcause(4 downto 0)      <= trap_ctrl.cause(4 downto 0); -- identifier
2207
 
2208
              -- trap PC --
2209
              if (trap_ctrl.cause(trap_ctrl.cause'left) = '1') then -- for INTERRUPTS (async source)
2210
                csr.mepc <= execute_engine.pc(data_width_c-1 downto 1) & '0'; -- this is the CURRENT pc = interrupted instruction
2211
              else -- for sync. EXCEPTIONS (sync source)
2212
                csr.mepc <= execute_engine.last_pc(data_width_c-1 downto 1) & '0'; -- this is the LAST pc = last executed instruction
2213
              end if;
2214
 
2215
              -- trap value --
2216
              case trap_ctrl.cause is
2217
                when trap_ima_c | trap_iba_c => -- misaligned instruction address OR instruction access error
2218
                  csr.mtval <= execute_engine.pc(data_width_c-1 downto 1) & '0'; -- address of faulting instruction
2219
                when trap_brk_c => -- breakpoint
2220
                  csr.mtval <= execute_engine.last_pc(data_width_c-1 downto 1) & '0'; -- address of breakpoint instruction
2221
                when trap_lma_c | trap_lbe_c | trap_sma_c | trap_sbe_c => -- misaligned load/store address OR load/store access error
2222
                  csr.mtval <= mar_i; -- faulting data access address
2223
                when trap_iil_c => -- illegal instruction
2224
                  csr.mtval <= execute_engine.i_reg_last; -- faulting instruction itself
2225
                when others => -- everything else including all interrupts
2226
                  csr.mtval <= (others => '0');
2227
              end case;
2228
 
2229 40 zero_gravi
            end if;
2230 59 zero_gravi
 
2231
            -- trap enter: write dpc and dcsr --
2232
            -- --------------------------------------------------------------------
2233
            if (CPU_EXTENSION_RISCV_DEBUG = true) and (trap_ctrl.cause(5) = '1') and (debug_ctrl.running = '0') then -- debug mode entry exception
2234
 
2235
              -- trap cause ID code --
2236
              csr.dcsr_cause <= trap_ctrl.cause(2 downto 0); -- why did we enter debug mode?
2237
              -- current privilege mode when debug mode was entered --
2238
              csr.dcsr_prv <= csr.privilege;
2239
 
2240
              -- trap PC --
2241
              if (trap_ctrl.cause(trap_ctrl.cause'left) = '1') then -- for INTERRUPTS (async source)
2242
                csr.dpc <= execute_engine.pc(data_width_c-1 downto 1) & '0'; -- this is the CURRENT pc = interrupted instruction
2243
              else -- for sync. EXCEPTIONS (sync source)
2244
                csr.dpc <= execute_engine.last_pc(data_width_c-1 downto 1) & '0'; -- this is the LAST pc = last executed instruction
2245
              end if;
2246
 
2247
            end if;
2248
 
2249 2 zero_gravi
          end if;
2250
 
2251 36 zero_gravi
          -- mstatus: context switch --
2252
          -- --------------------------------------------------------------------
2253 59 zero_gravi
          -- ENTER: trap handling starting?
2254
          if (trap_ctrl.env_start_ack = '1') then
2255
            if (CPU_EXTENSION_RISCV_DEBUG = false) or -- normal trapping (debug mode NOT implemented)
2256
               ((debug_ctrl.running = '0') and (trap_ctrl.cause(5) = '0')) then -- not IN debug mode and not ENTERING debug mode
2257
              csr.mstatus_mie  <= '0'; -- disable interrupts
2258
              csr.mstatus_mpie <= csr.mstatus_mie; -- buffer previous mie state
2259
              if (CPU_EXTENSION_RISCV_U = true) then -- implement user mode
2260
                csr.privilege   <= priv_mode_m_c; -- execute trap in machine mode
2261
                csr.mstatus_mpp <= csr.privilege; -- buffer previous privilege mode
2262
              end if;
2263 2 zero_gravi
            end if;
2264 59 zero_gravi
 
2265
          -- EXIT: return from exception
2266
          elsif (trap_ctrl.env_end = '1') then
2267
            if (CPU_EXTENSION_RISCV_DEBUG = true) and (debug_ctrl.running = '1') then -- return from debug mode
2268
              if (CPU_EXTENSION_RISCV_U = true) then -- implement user mode
2269
                csr.privilege <= csr.dcsr_prv;
2270
              end if;
2271
            else -- return from "normal trap"
2272
              csr.mstatus_mie  <= csr.mstatus_mpie; -- restore global IRQ enable flag
2273
              csr.mstatus_mpie <= '1';
2274
              if (CPU_EXTENSION_RISCV_U = true) then -- implement user mode
2275
                csr.privilege   <= csr.mstatus_mpp; -- go back to previous privilege mode
2276 60 zero_gravi
                csr.mstatus_mpp <= (others => '0');
2277 59 zero_gravi
              end if;
2278 30 zero_gravi
            end if;
2279 2 zero_gravi
          end if;
2280 59 zero_gravi
 
2281 52 zero_gravi
        end if; -- /hardware csr access
2282
      end if;
2283 29 zero_gravi
 
2284 52 zero_gravi
      -- --------------------------------------------------------------------------------
2285
      -- override write access for disabled functions
2286
      -- --------------------------------------------------------------------------------
2287
 
2288
      -- user mode disabled --
2289
      if (CPU_EXTENSION_RISCV_U = false) then
2290
        csr.privilege      <= priv_mode_m_c;
2291
        csr.mstatus_mpp    <= priv_mode_m_c;
2292
        csr.mcounteren_cy  <= '0';
2293
        csr.mcounteren_tm  <= '0';
2294
        csr.mcounteren_ir  <= '0';
2295
        csr.mcounteren_hpm <= (others => '0');
2296 59 zero_gravi
        csr.dcsr_ebreaku   <= '0';
2297 60 zero_gravi
        csr.dcsr_prv       <= priv_mode_m_c;
2298 34 zero_gravi
      end if;
2299 52 zero_gravi
 
2300
      -- pmp disabled --
2301
      if (PMP_NUM_REGIONS = 0) then
2302
        csr.pmpcfg  <= (others => (others => '0'));
2303
        csr.pmpaddr <= (others => (others => '1'));
2304
      end if;
2305
 
2306
      -- hpms disabled --
2307
      if (HPM_NUM_CNTS = 0) then
2308
        csr.mhpmevent         <= (others => (others => '0'));
2309
        csr.mcounteren_hpm    <= (others => '0');
2310
        csr.mcountinhibit_hpm <= (others => '0');
2311
      end if;
2312
 
2313 56 zero_gravi
      -- cpu counters disabled --
2314
      if (CPU_CNT_WIDTH = 0) then
2315
        csr.mcounteren_cy    <= '0';
2316
        csr.mcounteren_ir    <= '0';
2317
        csr.mcountinhibit_cy <= '0';
2318
        csr.mcountinhibit_ir <= '0';
2319
      end if;
2320
 
2321 52 zero_gravi
      -- floating-point extension disabled --
2322 53 zero_gravi
      if (CPU_EXTENSION_RISCV_Zfinx = false) then
2323 52 zero_gravi
        csr.fflags <= (others => '0');
2324
        csr.frm    <= (others => '0');
2325
      end if;
2326
 
2327 59 zero_gravi
      -- debug mode disabled --
2328
      if (CPU_EXTENSION_RISCV_DEBUG = false) then
2329
        csr.dcsr_ebreakm <= '0';
2330
        csr.dcsr_ebreaku <= '0';
2331
        csr.dcsr_step    <= '0';
2332
        csr.dcsr_cause   <= (others => '0');
2333
        csr.dpc          <= (others => '0');
2334
        csr.dscratch0    <= (others => '0');
2335
      end if;
2336
 
2337 2 zero_gravi
    end if;
2338
  end process csr_write_access;
2339
 
2340 56 zero_gravi
  -- decode current privilege mode --
2341 59 zero_gravi
  csr.privilege_rd <= priv_mode_m_c when (CPU_EXTENSION_RISCV_E) and (debug_ctrl.running = '1') else csr.privilege; -- effective privilege mode ("machine" when in debug mode)
2342
  csr.priv_m_mode  <= '1' when (csr.privilege_rd = priv_mode_m_c) else '0';
2343
  csr.priv_u_mode  <= '1' when (csr.privilege_rd = priv_mode_u_c) and (CPU_EXTENSION_RISCV_U = true) else '0';
2344 40 zero_gravi
 
2345 36 zero_gravi
  -- PMP configuration output to bus unit --
2346 34 zero_gravi
  pmp_output: process(csr)
2347
  begin
2348
    pmp_addr_o <= (others => (others => '0'));
2349
    pmp_ctrl_o <= (others => (others => '0'));
2350 56 zero_gravi
    if (PMP_NUM_REGIONS /= 0) then
2351
      for i in 0 to PMP_NUM_REGIONS-1 loop
2352
        pmp_addr_o(i) <= csr.pmpaddr(i) & "11";
2353
        pmp_addr_o(i)(index_size_f(PMP_MIN_GRANULARITY)-4 downto 0) <= (others => '1');
2354
        pmp_ctrl_o(i) <= csr.pmpcfg(i);
2355
      end loop; -- i
2356
    end if;
2357 42 zero_gravi
  end process pmp_output;
2358
 
2359 58 zero_gravi
  -- PMP config read dummy --
2360 42 zero_gravi
  pmp_rd_dummy: process(csr)
2361
  begin
2362
    csr.pmpcfg_rd  <= (others => (others => '0'));
2363 56 zero_gravi
    if (PMP_NUM_REGIONS /= 0) then
2364
      for i in 0 to PMP_NUM_REGIONS-1 loop
2365
        csr.pmpcfg_rd(i)  <= csr.pmpcfg(i);
2366
      end loop; -- i
2367
    end if;
2368 42 zero_gravi
  end process pmp_rd_dummy;
2369
 
2370 52 zero_gravi
  -- FPU rounding mode --
2371
  fpu_rm_o <= csr.frm;
2372 42 zero_gravi
 
2373 52 zero_gravi
 
2374 42 zero_gravi
  -- Control and Status Registers - Counters ------------------------------------------------
2375
  -- -------------------------------------------------------------------------------------------
2376 56 zero_gravi
  csr_counters: process(rstn_i, clk_i)
2377 42 zero_gravi
  begin
2378 56 zero_gravi
    -- Counter CSRs (each counter is split into two 32-bit counters - coupled via an MSB overflow detector)
2379
    if (rstn_i = '0') then
2380 60 zero_gravi
      csr.mcycle          <= (others => def_rst_val_c);
2381
      csr.mcycle_msb      <= def_rst_val_c;
2382
      csr.mcycleh         <= (others => def_rst_val_c);
2383
      csr.minstret        <= (others => def_rst_val_c);
2384
      csr.minstret_msb    <= def_rst_val_c;
2385
      csr.minstreth       <= (others => def_rst_val_c);
2386
      csr.mhpmcounter     <= (others => (others => def_rst_val_c));
2387
      csr.mhpmcounter_msb <= (others => def_rst_val_c);
2388
      csr.mhpmcounterh    <= (others => (others => def_rst_val_c));
2389 56 zero_gravi
    elsif rising_edge(clk_i) then
2390 42 zero_gravi
 
2391
      -- [m]cycle --
2392 60 zero_gravi
      if (cpu_cnt_lo_width_c > 0) then
2393
        csr.mcycle_msb <= csr.mcycle(csr.mcycle'left);
2394
        if (csr.we = '1') and (csr.addr = csr_mcycle_c) then -- write access
2395
          csr.mcycle(cpu_cnt_lo_width_c downto 0) <= '0' & csr.wdata(cpu_cnt_lo_width_c-1 downto 0);
2396
        elsif (csr.mcountinhibit_cy = '0') and (cnt_event(hpmcnt_event_cy_c) = '1') then -- non-inhibited automatic update
2397
          csr.mcycle(cpu_cnt_lo_width_c downto 0) <= std_ulogic_vector(unsigned('0' & csr.mcycle(cpu_cnt_lo_width_c-1 downto 0)) + 1);
2398
        end if;
2399
      else
2400
        csr.mcycle     <= (others => '-');
2401
        csr.mcycle_msb <= '-';
2402 42 zero_gravi
      end if;
2403
 
2404
      -- [m]cycleh --
2405 60 zero_gravi
      if (cpu_cnt_hi_width_c > 0) then
2406
        if (csr.we = '1') and (csr.addr = csr_mcycleh_c) then -- write access
2407
          csr.mcycleh(cpu_cnt_hi_width_c-1 downto 0) <= csr.wdata(cpu_cnt_hi_width_c-1 downto 0);
2408
        elsif (csr.mcycle_msb = '0') and (csr.mcycle(csr.mcycle'left) = '1') then -- automatic update (continued)
2409
          csr.mcycleh(cpu_cnt_hi_width_c-1 downto 0) <= std_ulogic_vector(unsigned(csr.mcycleh(cpu_cnt_hi_width_c-1 downto 0)) + 1);
2410
        end if;
2411
      else
2412
        csr.mcycleh <= (others => '-');
2413 42 zero_gravi
      end if;
2414
 
2415 60 zero_gravi
 
2416 42 zero_gravi
      -- [m]instret --
2417 60 zero_gravi
      if (cpu_cnt_lo_width_c > 0) then
2418
        csr.minstret_msb <= csr.minstret(csr.minstret'left);
2419
        if (csr.we = '1') and (csr.addr = csr_minstret_c) then -- write access
2420
          csr.minstret(cpu_cnt_lo_width_c downto 0) <= '0' & csr.wdata(cpu_cnt_lo_width_c-1 downto 0);
2421
        elsif (csr.mcountinhibit_ir = '0') and (cnt_event(hpmcnt_event_ir_c) = '1') then -- non-inhibited automatic update
2422
          csr.minstret(cpu_cnt_lo_width_c downto 0) <= std_ulogic_vector(unsigned('0' & csr.minstret(cpu_cnt_lo_width_c-1 downto 0)) + 1);
2423
        end if;
2424
      else
2425
        csr.minstret     <= (others => '-');
2426
        csr.minstret_msb <= '-';
2427 42 zero_gravi
      end if;
2428
 
2429
      -- [m]instreth --
2430 60 zero_gravi
      if (cpu_cnt_hi_width_c > 0) then
2431
        if (csr.we = '1') and (csr.addr = csr_minstreth_c) then -- write access
2432
          csr.minstreth(cpu_cnt_hi_width_c-1 downto 0) <= csr.wdata(cpu_cnt_hi_width_c-1 downto 0);
2433
        elsif (csr.minstret_msb = '0') and (csr.minstret(csr.minstret'left) = '1') then -- automatic update (continued)
2434
          csr.minstreth(cpu_cnt_hi_width_c-1 downto 0) <= std_ulogic_vector(unsigned(csr.minstreth(cpu_cnt_hi_width_c-1 downto 0)) + 1);
2435
        end if;
2436
      else
2437
        csr.minstreth <= (others => '-');
2438 42 zero_gravi
      end if;
2439
 
2440 60 zero_gravi
 
2441 45 zero_gravi
      -- [machine] hardware performance monitors (counters) --
2442 42 zero_gravi
      for i in 0 to HPM_NUM_CNTS-1 loop
2443 60 zero_gravi
 
2444
        -- [m]hpmcounter* --
2445
        if (hpm_cnt_lo_width_c > 0) then
2446
          csr.mhpmcounter_msb(i) <= csr.mhpmcounter(i)(csr.mhpmcounter(i)'left);
2447 56 zero_gravi
          if (csr.we = '1') and (csr.addr = std_ulogic_vector(unsigned(csr_mhpmcounter3_c) + i)) then -- write access
2448
            csr.mhpmcounter(i)(hpm_cnt_lo_width_c downto 0) <= '0' & csr.wdata(hpm_cnt_lo_width_c-1 downto 0);
2449
          elsif (csr.mcountinhibit_hpm(i) = '0') and (hpmcnt_trigger(i) = '1') then -- non-inhibited automatic update
2450 60 zero_gravi
            csr.mhpmcounter(i)(hpm_cnt_lo_width_c downto 0) <= std_ulogic_vector(unsigned('0' & csr.mhpmcounter(i)(hpm_cnt_lo_width_c-1 downto 0)) + 1);
2451 56 zero_gravi
          end if;
2452 60 zero_gravi
        else
2453
          csr.mhpmcounter(i)     <= (others => '-');
2454
          csr.mhpmcounter_msb(i) <= '-';
2455 42 zero_gravi
        end if;
2456
 
2457
        -- [m]hpmcounter*h --
2458 60 zero_gravi
        if (hpm_cnt_hi_width_c > 0) then
2459 56 zero_gravi
          if (csr.we = '1') and (csr.addr = std_ulogic_vector(unsigned(csr_mhpmcounter3h_c) + i)) then -- write access
2460
            csr.mhpmcounterh(i)(hpm_cnt_hi_width_c-1 downto 0) <= csr.wdata(hpm_cnt_hi_width_c-1 downto 0);
2461 60 zero_gravi
          elsif (csr.mhpmcounter_msb(i) = '0') and (csr.mhpmcounter(i)(csr.mhpmcounter(i)'left) = '1') then -- automatic update (continued)
2462 56 zero_gravi
            csr.mhpmcounterh(i)(hpm_cnt_hi_width_c-1 downto 0) <= std_ulogic_vector(unsigned(csr.mhpmcounterh(i)(hpm_cnt_hi_width_c-1 downto 0)) + 1);
2463
          end if;
2464 60 zero_gravi
        else
2465
          csr.mhpmcounterh(i) <= (others => '-');
2466 42 zero_gravi
        end if;
2467 60 zero_gravi
 
2468 34 zero_gravi
      end loop; -- i
2469 42 zero_gravi
 
2470 34 zero_gravi
    end if;
2471 42 zero_gravi
  end process csr_counters;
2472 34 zero_gravi
 
2473 60 zero_gravi
 
2474 58 zero_gravi
  -- hpm counters read dummy --
2475 42 zero_gravi
  hpm_rd_dummy: process(csr)
2476
  begin
2477
    csr.mhpmcounter_rd  <= (others => (others => '0'));
2478
    csr.mhpmcounterh_rd <= (others => (others => '0'));
2479 56 zero_gravi
    if (HPM_NUM_CNTS /= 0) then
2480
      for i in 0 to HPM_NUM_CNTS-1 loop
2481
        if (hpm_cnt_lo_width_c > 0) then
2482 59 zero_gravi
          csr.mhpmcounter_rd(i)(hpm_cnt_lo_width_c-1 downto 0) <= csr.mhpmcounter(i)(hpm_cnt_lo_width_c-1 downto 0);
2483 56 zero_gravi
        end if;
2484
        if (hpm_cnt_hi_width_c > 0) then
2485
          csr.mhpmcounterh_rd(i)(hpm_cnt_hi_width_c-1 downto 0) <= csr.mhpmcounterh(i)(hpm_cnt_hi_width_c-1 downto 0);
2486
        end if;
2487
      end loop; -- i
2488
    end if;
2489 42 zero_gravi
  end process hpm_rd_dummy;
2490 34 zero_gravi
 
2491 42 zero_gravi
 
2492 56 zero_gravi
  -- Hardware Performance Monitor - Counter Event Control -----------------------------------
2493 42 zero_gravi
  -- -------------------------------------------------------------------------------------------
2494 56 zero_gravi
  hpmcnt_ctrl: process(rstn_i, clk_i)
2495 42 zero_gravi
  begin
2496 56 zero_gravi
    if (rstn_i = '0') then
2497
      cnt_event      <= (others => def_rst_val_c);
2498
      hpmcnt_trigger <= (others => def_rst_val_c);
2499
    elsif rising_edge(clk_i) then
2500 47 zero_gravi
      -- buffer event sources --
2501
      cnt_event <= cnt_event_nxt;
2502
      -- enable selected triggers by ANDing actual events and according CSR configuration bits --
2503
      -- OR everything to see if counter should increment --
2504 42 zero_gravi
      hpmcnt_trigger <= (others => '0'); -- default
2505 56 zero_gravi
      if (HPM_NUM_CNTS /= 0) then
2506
        for i in 0 to HPM_NUM_CNTS-1 loop
2507 60 zero_gravi
          hpmcnt_trigger(i) <= or_reduce_f(cnt_event and csr.mhpmevent(i)(cnt_event'left downto 0));
2508 56 zero_gravi
        end loop; -- i
2509
      end if;
2510 42 zero_gravi
    end if;
2511
  end process hpmcnt_ctrl;
2512
 
2513 56 zero_gravi
  -- counter event trigger - RISC-V-specific --
2514
  cnt_event_nxt(hpmcnt_event_cy_c)      <= not execute_engine.sleep; -- active cycle
2515
  cnt_event_nxt(hpmcnt_event_never_c)   <= '0'; -- undefined (never)
2516
  cnt_event_nxt(hpmcnt_event_ir_c)      <= '1' when (execute_engine.state = EXECUTE) else '0'; -- retired instruction
2517 42 zero_gravi
 
2518
  -- counter event trigger - custom / NEORV32-specific --
2519 47 zero_gravi
  cnt_event_nxt(hpmcnt_event_cir_c)     <= '1' when (execute_engine.state = EXECUTE)      and (execute_engine.is_ci = '1')             else '0'; -- retired compressed instruction
2520
  cnt_event_nxt(hpmcnt_event_wait_if_c) <= '1' when (fetch_engine.state   = IFETCH_ISSUE) and (fetch_engine.state_prev = IFETCH_ISSUE) else '0'; -- instruction fetch memory wait cycle
2521
  cnt_event_nxt(hpmcnt_event_wait_ii_c) <= '1' when (execute_engine.state = DISPATCH)     and (execute_engine.state_prev = DISPATCH)   else '0'; -- instruction issue wait cycle
2522
  cnt_event_nxt(hpmcnt_event_wait_mc_c) <= '1' when (execute_engine.state = ALU_WAIT)     and (execute_engine.state_prev = ALU_WAIT)   else '0'; -- multi-cycle alu-operation wait cycle
2523 42 zero_gravi
 
2524
  cnt_event_nxt(hpmcnt_event_load_c)    <= '1' when (execute_engine.state = LOADSTORE_1) and (ctrl(ctrl_bus_rd_c) = '1')               else '0'; -- load operation
2525
  cnt_event_nxt(hpmcnt_event_store_c)   <= '1' when (execute_engine.state = LOADSTORE_1) and (ctrl(ctrl_bus_wr_c) = '1')               else '0'; -- store operation
2526
  cnt_event_nxt(hpmcnt_event_wait_ls_c) <= '1' when (execute_engine.state = LOADSTORE_2) and (execute_engine.state_prev = LOADSTORE_2) else '0'; -- load/store memory wait cycle
2527
 
2528
  cnt_event_nxt(hpmcnt_event_jump_c)    <= '1' when (execute_engine.state = BRANCH) and (execute_engine.i_reg(instr_opcode_lsb_c+2) = '1') else '0'; -- jump (unconditional)
2529
  cnt_event_nxt(hpmcnt_event_branch_c)  <= '1' when (execute_engine.state = BRANCH) and (execute_engine.i_reg(instr_opcode_lsb_c+2) = '0') else '0'; -- branch (conditional, taken or not taken)
2530
  cnt_event_nxt(hpmcnt_event_tbranch_c) <= '1' when (execute_engine.state = BRANCH) and (execute_engine.i_reg(instr_opcode_lsb_c+2) = '0') and (execute_engine.branch_taken = '1') else '0'; -- taken branch (conditional)
2531
 
2532
  cnt_event_nxt(hpmcnt_event_trap_c)    <= '1' when (trap_ctrl.env_start_ack = '1')                                    else '0'; -- entered trap
2533
  cnt_event_nxt(hpmcnt_event_illegal_c) <= '1' when (trap_ctrl.env_start_ack = '1') and (trap_ctrl.cause = trap_iil_c) else '0'; -- illegal operation
2534
 
2535
 
2536 52 zero_gravi
  -- Control and Status Registers - Read Access ---------------------------------------------
2537 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
2538 56 zero_gravi
  csr_read_access: process(rstn_i, clk_i)
2539 2 zero_gravi
  begin
2540 56 zero_gravi
    if (rstn_i = '0') then
2541
      csr.re    <= def_rst_val_c;
2542
      csr.rdata <= (others => def_rst_val_c);
2543
    elsif rising_edge(clk_i) then
2544 29 zero_gravi
      csr.re    <= csr.re_nxt; -- read access?
2545 35 zero_gravi
      csr.rdata <= (others => '0'); -- default output
2546 11 zero_gravi
      if (CPU_EXTENSION_RISCV_Zicsr = true) and (csr.re = '1') then
2547 41 zero_gravi
        case csr.addr is
2548 11 zero_gravi
 
2549 58 zero_gravi
          -- floating-point CSRs --
2550 52 zero_gravi
          -- --------------------------------------------------------------------
2551 59 zero_gravi
          when csr_fflags_c => -- fflags (r/w): floating-point (FPU) exception flags
2552
            if (CPU_EXTENSION_RISCV_Zfinx = true) then csr.rdata(4 downto 0) <= csr.fflags; else NULL; end if;
2553
          when csr_frm_c => -- frm (r/w): floating-point (FPU) rounding mode
2554
            if (CPU_EXTENSION_RISCV_Zfinx = true) then csr.rdata(2 downto 0) <= csr.frm; else NULL; end if;
2555
          when csr_fcsr_c => -- fcsr (r/w): floating-point (FPU) control/status (frm + fflags)
2556
            if (CPU_EXTENSION_RISCV_Zfinx = true) then csr.rdata(7 downto 5) <= csr.frm; csr.rdata(4 downto 0) <= csr.fflags; else NULL; end if;
2557 52 zero_gravi
 
2558 11 zero_gravi
          -- machine trap setup --
2559 59 zero_gravi
          -- --------------------------------------------------------------------
2560
          when csr_mstatus_c => -- mstatus (r/w): machine status register
2561 41 zero_gravi
            csr.rdata(03) <= csr.mstatus_mie; -- MIE
2562 27 zero_gravi
            csr.rdata(07) <= csr.mstatus_mpie; -- MPIE
2563 29 zero_gravi
            csr.rdata(11) <= csr.mstatus_mpp(0); -- MPP: machine previous privilege mode low
2564
            csr.rdata(12) <= csr.mstatus_mpp(1); -- MPP: machine previous privilege mode high
2565 59 zero_gravi
          when csr_misa_c => -- misa (r/-): ISA and extensions
2566 39 zero_gravi
            csr.rdata(00) <= bool_to_ulogic_f(CPU_EXTENSION_RISCV_A);     -- A CPU extension
2567 27 zero_gravi
            csr.rdata(02) <= bool_to_ulogic_f(CPU_EXTENSION_RISCV_C);     -- C CPU extension
2568
            csr.rdata(04) <= bool_to_ulogic_f(CPU_EXTENSION_RISCV_E);     -- E CPU extension
2569
            csr.rdata(08) <= not bool_to_ulogic_f(CPU_EXTENSION_RISCV_E); -- I CPU extension (if not E)
2570
            csr.rdata(12) <= bool_to_ulogic_f(CPU_EXTENSION_RISCV_M);     -- M CPU extension
2571
            csr.rdata(20) <= bool_to_ulogic_f(CPU_EXTENSION_RISCV_U);     -- U CPU extension
2572
            csr.rdata(23) <= '1';                                         -- X CPU extension (non-std extensions)
2573
            csr.rdata(30) <= '1'; -- 32-bit architecture (MXL lo)
2574
            csr.rdata(31) <= '0'; -- 32-bit architecture (MXL hi)
2575 59 zero_gravi
          when csr_mie_c => -- mie (r/w): machine interrupt-enable register
2576 27 zero_gravi
            csr.rdata(03) <= csr.mie_msie; -- machine software IRQ enable
2577
            csr.rdata(07) <= csr.mie_mtie; -- machine timer IRQ enable
2578
            csr.rdata(11) <= csr.mie_meie; -- machine external IRQ enable
2579 48 zero_gravi
            for i in 0 to 15 loop -- fast interrupt channels 0..15 enable
2580
              csr.rdata(16+i) <= csr.mie_firqe(i);
2581
            end loop; -- i
2582 59 zero_gravi
          when csr_mtvec_c => -- mtvec (r/w): machine trap-handler base address (for ALL exceptions)
2583 27 zero_gravi
            csr.rdata <= csr.mtvec(data_width_c-1 downto 2) & "00"; -- mtvec.MODE=0
2584 59 zero_gravi
          when csr_mcounteren_c => -- mcounteren (r/w): machine counter enable register
2585 58 zero_gravi
            if (CPU_EXTENSION_RISCV_U = false) then -- this CSR is hardwired to zero if user mode is not implemented
2586
              NULL;
2587
            else
2588 51 zero_gravi
              csr.rdata(0) <= csr.mcounteren_cy; -- enable user-level access to cycle[h]
2589
              csr.rdata(1) <= csr.mcounteren_tm; -- enable user-level access to time[h]
2590
              csr.rdata(2) <= csr.mcounteren_ir; -- enable user-level access to instret[h]
2591
              csr.rdata(csr.mcounteren_hpm'left+3 downto 3) <= csr.mcounteren_hpm; -- enable user-level access to hpmcounterx[h]
2592
            end if;
2593 11 zero_gravi
 
2594
          -- machine trap handling --
2595 59 zero_gravi
          -- --------------------------------------------------------------------
2596
          when csr_mscratch_c => -- mscratch (r/w): machine scratch register
2597 27 zero_gravi
            csr.rdata <= csr.mscratch;
2598 59 zero_gravi
          when csr_mepc_c => -- mepc (r/w): machine exception program counter
2599 27 zero_gravi
            csr.rdata <= csr.mepc(data_width_c-1 downto 1) & '0';
2600 59 zero_gravi
          when csr_mcause_c => -- mcause (r/w): machine trap cause
2601 49 zero_gravi
            csr.rdata(31) <= csr.mcause(csr.mcause'left);
2602
            csr.rdata(csr.mcause'left-1 downto 0) <= csr.mcause(csr.mcause'left-1 downto 0);
2603 59 zero_gravi
          when csr_mtval_c => -- mtval (r/w): machine bad address or instruction
2604 27 zero_gravi
            csr.rdata <= csr.mtval;
2605 59 zero_gravi
          when csr_mip_c => -- mip (r/-): machine interrupt pending
2606 58 zero_gravi
            csr.rdata(03) <= trap_ctrl.irq_buf(interrupt_msw_irq_c);
2607
            csr.rdata(07) <= trap_ctrl.irq_buf(interrupt_mtime_irq_c);
2608
            csr.rdata(11) <= trap_ctrl.irq_buf(interrupt_mext_irq_c);
2609 48 zero_gravi
            for i in 0 to 15 loop -- fast interrupt channels 0..15 pending
2610 58 zero_gravi
              csr.rdata(16+i) <= trap_ctrl.irq_buf(interrupt_firq_0_c+i);
2611 48 zero_gravi
            end loop; -- i
2612 11 zero_gravi
 
2613 37 zero_gravi
          -- physical memory protection - configuration --
2614 59 zero_gravi
          -- --------------------------------------------------------------------
2615 58 zero_gravi
          when csr_pmpcfg0_c  => if (PMP_NUM_REGIONS > 00) then csr.rdata <= csr.pmpcfg_rd(03) & csr.pmpcfg_rd(02) & csr.pmpcfg_rd(01) & csr.pmpcfg_rd(00); else NULL; end if; -- R/W: pmpcfg0
2616
          when csr_pmpcfg1_c  => if (PMP_NUM_REGIONS > 03) then csr.rdata <= csr.pmpcfg_rd(07) & csr.pmpcfg_rd(06) & csr.pmpcfg_rd(05) & csr.pmpcfg_rd(04); else NULL; end if; -- R/W: pmpcfg1
2617
          when csr_pmpcfg2_c  => if (PMP_NUM_REGIONS > 07) then csr.rdata <= csr.pmpcfg_rd(11) & csr.pmpcfg_rd(10) & csr.pmpcfg_rd(09) & csr.pmpcfg_rd(08); else NULL; end if; -- R/W: pmpcfg2
2618
          when csr_pmpcfg3_c  => if (PMP_NUM_REGIONS > 11) then csr.rdata <= csr.pmpcfg_rd(15) & csr.pmpcfg_rd(14) & csr.pmpcfg_rd(13) & csr.pmpcfg_rd(12); else NULL; end if; -- R/W: pmpcfg3
2619
          when csr_pmpcfg4_c  => if (PMP_NUM_REGIONS > 15) then csr.rdata <= csr.pmpcfg_rd(19) & csr.pmpcfg_rd(18) & csr.pmpcfg_rd(17) & csr.pmpcfg_rd(16); else NULL; end if; -- R/W: pmpcfg4
2620
          when csr_pmpcfg5_c  => if (PMP_NUM_REGIONS > 19) then csr.rdata <= csr.pmpcfg_rd(23) & csr.pmpcfg_rd(22) & csr.pmpcfg_rd(21) & csr.pmpcfg_rd(20); else NULL; end if; -- R/W: pmpcfg5
2621
          when csr_pmpcfg6_c  => if (PMP_NUM_REGIONS > 23) then csr.rdata <= csr.pmpcfg_rd(27) & csr.pmpcfg_rd(26) & csr.pmpcfg_rd(25) & csr.pmpcfg_rd(24); else NULL; end if; -- R/W: pmpcfg6
2622
          when csr_pmpcfg7_c  => if (PMP_NUM_REGIONS > 27) then csr.rdata <= csr.pmpcfg_rd(31) & csr.pmpcfg_rd(30) & csr.pmpcfg_rd(29) & csr.pmpcfg_rd(28); else NULL; end if; -- R/W: pmpcfg7
2623
          when csr_pmpcfg8_c  => if (PMP_NUM_REGIONS > 31) then csr.rdata <= csr.pmpcfg_rd(35) & csr.pmpcfg_rd(34) & csr.pmpcfg_rd(33) & csr.pmpcfg_rd(32); else NULL; end if; -- R/W: pmpcfg8
2624
          when csr_pmpcfg9_c  => if (PMP_NUM_REGIONS > 35) then csr.rdata <= csr.pmpcfg_rd(39) & csr.pmpcfg_rd(38) & csr.pmpcfg_rd(37) & csr.pmpcfg_rd(36); else NULL; end if; -- R/W: pmpcfg9
2625
          when csr_pmpcfg10_c => if (PMP_NUM_REGIONS > 39) then csr.rdata <= csr.pmpcfg_rd(43) & csr.pmpcfg_rd(42) & csr.pmpcfg_rd(41) & csr.pmpcfg_rd(40); else NULL; end if; -- R/W: pmpcfg10
2626
          when csr_pmpcfg11_c => if (PMP_NUM_REGIONS > 43) then csr.rdata <= csr.pmpcfg_rd(47) & csr.pmpcfg_rd(46) & csr.pmpcfg_rd(45) & csr.pmpcfg_rd(44); else NULL; end if; -- R/W: pmpcfg11
2627
          when csr_pmpcfg12_c => if (PMP_NUM_REGIONS > 47) then csr.rdata <= csr.pmpcfg_rd(51) & csr.pmpcfg_rd(50) & csr.pmpcfg_rd(49) & csr.pmpcfg_rd(48); else NULL; end if; -- R/W: pmpcfg12
2628
          when csr_pmpcfg13_c => if (PMP_NUM_REGIONS > 51) then csr.rdata <= csr.pmpcfg_rd(55) & csr.pmpcfg_rd(54) & csr.pmpcfg_rd(53) & csr.pmpcfg_rd(52); else NULL; end if; -- R/W: pmpcfg13
2629
          when csr_pmpcfg14_c => if (PMP_NUM_REGIONS > 55) then csr.rdata <= csr.pmpcfg_rd(59) & csr.pmpcfg_rd(58) & csr.pmpcfg_rd(57) & csr.pmpcfg_rd(56); else NULL; end if; -- R/W: pmpcfg14
2630
          when csr_pmpcfg15_c => if (PMP_NUM_REGIONS > 59) then csr.rdata <= csr.pmpcfg_rd(63) & csr.pmpcfg_rd(62) & csr.pmpcfg_rd(61) & csr.pmpcfg_rd(60); else NULL; end if; -- R/W: pmpcfg15
2631 15 zero_gravi
 
2632 37 zero_gravi
          -- physical memory protection - addresses --
2633 59 zero_gravi
          -- --------------------------------------------------------------------
2634 58 zero_gravi
          when csr_pmpaddr0_c  => if (PMP_NUM_REGIONS > 00) then csr.rdata <= csr.pmpaddr(00); else NULL; end if; -- R/W: pmpaddr0
2635
          when csr_pmpaddr1_c  => if (PMP_NUM_REGIONS > 01) then csr.rdata <= csr.pmpaddr(01); else NULL; end if; -- R/W: pmpaddr1
2636
          when csr_pmpaddr2_c  => if (PMP_NUM_REGIONS > 02) then csr.rdata <= csr.pmpaddr(02); else NULL; end if; -- R/W: pmpaddr2
2637
          when csr_pmpaddr3_c  => if (PMP_NUM_REGIONS > 03) then csr.rdata <= csr.pmpaddr(03); else NULL; end if; -- R/W: pmpaddr3
2638
          when csr_pmpaddr4_c  => if (PMP_NUM_REGIONS > 04) then csr.rdata <= csr.pmpaddr(04); else NULL; end if; -- R/W: pmpaddr4
2639
          when csr_pmpaddr5_c  => if (PMP_NUM_REGIONS > 05) then csr.rdata <= csr.pmpaddr(05); else NULL; end if; -- R/W: pmpaddr5
2640
          when csr_pmpaddr6_c  => if (PMP_NUM_REGIONS > 06) then csr.rdata <= csr.pmpaddr(06); else NULL; end if; -- R/W: pmpaddr6
2641
          when csr_pmpaddr7_c  => if (PMP_NUM_REGIONS > 07) then csr.rdata <= csr.pmpaddr(07); else NULL; end if; -- R/W: pmpaddr7
2642
          when csr_pmpaddr8_c  => if (PMP_NUM_REGIONS > 08) then csr.rdata <= csr.pmpaddr(08); else NULL; end if; -- R/W: pmpaddr8
2643
          when csr_pmpaddr9_c  => if (PMP_NUM_REGIONS > 09) then csr.rdata <= csr.pmpaddr(09); else NULL; end if; -- R/W: pmpaddr9
2644
          when csr_pmpaddr10_c => if (PMP_NUM_REGIONS > 10) then csr.rdata <= csr.pmpaddr(10); else NULL; end if; -- R/W: pmpaddr10
2645
          when csr_pmpaddr11_c => if (PMP_NUM_REGIONS > 11) then csr.rdata <= csr.pmpaddr(11); else NULL; end if; -- R/W: pmpaddr11
2646
          when csr_pmpaddr12_c => if (PMP_NUM_REGIONS > 12) then csr.rdata <= csr.pmpaddr(12); else NULL; end if; -- R/W: pmpaddr12
2647
          when csr_pmpaddr13_c => if (PMP_NUM_REGIONS > 13) then csr.rdata <= csr.pmpaddr(13); else NULL; end if; -- R/W: pmpaddr13
2648
          when csr_pmpaddr14_c => if (PMP_NUM_REGIONS > 14) then csr.rdata <= csr.pmpaddr(14); else NULL; end if; -- R/W: pmpaddr14
2649
          when csr_pmpaddr15_c => if (PMP_NUM_REGIONS > 15) then csr.rdata <= csr.pmpaddr(15); else NULL; end if; -- R/W: pmpaddr15
2650
          when csr_pmpaddr16_c => if (PMP_NUM_REGIONS > 16) then csr.rdata <= csr.pmpaddr(16); else NULL; end if; -- R/W: pmpaddr16
2651
          when csr_pmpaddr17_c => if (PMP_NUM_REGIONS > 17) then csr.rdata <= csr.pmpaddr(17); else NULL; end if; -- R/W: pmpaddr17
2652
          when csr_pmpaddr18_c => if (PMP_NUM_REGIONS > 18) then csr.rdata <= csr.pmpaddr(18); else NULL; end if; -- R/W: pmpaddr18
2653
          when csr_pmpaddr19_c => if (PMP_NUM_REGIONS > 19) then csr.rdata <= csr.pmpaddr(19); else NULL; end if; -- R/W: pmpaddr19
2654
          when csr_pmpaddr20_c => if (PMP_NUM_REGIONS > 20) then csr.rdata <= csr.pmpaddr(20); else NULL; end if; -- R/W: pmpaddr20
2655
          when csr_pmpaddr21_c => if (PMP_NUM_REGIONS > 21) then csr.rdata <= csr.pmpaddr(21); else NULL; end if; -- R/W: pmpaddr21
2656
          when csr_pmpaddr22_c => if (PMP_NUM_REGIONS > 22) then csr.rdata <= csr.pmpaddr(22); else NULL; end if; -- R/W: pmpaddr22
2657
          when csr_pmpaddr23_c => if (PMP_NUM_REGIONS > 23) then csr.rdata <= csr.pmpaddr(23); else NULL; end if; -- R/W: pmpaddr23
2658
          when csr_pmpaddr24_c => if (PMP_NUM_REGIONS > 24) then csr.rdata <= csr.pmpaddr(24); else NULL; end if; -- R/W: pmpaddr24
2659
          when csr_pmpaddr25_c => if (PMP_NUM_REGIONS > 25) then csr.rdata <= csr.pmpaddr(25); else NULL; end if; -- R/W: pmpaddr25
2660
          when csr_pmpaddr26_c => if (PMP_NUM_REGIONS > 26) then csr.rdata <= csr.pmpaddr(26); else NULL; end if; -- R/W: pmpaddr26
2661
          when csr_pmpaddr27_c => if (PMP_NUM_REGIONS > 27) then csr.rdata <= csr.pmpaddr(27); else NULL; end if; -- R/W: pmpaddr27
2662
          when csr_pmpaddr28_c => if (PMP_NUM_REGIONS > 28) then csr.rdata <= csr.pmpaddr(28); else NULL; end if; -- R/W: pmpaddr28
2663
          when csr_pmpaddr29_c => if (PMP_NUM_REGIONS > 29) then csr.rdata <= csr.pmpaddr(29); else NULL; end if; -- R/W: pmpaddr29
2664
          when csr_pmpaddr30_c => if (PMP_NUM_REGIONS > 30) then csr.rdata <= csr.pmpaddr(30); else NULL; end if; -- R/W: pmpaddr30
2665
          when csr_pmpaddr31_c => if (PMP_NUM_REGIONS > 31) then csr.rdata <= csr.pmpaddr(31); else NULL; end if; -- R/W: pmpaddr31
2666
          when csr_pmpaddr32_c => if (PMP_NUM_REGIONS > 32) then csr.rdata <= csr.pmpaddr(32); else NULL; end if; -- R/W: pmpaddr32
2667
          when csr_pmpaddr33_c => if (PMP_NUM_REGIONS > 33) then csr.rdata <= csr.pmpaddr(33); else NULL; end if; -- R/W: pmpaddr33
2668
          when csr_pmpaddr34_c => if (PMP_NUM_REGIONS > 34) then csr.rdata <= csr.pmpaddr(34); else NULL; end if; -- R/W: pmpaddr34
2669
          when csr_pmpaddr35_c => if (PMP_NUM_REGIONS > 35) then csr.rdata <= csr.pmpaddr(35); else NULL; end if; -- R/W: pmpaddr35
2670
          when csr_pmpaddr36_c => if (PMP_NUM_REGIONS > 36) then csr.rdata <= csr.pmpaddr(36); else NULL; end if; -- R/W: pmpaddr36
2671
          when csr_pmpaddr37_c => if (PMP_NUM_REGIONS > 37) then csr.rdata <= csr.pmpaddr(37); else NULL; end if; -- R/W: pmpaddr37
2672
          when csr_pmpaddr38_c => if (PMP_NUM_REGIONS > 38) then csr.rdata <= csr.pmpaddr(38); else NULL; end if; -- R/W: pmpaddr38
2673
          when csr_pmpaddr39_c => if (PMP_NUM_REGIONS > 39) then csr.rdata <= csr.pmpaddr(39); else NULL; end if; -- R/W: pmpaddr39
2674
          when csr_pmpaddr40_c => if (PMP_NUM_REGIONS > 40) then csr.rdata <= csr.pmpaddr(40); else NULL; end if; -- R/W: pmpaddr40
2675
          when csr_pmpaddr41_c => if (PMP_NUM_REGIONS > 41) then csr.rdata <= csr.pmpaddr(41); else NULL; end if; -- R/W: pmpaddr41
2676
          when csr_pmpaddr42_c => if (PMP_NUM_REGIONS > 42) then csr.rdata <= csr.pmpaddr(42); else NULL; end if; -- R/W: pmpaddr42
2677
          when csr_pmpaddr43_c => if (PMP_NUM_REGIONS > 43) then csr.rdata <= csr.pmpaddr(43); else NULL; end if; -- R/W: pmpaddr43
2678
          when csr_pmpaddr44_c => if (PMP_NUM_REGIONS > 44) then csr.rdata <= csr.pmpaddr(44); else NULL; end if; -- R/W: pmpaddr44
2679
          when csr_pmpaddr45_c => if (PMP_NUM_REGIONS > 45) then csr.rdata <= csr.pmpaddr(45); else NULL; end if; -- R/W: pmpaddr45
2680
          when csr_pmpaddr46_c => if (PMP_NUM_REGIONS > 46) then csr.rdata <= csr.pmpaddr(46); else NULL; end if; -- R/W: pmpaddr46
2681
          when csr_pmpaddr47_c => if (PMP_NUM_REGIONS > 47) then csr.rdata <= csr.pmpaddr(47); else NULL; end if; -- R/W: pmpaddr47
2682
          when csr_pmpaddr48_c => if (PMP_NUM_REGIONS > 48) then csr.rdata <= csr.pmpaddr(48); else NULL; end if; -- R/W: pmpaddr48
2683
          when csr_pmpaddr49_c => if (PMP_NUM_REGIONS > 49) then csr.rdata <= csr.pmpaddr(49); else NULL; end if; -- R/W: pmpaddr49
2684
          when csr_pmpaddr50_c => if (PMP_NUM_REGIONS > 50) then csr.rdata <= csr.pmpaddr(50); else NULL; end if; -- R/W: pmpaddr50
2685
          when csr_pmpaddr51_c => if (PMP_NUM_REGIONS > 51) then csr.rdata <= csr.pmpaddr(51); else NULL; end if; -- R/W: pmpaddr51
2686
          when csr_pmpaddr52_c => if (PMP_NUM_REGIONS > 52) then csr.rdata <= csr.pmpaddr(52); else NULL; end if; -- R/W: pmpaddr52
2687
          when csr_pmpaddr53_c => if (PMP_NUM_REGIONS > 53) then csr.rdata <= csr.pmpaddr(53); else NULL; end if; -- R/W: pmpaddr53
2688
          when csr_pmpaddr54_c => if (PMP_NUM_REGIONS > 54) then csr.rdata <= csr.pmpaddr(54); else NULL; end if; -- R/W: pmpaddr54
2689
          when csr_pmpaddr55_c => if (PMP_NUM_REGIONS > 55) then csr.rdata <= csr.pmpaddr(55); else NULL; end if; -- R/W: pmpaddr55
2690
          when csr_pmpaddr56_c => if (PMP_NUM_REGIONS > 56) then csr.rdata <= csr.pmpaddr(56); else NULL; end if; -- R/W: pmpaddr56
2691
          when csr_pmpaddr57_c => if (PMP_NUM_REGIONS > 57) then csr.rdata <= csr.pmpaddr(57); else NULL; end if; -- R/W: pmpaddr57
2692
          when csr_pmpaddr58_c => if (PMP_NUM_REGIONS > 58) then csr.rdata <= csr.pmpaddr(58); else NULL; end if; -- R/W: pmpaddr58
2693
          when csr_pmpaddr59_c => if (PMP_NUM_REGIONS > 59) then csr.rdata <= csr.pmpaddr(59); else NULL; end if; -- R/W: pmpaddr59
2694
          when csr_pmpaddr60_c => if (PMP_NUM_REGIONS > 60) then csr.rdata <= csr.pmpaddr(60); else NULL; end if; -- R/W: pmpaddr60
2695
          when csr_pmpaddr61_c => if (PMP_NUM_REGIONS > 61) then csr.rdata <= csr.pmpaddr(61); else NULL; end if; -- R/W: pmpaddr61
2696
          when csr_pmpaddr62_c => if (PMP_NUM_REGIONS > 62) then csr.rdata <= csr.pmpaddr(62); else NULL; end if; -- R/W: pmpaddr62
2697
          when csr_pmpaddr63_c => if (PMP_NUM_REGIONS > 63) then csr.rdata <= csr.pmpaddr(63); else NULL; end if; -- R/W: pmpaddr63
2698 15 zero_gravi
 
2699 41 zero_gravi
          -- machine counter setup --
2700
          -- --------------------------------------------------------------------
2701 59 zero_gravi
          when csr_mcountinhibit_c => -- mcountinhibit (r/w): machine counter-inhibit register
2702 41 zero_gravi
            csr.rdata(0) <= csr.mcountinhibit_cy; -- enable auto-increment of [m]cycle[h] counter
2703
            csr.rdata(2) <= csr.mcountinhibit_ir; -- enable auto-increment of [m]instret[h] counter
2704 42 zero_gravi
            csr.rdata(csr.mcountinhibit_hpm'left+3 downto 3) <= csr.mcountinhibit_hpm; -- enable auto-increment of [m]hpmcounterx[h] counter
2705 41 zero_gravi
 
2706 42 zero_gravi
          -- machine performance-monitoring event selector --
2707 59 zero_gravi
          -- --------------------------------------------------------------------
2708 58 zero_gravi
          when csr_mhpmevent3_c  => if (HPM_NUM_CNTS > 00) then csr.rdata(hpmcnt_event_size_c-1 downto 0) <= csr.mhpmevent(00); else NULL; end if; -- R/W: mhpmevent3
2709
          when csr_mhpmevent4_c  => if (HPM_NUM_CNTS > 01) then csr.rdata(hpmcnt_event_size_c-1 downto 0) <= csr.mhpmevent(01); else NULL; end if; -- R/W: mhpmevent4
2710
          when csr_mhpmevent5_c  => if (HPM_NUM_CNTS > 02) then csr.rdata(hpmcnt_event_size_c-1 downto 0) <= csr.mhpmevent(02); else NULL; end if; -- R/W: mhpmevent5
2711
          when csr_mhpmevent6_c  => if (HPM_NUM_CNTS > 03) then csr.rdata(hpmcnt_event_size_c-1 downto 0) <= csr.mhpmevent(03); else NULL; end if; -- R/W: mhpmevent6
2712
          when csr_mhpmevent7_c  => if (HPM_NUM_CNTS > 04) then csr.rdata(hpmcnt_event_size_c-1 downto 0) <= csr.mhpmevent(04); else NULL; end if; -- R/W: mhpmevent7
2713
          when csr_mhpmevent8_c  => if (HPM_NUM_CNTS > 05) then csr.rdata(hpmcnt_event_size_c-1 downto 0) <= csr.mhpmevent(05); else NULL; end if; -- R/W: mhpmevent8
2714
          when csr_mhpmevent9_c  => if (HPM_NUM_CNTS > 06) then csr.rdata(hpmcnt_event_size_c-1 downto 0) <= csr.mhpmevent(06); else NULL; end if; -- R/W: mhpmevent9
2715
          when csr_mhpmevent10_c => if (HPM_NUM_CNTS > 07) then csr.rdata(hpmcnt_event_size_c-1 downto 0) <= csr.mhpmevent(07); else NULL; end if; -- R/W: mhpmevent10
2716
          when csr_mhpmevent11_c => if (HPM_NUM_CNTS > 08) then csr.rdata(hpmcnt_event_size_c-1 downto 0) <= csr.mhpmevent(08); else NULL; end if; -- R/W: mhpmevent11
2717
          when csr_mhpmevent12_c => if (HPM_NUM_CNTS > 09) then csr.rdata(hpmcnt_event_size_c-1 downto 0) <= csr.mhpmevent(09); else NULL; end if; -- R/W: mhpmevent12
2718
          when csr_mhpmevent13_c => if (HPM_NUM_CNTS > 10) then csr.rdata(hpmcnt_event_size_c-1 downto 0) <= csr.mhpmevent(10); else NULL; end if; -- R/W: mhpmevent13
2719
          when csr_mhpmevent14_c => if (HPM_NUM_CNTS > 11) then csr.rdata(hpmcnt_event_size_c-1 downto 0) <= csr.mhpmevent(11); else NULL; end if; -- R/W: mhpmevent14
2720
          when csr_mhpmevent15_c => if (HPM_NUM_CNTS > 12) then csr.rdata(hpmcnt_event_size_c-1 downto 0) <= csr.mhpmevent(12); else NULL; end if; -- R/W: mhpmevent15
2721
          when csr_mhpmevent16_c => if (HPM_NUM_CNTS > 13) then csr.rdata(hpmcnt_event_size_c-1 downto 0) <= csr.mhpmevent(13); else NULL; end if; -- R/W: mhpmevent16
2722
          when csr_mhpmevent17_c => if (HPM_NUM_CNTS > 14) then csr.rdata(hpmcnt_event_size_c-1 downto 0) <= csr.mhpmevent(14); else NULL; end if; -- R/W: mhpmevent17
2723
          when csr_mhpmevent18_c => if (HPM_NUM_CNTS > 15) then csr.rdata(hpmcnt_event_size_c-1 downto 0) <= csr.mhpmevent(15); else NULL; end if; -- R/W: mhpmevent18
2724
          when csr_mhpmevent19_c => if (HPM_NUM_CNTS > 16) then csr.rdata(hpmcnt_event_size_c-1 downto 0) <= csr.mhpmevent(16); else NULL; end if; -- R/W: mhpmevent19
2725
          when csr_mhpmevent20_c => if (HPM_NUM_CNTS > 17) then csr.rdata(hpmcnt_event_size_c-1 downto 0) <= csr.mhpmevent(17); else NULL; end if; -- R/W: mhpmevent20
2726
          when csr_mhpmevent21_c => if (HPM_NUM_CNTS > 18) then csr.rdata(hpmcnt_event_size_c-1 downto 0) <= csr.mhpmevent(18); else NULL; end if; -- R/W: mhpmevent21
2727
          when csr_mhpmevent22_c => if (HPM_NUM_CNTS > 19) then csr.rdata(hpmcnt_event_size_c-1 downto 0) <= csr.mhpmevent(19); else NULL; end if; -- R/W: mhpmevent22
2728
          when csr_mhpmevent23_c => if (HPM_NUM_CNTS > 20) then csr.rdata(hpmcnt_event_size_c-1 downto 0) <= csr.mhpmevent(20); else NULL; end if; -- R/W: mhpmevent23
2729
          when csr_mhpmevent24_c => if (HPM_NUM_CNTS > 21) then csr.rdata(hpmcnt_event_size_c-1 downto 0) <= csr.mhpmevent(21); else NULL; end if; -- R/W: mhpmevent24
2730
          when csr_mhpmevent25_c => if (HPM_NUM_CNTS > 22) then csr.rdata(hpmcnt_event_size_c-1 downto 0) <= csr.mhpmevent(22); else NULL; end if; -- R/W: mhpmevent25
2731
          when csr_mhpmevent26_c => if (HPM_NUM_CNTS > 23) then csr.rdata(hpmcnt_event_size_c-1 downto 0) <= csr.mhpmevent(23); else NULL; end if; -- R/W: mhpmevent26
2732
          when csr_mhpmevent27_c => if (HPM_NUM_CNTS > 24) then csr.rdata(hpmcnt_event_size_c-1 downto 0) <= csr.mhpmevent(24); else NULL; end if; -- R/W: mhpmevent27
2733
          when csr_mhpmevent28_c => if (HPM_NUM_CNTS > 25) then csr.rdata(hpmcnt_event_size_c-1 downto 0) <= csr.mhpmevent(25); else NULL; end if; -- R/W: mhpmevent28
2734
          when csr_mhpmevent29_c => if (HPM_NUM_CNTS > 26) then csr.rdata(hpmcnt_event_size_c-1 downto 0) <= csr.mhpmevent(26); else NULL; end if; -- R/W: mhpmevent29
2735
          when csr_mhpmevent30_c => if (HPM_NUM_CNTS > 27) then csr.rdata(hpmcnt_event_size_c-1 downto 0) <= csr.mhpmevent(27); else NULL; end if; -- R/W: mhpmevent30
2736
          when csr_mhpmevent31_c => if (HPM_NUM_CNTS > 28) then csr.rdata(hpmcnt_event_size_c-1 downto 0) <= csr.mhpmevent(28); else NULL; end if; -- R/W: mhpmevent31
2737 42 zero_gravi
 
2738 29 zero_gravi
          -- counters and timers --
2739 59 zero_gravi
          -- --------------------------------------------------------------------
2740
          when csr_cycle_c | csr_mcycle_c => -- [m]cycle (r/w): Cycle counter LOW
2741 58 zero_gravi
            if (cpu_cnt_lo_width_c > 0) then csr.rdata(cpu_cnt_lo_width_c-1 downto 0) <= csr.mcycle(cpu_cnt_lo_width_c-1 downto 0); else NULL; end if;
2742 59 zero_gravi
          when csr_cycleh_c | csr_mcycleh_c => -- [m]cycleh (r/w): Cycle counter HIGH
2743 58 zero_gravi
            if (cpu_cnt_hi_width_c > 0) then csr.rdata(cpu_cnt_hi_width_c-1 downto 0) <= csr.mcycleh(cpu_cnt_hi_width_c-1 downto 0); else NULL; end if;
2744
 
2745 59 zero_gravi
          when csr_instret_c | csr_minstret_c => -- [m]instret (r/w): Instructions-retired counter LOW
2746 58 zero_gravi
            if (cpu_cnt_lo_width_c > 0) then csr.rdata(cpu_cnt_lo_width_c-1 downto 0) <= csr.minstret(cpu_cnt_lo_width_c-1 downto 0); else NULL; end if;
2747 59 zero_gravi
          when csr_instreth_c | csr_minstreth_c => -- [m]instreth (r/w): Instructions-retired counter HIGH
2748 58 zero_gravi
            if (cpu_cnt_hi_width_c > 0) then csr.rdata(cpu_cnt_hi_width_c-1 downto 0) <= csr.minstreth(cpu_cnt_hi_width_c-1 downto 0); else NULL; end if;
2749
 
2750 59 zero_gravi
          when csr_time_c  => csr.rdata <= time_i(31 downto 0); -- time (r/-): System time LOW (from MTIME unit)
2751
          when csr_timeh_c => csr.rdata <= time_i(63 downto 32); -- timeh (r/-): System time HIGH (from MTIME unit)
2752 11 zero_gravi
 
2753 42 zero_gravi
          -- hardware performance counters --
2754 59 zero_gravi
          -- --------------------------------------------------------------------
2755 58 zero_gravi
          when csr_hpmcounter3_c   | csr_mhpmcounter3_c   => if (HPM_NUM_CNTS > 00) then csr.rdata <= csr.mhpmcounter_rd(00); else NULL; end if; -- (R)/(W): [m]hpmcounter3 - low
2756
          when csr_hpmcounter4_c   | csr_mhpmcounter4_c   => if (HPM_NUM_CNTS > 01) then csr.rdata <= csr.mhpmcounter_rd(01); else NULL; end if; -- (R)/(W): [m]hpmcounter4 - low
2757
          when csr_hpmcounter5_c   | csr_mhpmcounter5_c   => if (HPM_NUM_CNTS > 02) then csr.rdata <= csr.mhpmcounter_rd(02); else NULL; end if; -- (R)/(W): [m]hpmcounter5 - low
2758
          when csr_hpmcounter6_c   | csr_mhpmcounter6_c   => if (HPM_NUM_CNTS > 03) then csr.rdata <= csr.mhpmcounter_rd(03); else NULL; end if; -- (R)/(W): [m]hpmcounter6 - low
2759
          when csr_hpmcounter7_c   | csr_mhpmcounter7_c   => if (HPM_NUM_CNTS > 04) then csr.rdata <= csr.mhpmcounter_rd(04); else NULL; end if; -- (R)/(W): [m]hpmcounter7 - low
2760
          when csr_hpmcounter8_c   | csr_mhpmcounter8_c   => if (HPM_NUM_CNTS > 05) then csr.rdata <= csr.mhpmcounter_rd(05); else NULL; end if; -- (R)/(W): [m]hpmcounter8 - low
2761
          when csr_hpmcounter9_c   | csr_mhpmcounter9_c   => if (HPM_NUM_CNTS > 06) then csr.rdata <= csr.mhpmcounter_rd(06); else NULL; end if; -- (R)/(W): [m]hpmcounter9 - low
2762
          when csr_hpmcounter10_c  | csr_mhpmcounter10_c  => if (HPM_NUM_CNTS > 07) then csr.rdata <= csr.mhpmcounter_rd(07); else NULL; end if; -- (R)/(W): [m]hpmcounter10 - low
2763
          when csr_hpmcounter11_c  | csr_mhpmcounter11_c  => if (HPM_NUM_CNTS > 08) then csr.rdata <= csr.mhpmcounter_rd(08); else NULL; end if; -- (R)/(W): [m]hpmcounter11 - low
2764
          when csr_hpmcounter12_c  | csr_mhpmcounter12_c  => if (HPM_NUM_CNTS > 09) then csr.rdata <= csr.mhpmcounter_rd(09); else NULL; end if; -- (R)/(W): [m]hpmcounter12 - low
2765
          when csr_hpmcounter13_c  | csr_mhpmcounter13_c  => if (HPM_NUM_CNTS > 10) then csr.rdata <= csr.mhpmcounter_rd(10); else NULL; end if; -- (R)/(W): [m]hpmcounter13 - low
2766
          when csr_hpmcounter14_c  | csr_mhpmcounter14_c  => if (HPM_NUM_CNTS > 11) then csr.rdata <= csr.mhpmcounter_rd(11); else NULL; end if; -- (R)/(W): [m]hpmcounter14 - low
2767
          when csr_hpmcounter15_c  | csr_mhpmcounter15_c  => if (HPM_NUM_CNTS > 12) then csr.rdata <= csr.mhpmcounter_rd(12); else NULL; end if; -- (R)/(W): [m]hpmcounter15 - low
2768
          when csr_hpmcounter16_c  | csr_mhpmcounter16_c  => if (HPM_NUM_CNTS > 13) then csr.rdata <= csr.mhpmcounter_rd(13); else NULL; end if; -- (R)/(W): [m]hpmcounter16 - low
2769
          when csr_hpmcounter17_c  | csr_mhpmcounter17_c  => if (HPM_NUM_CNTS > 14) then csr.rdata <= csr.mhpmcounter_rd(14); else NULL; end if; -- (R)/(W): [m]hpmcounter17 - low
2770
          when csr_hpmcounter18_c  | csr_mhpmcounter18_c  => if (HPM_NUM_CNTS > 15) then csr.rdata <= csr.mhpmcounter_rd(15); else NULL; end if; -- (R)/(W): [m]hpmcounter18 - low
2771
          when csr_hpmcounter19_c  | csr_mhpmcounter19_c  => if (HPM_NUM_CNTS > 16) then csr.rdata <= csr.mhpmcounter_rd(16); else NULL; end if; -- (R)/(W): [m]hpmcounter19 - low
2772
          when csr_hpmcounter20_c  | csr_mhpmcounter20_c  => if (HPM_NUM_CNTS > 17) then csr.rdata <= csr.mhpmcounter_rd(17); else NULL; end if; -- (R)/(W): [m]hpmcounter20 - low
2773
          when csr_hpmcounter21_c  | csr_mhpmcounter21_c  => if (HPM_NUM_CNTS > 18) then csr.rdata <= csr.mhpmcounter_rd(18); else NULL; end if; -- (R)/(W): [m]hpmcounter21 - low
2774
          when csr_hpmcounter22_c  | csr_mhpmcounter22_c  => if (HPM_NUM_CNTS > 19) then csr.rdata <= csr.mhpmcounter_rd(19); else NULL; end if; -- (R)/(W): [m]hpmcounter22 - low
2775
          when csr_hpmcounter23_c  | csr_mhpmcounter23_c  => if (HPM_NUM_CNTS > 20) then csr.rdata <= csr.mhpmcounter_rd(20); else NULL; end if; -- (R)/(W): [m]hpmcounter23 - low
2776
          when csr_hpmcounter24_c  | csr_mhpmcounter24_c  => if (HPM_NUM_CNTS > 21) then csr.rdata <= csr.mhpmcounter_rd(21); else NULL; end if; -- (R)/(W): [m]hpmcounter24 - low
2777
          when csr_hpmcounter25_c  | csr_mhpmcounter25_c  => if (HPM_NUM_CNTS > 22) then csr.rdata <= csr.mhpmcounter_rd(22); else NULL; end if; -- (R)/(W): [m]hpmcounter25 - low
2778
          when csr_hpmcounter26_c  | csr_mhpmcounter26_c  => if (HPM_NUM_CNTS > 23) then csr.rdata <= csr.mhpmcounter_rd(23); else NULL; end if; -- (R)/(W): [m]hpmcounter26 - low
2779
          when csr_hpmcounter27_c  | csr_mhpmcounter27_c  => if (HPM_NUM_CNTS > 24) then csr.rdata <= csr.mhpmcounter_rd(24); else NULL; end if; -- (R)/(W): [m]hpmcounter27 - low
2780
          when csr_hpmcounter28_c  | csr_mhpmcounter28_c  => if (HPM_NUM_CNTS > 25) then csr.rdata <= csr.mhpmcounter_rd(25); else NULL; end if; -- (R)/(W): [m]hpmcounter28 - low
2781
          when csr_hpmcounter29_c  | csr_mhpmcounter29_c  => if (HPM_NUM_CNTS > 26) then csr.rdata <= csr.mhpmcounter_rd(26); else NULL; end if; -- (R)/(W): [m]hpmcounter29 - low
2782
          when csr_hpmcounter30_c  | csr_mhpmcounter30_c  => if (HPM_NUM_CNTS > 27) then csr.rdata <= csr.mhpmcounter_rd(27); else NULL; end if; -- (R)/(W): [m]hpmcounter30 - low
2783
          when csr_hpmcounter31_c  | csr_mhpmcounter31_c  => if (HPM_NUM_CNTS > 28) then csr.rdata <= csr.mhpmcounter_rd(28); else NULL; end if; -- (R)/(W): [m]hpmcounter31 - low
2784 42 zero_gravi
 
2785 58 zero_gravi
          when csr_hpmcounter3h_c  | csr_mhpmcounter3h_c  => if (HPM_NUM_CNTS > 00) and (hpm_cnt_hi_width_c > 0) then csr.rdata <= csr.mhpmcounterh_rd(00); else NULL; end if; -- (R)/(W): [m]hpmcounter3h - high
2786
          when csr_hpmcounter4h_c  | csr_mhpmcounter4h_c  => if (HPM_NUM_CNTS > 01) and (hpm_cnt_hi_width_c > 0) then csr.rdata <= csr.mhpmcounterh_rd(01); else NULL; end if; -- (R)/(W): [m]hpmcounter4h - high
2787
          when csr_hpmcounter5h_c  | csr_mhpmcounter5h_c  => if (HPM_NUM_CNTS > 02) and (hpm_cnt_hi_width_c > 0) then csr.rdata <= csr.mhpmcounterh_rd(02); else NULL; end if; -- (R)/(W): [m]hpmcounter5h - high
2788
          when csr_hpmcounter6h_c  | csr_mhpmcounter6h_c  => if (HPM_NUM_CNTS > 03) and (hpm_cnt_hi_width_c > 0) then csr.rdata <= csr.mhpmcounterh_rd(03); else NULL; end if; -- (R)/(W): [m]hpmcounter6h - high
2789
          when csr_hpmcounter7h_c  | csr_mhpmcounter7h_c  => if (HPM_NUM_CNTS > 04) and (hpm_cnt_hi_width_c > 0) then csr.rdata <= csr.mhpmcounterh_rd(04); else NULL; end if; -- (R)/(W): [m]hpmcounter7h - high
2790
          when csr_hpmcounter8h_c  | csr_mhpmcounter8h_c  => if (HPM_NUM_CNTS > 05) and (hpm_cnt_hi_width_c > 0) then csr.rdata <= csr.mhpmcounterh_rd(05); else NULL; end if; -- (R)/(W): [m]hpmcounter8h - high
2791
          when csr_hpmcounter9h_c  | csr_mhpmcounter9h_c  => if (HPM_NUM_CNTS > 06) and (hpm_cnt_hi_width_c > 0) then csr.rdata <= csr.mhpmcounterh_rd(06); else NULL; end if; -- (R)/(W): [m]hpmcounter9h - high
2792
          when csr_hpmcounter10h_c | csr_mhpmcounter10h_c => if (HPM_NUM_CNTS > 07) and (hpm_cnt_hi_width_c > 0) then csr.rdata <= csr.mhpmcounterh_rd(07); else NULL; end if; -- (R)/(W): [m]hpmcounter10h - high
2793
          when csr_hpmcounter11h_c | csr_mhpmcounter11h_c => if (HPM_NUM_CNTS > 08) and (hpm_cnt_hi_width_c > 0) then csr.rdata <= csr.mhpmcounterh_rd(08); else NULL; end if; -- (R)/(W): [m]hpmcounter11h - high
2794
          when csr_hpmcounter12h_c | csr_mhpmcounter12h_c => if (HPM_NUM_CNTS > 09) and (hpm_cnt_hi_width_c > 0) then csr.rdata <= csr.mhpmcounterh_rd(09); else NULL; end if; -- (R)/(W): [m]hpmcounter12h - high
2795
          when csr_hpmcounter13h_c | csr_mhpmcounter13h_c => if (HPM_NUM_CNTS > 10) and (hpm_cnt_hi_width_c > 0) then csr.rdata <= csr.mhpmcounterh_rd(10); else NULL; end if; -- (R)/(W): [m]hpmcounter13h - high
2796
          when csr_hpmcounter14h_c | csr_mhpmcounter14h_c => if (HPM_NUM_CNTS > 11) and (hpm_cnt_hi_width_c > 0) then csr.rdata <= csr.mhpmcounterh_rd(11); else NULL; end if; -- (R)/(W): [m]hpmcounter14h - high
2797
          when csr_hpmcounter15h_c | csr_mhpmcounter15h_c => if (HPM_NUM_CNTS > 12) and (hpm_cnt_hi_width_c > 0) then csr.rdata <= csr.mhpmcounterh_rd(12); else NULL; end if; -- (R)/(W): [m]hpmcounter15h - high
2798
          when csr_hpmcounter16h_c | csr_mhpmcounter16h_c => if (HPM_NUM_CNTS > 13) and (hpm_cnt_hi_width_c > 0) then csr.rdata <= csr.mhpmcounterh_rd(13); else NULL; end if; -- (R)/(W): [m]hpmcounter16h - high
2799
          when csr_hpmcounter17h_c | csr_mhpmcounter17h_c => if (HPM_NUM_CNTS > 14) and (hpm_cnt_hi_width_c > 0) then csr.rdata <= csr.mhpmcounterh_rd(14); else NULL; end if; -- (R)/(W): [m]hpmcounter17h - high
2800
          when csr_hpmcounter18h_c | csr_mhpmcounter18h_c => if (HPM_NUM_CNTS > 15) and (hpm_cnt_hi_width_c > 0) then csr.rdata <= csr.mhpmcounterh_rd(15); else NULL; end if; -- (R)/(W): [m]hpmcounter18h - high
2801
          when csr_hpmcounter19h_c | csr_mhpmcounter19h_c => if (HPM_NUM_CNTS > 16) and (hpm_cnt_hi_width_c > 0) then csr.rdata <= csr.mhpmcounterh_rd(16); else NULL; end if; -- (R)/(W): [m]hpmcounter19h - high
2802
          when csr_hpmcounter20h_c | csr_mhpmcounter20h_c => if (HPM_NUM_CNTS > 17) and (hpm_cnt_hi_width_c > 0) then csr.rdata <= csr.mhpmcounterh_rd(17); else NULL; end if; -- (R)/(W): [m]hpmcounter20h - high
2803
          when csr_hpmcounter21h_c | csr_mhpmcounter21h_c => if (HPM_NUM_CNTS > 18) and (hpm_cnt_hi_width_c > 0) then csr.rdata <= csr.mhpmcounterh_rd(18); else NULL; end if; -- (R)/(W): [m]hpmcounter21h - high
2804
          when csr_hpmcounter22h_c | csr_mhpmcounter22h_c => if (HPM_NUM_CNTS > 19) and (hpm_cnt_hi_width_c > 0) then csr.rdata <= csr.mhpmcounterh_rd(19); else NULL; end if; -- (R)/(W): [m]hpmcounter22h - high
2805
          when csr_hpmcounter23h_c | csr_mhpmcounter23h_c => if (HPM_NUM_CNTS > 20) and (hpm_cnt_hi_width_c > 0) then csr.rdata <= csr.mhpmcounterh_rd(20); else NULL; end if; -- (R)/(W): [m]hpmcounter23h - high
2806
          when csr_hpmcounter24h_c | csr_mhpmcounter24h_c => if (HPM_NUM_CNTS > 21) and (hpm_cnt_hi_width_c > 0) then csr.rdata <= csr.mhpmcounterh_rd(21); else NULL; end if; -- (R)/(W): [m]hpmcounter24h - high
2807
          when csr_hpmcounter25h_c | csr_mhpmcounter25h_c => if (HPM_NUM_CNTS > 22) and (hpm_cnt_hi_width_c > 0) then csr.rdata <= csr.mhpmcounterh_rd(22); else NULL; end if; -- (R)/(W): [m]hpmcounter25h - high
2808
          when csr_hpmcounter26h_c | csr_mhpmcounter26h_c => if (HPM_NUM_CNTS > 23) and (hpm_cnt_hi_width_c > 0) then csr.rdata <= csr.mhpmcounterh_rd(23); else NULL; end if; -- (R)/(W): [m]hpmcounter26h - high
2809
          when csr_hpmcounter27h_c | csr_mhpmcounter27h_c => if (HPM_NUM_CNTS > 24) and (hpm_cnt_hi_width_c > 0) then csr.rdata <= csr.mhpmcounterh_rd(24); else NULL; end if; -- (R)/(W): [m]hpmcounter27h - high
2810
          when csr_hpmcounter28h_c | csr_mhpmcounter28h_c => if (HPM_NUM_CNTS > 25) and (hpm_cnt_hi_width_c > 0) then csr.rdata <= csr.mhpmcounterh_rd(25); else NULL; end if; -- (R)/(W): [m]hpmcounter28h - high
2811
          when csr_hpmcounter29h_c | csr_mhpmcounter29h_c => if (HPM_NUM_CNTS > 26) and (hpm_cnt_hi_width_c > 0) then csr.rdata <= csr.mhpmcounterh_rd(26); else NULL; end if; -- (R)/(W): [m]hpmcounter29h - high
2812
          when csr_hpmcounter30h_c | csr_mhpmcounter30h_c => if (HPM_NUM_CNTS > 27) and (hpm_cnt_hi_width_c > 0) then csr.rdata <= csr.mhpmcounterh_rd(27); else NULL; end if; -- (R)/(W): [m]hpmcounter30h - high
2813
          when csr_hpmcounter31h_c | csr_mhpmcounter31h_c => if (HPM_NUM_CNTS > 28) and (hpm_cnt_hi_width_c > 0) then csr.rdata <= csr.mhpmcounterh_rd(28); else NULL; end if; -- (R)/(W): [m]hpmcounter31h - high
2814 42 zero_gravi
 
2815 11 zero_gravi
          -- machine information registers --
2816 59 zero_gravi
          -- --------------------------------------------------------------------
2817
          when csr_mvendorid_c => csr.rdata <= (others => '0'); -- mvendorid (r/-): vendor ID
2818
          when csr_marchid_c   => csr.rdata(4 downto 0) <= "10011"; -- marchid (r/-): arch ID - official RISC-V open-source arch ID
2819
          when csr_mimpid_c    => csr.rdata <= hw_version_c; -- mimpid (r/-): implementation ID -- NEORV32 hardware version
2820
          when csr_mhartid_c   => csr.rdata <= std_ulogic_vector(to_unsigned(HW_THREAD_ID, 32)); -- mhartid (r/-): hardware thread ID
2821 11 zero_gravi
 
2822 22 zero_gravi
          -- custom machine read-only CSRs --
2823 59 zero_gravi
          -- --------------------------------------------------------------------
2824
          when csr_mzext_c => -- mzext (r/-): available RISC-V Z* sub-extensions
2825 44 zero_gravi
            csr.rdata(0) <= bool_to_ulogic_f(CPU_EXTENSION_RISCV_Zicsr);    -- Zicsr
2826
            csr.rdata(1) <= bool_to_ulogic_f(CPU_EXTENSION_RISCV_Zifencei); -- Zifencei
2827 53 zero_gravi
            csr.rdata(5) <= bool_to_ulogic_f(CPU_EXTENSION_RISCV_Zfinx);    -- Zfinx ("F-alternative")
2828 56 zero_gravi
            if (CPU_CNT_WIDTH = 64) then
2829
              csr.rdata(6) <= '0'; -- Zxscnt (custom)
2830
              csr.rdata(7) <= '0'; -- Zxnocnt (custom)
2831
            elsif (CPU_CNT_WIDTH = 0) then
2832
              csr.rdata(6) <= '0'; -- Zxscnt (custom)
2833
              csr.rdata(7) <= '1'; -- Zxnocnt (custom)
2834
            else -- counters available but 0-bit < actual_size < 64-bit
2835
              csr.rdata(6) <= '1'; -- Zxscnt (custom)
2836
              csr.rdata(7) <= '0'; -- Zxnocnt (custom)
2837
            end if;
2838 58 zero_gravi
            csr.rdata(8) <= bool_to_ulogic_f(boolean(PMP_NUM_REGIONS > 0)); -- PMP (physical memory protection)
2839
            csr.rdata(9) <= bool_to_ulogic_f(boolean(HPM_NUM_CNTS > 0)); -- HPM (hardware performance monitors)
2840 59 zero_gravi
            csr.rdata(10) <= bool_to_ulogic_f(CPU_EXTENSION_RISCV_DEBUG); -- RISC-V debug mode
2841 22 zero_gravi
 
2842 59 zero_gravi
          -- debug mode CSRs --
2843
          -- --------------------------------------------------------------------
2844
          when csr_dcsr_c      => if (CPU_EXTENSION_RISCV_DEBUG = true) then csr.rdata <= csr.dcsr_rd;   else NULL; end if; -- dcsr (r/w): debug mode control and status
2845
          when csr_dpc_c       => if (CPU_EXTENSION_RISCV_DEBUG = true) then csr.rdata <= csr.dpc;       else NULL; end if; -- dpc (r/w): debug mode program counter
2846
          when csr_dscratch0_c => if (CPU_EXTENSION_RISCV_DEBUG = true) then csr.rdata <= csr.dscratch0; else NULL; end if; -- dscratch0 (r/w): debug mode scratch register 0
2847
 
2848 11 zero_gravi
          -- undefined/unavailable --
2849 59 zero_gravi
          -- --------------------------------------------------------------------
2850 11 zero_gravi
          when others =>
2851 60 zero_gravi
            NULL; -- not implemented, read as zero if read access is granted
2852 11 zero_gravi
 
2853
        end case;
2854 2 zero_gravi
      end if;
2855
    end if;
2856
  end process csr_read_access;
2857
 
2858 27 zero_gravi
  -- CSR read data output --
2859
  csr_rdata_o <= csr.rdata;
2860
 
2861 12 zero_gravi
 
2862 59 zero_gravi
  -- Debug Control --------------------------------------------------------------------------
2863
  -- -------------------------------------------------------------------------------------------
2864
  debug_control: process(rstn_i, clk_i)
2865
  begin
2866
    if (rstn_i = '0') then
2867
      debug_ctrl.state        <= DEBUG_OFFLINE;
2868
      debug_ctrl.ext_halt_req <= "00";
2869
    elsif rising_edge(clk_i) then
2870
      if (CPU_EXTENSION_RISCV_DEBUG = true) then
2871
 
2872
        -- rising edge detector --
2873
        debug_ctrl.ext_halt_req <= debug_ctrl.ext_halt_req(0) & db_halt_req_i;
2874
 
2875
        -- state machine --
2876
        case debug_ctrl.state is
2877
 
2878
          when DEBUG_OFFLINE => -- not in debug mode, waiting for entering request
2879
            if (debug_ctrl.trig_halt = '1') or -- external request (from DM)
2880
               (debug_ctrl.trig_break = '1') or -- ebreak instruction
2881
               (debug_ctrl.trig_step = '1') then -- single-stepping mode
2882
              debug_ctrl.state <= DEBUG_PENDING;
2883
            end if;
2884
 
2885
          when DEBUG_PENDING => -- waiting to start debug mode
2886
            if (trap_ctrl.env_start_ack = '1') and (trap_ctrl.cause(5) = '1') then -- processing trap entry into debug mode
2887
              debug_ctrl.state <= DEBUG_ONLINE;
2888
            end if;
2889
 
2890
          when DEBUG_ONLINE => -- we are in debug mode
2891
            if (debug_ctrl.dret = '1') then -- DRET instruction
2892
              debug_ctrl.state <= DEBUG_EXIT;
2893
            end if;
2894
 
2895
          when DEBUG_EXIT => -- leaving debug mode
2896
            if (execute_engine.state = TRAP_EXECUTE) then -- processing trap exit
2897
              debug_ctrl.state <= DEBUG_OFFLINE;
2898
            end if;
2899
 
2900
          when others => -- undefined
2901
            debug_ctrl.state <= DEBUG_OFFLINE;
2902
 
2903
        end case;
2904
      else -- debug mode NOT implemented
2905
        debug_ctrl.state        <= DEBUG_OFFLINE;
2906
        debug_ctrl.ext_halt_req <= "00";
2907
      end if;
2908
    end if;
2909
  end process debug_control;
2910
 
2911
  -- state decoding --
2912
  debug_ctrl.pending <= '1' when (debug_ctrl.state = DEBUG_PENDING) and (CPU_EXTENSION_RISCV_DEBUG = true) else '0';
2913
  debug_ctrl.running <= '1' when ((debug_ctrl.state = DEBUG_ONLINE) or (debug_ctrl.state = DEBUG_EXIT)) and (CPU_EXTENSION_RISCV_DEBUG = true) else '0';
2914
 
2915
  -- entry debug mode triggers --
2916
  debug_ctrl.trig_break <= trap_ctrl.break_point and (debug_ctrl.running or -- we are in debug mode: re-enter debug mode
2917 60 zero_gravi
                           (csr.priv_m_mode and csr.dcsr_ebreakm and (not debug_ctrl.running)) or -- enabled goto-debug-mode in machine mode on "ebreak"
2918
                           (csr.priv_u_mode and csr.dcsr_ebreaku and (not debug_ctrl.running))); -- enabled goto-debug-mode in user mode on "ebreak"
2919 59 zero_gravi
  debug_ctrl.trig_halt <= (not debug_ctrl.ext_halt_req(1)) and debug_ctrl.ext_halt_req(0) and (not debug_ctrl.running); -- rising edge detector from external halt request (if not halted already)
2920
  debug_ctrl.trig_step <= csr.dcsr_step and (not debug_ctrl.running); -- single-step mode (trigger when NOT CURRENTLY in debug mode)
2921
 
2922
 
2923
  -- Debug Control and Status Register (dcsr) - Read-Back -----------------------------------
2924
  -- -------------------------------------------------------------------------------------------
2925
  dcsr_readback_false:
2926
  if (CPU_EXTENSION_RISCV_DEBUG = false) generate
2927 60 zero_gravi
    csr.dcsr_rd <= (others => '-');
2928 59 zero_gravi
  end generate;
2929
 
2930
  dcsr_readback_true:
2931
  if (CPU_EXTENSION_RISCV_DEBUG = true) generate
2932
    csr.dcsr_rd(31 downto 28) <= "0100"; -- xdebugver: external debug support compatible to spec
2933
    csr.dcsr_rd(27 downto 16) <= (others => '0'); -- reserved
2934
    csr.dcsr_rd(15) <= csr.dcsr_ebreakm; -- ebreakm: what happens on ebreak in m-mode? (normal trap OR debug-enter)
2935
    csr.dcsr_rd(14) <= '0'; -- ebreakh: not available
2936
    csr.dcsr_rd(13) <= '0'; -- ebreaks: not available
2937
    csr.dcsr_rd(12) <= csr.dcsr_ebreaku when (CPU_EXTENSION_RISCV_U = true) else '0'; -- ebreaku: what happens on ebreak in u-mode? (normal trap OR debug-enter)
2938 60 zero_gravi
    csr.dcsr_rd(11) <= '0'; -- stepie: interrupts are disabled during single-stepping
2939 59 zero_gravi
    csr.dcsr_rd(10) <= '0'; -- stopcount: counters increment as usual FIXME ???
2940 60 zero_gravi
    csr.dcsr_rd(09) <= '0'; -- stoptime: timers increment as usual
2941
    csr.dcsr_rd(08 downto 06) <= csr.dcsr_cause; -- debug mode entry cause
2942 59 zero_gravi
    csr.dcsr_rd(05) <= '0'; -- reserved
2943
    csr.dcsr_rd(04) <= '0'; -- mprven: mstatus.mprv is ignored in debug mode
2944
    csr.dcsr_rd(03) <= trap_ctrl.irq_buf(interrupt_nm_irq_c); -- nmip: pending non-maskable interrupt
2945
    csr.dcsr_rd(02) <= csr.dcsr_step; -- step: single-step mode
2946
    csr.dcsr_rd(01 downto 00) <= csr.dcsr_prv; -- prv: privilege mode when debug mode was entered
2947
  end generate;
2948
 
2949
 
2950 2 zero_gravi
end neorv32_cpu_control_rtl;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.