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

Subversion Repositories neorv32

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

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
    CLOCK_FREQUENCY           : natural := 0; -- clock frequency of clk_i in Hz
50
    HART_ID                   : std_ulogic_vector(31 downto 0) := x"00000000"; -- custom hardware thread ID
51
    BOOTLOADER_USE            : boolean := true;   -- implement processor-internal bootloader?
52 6 zero_gravi
    CSR_COUNTERS_USE          : boolean := true;   -- implement RISC-V perf. counters ([m]instret[h], [m]cycle[h], time[h])?
53 2 zero_gravi
    -- RISC-V CPU Extensions --
54
    CPU_EXTENSION_RISCV_C     : boolean := false;  -- implement compressed extension?
55
    CPU_EXTENSION_RISCV_E     : boolean := false;  -- implement embedded RF extension?
56
    CPU_EXTENSION_RISCV_M     : boolean := false;  -- implement muld/div extension?
57
    CPU_EXTENSION_RISCV_Zicsr : boolean := true;   -- implement CSR system?
58
    -- Memory configuration: Instruction memory --
59
    MEM_ISPACE_BASE           : std_ulogic_vector(31 downto 0) := x"00000000"; -- base address of instruction memory space
60
    MEM_ISPACE_SIZE           : natural := 8*1024; -- total size of instruction memory space in byte
61
    MEM_INT_IMEM_USE          : boolean := true;   -- implement processor-internal instruction memory
62
    MEM_INT_IMEM_SIZE         : natural := 8*1024; -- size of processor-internal instruction memory in bytes
63
    MEM_INT_IMEM_ROM          : boolean := false;  -- implement processor-internal instruction memory as ROM
64
    -- Memory configuration: Data memory --
65
    MEM_DSPACE_BASE           : std_ulogic_vector(31 downto 0) := x"80000000"; -- base address of data memory space
66
    MEM_DSPACE_SIZE           : natural := 4*1024; -- total size of data memory space in byte
67
    MEM_INT_DMEM_USE          : boolean := true;   -- implement processor-internal data memory
68
    MEM_INT_DMEM_SIZE         : natural := 4*1024; -- size of processor-internal data memory in bytes
69
    -- Memory configuration: External memory interface --
70
    MEM_EXT_USE               : boolean := false;  -- implement external memory bus interface?
71
    -- Processor peripherals --
72
    IO_GPIO_USE               : boolean := true;   -- implement general purpose input/output port unit (GPIO)?
73
    IO_MTIME_USE              : boolean := true;   -- implement machine system timer (MTIME)?
74
    IO_UART_USE               : boolean := true;   -- implement universal asynchronous receiver/transmitter (UART)?
75
    IO_SPI_USE                : boolean := true;   -- implement serial peripheral interface (SPI)?
76
    IO_TWI_USE                : boolean := true;   -- implement two-wire interface (TWI)?
77
    IO_PWM_USE                : boolean := true;   -- implement pulse-width modulation unit (PWM)?
78
    IO_WDT_USE                : boolean := true;   -- implement watch dog timer (WDT)?
79
    IO_CLIC_USE               : boolean := true;   -- implement core local interrupt controller (CLIC)?
80 3 zero_gravi
    IO_TRNG_USE               : boolean := true;   -- implement true random number generator (TRNG)?
81
    IO_DEVNULL_USE            : boolean := true    -- implement dummy device (DEVNULL)?
82 2 zero_gravi
  );
83
  port (
84
    -- global control --
85
    clk_i         : in  std_ulogic; -- global clock, rising edge
86
    rstn_i        : in  std_ulogic; -- global reset, low-active, async
87
    ctrl_o        : out std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
88
    -- status input --
89
    alu_wait_i    : in  std_ulogic; -- wait for ALU
90
    bus_wait_i    : in  std_ulogic; -- wait for bus
91
    -- data input --
92
    instr_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- instruction
93
    cmp_i         : in  std_ulogic_vector(1 downto 0); -- comparator status
94
    alu_add_i     : in  std_ulogic_vector(data_width_c-1 downto 0); -- ALU.add result
95
    -- data output --
96
    imm_o         : out std_ulogic_vector(data_width_c-1 downto 0); -- immediate
97 6 zero_gravi
    fetch_pc_o    : out std_ulogic_vector(data_width_c-1 downto 0); -- PC for instruction fetch
98
    curr_pc_o     : out std_ulogic_vector(data_width_c-1 downto 0); -- current PC (corresponding to current instruction)
99
    next_pc_o     : out std_ulogic_vector(data_width_c-1 downto 0); -- next PC (corresponding to current instruction)
100 2 zero_gravi
    -- csr data interface --
101
    csr_wdata_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- CSR write data
102
    csr_rdata_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- CSR read data
103
    -- external interrupt --
104
    clic_irq_i    : in  std_ulogic; -- CLIC interrupt request
105
    mtime_irq_i   : in  std_ulogic; -- machine timer interrupt
106
    -- bus access exceptions --
107
    mar_i         : in  std_ulogic_vector(data_width_c-1 downto 0);  -- memory address register
108
    ma_instr_i    : in  std_ulogic; -- misaligned instruction address
109
    ma_load_i     : in  std_ulogic; -- misaligned load data address
110
    ma_store_i    : in  std_ulogic; -- misaligned store data address
111
    be_instr_i    : in  std_ulogic; -- bus error on instruction access
112
    be_load_i     : in  std_ulogic; -- bus error on load data access
113
    be_store_i    : in  std_ulogic; -- bus error on store data access
114 6 zero_gravi
    bus_exc_ack_o : out std_ulogic; -- bus exception error acknowledge
115
    bus_busy_i    : in  std_ulogic  -- bus unit is busy
116 2 zero_gravi
  );
117
end neorv32_cpu_control;
118
 
119
architecture neorv32_cpu_control_rtl of neorv32_cpu_control is
120
 
121 6 zero_gravi
  -- instruction fetch enginge --
122
  type fetch_engine_state_t is (IFETCH_RESET, IFETCH_0, IFETCH_1, IFETCH_2, IFETCH_3);
123
  type fetch_engine_t is record
124
    state           : fetch_engine_state_t;
125
    state_nxt       : fetch_engine_state_t;
126
    i_buf           : std_ulogic_vector(33 downto 0);
127
    i_buf_nxt       : std_ulogic_vector(33 downto 0);
128
    i_buf2          : std_ulogic_vector(33 downto 0);
129
    i_buf2_nxt      : std_ulogic_vector(33 downto 0);
130
    ci_reg          : std_ulogic_vector(17 downto 0);
131
    ci_reg_nxt      : std_ulogic_vector(17 downto 0);
132
    i_buf_state     : std_ulogic_vector(01 downto 0);
133
    i_buf_state_nxt : std_ulogic_vector(01 downto 0);
134
    pc_real         : std_ulogic_vector(data_width_c-1 downto 0);
135
    pc_real_add     : std_ulogic_vector(data_width_c-1 downto 0);
136
    pc_fetch        : std_ulogic_vector(data_width_c-1 downto 0);
137
    pc_fetch_add    : std_ulogic_vector(data_width_c-1 downto 0);
138
    ci_return       : std_ulogic;
139
    ci_return_nxt   : std_ulogic;
140
    reset           : std_ulogic;
141
    bus_err_ack     : std_ulogic;
142
  end record;
143
  signal fetch_engine : fetch_engine_t;
144 2 zero_gravi
 
145
  -- pre-decoder --
146
  signal ci_instr32 : std_ulogic_vector(31 downto 0);
147
  signal ci_illegal : std_ulogic;
148
 
149 6 zero_gravi
  -- instrucion prefetch buffer (IPB) --
150
  type ipb_t is record
151
    wdata  : std_ulogic_vector(34 downto 0);
152
    rdata  : std_ulogic_vector(34 downto 0);
153
    waddr  : std_ulogic_vector(31 downto 0);
154
    raddr  : std_ulogic_vector(31 downto 0);
155
    status : std_ulogic;
156
    free   : std_ulogic;
157
    avail  : std_ulogic;
158
    we     : std_ulogic;
159
    re     : std_ulogic;
160
    clear  : std_ulogic;
161
  end record;
162
  signal ipb : ipb_t;
163 2 zero_gravi
 
164 6 zero_gravi
  -- instruction execution engine --
165 7 zero_gravi
  type execute_engine_state_t is (SYS_WAIT, DISPATCH, TRAP, EXECUTE, ALU_WAIT, BRANCH, STORE, LOAD, LOADSTORE_0, LOADSTORE_1, CSR_ACCESS);
166 6 zero_gravi
  type execute_engine_t is record
167
    state        : execute_engine_state_t;
168
    state_nxt    : execute_engine_state_t;
169
    state_prev   : execute_engine_state_t;
170
    i_reg        : std_ulogic_vector(31 downto 0);
171
    i_reg_nxt    : std_ulogic_vector(31 downto 0);
172
    is_ci        : std_ulogic; -- current instruction is de-compressed instruction
173
    is_ci_nxt    : std_ulogic;
174
    is_jump      : std_ulogic; -- current instruction is jump instruction
175
    is_jump_nxt  : std_ulogic;
176
    branch_taken : std_ulogic; -- branch condition fullfilled
177
    pc           : std_ulogic_vector(data_width_c-1 downto 0); -- actual PC, corresponding to current executed instruction
178
    pc_nxt       : std_ulogic_vector(data_width_c-1 downto 0);
179
    next_pc      : std_ulogic_vector(data_width_c-1 downto 0); -- next PC, corresponding to next instruction to be executed
180
    last_pc      : std_ulogic_vector(data_width_c-1 downto 0); -- PC of last executed instruction
181
  end record;
182
  signal execute_engine : execute_engine_t;
183 2 zero_gravi
 
184 6 zero_gravi
  -- trap controller --
185
  type trap_ctrl_t is record
186
    exc_buf       : std_ulogic_vector(exception_width_c-1 downto 0);
187
    exc_fire      : std_ulogic; -- set if there is a valid source in the exception buffer
188
    irq_buf       : std_ulogic_vector(interrupt_width_c-1 downto 0);
189
    irq_fire      : std_ulogic; -- set if there is a valid source in the interrupt buffer
190
    exc_ack       : std_ulogic; -- acknowledge all exceptions
191
    irq_ack       : std_ulogic_vector(interrupt_width_c-1 downto 0); -- acknowledge specific interrupt
192
    irq_ack_nxt   : std_ulogic_vector(interrupt_width_c-1 downto 0);
193
    cause         : std_ulogic_vector(data_width_c-1 downto 0); -- trap ID (for "mcause")
194
    cause_nxt     : std_ulogic_vector(data_width_c-1 downto 0);
195
    exc_src       : std_ulogic_vector(exception_width_c-1 downto 0);
196
    --
197
    env_start     : std_ulogic; -- start trap handler env
198
    env_start_ack : std_ulogic; -- start of trap handler acknowledged
199
    env_end       : std_ulogic; -- end trap handler env
200
    --
201
    instr_be      : std_ulogic; -- instruction fetch bus error
202
    instr_ma      : std_ulogic; -- instruction fetch misaligned address
203
    instr_il      : std_ulogic; -- illegal instruction
204
    env_call      : std_ulogic;
205
    break_point   : std_ulogic;
206
  end record;
207
  signal trap_ctrl : trap_ctrl_t;
208
 
209
  -- CPU control signals --
210
  signal ctrl_nxt, ctrl : std_ulogic_vector(ctrl_width_c-1 downto 0);
211 2 zero_gravi
 
212 6 zero_gravi
  -- fast bus access --
213
  signal bus_fast_ir : std_ulogic;
214
  signal bus_fast_rd : std_ulogic;
215
  signal bus_fast_wr : std_ulogic;
216 2 zero_gravi
 
217 6 zero_gravi
  -- RISC-V control and status registers (CSRs) --
218
  type csr_t is record
219
    we           : std_ulogic; -- write enable
220
    we_nxt       : std_ulogic;
221
    re           : std_ulogic; -- read enable
