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

Subversion Repositories neorv32

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

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

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