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

Subversion Repositories neorv32

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

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