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

Subversion Repositories neorv32

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

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