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

Subversion Repositories neorv32

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

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

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