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

Subversion Repositories neorv32

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

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

powered by: WebSVN 2.1.0

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