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

Subversion Repositories neorv32

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

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
-- # handling controller and the RISC-V status and control register set (CSRs).                    #
8 2 zero_gravi
-- # ********************************************************************************************* #
9
-- # BSD 3-Clause License                                                                          #
10
-- #                                                                                               #
11
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved.                                     #
12
-- #                                                                                               #
13
-- # Redistribution and use in source and binary forms, with or without modification, are          #
14
-- # permitted provided that the following conditions are met:                                     #
15
-- #                                                                                               #
16
-- # 1. Redistributions of source code must retain the above copyright notice, this list of        #
17
-- #    conditions and the following disclaimer.                                                   #
18
-- #                                                                                               #
19
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
20
-- #    conditions and the following disclaimer in the documentation and/or other materials        #
21
-- #    provided with the distribution.                                                            #
22
-- #                                                                                               #
23
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
24
-- #    endorse or promote products derived from this software without specific prior written      #
25
-- #    permission.                                                                                #
26
-- #                                                                                               #
27
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
28
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
29
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
30
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
31
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
32
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
33
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
34
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
35
-- # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
36
-- # ********************************************************************************************* #
37
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
38
-- #################################################################################################
39
 
40
library ieee;
41
use ieee.std_logic_1164.all;
42
use ieee.numeric_std.all;
43
 
44
library neorv32;
45
use neorv32.neorv32_package.all;
46
 
47
entity neorv32_cpu_control is
48
  generic (
49
    -- General --
50 12 zero_gravi
    HW_THREAD_ID                 : std_ulogic_vector(31 downto 0):= x"00000000"; -- hardware thread id
51
    CPU_BOOT_ADDR                : std_ulogic_vector(31 downto 0):= x"00000000"; -- cpu boot address
52 2 zero_gravi
    -- RISC-V CPU Extensions --
53 12 zero_gravi
    CPU_EXTENSION_RISCV_C        : boolean := false; -- implement compressed extension?
54
    CPU_EXTENSION_RISCV_E        : boolean := false; -- implement embedded RF extension?
55
    CPU_EXTENSION_RISCV_M        : boolean := false; -- implement muld/div extension?
56 15 zero_gravi
    CPU_EXTENSION_RISCV_U        : boolean := false; -- implement user mode extension?
57 12 zero_gravi
    CPU_EXTENSION_RISCV_Zicsr    : boolean := true;  -- implement CSR system?
58 15 zero_gravi
    CPU_EXTENSION_RISCV_Zifencei : boolean := true;  -- implement instruction stream sync.?
59
    -- Physical memory protection (PMP) --
60
    PMP_USE                      : boolean := false; -- implement physical memory protection?
61
    PMP_NUM_REGIONS              : natural := 4; -- number of regions (1..4)
62
    PMP_GRANULARITY              : natural := 0  -- granularity (0=none, 1=8B, 2=16B, 3=32B, ...)
63 2 zero_gravi
  );
64
  port (
65
    -- global control --
66
    clk_i         : in  std_ulogic; -- global clock, rising edge
67
    rstn_i        : in  std_ulogic; -- global reset, low-active, async
68
    ctrl_o        : out std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
69
    -- status input --
70
    alu_wait_i    : in  std_ulogic; -- wait for ALU
71 12 zero_gravi
    bus_i_wait_i  : in  std_ulogic; -- wait for bus
72
    bus_d_wait_i  : in  std_ulogic; -- wait for bus
73 2 zero_gravi
    -- data input --
74
    instr_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- instruction
75
    cmp_i         : in  std_ulogic_vector(1 downto 0); -- comparator status
76 27 zero_gravi
    alu_res_i     : in  std_ulogic_vector(data_width_c-1 downto 0); -- ALU processing result
77 2 zero_gravi
    -- data output --
78
    imm_o         : out std_ulogic_vector(data_width_c-1 downto 0); -- immediate
79 6 zero_gravi
    fetch_pc_o    : out std_ulogic_vector(data_width_c-1 downto 0); -- PC for instruction fetch
80
    curr_pc_o     : out std_ulogic_vector(data_width_c-1 downto 0); -- current PC (corresponding to current instruction)
81
    next_pc_o     : out std_ulogic_vector(data_width_c-1 downto 0); -- next PC (corresponding to current instruction)
82 2 zero_gravi
    csr_rdata_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- CSR read data
83 14 zero_gravi
    -- interrupts (risc-v compliant) --
84
    msw_irq_i     : in  std_ulogic; -- machine software interrupt
85
    mext_irq_i    : in  std_ulogic; -- machine external interrupt
86 2 zero_gravi
    mtime_irq_i   : in  std_ulogic; -- machine timer interrupt
87 14 zero_gravi
    -- fast interrupts (custom) --
88
    firq_i        : in  std_ulogic_vector(3 downto 0);
89 11 zero_gravi
    -- system time input from MTIME --
90
    time_i        : in  std_ulogic_vector(63 downto 0); -- current system time
91 15 zero_gravi
    -- physical memory protection --
92 23 zero_gravi
    pmp_addr_o    : out pmp_addr_if_t; -- addresses
93
    pmp_ctrl_o    : out pmp_ctrl_if_t; -- configs
94
    priv_mode_o   : out std_ulogic_vector(1 downto 0); -- current CPU privilege level
95 2 zero_gravi
    -- bus access exceptions --
96
    mar_i         : in  std_ulogic_vector(data_width_c-1 downto 0);  -- memory address register
97
    ma_instr_i    : in  std_ulogic; -- misaligned instruction address
98
    ma_load_i     : in  std_ulogic; -- misaligned load data address
99
    ma_store_i    : in  std_ulogic; -- misaligned store data address
100
    be_instr_i    : in  std_ulogic; -- bus error on instruction access
101
    be_load_i     : in  std_ulogic; -- bus error on load data access
102 12 zero_gravi
    be_store_i    : in  std_ulogic  -- bus error on store data access
103 2 zero_gravi
  );
104
end neorv32_cpu_control;
105
 
106
architecture neorv32_cpu_control_rtl of neorv32_cpu_control is
107
 
108 6 zero_gravi
  -- instruction fetch enginge --
109 31 zero_gravi
  type fetch_engine_state_t is (IFETCH_RESET, IFETCH_REQUEST, IFETCH_ISSUE);
110 6 zero_gravi
  type fetch_engine_t is record
111 31 zero_gravi
    state       : fetch_engine_state_t;
112
    state_nxt   : fetch_engine_state_t;
113
    pc          : std_ulogic_vector(data_width_c-1 downto 0);
114
    pc_nxt      : std_ulogic_vector(data_width_c-1 downto 0);
115
    reset       : std_ulogic;
116
    bus_err_ack : std_ulogic;
117 6 zero_gravi
  end record;
118
  signal fetch_engine : fetch_engine_t;
119 2 zero_gravi
 
120 32 zero_gravi
  -- instrucion prefetch buffer (IPB, real FIFO) --
121 31 zero_gravi
  type ipb_data_fifo_t is array (0 to ipb_entries_c-1) of std_ulogic_vector(2+31 downto 0);
122 6 zero_gravi
  type ipb_t is record
123 31 zero_gravi
    wdata : std_ulogic_vector(2+31 downto 0); -- write status (bus_error, align_error) + 32-bit instruction data
124
    we    : std_ulogic; -- trigger write
125
    free  : std_ulogic; -- free entry available?
126
    clear : std_ulogic; -- clear all entries
127 20 zero_gravi
    --
128 31 zero_gravi
    rdata : std_ulogic_vector(2+31 downto 0); -- read data: status (bus_error, align_error) + 32-bit instruction data
129
    re    : std_ulogic; -- read enable
130
    avail : std_ulogic; -- data available?
131 20 zero_gravi
    --
132 31 zero_gravi
    w_pnt : std_ulogic_vector(index_size_f(ipb_entries_c) downto 0); -- write pointer
133
    r_pnt : std_ulogic_vector(index_size_f(ipb_entries_c) downto 0); -- read pointer
134
    empty : std_ulogic;
135
    full  : std_ulogic;
136 20 zero_gravi
    --
137 31 zero_gravi
    data  : ipb_data_fifo_t; -- fifo memory
138 6 zero_gravi
  end record;
139
  signal ipb : ipb_t;
140 2 zero_gravi
 
141 31 zero_gravi
  -- pre-decoder --
142
  signal ci_instr16 : std_ulogic_vector(15 downto 0);
143
  signal ci_instr32 : std_ulogic_vector(31 downto 0);
144
  signal ci_illegal : std_ulogic;
145
 
146
  -- instruction issue enginge --
147
  type issue_engine_state_t is (ISSUE_ACTIVE, ISSUE_REALIGN);
148
  type issue_engine_t is record
149
    state     : issue_engine_state_t;
150
    state_nxt : issue_engine_state_t;
151
    align     : std_ulogic;
152
    align_nxt : std_ulogic;
153
    buf       : std_ulogic_vector(2+15 downto 0);
154
    buf_nxt   : std_ulogic_vector(2+15 downto 0);
155
  end record;
156
  signal issue_engine : issue_engine_t;
157
 
158 32 zero_gravi
  -- instruction buffer ("FIFO" with just one entry) --
159 31 zero_gravi
  type i_buf_t is record
160
    wdata  : std_ulogic_vector(35 downto 0); -- 4-bit status + 32-bit instruction
161
    rdata  : std_ulogic_vector(35 downto 0); -- 4-bit status + 32-bit instruction
162
    status : std_ulogic;
163
    clear  : std_ulogic;
164
    we     : std_ulogic;
165
    re     : std_ulogic;
166
    free   : std_ulogic;
167
    avail  : std_ulogic;
168
  end record;
169
  signal i_buf : i_buf_t;
170
 
171 6 zero_gravi
  -- instruction execution engine --
172 12 zero_gravi
  type execute_engine_state_t is (SYS_WAIT, DISPATCH, TRAP, EXECUTE, ALU_WAIT, BRANCH, LOADSTORE_0, LOADSTORE_1, LOADSTORE_2, CSR_ACCESS);
173 6 zero_gravi
  type execute_engine_t is record
174
    state        : execute_engine_state_t;
175 19 zero_gravi
    state_prev   : execute_engine_state_t;
176 6 zero_gravi
    state_nxt    : execute_engine_state_t;
177
    i_reg        : std_ulogic_vector(31 downto 0);
178
    i_reg_nxt    : std_ulogic_vector(31 downto 0);
179 33 zero_gravi
    i_reg_last   : std_ulogic_vector(31 downto 0); -- last executed instruction
180 6 zero_gravi
    is_ci        : std_ulogic; -- current instruction is de-compressed instruction
181
    is_ci_nxt    : std_ulogic;
182
    is_jump      : std_ulogic; -- current instruction is jump instruction
183
    is_jump_nxt  : std_ulogic;
184 29 zero_gravi
    is_cp_op     : std_ulogic; -- current instruction is a co-processor operation
185
    is_cp_op_nxt : std_ulogic;
186 6 zero_gravi
    branch_taken : std_ulogic; -- branch condition fullfilled
187
    pc           : std_ulogic_vector(data_width_c-1 downto 0); -- actual PC, corresponding to current executed instruction
188
    pc_nxt       : std_ulogic_vector(data_width_c-1 downto 0);
189
    next_pc      : std_ulogic_vector(data_width_c-1 downto 0); -- next PC, corresponding to next instruction to be executed
190
    last_pc      : std_ulogic_vector(data_width_c-1 downto 0); -- PC of last executed instruction
191 27 zero_gravi
    last_pc_nxt  : std_ulogic_vector(data_width_c-1 downto 0); -- PC of last executed instruction
192 11 zero_gravi
    sleep        : std_ulogic; -- CPU in sleep mode
193
    sleep_nxt    : std_ulogic; -- CPU in sleep mode
194 20 zero_gravi
    if_rst       : std_ulogic; -- instruction fetch was reset
195
    if_rst_nxt   : std_ulogic; -- instruction fetch was reset
196 6 zero_gravi
  end record;
197
  signal execute_engine : execute_engine_t;
198 2 zero_gravi
 
199 12 zero_gravi
  signal next_pc_tmp : std_ulogic_vector(data_width_c-1 downto 0);
200
 
201 6 zero_gravi
  -- trap controller --
202
  type trap_ctrl_t is record
203
    exc_buf       : std_ulogic_vector(exception_width_c-1 downto 0);
204
    exc_fire      : std_ulogic; -- set if there is a valid source in the exception buffer
205
    irq_buf       : std_ulogic_vector(interrupt_width_c-1 downto 0);
206
    irq_fire      : std_ulogic; -- set if there is a valid source in the interrupt buffer
207
    exc_ack       : std_ulogic; -- acknowledge all exceptions
208
    irq_ack       : std_ulogic_vector(interrupt_width_c-1 downto 0); -- acknowledge specific interrupt
209
    irq_ack_nxt   : std_ulogic_vector(interrupt_width_c-1 downto 0);
210 14 zero_gravi
    cause         : std_ulogic_vector(5 downto 0); -- trap ID (for "mcause"), only for hw
211
    cause_nxt     : std_ulogic_vector(5 downto 0);
212 6 zero_gravi
    --
213
    env_start     : std_ulogic; -- start trap handler env
214
    env_start_ack : std_ulogic; -- start of trap handler acknowledged
215
    env_end       : std_ulogic; -- end trap handler env
216
    --
217
    instr_be      : std_ulogic; -- instruction fetch bus error
218
    instr_ma      : std_ulogic; -- instruction fetch misaligned address
219
    instr_il      : std_ulogic; -- illegal instruction
220
    env_call      : std_ulogic;
221
    break_point   : std_ulogic;
222
  end record;
223
  signal trap_ctrl : trap_ctrl_t;
224
 
225
  -- CPU control signals --
226
  signal ctrl_nxt, ctrl : std_ulogic_vector(ctrl_width_c-1 downto 0);
227 2 zero_gravi
 
228 6 zero_gravi
  -- fast bus access --
229
  signal bus_fast_ir : std_ulogic;
230 2 zero_gravi
 
231 6 zero_gravi
  -- RISC-V control and status registers (CSRs) --
232 15 zero_gravi
  type pmp_ctrl_t is array (0 to PMP_NUM_REGIONS-1) of std_ulogic_vector(7 downto 0);
233
  type pmp_addr_t is array (0 to PMP_NUM_REGIONS-1) of std_ulogic_vector(data_width_c-1 downto 0);
234 6 zero_gravi
  type csr_t is record
235 29 zero_gravi
    we           : std_ulogic; -- csr write enable
236 6 zero_gravi
    we_nxt       : std_ulogic;
237 29 zero_gravi
    re           : std_ulogic; -- csr read enable
238 6 zero_gravi
    re_nxt       : std_ulogic;
239 29 zero_gravi
    wdata        : std_ulogic_vector(data_width_c-1 downto 0); -- csr write data
240
    rdata        : std_ulogic_vector(data_width_c-1 downto 0); -- csr read data
241
    --
242 6 zero_gravi
    mstatus_mie  : std_ulogic; -- mstatus.MIE: global IRQ enable (R/W)
243
    mstatus_mpie : std_ulogic; -- mstatus.MPIE: previous global IRQ enable (R/-)
244 29 zero_gravi
    mstatus_mpp  : std_ulogic_vector(1 downto 0); -- mstatus.MPP: machine previous privilege mode
245
    --
246 6 zero_gravi
    mie_msie     : std_ulogic; -- mie.MSIE: machine software interrupt enable (R/W)
247
    mie_meie     : std_ulogic; -- mie.MEIE: machine external interrupt enable (R/W)