222
    re_nxt       : std_ulogic;
223
    mstatus_mie  : std_ulogic; -- mstatus.MIE: global IRQ enable (R/W)
224
    mstatus_mpie : std_ulogic; -- mstatus.MPIE: previous global IRQ enable (R/-)
225
    mip_msip     : std_ulogic; -- mip.MSIP: machine software interrupt pending (R/W)
226
    mie_msie     : std_ulogic; -- mie.MSIE: machine software interrupt enable (R/W)
227
    mie_meie     : std_ulogic; -- mie.MEIE: machine external interrupt enable (R/W)
228
    mie_mtie     : std_ulogic; -- mie.MEIE: machine timer interrupt enable (R/W)
229
    mepc         : std_ulogic_vector(data_width_c-1 downto 0); -- mepc: machine exception pc (R/W)
230
    mcause       : std_ulogic_vector(data_width_c-1 downto 0); -- mcause: machine trap cause (R/-)
231
    mtvec        : std_ulogic_vector(data_width_c-1 downto 0); -- mtvec: machine trap-handler base address (R/W)
232
    mtval        : std_ulogic_vector(data_width_c-1 downto 0); -- mtval: machine bad address or isntruction (R/-)
233
    mscratch     : std_ulogic_vector(data_width_c-1 downto 0); -- mscratch: scratch register (R/W)
234
    cycle        : std_ulogic_vector(32 downto 0); -- cycle, mtime (R/-), plus carry bit
235
    instret      : std_ulogic_vector(32 downto 0); -- instret (R/-), plus carry bit
236
    cycleh       : std_ulogic_vector(31 downto 0); -- cycleh, mtimeh (R/-)
237
    instreth     : std_ulogic_vector(31 downto 0); -- instreth (R/-)
238
    misa_c_en    : std_ulogic; -- misa: C extension enable bit (R/W)
239
    misa_m_en    : std_ulogic; -- misa: M extension enable bit (R/W)
240
  end record;
241
  signal csr : csr_t;
242 2 zero_gravi
 
243 6 zero_gravi
  signal cycle_msb   : std_ulogic;
244
  signal instret_msb : std_ulogic;
245 2 zero_gravi
 
246 6 zero_gravi
  -- illegal instruction check --
247 2 zero_gravi
  signal illegal_instruction : std_ulogic;
248
  signal illegal_register    : std_ulogic; -- only for E-extension
249
  signal illegal_compressed  : std_ulogic; -- only fir C-extension
250
 
251
begin
252
 
253 6 zero_gravi
-- ****************************************************************************************************************************
254
-- Instruction Fetch
255
-- ****************************************************************************************************************************
256
 
257 2 zero_gravi
  -- Compressed Instructions Recoding -------------------------------------------------------
258
  -- -------------------------------------------------------------------------------------------
259
  neorv32_cpu_decompressor_inst_true:
260
  if (CPU_EXTENSION_RISCV_C = true) generate
261
    neorv32_cpu_decompressor_inst: neorv32_cpu_decompressor
262
    port map (
263
      -- instruction input --
264 6 zero_gravi
      ci_instr16_i => fetch_engine.ci_reg(15 downto 0), -- compressed instruction input
265 2 zero_gravi
      -- instruction output --
266
      ci_illegal_o => ci_illegal, -- is an illegal compressed instruction
267
      ci_instr32_o => ci_instr32  -- 32-bit decompressed instruction
268
    );
269
  end generate;
270
 
271
  neorv32_cpu_decompressor_inst_false:
272
  if (CPU_EXTENSION_RISCV_C = false) generate
273 6 zero_gravi
    ci_instr32 <= (others => '0');
274 2 zero_gravi
    ci_illegal <= '0';
275
  end generate;
276
 
277
 
278 6 zero_gravi
  -- Fetch Engine FSM Sync ------------------------------------------------------------------
279
  -- -------------------------------------------------------------------------------------------
280
  -- for registers that require a specific reset state --
281
  fetch_engine_fsm_sync_rst: process(rstn_i, clk_i)
282
  begin
283
    if (rstn_i = '0') then
284
      fetch_engine.state <= IFETCH_RESET;
285
    elsif rising_edge(clk_i) then
286
      if (fetch_engine.reset = '1') then
287
        fetch_engine.state <= IFETCH_RESET;
288
      else
289
        fetch_engine.state <= fetch_engine.state_nxt;
290
      end if;
291
    end if;
292
  end process fetch_engine_fsm_sync_rst;
293
 
294
 
295
  -- for registers that DO NOT require a specific reset state --
296
  fetch_engine_fsm_sync: process(clk_i)
297
  begin
298
    if rising_edge(clk_i) then
299
      if (fetch_engine.state = IFETCH_RESET) then
300
        fetch_engine.pc_fetch  <= execute_engine.pc(data_width_c-1 downto 1) & '0'; -- initialize with "real" application PC
301
        fetch_engine.pc_real   <= execute_engine.pc(data_width_c-1 downto 1) & '0'; -- initialize with "real" application PC
302
      else
303
        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'));
304
        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'));
305
      end if;
306
      --
307
      fetch_engine.i_buf       <= fetch_engine.i_buf_nxt;
308
      fetch_engine.i_buf2      <= fetch_engine.i_buf2_nxt;
309
      fetch_engine.i_buf_state <= fetch_engine.i_buf_state_nxt;
310
      --
311
      fetch_engine.ci_reg      <= fetch_engine.ci_reg_nxt;
312
      fetch_engine.ci_return   <= fetch_engine.ci_return_nxt;
313
    end if;
314
  end process fetch_engine_fsm_sync;
315
 
316
 
317
  -- Fetch Engine FSM Comb ------------------------------------------------------------------
318
  -- -------------------------------------------------------------------------------------------
319
  fetch_engine_fsm_comb: process(fetch_engine, execute_engine, csr, ipb, instr_i, bus_wait_i, bus_busy_i, ci_instr32, be_instr_i, ma_instr_i)
320
  begin
321
    -- arbiter defaults --
322
    fetch_engine.state_nxt       <= fetch_engine.state;
323
    fetch_engine.pc_fetch_add    <= (others => '0');
324
    fetch_engine.pc_real_add     <= (others => '0');
325
    bus_fast_ir                  <= '0';
326
    fetch_engine.i_buf_nxt       <= fetch_engine.i_buf;
327
    fetch_engine.i_buf2_nxt      <= fetch_engine.i_buf2;
328
    fetch_engine.i_buf_state_nxt <= fetch_engine.i_buf_state;
329
    fetch_engine.ci_reg_nxt      <= fetch_engine.ci_reg;
330
    fetch_engine.ci_return_nxt   <= fetch_engine.ci_return;
331
    fetch_engine.bus_err_ack     <= '0';
332
 
333
    -- instruction prefetch buffer interface --
334
    ipb.we    <= '0';
335
    ipb.clear <= '0';
336
    ipb.wdata <= fetch_engine.i_buf2(33 downto 32) & '0' & fetch_engine.i_buf2(31 downto 0);
337
    ipb.waddr <= fetch_engine.pc_real(data_width_c-1 downto 1) & '0';
338
 
339
    -- state machine --
340
    case fetch_engine.state is
341
 
342
      when IFETCH_RESET => -- reset engine, prefetch buffer, get PC
343
      -- ------------------------------------------------------------
344
        fetch_engine.i_buf_state_nxt <= (others => '0');
345
        fetch_engine.ci_return_nxt   <= '0';
346
        ipb.clear                    <= '1'; -- clear instruction prefetch buffer
347
        fetch_engine.bus_err_ack     <= '1'; -- ack bus errors, the execute engine has to take care of them
348
        fetch_engine.state_nxt       <= IFETCH_0;
349
 
350
      when IFETCH_0 => -- output current PC to bus system, request 32-bit word
351
      -- ------------------------------------------------------------
352
        if (bus_busy_i = '0') and (execute_engine.state /= LOAD) and (execute_engine.state /= STORE) and
353
                                  (execute_engine.state /= LOADSTORE_0) and (execute_engine.state /= LOADSTORE_1) then -- wait if execute engine is using bus unit
354
          bus_fast_ir            <= '1'; -- fast instruction fetch request (output PC to bus.address)
355
          fetch_engine.state_nxt <= IFETCH_1;
356
        end if;
357
 
358
      when IFETCH_1 => -- store data from memory to buffer(s)
359
      -- ------------------------------------------------------------
360
        fetch_engine.i_buf_nxt  <= be_instr_i & ma_instr_i & instr_i(31 downto 0); -- store data word and exception info
361
        if (bus_wait_i = '0') then -- wait for bus response
362
          fetch_engine.i_buf2_nxt <= fetch_engine.i_buf;
363
          fetch_engine.i_buf_state_nxt(1) <= fetch_engine.i_buf_state(0);
364
          fetch_engine.state_nxt          <= IFETCH_2;
365
        end if;
366
 
367
        fetch_engine.i_buf_state_nxt(0) <= '1';
368
        if (be_instr_i = '1') or (ma_instr_i = '1') then -- any fetch exception?
369
          fetch_engine.bus_err_ack <= '1'; -- ack bus errors, the execute engine has to take care of them
370
        end if;
371
 
372
      when IFETCH_2 => -- construct instruction and issue
373
      -- ------------------------------------------------------------
374
        if (fetch_engine.i_buf_state(1) = '1') then
375
          if (fetch_engine.pc_fetch(1) = '0') or (CPU_EXTENSION_RISCV_C = false) or (csr.misa_c_en = '0') then -- 32-bit aligned
376
            fetch_engine.ci_reg_nxt <= fetch_engine.i_buf2(33 downto 32) & fetch_engine.i_buf2(15 downto 00);
377
            ipb.wdata <= fetch_engine.i_buf2(33 downto 32) & '0' & fetch_engine.i_buf2(31 downto 0);
378
 
379
            if (fetch_engine.i_buf2(01 downto 00) = "11") or (CPU_EXTENSION_RISCV_C = false) or (csr.misa_c_en = '0') then -- uncompressed
380
              if (ipb.free = '1') then -- free entry in buffer?
381
                ipb.we                    <= '1';
382
                fetch_engine.pc_real_add  <= std_ulogic_vector(to_unsigned(4, data_width_c));
383
                fetch_engine.pc_fetch_add <= std_ulogic_vector(to_unsigned(4, data_width_c));
384
                fetch_engine.state_nxt    <= IFETCH_0;
385
              end if;
386
 
387
            else -- compressed
388
              fetch_engine.pc_fetch_add  <= std_ulogic_vector(to_unsigned(2, data_width_c));
389
              fetch_engine.ci_return_nxt <= '1'; -- come back here after issueing
390
              fetch_engine.state_nxt     <= IFETCH_3;
391
            end if;
392
 
393
          else -- 16-bit aligned
394
            fetch_engine.ci_reg_nxt <= fetch_engine.i_buf2(33 downto 32) & fetch_engine.i_buf2(31 downto 16);
395
            ipb.wdata <= fetch_engine.i_buf(33 downto 32) & '0' & fetch_engine.i_buf(15 downto 00) & fetch_engine.i_buf2(31 downto 16);
396
 
397
            if (fetch_engine.i_buf2(17 downto 16) = "11") then -- uncompressed
398
              if (ipb.free = '1') then -- free entry in buffer?
399
                ipb.we                    <= '1';
400
                fetch_engine.pc_real_add  <= std_ulogic_vector(to_unsigned(4, data_width_c));
401
                fetch_engine.pc_fetch_add <= std_ulogic_vector(to_unsigned(4, data_width_c));
402
                fetch_engine.state_nxt    <= IFETCH_0;
403
              end if;
404
 
405
            else -- compressed
406
              fetch_engine.pc_fetch_add  <= std_ulogic_vector(to_unsigned(2, data_width_c));
407
              fetch_engine.ci_return_nxt <= '0'; -- start next fetch after issueing
408
              fetch_engine.state_nxt     <= IFETCH_3;
409
            end if;
410
          end if;
411
        else
