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

Subversion Repositories neorv32

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

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