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

Subversion Repositories neorv32

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

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