248 14 zero_gravi
    mie_mtie     : std_ulogic; -- mie.MEIE: machine timer interrupt enable (R/W
249
    mie_firqe    : std_ulogic_vector(3 downto 0); -- mie.firq*e: fast interrupt enabled (R/W)
250 29 zero_gravi
    --
251 15 zero_gravi
    privilege    : std_ulogic_vector(1 downto 0); -- hart's current previous privilege mode
252 29 zero_gravi
    --
253 6 zero_gravi
    mepc         : std_ulogic_vector(data_width_c-1 downto 0); -- mepc: machine exception pc (R/W)
254 14 zero_gravi
    mcause       : std_ulogic_vector(data_width_c-1 downto 0); -- mcause: machine trap cause (R/-)
255 12 zero_gravi
    mtvec        : std_ulogic_vector(data_width_c-1 downto 0); -- mtvec: machine trap-handler base address (R/W), bit 1:0 == 00
256 11 zero_gravi
    mtval        : std_ulogic_vector(data_width_c-1 downto 0); -- mtval: machine bad address or isntruction (R/W)
257 6 zero_gravi
    mscratch     : std_ulogic_vector(data_width_c-1 downto 0); -- mscratch: scratch register (R/W)
258 11 zero_gravi
    mcycle       : std_ulogic_vector(32 downto 0); -- mcycle (R/W), plus carry bit
259
    minstret     : std_ulogic_vector(32 downto 0); -- minstret (R/W), plus carry bit
260 27 zero_gravi
    mcycleh      : std_ulogic_vector(31 downto 0); -- mcycleh (R/W) - REDUCED BIT-WIDTH!
261
    minstreth    : std_ulogic_vector(31 downto 0); -- minstreth (R/W) - REDUCED BIT-WIDTH!
262 15 zero_gravi
    pmpcfg       : pmp_ctrl_t; -- physical memory protection - configuration registers
263
    pmpaddr      : pmp_addr_t; -- physical memory protection - address registers
264 6 zero_gravi
  end record;
265
  signal csr : csr_t;
266 2 zero_gravi
 
267 11 zero_gravi
  signal mcycle_msb   : std_ulogic;
268
  signal minstret_msb : std_ulogic;
269 2 zero_gravi
 
270 6 zero_gravi
  -- illegal instruction check --
271 2 zero_gravi
  signal illegal_instruction : std_ulogic;
272
  signal illegal_register    : std_ulogic; -- only for E-extension
273
  signal illegal_compressed  : std_ulogic; -- only fir C-extension
274
 
275 15 zero_gravi
  -- access (privilege) check --
276
  signal csr_acc_valid : std_ulogic; -- valid CSR access (implemented and valid access rights)
277
 
278 2 zero_gravi
begin
279
 
280 6 zero_gravi
-- ****************************************************************************************************************************
281 31 zero_gravi
-- Instruction Fetch (always fetches aligned 32-bit chunks of data)
282 6 zero_gravi
-- ****************************************************************************************************************************
283
 
284
  -- Fetch Engine FSM Sync ------------------------------------------------------------------
285
  -- -------------------------------------------------------------------------------------------
286 31 zero_gravi
  fetch_engine_fsm_sync: process(rstn_i, clk_i)
287 6 zero_gravi
  begin
288
    if (rstn_i = '0') then
289
      fetch_engine.state <= IFETCH_RESET;
290 31 zero_gravi
      fetch_engine.pc    <= (others => '0');
291 6 zero_gravi
    elsif rising_edge(clk_i) then
292
      if (fetch_engine.reset = '1') then
293
        fetch_engine.state <= IFETCH_RESET;
294
      else
295
        fetch_engine.state <= fetch_engine.state_nxt;
296
      end if;
297 31 zero_gravi
      fetch_engine.pc <= fetch_engine.pc_nxt;
298 6 zero_gravi
    end if;
299
  end process fetch_engine_fsm_sync;
300
 
301 12 zero_gravi
  -- PC output --
302 31 zero_gravi
  fetch_pc_o <= fetch_engine.pc(data_width_c-1 downto 1) & '0'; -- half-word aligned
303 6 zero_gravi
 
304 12 zero_gravi
 
305 6 zero_gravi
  -- Fetch Engine FSM Comb ------------------------------------------------------------------
306
  -- -------------------------------------------------------------------------------------------
307 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)
308 6 zero_gravi
  begin
309
    -- arbiter defaults --
310 31 zero_gravi
    bus_fast_ir              <= '0';
311
    fetch_engine.state_nxt   <= fetch_engine.state;
312
    fetch_engine.pc_nxt      <= fetch_engine.pc;
313
    fetch_engine.bus_err_ack <= '0';
314 6 zero_gravi
 
315
    -- instruction prefetch buffer interface --
316
    ipb.we    <= '0';
317 31 zero_gravi
    ipb.wdata <= be_instr_i & ma_instr_i & instr_i(31 downto 0); -- store exception info and instruction word
318 6 zero_gravi
    ipb.clear <= '0';
319
 
320
    -- state machine --
321
    case fetch_engine.state is
322
 
323 31 zero_gravi
      when IFETCH_RESET => -- reset engine and prefetch buffer, get appilcation PC
324 6 zero_gravi
      -- ------------------------------------------------------------
325 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
326
        fetch_engine.pc_nxt      <= execute_engine.pc(data_width_c-1 downto 1) & '0'; -- initialize with "real" application PC
327
        ipb.clear                <= '1'; -- clear prefetch buffer
328
        fetch_engine.state_nxt   <= IFETCH_REQUEST;
329 6 zero_gravi
 
330 31 zero_gravi
      when IFETCH_REQUEST => -- output current PC to bus system and request 32-bit (aligned!) instruction data
331 6 zero_gravi
      -- ------------------------------------------------------------
332 31 zero_gravi
        if (ipb.free = '1') then -- free entry in buffer?
333
          bus_fast_ir            <= '1'; -- fast instruction fetch request
334
          fetch_engine.state_nxt <= IFETCH_ISSUE;
335
        end if;
336 6 zero_gravi
 
337 31 zero_gravi
      when IFETCH_ISSUE => -- store instruction data to prefetch buffer
338 6 zero_gravi
      -- ------------------------------------------------------------
339 12 zero_gravi
        if (bus_i_wait_i = '0') or (be_instr_i = '1') or (ma_instr_i = '1') then -- wait for bus response
340 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
341
          fetch_engine.pc_nxt      <= std_ulogic_vector(unsigned(fetch_engine.pc) + 4);
342
          ipb.we                   <= '1';
343
          fetch_engine.state_nxt   <= IFETCH_REQUEST;
344 6 zero_gravi
        end if;
345 11 zero_gravi
 
346 6 zero_gravi
      when others => -- undefined
347
      -- ------------------------------------------------------------
348
        fetch_engine.state_nxt <= IFETCH_RESET;
349
 
350
    end case;
351
  end process fetch_engine_fsm_comb;
352
 
353
 
354
-- ****************************************************************************************************************************
355
-- Instruction Prefetch Buffer
356
-- ****************************************************************************************************************************
357
 
358
 
359 20 zero_gravi
  -- Instruction Prefetch Buffer (FIFO) -----------------------------------------------------
360 6 zero_gravi
  -- -------------------------------------------------------------------------------------------
361 20 zero_gravi
  instr_prefetch_buffer_ctrl: process(rstn_i, clk_i)
362 6 zero_gravi
  begin
363
    if (rstn_i = '0') then
364 20 zero_gravi
      ipb.w_pnt <= (others => '0');
365
      ipb.r_pnt <= (others => '0');
366 6 zero_gravi
    elsif rising_edge(clk_i) then
367 20 zero_gravi
      -- write port --
368 6 zero_gravi
      if (ipb.clear = '1') then
369 20 zero_gravi
        ipb.w_pnt <= (others => '0');
370 6 zero_gravi
      elsif (ipb.we = '1') then
371 20 zero_gravi
        ipb.w_pnt <= std_ulogic_vector(unsigned(ipb.w_pnt) + 1);
372
      end if;
373 31 zero_gravi
      -- read ports --
374 20 zero_gravi
      if (ipb.clear = '1') then
375
        ipb.r_pnt <= (others => '0');
376 6 zero_gravi
      elsif (ipb.re = '1') then
377 20 zero_gravi
        ipb.r_pnt <= std_ulogic_vector(unsigned(ipb.r_pnt) + 1);
378 6 zero_gravi
      end if;
379 20 zero_gravi
    end if;
380
  end process instr_prefetch_buffer_ctrl;
381
 
382
  instr_prefetch_buffer_data: process(clk_i)
383
  begin
384
    if rising_edge(clk_i) then
385
      if (ipb.we = '1') then -- write port
