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

Subversion Repositories neorv32

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

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