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

Subversion Repositories neorv32

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

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