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

Subversion Repositories neorv32

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

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

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