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

Subversion Repositories neorv32

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

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