386
        ipb.data(to_integer(unsigned(ipb.w_pnt(ipb.w_pnt'left-1 downto 0)))) <= ipb.wdata;
387 6 zero_gravi
      end if;
388
    end if;
389 20 zero_gravi
  end process instr_prefetch_buffer_data;
390 6 zero_gravi
 
391 20 zero_gravi
  -- async read --
392 31 zero_gravi
  ipb.rdata <= ipb.data(to_integer(unsigned(ipb.r_pnt(ipb.r_pnt'left-1 downto 0))));
393 20 zero_gravi
 
394 6 zero_gravi
  -- status --
395 20 zero_gravi
  ipb.full  <= '1' when (ipb.r_pnt(ipb.r_pnt'left) /= ipb.w_pnt(ipb.w_pnt'left)) and (ipb.r_pnt(ipb.r_pnt'left-1 downto 0) = ipb.w_pnt(ipb.w_pnt'left-1 downto 0)) else '0';
396
  ipb.empty <= '1' when (ipb.r_pnt(ipb.r_pnt'left)  = ipb.w_pnt(ipb.w_pnt'left)) and (ipb.r_pnt(ipb.r_pnt'left-1 downto 0) = ipb.w_pnt(ipb.w_pnt'left-1 downto 0)) else '0';
397 31 zero_gravi
 
398 20 zero_gravi
  ipb.free  <= not ipb.full;
399
  ipb.avail <= not ipb.empty;
400 6 zero_gravi
 
401
 
402
-- ****************************************************************************************************************************
403 31 zero_gravi
-- Instruction Issue (recoding of compressed instructions and 32-bit instruction word construction)
404
-- ****************************************************************************************************************************
405
 
406
 
407
  -- Issue Engine FSM Sync ------------------------------------------------------------------
408
  -- -------------------------------------------------------------------------------------------
409
  issue_engine_fsm_sync: process(rstn_i, clk_i)
410
  begin
411
    if (rstn_i = '0') then
412
      issue_engine.state <= ISSUE_ACTIVE;
413
      issue_engine.align <= CPU_BOOT_ADDR(1);
414
      issue_engine.buf   <= (others => '0');
415
    elsif rising_edge(clk_i) then
416
      if (ipb.clear = '1') then
417
        if (CPU_EXTENSION_RISCV_C = true) then
418
          if (execute_engine.pc(1) = '1') then -- branch to unaligned address?
419
            issue_engine.state <= ISSUE_REALIGN;
420
            issue_engine.align <= '1'; -- aligned on 16-bit boundary
421
          else
422
            issue_engine.state <= issue_engine.state_nxt;
423
            issue_engine.align <= '0'; -- aligned on 32-bit boundary
424
          end if;
425
        else
426
          issue_engine.state <= issue_engine.state_nxt;
427
          issue_engine.align <= '0'; -- always aligned on 32-bit boundaries
428
        end if;
429
      else
430
        issue_engine.state <= issue_engine.state_nxt;
431
        issue_engine.align <= issue_engine.align_nxt;
432
      end if;
433
      issue_engine.buf <= issue_engine.buf_nxt;
434
    end if;
435
  end process issue_engine_fsm_sync;
436
 
437
 
438
  -- Issue Engine FSM Comb ------------------------------------------------------------------
439
  -- -------------------------------------------------------------------------------------------
440
  issue_engine_fsm_comb: process(issue_engine, ipb, i_buf, execute_engine, ci_illegal, ci_instr32)
441
  begin
442
    -- arbiter defaults --
443
    issue_engine.state_nxt <= issue_engine.state;
444
    issue_engine.align_nxt <= issue_engine.align;
445
    issue_engine.buf_nxt   <= issue_engine.buf;
446
 
447
    -- instruction prefetch buffer interface defaults --
448
    ipb.re <= '0';
449
 
450
    -- instruction buffer interface defaults --
451
    i_buf.we    <= '0';
452
    i_buf.wdata <= '0' & ipb.rdata(33 downto 32) & '0' & ipb.rdata(31 downto 0);
453
 
454
    -- state machine --
455
    case issue_engine.state is
456
 
457
      when ISSUE_ACTIVE => -- issue instruction if available
458
      -- ------------------------------------------------------------
459
        if (ipb.avail = '1') then -- instructions available?
460
 
461
          if (issue_engine.align = '0') or (CPU_EXTENSION_RISCV_C = false) then -- begin check in LOW instruction half-word
462
            if (i_buf.free = '1') then
463
              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
464
              if (ipb.rdata(1 downto 0) = "11") or (CPU_EXTENSION_RISCV_C = false) then -- uncompressed and "aligned"
465
                ipb.re      <= '1';
466
                i_buf.wdata <= '0' & ipb.rdata(33 downto 32) & '0' & ipb.rdata(31 downto 0);
467
                i_buf.we    <= '1';
468
              else -- compressed
469
                ipb.re      <= '1';
470
                i_buf.wdata <= ci_illegal & ipb.rdata(33 downto 32) & '1' & ci_instr32;
471
                i_buf.we    <= '1';
472
                issue_engine.align_nxt <= '1';
473
              end if;
474
            end if;
475
 
476
          else -- begin check in HIGH instruction half-word
477
            if (i_buf.free = '1') then
478
              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
479
              if (issue_engine.buf(1 downto 0) = "11") then -- uncompressed and "unaligned"
480
                ipb.re      <= '1';
481
                i_buf.wdata <= '0' & issue_engine.buf(17 downto 16) & '0' & (ipb.rdata(15 downto 0) & issue_engine.buf(15 downto 0));
482
                i_buf.we    <= '1';
483
              else -- compressed
484
                --ipb.re      <= '1';
485
                i_buf.wdata <= ci_illegal & ipb.rdata(33 downto 32) & '1' & ci_instr32;
486
                i_buf.we    <= '1';
487
                issue_engine.align_nxt <= '0';
488
              end if;
489
            end if;
490
          end if;
491
        end if;
492
 
493
      when ISSUE_REALIGN => -- re-align input fifos after a branch to an unaligned address
494
      -- ------------------------------------------------------------
495
        issue_engine.buf_nxt <= ipb.rdata(33 downto 32) & ipb.rdata(31 downto 16);
496
        if (ipb.avail = '1') then -- instructions available?
497
          ipb.re <= '1';
498
          issue_engine.state_nxt <= ISSUE_ACTIVE;
499
        end if;
500
 
501
      when others => -- undefined
502
      -- ------------------------------------------------------------
503
        issue_engine.state_nxt <= ISSUE_ACTIVE;
504
 
505
    end case;
506
  end process issue_engine_fsm_comb;
507
 
508
  -- 16-bit instruction: half-word select --
509
  ci_instr16 <= ipb.rdata(15 downto 0) when (issue_engine.align = '0') else issue_engine.buf(15 downto 0);
510
 
511
 
512
  -- Compressed Instructions Recoding -------------------------------------------------------
513
  -- -------------------------------------------------------------------------------------------
514
  neorv32_cpu_decompressor_inst_true:
515
  if (CPU_EXTENSION_RISCV_C = true) generate
516
    neorv32_cpu_decompressor_inst: neorv32_cpu_decompressor
517
    port map (
518
      -- instruction input --
519
      ci_instr16_i => ci_instr16, -- compressed instruction input
520
      -- instruction output --
521
      ci_illegal_o => ci_illegal, -- is an illegal compressed instruction
522
      ci_instr32_o => ci_instr32  -- 32-bit decompressed instruction
523
    );
524
  end generate;
525
 
526
  neorv32_cpu_decompressor_inst_false:
527
  if (CPU_EXTENSION_RISCV_C = false) generate
528
    ci_instr32 <= (others => '0');
529
    ci_illegal <= '0';
530
  end generate;
531
 
532
 
533
  -- Instruction Buffer ---------------------------------------------------------------------
534
  -- -------------------------------------------------------------------------------------------
535
  instruction_buffer_ctrl: process(rstn_i, clk_i)
536
  begin
537
    if (rstn_i = '0') then
538
      i_buf.status <= '0';
539
    elsif rising_edge(clk_i) then
540
      if (i_buf.clear = '1') then
541
        i_buf.status <= '0';
542
      elsif (i_buf.we = '1') then
543
        i_buf.status <= '1';
544
      elsif (i_buf.re = '1') then
545
        i_buf.status <= '0';
546
      end if;
547
    end if;
548
  end process instruction_buffer_ctrl;
549
 
550
  instruction_buffer_data: process(clk_i)
551
  begin
552
    if rising_edge(clk_i) then
553
      if (i_buf.we = '1') and (ipb.clear = '0') then
554
        i_buf.rdata <= i_buf.wdata;
555
      end if;
556
    end if;
557
  end process instruction_buffer_data;
558
 
559
  -- status --
560
  i_buf.free  <= not i_buf.status;
561
  i_buf.avail <= i_buf.status;
562
 
563
  -- clear i_buf when clearing ipb --
564
  i_buf.clear <= ipb.clear;
565
 
566
 
567
-- ****************************************************************************************************************************
568 6 zero_gravi
-- Instruction Execution
569
-- ****************************************************************************************************************************
570
 
571
 
572 2 zero_gravi
  -- Immediate Generator --------------------------------------------------------------------
573
  -- -------------------------------------------------------------------------------------------
574
  imm_gen: process(clk_i)
575
  begin
576
    if rising_edge(clk_i) then
577 6 zero_gravi
      case execute_engine.i_reg(instr_opcode_msb_c downto instr_opcode_lsb_c) is
578 2 zero_gravi
        when opcode_store_c => -- S-immediate
579 6 zero_gravi
          imm_o(31 downto 11) <= (others => execute_engine.i_reg(31)); -- sign extension
580
          imm_o(10 downto 05) <= execute_engine.i_reg(30 downto 25);
581
          imm_o(04 downto 01) <= execute_engine.i_reg(11 downto 08);
582
          imm_o(00)           <= execute_engine.i_reg(07);
583 2 zero_gravi
        when opcode_branch_c => -- B-immediate
584 6 zero_gravi
          imm_o(31 downto 12) <= (others => execute_engine.i_reg(31)); -- sign extension
585
          imm_o(11)           <= execute_engine.i_reg(07);
586
          imm_o(10 downto 05) <= execute_engine.i_reg(30 downto 25);
587
          imm_o(04 downto 01) <= execute_engine.i_reg(11 downto 08);
588
          imm_o(00)           <= '0';
589 2 zero_gravi
        when opcode_lui_c | opcode_auipc_c => -- U-immediate
590 6 zero_gravi
          imm_o(31 downto 20) <= execute_engine.i_reg(31 downto 20);
591
          imm_o(19 downto 12) <= execute_engine.i_reg(19 downto 12);
592
          imm_o(11 downto 00) <= (others => '0');
593 2 zero_gravi
        when opcode_jal_c => -- J-immediate
594 6 zero_gravi
          imm_o(31 downto 20) <= (others => execute_engine.i_reg(31)); -- sign extension
595
          imm_o(19 downto 12) <= execute_engine.i_reg(19 downto 12);
596
          imm_o(11)           <= execute_engine.i_reg(20);
597
          imm_o(10 downto 05) <= execute_engine.i_reg(30 downto 25);
598
          imm_o(04 downto 01) <= execute_engine.i_reg(24 downto 21);
599
          imm_o(00)           <= '0';
600 2 zero_gravi
        when opcode_syscsr_c => -- CSR-immediate
601 6 zero_gravi
          imm_o(31 downto 05) <= (others => '0');
602
          imm_o(04 downto 00) <= execute_engine.i_reg(19 downto 15);
603 2 zero_gravi
        when others => -- I-immediate
604 6 zero_gravi
          imm_o(31 downto 11) <= (others => execute_engine.i_reg(31)); -- sign extension
605
          imm_o(10 downto 05) <= execute_engine.i_reg(30 downto 25);
606
          imm_o(04 downto 01) <= execute_engine.i_reg(24 downto 21);
607
          imm_o(00)           <= execute_engine.i_reg(20);
608 2 zero_gravi
      end case;
609
    end if;
610
  end process imm_gen;
611
 
612
 
613
  -- Branch Condition Check -----------------------------------------------------------------
614
  -- -------------------------------------------------------------------------------------------
615 6 zero_gravi
  branch_check: process(execute_engine.i_reg, cmp_i)
616 2 zero_gravi
  begin
617 6 zero_gravi
    case execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) is
618 2 zero_gravi
      when funct3_beq_c => -- branch if equal
619 6 zero_gravi
        execute_engine.branch_taken <= cmp_i(alu_cmp_equal_c);
620 2 zero_gravi
      when funct3_bne_c => -- branch if not equal
621 6 zero_gravi
        execute_engine.branch_taken <= not cmp_i(alu_cmp_equal_c);
622 2 zero_gravi
      when funct3_blt_c | funct3_bltu_c => -- branch if less (signed/unsigned)
623 6 zero_gravi
        execute_engine.branch_taken <= cmp_i(alu_cmp_less_c);
624 2 zero_gravi
      when funct3_bge_c | funct3_bgeu_c => -- branch if greater or equal (signed/unsigned)
625 6 zero_gravi
        execute_engine.branch_taken <= not cmp_i(alu_cmp_less_c);
626 2 zero_gravi
      when others => -- undefined
627 6 zero_gravi
        execute_engine.branch_taken <= '0';
628 2 zero_gravi
    end case;
629
  end process branch_check;
630
 
631
 
632 6 zero_gravi
  -- Execute Engine FSM Sync ----------------------------------------------------------------
633 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
634 12 zero_gravi
  -- for registers that DO require a specific reset state --
635 6 zero_gravi
  execute_engine_fsm_sync_rst: process(rstn_i, clk_i)
636 2 zero_gravi
  begin
637
    if (rstn_i = '0') then
638 12 zero_gravi
      execute_engine.pc      <= CPU_BOOT_ADDR(data_width_c-1 downto 1) & '0';
639
      execute_engine.last_pc <= CPU_BOOT_ADDR(data_width_c-1 downto 1) & '0';
640
      execute_engine.state   <= SYS_WAIT;
641 13 zero_gravi
      execute_engine.sleep   <= '0';
642 23 zero_gravi
      execute_engine.if_rst  <= '1'; -- instruction fetch is reset after system reset
643 2 zero_gravi
    elsif rising_edge(clk_i) then
644 27 zero_gravi
      execute_engine.pc      <= execute_engine.pc_nxt(data_width_c-1 downto 1) & '0';
645
      execute_engine.last_pc <= execute_engine.last_pc_nxt;
646
      execute_engine.state   <= execute_engine.state_nxt;
647
      execute_engine.sleep   <= execute_engine.sleep_nxt;
648
      execute_engine.if_rst  <= execute_engine.if_rst_nxt;
649 2 zero_gravi
    end if;
650 6 zero_gravi
  end process execute_engine_fsm_sync_rst;
651 2 zero_gravi
 
652 6 zero_gravi
 
653 12 zero_gravi
  -- for registers that do NOT require a specific reset state --
654 6 zero_gravi
  execute_engine_fsm_sync: process(clk_i)
655 2 zero_gravi
  begin
656
    if rising_edge(clk_i) then
657 19 zero_gravi
      execute_engine.state_prev <= execute_engine.state;
658
      execute_engine.i_reg      <= execute_engine.i_reg_nxt;
659
      execute_engine.is_ci      <= execute_engine.is_ci_nxt;
660
      execute_engine.is_jump    <= execute_engine.is_jump_nxt;
661 29 zero_gravi
      execute_engine.is_cp_op   <= execute_engine.is_cp_op_nxt;
662 19 zero_gravi
      --
663 33 zero_gravi
      if (execute_engine.state = EXECUTE) then
664
        execute_engine.i_reg_last <= execute_engine.i_reg;
665
      end if;
666
      --
667 6 zero_gravi
      ctrl <= ctrl_nxt;
668 2 zero_gravi
    end if;
669 6 zero_gravi
  end process execute_engine_fsm_sync;
670 2 zero_gravi
 
671 20 zero_gravi
  -- next PC --
672
  next_pc_tmp <= std_ulogic_vector(unsigned(execute_engine.pc) + 2) when (execute_engine.is_ci = '1') else std_ulogic_vector(unsigned(execute_engine.pc) + 4);
673 12 zero_gravi
  execute_engine.next_pc <= next_pc_tmp(data_width_c-1 downto 1) & '0';
674 6 zero_gravi
 
675 20 zero_gravi
  -- PC output --
676
  curr_pc_o <= execute_engine.pc(data_width_c-1 downto 1) & '0';
677
  next_pc_o <= next_pc_tmp(data_width_c-1 downto 1) & '0';
678 6 zero_gravi
 
679 20 zero_gravi
 
680 6 zero_gravi
  -- CPU Control Bus Output -----------------------------------------------------------------
681
  -- -------------------------------------------------------------------------------------------
682 29 zero_gravi
  ctrl_output: process(ctrl, fetch_engine, trap_ctrl, bus_fast_ir, execute_engine)
683 2 zero_gravi
  begin
684
    ctrl_o <= ctrl;
685 12 zero_gravi
    -- fast bus access requests --
686 6 zero_gravi
    ctrl_o(ctrl_bus_if_c) <= ctrl(ctrl_bus_if_c) or bus_fast_ir;
687 12 zero_gravi
    -- bus error control --
688
    ctrl_o(ctrl_bus_ierr_ack_c) <= fetch_engine.bus_err_ack;
689
    ctrl_o(ctrl_bus_derr_ack_c) <= trap_ctrl.env_start_ack;
690 29 zero_gravi
    -- co-processor operation --
691
    ctrl_o(ctrl_cp_cmd2_c downto ctrl_cp_cmd0_c) <= execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c);
692 6 zero_gravi
  end process ctrl_output;
693 2 zero_gravi
 
694
 
695 6 zero_gravi
  -- Execute Engine FSM Comb ----------------------------------------------------------------
696
  -- -------------------------------------------------------------------------------------------
697 31 zero_gravi
  execute_engine_fsm_comb: process(execute_engine, fetch_engine, i_buf, trap_ctrl, csr, ctrl, csr_acc_valid,
698 29 zero_gravi
                                   alu_res_i, alu_wait_i, bus_d_wait_i, ma_load_i, be_load_i, ma_store_i, be_store_i)
699 2 zero_gravi
    variable alu_immediate_v : std_ulogic;
700
    variable rs1_is_r0_v     : std_ulogic;
701
  begin
702
    -- arbiter defaults --
703 29 zero_gravi
    execute_engine.state_nxt    <= execute_engine.state;
704
    execute_engine.i_reg_nxt    <= execute_engine.i_reg;
705
    execute_engine.is_jump_nxt  <= '0';
706
    execute_engine.is_cp_op_nxt <= execute_engine.is_cp_op;
707
    execute_engine.is_ci_nxt    <= execute_engine.is_ci;
708
    execute_engine.pc_nxt       <= execute_engine.pc;
709
    execute_engine.last_pc_nxt  <= execute_engine.last_pc;
710
    execute_engine.sleep_nxt    <= execute_engine.sleep;
711
    execute_engine.if_rst_nxt   <= execute_engine.if_rst;
712 2 zero_gravi
 
713 6 zero_gravi
    -- instruction dispatch --
714
    fetch_engine.reset         <= '0';
715 31 zero_gravi
    i_buf.re                   <= '0';
716 2 zero_gravi
 
717 6 zero_gravi
    -- trap environment control --
718
    trap_ctrl.env_start_ack    <= '0';
719
    trap_ctrl.env_end          <= '0';
720
 
721 2 zero_gravi
    -- exception trigger --
722 6 zero_gravi
    trap_ctrl.instr_be         <= '0';
723
    trap_ctrl.instr_ma         <= '0';
724
    trap_ctrl.env_call         <= '0';
725
    trap_ctrl.break_point      <= '0';
726 13 zero_gravi
    illegal_compressed         <= '0';
727 2 zero_gravi
 
728 6 zero_gravi
    -- CSR access --
729
    csr.we_nxt                 <= '0';
730
    csr.re_nxt                 <= '0';
731
 
732 2 zero_gravi
    -- control defaults --
733
    ctrl_nxt <= (others => '0'); -- all off at first
734 6 zero_gravi
    if (execute_engine.i_reg(instr_opcode_lsb_c+4) = '1') then -- ALU ops
735
      ctrl_nxt(ctrl_alu_unsigned_c) <= execute_engine.i_reg(instr_funct3_lsb_c+0); -- unsigned ALU operation (SLTIU, SLTU)
736 2 zero_gravi
    else -- branches
737 6 zero_gravi
      ctrl_nxt(ctrl_alu_unsigned_c) <= execute_engine.i_reg(instr_funct3_lsb_c+1); -- unsigned branches (BLTU, BGEU)
738 2 zero_gravi
    end if;
739 27 zero_gravi
    ctrl_nxt(ctrl_bus_unsigned_c)  <= execute_engine.i_reg(instr_funct3_msb_c); -- unsigned LOAD (LBU, LHU)
740
    ctrl_nxt(ctrl_alu_shift_dir_c) <= execute_engine.i_reg(instr_funct3_msb_c); -- shift direction (left/right)
741
    ctrl_nxt(ctrl_alu_shift_ar_c)  <= execute_engine.i_reg(30); -- is arithmetic shift
742 29 zero_gravi
    ctrl_nxt(ctrl_alu_cmd2_c     downto ctrl_alu_cmd0_c)     <= alu_cmd_addsub_c; -- default ALU operation: ADD(I)
743 27 zero_gravi
    ctrl_nxt(ctrl_cp_id_msb_c    downto ctrl_cp_id_lsb_c)    <= cp_sel_muldiv_c; -- only CP0 (=MULDIV) implemented yet
744
    ctrl_nxt(ctrl_rf_rd_adr4_c   downto ctrl_rf_rd_adr0_c)   <= ctrl(ctrl_rf_rd_adr4_c  downto ctrl_rf_rd_adr0_c); -- keep rd addr
745
    ctrl_nxt(ctrl_rf_rs1_adr4_c  downto ctrl_rf_rs1_adr0_c)  <= ctrl(ctrl_rf_rs1_adr4_c downto ctrl_rf_rs1_adr0_c); -- keep rs1 addr
746
    ctrl_nxt(ctrl_rf_rs2_adr4_c  downto ctrl_rf_rs2_adr0_c)  <= ctrl(ctrl_rf_rs2_adr4_c downto ctrl_rf_rs2_adr0_c); -- keep rs2 addr
747
    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
748 2 zero_gravi
 
749 26 zero_gravi
    -- is immediate ALU operation? --
750
    alu_immediate_v := not execute_engine.i_reg(instr_opcode_msb_c-1);
751 2 zero_gravi
 
752 26 zero_gravi
    -- is rs1 == r0? --
753
    rs1_is_r0_v := not or_all_f(execute_engine.i_reg(instr_rs1_msb_c downto instr_rs1_lsb_c));
754 2 zero_gravi
 
755 26 zero_gravi
 
756 6 zero_gravi
    -- state machine --
757
    case execute_engine.state is
758 2 zero_gravi
 
759 25 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))
760 2 zero_gravi
      -- ------------------------------------------------------------
761 26 zero_gravi
        -- set reg_file's r0 to zero --
762 25 zero_gravi
        if (rf_r0_is_reg_c = true) then -- is r0 implemented as physical register, which has to be set to zero?
763
          ctrl_nxt(ctrl_rf_rd_adr4_c downto ctrl_rf_rd_adr0_c) <= (others => '0'); -- rd addr = r0
764 26 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 request)
765 25 zero_gravi
          ctrl_nxt(ctrl_rf_r0_we_c) <= '1'; -- allow write access to r0
766
          ctrl_nxt(ctrl_rf_wb_en_c) <= '1'; -- valid RF write-back
767
        end if;
768
        --
769 6 zero_gravi
        execute_engine.state_nxt <= DISPATCH;
770 2 zero_gravi
 
771 31 zero_gravi
      when DISPATCH => -- Get new command from instruction buffer (I_BUF)
772 25 zero_gravi
      -- ------------------------------------------------------------
773 31 zero_gravi
        ctrl_nxt(ctrl_rf_rd_adr4_c  downto ctrl_rf_rd_adr0_c)  <= i_buf.rdata(instr_rd_msb_c  downto instr_rd_lsb_c); -- rd addr
774
        ctrl_nxt(ctrl_rf_rs1_adr4_c downto ctrl_rf_rs1_adr0_c) <= i_buf.rdata(instr_rs1_msb_c downto instr_rs1_lsb_c); -- rs1 addr
775
        ctrl_nxt(ctrl_rf_rs2_adr4_c downto ctrl_rf_rs2_adr0_c) <= i_buf.rdata(instr_rs2_msb_c downto instr_rs2_lsb_c); -- rs2 addr
776 27 zero_gravi
        --
777 31 zero_gravi
        if (i_buf.avail = '1') then -- instruction available?
778
          i_buf.re <= '1';
779 25 zero_gravi
          --
780 31 zero_gravi
          execute_engine.is_ci_nxt  <= i_buf.rdata(32); -- flag to indicate this is a de-compressed instruction beeing executed
781
          execute_engine.i_reg_nxt  <= i_buf.rdata(31 downto 0);
782 27 zero_gravi
          execute_engine.if_rst_nxt <= '0';
783
          --
784 31 zero_gravi
          trap_ctrl.instr_ma <= i_buf.rdata(33); -- misaligned instruction fetch address
785
          trap_ctrl.instr_be <= i_buf.rdata(34); -- bus access fault during instrucion fetch
786
          illegal_compressed <= i_buf.rdata(35); -- invalid decompressed instruction
787 25 zero_gravi
          --
788 27 zero_gravi
          if (execute_engine.if_rst = '0') then -- if there was NO non-linear PC modification
789 21 zero_gravi
            execute_engine.pc_nxt <= execute_engine.next_pc;
790
          end if;
791
          --
792 33 zero_gravi
          -- any reason to go FAST to trap state? --
793
          if (execute_engine.sleep = '1') or (trap_ctrl.env_start = '1') or (trap_ctrl.exc_fire = '1') or ((i_buf.rdata(33) or i_buf.rdata(34)) = '1') then
794 13 zero_gravi
            execute_engine.state_nxt <= TRAP;
795
          else
796 14 zero_gravi
            execute_engine.state_nxt <= EXECUTE;
797 13 zero_gravi
          end if;
798
        end if;
799 2 zero_gravi
 
800 11 zero_gravi
      when TRAP => -- Start trap environment (also used as cpu sleep state)
801 2 zero_gravi
      -- ------------------------------------------------------------
802 20 zero_gravi
        fetch_engine.reset        <= '1';
803
        execute_engine.if_rst_nxt <= '1'; -- this is a non-linear PC modification
804 13 zero_gravi
        if (trap_ctrl.env_start = '1') then -- check here again if we came directly from DISPATCH
805 6 zero_gravi
          trap_ctrl.env_start_ack  <= '1';
806 13 zero_gravi
          execute_engine.pc_nxt    <= csr.mtvec;
807 11 zero_gravi
          execute_engine.sleep_nxt <= '0'; -- waky waky
808 7 zero_gravi
          execute_engine.state_nxt <= SYS_WAIT;
809 2 zero_gravi
        end if;
810
 
811 6 zero_gravi
      when EXECUTE => -- Decode and execute instruction
812 2 zero_gravi
      -- ------------------------------------------------------------
813 27 zero_gravi
        execute_engine.last_pc_nxt <= execute_engine.pc; -- store address of current instruction for commit
814
        --
815 6 zero_gravi
        case execute_engine.i_reg(instr_opcode_msb_c downto instr_opcode_lsb_c) is
816 2 zero_gravi
 
817 25 zero_gravi
          when opcode_alu_c | opcode_alui_c => -- (immediate) ALU operation
818 2 zero_gravi
          -- ------------------------------------------------------------
819 27 zero_gravi
            ctrl_nxt(ctrl_alu_opa_mux_c) <= '0'; -- use RS1 as ALU.OPA
820
            ctrl_nxt(ctrl_alu_opb_mux_c) <= alu_immediate_v; -- use IMM as ALU.OPB for immediate operations
821 2 zero_gravi
            ctrl_nxt(ctrl_rf_in_mux_msb_c downto ctrl_rf_in_mux_lsb_c) <= "00"; -- RF input = ALU result
822 25 zero_gravi
 
823
            -- cp access? --
824
            if (CPU_EXTENSION_RISCV_M = true) and (execute_engine.i_reg(instr_opcode_lsb_c+5) = opcode_alu_c(5)) and
825
               (execute_engine.i_reg(instr_funct7_lsb_c) = '1') then -- MULDIV?
826 29 zero_gravi
              ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_cp_c;
827
              execute_engine.is_cp_op_nxt <= '1'; -- use CP
828
            -- ALU operation --
829
            else
830
              case execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) is -- actual ALU operation (re-coding)
831
                when funct3_sll_c  => ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_shift_c;  -- SLL(I)
832
                when funct3_slt_c  => ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_slt_c;    -- SLT(I)
833
                when funct3_sltu_c => ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_slt_c;    -- SLTU(I)
834
                when funct3_xor_c  => ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_xor_c;    -- XOR(I)
835
                when funct3_sr_c   => ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_shift_c;  -- SRL(I) / SRA(I)
836
                when funct3_or_c   => ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_or_c;     -- OR(I)
837
                when funct3_and_c  => ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_and_c;    -- AND(I)
838
                when others        => ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_addsub_c; -- ADD(I) / SUB
839
              end case;
840
              execute_engine.is_cp_op_nxt <= '0'; -- no CP operation
841 25 zero_gravi
            end if;
842
 
843 29 zero_gravi
            -- ADD/SUB --
844
            if ((alu_immediate_v = '0') and (execute_engine.i_reg(instr_funct7_msb_c-1) = '1')) or -- not an immediate op and funct7.6 set => SUB
845
               (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_slt_c) or -- SLT operation
846
               (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sltu_c) then -- SLTU operation
847
              ctrl_nxt(ctrl_alu_addsub_c) <= '1'; -- SUB/SLT
848
            else
849
              ctrl_nxt(ctrl_alu_addsub_c) <= '0'; -- ADD(I)
850
            end if;
851
 
852 11 zero_gravi
            -- multi cycle alu operation? --
853 25 zero_gravi
            if (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sll_c) or -- SLL shift operation?
854
               (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sr_c) or -- SR shift operation?
855 33 zero_gravi
               ((execute_engine.i_reg(instr_opcode_lsb_c+5) = opcode_alu_c(5)) and (execute_engine.i_reg(instr_funct7_lsb_c) = '1') and (CPU_EXTENSION_RISCV_M = true)) then -- MULDIV?
856 6 zero_gravi
              execute_engine.state_nxt <= ALU_WAIT;
857 26 zero_gravi
            else -- single cycle ALU operation
858 2 zero_gravi
              ctrl_nxt(ctrl_rf_wb_en_c) <= '1'; -- valid RF write-back
859 6 zero_gravi
              execute_engine.state_nxt <= DISPATCH;
860 2 zero_gravi
            end if;
861
 
862 25 zero_gravi
          when opcode_lui_c | opcode_auipc_c => -- load upper immediate / add upper immediate to PC
863 2 zero_gravi
          -- ------------------------------------------------------------
864 27 zero_gravi
            ctrl_nxt(ctrl_alu_opa_mux_c) <= '1'; -- ALU.OPA = PC (for AUIPC only)
865
            ctrl_nxt(ctrl_alu_opb_mux_c) <= '1'; -- use IMM as ALU.OPB
866 25 zero_gravi
            if (execute_engine.i_reg(instr_opcode_lsb_c+5) = opcode_lui_c(5)) then -- LUI
867 27 zero_gravi
              ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_movb_c; -- actual ALU operation = MOVB
868
            else -- AUIPC
869 29 zero_gravi
              ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_addsub_c; -- actual ALU operation = ADD
870 2 zero_gravi
            end if;
871
            ctrl_nxt(ctrl_rf_in_mux_msb_c downto ctrl_rf_in_mux_lsb_c) <= "00"; -- RF input = ALU result
872
            ctrl_nxt(ctrl_rf_wb_en_c) <= '1'; -- valid RF write-back
873 25 zero_gravi
            execute_engine.state_nxt  <= DISPATCH;
874 2 zero_gravi
 
875
          when opcode_load_c | opcode_store_c => -- load/store
876
          -- ------------------------------------------------------------
877 27 zero_gravi
            ctrl_nxt(ctrl_alu_opa_mux_c) <= '0'; -- use RS1 as ALU.OPA
878
            ctrl_nxt(ctrl_alu_opb_mux_c) <= '1'; -- use IMM as ALU.OPB
879 29 zero_gravi
            ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_addsub_c; -- actual ALU operation = ADD
880 6 zero_gravi
            ctrl_nxt(ctrl_bus_mar_we_c) <= '1'; -- write to MAR
881
            ctrl_nxt(ctrl_bus_mdo_we_c) <= '1'; -- write to MDO (only relevant for stores)
882 12 zero_gravi
            execute_engine.state_nxt    <= LOADSTORE_0;
883 2 zero_gravi
 
884 29 zero_gravi
          when opcode_branch_c | opcode_jal_c | opcode_jalr_c => -- branch / jump and link (with register)
885 2 zero_gravi
          -- ------------------------------------------------------------
886 29 zero_gravi
            ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_addsub_c; -- actual ALU operation = ADD
887 2 zero_gravi
            -- compute target address --
888 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
889
              ctrl_nxt(ctrl_alu_opa_mux_c) <= '0'; -- use RS1 as ALU.OPA (branch target address base)
890
            else -- JAL / branch
891
              ctrl_nxt(ctrl_alu_opa_mux_c) <= '1'; -- use PC as ALU.OPA (branch target address base)
892 2 zero_gravi
            end if;
893 29 zero_gravi
            ctrl_nxt(ctrl_alu_opb_mux_c) <= '1'; -- use IMM as ALU.OPB (branch target address offset)
894 2 zero_gravi
            -- save return address --
895 13 zero_gravi
            ctrl_nxt(ctrl_rf_in_mux_msb_c downto ctrl_rf_in_mux_lsb_c) <= "10"; -- RF input = next PC (save return address)
896 29 zero_gravi
            ctrl_nxt(ctrl_rf_wb_en_c)  <= execute_engine.i_reg(instr_opcode_lsb_c+2); -- valid RF write-back? (for JAL/JALR)
897
            execute_engine.is_jump_nxt <= execute_engine.i_reg(instr_opcode_lsb_c+2); -- is this is a jump operation? (for JAL/JALR)
898 6 zero_gravi
            execute_engine.state_nxt   <= BRANCH;
899 2 zero_gravi
 
900 8 zero_gravi
          when opcode_fence_c => -- fence operations
901
          -- ------------------------------------------------------------
902 27 zero_gravi
            -- for simplicity: internally, fence and fence.i perform the same operations (flush and reload of instruction prefetch buffer)
903 26 zero_gravi
            -- FENCE.I --
904
            if (CPU_EXTENSION_RISCV_Zifencei = true) then
905
              execute_engine.pc_nxt     <= execute_engine.next_pc; -- "refetch" next instruction
906
              execute_engine.if_rst_nxt <= '1'; -- this is a non-linear PC modification
907
              fetch_engine.reset        <= '1';
908
              if (execute_engine.i_reg(instr_funct3_lsb_c) = funct3_fencei_c(0)) then
909
                ctrl_nxt(ctrl_bus_fencei_c) <= '1';
910
              end if;
911 8 zero_gravi
            end if;
912 26 zero_gravi
            -- FENCE --
913
            if (execute_engine.i_reg(instr_funct3_lsb_c) = funct3_fence_c(0)) then
914 12 zero_gravi
              ctrl_nxt(ctrl_bus_fence_c) <= '1';
915
            end if;
916 26 zero_gravi
            --
917 12 zero_gravi
            execute_engine.state_nxt <= SYS_WAIT;
918 8 zero_gravi
 
919 2 zero_gravi
          when opcode_syscsr_c => -- system/csr access
920
          -- ------------------------------------------------------------
921 25 zero_gravi
            ctrl_nxt(ctrl_rf_rs2_adr4_c downto ctrl_rf_rs2_adr0_c) <= ctrl(ctrl_rf_rs1_adr4_c downto ctrl_rf_rs1_adr0_c); -- copy rs1_addr to rs2_addr (for CSR mod)
922
            --
923 6 zero_gravi
            if (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_env_c) then -- system
924
              case execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) is
925 11 zero_gravi
                when funct12_ecall_c => -- ECALL
926 6 zero_gravi
                  trap_ctrl.env_call <= '1';
927 11 zero_gravi
                when funct12_ebreak_c => -- EBREAK
928 6 zero_gravi
                  trap_ctrl.break_point <= '1';
929 11 zero_gravi
                when funct12_mret_c => -- MRET
930 25 zero_gravi
                  trap_ctrl.env_end <= '1';
931
                  execute_engine.pc_nxt <= csr.mepc;
932
                  fetch_engine.reset <= '1';
933 20 zero_gravi
                  execute_engine.if_rst_nxt <= '1'; -- this is a non-linear PC modification
934 25 zero_gravi
                when funct12_wfi_c => -- WFI (CPU sleep)
935 27 zero_gravi
                  execute_engine.sleep_nxt <= '1'; -- good night
936 6 zero_gravi
                when others => -- undefined
937
                  NULL;
938 2 zero_gravi
              end case;
939 11 zero_gravi
              execute_engine.state_nxt <= SYS_WAIT;
940 13 zero_gravi
            else -- CSR access
941 27 zero_gravi
              csr.re_nxt <= '1'; -- always read CSR (internally)
942 13 zero_gravi
              execute_engine.state_nxt <= CSR_ACCESS;
943 2 zero_gravi
            end if;
944
 
945
          when others => -- undefined
946
          -- ------------------------------------------------------------
947 6 zero_gravi
            execute_engine.state_nxt <= DISPATCH;
948 2 zero_gravi
 
949
        end case;
950
 
951
      when CSR_ACCESS => -- write CSR data to RF, write ALU.res to CSR
952
      -- ------------------------------------------------------------
953 25 zero_gravi
        ctrl_nxt(ctrl_alu_opb_mux_c) <= execute_engine.i_reg(instr_funct3_msb_c); -- OPB = rs2 (which is rs1 here) / immediate
954 27 zero_gravi
        ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_movb_c; -- actual ALU operation = MOVB
955
        -- CSR write access --
956 6 zero_gravi
        case execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) is