412
         fetch_engine.pc_fetch_add <= std_ulogic_vector(to_unsigned(4, data_width_c));
413
         fetch_engine.state_nxt    <= IFETCH_0;
414
        end if;
415
 
416
      when IFETCH_3 => -- additional cycle for issueing decompressed instructions
417
      -- ------------------------------------------------------------
418
        if (ipb.free = '1') then -- free entry in buffer?
419
          ipb.we    <= '1';
420
          ipb.wdata <= fetch_engine.ci_reg(17 downto 16) & '1' & ci_instr32;
421
          fetch_engine.pc_real_add <= std_ulogic_vector(to_unsigned(2, data_width_c));
422
          if (fetch_engine.ci_return = '0') then
423
            fetch_engine.state_nxt <= IFETCH_0;
424
          else
425
            fetch_engine.state_nxt <= IFETCH_2;
426
          end if;
427
        end if;
428
 
429
      when others => -- undefined
430
      -- ------------------------------------------------------------
431
        fetch_engine.state_nxt <= IFETCH_RESET;
432
 
433
    end case;
434
  end process fetch_engine_fsm_comb;
435
 
436
 
437
-- ****************************************************************************************************************************
438
-- Instruction Prefetch Buffer
439
-- ****************************************************************************************************************************
440
 
441
 
442
  -- Instruction Prefetch Buffer Stage ------------------------------------------------------
443
  -- -------------------------------------------------------------------------------------------
444
  instr_prefetch_buffer: process(rstn_i, clk_i)
445
  begin
446
    if (rstn_i = '0') then
447
      ipb.status <= '0';
448
      ipb.rdata  <= (others => '0');
449
      ipb.raddr  <= (others => '0');
450
    elsif rising_edge(clk_i) then
451
      if (ipb.clear = '1') then
452
        ipb.status <= '0';
453
      elsif (ipb.we = '1') then
454
        ipb.status <= '1';
455
      elsif (ipb.re = '1') then
456
        ipb.status <= '0';
457
      end if;
458
      if (ipb.we = '1') then
459
        ipb.rdata <= ipb.wdata;
460
        ipb.raddr <= ipb.waddr;
461
      end if;
462
    end if;
463
  end process instr_prefetch_buffer;
464
 
465
  -- status --
466
  ipb.free  <= not ipb.status;
467
  ipb.avail <= ipb.status;
468
 
469
 
470
-- ****************************************************************************************************************************
471
-- Instruction Execution
472
-- ****************************************************************************************************************************
473
 
474
 
475 2 zero_gravi
  -- Immediate Generator --------------------------------------------------------------------
476
  -- -------------------------------------------------------------------------------------------
477
  imm_gen: process(clk_i)
478
  begin
479
    if rising_edge(clk_i) then
480 6 zero_gravi
      case execute_engine.i_reg(instr_opcode_msb_c downto instr_opcode_lsb_c) is
481 2 zero_gravi
        when opcode_store_c => -- S-immediate
482 6 zero_gravi
          imm_o(31 downto 11) <= (others => execute_engine.i_reg(31)); -- sign extension
483
          imm_o(10 downto 05) <= execute_engine.i_reg(30 downto 25);
484
          imm_o(04 downto 01) <= execute_engine.i_reg(11 downto 08);
485
          imm_o(00)           <= execute_engine.i_reg(07);
486 2 zero_gravi
        when opcode_branch_c => -- B-immediate
487 6 zero_gravi
          imm_o(31 downto 12) <= (others => execute_engine.i_reg(31)); -- sign extension
488
          imm_o(11)           <= execute_engine.i_reg(07);
489
          imm_o(10 downto 05) <= execute_engine.i_reg(30 downto 25);
490
          imm_o(04 downto 01) <= execute_engine.i_reg(11 downto 08);
491
          imm_o(00)           <= '0';
492 2 zero_gravi
        when opcode_lui_c | opcode_auipc_c => -- U-immediate
493 6 zero_gravi
          imm_o(31 downto 20) <= execute_engine.i_reg(31 downto 20);
494
          imm_o(19 downto 12) <= execute_engine.i_reg(19 downto 12);
495
          imm_o(11 downto 00) <= (others => '0');
496 2 zero_gravi
        when opcode_jal_c => -- J-immediate
497 6 zero_gravi
          imm_o(31 downto 20) <= (others => execute_engine.i_reg(31)); -- sign extension
498
          imm_o(19 downto 12) <= execute_engine.i_reg(19 downto 12);
499
          imm_o(11)           <= execute_engine.i_reg(20);
500
          imm_o(10 downto 05) <= execute_engine.i_reg(30 downto 25);
501
          imm_o(04 downto 01) <= execute_engine.i_reg(24 downto 21);
502
          imm_o(00)           <= '0';
503 2 zero_gravi
        when opcode_syscsr_c => -- CSR-immediate
504 6 zero_gravi
          imm_o(31 downto 05) <= (others => '0');
505
          imm_o(04 downto 00) <= execute_engine.i_reg(19 downto 15);
506 2 zero_gravi
        when others => -- I-immediate
507 6 zero_gravi
          imm_o(31 downto 11) <= (others => execute_engine.i_reg(31)); -- sign extension
508
          imm_o(10 downto 05) <= execute_engine.i_reg(30 downto 25);
509
          imm_o(04 downto 01) <= execute_engine.i_reg(24 downto 21);
510
          imm_o(00)           <= execute_engine.i_reg(20);
511 2 zero_gravi
      end case;
512
    end if;
513
  end process imm_gen;
514
 
515
 
516
  -- Branch Condition Check -----------------------------------------------------------------
517
  -- -------------------------------------------------------------------------------------------
518 6 zero_gravi
  branch_check: process(execute_engine.i_reg, cmp_i)
519 2 zero_gravi
  begin
520 6 zero_gravi
    case execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) is
521 2 zero_gravi
      when funct3_beq_c => -- branch if equal
522 6 zero_gravi
        execute_engine.branch_taken <= cmp_i(alu_cmp_equal_c);
523 2 zero_gravi
      when funct3_bne_c => -- branch if not equal
524 6 zero_gravi
        execute_engine.branch_taken <= not cmp_i(alu_cmp_equal_c);
525 2 zero_gravi
      when funct3_blt_c | funct3_bltu_c => -- branch if less (signed/unsigned)
526 6 zero_gravi
        execute_engine.branch_taken <= cmp_i(alu_cmp_less_c);
527 2 zero_gravi
      when funct3_bge_c | funct3_bgeu_c => -- branch if greater or equal (signed/unsigned)
528 6 zero_gravi
        execute_engine.branch_taken <= not cmp_i(alu_cmp_less_c);
529 2 zero_gravi
      when others => -- undefined
530 6 zero_gravi
        execute_engine.branch_taken <= '0';
531 2 zero_gravi
    end case;
532
  end process branch_check;
533
 
534
 
535 6 zero_gravi
  -- Execute Engine FSM Sync ----------------------------------------------------------------
536 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
537 6 zero_gravi
  -- for registers that require a specific reset state --
538
  execute_engine_fsm_sync_rst: process(rstn_i, clk_i)
539 2 zero_gravi
  begin
540
    if (rstn_i = '0') then
541 6 zero_gravi
      if (BOOTLOADER_USE = true) then -- boot from bootloader ROM
542
        execute_engine.pc      <= boot_base_c(data_width_c-1 downto 1) & '0';
543
        execute_engine.last_pc <= boot_base_c(data_width_c-1 downto 1) & '0';
544
      else -- boot from IMEM
545
        execute_engine.pc      <= MEM_ISPACE_BASE(data_width_c-1 downto 1) & '0';
546
        execute_engine.last_pc <= MEM_ISPACE_BASE(data_width_c-1 downto 1) & '0';
547
      end if;
548 7 zero_gravi
      execute_engine.state      <= SYS_WAIT;
549
      execute_engine.state_prev <= SYS_WAIT;
550 2 zero_gravi
    elsif rising_edge(clk_i) then
551 6 zero_gravi
      execute_engine.pc <= execute_engine.pc_nxt(data_width_c-1 downto 1) & '0';
552
      if (execute_engine.state = EXECUTE) then
553
        execute_engine.last_pc <= execute_engine.pc(data_width_c-1 downto 1) & '0';
554
      end if;
555
      execute_engine.state      <= execute_engine.state_nxt;
556
      execute_engine.state_prev <= execute_engine.state;
557 2 zero_gravi
    end if;
558 6 zero_gravi
  end process execute_engine_fsm_sync_rst;
559 2 zero_gravi
 
560 6 zero_gravi
 
561
  -- for registers that DO NOT require a specific reset state --
562
  execute_engine_fsm_sync: process(clk_i)
563 2 zero_gravi
  begin
564
    if rising_edge(clk_i) then
565 6 zero_gravi
      execute_engine.i_reg   <= execute_engine.i_reg_nxt;
566
      execute_engine.is_ci   <= execute_engine.is_ci_nxt;
567
      execute_engine.is_jump <= execute_engine.is_jump_nxt;
568
      -- control signals --
569
      ctrl <= ctrl_nxt;
570 2 zero_gravi
    end if;
571 6 zero_gravi
  end process execute_engine_fsm_sync;
572 2 zero_gravi
 
573 6 zero_gravi
 
574
  -- PC output --
575
  execute_engine.next_pc <= std_ulogic_vector(unsigned(execute_engine.pc(data_width_c-1 downto 1) & '0') + 2) when (execute_engine.is_ci = '1') else
576
                            std_ulogic_vector(unsigned(execute_engine.pc(data_width_c-1 downto 1) & '0') + 4);
577
  fetch_pc_o <= fetch_engine.pc_fetch(data_width_c-1 downto 1) & '0';
578
  curr_pc_o  <= execute_engine.pc(data_width_c-1 downto 1) & '0';
579
  next_pc_o  <= execute_engine.next_pc(data_width_c-1 downto 1) & '0';
580
 
581
 
582
  -- CPU Control Bus Output -----------------------------------------------------------------
583
  -- -------------------------------------------------------------------------------------------
584
  ctrl_output: process(ctrl, execute_engine, csr, bus_fast_ir, bus_fast_rd, bus_fast_wr)
585 2 zero_gravi
  begin
586
    ctrl_o <= ctrl;
587
    -- direct output of register addresses --
588 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);
589
    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);
590
    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);
591
    -- bus access requests --
592
    ctrl_o(ctrl_bus_if_c) <= ctrl(ctrl_bus_if_c) or bus_fast_ir;
593
    ctrl_o(ctrl_bus_rd_c) <= ctrl(ctrl_bus_rd_c) or bus_fast_rd;
594
    ctrl_o(ctrl_bus_wr_c) <= ctrl(ctrl_bus_wr_c) or bus_fast_wr;
595
    -- cpu extension control --
596
    ctrl_o(ctrl_sys_c_ext_en_c) <= csr.misa_c_en; -- C extension enabled
597
    ctrl_o(ctrl_sys_m_ext_en_c) <= csr.misa_m_en; -- M extension enabled
598
  end process ctrl_output;
599 2 zero_gravi
 
600
 
601 6 zero_gravi
  -- Execute Engine FSM Comb ----------------------------------------------------------------
602
  -- -------------------------------------------------------------------------------------------
603
  execute_engine_fsm_comb: process(execute_engine, fetch_engine, ipb, trap_ctrl, csr, ctrl,
604
                                   alu_add_i, alu_wait_i, bus_wait_i, ma_load_i, be_load_i, ma_store_i, be_store_i)
605 2 zero_gravi
    variable alu_immediate_v : std_ulogic;
606
    variable alu_operation_v : std_ulogic_vector(2 downto 0);
607 6 zero_gravi
    variable rd_is_r0_v      : std_ulogic;
608 2 zero_gravi
    variable rs1_is_r0_v     : std_ulogic;
609
  begin
610
    -- arbiter defaults --
611 6 zero_gravi
    execute_engine.state_nxt   <= execute_engine.state;
612
    execute_engine.i_reg_nxt   <= execute_engine.i_reg;
