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

Subversion Repositories neorv32

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

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