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

Subversion Repositories neorv32

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

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