613
    execute_engine.is_jump_nxt <= '0';
614
    execute_engine.is_ci_nxt   <= execute_engine.is_ci;
615
    execute_engine.pc_nxt      <= execute_engine.pc(data_width_c-1 downto 1) & '0';
616 2 zero_gravi
 
617 6 zero_gravi
    -- instruction dispatch --
618
    fetch_engine.reset         <= '0';
619
    ipb.re                     <= '0';
620 2 zero_gravi
 
621 6 zero_gravi
    -- trap environment control --
622
    trap_ctrl.env_start_ack    <= '0';
623
    trap_ctrl.env_end          <= '0';
624
 
625
    -- bus access (fast) --
626
    bus_fast_rd                <= '0';
627
    bus_fast_wr                <= '0';
628
 
629 2 zero_gravi
    -- exception trigger --
630 6 zero_gravi
    trap_ctrl.instr_be         <= '0';
631
    trap_ctrl.instr_ma         <= '0';
632
    trap_ctrl.env_call         <= '0';
633
    trap_ctrl.break_point      <= '0';
634 2 zero_gravi
 
635 6 zero_gravi
    -- CSR access --
636
    csr.we_nxt                 <= '0';
637
    csr.re_nxt                 <= '0';
638
 
639 2 zero_gravi
    -- control defaults --
640
    ctrl_nxt <= (others => '0'); -- all off at first
641 6 zero_gravi
    ctrl_nxt(ctrl_bus_unsigned_c) <= execute_engine.i_reg(instr_funct3_msb_c); -- unsigned LOAD (LBU, LHU)
642
    if (execute_engine.i_reg(instr_opcode_lsb_c+4) = '1') then -- ALU ops
643
      ctrl_nxt(ctrl_alu_unsigned_c) <= execute_engine.i_reg(instr_funct3_lsb_c+0); -- unsigned ALU operation (SLTIU, SLTU)
644 2 zero_gravi
    else -- branches
645 6 zero_gravi
      ctrl_nxt(ctrl_alu_unsigned_c) <= execute_engine.i_reg(instr_funct3_lsb_c+1); -- unsigned branches (BLTU, BGEU)
646 2 zero_gravi
    end if;
647 6 zero_gravi
    ctrl_nxt(ctrl_alu_shift_dir_c) <= execute_engine.i_reg(instr_funct3_msb_c); -- shift direction
648
    ctrl_nxt(ctrl_alu_shift_ar_c)  <= execute_engine.i_reg(30); -- arithmetic shift
649
    ctrl_nxt(ctrl_bus_size_lsb_c)  <= execute_engine.i_reg(instr_funct3_lsb_c+0); -- transfer size lsb (00=byte, 01=half-word)
650
    ctrl_nxt(ctrl_bus_size_msb_c)  <= execute_engine.i_reg(instr_funct3_lsb_c+1); -- transfer size msb (10=word, 11=?)
651 2 zero_gravi
    ctrl_nxt(ctrl_alu_cmd2_c  downto ctrl_alu_cmd0_c)  <= alu_cmd_add_c; -- actual ALU operation = add
652 6 zero_gravi
    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
653 2 zero_gravi
    ctrl_nxt(ctrl_cp_id_msb_c downto ctrl_cp_id_lsb_c) <= cp_sel_muldiv_c; -- only CP0 implemented yet
654
 
655
    -- is immediate operation? --
656
    alu_immediate_v := '0';
657 6 zero_gravi
    if (execute_engine.i_reg(instr_opcode_msb_c-1) = '0') then
658 2 zero_gravi
      alu_immediate_v := '1';
659
    end if;
660
 
661 6 zero_gravi
    -- alu operation re-coding --
662
    case execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) is
663 2 zero_gravi
      when funct3_subadd_c => -- SUB / ADD(I)
664 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
665 2 zero_gravi
          alu_operation_v := alu_cmd_sub_c;
666
        else
667
          alu_operation_v := alu_cmd_add_c;
668
        end if;
669
      when funct3_sll_c  => alu_operation_v := alu_cmd_shift_c; -- SLL(I)
670 6 zero_gravi
      when funct3_slt_c  => alu_operation_v := alu_cmd_slt_c;   -- SLT(I)
671
      when funct3_sltu_c => alu_operation_v := alu_cmd_slt_c;   -- SLTU(I)
672
      when funct3_xor_c  => alu_operation_v := alu_cmd_xor_c;   -- XOR(I)
673 2 zero_gravi
      when funct3_sr_c   => alu_operation_v := alu_cmd_shift_c; -- SRL(I) / SRA(I)
674 6 zero_gravi
      when funct3_or_c   => alu_operation_v := alu_cmd_or_c;    -- OR(I)
675
      when funct3_and_c  => alu_operation_v := alu_cmd_and_c;   -- AND(I)
676 3 zero_gravi
      when others        => alu_operation_v := (others => '0'); -- undefined
677 2 zero_gravi
    end case;
678
 
679 6 zero_gravi
    -- is rd = r0? --
680
    rd_is_r0_v := '0';
681
    if (execute_engine.i_reg(instr_rd_msb_c downto instr_rd_lsb_c) = "00000") then
682
      rd_is_r0_v := '1';
683
    end if;
684
 
685 2 zero_gravi
    -- is rs1 = r0? --
686
    rs1_is_r0_v := '0';
687 6 zero_gravi
    if (execute_engine.i_reg(instr_rs1_msb_c downto instr_rs1_lsb_c) = "00000") then
688 2 zero_gravi
      rs1_is_r0_v := '1';
689
    end if;
690
 
691 6 zero_gravi
    -- state machine --
692
    case execute_engine.state is
693 2 zero_gravi
 
694 7 zero_gravi
      when SYS_WAIT => -- Delay cycle (used to wait for side effects to kick in)
695 2 zero_gravi
      -- ------------------------------------------------------------
696 6 zero_gravi
        execute_engine.state_nxt <= DISPATCH;
697 2 zero_gravi
 
698 6 zero_gravi
       when DISPATCH => -- Get new command from instruction prefetch buffer (IPB)
699
       -- ------------------------------------------------------------
700
         if (ipb.avail = '1') then -- instruction available?
701
           ipb.re <= '1';
702
           trap_ctrl.instr_ma    <= ipb.rdata(33);
703
           trap_ctrl.instr_be    <= ipb.rdata(34);
704
           if (trap_ctrl.env_start = '1') or (ipb.rdata(33) = '1') or (ipb.rdata(34) = '1') then -- exception/interrupt?
705
             execute_engine.state_nxt <= TRAP;
706
           else
707
             execute_engine.is_ci_nxt <= ipb.rdata(32); -- flag to indicate this is a compressed instruction beeing executed
708
             execute_engine.i_reg_nxt <= ipb.rdata(31 downto 0);
709
             execute_engine.pc_nxt    <= ipb.raddr(data_width_c-1 downto 1) & '0'; -- the PC according to the current instruction
710
             execute_engine.state_nxt <= EXECUTE;
711
           end if;
712
         end if;
713 2 zero_gravi
 
714 6 zero_gravi
      when TRAP => -- Start trap environment (also used as sleep state)
715 2 zero_gravi
      -- ------------------------------------------------------------
716 6 zero_gravi
        if (trap_ctrl.env_start = '1') then
717
          trap_ctrl.env_start_ack  <= '1';
718
          execute_engine.pc_nxt    <= csr.mtvec(data_width_c-1 downto 1) & '0';
719
          fetch_engine.reset       <= '1';
720 7 zero_gravi
          execute_engine.state_nxt <= SYS_WAIT;
721 2 zero_gravi
        end if;
722
 
723 6 zero_gravi
      when EXECUTE => -- Decode and execute instruction
724 2 zero_gravi
      -- ------------------------------------------------------------
725 6 zero_gravi
        case execute_engine.i_reg(instr_opcode_msb_c downto instr_opcode_lsb_c) is
726 2 zero_gravi
 
727
          when opcode_alu_c | opcode_alui_c => -- ALU operation
728
          -- ------------------------------------------------------------
729
            ctrl_nxt(ctrl_alu_opa_mux_lsb_c) <= '0'; -- use RS1 as ALU.OPA
730
            ctrl_nxt(ctrl_alu_opb_mux_lsb_c) <= alu_immediate_v; -- use IMM as ALU.OPB for immediate operations
731
            ctrl_nxt(ctrl_alu_opc_mux_c)     <= not alu_immediate_v;
732
            ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_operation_v; -- actual ALU operation
733
            ctrl_nxt(ctrl_rf_in_mux_msb_c downto ctrl_rf_in_mux_lsb_c) <= "00"; -- RF input = ALU result
734 6 zero_gravi
            if (CPU_EXTENSION_RISCV_M = true) and (execute_engine.i_reg(instr_opcode_msb_c downto instr_opcode_lsb_c) = opcode_alu_c) and
735
               (execute_engine.i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c) = "0000001") then -- MULDIV?
736 2 zero_gravi
              ctrl_nxt(ctrl_cp_use_c) <= '1'; -- use CP
737
              ctrl_nxt(ctrl_cp_id_msb_c downto ctrl_cp_id_lsb_c) <= cp_sel_muldiv_c; -- muldiv CP
738 6 zero_gravi
              execute_engine.state_nxt <= ALU_WAIT;
739 2 zero_gravi
            elsif (alu_operation_v = alu_cmd_shift_c) then -- multi-cycle shift operation?
740 6 zero_gravi
              execute_engine.state_nxt <= ALU_WAIT;
741 2 zero_gravi
            else
742
              ctrl_nxt(ctrl_rf_wb_en_c) <= '1'; -- valid RF write-back
743 6 zero_gravi
              execute_engine.state_nxt <= DISPATCH;
744 2 zero_gravi
            end if;
745
 
746
          when opcode_lui_c | opcode_auipc_c => -- load upper immediate (add to PC)
747
          -- ------------------------------------------------------------
748
            ctrl_nxt(ctrl_rf_clear_rs1_c) <= '1'; -- force RS1 = r0
749 6 zero_gravi
            if (execute_engine.i_reg(instr_opcode_msb_c downto instr_opcode_lsb_c) = opcode_auipc_c) then -- AUIPC
750 2 zero_gravi
              ctrl_nxt(ctrl_alu_opa_mux_lsb_c) <= '1'; -- use PC as ALU.OPA
751
            else -- LUI
752
              ctrl_nxt(ctrl_alu_opa_mux_lsb_c) <= '0'; -- use RS1 as ALU.OPA
753
            end if;
754 6 zero_gravi
            ctrl_nxt(ctrl_alu_opb_mux_lsb_c) <= '1'; -- use IMM as ALU.OPB
755 2 zero_gravi
            ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_add_c; -- actual ALU operation
756
            ctrl_nxt(ctrl_rf_in_mux_msb_c downto ctrl_rf_in_mux_lsb_c) <= "00"; -- RF input = ALU result
757
            ctrl_nxt(ctrl_rf_wb_en_c) <= '1'; -- valid RF write-back
758 6 zero_gravi
            execute_engine.state_nxt <= DISPATCH;
759 2 zero_gravi
 
760
          when opcode_load_c | opcode_store_c => -- load/store
761
          -- ------------------------------------------------------------
762
            ctrl_nxt(ctrl_alu_opa_mux_lsb_c) <= '0'; -- use RS1 as ALU.OPA
763
            ctrl_nxt(ctrl_alu_opb_mux_lsb_c) <= '1'; -- use IMM as ALU.OPB
764
            ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_add_c; -- actual ALU operation
765 6 zero_gravi
            ctrl_nxt(ctrl_bus_mar_we_c) <= '1'; -- write to MAR
766
            ctrl_nxt(ctrl_bus_mdo_we_c) <= '1'; -- write to MDO (only relevant for stores)
767
            if (fetch_engine.state /= IFETCH_0) then
768
              if (execute_engine.i_reg(instr_opcode_msb_c downto instr_opcode_lsb_c) = opcode_load_c) then -- LOAD
