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

Subversion Repositories neorv32

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

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