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

Subversion Repositories neorv32

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

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