769
                execute_engine.state_nxt <= LOAD;
770
              else -- STORE
771
                execute_engine.state_nxt <= STORE;
772
              end if;
773 2 zero_gravi
            end if;
774
 
775
          when opcode_branch_c => -- branch instruction
776
          -- ------------------------------------------------------------
777
            ctrl_nxt(ctrl_alu_opa_mux_lsb_c) <= '1'; -- use PC as ALU.OPA
778
            ctrl_nxt(ctrl_alu_opb_mux_lsb_c) <= '1'; -- use IMM as ALU.OPB
779 6 zero_gravi
            ctrl_nxt(ctrl_alu_opc_mux_c)     <= '1'; -- use RS2 as ALU.OPC
780
            execute_engine.state_nxt         <= BRANCH;
781 2 zero_gravi
 
782
          when opcode_jal_c | opcode_jalr_c => -- jump and link (with register)
783
          -- ------------------------------------------------------------
784
            -- compute target address --
785 6 zero_gravi
            if (execute_engine.i_reg(instr_opcode_msb_c downto instr_opcode_lsb_c) = opcode_jal_c) then -- JAL
786 2 zero_gravi
              ctrl_nxt(ctrl_alu_opa_mux_lsb_c) <= '1'; -- use PC as ALU.OPA
787
            else -- JALR
788
              ctrl_nxt(ctrl_alu_opa_mux_lsb_c) <= '0'; -- use RS1 as ALU.OPA
789
            end if;
790
            ctrl_nxt(ctrl_alu_opb_mux_lsb_c) <= '1'; -- use IMM as ALU.OPB
791
            -- save return address --
792
            ctrl_nxt(ctrl_rf_in_mux_msb_c downto ctrl_rf_in_mux_lsb_c) <= "10"; -- RF input = current PC
793
            ctrl_nxt(ctrl_rf_wb_en_c) <= '1'; -- valid RF write-back
794 6 zero_gravi
            --
795
            execute_engine.is_jump_nxt <= '1'; -- this is a jump operation
796
            execute_engine.state_nxt   <= BRANCH;
797 2 zero_gravi
 
798
          when opcode_syscsr_c => -- system/csr access
799
          -- ------------------------------------------------------------
800 6 zero_gravi
            if (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrw_c) or
801
               (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrwi_c) then
802
              csr.re_nxt <= not rd_is_r0_v; -- only read CSR if not writing to zero_reg
803
            else
804
              csr.re_nxt <= '1'; -- always read CSR
805
            end if;
806
            --
807
            if (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_env_c) then -- system
808
              case execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) is
809
                when x"000" => -- ECALL
810
                  trap_ctrl.env_call <= '1';
811 7 zero_gravi
                  execute_engine.state_nxt <= SYS_WAIT;
812 6 zero_gravi
                when x"001" => -- EBREAK
813
                  trap_ctrl.break_point <= '1';
814 7 zero_gravi
                  execute_engine.state_nxt <= SYS_WAIT;
815 6 zero_gravi
                when x"302" => -- MRET
816
                  trap_ctrl.env_end        <= '1';
817
                  execute_engine.pc_nxt    <= csr.mepc(data_width_c-1 downto 1) & '0';
818
                  fetch_engine.reset       <= '1';
819 7 zero_gravi
                  execute_engine.state_nxt <= SYS_WAIT;
820 6 zero_gravi
                when x"105" => -- WFI
821
                  execute_engine.state_nxt <= TRAP;
822
                when others => -- undefined
823
                  NULL;
824 2 zero_gravi
              end case;
825
            elsif (CPU_EXTENSION_RISCV_Zicsr = true) then -- CSR access
826 6 zero_gravi
              execute_engine.state_nxt <= CSR_ACCESS;
827 2 zero_gravi
            else
828 6 zero_gravi
              execute_engine.state_nxt <= DISPATCH;
829 2 zero_gravi
            end if;
830
 
831
          when others => -- undefined
832
          -- ------------------------------------------------------------
833 6 zero_gravi
            execute_engine.state_nxt <= DISPATCH;
834 2 zero_gravi
 
835
        end case;
836
 
837
      when CSR_ACCESS => -- write CSR data to RF, write ALU.res to CSR
838
      -- ------------------------------------------------------------
839
        ctrl_nxt(ctrl_alu_opa_mux_msb_c) <= '0'; -- default
840
        ctrl_nxt(ctrl_alu_opa_mux_lsb_c) <= '0'; -- default
841
        ctrl_nxt(ctrl_alu_opb_mux_msb_c) <= '0'; -- default
842
        ctrl_nxt(ctrl_alu_opb_mux_lsb_c) <= '0'; -- default
843 6 zero_gravi
        case execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) is
844 7 zero_gravi
          -- register operations --
845 6 zero_gravi
          when funct3_csrrw_c => -- CSRRW
846 2 zero_gravi
            ctrl_nxt(ctrl_alu_opa_mux_lsb_c) <= '0'; -- OPA = rs1
847
            ctrl_nxt(ctrl_alu_opb_mux_lsb_c) <= '0'; -- OPB = rs2
848
            ctrl_nxt(ctrl_rf_clear_rs2_c)    <= '1'; -- rs2 = 0
849
            ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_add_c; -- actual ALU operation = ADD
850 6 zero_gravi
            csr.we_nxt <= '1'; -- always write CSR
851
          when funct3_csrrs_c => -- CSRRS
852 2 zero_gravi
            ctrl_nxt(ctrl_alu_opa_mux_msb_c) <= '1'; -- OPA = csr
853
            ctrl_nxt(ctrl_alu_opb_mux_msb_c) <= '1'; -- OPB = crs1
854
            ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_or_c; -- actual ALU operation = OR
855 6 zero_gravi
            csr.we_nxt <= not rs1_is_r0_v; -- write CSR if rs1 is not zero_reg
856
          when funct3_csrrc_c => -- CSRRC
857 2 zero_gravi
            ctrl_nxt(ctrl_alu_opa_mux_msb_c) <= '1'; -- OPA = csr
858
            ctrl_nxt(ctrl_alu_opb_mux_msb_c) <= '1'; -- OPB = rs1
859
            ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_bitc_c; -- actual ALU operation = bit clear
860 6 zero_gravi
            csr.we_nxt <= not rs1_is_r0_v; -- write CSR if rs1 is not zero_reg
861 7 zero_gravi
          -- immediate operations --
862 6 zero_gravi
          when funct3_csrrwi_c => -- CSRRWI
863 2 zero_gravi
            ctrl_nxt(ctrl_alu_opa_mux_lsb_c) <= '0'; -- OPA = rs1
864
            ctrl_nxt(ctrl_rf_clear_rs1_c)    <= '1'; -- rs1 = 0
865
            ctrl_nxt(ctrl_alu_opb_mux_lsb_c) <= '1'; -- OPB = immediate
866
            ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_add_c; -- actual ALU operation = ADD
867 6 zero_gravi
            csr.we_nxt <= '1'; -- always write CSR
868
          when funct3_csrrsi_c => -- CSRRSI
869 2 zero_gravi
            ctrl_nxt(ctrl_alu_opa_mux_msb_c) <= '1'; -- OPA = csr
870
            ctrl_nxt(ctrl_alu_opb_mux_lsb_c) <= '1'; -- OPB = immediate
871
            ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_or_c; -- actual ALU operation = OR
872 6 zero_gravi
            csr.we_nxt <= not rs1_is_r0_v; -- write CSR if UIMM5 is not zero (bits from rs1 filed)
873
          when funct3_csrrci_c => -- CSRRCI
874 2 zero_gravi
            ctrl_nxt(ctrl_alu_opa_mux_msb_c) <= '1'; -- OPA = csr
875
            ctrl_nxt(ctrl_alu_opb_mux_lsb_c) <= '1'; -- OPB = immediate
876
            ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_bitc_c; -- actual ALU operation = bit clear
877 6 zero_gravi
            csr.we_nxt <= not rs1_is_r0_v; -- write CSR if UIMM5 is not zero (bits from rs1 filed)
878 2 zero_gravi
          when others => -- undefined
879
            NULL;
880
        end case;
881
        -- RF write back --
882
        ctrl_nxt(ctrl_rf_in_mux_msb_c downto ctrl_rf_in_mux_lsb_c) <= "11"; -- RF input = CSR output register
883
        ctrl_nxt(ctrl_rf_wb_en_c) <= '1'; -- valid RF write-back
884 6 zero_gravi
        execute_engine.state_nxt <= DISPATCH;
885 2 zero_gravi
 
886 6 zero_gravi
      when ALU_WAIT => -- wait for multi-cycle ALU operation to finish
887 2 zero_gravi
      -- ------------------------------------------------------------
888 6 zero_gravi
        ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_operation_v; -- actual ALU operation
889
        ctrl_nxt(ctrl_rf_in_mux_msb_c downto ctrl_rf_in_mux_lsb_c) <= "00"; -- RF input = ALU result
890
        ctrl_nxt(ctrl_rf_wb_en_c) <= '1'; -- valid RF write-back (write back all the time)
891
        if (alu_wait_i = '0') then
892
          execute_engine.state_nxt <= DISPATCH;
893 2 zero_gravi
        end if;
894
 
895 6 zero_gravi
      when BRANCH => -- update PC for taken branches and jumps
896
      -- ------------------------------------------------------------
897
        if (execute_engine.is_jump = '1') or (execute_engine.branch_taken = '1') then
898
          execute_engine.pc_nxt <= alu_add_i(data_width_c-1 downto 1) & '0';
899
          fetch_engine.reset    <= '1';
900
        end if;
901 7 zero_gravi
        execute_engine.state_nxt <= SYS_WAIT;
902 6 zero_gravi
 
903
      when LOAD => -- trigger memory read request
904
      -- ------------------------------------------------------------
905
        ctrl_nxt(ctrl_bus_rd_c)  <= '1';--bus_fast_rd <= '1'; -- fast read request
906
        execute_engine.state_nxt <= LOADSTORE_0;
907
 
908
      when STORE => -- trigger memory write request
909
      -- ------------------------------------------------------------
910
        ctrl_nxt(ctrl_bus_wr_c)  <= '1';--bus_fast_wr <= '1'; -- fast write request
911
        execute_engine.state_nxt <= LOADSTORE_0;
912
 
913
      when LOADSTORE_0 => -- memory latency
914
      -- ------------------------------------------------------------
915
        ctrl_nxt(ctrl_bus_mdi_we_c) <= '1'; -- write input data to MDI (only relevant for LOAD)
916
        execute_engine.state_nxt <= LOADSTORE_1;
917
 
918
      when LOADSTORE_1 => -- wait for bus transaction to finish
919
      -- ------------------------------------------------------------
920
        ctrl_nxt(ctrl_bus_mdi_we_c) <= '1'; -- keep writing input data to MDI (only relevant for LOAD)
921
        ctrl_nxt(ctrl_rf_in_mux_msb_c downto ctrl_rf_in_mux_lsb_c) <= "01"; -- RF input = memory input (only relevant for LOAD)
922
        if (ma_load_i = '1') or (be_load_i = '1') or (ma_store_i = '1') or (be_store_i = '1') then -- abort if exception
923 7 zero_gravi
          execute_engine.state_nxt <= SYS_WAIT;
924 6 zero_gravi
        elsif (bus_wait_i = '0') then -- wait here for bus to finish transaction
925
          if (execute_engine.i_reg(instr_opcode_msb_c downto instr_opcode_lsb_c) = opcode_load_c) then -- LOAD?
926
            ctrl_nxt(ctrl_rf_wb_en_c) <= '1'; -- valid RF write-back
927
          end if;
928
          execute_engine.state_nxt <= DISPATCH;
929
        end if;
930
 
931 2 zero_gravi
      when others => -- undefined
932
      -- ------------------------------------------------------------
933 7 zero_gravi
        execute_engine.state_nxt <= SYS_WAIT;
934 2 zero_gravi
 
935
    end case;
936 6 zero_gravi
  end process execute_engine_fsm_comb;
