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

Subversion Repositories neorv32

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

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