957 25 zero_gravi
          when funct3_csrrw_c | funct3_csrrwi_c => -- CSRRW(I)
958 15 zero_gravi
            csr.we_nxt <= csr_acc_valid; -- always write CSR if valid access
959 27 zero_gravi
          when funct3_csrrs_c | funct3_csrrsi_c | funct3_csrrc_c | funct3_csrrci_c => -- CSRRS(I) / CSRRC(I)
960
            csr.we_nxt <= (not rs1_is_r0_v) and csr_acc_valid; -- write CSR if rs1/imm is not zero and if valid access
961 29 zero_gravi
          when others => -- invalid
962 27 zero_gravi
            csr.we_nxt <= '0';
963 2 zero_gravi
        end case;
964 27 zero_gravi
        -- register file write back --
965 12 zero_gravi
        ctrl_nxt(ctrl_rf_in_mux_msb_c downto ctrl_rf_in_mux_lsb_c) <= "11"; -- RF input = CSR output
966 2 zero_gravi
        ctrl_nxt(ctrl_rf_wb_en_c) <= '1'; -- valid RF write-back
967 27 zero_gravi
        execute_engine.state_nxt  <= SYS_WAIT; -- have another cycle to let side-effects kick in (FIXME?)
968 2 zero_gravi
 
969 19 zero_gravi
      when ALU_WAIT => -- wait for multi-cycle ALU operation (shifter or CP) to finish
970 2 zero_gravi
      -- ------------------------------------------------------------
971 6 zero_gravi
        ctrl_nxt(ctrl_rf_in_mux_msb_c downto ctrl_rf_in_mux_lsb_c) <= "00"; -- RF input = ALU result
972 12 zero_gravi
        ctrl_nxt(ctrl_rf_wb_en_c) <= '1'; -- valid RF write-back (permanent write-back)
973 29 zero_gravi
        -- cp access or alu shift? --
974
        if (execute_engine.is_cp_op = '1') then
975
          ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_cp_c;
976
        else
977
          ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_shift_c;
978 19 zero_gravi
        end if;
979
        -- wait for result --
980 6 zero_gravi
        if (alu_wait_i = '0') then
981 29 zero_gravi
          execute_engine.state_nxt <= DISPATCH;
982 2 zero_gravi
        end if;
983
 
984 6 zero_gravi
      when BRANCH => -- update PC for taken branches and jumps
985
      -- ------------------------------------------------------------
986
        if (execute_engine.is_jump = '1') or (execute_engine.branch_taken = '1') then
987 29 zero_gravi
          execute_engine.pc_nxt     <= alu_res_i; -- branch/jump destination
988 20 zero_gravi
          fetch_engine.reset        <= '1'; -- trigger new instruction fetch from modified PC
989
          execute_engine.if_rst_nxt <= '1'; -- this is a non-linear PC modification
990
          execute_engine.state_nxt  <= SYS_WAIT;
991 11 zero_gravi
        else
992
          execute_engine.state_nxt <= DISPATCH;
993 6 zero_gravi
        end if;
994
 
995 12 zero_gravi
      when LOADSTORE_0 => -- trigger memory request
996 6 zero_gravi
      -- ------------------------------------------------------------
997 12 zero_gravi
        if (execute_engine.i_reg(instr_opcode_msb_c-1) = '0') then -- LOAD
998
          ctrl_nxt(ctrl_bus_rd_c) <= '1'; -- read request
999
        else -- STORE
1000
          ctrl_nxt(ctrl_bus_wr_c) <= '1'; -- write request
1001
        end if;
1002
        execute_engine.state_nxt <= LOADSTORE_1;
1003 6 zero_gravi
 
1004 12 zero_gravi
      when LOADSTORE_1 => -- memory latency
1005 6 zero_gravi
      -- ------------------------------------------------------------