937 2 zero_gravi
 
938
 
939
  -- Illegal Instruction Check --------------------------------------------------------------
940
  -- -------------------------------------------------------------------------------------------
941 6 zero_gravi
  illegal_instruction_check: process(execute_engine, csr, ctrl_nxt, ci_illegal)
942 2 zero_gravi
  begin
943 6 zero_gravi
    if (execute_engine.state = EXECUTE) then
944 2 zero_gravi
      -- defaults --
945
      illegal_instruction <= '0';
946
      illegal_register    <= '0';
947
      illegal_compressed  <= '0';
948
 
949
      -- check if using reg >= 16 for E-CPUs --
950 6 zero_gravi
--if (CPU_EXTENSION_RISCV_E = true) then
951
--  illegal_register <= ctrl_nxt(ctrl_rf_rd_adr4_c) or ctrl_nxt(ctrl_rf_rs2_adr4_c) or ctrl_nxt(ctrl_rf_rs1_adr4_c);
952
--else
953
--  illegal_register <= '0';
954
--end if;
955 2 zero_gravi
 
956
      -- check instructions --
957 6 zero_gravi
      case execute_engine.i_reg(instr_opcode_msb_c downto instr_opcode_lsb_c) is
958 2 zero_gravi
 
959
        -- OPCODE check sufficient: LUI, UIPC, JAL --
960
        when opcode_lui_c | opcode_auipc_c | opcode_jal_c =>
961
          illegal_instruction <= '0';
962
 
963
        when opcode_alui_c => -- check ALUI funct7
964 6 zero_gravi
          if ((execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sll_c) and
965
              (execute_engine.i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c) /= "0000000")) or -- shift logical left
966
             ((execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sr_c) and
967
              ((execute_engine.i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c) /= "0000000") and
968
               (execute_engine.i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c) /= "0100000"))) then -- shift right
969 2 zero_gravi
            illegal_instruction <= '1';
970
          else
971
            illegal_instruction <= '0';
972
          end if;
973
 
974
        when opcode_load_c => -- check LOAD funct3
975 6 zero_gravi
          if (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_lb_c) or
976
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_lh_c) or
977
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_lw_c) or
978
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_lbu_c) or
979
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_lhu_c) then
980 2 zero_gravi
            illegal_instruction <= '0';
981
          else
982
            illegal_instruction <= '1';
983
          end if;
984
 
985
        when opcode_store_c => -- check STORE funct3
986 6 zero_gravi
          if (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sb_c) or
987
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sh_c) or
988
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sw_c) then
989 2 zero_gravi
            illegal_instruction <= '0';
990
          else
991
            illegal_instruction <= '1';
992
          end if;
993
 
994
        when opcode_branch_c => -- check BRANCH funct3
995 6 zero_gravi
          if (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_beq_c) or
996
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_bne_c) or
997
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_blt_c) or
998
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_bge_c) or
999
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_bltu_c) or
1000
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_bgeu_c) then
1001 2 zero_gravi
            illegal_instruction <= '0';
1002
          else
1003
            illegal_instruction <= '1';
1004
          end if;
1005
 
1006
        when opcode_jalr_c => -- check JALR funct3
1007 6 zero_gravi
          if (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = "000") then
1008 2 zero_gravi
            illegal_instruction <= '0';
1009
          else
1010
            illegal_instruction <= '1';
1011
          end if;
1012
 
1013
        when opcode_alu_c => -- check ALU funct3 & funct7
1014 6 zero_gravi
          if (execute_engine.i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c) = "0000001") then -- MULDIV
1015
            if (CPU_EXTENSION_RISCV_M = false) or (csr.misa_m_en = '0') then -- not implemented or disabled
1016 2 zero_gravi
              illegal_instruction <= '1';
1017
            end if;
1018 6 zero_gravi
          elsif ((execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_subadd_c) or
1019
                 (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sr_c)) and -- ADD/SUB or SRA/SRL check
1020
                ((execute_engine.i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c) /= "0000000") and
1021
                 (execute_engine.i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c) /= "0100000")) then -- ADD/SUB or SRA/SRL select
1022 2 zero_gravi
            illegal_instruction <= '1';
1023
          else
1024
            illegal_instruction <= '0';
1025
          end if;
1026
 
1027
        when opcode_syscsr_c => -- check system instructions --
1028
          -- CSR access --
1029 6 zero_gravi
          if (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrw_c) or
1030
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrs_c) or
1031
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrc_c) or
1032
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrwi_c) or
1033
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrsi_c) or
1034
             (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrci_c) then
1035 2 zero_gravi
            -- valid CSR? --
1036 6 zero_gravi
            if (execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"300") or -- mstatus
1037
               (execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"301") or -- misa
1038
               (execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"304") or -- mie
1039
               (execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"305") or -- mtvev
1040
               (execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"340") or -- mscratch
1041
               (execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"341") or -- mepc
1042
               (execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"342") or -- mcause
1043
               (execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"343") or -- mtval
1044
               (execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"344") or -- mip
1045 2 zero_gravi
               --
1046 6 zero_gravi
               ((execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"c00") and (CPU_EXTENSION_RISCV_E = false) and (CSR_COUNTERS_USE = true)) or -- cycle
1047
               ((execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"c01") and (CPU_EXTENSION_RISCV_E = false) and (CSR_COUNTERS_USE = true)) or -- time
1048
               ((execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"c02") and (CPU_EXTENSION_RISCV_E = false) and (CSR_COUNTERS_USE = true)) or -- instret
1049
               ((execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"c80") and (CPU_EXTENSION_RISCV_E = false) and (CSR_COUNTERS_USE = true)) or -- cycleh
1050
               ((execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"c81") and (CPU_EXTENSION_RISCV_E = false) and (CSR_COUNTERS_USE = true)) or -- timeh
1051
               ((execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"c82") and (CPU_EXTENSION_RISCV_E = false) and (CSR_COUNTERS_USE = true)) or -- instreth
1052 2 zero_gravi
               --
1053 6 zero_gravi
               ((execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"b00") and (CPU_EXTENSION_RISCV_E = false) and (CSR_COUNTERS_USE = true)) or -- mcycle
1054
               ((execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"b02") and (CPU_EXTENSION_RISCV_E = false) and (CSR_COUNTERS_USE = true)) or -- minstret
1055
               ((execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"b80") and (CPU_EXTENSION_RISCV_E = false) and (CSR_COUNTERS_USE = true)) or -- mcycleh
1056
               ((execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"b82") and (CPU_EXTENSION_RISCV_E = false) and (CSR_COUNTERS_USE = true)) or -- minstreth
1057 2 zero_gravi
               --
1058 6 zero_gravi
               (execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"f13") or -- mimpid
1059
               (execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"f14") or -- mhartid
1060 2 zero_gravi
               --
1061 6 zero_gravi
               (execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"fc0") or -- mfeatures
1062
               (execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"fc1") or -- mclock
1063
               (execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"fc4") or -- mispacebase
1064
               (execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"fc5") or -- mispacesize
1065
               (execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"fc6") or -- mdspacebase
1066
               (execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"fc7") then -- mdspacesize
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
            if (execute_engine.i_reg(instr_funct12_msb_c  downto instr_funct12_lsb_c) = "000000000000") or -- ECALL
1076
               (execute_engine.i_reg(instr_funct12_msb_c  downto instr_funct12_lsb_c) = "000000000001") or -- EBREAK 
1077
               (execute_engine.i_reg(instr_funct12_msb_c  downto instr_funct12_lsb_c) = "001100000010") or -- MRET
1078
               (execute_engine.i_reg(instr_funct12_msb_c  downto instr_funct12_lsb_c) = "000100000101") 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 6 zero_gravi
          else -- compressed instruction: illegal or disabled / not implemented
1091
            illegal_compressed <= ci_illegal or (not csr.misa_c_en);
1092 2 zero_gravi
          end if;
1093
 
1094
      end case;
1095
    else
1096
      illegal_instruction <= '0';
1097
      illegal_register    <= '0';
1098
      illegal_compressed  <= '0';
1099
    end if;
1100
  end process illegal_instruction_check;
1101
 
1102
  -- any illegal condition? --
1103 6 zero_gravi
  trap_ctrl.instr_il <= illegal_instruction or illegal_register or illegal_compressed;
1104 2 zero_gravi
 
1105
 
1106 6 zero_gravi
-- ****************************************************************************************************************************
1107
-- Exception and Interrupt Control
1108
-- ****************************************************************************************************************************
1109 2 zero_gravi
 
1110
 
1111 6 zero_gravi
  -- Trap Controller ------------------------------------------------------------------------
1112 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
1113 6 zero_gravi
  trap_controller: process(rstn_i, clk_i)
1114 2 zero_gravi
  begin
1115
    if (rstn_i = '0') then
1116 6 zero_gravi
      trap_ctrl.exc_buf   <= (others => '0');
1117
      trap_ctrl.irq_buf   <= (others => '0');
1118
      trap_ctrl.exc_ack   <= '0';
1119
      trap_ctrl.irq_ack   <= (others => '0');
1120
      trap_ctrl.cause     <= (others => '0');
1121
      trap_ctrl.exc_src   <= (others => '0');
1122
      trap_ctrl.env_start <= '0';
1123 2 zero_gravi
    elsif rising_edge(clk_i) then
1124
      if (CPU_EXTENSION_RISCV_Zicsr = true) then
1125
        -- exception buffer: misaligned load/store/instruction address
1126 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);
1127
        trap_ctrl.exc_buf(exception_salign_c)    <= (trap_ctrl.exc_buf(exception_salign_c)    or ma_store_i)            and (not trap_ctrl.exc_ack);
1128
        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);
1129 2 zero_gravi
        -- exception buffer: load/store/instruction bus access error
1130 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);
1131
        trap_ctrl.exc_buf(exception_saccess_c)   <= (trap_ctrl.exc_buf(exception_saccess_c)   or be_store_i)            and (not trap_ctrl.exc_ack);
1132
        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);
1133 2 zero_gravi
        -- exception buffer: illegal instruction / env call / break point
1134 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);
1135
        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);
1136
        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);
1137 2 zero_gravi
        -- interrupt buffer: machine software/external/timer interrupt
1138 6 zero_gravi
        trap_ctrl.irq_buf(interrupt_msw_irq_c)   <= csr.mie_msie and (trap_ctrl.irq_buf(interrupt_msw_irq_c)   or csr.mip_msip) and (not trap_ctrl.irq_ack(interrupt_msw_irq_c));
1139
        trap_ctrl.irq_buf(interrupt_mext_irq_c)  <= csr.mie_meie and (trap_ctrl.irq_buf(interrupt_mext_irq_c)  or clic_irq_i)   and (not trap_ctrl.irq_ack(interrupt_mext_irq_c));
1140
        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));
1141 2 zero_gravi
 
1142 6 zero_gravi
        -- trap control --
1143
        if (trap_ctrl.env_start = '0') then -- no started trap handler
1144
          if (trap_ctrl.exc_fire = '1') or ((trap_ctrl.irq_fire = '1') and
1145
             ((execute_engine.state = EXECUTE) or (execute_engine.state = TRAP))) then -- exception/IRQ detected!
1146 7 zero_gravi
            trap_ctrl.cause     <= trap_ctrl.cause_nxt;   -- capture source ID for program
1147
            trap_ctrl.exc_src   <= trap_ctrl.exc_buf;     -- capture exception source for hardware
1148
            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
            trap_ctrl.env_start <= '1';                   -- now we want to start the 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 6 zero_gravi
  trap_ctrl.exc_fire <= or_all_f(trap_ctrl.exc_buf); -- classic exceptions (faults/traps) cannot be masked
1165
  trap_ctrl.irq_fire <= or_all_f(trap_ctrl.irq_buf) and csr.mstatus_mie; -- classic interrupts can be enabled/disabled
1166 2 zero_gravi
 
1167
  -- exception acknowledge for bus unit --
