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

Subversion Repositories neorv32

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

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

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

powered by: WebSVN 2.1.0

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