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

Subversion Repositories neorv32

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

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

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