1168 6 zero_gravi
  bus_exc_ack_o <= trap_ctrl.env_start_ack or fetch_engine.bus_err_ack;
1169 2 zero_gravi
 
1170 6 zero_gravi
  -- exception/interrupt/status ID visible for program --
1171
  csr.mcause <= trap_ctrl.cause;
1172
 
1173
 
1174
  -- Trap Priority Detector -----------------------------------------------------------------
1175
  -- -------------------------------------------------------------------------------------------
1176
  trap_priority: process(trap_ctrl)
1177 2 zero_gravi
  begin
1178
    -- defaults --
1179 6 zero_gravi
    trap_ctrl.cause_nxt   <= (others => '0');
1180
    trap_ctrl.irq_ack_nxt <= (others => '0');
1181 2 zero_gravi
 
1182
    -- interrupt: 1.11 machine external interrupt --
1183 6 zero_gravi
    if (trap_ctrl.irq_buf(interrupt_mext_irq_c) = '1') then
1184
      trap_ctrl.cause_nxt(data_width_c-1) <= '1';
1185
      trap_ctrl.cause_nxt(3 downto 0) <= "1011";
1186
      trap_ctrl.irq_ack_nxt(interrupt_mext_irq_c) <= '1';
1187 2 zero_gravi
 
1188
    -- interrupt: 1.7 machine timer interrupt --
1189 6 zero_gravi
    elsif (trap_ctrl.irq_buf(interrupt_mtime_irq_c) = '1') then
1190
      trap_ctrl.cause_nxt(data_width_c-1) <= '1';
1191
      trap_ctrl.cause_nxt(3 downto 0) <= "0111";
1192
      trap_ctrl.irq_ack_nxt(interrupt_mtime_irq_c) <= '1';
1193 2 zero_gravi
 
1194
    -- interrupt: 1.3 machine SW interrupt --
1195 6 zero_gravi
    elsif (trap_ctrl.irq_buf(interrupt_msw_irq_c) = '1') then
1196
      trap_ctrl.cause_nxt(data_width_c-1) <= '1';
1197
      trap_ctrl.cause_nxt(3 downto 0) <= "0011";
1198
      trap_ctrl.irq_ack_nxt(interrupt_msw_irq_c) <= '1';
1199 2 zero_gravi
 
1200
 
1201 4 zero_gravi
    -- the following traps are caused by synchronous exceptions
1202
    -- here we do not need an acknowledge mask since only one exception can trigger at the same time
1203
 
1204 2 zero_gravi
    -- trap/fault: 0.0 instruction address misaligned --
1205 6 zero_gravi
    elsif (trap_ctrl.exc_buf(exception_ialign_c) = '1') then
1206
      trap_ctrl.cause_nxt(data_width_c-1) <= '0';
1207
      trap_ctrl.cause_nxt(3 downto 0) <= "0000";
1208 2 zero_gravi
 
1209
    -- trap/fault: 0.1 instruction access fault --
1210 6 zero_gravi
    elsif (trap_ctrl.exc_buf(exception_iaccess_c) = '1') then
1211
      trap_ctrl.cause_nxt(data_width_c-1) <= '0';
1212
      trap_ctrl.cause_nxt(3 downto 0) <= "0001";
1213 2 zero_gravi
 
1214
    -- trap/fault: 0.2 illegal instruction --
1215 6 zero_gravi
    elsif (trap_ctrl.exc_buf(exception_iillegal_c) = '1') then
1216
      trap_ctrl.cause_nxt(data_width_c-1) <= '0';
1217
      trap_ctrl.cause_nxt(3 downto 0) <= "0010";
1218 2 zero_gravi
 
1219
 
1220
    -- trap/fault: 0.11 environment call from M-mode --
1221 6 zero_gravi
    elsif (trap_ctrl.exc_buf(exception_m_envcall_c) = '1') then
1222
      trap_ctrl.cause_nxt(data_width_c-1) <= '0';
1223
      trap_ctrl.cause_nxt(3 downto 0) <= "1011";
1224 2 zero_gravi
 
1225
    -- trap/fault: 0.3 breakpoint --
1226 6 zero_gravi
    elsif (trap_ctrl.exc_buf(exception_break_c) = '1') then
1227
      trap_ctrl.cause_nxt(data_width_c-1) <= '0';
1228
      trap_ctrl.cause_nxt(3 downto 0) <= "0011";
1229 2 zero_gravi
 
1230
 
1231
    -- trap/fault: 0.6 store address misaligned -
1232 6 zero_gravi
    elsif (trap_ctrl.exc_buf(exception_salign_c) = '1') then
1233
      trap_ctrl.cause_nxt(data_width_c-1) <= '0';
1234
      trap_ctrl.cause_nxt(3 downto 0) <= "0110";
1235 2 zero_gravi
 
1236
    -- trap/fault: 0.4 load address misaligned --
1237 6 zero_gravi
    elsif (trap_ctrl.exc_buf(exception_lalign_c) = '1') then
1238
      trap_ctrl.cause_nxt(data_width_c-1) <= '0';
1239
      trap_ctrl.cause_nxt(3 downto 0) <= "0100";
1240 2 zero_gravi
 
1241
    -- trap/fault: 0.7 store access fault --
1242 6 zero_gravi
    elsif (trap_ctrl.exc_buf(exception_saccess_c) = '1') then
1243
      trap_ctrl.cause_nxt(data_width_c-1) <= '0';
1244
      trap_ctrl.cause_nxt(3 downto 0) <= "0111";
1245 2 zero_gravi
 
1246
    -- trap/fault: 0.5 load access fault --
1247 6 zero_gravi
    elsif (trap_ctrl.exc_buf(exception_laccess_c) = '1') then
1248
      trap_ctrl.cause_nxt(data_width_c-1) <= '0';
1249
      trap_ctrl.cause_nxt(3 downto 0) <= "0101";
1250 2 zero_gravi
 
1251
    -- undefined / not implemented --
1252
    else
1253 6 zero_gravi
      trap_ctrl.cause_nxt   <= (others => '0');
1254
      trap_ctrl.irq_ack_nxt <= (others => '0');
1255 2 zero_gravi
    end if;
1256 6 zero_gravi
  end process trap_priority;
1257
 
1258 2 zero_gravi
 
1259 6 zero_gravi
-- ****************************************************************************************************************************
1260
-- Control and Status Registers (CSRs)
1261
-- ****************************************************************************************************************************
1262 2 zero_gravi
 
1263 6 zero_gravi
  -- CSR CPU Access -------------------------------------------------------------------------
1264
  -- -------------------------------------------------------------------------------------------
1265
  csr_cpu_acc: process(clk_i)
1266
  begin
1267
    if rising_edge(clk_i) then
1268
      csr.we <= csr.we_nxt;
1269
      csr.re <= csr.re_nxt;
1270
    end if;
1271
  end process csr_cpu_acc;
1272
 
1273
 
1274 2 zero_gravi
  -- Control and Status Registers Write Access ----------------------------------------------
1275
  -- -------------------------------------------------------------------------------------------
1276
  csr_write_access: process(rstn_i, clk_i)
1277
  begin
1278
    if (rstn_i = '0') then
1279 6 zero_gravi
      csr.mstatus_mie  <= '0';
1280
      csr.mstatus_mpie <= '0';
1281
      csr.mie_msie     <= '0';
1282
      csr.mie_meie     <= '0';
1283
      csr.mie_mtie     <= '0';
1284
      csr.mtvec        <= (others => '0');
1285
      csr.mtval        <= (others => '0');
1286
      csr.mepc         <= (others => '0');
1287
      csr.mip_msip     <= '0';
1288
      csr.misa_c_en    <= bool_to_ulogic_f(CPU_EXTENSION_RISCV_C); -- C CPU extension
1289
      csr.misa_m_en    <= bool_to_ulogic_f(CPU_EXTENSION_RISCV_M); -- M CPU extension
1290 2 zero_gravi
    elsif rising_edge(clk_i) then
1291
      if (CPU_EXTENSION_RISCV_Zicsr = true) then
1292 6 zero_gravi
        csr.mip_msip <= '0';
1293 4 zero_gravi
 
1294 2 zero_gravi
        -- register that can be modified by user --
1295 6 zero_gravi
        if (csr.we = '1') then -- manual update
1296 4 zero_gravi
 
1297
          -- machine trap setup --
1298 6 zero_gravi
          if (execute_engine.i_reg(31 downto 24) = x"30") then
1299
            if (execute_engine.i_reg(23 downto 20) = x"0") then -- R/W: mstatus - machine status register
1300
              csr.mstatus_mie  <= csr_wdata_i(03);
1301
              csr.mstatus_mpie <= csr_wdata_i(07);
1302 4 zero_gravi
            end if;
1303 6 zero_gravi
            if (execute_engine.i_reg(23 downto 20) = x"1") then -- R/W: misa - machine instruction set extensions
1304
              csr.misa_c_en <= csr_wdata_i(02); -- C extension enable/disable
1305
              csr.misa_m_en <= csr_wdata_i(12); -- M extension enable/disable
1306 4 zero_gravi
            end if;
1307 6 zero_gravi
            if (execute_engine.i_reg(23 downto 20) = x"4") then -- R/W: mie - machine interrupt-enable register
1308
              csr.mie_msie <= csr_wdata_i(03); -- SW IRQ enable
1309
              csr.mie_mtie <= csr_wdata_i(07); -- TIMER IRQ enable
1310
              csr.mie_meie <= csr_wdata_i(11); -- EXT IRQ enable
1311 4 zero_gravi
            end if;
1312 6 zero_gravi
            if (execute_engine.i_reg(23 downto 20) = x"5") then -- R/W: mtvec - machine trap-handler base address (for ALL exceptions)
1313
              csr.mtvec <= csr_wdata_i;
1314
            end if;
1315 4 zero_gravi
          end if;
1316
 
1317
          -- machine trap handling --
1318 6 zero_gravi
          if (execute_engine.i_reg(31 downto 24) = x"34") then
1319
            if (execute_engine.i_reg(23 downto 20) = x"0") then -- R/W: mscratch - machine scratch register
1320
              csr.mscratch <= csr_wdata_i;
1321 4 zero_gravi
            end if;
1322 6 zero_gravi
            if (execute_engine.i_reg(23 downto 20) = x"1") then-- R/W: mepc - machine exception program counter
1323
              csr.mepc <= csr_wdata_i;
1324 4 zero_gravi
            end if;
1325 6 zero_gravi
            if (execute_engine.i_reg(23 downto 20) = x"4") then -- R/W: mip - machine interrupt pending
1326
              csr.mip_msip <= csr_wdata_i(03); -- manual SW IRQ trigger
1327 4 zero_gravi
            end if;
1328
          end if;
1329 2 zero_gravi
 
1330
        else -- automatic update by hardware
1331
          -- machine exception PC & exception value register --
1332 6 zero_gravi
          if (trap_ctrl.env_start_ack = '1') then -- trap handler started?
1333
            if (csr.mcause(data_width_c-1) = '1') then -- for INTERRUPTS only (mtval not defined for interrupts)
1334
              csr.mepc  <= execute_engine.pc(data_width_c-1 downto 1) & '0'; -- this is the CURRENT pc = interrupted instruction
1335
              csr.mtval <= (others => '0');
1336
            else -- for EXCEPTIONs
1337
              csr.mepc <= execute_engine.last_pc(data_width_c-1 downto 1) & '0'; -- this is the LAST pc = last executed instruction
1338
              if ((trap_ctrl.exc_src(exception_iaccess_c) or trap_ctrl.exc_src(exception_ialign_c)) = '1') then -- instruction access error OR misaligned instruction
1339
                csr.mtval <= execute_engine.pc(data_width_c-1 downto 1) & '0';
1340
              elsif (trap_ctrl.exc_src(exception_iillegal_c) = '1') then -- illegal instruction
1341
                csr.mtval <= execute_engine.i_reg;
1342
              else -- everything else