1006
        ctrl_nxt(ctrl_bus_mdi_we_c) <= '1'; -- write input data to MDI (only relevant for LOAD)
1007 12 zero_gravi
        execute_engine.state_nxt <= LOADSTORE_2;
1008 6 zero_gravi
 
1009 12 zero_gravi
      when LOADSTORE_2 => -- wait for bus transaction to finish
1010 6 zero_gravi
      -- ------------------------------------------------------------
1011
        ctrl_nxt(ctrl_bus_mdi_we_c) <= '1'; -- keep writing input data to MDI (only relevant for LOAD)
1012
        ctrl_nxt(ctrl_rf_in_mux_msb_c downto ctrl_rf_in_mux_lsb_c) <= "01"; -- RF input = memory input (only relevant for LOAD)
1013
        if (ma_load_i = '1') or (be_load_i = '1') or (ma_store_i = '1') or (be_store_i = '1') then -- abort if exception
1014 7 zero_gravi
          execute_engine.state_nxt <= SYS_WAIT;
1015 26 zero_gravi
        elsif (bus_d_wait_i = '0') then -- wait for bus to finish transaction
1016 23 zero_gravi
          if (execute_engine.i_reg(instr_opcode_msb_c-1) = '0') then -- LOAD
1017 6 zero_gravi
            ctrl_nxt(ctrl_rf_wb_en_c) <= '1'; -- valid RF write-back
1018
          end if;
1019
          execute_engine.state_nxt <= DISPATCH;
1020
        end if;
1021
 
1022 2 zero_gravi
      when others => -- undefined
1023
      -- ------------------------------------------------------------
1024 7 zero_gravi
        execute_engine.state_nxt <= SYS_WAIT;
1025 2 zero_gravi
 
1026
    end case;
1027 6 zero_gravi
  end process execute_engine_fsm_comb;
1028 2 zero_gravi
 
1029
 
1030 15 zero_gravi
-- ****************************************************************************************************************************
1031
-- Invalid Instruction / CSR access check
1032
-- ****************************************************************************************************************************
1033
 
1034
 
1035
  -- Illegal CSR Access Check ---------------------------------------------------------------
1036
  -- -------------------------------------------------------------------------------------------
1037 26 zero_gravi
  invalid_csr_access_check: process(execute_engine.i_reg, csr.privilege)
1038 15 zero_gravi
    variable is_m_mode_v : std_ulogic;
1039 30 zero_gravi
    variable csr_wacc_v  : std_ulogic; -- to check access to read-only CSRs
1040
--  variable csr_racc_v  : std_ulogic; -- to check access to write-only CSRs
1041 15 zero_gravi
  begin
1042
    -- are we in machine mode? --
1043 29 zero_gravi
    if (csr.privilege = priv_mode_m_c) then
1044 15 zero_gravi
      is_m_mode_v := '1';
1045 27 zero_gravi
    else
1046
      is_m_mode_v := '0';
1047 15 zero_gravi
    end if;
1048
 
1049 30 zero_gravi
    -- is this CSR instruction really going to write/read to/from a CSR? --
1050
    if (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrw_c) or
1051
       (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrwi_c) then
1052
      csr_wacc_v := '1'; -- always write CSR
1053
--    csr_racc_v := or_all_f(execute_engine.i_reg(instr_rd_msb_c downto instr_rd_lsb_c)); -- read allowed if rd != 0
1054
    else
1055
      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
1056
--    csr_racc_v := '1'; -- always read CSR
1057
    end if;
1058
 
1059 15 zero_gravi
    -- check CSR access --
1060 29 zero_gravi
    case execute_engine.i_reg(instr_csr_id_msb_c downto instr_csr_id_lsb_c) is
1061 30 zero_gravi
      when csr_mstatus_c   => csr_acc_valid <= is_m_mode_v; -- M-mode only
1062
      when csr_misa_c      => csr_acc_valid <= is_m_mode_v;-- and (not csr_wacc_v); -- M-mode only, MISA is read-only for the NEORV32 but we don't cause an exception here for compatibility
1063
      when csr_mie_c       => csr_acc_valid <= is_m_mode_v; -- M-mode only
1064
      when csr_mtvec_c     => csr_acc_valid <= is_m_mode_v; -- M-mode only
1065
      when csr_mscratch_c  => csr_acc_valid <= is_m_mode_v; -- M-mode only
1066
      when csr_mepc_c      => csr_acc_valid <= is_m_mode_v; -- M-mode only
1067
      when csr_mcause_c    => csr_acc_valid <= is_m_mode_v; -- M-mode only
1068
      when csr_mtval_c     => csr_acc_valid <= is_m_mode_v; -- M-mode only
1069
      when csr_mip_c       => csr_acc_valid <= is_m_mode_v; -- M-mode only
1070 15 zero_gravi
      --
1071 30 zero_gravi
      when csr_pmpcfg0_c   => csr_acc_valid <= bool_to_ulogic_f(PMP_USE) and bool_to_ulogic_f(boolean(PMP_NUM_REGIONS >= 1)) and is_m_mode_v; -- M-mode only
1072
      when csr_pmpcfg1_c   => csr_acc_valid <= bool_to_ulogic_f(PMP_USE) and bool_to_ulogic_f(boolean(PMP_NUM_REGIONS >= 5)) and is_m_mode_v; -- M-mode only
1073 15 zero_gravi
      --
1074 30 zero_gravi
      when csr_pmpaddr0_c  => csr_acc_valid <= bool_to_ulogic_f(PMP_USE) and bool_to_ulogic_f(boolean(PMP_NUM_REGIONS >= 1)) and is_m_mode_v; -- M-mode only
1075
      when csr_pmpaddr1_c  => csr_acc_valid <= bool_to_ulogic_f(PMP_USE) and bool_to_ulogic_f(boolean(PMP_NUM_REGIONS >= 2)) and is_m_mode_v; -- M-mode only
1076
      when csr_pmpaddr2_c  => csr_acc_valid <= bool_to_ulogic_f(PMP_USE) and bool_to_ulogic_f(boolean(PMP_NUM_REGIONS >= 3)) and is_m_mode_v; -- M-mode only
1077
      when csr_pmpaddr3_c  => csr_acc_valid <= bool_to_ulogic_f(PMP_USE) and bool_to_ulogic_f(boolean(PMP_NUM_REGIONS >= 4)) and is_m_mode_v; -- M-mode only
1078
      when csr_pmpaddr4_c  => csr_acc_valid <= bool_to_ulogic_f(PMP_USE) and bool_to_ulogic_f(boolean(PMP_NUM_REGIONS >= 5)) and is_m_mode_v; -- M-mode only
1079
      when csr_pmpaddr5_c  => csr_acc_valid <= bool_to_ulogic_f(PMP_USE) and bool_to_ulogic_f(boolean(PMP_NUM_REGIONS >= 6)) and is_m_mode_v; -- M-mode only
1080
      when csr_pmpaddr6_c  => csr_acc_valid <= bool_to_ulogic_f(PMP_USE) and bool_to_ulogic_f(boolean(PMP_NUM_REGIONS >= 7)) and is_m_mode_v; -- M-mode only
1081
      when csr_pmpaddr7_c  => csr_acc_valid <= bool_to_ulogic_f(PMP_USE) and bool_to_ulogic_f(boolean(PMP_NUM_REGIONS >= 8)) and is_m_mode_v; -- M-mode only
1082 15 zero_gravi
      --
1083 30 zero_gravi
      when csr_mcycle_c    => csr_acc_valid <= is_m_mode_v; -- M-mode only
1084
      when csr_minstret_c  => csr_acc_valid <= is_m_mode_v; -- M-mode only
1085 15 zero_gravi
      --
1086 30 zero_gravi
      when csr_mcycleh_c   => csr_acc_valid <= is_m_mode_v; -- M-mode only
1087
      when csr_minstreth_c => csr_acc_valid <= is_m_mode_v; -- M-mode only
1088 15 zero_gravi
      --
1089 30 zero_gravi
      when csr_cycle_c     => csr_acc_valid <= (not csr_wacc_v); -- all modes, read-only
1090
      when csr_time_c      => csr_acc_valid <= (not csr_wacc_v); -- all modes, read-only
1091
      when csr_instret_c   => csr_acc_valid <= (not csr_wacc_v); -- all modes, read-only
1092 15 zero_gravi
      --
1093 30 zero_gravi
      when csr_cycleh_c    => csr_acc_valid <= (not csr_wacc_v); -- all modes, read-only
1094
      when csr_timeh_c     => csr_acc_valid <= (not csr_wacc_v); -- all modes, read-only
1095
      when csr_instreth_c  => csr_acc_valid <= (not csr_wacc_v); -- all modes, read-only
1096 22 zero_gravi
      --
1097 30 zero_gravi
      when csr_mvendorid_c => csr_acc_valid <= is_m_mode_v and (not csr_wacc_v); -- M-mode only, read-only
1098
      when csr_marchid_c   => csr_acc_valid <= is_m_mode_v and (not csr_wacc_v); -- M-mode only, read-only
1099
      when csr_mimpid_c    => csr_acc_valid <= is_m_mode_v and (not csr_wacc_v); -- M-mode only, read-only
1100
      when csr_mhartid_c   => csr_acc_valid <= is_m_mode_v and (not csr_wacc_v); -- M-mode only, read-only
1101 29 zero_gravi
      --
1102 30 zero_gravi
      when csr_mzext_c     => csr_acc_valid <= is_m_mode_v and (not csr_wacc_v); -- M-mode only, read-only
1103 29 zero_gravi
      --
1104 23 zero_gravi
      when others => csr_acc_valid <= '0'; -- undefined, invalid access
1105 15 zero_gravi
    end case;
1106
  end process invalid_csr_access_check;
1107
 
1108
 
1109 2 zero_gravi
  -- Illegal Instruction Check --------------------------------------------------------------
1110
  -- -------------------------------------------------------------------------------------------
1111 26 zero_gravi
  illegal_instruction_check: process(execute_engine, csr_acc_valid)
1112 2 zero_gravi
  begin
1113 11 zero_gravi
    -- illegal instructions are checked in the EXECUTE stage
1114
    -- the execute engine will only commit valid instructions
1115 6 zero_gravi
    if (execute_engine.state = EXECUTE) then
1116 2 zero_gravi
      -- defaults --
1117
      illegal_instruction <= '0';
1118
      illegal_register    <= '0';
1119
 
1120
      -- check instructions --
1121 6 zero_gravi
      case execute_engine.i_reg(instr_opcode_msb_c downto instr_opcode_lsb_c) is
1122 2 zero_gravi
 
1123
        -- OPCODE check sufficient: LUI, UIPC, JAL --
1124
        when opcode_lui_c | opcode_auipc_c | opcode_jal_c =>
1125
          illegal_instruction <= '0';
1126 23 zero_gravi
          -- illegal E-CPU register? --
1127
          if (CPU_EXTENSION_RISCV_E = true) and (execute_engine.i_reg(instr_rd_msb_c) = '1') then
1128
            illegal_register <= '1';
1129
          end if;
1130 2 zero_gravi
 
1131
        when opcode_alui_c => -- check ALUI funct7
1132 6 zero_gravi
          if ((execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sll_c) and
1133
              (execute_engine.i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c) /= "0000000")) or -- shift logical left
1134
             ((execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sr_c) and
1135
              ((execute_engine.i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c) /= "0000000") and
1136
               (execute_engine.i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c) /= "0100000"))) then -- shift right
1137 2 zero_gravi
            illegal_instruction <= '1';
1138
          else
1139
            illegal_instruction <= '0';
1140
          end if;
1141 23 zero_gravi
          -- illegal E-CPU register? --
1142
          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
1143
            illegal_register <= '1';
1144
          end if;
1145 2 zero_gravi
 
1146
        when opcode_load_c => -- check LOAD funct3
1147 6 zero_gravi
          if (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_lb_c) or
1148
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_lh_c) or
1149
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_lw_c) or
1150
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_lbu_c) or
1151
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_lhu_c) then
1152 2 zero_gravi
            illegal_instruction <= '0';
1153
          else
1154
            illegal_instruction <= '1';
1155
          end if;
1156 23 zero_gravi
          -- illegal E-CPU register? --
1157
          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
1158
            illegal_register <= '1';
1159
          end if;
1160 2 zero_gravi
 
1161
        when opcode_store_c => -- check STORE funct3
1162 6 zero_gravi
          if (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sb_c) or
1163
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sh_c) or
1164
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sw_c) then
1165 2 zero_gravi
            illegal_instruction <= '0';
1166
          else
1167
            illegal_instruction <= '1';
1168
          end if;
1169 23 zero_gravi
          -- illegal E-CPU register? --
1170
          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
1171
            illegal_register <= '1';
1172
          end if;
1173 2 zero_gravi
 
1174
        when opcode_branch_c => -- check BRANCH funct3
1175 6 zero_gravi
          if (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_beq_c) or
1176
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_bne_c) or
1177
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_blt_c) or
1178
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_bge_c) or
1179
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_bltu_c) or
1180
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_bgeu_c) then
1181 2 zero_gravi
            illegal_instruction <= '0';
1182
          else
1183
            illegal_instruction <= '1';
1184
          end if;
1185 23 zero_gravi
          -- illegal E-CPU register? --
1186
          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
1187
            illegal_register <= '1';
1188
          end if;
1189 2 zero_gravi
 
1190
        when opcode_jalr_c => -- check JALR funct3
1191 6 zero_gravi
          if (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = "000") then
1192 2 zero_gravi
            illegal_instruction <= '0';
1193
          else
1194
            illegal_instruction <= '1';
1195
          end if;
1196 23 zero_gravi
          -- illegal E-CPU register? --
1197
          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
1198
            illegal_register <= '1';
1199
          end if;
1200 2 zero_gravi
 
1201
        when opcode_alu_c => -- check ALU funct3 & funct7
1202 6 zero_gravi
          if (execute_engine.i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c) = "0000001") then -- MULDIV
1203 11 zero_gravi
            if (CPU_EXTENSION_RISCV_M = false) then -- not implemented
1204 2 zero_gravi
              illegal_instruction <= '1';
1205
            end if;
1206 6 zero_gravi
          elsif ((execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_subadd_c) or
1207
                 (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sr_c)) and -- ADD/SUB or SRA/SRL check
1208
                ((execute_engine.i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c) /= "0000000") and
1209
                 (execute_engine.i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c) /= "0100000")) then -- ADD/SUB or SRA/SRL select
1210 2 zero_gravi
            illegal_instruction <= '1';
1211
          else
1212
            illegal_instruction <= '0';
1213
          end if;
1214 23 zero_gravi
          -- illegal E-CPU register? --
1215
          if (CPU_EXTENSION_RISCV_E = true) and
1216
             ((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
1217
            illegal_register <= '1';
1218
          end if;
1219 2 zero_gravi
 
1220 8 zero_gravi
        when opcode_fence_c => -- fence instructions --
1221
          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
1222
            illegal_instruction <= '0';
1223
          elsif (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_fence_c) then -- FENCE
1224
            illegal_instruction <= '0';
1225
          else
1226
            illegal_instruction <= '1';
1227
          end if;
1228
 
1229 2 zero_gravi
        when opcode_syscsr_c => -- check system instructions --
1230
          -- CSR access --
1231 6 zero_gravi
          if (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrw_c) or
1232
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrs_c) or
1233
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrc_c) or
1234
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrwi_c) or
1235
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrsi_c) or
1236
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrci_c) then
1237 15 zero_gravi
            -- valid CSR access? --
1238
            if (csr_acc_valid = '1') then
1239 2 zero_gravi
              illegal_instruction <= '0';
1240
            else
1241
              illegal_instruction <= '1';
1242
            end if;
1243 23 zero_gravi
            -- illegal E-CPU register? --
1244
            if (CPU_EXTENSION_RISCV_E = true) then
1245
              if (execute_engine.i_reg(instr_funct3_msb_c) = '0') then -- reg-reg CSR
1246
                illegal_register <= execute_engine.i_reg(instr_rs1_msb_c) or execute_engine.i_reg(instr_rd_msb_c);
1247
              else -- reg-imm CSR
1248
                illegal_register <= execute_engine.i_reg(instr_rd_msb_c);
1249
              end if;
1250
            end if;
1251 2 zero_gravi
 
1252
          -- ecall, ebreak, mret, wfi --
1253 6 zero_gravi
          elsif (execute_engine.i_reg(instr_rd_msb_c  downto instr_rd_lsb_c)  = "00000") and
1254
                (execute_engine.i_reg(instr_rs1_msb_c downto instr_rs1_lsb_c) = "00000") then
1255 13 zero_gravi
            if (execute_engine.i_reg(instr_funct12_msb_c  downto instr_funct12_lsb_c) = funct12_ecall_c)  or -- ECALL
