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

Subversion Repositories neorv32

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

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

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