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

Subversion Repositories neorv32

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

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

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