1256 11 zero_gravi
               (execute_engine.i_reg(instr_funct12_msb_c  downto instr_funct12_lsb_c) = funct12_ebreak_c) or -- EBREAK 
1257 13 zero_gravi
               (execute_engine.i_reg(instr_funct12_msb_c  downto instr_funct12_lsb_c) = funct12_mret_c)   or -- MRET
1258
               (execute_engine.i_reg(instr_funct12_msb_c  downto instr_funct12_lsb_c) = funct12_wfi_c) then  -- WFI
1259 2 zero_gravi
              illegal_instruction <= '0';
1260
            else
1261
              illegal_instruction <= '1';
1262
            end if;
1263
          else
1264
            illegal_instruction <= '1';
1265
          end if;
1266
 
1267
        when others => -- compressed instruction or undefined instruction
1268 6 zero_gravi
          if (execute_engine.i_reg(1 downto 0) = "11") then -- undefined/unimplemented opcode
1269 2 zero_gravi
            illegal_instruction <= '1';
1270
          end if;
1271
 
1272
      end case;
1273
    else
1274
      illegal_instruction <= '0';
1275
      illegal_register    <= '0';
1276
    end if;
1277
  end process illegal_instruction_check;
1278
 
1279
  -- any illegal condition? --
1280 6 zero_gravi
  trap_ctrl.instr_il <= illegal_instruction or illegal_register or illegal_compressed;
1281 2 zero_gravi
 
1282
 
1283 6 zero_gravi
-- ****************************************************************************************************************************
1284
-- Exception and Interrupt Control
1285
-- ****************************************************************************************************************************
1286 2 zero_gravi
 
1287
 
1288 6 zero_gravi
  -- Trap Controller ------------------------------------------------------------------------
1289 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
1290 6 zero_gravi
  trap_controller: process(rstn_i, clk_i)
1291 2 zero_gravi
  begin
1292
    if (rstn_i = '0') then
1293 6 zero_gravi
      trap_ctrl.exc_buf   <= (others => '0');
1294
      trap_ctrl.irq_buf   <= (others => '0');
1295
      trap_ctrl.exc_ack   <= '0';
1296
      trap_ctrl.irq_ack   <= (others => '0');
1297
      trap_ctrl.cause     <= (others => '0');
1298
      trap_ctrl.env_start <= '0';
1299 2 zero_gravi
    elsif rising_edge(clk_i) then
1300
      if (CPU_EXTENSION_RISCV_Zicsr = true) then
1301
        -- exception buffer: misaligned load/store/instruction address
1302 6 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);
1303
        trap_ctrl.exc_buf(exception_salign_c)    <= (trap_ctrl.exc_buf(exception_salign_c)    or ma_store_i)            and (not trap_ctrl.exc_ack);
1304
        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);
1305 2 zero_gravi
        -- exception buffer: load/store/instruction bus access error
1306 6 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);
1307
        trap_ctrl.exc_buf(exception_saccess_c)   <= (trap_ctrl.exc_buf(exception_saccess_c)   or be_store_i)            and (not trap_ctrl.exc_ack);
1308
        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);
1309 2 zero_gravi
        -- exception buffer: illegal instruction / env call / break point
1310 6 zero_gravi
        trap_ctrl.exc_buf(exception_m_envcall_c) <= (trap_ctrl.exc_buf(exception_m_envcall_c) or trap_ctrl.env_call)    and (not trap_ctrl.exc_ack);
1311
        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);
1312
        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);
1313 18 zero_gravi
        -- interrupt buffer: machine software/external/timer interrupt
1314 14 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));
1315
        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));
1316
        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));
1317 18 zero_gravi
        -- interrupt buffer: custom fast interrupts
1318 14 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));
1319
        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));
1320
        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));
1321
        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));
1322 2 zero_gravi
 
1323 6 zero_gravi
        -- trap control --
1324
        if (trap_ctrl.env_start = '0') then -- no started trap handler
1325 11 zero_gravi
          if (trap_ctrl.exc_fire = '1') or ((trap_ctrl.irq_fire = '1') and -- exception/IRQ detected!
1326 13 zero_gravi
             ((execute_engine.state = EXECUTE) or (execute_engine.state = TRAP))) then -- sample IRQs in EXECUTE or TRAP state only -> continue execution even if permanent IRQ
1327
            trap_ctrl.cause     <= trap_ctrl.cause_nxt;   -- capture source ID for program (for mcause csr)
1328 7 zero_gravi
            trap_ctrl.exc_ack   <= '1';                   -- clear execption
1329
            trap_ctrl.irq_ack   <= trap_ctrl.irq_ack_nxt; -- capture and clear with interrupt ACK mask
1330 13 zero_gravi
            trap_ctrl.env_start <= '1';                   -- now execute engine can start trap handler
1331 2 zero_gravi
          end if;
1332 6 zero_gravi
        else -- trap waiting to get started
1333
          if (trap_ctrl.env_start_ack = '1') then -- start of trap handler acknowledged by execution engine
1334
            trap_ctrl.exc_ack   <= '0';
1335
            trap_ctrl.irq_ack   <= (others => '0');
1336
            trap_ctrl.env_start <= '0';
1337 2 zero_gravi
          end if;
1338
        end if;
1339
      end if;
1340
    end if;
1341 6 zero_gravi
  end process trap_controller;
1342 2 zero_gravi
 
1343
  -- any exception/interrupt? --
1344 27 zero_gravi
  trap_ctrl.exc_fire <= or_all_f(trap_ctrl.exc_buf); -- exceptions/faults CANNOT be masked
1345
  trap_ctrl.irq_fire <= or_all_f(trap_ctrl.irq_buf) and csr.mstatus_mie; -- interrupts CAN be masked
1346 2 zero_gravi
 
1347
 
1348 6 zero_gravi
  -- Trap Priority Detector -----------------------------------------------------------------
1349
  -- -------------------------------------------------------------------------------------------
1350
  trap_priority: process(trap_ctrl)
1351 2 zero_gravi
  begin
1352
    -- defaults --
1353 6 zero_gravi
    trap_ctrl.cause_nxt   <= (others => '0');
1354
    trap_ctrl.irq_ack_nxt <= (others => '0');
1355 2 zero_gravi
 
1356 9 zero_gravi
    -- the following traps are caused by asynchronous exceptions (-> interrupts)
1357 12 zero_gravi
    -- here we do need a specific acknowledge mask since several sources can trigger at once
1358 9 zero_gravi
 
1359 2 zero_gravi
    -- interrupt: 1.11 machine external interrupt --
1360 6 zero_gravi
    if (trap_ctrl.irq_buf(interrupt_mext_irq_c) = '1') then
1361 12 zero_gravi
      trap_ctrl.cause_nxt <= trap_mei_c;
1362 6 zero_gravi
      trap_ctrl.irq_ack_nxt(interrupt_mext_irq_c) <= '1';
1363 2 zero_gravi
 
1364
    -- interrupt: 1.7 machine timer interrupt --
1365 6 zero_gravi
    elsif (trap_ctrl.irq_buf(interrupt_mtime_irq_c) = '1') then
1366 12 zero_gravi
      trap_ctrl.cause_nxt <= trap_mti_c;
1367 6 zero_gravi
      trap_ctrl.irq_ack_nxt(interrupt_mtime_irq_c) <= '1';
1368 2 zero_gravi
 
1369
    -- interrupt: 1.3 machine SW interrupt --
1370 6 zero_gravi
    elsif (trap_ctrl.irq_buf(interrupt_msw_irq_c) = '1') then
1371 12 zero_gravi
      trap_ctrl.cause_nxt <= trap_msi_c;
1372 6 zero_gravi
      trap_ctrl.irq_ack_nxt(interrupt_msw_irq_c) <= '1';
1373 2 zero_gravi
 
1374
 
1375 14 zero_gravi
    -- interrupt: 1.16 fast interrupt channel 0 --
1376
    elsif (trap_ctrl.irq_buf(interrupt_firq_0_c) = '1') then
1377
      trap_ctrl.cause_nxt <= trap_firq0_c;
1378
      trap_ctrl.irq_ack_nxt(interrupt_firq_0_c) <= '1';
1379
 
1380
    -- interrupt: 1.17 fast interrupt channel 1 --
1381
    elsif (trap_ctrl.irq_buf(interrupt_firq_1_c) = '1') then
1382
      trap_ctrl.cause_nxt <= trap_firq1_c;
1383
      trap_ctrl.irq_ack_nxt(interrupt_firq_1_c) <= '1';
1384
 
1385
    -- interrupt: 1.18 fast interrupt channel 2 --
1386
    elsif (trap_ctrl.irq_buf(interrupt_firq_2_c) = '1') then
1387
      trap_ctrl.cause_nxt <= trap_firq2_c;
1388
      trap_ctrl.irq_ack_nxt(interrupt_firq_2_c) <= '1';
1389
 
1390
    -- interrupt: 1.19 fast interrupt channel 3 --
1391
    elsif (trap_ctrl.irq_buf(interrupt_firq_3_c) = '1') then
1392
      trap_ctrl.cause_nxt <= trap_firq3_c;
1393
      trap_ctrl.irq_ack_nxt(interrupt_firq_3_c) <= '1';
1394
 
1395
 
1396 4 zero_gravi
    -- the following traps are caused by synchronous exceptions
1397 12 zero_gravi
    -- here we do not need a specific acknowledge mask since only one exception (the one
1398 9 zero_gravi
    -- with highest priority) can trigger at once
1399 4 zero_gravi
 
1400 2 zero_gravi
    -- trap/fault: 0.1 instruction access fault --
1401 6 zero_gravi
    elsif (trap_ctrl.exc_buf(exception_iaccess_c) = '1') then
1402 12 zero_gravi
      trap_ctrl.cause_nxt <= trap_iba_c;
1403 2 zero_gravi
 
1404
    -- trap/fault: 0.2 illegal instruction --
1405 6 zero_gravi
    elsif (trap_ctrl.exc_buf(exception_iillegal_c) = '1') then
1406 12 zero_gravi
      trap_ctrl.cause_nxt <= trap_iil_c;
1407 2 zero_gravi
 
1408 12 zero_gravi
    -- trap/fault: 0.0 instruction address misaligned --
1409
    elsif (trap_ctrl.exc_buf(exception_ialign_c) = '1') then
1410
      trap_ctrl.cause_nxt <= trap_ima_c;
1411 2 zero_gravi
 
1412 12 zero_gravi
 
1413 2 zero_gravi
    -- trap/fault: 0.11 environment call from M-mode --
1414 6 zero_gravi
    elsif (trap_ctrl.exc_buf(exception_m_envcall_c) = '1') then
1415 14 zero_gravi
      trap_ctrl.cause_nxt <= trap_menv_c;
1416 2 zero_gravi
 
1417
    -- trap/fault: 0.3 breakpoint --
1418 6 zero_gravi
    elsif (trap_ctrl.exc_buf(exception_break_c) = '1') then
1419 12 zero_gravi
      trap_ctrl.cause_nxt <= trap_brk_c;
1420 2 zero_gravi
 
1421
 
1422
    -- trap/fault: 0.6 store address misaligned -
1423 6 zero_gravi
    elsif (trap_ctrl.exc_buf(exception_salign_c) = '1') then
1424 12 zero_gravi
      trap_ctrl.cause_nxt <= trap_sma_c;
1425 2 zero_gravi
 
1426
    -- trap/fault: 0.4 load address misaligned --
1427 6 zero_gravi
    elsif (trap_ctrl.exc_buf(exception_lalign_c) = '1') then
1428 12 zero_gravi
      trap_ctrl.cause_nxt <= trap_lma_c;
1429 2 zero_gravi
 
1430
    -- trap/fault: 0.7 store access fault --
1431 6 zero_gravi
    elsif (trap_ctrl.exc_buf(exception_saccess_c) = '1') then
1432 12 zero_gravi
      trap_ctrl.cause_nxt <= trap_sbe_c;
1433 2 zero_gravi
 
1434
    -- trap/fault: 0.5 load access fault --
1435 6 zero_gravi
    elsif (trap_ctrl.exc_buf(exception_laccess_c) = '1') then
1436 12 zero_gravi
      trap_ctrl.cause_nxt <= trap_lbe_c;
1437 2 zero_gravi
 
1438
    -- undefined / not implemented --
1439
    else
1440 6 zero_gravi
      trap_ctrl.cause_nxt   <= (others => '0');
1441
      trap_ctrl.irq_ack_nxt <= (others => '0');
1442 2 zero_gravi
    end if;
1443 6 zero_gravi
  end process trap_priority;
1444
 
1445 2 zero_gravi
 
1446 6 zero_gravi
-- ****************************************************************************************************************************
1447
-- Control and Status Registers (CSRs)
1448
-- ****************************************************************************************************************************
1449 2 zero_gravi
 
1450 27 zero_gravi
  -- Control and Status Registers Write Data ------------------------------------------------
1451
  -- -------------------------------------------------------------------------------------------
1452
  csr_write_data: process(execute_engine.i_reg, csr.rdata, alu_res_i)
1453
  begin
1454 29 zero_gravi
    -- "mini ALU" for CSR update operations --
1455 27 zero_gravi
    case execute_engine.i_reg(instr_funct3_lsb_c+1 downto instr_funct3_lsb_c) is
1456
      when "10"   => csr.wdata <= csr.rdata or alu_res_i; -- CSRRS(I)
1457
      when "11"   => csr.wdata <= csr.rdata and (not alu_res_i); -- CSRRC(I)
1458
      when others => csr.wdata <= alu_res_i; -- CSRRW(I)
1459
    end case;
1460
  end process csr_write_data;
1461
 
1462
 
1463 2 zero_gravi
  -- Control and Status Registers Write Access ----------------------------------------------
1464
  -- -------------------------------------------------------------------------------------------
1465
  csr_write_access: process(rstn_i, clk_i)
1466
  begin
1467
    if (rstn_i = '0') then
1468 11 zero_gravi
      csr.we <= '0';
1469
      --
1470 6 zero_gravi
      csr.mstatus_mie  <= '0';
1471
      csr.mstatus_mpie <= '0';
1472 29 zero_gravi
      csr.mstatus_mpp  <= priv_mode_m_c; -- start in MACHINE mode
1473
      csr.privilege    <= priv_mode_m_c; -- start in MACHINE mode
1474 6 zero_gravi
      csr.mie_msie     <= '0';
1475
      csr.mie_meie     <= '0';
1476
      csr.mie_mtie     <= '0';
1477 14 zero_gravi
      csr.mie_firqe    <= (others => '0');
1478 6 zero_gravi
      csr.mtvec        <= (others => '0');
1479 12 zero_gravi
      csr.mscratch     <= (others => '0');
1480
      csr.mepc         <= (others => '0');
1481
      csr.mcause       <= (others => '0');
1482 6 zero_gravi
      csr.mtval        <= (others => '0');
1483 15 zero_gravi
      csr.pmpcfg       <= (others => (others => '0'));
1484
      csr.pmpaddr      <= (others => (others => '0'));
1485 2 zero_gravi
    elsif rising_edge(clk_i) then
1486 11 zero_gravi
 
1487 29 zero_gravi
      -- write access? --
1488
      csr.we <= csr.we_nxt;
1489 4 zero_gravi
 
1490 29 zero_gravi
      -- --------------------------------------------------------------------------------
1491
      -- CSRs that can be written by application software only
1492
      -- --------------------------------------------------------------------------------
1493
      if (CPU_EXTENSION_RISCV_Zicsr = true) and (csr.we = '1') then -- manual update
1494
 
1495
        -- machine CSRs --
1496
        if (execute_engine.i_reg(31 downto 28) = csr_mie_c(11 downto 8)) then
1497
 
1498
          -- machine trap setup --
1499
          if (execute_engine.i_reg(27 downto 24) = csr_mie_c(7 downto 4)) then
1500
            if (execute_engine.i_reg(23 downto 20) = csr_mie_c(3 downto 0)) then -- R/W: mie - machine interrupt-enable register
1501
              csr.mie_msie <= csr.wdata(03); -- machine SW IRQ enable
1502
              csr.mie_mtie <= csr.wdata(07); -- machine TIMER IRQ enable
1503
              csr.mie_meie <= csr.wdata(11); -- machine EXT IRQ enable
1504
              --
1505
              csr.mie_firqe(0) <= csr.wdata(16); -- fast interrupt channel 0
1506
              csr.mie_firqe(1) <= csr.wdata(17); -- fast interrupt channel 1
1507
              csr.mie_firqe(2) <= csr.wdata(18); -- fast interrupt channel 2
