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

Subversion Repositories neorv32

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

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

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

powered by: WebSVN 2.1.0

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