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

Subversion Repositories neorv32

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

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