1508
              csr.mie_firqe(3) <= csr.wdata(19); -- fast interrupt channel 3
1509 4 zero_gravi
            end if;
1510 29 zero_gravi
            if (execute_engine.i_reg(23 downto 20) = csr_mtvec_c(3 downto 0)) then -- R/W: mtvec - machine trap-handler base address (for ALL exceptions)
1511
              csr.mtvec <= csr.wdata(data_width_c-1 downto 2) & "00"; -- mtvec.MODE=0
1512 4 zero_gravi
            end if;
1513 29 zero_gravi
          end if;
1514
 
1515
          -- machine trap handling --
1516
          if (execute_engine.i_reg(27 downto 20) = csr_mscratch_c(7 downto 0)) then -- R/W: mscratch - machine scratch register
1517
            csr.mscratch <= csr.wdata;
1518
          end if;
1519
 
1520
          -- machine physical memory protection (pmp) --
1521
          if (PMP_USE = true) then
1522
            -- pmpcfg --
1523
            if (execute_engine.i_reg(27 downto 24) = csr_pmpcfg0_c(7 downto 4)) then
1524
              if (PMP_NUM_REGIONS >= 1) then
1525
                if (execute_engine.i_reg(23 downto 20) = csr_pmpcfg0_c(3 downto 0)) then -- pmpcfg0
1526
                  for j in 0 to 3 loop -- bytes in pmpcfg CSR
1527
                    if ((j+1) <= PMP_NUM_REGIONS) then
1528
                      if (csr.pmpcfg(0+j)(7) = '0') then -- unlocked pmpcfg access
1529
                        csr.pmpcfg(0+j)(0) <= csr.wdata(j*8+0); -- R
1530
                        csr.pmpcfg(0+j)(1) <= csr.wdata(j*8+1); -- W
1531
                        csr.pmpcfg(0+j)(2) <= csr.wdata(j*8+2); -- X
1532
                        csr.pmpcfg(0+j)(3) <= csr.wdata(j*8+3) and csr.wdata(j*8+4); -- A_L
1533
                        csr.pmpcfg(0+j)(4) <= csr.wdata(j*8+3) and csr.wdata(j*8+4); -- A_H - NAPOT/OFF only
1534
                        csr.pmpcfg(0+j)(5) <= '0'; -- reserved
1535
                        csr.pmpcfg(0+j)(6) <= '0'; -- reserved
1536
                        csr.pmpcfg(0+j)(7) <= csr.wdata(j*8+7); -- L
1537 15 zero_gravi
                      end if;
1538 29 zero_gravi
                    end if;
1539
                  end loop; -- j (bytes in CSR)
1540 15 zero_gravi
                end if;
1541 29 zero_gravi
              end if;
1542
              if (PMP_NUM_REGIONS >= 5) then
1543
                if (execute_engine.i_reg(23 downto 20) = csr_pmpcfg1_c(3 downto 0)) then -- pmpcfg1
1544
                  for j in 0 to 3 loop -- bytes in pmpcfg CSR
1545
                    if ((j+1+4) <= PMP_NUM_REGIONS) then
1546
                      if (csr.pmpcfg(4+j)(7) = '0') then -- unlocked pmpcfg access
1547
                        csr.pmpcfg(4+j)(0) <= csr.wdata(j*8+0); -- R
1548
                        csr.pmpcfg(4+j)(1) <= csr.wdata(j*8+1); -- W
1549
                        csr.pmpcfg(4+j)(2) <= csr.wdata(j*8+2); -- X
1550
                        csr.pmpcfg(4+j)(3) <= csr.wdata(j*8+3) and csr.wdata(j*8+4); -- A_L
1551
                        csr.pmpcfg(4+j)(4) <= csr.wdata(j*8+3) and csr.wdata(j*8+4); -- A_H - NAPOT/OFF only
1552
                        csr.pmpcfg(4+j)(5) <= '0'; -- reserved
1553
                        csr.pmpcfg(4+j)(6) <= '0'; -- reserved
1554
                        csr.pmpcfg(4+j)(7) <= csr.wdata(j*8+7); -- L
1555 15 zero_gravi
                      end if;
1556 29 zero_gravi
                    end if;
1557
                  end loop; -- j (bytes in CSR)
1558 15 zero_gravi
                end if;
1559
              end if;
1560 29 zero_gravi
            end if;
1561
            -- pmpaddr --
1562
            if (execute_engine.i_reg(27 downto 24) = csr_pmpaddr0_c(7 downto 4)) then
1563
              for i in 0 to PMP_NUM_REGIONS-1 loop
1564
                if (execute_engine.i_reg(23 downto 20) = std_ulogic_vector(to_unsigned(i, 4))) and (csr.pmpcfg(i)(7) = '0') then -- unlocked pmpaddr access
1565
                  csr.pmpaddr(i) <= csr.wdata(31 downto 1) & '0'; -- min granularity is 8 bytes -> bit zero cannot be configured
1566
                end if;
1567
              end loop; -- i (CSRs)
1568
            end if;
1569
          end if; -- implement PMP at all?
1570
        end if;
1571 4 zero_gravi
 
1572 29 zero_gravi
      end if;
1573 2 zero_gravi
 
1574 29 zero_gravi
      -- --------------------------------------------------------------------------------
1575
      -- CSRs that can be written by application and hardware (application access)
1576
      -- --------------------------------------------------------------------------------
1577
      if (CPU_EXTENSION_RISCV_Zicsr = true) and (csr.we = '1') then -- manual update
1578
 
1579
        -- machine CSRs --
1580
        if (execute_engine.i_reg(31 downto 28) = csr_mstatus_c(11 downto 8)) then
1581
 
1582
          -- machine trap setup --
1583
          if (execute_engine.i_reg(27 downto 20) = csr_mstatus_c(7 downto 0)) then -- R/W: mstatus - machine status register
1584
            csr.mstatus_mie  <= csr.wdata(03);
1585
            csr.mstatus_mpie <= csr.wdata(07);
1586
            --
1587
            if (CPU_EXTENSION_RISCV_U = true) then -- user mode implemented
1588
              csr.mstatus_mpp(0) <= csr.wdata(11) or csr.wdata(12);
1589
              csr.mstatus_mpp(1) <= csr.wdata(11) or csr.wdata(12);
1590 2 zero_gravi
            end if;
1591
          end if;
1592
 
1593 29 zero_gravi
          -- machine trap handling --
1594
          if (execute_engine.i_reg(27 downto 24) = csr_mepc_c(7 downto 4)) then
1595
            if (execute_engine.i_reg(23 downto 20) = csr_mepc_c(3 downto 0)) then -- R/W: mepc - machine exception program counter
1596
              csr.mepc <= csr.wdata(data_width_c-1 downto 1) & '0';
1597 2 zero_gravi
            end if;
1598 30 zero_gravi
            if (execute_engine.i_reg(23 downto 20) = csr_mcause_c(3 downto 0)) then -- R/W: mcause - machine trap cause
1599
              csr.mcause <= (others => '0');
