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

Subversion Repositories neorv32

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

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

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