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

Subversion Repositories neorv32

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

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