1343
              --elsif ((trap_ctrl.exc_src(exception_lalign_c)  or trap_ctrl.exc_src(exception_salign_c) or
1344
              --        trap_ctrl.exc_src(exception_laccess_c) or trap_ctrl.exc_src(exception_saccess_c)) = '1') then -- load/store misaligned / access error
1345
                csr.mtval <= mar_i;
1346 2 zero_gravi
              end if;
1347
            end if;
1348
          end if;
1349
 
1350
          -- context switch in mstatus --
1351 6 zero_gravi
          if (trap_ctrl.env_start_ack = '1') then -- actually entering trap
1352
            csr.mstatus_mie <= '0';
1353
            if (csr.mstatus_mpie = '0') then -- FIXME: prevent loosing the prev MIE state after several traps
1354
              csr.mstatus_mpie <= csr.mstatus_mie;
1355 2 zero_gravi
            end if;
1356 6 zero_gravi
          elsif (trap_ctrl.env_end = '1') then -- return from exception
1357
            csr.mstatus_mie <= csr.mstatus_mpie;
1358 2 zero_gravi
          end if;
1359
        end if;
1360
      end if;
1361
    end if;
1362
  end process csr_write_access;
1363
 
1364
 
1365
  -- Control and Status Registers Read Access -----------------------------------------------
1366
  -- -------------------------------------------------------------------------------------------
1367
  csr_read_access: process(clk_i)
1368
  begin
1369
    if rising_edge(clk_i) then
1370
      csr_rdata_o <= (others => '0'); -- default
1371
      if (CPU_EXTENSION_RISCV_Zicsr = true) then -- implement CSR access at all?
1372 6 zero_gravi
        if (csr.re = '1') then
1373
          case execute_engine.i_reg(31 downto 20) is
1374 2 zero_gravi
            -- machine trap setup --
1375
            when x"300" => -- R/W: mstatus - machine status register
1376 6 zero_gravi
              csr_rdata_o(03) <= csr.mstatus_mie; -- MIE
1377
              csr_rdata_o(07) <= csr.mstatus_mpie; -- MPIE
1378 2 zero_gravi
              csr_rdata_o(11) <= '1'; -- MPP low
1379
              csr_rdata_o(12) <= '1'; -- MPP high
1380 6 zero_gravi
            when x"301" => -- R/W: misa - ISA and extensions
1381
              csr_rdata_o(02) <= csr.misa_c_en;                               -- C CPU extension
1382 2 zero_gravi
              csr_rdata_o(04) <= bool_to_ulogic_f(CPU_EXTENSION_RISCV_E);     -- E CPU extension
1383
              csr_rdata_o(08) <= not bool_to_ulogic_f(CPU_EXTENSION_RISCV_E); -- I CPU extension (if not E)
1384 6 zero_gravi
              csr_rdata_o(12) <= csr.misa_m_en;                               -- M CPU extension
1385 2 zero_gravi
              csr_rdata_o(23) <= '1';                                         -- X CPU extension: non-standard extensions
1386
              csr_rdata_o(25) <= bool_to_ulogic_f(CPU_EXTENSION_RISCV_Zicsr); -- Z CPU extension
1387
              csr_rdata_o(30) <= '1'; -- 32-bit architecture (MXL lo)
1388
              csr_rdata_o(31) <= '0'; -- 32-bit architecture (MXL hi)
1389
            when x"304" => -- R/W: mie - machine interrupt-enable register
1390 6 zero_gravi
              csr_rdata_o(03) <= csr.mie_msie; -- software IRQ enable
1391
              csr_rdata_o(07) <= csr.mie_mtie; -- timer IRQ enable
1392
              csr_rdata_o(11) <= csr.mie_meie; -- external IRQ enable
1393 2 zero_gravi
            when x"305" => -- R/W: mtvec - machine trap-handler base address (for ALL exceptions)
1394 6 zero_gravi
              csr_rdata_o <= csr.mtvec;
1395 2 zero_gravi
            -- machine trap handling --
1396
            when x"340" => -- R/W: mscratch - machine scratch register
1397 6 zero_gravi
              csr_rdata_o <= csr.mscratch;
1398 2 zero_gravi
            when x"341" => -- R/W: mepc - machine exception program counter
1399 6 zero_gravi
              csr_rdata_o <= csr.mepc;
1400 2 zero_gravi
            when x"342" => -- R/-: mcause - machine trap cause
1401 6 zero_gravi
              csr_rdata_o <= csr.mcause;
1402 2 zero_gravi
            when x"343" => -- R/-: mtval - machine bad address or instruction
1403 6 zero_gravi
              csr_rdata_o <= csr.mtval;
1404 2 zero_gravi
            when x"344" => -- R/W: mip - machine interrupt pending
1405 6 zero_gravi
              csr_rdata_o(03) <= trap_ctrl.irq_buf(interrupt_msw_irq_c);
1406
              csr_rdata_o(07) <= trap_ctrl.irq_buf(interrupt_mtime_irq_c);
1407
              csr_rdata_o(11) <= trap_ctrl.irq_buf(interrupt_mext_irq_c);
1408 2 zero_gravi
            -- counter and timers --
1409
            when x"c00" | x"c01" | x"b00" => -- R/-: cycle/time/mcycle: Cycle counter LOW / Timer LOW
1410 6 zero_gravi
              csr_rdata_o <= csr.cycle(31 downto 0);
1411 2 zero_gravi
            when x"c02" | x"b02" => -- R/-: instret/minstret: Instructions-retired counter LOW
1412 6 zero_gravi
              csr_rdata_o <= csr.instret(31 downto 0);
1413 2 zero_gravi
            when x"c80" | x"c81" | x"b80" => -- R/-: cycleh/timeh/mcycleh: Cycle counter HIGH / Timer HIGH
1414 6 zero_gravi
              csr_rdata_o <= csr.cycleh;
1415 2 zero_gravi
            when x"c82" | x"b82" => -- R/-: instreth/minstreth: Instructions-retired counter HIGH
1416 6 zero_gravi
              csr_rdata_o <= csr.instreth;
1417 2 zero_gravi
            -- machine information registers --
1418
            when x"f13" => -- R/-: mimpid - implementation ID / version
1419
              csr_rdata_o <= hw_version_c;
1420
            when x"f14" => -- R/-: mhartid - hardware thread ID
1421
              csr_rdata_o <= HART_ID;
1422
            -- CUSTOM read-only machine CSRs --
1423
            when x"fc0" => -- R/-: mfeatures - implemented processor devices/features
1424
              csr_rdata_o(00) <= bool_to_ulogic_f(BOOTLOADER_USE);   -- implement processor-internal bootloader?
1425
              csr_rdata_o(01) <= bool_to_ulogic_f(MEM_EXT_USE);      -- implement external memory bus interface?
1426 6 zero_gravi
              csr_rdata_o(02) <= bool_to_ulogic_f(MEM_INT_IMEM_USE); -- implement processor-internal instruction memory?
1427
              csr_rdata_o(03) <= bool_to_ulogic_f(MEM_INT_IMEM_ROM); -- implement processor-internal instruction memory as ROM?
1428
              csr_rdata_o(04) <= bool_to_ulogic_f(MEM_INT_DMEM_USE); -- implement processor-internal data memory?
1429
              csr_rdata_o(05) <= bool_to_ulogic_f(CSR_COUNTERS_USE); -- implement RISC-V (performance) counter?
1430 2 zero_gravi
              --
1431 3 zero_gravi
              csr_rdata_o(16) <= bool_to_ulogic_f(IO_GPIO_USE);      -- implement general purpose input/output port unit (GPIO)?
1432
              csr_rdata_o(17) <= bool_to_ulogic_f(IO_MTIME_USE);     -- implement machine system timer (MTIME)?
1433
              csr_rdata_o(18) <= bool_to_ulogic_f(IO_UART_USE);      -- implement universal asynchronous receiver/transmitter (UART)?
1434
              csr_rdata_o(19) <= bool_to_ulogic_f(IO_SPI_USE);       -- implement serial peripheral interface (SPI)?
1435
              csr_rdata_o(20) <= bool_to_ulogic_f(IO_TWI_USE);       -- implement two-wire interface (TWI)?
1436
              csr_rdata_o(21) <= bool_to_ulogic_f(IO_PWM_USE);       -- implement pulse-width modulation unit (PWM)?
1437
              csr_rdata_o(22) <= bool_to_ulogic_f(IO_WDT_USE);       -- implement watch dog timer (WDT)?
1438
              csr_rdata_o(23) <= bool_to_ulogic_f(IO_CLIC_USE);      -- implement core local interrupt controller (CLIC)?
1439
              csr_rdata_o(24) <= bool_to_ulogic_f(IO_TRNG_USE);      -- implement true random number generator (TRNG)?
1440
              csr_rdata_o(25) <= bool_to_ulogic_f(IO_DEVNULL_USE);   -- implement dummy device (DEVNULL)?
1441 2 zero_gravi
            when x"fc1" => -- R/-: mclock - processor clock speed
1442
              csr_rdata_o <= std_ulogic_vector(to_unsigned(CLOCK_FREQUENCY, 32));
1443
            when x"fc4" => -- R/-: mispacebase - Base address of instruction memory space
1444
              csr_rdata_o <= MEM_ISPACE_BASE;
1445
            when x"fc5" => -- R/-: mdspacebase - Base address of data memory space
1446
              csr_rdata_o <= MEM_DSPACE_BASE;
1447
            when x"fc6" => -- R/-: mispacesize - Total size of instruction memory space in byte
1448
              csr_rdata_o <= std_ulogic_vector(to_unsigned(MEM_ISPACE_SIZE, 32));
1449
            when x"fc7" => -- R/-: mdspacesize - Total size of data memory space in byte
1450
              csr_rdata_o <= std_ulogic_vector(to_unsigned(MEM_DSPACE_SIZE, 32));
1451
            -- undefined/unavailable --
1452
            when others =>
1453
              csr_rdata_o <= (others => '0'); -- not implemented (yet)
1454
          end case;
1455
        end if;
1456
      end if;
1457
    end if;
1458
  end process csr_read_access;
1459
 
1460
 
1461 6 zero_gravi
  -- RISC-V Counter CSRs --------------------------------------------------------------------
1462 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
1463
  csr_counters: process(rstn_i, clk_i)
1464
  begin
1465 6 zero_gravi
    if (rstn_i = '0') then
1466
      csr.cycle    <= (others => '0');
1467
      csr.instret  <= (others => '0');
1468
      csr.cycleh   <= (others => '0');
1469
      csr.instreth <= (others => '0');
1470
      cycle_msb    <= '0';
1471
      instret_msb  <= '0';
1472
    elsif rising_edge(clk_i) then
1473
      if (CPU_EXTENSION_RISCV_E = false) and (CSR_COUNTERS_USE = true) then
1474 2 zero_gravi
        -- low word overflow buffers --
1475 6 zero_gravi
        cycle_msb   <= csr.cycle(csr.cycle'left);
1476
        instret_msb <= csr.instret(csr.instret'left);
1477 2 zero_gravi
        -- low word counters --
1478 6 zero_gravi
        csr.cycle <= std_ulogic_vector(unsigned(csr.cycle) + 1);
1479
        if (execute_engine.state_prev /= EXECUTE) and (execute_engine.state = EXECUTE) then
1480
          csr.instret <= std_ulogic_vector(unsigned(csr.instret) + 1);
1481 2 zero_gravi
        end if;
1482
        -- high word counters --
1483 6 zero_gravi
        if ((cycle_msb xor csr.cycle(csr.cycle'left)) = '1') then
1484
          csr.cycleh <= std_ulogic_vector(unsigned(csr.cycleh) + 1);
1485 2 zero_gravi
        end if;
1486 6 zero_gravi
        if ((instret_msb xor csr.instret(csr.instret'left)) = '1') then
1487
          csr.instreth <= std_ulogic_vector(unsigned(csr.instreth) + 1);
1488 2 zero_gravi
        end if;
1489
      end if;
1490
    end if;
1491
  end process csr_counters;
1492
 
1493
 
1494
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.