1600
              csr.mcause(csr.mcause'left) <= csr.wdata(31); -- 1: interrupt, 0: exception
1601
              csr.mcause(4 downto 0)      <= csr.wdata(4 downto 0); -- identifier
1602
            end if;
1603 29 zero_gravi
            if (execute_engine.i_reg(23 downto 20) = csr_mtval_c(3 downto 0)) then -- R/W: mtval - machine bad address or instruction
1604
              csr.mtval <= csr.wdata;
1605 15 zero_gravi
            end if;
1606 2 zero_gravi
          end if;
1607 9 zero_gravi
 
1608 29 zero_gravi
        end if;
1609
 
1610
      -- --------------------------------------------------------------------------------
1611
      -- CSRs that can be written by application and hardware (hardware access)
1612
      -- --------------------------------------------------------------------------------
1613
      else -- hardware update
1614
 
1615
        -- mepc & mtval: machine exception PC & machine trap value register --
1616
        if (trap_ctrl.env_start_ack = '1') then -- trap handler starting?
1617
          if (trap_ctrl.cause(trap_ctrl.cause'left) = '1') then -- for INTERRUPTS (is mcause(31))
1618
            csr.mepc  <= execute_engine.pc(data_width_c-1 downto 1) & '0'; -- this is the CURRENT pc = interrupted instruction
1619
            csr.mtval <= (others => '0'); -- mtval is zero for interrupts
1620
          else -- for EXCEPTIONS (according to their priority)
1621
            csr.mepc <= execute_engine.last_pc(data_width_c-1 downto 1) & '0'; -- this is the LAST pc = last executed instruction
1622
            if (trap_ctrl.cause(4 downto 0) = trap_iba_c(4 downto 0)) or -- instruction access error OR
1623
               (trap_ctrl.cause(4 downto 0) = trap_ima_c(4 downto 0)) or -- misaligned instruction address OR
1624
               (trap_ctrl.cause(4 downto 0) = trap_brk_c(4 downto 0)) or -- breakpoint OR
1625
               (trap_ctrl.cause(4 downto 0) = trap_menv_c(4 downto 0)) then -- environment call
1626
              csr.mtval <= execute_engine.pc(data_width_c-1 downto 1) & '0'; -- address of faulting instruction
1627
            elsif (trap_ctrl.cause(4 downto 0) = trap_iil_c(4 downto 0)) then -- illegal instruction
1628 33 zero_gravi
              csr.mtval <= execute_engine.i_reg_last; -- faulting instruction itself
1629 29 zero_gravi
            else -- load/store misalignments/access errors
1630
              csr.mtval <= mar_i; -- faulting data access address
1631
            end if;
1632 15 zero_gravi
          end if;
1633 2 zero_gravi
        end if;
1634 29 zero_gravi
 
1635
        -- mstatus: context switch --
1636
        if (trap_ctrl.env_start_ack = '1') then -- ENTER: trap handler starting?
1637 30 zero_gravi
          -- trap ID code --
1638
          csr.mcause <= (others => '0');
1639
          csr.mcause(csr.mcause'left) <= trap_ctrl.cause(trap_ctrl.cause'left); -- 1: interrupt, 0: exception
1640
          csr.mcause(4 downto 0)      <= trap_ctrl.cause(4 downto 0); -- identifier
1641
          --
1642 29 zero_gravi
          csr.mstatus_mie  <= '0'; -- disable interrupts
1643
          csr.mstatus_mpie <= csr.mstatus_mie; -- buffer previous mie state
1644
          if (CPU_EXTENSION_RISCV_U = true) then -- implement user mode
1645
            csr.privilege   <= priv_mode_m_c; -- execute trap in machine mode
1646
            csr.mstatus_mpp <= csr.privilege; -- buffer previous privilege mode
1647
          end if;
1648
        elsif (trap_ctrl.env_end = '1') then -- EXIT: return from exception
1649
          csr.mstatus_mie  <= csr.mstatus_mpie; -- restore global IRQ enable flag
1650
          csr.mstatus_mpie <= '1';
1651
          if (CPU_EXTENSION_RISCV_U = true) then -- implement user mode
1652
            csr.privilege   <= csr.mstatus_mpp; -- go back to previous privilege mode
1653
            csr.mstatus_mpp <= priv_mode_u_c;
1654
          end if;
1655
        end if;
1656
        -- user mode NOT implemented --
1657
        if (CPU_EXTENSION_RISCV_U = false) then
1658
          csr.privilege   <= priv_mode_m_c;
1659
          csr.mstatus_mpp <= priv_mode_m_c;
1660
        end if;
1661 2 zero_gravi
      end if;
1662 29 zero_gravi
 
1663 2 zero_gravi
    end if;
1664
  end process csr_write_access;
1665
 
1666
 
1667
  -- Control and Status Registers Read Access -----------------------------------------------
1668
  -- -------------------------------------------------------------------------------------------
1669
  csr_read_access: process(clk_i)
1670
  begin
1671
    if rising_edge(clk_i) then
1672 27 zero_gravi
      csr.rdata <= (others => '0'); -- default
1673 29 zero_gravi
      csr.re    <= csr.re_nxt; -- read access?
1674 11 zero_gravi
      if (CPU_EXTENSION_RISCV_Zicsr = true) and (csr.re = '1') then
1675 29 zero_gravi
        case execute_engine.i_reg(instr_csr_id_msb_c downto instr_csr_id_lsb_c) is
1676 11 zero_gravi
 
1677
          -- machine trap setup --
1678 29 zero_gravi
          when csr_mstatus_c => -- R/W: mstatus - machine status register
1679 27 zero_gravi
            csr.rdata(03) <= csr.mstatus_mie;  -- MIE
1680
            csr.rdata(07) <= csr.mstatus_mpie; -- MPIE
1681 29 zero_gravi
            csr.rdata(11) <= csr.mstatus_mpp(0); -- MPP: machine previous privilege mode low
1682
            csr.rdata(12) <= csr.mstatus_mpp(1); -- MPP: machine previous privilege mode high
1683
          when csr_misa_c => -- R/-: misa - ISA and extensions
1684 27 zero_gravi
            csr.rdata(02) <= bool_to_ulogic_f(CPU_EXTENSION_RISCV_C);     -- C CPU extension
1685
            csr.rdata(04) <= bool_to_ulogic_f(CPU_EXTENSION_RISCV_E);     -- E CPU extension
1686
            csr.rdata(08) <= not bool_to_ulogic_f(CPU_EXTENSION_RISCV_E); -- I CPU extension (if not E)
1687
            csr.rdata(12) <= bool_to_ulogic_f(CPU_EXTENSION_RISCV_M);     -- M CPU extension
1688
            csr.rdata(20) <= bool_to_ulogic_f(CPU_EXTENSION_RISCV_U);     -- U CPU extension
1689
            csr.rdata(23) <= '1';                                         -- X CPU extension (non-std extensions)
1690
            csr.rdata(30) <= '1'; -- 32-bit architecture (MXL lo)
1691
            csr.rdata(31) <= '0'; -- 32-bit architecture (MXL hi)
1692 29 zero_gravi
          when csr_mie_c => -- R/W: mie - machine interrupt-enable register
1693 27 zero_gravi
            csr.rdata(03) <= csr.mie_msie; -- machine software IRQ enable
1694
            csr.rdata(07) <= csr.mie_mtie; -- machine timer IRQ enable
1695
            csr.rdata(11) <= csr.mie_meie; -- machine external IRQ enable
1696 14 zero_gravi
            --
1697 27 zero_gravi
            csr.rdata(16) <= csr.mie_firqe(0); -- fast interrupt channel 0
1698
            csr.rdata(17) <= csr.mie_firqe(1); -- fast interrupt channel 1
1699
            csr.rdata(18) <= csr.mie_firqe(2); -- fast interrupt channel 2
1700
            csr.rdata(19) <= csr.mie_firqe(3); -- fast interrupt channel 3
1701 29 zero_gravi
          when csr_mtvec_c => -- R/W: mtvec - machine trap-handler base address (for ALL exceptions)
1702 27 zero_gravi
            csr.rdata <= csr.mtvec(data_width_c-1 downto 2) & "00"; -- mtvec.MODE=0
1703 11 zero_gravi
 
1704
          -- machine trap handling --
1705 29 zero_gravi
          when csr_mscratch_c => -- R/W: mscratch - machine scratch register
1706 27 zero_gravi
            csr.rdata <= csr.mscratch;
1707 29 zero_gravi
          when csr_mepc_c => -- R/W: mepc - machine exception program counter
1708 27 zero_gravi
            csr.rdata <= csr.mepc(data_width_c-1 downto 1) & '0';
1709 29 zero_gravi
          when csr_mcause_c => -- R/-: mcause - machine trap cause
1710 27 zero_gravi
            csr.rdata <= csr.mcause;
1711 29 zero_gravi
          when csr_mtval_c => -- R/W: mtval - machine bad address or instruction
1712 27 zero_gravi
            csr.rdata <= csr.mtval;
1713 29 zero_gravi
          when csr_mip_c => -- R/W: mip - machine interrupt pending
1714 27 zero_gravi
            csr.rdata(03) <= trap_ctrl.irq_buf(interrupt_msw_irq_c);
1715
            csr.rdata(07) <= trap_ctrl.irq_buf(interrupt_mtime_irq_c);
1716
            csr.rdata(11) <= trap_ctrl.irq_buf(interrupt_mext_irq_c);
1717 14 zero_gravi
            --
1718 27 zero_gravi
            csr.rdata(16) <= trap_ctrl.irq_buf(interrupt_firq_0_c);
1719
            csr.rdata(17) <= trap_ctrl.irq_buf(interrupt_firq_1_c);
1720
            csr.rdata(18) <= trap_ctrl.irq_buf(interrupt_firq_2_c);
1721
            csr.rdata(19) <= trap_ctrl.irq_buf(interrupt_firq_3_c);
1722 11 zero_gravi
 
1723 15 zero_gravi
          -- physical memory protection --
1724 29 zero_gravi
          when csr_pmpcfg0_c => -- R/W: pmpcfg0 - physical memory protection configuration register 0
1725 15 zero_gravi
            if (PMP_USE = true) then
1726
              if (PMP_NUM_REGIONS >= 1) then
1727 27 zero_gravi
                csr.rdata(07 downto 00) <= csr.pmpcfg(0);
1728 15 zero_gravi
              end if;
1729
              if (PMP_NUM_REGIONS >= 2) then
1730 27 zero_gravi
                csr.rdata(15 downto 08) <= csr.pmpcfg(1);
1731 15 zero_gravi
              end if;
1732
              if (PMP_NUM_REGIONS >= 3) then
1733 27 zero_gravi
                csr.rdata(23 downto 16) <= csr.pmpcfg(2);
1734 15 zero_gravi
              end if;
1735
              if (PMP_NUM_REGIONS >= 4) then
1736 27 zero_gravi
                csr.rdata(31 downto 24) <= csr.pmpcfg(3);
1737 15 zero_gravi
              end if;
1738
            end if;
1739 29 zero_gravi
          when csr_pmpcfg1_c => -- R/W: pmpcfg1 - physical memory protection configuration register 1
1740 15 zero_gravi
            if (PMP_USE = true) then
1741
              if (PMP_NUM_REGIONS >= 5) then
1742 27 zero_gravi
                csr.rdata(07 downto 00) <= csr.pmpcfg(4);
1743 15 zero_gravi
              end if;
1744
              if (PMP_NUM_REGIONS >= 6) then
1745 27 zero_gravi
                csr.rdata(15 downto 08) <= csr.pmpcfg(5);
1746 15 zero_gravi
              end if;
1747
              if (PMP_NUM_REGIONS >= 7) then
1748 27 zero_gravi
                csr.rdata(23 downto 16) <= csr.pmpcfg(6);
1749 15 zero_gravi
              end if;
1750
              if (PMP_NUM_REGIONS >= 8) then
1751 27 zero_gravi
                csr.rdata(31 downto 24) <= csr.pmpcfg(7);
1752 15 zero_gravi
              end if;
1753
            end if;
1754
 
1755 29 zero_gravi
          when csr_pmpaddr0_c => -- R/W: pmpaddr0 - physical memory protection address register 0
1756 15 zero_gravi
            if (PMP_USE = true) and (PMP_NUM_REGIONS >= 1) then
1757 27 zero_gravi
              csr.rdata <= csr.pmpaddr(0);
1758 15 zero_gravi
              if (csr.pmpcfg(0)(4 downto 3) = "00") then -- mode = off
1759 27 zero_gravi
                csr.rdata(PMP_GRANULARITY-1 downto 0) <= (others => '0'); -- required for granularity check by SW
1760 15 zero_gravi
              else -- mode = NAPOT
1761 27 zero_gravi
                csr.rdata(PMP_GRANULARITY-2 downto 0) <= (others => '1');
1762 15 zero_gravi
              end if;
1763
            end if;
1764 29 zero_gravi
          when csr_pmpaddr1_c => -- R/W: pmpaddr1 - physical memory protection address register 1
1765 15 zero_gravi
            if (PMP_USE = true) and (PMP_NUM_REGIONS >= 2) then
1766 27 zero_gravi
              csr.rdata <= csr.pmpaddr(1);
1767 15 zero_gravi
              if (csr.pmpcfg(1)(4 downto 3) = "00") then -- mode = off
1768 27 zero_gravi
                csr.rdata(PMP_GRANULARITY-1 downto 0) <= (others => '0'); -- required for granularity check by SW
1769 15 zero_gravi
              else -- mode = NAPOT
1770 27 zero_gravi
                csr.rdata(PMP_GRANULARITY-2 downto 0) <= (others => '1');
1771 15 zero_gravi
              end if;
1772
            end if;
1773 29 zero_gravi
          when csr_pmpaddr2_c => -- R/W: pmpaddr2 - physical memory protection address register 2
1774 15 zero_gravi
            if (PMP_USE = true) and (PMP_NUM_REGIONS >= 3) then
1775 27 zero_gravi
              csr.rdata <= csr.pmpaddr(2);
1776 15 zero_gravi
              if (csr.pmpcfg(2)(4 downto 3) = "00") then -- mode = off
1777 27 zero_gravi
                csr.rdata(PMP_GRANULARITY-1 downto 0) <= (others => '0'); -- required for granularity check by SW
1778 15 zero_gravi
              else -- mode = NAPOT
1779 27 zero_gravi
                csr.rdata(PMP_GRANULARITY-2 downto 0) <= (others => '1');
1780 15 zero_gravi
              end if;
1781
            end if;
1782 29 zero_gravi
          when csr_pmpaddr3_c => -- R/W: pmpaddr3 - physical memory protection address register 3
1783 15 zero_gravi
            if (PMP_USE = true) and (PMP_NUM_REGIONS >= 4) then
1784 27 zero_gravi
              csr.rdata <= csr.pmpaddr(3);
1785 15 zero_gravi
              if (csr.pmpcfg(3)(4 downto 3) = "00") then -- mode = off
1786 27 zero_gravi
                csr.rdata(PMP_GRANULARITY-1 downto 0) <= (others => '0'); -- required for granularity check by SW
1787 15 zero_gravi
              else -- mode = NAPOT
1788 27 zero_gravi
                csr.rdata(PMP_GRANULARITY-2 downto 0) <= (others => '1');
1789 15 zero_gravi
              end if;
1790
            end if;
1791 29 zero_gravi
          when csr_pmpaddr4_c => -- R/W: pmpaddr4 - physical memory protection address register 4
1792 15 zero_gravi
            if (PMP_USE = true) and (PMP_NUM_REGIONS >= 5) then
1793 27 zero_gravi
              csr.rdata <= csr.pmpaddr(4);
1794 15 zero_gravi
              if (csr.pmpcfg(4)(4 downto 3) = "00") then -- mode = off
1795 27 zero_gravi
                csr.rdata(PMP_GRANULARITY-1 downto 0) <= (others => '0'); -- required for granularity check by SW
1796 15 zero_gravi
              else -- mode = NAPOT
1797 27 zero_gravi
                csr.rdata(PMP_GRANULARITY-2 downto 0) <= (others => '1');
1798 15 zero_gravi
              end if;
1799
            end if;
1800 29 zero_gravi
          when csr_pmpaddr5_c => -- R/W: pmpaddr5 - physical memory protection address register 5
1801 15 zero_gravi
            if (PMP_USE = true) and (PMP_NUM_REGIONS >= 6) then
1802 27 zero_gravi
              csr.rdata <= csr.pmpaddr(5);
1803 15 zero_gravi
              if (csr.pmpcfg(5)(4 downto 3) = "00") then -- mode = off
1804 27 zero_gravi
                csr.rdata(PMP_GRANULARITY-1 downto 0) <= (others => '0'); -- required for granularity check by SW
1805 15 zero_gravi
              else -- mode = NAPOT
1806 27 zero_gravi
                csr.rdata(PMP_GRANULARITY-2 downto 0) <= (others => '1');
1807 15 zero_gravi
              end if;
1808
            end if;
1809 29 zero_gravi
          when csr_pmpaddr6_c => -- R/W: pmpaddr6 - physical memory protection address register 6
1810 15 zero_gravi
            if (PMP_USE = true) and (PMP_NUM_REGIONS >= 7) then
1811 27 zero_gravi
              csr.rdata <= csr.pmpaddr(6);
1812 15 zero_gravi
              if (csr.pmpcfg(6)(4 downto 3) = "00") then -- mode = off
1813 27 zero_gravi
                csr.rdata(PMP_GRANULARITY-1 downto 0) <= (others => '0'); -- required for granularity check by SW
1814 15 zero_gravi
              else -- mode = NAPOT
1815 27 zero_gravi
                csr.rdata(PMP_GRANULARITY-2 downto 0) <= (others => '1');
1816 15 zero_gravi
              end if;
1817
            end if;
1818 29 zero_gravi
          when csr_pmpaddr7_c => -- R/W: pmpaddr7 - physical memory protection address register 7
1819 15 zero_gravi
            if (PMP_USE = true) and (PMP_NUM_REGIONS >= 8) then
1820 27 zero_gravi
              csr.rdata <= csr.pmpaddr(7);
1821 15 zero_gravi
              if (csr.pmpcfg(7)(4 downto 3) = "00") then -- mode = off
1822 27 zero_gravi
                csr.rdata(PMP_GRANULARITY-1 downto 0) <= (others => '0'); -- required for granularity check by SW
1823 15 zero_gravi
              else -- mode = NAPOT
1824 27 zero_gravi
                csr.rdata(PMP_GRANULARITY-2 downto 0) <= (others => '1');
1825 15 zero_gravi
              end if;
1826
            end if;
1827
 
1828 29 zero_gravi
          -- counters and timers --
1829
          when csr_cycle_c | csr_mcycle_c => -- R/(W): [m]cycle: Cycle counter LOW
1830 27 zero_gravi
            csr.rdata <= csr.mcycle(31 downto 0);
1831 29 zero_gravi
          when csr_time_c => -- R/-: time: System time LOW (from MTIME unit)
1832 27 zero_gravi
            csr.rdata <= time_i(31 downto 0);
1833 29 zero_gravi
          when csr_instret_c | csr_minstret_c => -- R/(W): [m]instret: Instructions-retired counter LOW
1834 27 zero_gravi
            csr.rdata <= csr.minstret(31 downto 0);
1835 29 zero_gravi
          when csr_cycleh_c | csr_mcycleh_c => -- R/(W): [m]cycleh: Cycle counter HIGH
1836 27 zero_gravi
            csr.rdata <= csr.mcycleh(31 downto 0);
1837 29 zero_gravi
          when csr_timeh_c => -- R/-: timeh: System time HIGH (from MTIME unit)
1838 27 zero_gravi
            csr.rdata <= time_i(63 downto 32);
1839 29 zero_gravi
          when csr_instreth_c | csr_minstreth_c => -- R/(W): [m]instreth: Instructions-retired counter HIGH
1840 27 zero_gravi
            csr.rdata <= csr.minstreth(31 downto 0);
1841 11 zero_gravi
 
1842
          -- machine information registers --
1843 29 zero_gravi
          when csr_mvendorid_c => -- R/-: mvendorid - vendor ID
1844 27 zero_gravi
            csr.rdata <= (others => '0');
1845 29 zero_gravi
          when csr_marchid_c => -- R/-: marchid - architecture ID
1846 32 zero_gravi
            csr.rdata(4 downto 0) <= "10011"; -- official open-source arch ID
1847
          when csr_mimpid_c => -- R/-: mimpid - implementation ID
1848
            csr.rdata <= hw_version_c; -- NEORV32 hardware version
1849 29 zero_gravi
          when csr_mhartid_c => -- R/-: mhartid - hardware thread ID
1850 27 zero_gravi
            csr.rdata <= HW_THREAD_ID;
1851 11 zero_gravi
 
1852 22 zero_gravi
          -- custom machine read-only CSRs --
1853 29 zero_gravi
          when csr_mzext_c => -- R/-: mzext
1854 32 zero_gravi
            csr.rdata(0) <= bool_to_ulogic_f(CPU_EXTENSION_RISCV_Zicsr);    -- RISC-V.Zicsr CPU extension
1855
            csr.rdata(1) <= bool_to_ulogic_f(CPU_EXTENSION_RISCV_Zifencei); -- RISC-V.Zifencei CPU extension
1856 33 zero_gravi
            csr.rdata(2) <= bool_to_ulogic_f(PMP_USE);                      -- RISC-V physical memory protection
1857 22 zero_gravi
 
1858 11 zero_gravi
          -- undefined/unavailable --
1859
          when others =>
1860 27 zero_gravi
            csr.rdata <= (others => '0'); -- not implemented
1861 11 zero_gravi
 
1862
        end case;
1863 2 zero_gravi
      end if;
1864
    end if;
1865
  end process csr_read_access;
1866
 
1867 27 zero_gravi
  -- CSR read data output --
1868
  csr_rdata_o <= csr.rdata;
1869
 
1870 15 zero_gravi
  -- CPU's current privilege level --
1871
  priv_mode_o <= csr.privilege;
1872 12 zero_gravi
 
1873 15 zero_gravi
  -- PMP output --
1874
  pmp_output: process(csr)
1875
  begin
1876
    pmp_addr_o <= (others => (others => '0'));
1877
    pmp_ctrl_o <= (others => (others => '0'));
1878
    if (PMP_USE = true) then
1879
      for i in 0 to PMP_NUM_REGIONS-1 loop
1880
        pmp_addr_o(i) <= csr.pmpaddr(i) & "00";
1881
        pmp_ctrl_o(i) <= csr.pmpcfg(i);
1882
      end loop; -- i
1883
    end if;
1884
  end process pmp_output;
1885
 
1886
 
1887 6 zero_gravi
  -- RISC-V Counter CSRs --------------------------------------------------------------------
1888 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
1889
  csr_counters: process(rstn_i, clk_i)
1890
  begin
1891 6 zero_gravi
    if (rstn_i = '0') then
1892 11 zero_gravi
      csr.mcycle    <= (others => '0');
1893
      csr.minstret  <= (others => '0');
1894
      csr.mcycleh   <= (others => '0');
1895
      csr.minstreth <= (others => '0');
1896
      mcycle_msb    <= '0';
1897
      minstret_msb  <= '0';
1898 6 zero_gravi
    elsif rising_edge(clk_i) then
1899 11 zero_gravi
 
1900 23 zero_gravi
      -- mcycle (cycle) --
1901
      mcycle_msb <= csr.mcycle(csr.mcycle'left);
1902 29 zero_gravi
      if (csr.we = '1') and (execute_engine.i_reg(instr_csr_id_msb_c downto instr_csr_id_lsb_c) = csr_mcycle_c) then -- write access
1903 27 zero_gravi
        csr.mcycle(31 downto 0) <= csr.wdata;
1904 23 zero_gravi
        csr.mcycle(32) <= '0';
1905
      elsif (execute_engine.sleep = '0') then -- automatic update (if CPU is not in sleep mode)
1906
        csr.mcycle <= std_ulogic_vector(unsigned(csr.mcycle) + 1);
1907
      end if;
1908 11 zero_gravi
 
1909 23 zero_gravi
      -- mcycleh (cycleh) --
1910 29 zero_gravi
      if (csr.we = '1') and (execute_engine.i_reg(instr_csr_id_msb_c downto instr_csr_id_lsb_c) = csr_mcycleh_c) then -- write access
1911 27 zero_gravi
        csr.mcycleh <= csr.wdata(csr.mcycleh'left downto 0);
1912 23 zero_gravi
      elsif ((mcycle_msb xor csr.mcycle(csr.mcycle'left)) = '1') then -- automatic update
1913
        csr.mcycleh <= std_ulogic_vector(unsigned(csr.mcycleh) + 1);
1914
      end if;
1915 11 zero_gravi
 
1916 23 zero_gravi
      -- minstret (instret) --
1917
      minstret_msb <= csr.minstret(csr.minstret'left);
1918 29 zero_gravi
      if (csr.we = '1') and (execute_engine.i_reg(instr_csr_id_msb_c downto instr_csr_id_lsb_c) = csr_minstret_c) then -- write access
1919 27 zero_gravi
        csr.minstret(31 downto 0) <= csr.wdata;
1920 23 zero_gravi
        csr.minstret(32) <= '0';
1921
      elsif (execute_engine.state_prev /= EXECUTE) and (execute_engine.state = EXECUTE) then -- automatic update
1922
        csr.minstret <= std_ulogic_vector(unsigned(csr.minstret) + 1);
1923
      end if;
1924 11 zero_gravi
 
1925 23 zero_gravi
      -- minstreth (instreth) --
1926 29 zero_gravi
      if (csr.we = '1') and (execute_engine.i_reg(instr_csr_id_msb_c downto instr_csr_id_lsb_c) = csr_minstreth_c) then -- write access
1927 27 zero_gravi
        csr.minstreth <= csr.wdata(csr.minstreth'left downto 0);
1928 23 zero_gravi
      elsif ((minstret_msb xor csr.minstret(csr.minstret'left)) = '1') then -- automatic update
1929
        csr.minstreth <= std_ulogic_vector(unsigned(csr.minstreth) + 1);
1930 2 zero_gravi
      end if;
1931
    end if;
1932
  end process csr_counters;
1933
 
1934
 
1935
end neorv32_cpu_control_rtl;

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.