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

Subversion Repositories neorv32

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

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