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

Subversion Repositories neorv32

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

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