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

Subversion Repositories neorv32

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

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
-- # FSM to control CPU operations. This unit also includes the control and status registers (CSR) #
5
-- # and the interrupt and exception controller.                                                   #
6
-- # ********************************************************************************************* #
7
-- # BSD 3-Clause License                                                                          #
8
-- #                                                                                               #
9
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved.                                     #
10
-- #                                                                                               #
11
-- # Redistribution and use in source and binary forms, with or without modification, are          #
12
-- # permitted provided that the following conditions are met:                                     #
13
-- #                                                                                               #
14
-- # 1. Redistributions of source code must retain the above copyright notice, this list of        #
15
-- #    conditions and the following disclaimer.                                                   #
16
-- #                                                                                               #
17
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
18
-- #    conditions and the following disclaimer in the documentation and/or other materials        #
19
-- #    provided with the distribution.                                                            #
20
-- #                                                                                               #
21
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
22
-- #    endorse or promote products derived from this software without specific prior written      #
23
-- #    permission.                                                                                #
24
-- #                                                                                               #
25
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
26
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
27
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
28
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
29
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
30
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
31
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
32
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
33
-- # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
34
-- # ********************************************************************************************* #
35
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
36
-- #################################################################################################
37
 
38
library ieee;
39
use ieee.std_logic_1164.all;
40
use ieee.numeric_std.all;
41
 
42
library neorv32;
43
use neorv32.neorv32_package.all;
44
 
45
entity neorv32_cpu_control is
46
  generic (
47
    -- General --
48
    CLOCK_FREQUENCY           : natural := 0; -- clock frequency of clk_i in Hz
49
    HART_ID                   : std_ulogic_vector(31 downto 0) := x"00000000"; -- custom hardware thread ID
50
    BOOTLOADER_USE            : boolean := true;   -- implement processor-internal bootloader?
51
    -- RISC-V CPU Extensions --
52
    CPU_EXTENSION_RISCV_C     : boolean := false;  -- implement compressed extension?
53
    CPU_EXTENSION_RISCV_E     : boolean := false;  -- implement embedded RF extension?
54
    CPU_EXTENSION_RISCV_M     : boolean := false;  -- implement muld/div extension?
55
    CPU_EXTENSION_RISCV_Zicsr : boolean := true;   -- implement CSR system?
56
    -- Memory configuration: Instruction memory --
57
    MEM_ISPACE_BASE           : std_ulogic_vector(31 downto 0) := x"00000000"; -- base address of instruction memory space
58
    MEM_ISPACE_SIZE           : natural := 8*1024; -- total size of instruction memory space in byte
59
    MEM_INT_IMEM_USE          : boolean := true;   -- implement processor-internal instruction memory
60
    MEM_INT_IMEM_SIZE         : natural := 8*1024; -- size of processor-internal instruction memory in bytes
61
    MEM_INT_IMEM_ROM          : boolean := false;  -- implement processor-internal instruction memory as ROM
62
    -- Memory configuration: Data memory --
63
    MEM_DSPACE_BASE           : std_ulogic_vector(31 downto 0) := x"80000000"; -- base address of data memory space
64
    MEM_DSPACE_SIZE           : natural := 4*1024; -- total size of data memory space in byte
65
    MEM_INT_DMEM_USE          : boolean := true;   -- implement processor-internal data memory
66
    MEM_INT_DMEM_SIZE         : natural := 4*1024; -- size of processor-internal data memory in bytes
67
    -- Memory configuration: External memory interface --
68
    MEM_EXT_USE               : boolean := false;  -- implement external memory bus interface?
69
    -- Processor peripherals --
70
    IO_GPIO_USE               : boolean := true;   -- implement general purpose input/output port unit (GPIO)?
71
    IO_MTIME_USE              : boolean := true;   -- implement machine system timer (MTIME)?
72
    IO_UART_USE               : boolean := true;   -- implement universal asynchronous receiver/transmitter (UART)?
73
    IO_SPI_USE                : boolean := true;   -- implement serial peripheral interface (SPI)?
74
    IO_TWI_USE                : boolean := true;   -- implement two-wire interface (TWI)?
75
    IO_PWM_USE                : boolean := true;   -- implement pulse-width modulation unit (PWM)?
76
    IO_WDT_USE                : boolean := true;   -- implement watch dog timer (WDT)?
77
    IO_CLIC_USE               : boolean := true;   -- implement core local interrupt controller (CLIC)?
78 3 zero_gravi
    IO_TRNG_USE               : boolean := true;   -- implement true random number generator (TRNG)?
79
    IO_DEVNULL_USE            : boolean := true    -- implement dummy device (DEVNULL)?
80 2 zero_gravi
  );
81
  port (
82
    -- global control --
83
    clk_i         : in  std_ulogic; -- global clock, rising edge
84
    rstn_i        : in  std_ulogic; -- global reset, low-active, async
85
    ctrl_o        : out std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
86
    -- status input --
87
    alu_wait_i    : in  std_ulogic; -- wait for ALU
88
    bus_wait_i    : in  std_ulogic; -- wait for bus
89
    -- data input --
90
    instr_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- instruction
91
    cmp_i         : in  std_ulogic_vector(1 downto 0); -- comparator status
92
    alu_add_i     : in  std_ulogic_vector(data_width_c-1 downto 0); -- ALU.add result
93
    -- data output --
94
    imm_o         : out std_ulogic_vector(data_width_c-1 downto 0); -- immediate
95
    pc_o          : out std_ulogic_vector(data_width_c-1 downto 0); -- current PC
96
    alu_pc_o      : out std_ulogic_vector(data_width_c-1 downto 0); -- delayed PC for ALU
97
    -- csr data interface --
98
    csr_wdata_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- CSR write data
99
    csr_rdata_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- CSR read data
100
    -- external interrupt --
101
    clic_irq_i    : in  std_ulogic; -- CLIC interrupt request
102
    mtime_irq_i   : in  std_ulogic; -- machine timer interrupt
103
    -- bus access exceptions --
104
    mar_i         : in  std_ulogic_vector(data_width_c-1 downto 0);  -- memory address register
105
    ma_instr_i    : in  std_ulogic; -- misaligned instruction address
106
    ma_load_i     : in  std_ulogic; -- misaligned load data address
107
    ma_store_i    : in  std_ulogic; -- misaligned store data address
108
    be_instr_i    : in  std_ulogic; -- bus error on instruction access
109
    be_load_i     : in  std_ulogic; -- bus error on load data access
110
    be_store_i    : in  std_ulogic; -- bus error on store data access
111
    bus_exc_ack_o : out std_ulogic  -- bus exception error acknowledge
112
  );
113
end neorv32_cpu_control;
114
 
115
architecture neorv32_cpu_control_rtl of neorv32_cpu_control is
116
 
117
  -- state machine --
118
  type state_t is (IFETCH_0, IFETCH_1, IFETCH_2, IFETCH_3, IFETCH_4, IFETCH_5, IFETCH_6, EXECUTE,
119
                   ALU_WAIT, STORE_0, LOAD_0, LOADSTORE_0, LOADSTORE_1, CSR_ACCESS, SLEEP);
120
  signal state, state_nxt : state_t;
121
  signal ctrl_nxt, ctrl   : std_ulogic_vector(ctrl_width_c-1 downto 0);
122
  signal hw_control       : std_ulogic_vector(data_width_c-1 downto 0);
123
 
124
  -- pre-decoder --
125
  signal ci_instr32 : std_ulogic_vector(31 downto 0);
126
  signal ci_valid   : std_ulogic;
127
  signal ci_illegal : std_ulogic;
128
 
129
  -- instruction register --
130
  signal i_reg,  i_reg_nxt  : std_ulogic_vector(31 downto 0);
131
  signal i_buf,  i_buf_nxt  : std_ulogic_vector(15 downto 0);
132
  signal ci_reg, ci_reg_nxt : std_ulogic_vector(15 downto 0);
133
  signal iavail, iavail_nxt : std_ulogic;
134
  signal is_ci,  is_ci_nxt  : std_ulogic; -- current instruction is COMPRESSED instruction flag
135
 
136
  -- immediates --
137
  signal imm_reg : std_ulogic_vector(data_width_c-1 downto 0);
138
 
139
  -- branch system --
140
  signal is_branch     : std_ulogic;
141
  signal is_branch_nxt : std_ulogic;
142
  signal branch_taken  : std_ulogic;
143
 
144
  -- program counter --
145
  signal pc_reg         : std_ulogic_vector(data_width_c-1 downto 0); -- actual PC
146
  signal pc_backup_reg  : std_ulogic_vector(data_width_c-1 downto 0); -- delayed PC (for ALU operations)
147
  signal pc_backup2_reg : std_ulogic_vector(data_width_c-1 downto 0); -- delayed delayed PC (for exception handling)
148
  signal mepc           : std_ulogic_vector(data_width_c-1 downto 0); -- exception PC
149
 
150
  -- irq controller --
151
  signal exc_buf       : std_ulogic_vector(exception_width_c-1 downto 0);
152
  signal exc_ack       : std_ulogic_vector(exception_width_c-1 downto 0);
153
  signal exc_ack_nxt   : std_ulogic_vector(exception_width_c-1 downto 0);
154
  signal exc_src       : std_ulogic_vector(exception_width_c-1 downto 0);
155
  signal exc_fire      : std_ulogic;
156
  signal irq_buf       : std_ulogic_vector(interrupt_width_c-1 downto 0);
157
  signal irq_ack       : std_ulogic_vector(interrupt_width_c-1 downto 0);
158
  signal irq_ack_nxt   : std_ulogic_vector(interrupt_width_c-1 downto 0);
159
  signal irq_fire      : std_ulogic;
160
  signal exc_cpu_start : std_ulogic; -- starting exception env
161
  signal exc_cpu_ack   : std_ulogic; -- starting of exception env acknowledge
162
  signal exc_cpu_end   : std_ulogic; -- exiting eception env
163
  signal exc_cause     : std_ulogic_vector(data_width_c-1 downto 0);
164
  signal exc_cause_nxt : std_ulogic_vector(data_width_c-1 downto 0);
165
 
166
  -- RISC-V CSRs --
167
  signal mstatus_mie    : std_ulogic; -- mstatus.MIE: global IRQ enable (R/W)
168
  signal mstatus_mpie   : std_ulogic; -- mstatus.MPIE: previous global IRQ enable (R/-)
169
  signal mip_msip       : std_ulogic; -- mip.MSIP: machine software interrupt pending (R/W)
170
  signal mie_msie       : std_ulogic; -- mie.MSIE: machine software interrupt enable (R/W)
171
  signal mie_meie       : std_ulogic; -- mie.MEIE: machine external interrupt enable (R/W)
172
  signal mie_mtie       : std_ulogic; -- mie.MEIE: machine timer interrupt enable (R/W)
173
  signal mtvec          : std_ulogic_vector(data_width_c-1 downto 0); -- mtvec: machine trap-handler base address (R/W)
174
  signal mtval          : std_ulogic_vector(data_width_c-1 downto 0); -- mtval: machine bad address or isntruction (R/-)
175
  signal mscratch       : std_ulogic_vector(data_width_c-1 downto 0); -- mscratch: scratch register (R/W)
176
  signal mtinst         : std_ulogic_vector(data_width_c-1 downto 0); -- mtinst: machine trap instruction (transformed) (R/-)
177
  signal cycle_lo       : std_ulogic_vector(32 downto 0); -- cycle, mtime (R/-)
178
  signal instret_lo     : std_ulogic_vector(32 downto 0); -- instret (R/-)
179
  signal cycle_hi       : std_ulogic_vector(15 downto 0); -- cycleh, mtimeh (R/-) - only 16-bit wide
180
  signal instret_hi     : std_ulogic_vector(15 downto 0); -- instreth (R/-) - only 16-bit wide
181
  signal cycle_lo_msb   : std_ulogic;
182
  signal instret_lo_msb : std_ulogic;
183
 
184
  -- illegal instruction check ..
185
  signal illegal_instruction : std_ulogic;
186
  signal illegal_register    : std_ulogic; -- only for E-extension
187
  signal illegal_compressed  : std_ulogic; -- only fir C-extension
188
 
189
  -- synchronous exceptions trigger --
190
  signal illegal_instr_exc : std_ulogic;
191
  signal env_call          : std_ulogic;
192
  signal break_point       : std_ulogic;
193
 
194
begin
195
 
196
  -- Compressed Instructions Recoding -------------------------------------------------------
197
  -- -------------------------------------------------------------------------------------------
198
  neorv32_cpu_decompressor_inst_true:
199
  if (CPU_EXTENSION_RISCV_C = true) generate
200
    neorv32_cpu_decompressor_inst: neorv32_cpu_decompressor
201
    port map (
202
      -- instruction input --
203
      ci_instr16_i => ci_reg,     -- compressed instruction input
204
      -- instruction output --
205
      ci_valid_o   => ci_valid,   -- is a compressed instruction
206
      ci_illegal_o => ci_illegal, -- is an illegal compressed instruction
207
      ci_instr32_o => ci_instr32  -- 32-bit decompressed instruction
208
    );
209
  end generate;
210
 
211
  neorv32_cpu_decompressor_inst_false:
212
  if (CPU_EXTENSION_RISCV_C = false) generate
213
    ci_instr32 <= instr_i;
214
    ci_valid   <= '0';
215
    ci_illegal <= '0';
216
  end generate;
217
 
218
 
219
  -- Immediate Generator --------------------------------------------------------------------
220
  -- -------------------------------------------------------------------------------------------
221
  imm_gen: process(clk_i)
222
  begin
223
    if rising_edge(clk_i) then
224
      -- default: I-immediate --
225
      imm_reg(31 downto 11) <= (others => i_reg(31)); -- sign extension
226
      imm_reg(10 downto 05) <= i_reg(30 downto 25);
227
      imm_reg(04 downto 01) <= i_reg(24 downto 21);
228
      imm_reg(00)           <= i_reg(20);
229
      case i_reg(instr_opcode_msb_c downto instr_opcode_lsb_c) is
230
        when opcode_store_c => -- S-immediate
231
          imm_reg(31 downto 11) <= (others => i_reg(31)); -- sign extension
232
          imm_reg(10 downto 05) <= i_reg(30 downto 25);
233
          imm_reg(04 downto 01) <= i_reg(11 downto 08);
234
          imm_reg(00)           <= i_reg(07);
235
        when opcode_branch_c => -- B-immediate
236
          imm_reg(31 downto 12) <= (others => i_reg(31)); -- sign extension
237
          imm_reg(11)           <= i_reg(07);
238
          imm_reg(10 downto 05) <= i_reg(30 downto 25);
239
          imm_reg(04 downto 01) <= i_reg(11 downto 08);
240
          imm_reg(00)           <= '0';
241
        when opcode_lui_c | opcode_auipc_c => -- U-immediate
242
          imm_reg(31 downto 20) <= i_reg(31 downto 20);
243
          imm_reg(19 downto 12) <= i_reg(19 downto 12);
244
          imm_reg(11 downto 00) <= (others => '0');
245
        when opcode_jal_c => -- J-immediate
246
          imm_reg(31 downto 20) <= (others => i_reg(31)); -- sign extension
247
          imm_reg(19 downto 12) <= i_reg(19 downto 12);
248
          imm_reg(11)           <= i_reg(20);
249
          imm_reg(10 downto 05) <= i_reg(30 downto 25);
250
          imm_reg(04 downto 01) <= i_reg(24 downto 21);
251
          imm_reg(00)           <= '0';
252
        when opcode_syscsr_c => -- CSR-immediate
253
          imm_reg(31 downto 05) <= (others => '0');
254
          imm_reg(04 downto 00) <= i_reg(19 downto 15);
255
        when others => -- I-immediate
256
          imm_reg(31 downto 11) <= (others => i_reg(31)); -- sign extension
257
          imm_reg(10 downto 05) <= i_reg(30 downto 25);
258
          imm_reg(04 downto 01) <= i_reg(24 downto 21);
259
          imm_reg(00)           <= i_reg(20);
260
      end case;
261
    end if;
262
  end process imm_gen;
263
 
264
  -- output --
265
  imm_o <= imm_reg;
266
 
267
 
268
  -- Branch Condition Check -----------------------------------------------------------------
269
  -- -------------------------------------------------------------------------------------------
270
  branch_check: process(i_reg, cmp_i)
271
  begin
272
    case i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) is
273
      when funct3_beq_c => -- branch if equal
274
        branch_taken <= cmp_i(alu_cmp_equal_c);
275
      when funct3_bne_c => -- branch if not equal
276
        branch_taken <= not cmp_i(alu_cmp_equal_c);
277
      when funct3_blt_c | funct3_bltu_c => -- branch if less (signed/unsigned)
278
        branch_taken <= cmp_i(alu_cmp_less_c);
279
      when funct3_bge_c | funct3_bgeu_c => -- branch if greater or equal (signed/unsigned)
280
        branch_taken <= not cmp_i(alu_cmp_less_c);
281
      when others => -- undefined
282
        branch_taken <= '0';
283
    end case;
284
  end process branch_check;
285
 
286
 
287
  -- Arbiter State Machine Sync -------------------------------------------------------------
288
  -- -------------------------------------------------------------------------------------------
289
  arbiter_sync_rst: process(rstn_i, clk_i)
290
  begin
291
    if (rstn_i = '0') then
292
      -- these registers REQUIRE a specific reset state
293
      state <= IFETCH_0;
294
    elsif rising_edge(clk_i) then
295
      state <= state_nxt;
296
    end if;
297
  end process arbiter_sync_rst;
298
 
299
  arbiter_sync: process(clk_i)
300
  begin
301
    if rising_edge(clk_i) then
302
      -- these registers do not need a specific reset state
303
      ctrl      <= ctrl_nxt;
304
      i_reg     <= i_reg_nxt;
305
      i_buf     <= i_buf_nxt;
306
      ci_reg    <= ci_reg_nxt;
307
      iavail    <= iavail_nxt;
308
      is_ci     <= is_ci_nxt;
309
      is_branch <= is_branch_nxt;
310
    end if;
311
  end process arbiter_sync;
312
 
313
  -- control bus output --
314
  ctrl_outpu: process(ctrl, i_reg)
315
  begin
316
    ctrl_o <= ctrl;
317
    -- direct output of register addresses --
318
    ctrl_o(ctrl_rf_rd_adr4_c  downto ctrl_rf_rd_adr0_c)  <= i_reg(instr_rd_msb_c  downto instr_rd_lsb_c);
319
    ctrl_o(ctrl_rf_rs1_adr4_c downto ctrl_rf_rs1_adr0_c) <= i_reg(instr_rs1_msb_c downto instr_rs1_lsb_c);
320
    ctrl_o(ctrl_rf_rs2_adr4_c downto ctrl_rf_rs2_adr0_c) <= i_reg(instr_rs2_msb_c downto instr_rs2_lsb_c);
321
  end process ctrl_outpu;
322
 
323
 
324
  -- Arbiter State Machine Comb -----------------------------------------------
325
  -- -----------------------------------------------------------------------------
326
  arbiter_comb: process(state, ctrl, i_reg, alu_wait_i, bus_wait_i, exc_cpu_start, ma_load_i, be_load_i, ma_store_i, be_store_i,
327
                        i_reg, ci_reg, i_buf, instr_i, is_ci, iavail, pc_backup_reg, ci_valid, ci_instr32)
328
    variable alu_immediate_v : std_ulogic;
329
    variable alu_operation_v : std_ulogic_vector(2 downto 0);
330
    variable rs1_is_r0_v     : std_ulogic;
331
    variable rd_is_r0_v      : std_ulogic;
332
  begin
333
    -- arbiter defaults --
334
    state_nxt     <= state;
335
    is_branch_nxt <= '0';
336
    exc_cpu_ack   <= '0';
337
    exc_cpu_end   <= '0';
338
 
339
    i_reg_nxt  <= i_reg;
340
    i_buf_nxt  <= i_buf;
341
    ci_reg_nxt <= ci_reg;
342
    iavail_nxt <= iavail;
343
    is_ci_nxt  <= is_ci;
344
 
345
    -- exception trigger --
346
    env_call    <= '0';
347
    break_point <= '0';
348
 
349
    -- control defaults --
350
    ctrl_nxt <= (others => '0'); -- all off at first
351
    ctrl_nxt(ctrl_bus_unsigned_c)   <= i_reg(instr_funct3_msb_c); -- unsigned LOAD (LBU, LHU)
352
    if (i_reg(instr_opcode_lsb_c+4) = '1') then -- ALU ops
353
      ctrl_nxt(ctrl_alu_unsigned_c) <= i_reg(instr_funct3_lsb_c+0); -- unsigned ALU operation (SLTIU, SLTU)
354
    else -- branches
355
      ctrl_nxt(ctrl_alu_unsigned_c) <= i_reg(instr_funct3_lsb_c+1); -- unsigned branches (BLTU, BGEU)
356
    end if;
357
    ctrl_nxt(ctrl_alu_shift_dir_c)  <= i_reg(instr_funct3_msb_c); -- shift direction
358
    ctrl_nxt(ctrl_alu_shift_ar_c)   <= i_reg(30); -- arithmetic shift
359
    ctrl_nxt(ctrl_bus_size_lsb_c)   <= i_reg(instr_funct3_lsb_c+0); -- transfer size lsb (00=byte, 01=half-word)
360
    ctrl_nxt(ctrl_bus_size_msb_c)   <= i_reg(instr_funct3_lsb_c+1); -- transfer size msb (10=word, 11=?)
361
    ctrl_nxt(ctrl_alu_cmd2_c  downto ctrl_alu_cmd0_c)  <= alu_cmd_add_c; -- actual ALU operation = add
362
    ctrl_nxt(ctrl_cp_cmd2_c   downto ctrl_cp_cmd0_c)   <= i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c); -- CP operation
363
    ctrl_nxt(ctrl_cp_id_msb_c downto ctrl_cp_id_lsb_c) <= cp_sel_muldiv_c; -- only CP0 implemented yet
364
 
365
    -- is immediate operation? --
366
    alu_immediate_v := '0';
367
    if (i_reg(instr_opcode_msb_c-1) = '0') then
368
      alu_immediate_v := '1';
369
    end if;
370
 
371
    -- hardware branch operation control --
372
    hw_control(31 downto 24) <= ('0' & funct3_bne_c) & ('1' & funct3_bne_c);
373
    hw_control(23 downto 16) <= funct3_xor_c & funct3_slt_c & "00";
374
    hw_control(15 downto 08) <= funct3_beq_c & funct3_bne_c & "11";
375
    hw_control(07 downto 00) <= funct3_beq_c & funct3_bne_c & "00";
376
 
377
    -- alu operation --
378
    case i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) is
379
      when funct3_subadd_c => -- SUB / ADD(I)
380
        if (alu_immediate_v = '0') and (i_reg(instr_funct7_msb_c-1) = '1') then -- not immediate and funct7 = SUB
381
          alu_operation_v := alu_cmd_sub_c;
382
        else
383
          alu_operation_v := alu_cmd_add_c;
384
        end if;
385
      when funct3_sll_c  => alu_operation_v := alu_cmd_shift_c; -- SLL(I)
386
      when funct3_slt_c  => alu_operation_v := alu_cmd_slt_c; -- SLT(I)
387
      when funct3_sltu_c => alu_operation_v := alu_cmd_slt_c; -- SLTU(I)
388
      when funct3_xor_c  => alu_operation_v := alu_cmd_xor_c; -- XOR(I)
389
      when funct3_sr_c   => alu_operation_v := alu_cmd_shift_c; -- SRL(I) / SRA(I)
390
      when funct3_or_c   => alu_operation_v := alu_cmd_or_c; -- OR(I)
391
      when funct3_and_c  => alu_operation_v := alu_cmd_and_c; -- AND(I)
392 3 zero_gravi
      when others        => alu_operation_v := (others => '0'); -- undefined
393 2 zero_gravi
    end case;
394
 
395
    -- is rs1 = r0? --
396
    rs1_is_r0_v := '0';
397
    if (i_reg(instr_rs1_msb_c downto instr_rs1_lsb_c) = "00000") then
398
      rs1_is_r0_v := '1';
399
    end if;
400
 
401
    -- is rd = r0? --
402
    rd_is_r0_v := '0';
403
    if (i_reg(instr_rd_msb_c downto instr_rd_lsb_c) = "00000") then
404
      rd_is_r0_v := '1';
405
    end if;
406
 
407
 
408
    -- state machine: instruction fetch and execution --
409
    case state is
410
 
411
      when IFETCH_0 => -- output current PC to bus system
412
      -- ------------------------------------------------------------
413
      ctrl_nxt(ctrl_bus_if_c) <= '1'; -- instruction fetch request (output PC to bus address)
414
      iavail_nxt <= '0'; -- absolutely no instruction available yet
415
      state_nxt  <= IFETCH_1;
416
 
417
      when IFETCH_1 => -- memory latency, update PC
418
      -- ------------------------------------------------------------
419
        ctrl_nxt(ctrl_alu_opa_mux_msb_c downto ctrl_alu_opa_mux_lsb_c) <= "11"; -- opa = current PC
420
        ctrl_nxt(ctrl_alu_opb_mux_msb_c downto ctrl_alu_opb_mux_lsb_c) <= "11"; -- opb = PC_increment
421
        ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_add_c; -- actual ALU operation = ADD
422
        ctrl_nxt(ctrl_csr_pc_we_c) <= '1'; -- update PC
423
        state_nxt <= IFETCH_2;
424
 
425
      when IFETCH_2 => -- update instruction buffers
426
      -- ------------------------------------------------------------
427
        if (exc_cpu_start = '1') then -- exception detected!
428
          exc_cpu_ack <= '1';
429
          state_nxt <= IFETCH_0; -- start new instruction fetch
430
        else -- normal operation
431
          -- instruction register update --
432
          if (CPU_EXTENSION_RISCV_C = true) then -- compressed AND uncompressed instructions possible
433
            if (pc_backup_reg(1) = '0') then
434
              i_buf_nxt  <= instr_i(31 downto 16);
435
              ci_reg_nxt <= instr_i(15 downto 00);
436
            else
437
              i_buf_nxt  <= instr_i(15 downto 00);
438
              ci_reg_nxt <= instr_i(31 downto 16);
439
            end if;
440
          else -- only uncompressed instructions
441
            i_reg_nxt <= instr_i(31 downto 0);
442
          end if;
443
          -- next state --
444
          if (bus_wait_i = '0') then -- wait for bus response
445
            if (CPU_EXTENSION_RISCV_C = true) then -- compressed AND uncompressed instructions possible
446
              state_nxt <= IFETCH_3;
447
            else -- only uncompressed instructions
448
              state_nxt <= EXECUTE;
449
            end if;
450
          end if;
451
        end if;
452
 
453
 
454
      when IFETCH_3 => -- check for exception, start instruction execution (only available for C-extension)
455
      -- ------------------------------------------------------------
456
        ctrl_nxt(ctrl_alu_opa_mux_msb_c downto ctrl_alu_opa_mux_lsb_c) <= "11"; -- opa = current PC
457
        ctrl_nxt(ctrl_alu_opb_mux_msb_c downto ctrl_alu_opb_mux_lsb_c) <= "11"; -- opb = PC_increment
458
        ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_add_c; -- actual ALU operation = ADD
459
        --
460
        if (exc_cpu_start = '1') then -- exception detected!
461
          exc_cpu_ack <= '1';
462
          state_nxt   <= IFETCH_0; -- start new instruction fetch
463
        else -- normal operation
464
          if (ci_valid = '1') then -- directly execute decoded compressed instruction
465
            i_reg_nxt <= ci_instr32;
466
            state_nxt <= EXECUTE;
467
          elsif (pc_backup_reg(1) = '0') or (iavail = '1') then -- 32-bit aligned uncompressed instruction
468
            i_reg_nxt <= i_buf & ci_reg;
469
            state_nxt <= EXECUTE;
470
            ctrl_nxt(ctrl_csr_pc_we_c) <= not pc_backup_reg(1); -- update PC again when on 32b-aligned address
471
          else
472
            i_reg_nxt <= i_buf & ci_reg;
473
            state_nxt <= IFETCH_4;
474
          end if;
475
        end if;
476
 
477
      when IFETCH_4 => -- get missing instruction parts: output current PC to bus system (only available for C-extension)
478
      -- ------------------------------------------------------------
479
      ctrl_nxt(ctrl_bus_if_c) <= '1'; -- instruction fetch request (output PC to bus address)
480
      state_nxt <= IFETCH_5;
481
 
482
      when IFETCH_5 => -- memory latency, update PC (only available for C-extension)
483
      -- ------------------------------------------------------------
484
        ctrl_nxt(ctrl_alu_opa_mux_msb_c downto ctrl_alu_opa_mux_lsb_c) <= "11"; -- opa = current PC
485
        ctrl_nxt(ctrl_alu_opb_mux_msb_c downto ctrl_alu_opb_mux_lsb_c) <= "11"; -- opb = PC_increment
486
        ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_add_c; -- actual ALU operation = ADD
487
        ctrl_nxt(ctrl_csr_pc_we_c) <= '1'; -- update PC
488
        state_nxt <= IFETCH_6;
489
 
490
      when IFETCH_6 => -- update missing instruction buffer parts (only available for C-extension)
491
      -- ------------------------------------------------------------
492
        if (bus_wait_i = '0') then -- wait for bus response
493
          i_buf_nxt  <= instr_i(15 downto 00);
494
          iavail_nxt <= '1';
495
          state_nxt  <= IFETCH_3;
496
        end if;
497
 
498
 
499
      when EXECUTE => -- decode and execute instruction
500
      -- ------------------------------------------------------------
501
        is_ci_nxt <= ci_valid; -- flag to indicate this is a compressed instruction beeing executed (ci_valid is zero if not C-ext is not implemented)
502
        case i_reg(instr_opcode_msb_c downto instr_opcode_lsb_c) is
503
 
504
          when opcode_alu_c | opcode_alui_c => -- ALU operation
505
          -- ------------------------------------------------------------
506
            ctrl_nxt(ctrl_alu_opa_mux_lsb_c) <= '0'; -- use RS1 as ALU.OPA
507
            ctrl_nxt(ctrl_alu_opb_mux_lsb_c) <= alu_immediate_v; -- use IMM as ALU.OPB for immediate operations
508
            ctrl_nxt(ctrl_alu_opc_mux_c)     <= not alu_immediate_v;
509
            ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_operation_v; -- actual ALU operation
510
            ctrl_nxt(ctrl_rf_in_mux_msb_c downto ctrl_rf_in_mux_lsb_c) <= "00"; -- RF input = ALU result
511
            if (CPU_EXTENSION_RISCV_M = true) and (i_reg(instr_opcode_msb_c downto instr_opcode_lsb_c) = opcode_alu_c) and
512
               (i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c) = "0000001") then -- MULDIV?
513
              ctrl_nxt(ctrl_cp_use_c) <= '1'; -- use CP
514
              ctrl_nxt(ctrl_cp_id_msb_c downto ctrl_cp_id_lsb_c) <= cp_sel_muldiv_c; -- muldiv CP
515
              state_nxt <= ALU_WAIT;
516
            elsif (alu_operation_v = alu_cmd_shift_c) then -- multi-cycle shift operation?
517
              state_nxt <= ALU_WAIT;
518
            else
519
              ctrl_nxt(ctrl_rf_wb_en_c) <= '1'; -- valid RF write-back
520
              state_nxt <= IFETCH_0;
521
            end if;
522
 
523
          when opcode_lui_c | opcode_auipc_c => -- load upper immediate (add to PC)
524
          -- ------------------------------------------------------------
525
            ctrl_nxt(ctrl_rf_clear_rs1_c) <= '1'; -- force RS1 = r0
526
            if (i_reg(instr_opcode_msb_c downto instr_opcode_lsb_c) = opcode_auipc_c) then -- AUIPC
527
              ctrl_nxt(ctrl_alu_opa_mux_lsb_c) <= '1'; -- use PC as ALU.OPA
528
            else -- LUI
529
              ctrl_nxt(ctrl_alu_opa_mux_lsb_c) <= '0'; -- use RS1 as ALU.OPA
530
            end if;
531
            ctrl_nxt(ctrl_alu_opb_mux_lsb_c)  <= '1'; -- use IMM as ALU.OPB
532
            ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_add_c; -- actual ALU operation
533
            ctrl_nxt(ctrl_rf_in_mux_msb_c downto ctrl_rf_in_mux_lsb_c) <= "00"; -- RF input = ALU result
534
            ctrl_nxt(ctrl_rf_wb_en_c) <= '1'; -- valid RF write-back
535
            state_nxt <= IFETCH_0;
536
 
537
          when opcode_load_c | opcode_store_c => -- load/store
538
          -- ------------------------------------------------------------
539
            ctrl_nxt(ctrl_alu_opa_mux_lsb_c) <= '0'; -- use RS1 as ALU.OPA
540
            ctrl_nxt(ctrl_alu_opb_mux_lsb_c) <= '1'; -- use IMM as ALU.OPB
541
            ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_add_c; -- actual ALU operation
542
            ctrl_nxt(ctrl_bus_mar_we_c)  <= '1'; -- write to MAR
543
            ctrl_nxt(ctrl_bus_mdo_we_c)  <= '1'; -- write to MDO (only relevant for stores)
544
            if (i_reg(instr_opcode_msb_c downto instr_opcode_lsb_c) = opcode_load_c) then -- LOAD
545
              state_nxt <= LOAD_0;
546
            else -- STORE
547
              state_nxt <= STORE_0;
548
            end if;
549
 
550
          when opcode_branch_c => -- branch instruction
551
          -- ------------------------------------------------------------
552
            ctrl_nxt(ctrl_alu_opa_mux_lsb_c) <= '1'; -- use PC as ALU.OPA
553
            ctrl_nxt(ctrl_alu_opb_mux_lsb_c) <= '1'; -- use IMM as ALU.OPB
554
            ctrl_nxt(ctrl_alu_opc_mux_c) <= '1'; -- use RS2 as ALU.OPC
555
            is_branch_nxt <= '1';
556
            state_nxt <= IFETCH_0;
557
 
558
          when opcode_jal_c | opcode_jalr_c => -- jump and link (with register)
559
          -- ------------------------------------------------------------
560
            -- compute target address --
561
            if (i_reg(instr_opcode_msb_c downto instr_opcode_lsb_c) = opcode_jal_c) then -- JAL
562
              ctrl_nxt(ctrl_alu_opa_mux_lsb_c) <= '1'; -- use PC as ALU.OPA
563
            else -- JALR
564
              ctrl_nxt(ctrl_alu_opa_mux_lsb_c) <= '0'; -- use RS1 as ALU.OPA
565
            end if;
566
            ctrl_nxt(ctrl_alu_opb_mux_lsb_c) <= '1'; -- use IMM as ALU.OPB
567
            -- save return address --
568
            ctrl_nxt(ctrl_rf_in_mux_msb_c downto ctrl_rf_in_mux_lsb_c) <= "10"; -- RF input = current PC
569
            ctrl_nxt(ctrl_rf_wb_en_c) <= '1'; -- valid RF write-back
570
            -- update PC --
571
            ctrl_nxt(ctrl_csr_pc_we_c)  <= '1'; -- update PC
572
            state_nxt <= IFETCH_0;
573
 
574
          when opcode_syscsr_c => -- system/csr access
575
          -- ------------------------------------------------------------
576
            ctrl_nxt(ctrl_csr_re_c) <= '1'; -- ALWAYS READ CSR!!! (OLD: not rd_is_r0_v; -- valid CSR read if rd is not r0)
577
            if (i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_env_c) then -- system
578
              case i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) is
579
                when x"000" => env_call    <= '1'; state_nxt <= IFETCH_0; -- ECALL
580
                when x"001" => break_point <= '1'; state_nxt <= IFETCH_0; -- EBREAK
581
                when x"302" => exc_cpu_end <= '1'; state_nxt <= IFETCH_0; -- MRET
582
                when x"105" =>                     state_nxt <= SLEEP;    -- WFI
583
                when others => NULL; -- undefined
584
              end case;
585
            elsif (CPU_EXTENSION_RISCV_Zicsr = true) then -- CSR access
586
              state_nxt <= CSR_ACCESS;
587
            else
588
              state_nxt <= IFETCH_0;
589
            end if;
590
 
591
          when others => -- undefined
592
          -- ------------------------------------------------------------
593
            state_nxt <= IFETCH_0;
594
 
595
        end case;
596
 
597
      when ALU_WAIT => -- wait for multi-cycle ALU operation to finish
598
      -- ------------------------------------------------------------
599
        ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_operation_v; -- actual ALU operation
600
        ctrl_nxt(ctrl_rf_in_mux_msb_c downto ctrl_rf_in_mux_lsb_c) <= "00"; -- RF input = ALU result
601
        ctrl_nxt(ctrl_rf_wb_en_c) <= '1'; -- valid RF write-back (write back all the time)
602
        if (alu_wait_i = '0') then
603
          state_nxt <= IFETCH_0;
604
        end if;
605
 
606
      when LOAD_0 => -- trigger memory read request
607
      -- ------------------------------------------------------------
608
        ctrl_nxt(ctrl_bus_rd_c) <= '1'; -- read request
609
        state_nxt <= LOADSTORE_0;
610
 
611
      when STORE_0 => -- trigger memory write request
612
      -- ------------------------------------------------------------
613
        ctrl_nxt(ctrl_bus_wr_c) <= '1'; -- write request
614
        state_nxt <= LOADSTORE_0;
615
 
616
      when LOADSTORE_0 => -- memory latency
617
      -- ------------------------------------------------------------
618
        ctrl_nxt(ctrl_bus_mdi_we_c) <= '1'; -- write input data to MDI (only relevant for LOAD)
619
        state_nxt <= LOADSTORE_1;
620
 
621
      when LOADSTORE_1 => -- wait for bus transaction to finish
622
      -- ------------------------------------------------------------
623
        ctrl_nxt(ctrl_bus_mdi_we_c) <= '1'; -- keep writing input data to MDI (only relevant for LOAD)
624
        ctrl_nxt(ctrl_rf_in_mux_msb_c downto ctrl_rf_in_mux_lsb_c) <= "01"; -- RF input = memory input (only relevant for LOAD)
625
        if (ma_load_i = '1') or (be_load_i = '1') or (ma_store_i = '1') or (be_store_i = '1') then -- abort if exception
626
          state_nxt <= IFETCH_0;
627
        elsif (bus_wait_i = '0') then -- wait here for bus to finish transaction
628
          if (i_reg(instr_opcode_msb_c downto instr_opcode_lsb_c) = opcode_load_c) then -- LOAD?
629
            ctrl_nxt(ctrl_rf_wb_en_c) <= '1'; -- valid RF write-back
630
          end if;
631
          state_nxt <= IFETCH_0;
632
        end if;
633
 
634
      when CSR_ACCESS => -- write CSR data to RF, write ALU.res to CSR
635
      -- ------------------------------------------------------------
636
        ctrl_nxt(ctrl_alu_opa_mux_msb_c) <= '0'; -- default
637
        ctrl_nxt(ctrl_alu_opa_mux_lsb_c) <= '0'; -- default
638
        ctrl_nxt(ctrl_alu_opb_mux_msb_c) <= '0'; -- default
639
        ctrl_nxt(ctrl_alu_opb_mux_lsb_c) <= '0'; -- default
640
        ctrl_nxt(ctrl_csr_we_c) <= not rs1_is_r0_v; -- valid CSR write if rs1 is not r0 (or not imm5 = 0)
641
        case i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) is
642
          when funct3_csrrw_c => -- CSSRW
643
            ctrl_nxt(ctrl_alu_opa_mux_lsb_c) <= '0'; -- OPA = rs1
644
            ctrl_nxt(ctrl_alu_opb_mux_lsb_c) <= '0'; -- OPB = rs2
645
            ctrl_nxt(ctrl_rf_clear_rs2_c)    <= '1'; -- rs2 = 0
646
            ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_add_c; -- actual ALU operation = ADD
647
          when funct3_csrrs_c => -- CSSRS
648
            ctrl_nxt(ctrl_alu_opa_mux_msb_c) <= '1'; -- OPA = csr
649
            ctrl_nxt(ctrl_alu_opb_mux_msb_c) <= '1'; -- OPB = crs1
650
            ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_or_c; -- actual ALU operation = OR
651
          when funct3_csrrc_c => -- CSSRC
652
            ctrl_nxt(ctrl_alu_opa_mux_msb_c) <= '1'; -- OPA = csr
653
            ctrl_nxt(ctrl_alu_opb_mux_msb_c) <= '1'; -- OPB = rs1
654
            ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_bitc_c; -- actual ALU operation = bit clear
655
          when funct3_csrrwi_c => -- CSSRWI
656
            ctrl_nxt(ctrl_alu_opa_mux_lsb_c) <= '0'; -- OPA = rs1
657
            ctrl_nxt(ctrl_rf_clear_rs1_c)    <= '1'; -- rs1 = 0
658
            ctrl_nxt(ctrl_alu_opb_mux_lsb_c) <= '1'; -- OPB = immediate
659
            ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_add_c; -- actual ALU operation = ADD
660
          when funct3_csrrsi_c => -- CSSRSI
661
            ctrl_nxt(ctrl_alu_opa_mux_msb_c) <= '1'; -- OPA = csr
662
            ctrl_nxt(ctrl_alu_opb_mux_lsb_c) <= '1'; -- OPB = immediate
663
            ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_or_c; -- actual ALU operation = OR
664
          when funct3_csrrci_c => -- CSSRCI
665
            ctrl_nxt(ctrl_alu_opa_mux_msb_c) <= '1'; -- OPA = csr
666
            ctrl_nxt(ctrl_alu_opb_mux_lsb_c) <= '1'; -- OPB = immediate
667
            ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_bitc_c; -- actual ALU operation = bit clear
668
          when others => -- undefined
669
            NULL;
670
        end case;
671
        -- RF write back --
672
        ctrl_nxt(ctrl_rf_in_mux_msb_c downto ctrl_rf_in_mux_lsb_c) <= "11"; -- RF input = CSR output register
673
        ctrl_nxt(ctrl_rf_wb_en_c) <= '1'; -- valid RF write-back
674
        state_nxt <= IFETCH_0;
675
 
676
      when SLEEP => -- stall and wait for interrupt (WFI)
677
      -- ------------------------------------------------------------
678
        if (exc_cpu_start = '1') then -- exception detected!
679
          exc_cpu_ack <= '1';
680
          state_nxt   <= IFETCH_0; -- start new instruction fetch
681
        end if;
682
 
683
      when others => -- undefined
684
      -- ------------------------------------------------------------
685
        state_nxt <= IFETCH_0;
686
 
687
    end case;
688
  end process arbiter_comb;
689
 
690
 
691
  -- Illegal Instruction Check --------------------------------------------------------------
692
  -- -------------------------------------------------------------------------------------------
693
  illegal_instruction_check: process(i_reg, state, ctrl_nxt)
694
  begin
695
    if (state = EXECUTE) then
696
      -- defaults --
697
      illegal_instruction <= '0';
698
      illegal_register    <= '0';
699
      illegal_compressed  <= '0';
700
 
701
      -- check if using reg >= 16 for E-CPUs --
702
      if (CPU_EXTENSION_RISCV_E = true) then
703
        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);
704
      else
705
        illegal_register <= '0';
706
      end if;
707
 
708
      -- check instructions --
709
      case i_reg(instr_opcode_msb_c downto instr_opcode_lsb_c) is
710
 
711
        -- OPCODE check sufficient: LUI, UIPC, JAL --
712
        when opcode_lui_c | opcode_auipc_c | opcode_jal_c =>
713
          illegal_instruction <= '0';
714
 
715
        when opcode_alui_c => -- check ALUI funct7
716
          if ((i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sll_c) and
717
              (i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c) /= "0000000")) or -- shift logical left
718
             ((i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sr_c) and
719
              ((i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c) /= "0000000") and
720
               (i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c) /= "0100000"))) then -- shift right
721
            illegal_instruction <= '1';
722
          else
723
            illegal_instruction <= '0';
724
          end if;
725
 
726
        when opcode_load_c => -- check LOAD funct3
727
          if (i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_lb_c) or
728
             (i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_lh_c) or
729
             (i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_lw_c) or
730
             (i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_lbu_c) or
731
             (i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_lhu_c) then
732
            illegal_instruction <= '0';
733
          else
734
            illegal_instruction <= '1';
735
          end if;
736
 
737
        when opcode_store_c => -- check STORE funct3
738
          if (i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sb_c) or
739
             (i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sh_c) or
740
             (i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sw_c) then
741
            illegal_instruction <= '0';
742
          else
743
            illegal_instruction <= '1';
744
          end if;
745
 
746
        when opcode_branch_c => -- check BRANCH funct3
747
          if (i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_beq_c) or
748
             (i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_bne_c) or
749
             (i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_blt_c) or
750
             (i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_bge_c) or
751
             (i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_bltu_c) or
752
             (i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_bgeu_c) then
753
            illegal_instruction <= '0';
754
          else
755
            illegal_instruction <= '1';
756
          end if;
757
 
758
        when opcode_jalr_c => -- check JALR funct3
759
          if (i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = "000") then
760
            illegal_instruction <= '0';
761
          else
762
            illegal_instruction <= '1';
763
          end if;
764
 
765
        when opcode_alu_c => -- check ALU funct3 & funct7
766
          if (i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c) = "0000001") then -- MULDIV
767
            if (CPU_EXTENSION_RISCV_M = false) then
768
              illegal_instruction <= '1';
769
            end if;
770
          elsif ((i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_subadd_c) or
771
                 (i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sr_c)) and -- ADD/SUB or SRA/SRL check
772
                ((i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c) /= "0000000") and
773
                 (i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c) /= "0100000")) then -- ADD/SUB or SRA/SRL select
774
            illegal_instruction <= '1';
775
          else
776
            illegal_instruction <= '0';
777
          end if;
778
 
779
        when opcode_syscsr_c => -- check system instructions --
780
          -- CSR access --
781
          if (i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrw_c) or
782
             (i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrs_c) or
783
             (i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrc_c) or
784
             (i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrwi_c) or
785
             (i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrsi_c) or
786
             (i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrci_c) then
787
            -- valid CSR? --
788
            if (i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"300") or -- mstatus
789
               (i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"301") or -- misa
790
               (i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"304") or -- mie
791
               (i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"305") or -- mtvev
792
               (i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"340") or -- mscratch
793
               (i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"341") or -- mepc
794
               (i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"342") or -- mcause
795
               (i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"343") or -- mtval
796
               (i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"344") or -- mip
797
               (i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"34a") or -- mtinst
798
               --
799
               ((i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"c00") and (CPU_EXTENSION_RISCV_E = false)) or -- cycle
800
               ((i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"c01") and (CPU_EXTENSION_RISCV_E = false)) or -- time
801
               ((i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"c02") and (CPU_EXTENSION_RISCV_E = false)) or -- instret
802
               ((i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"c80") and (CPU_EXTENSION_RISCV_E = false)) or -- cycleh
803
               ((i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"c81") and (CPU_EXTENSION_RISCV_E = false)) or -- timeh
804
               ((i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"c82") and (CPU_EXTENSION_RISCV_E = false)) or -- instreth
805
               --
806
               ((i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"b00") and (CPU_EXTENSION_RISCV_E = false)) or -- mcycle
807
               ((i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"b02") and (CPU_EXTENSION_RISCV_E = false)) or -- minstret
808
               ((i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"b80") and (CPU_EXTENSION_RISCV_E = false)) or -- mcycleh
809
               ((i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"b82") and (CPU_EXTENSION_RISCV_E = false)) or -- minstreth
810
               --
811
               (i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"f13") or -- mimpid
812
               (i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"f14") or -- mhartid
813
               (i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"fff") or -- mhwctrl
814
               --
815
               (i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"fc0") or -- mfeatures
816
               (i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"fc1") or -- mclock
817
               (i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"fc4") or -- mispacebase
818
               (i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"fc5") or -- mispacesize
819
               (i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"fc6") or -- mdspacebase
820
               (i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = x"fc7") then -- mdspacesize
821
              illegal_instruction <= '0';
822
            else
823
              illegal_instruction <= '1';
824
            end if;
825
 
826
          -- ecall, ebreak, mret, wfi --
827
          elsif (i_reg(instr_rd_msb_c  downto instr_rd_lsb_c)  = "00000") and
828
                (i_reg(instr_rs1_msb_c downto instr_rs1_lsb_c) = "00000") then
829
            if (i_reg(instr_funct12_msb_c  downto instr_funct12_lsb_c) = "000000000000") or -- ECALL
830
               (i_reg(instr_funct12_msb_c  downto instr_funct12_lsb_c) = "000000000001") or -- EBREAK 
831
               (i_reg(instr_funct12_msb_c  downto instr_funct12_lsb_c) = "001100000010") or -- MRET
832
               (i_reg(instr_funct12_msb_c  downto instr_funct12_lsb_c) = "000100000101") then -- WFI
833
              illegal_instruction <= '0';
834
            else
835
              illegal_instruction <= '1';
836
            end if;
837
          else
838
            illegal_instruction <= '1';
839
          end if;
840
 
841
        when others => -- compressed instruction or undefined instruction
842
          if (i_reg(1 downto 0) = "11") then -- undefined/unimplemented opcode
843
            illegal_instruction <= '1';
844
          else -- compressed instruction
845
            illegal_compressed <= ci_valid and ci_illegal;
846
          end if;
847
 
848
      end case;
849
    else
850
      illegal_instruction <= '0';
851
      illegal_register    <= '0';
852
      illegal_compressed  <= '0';
853
    end if;
854
  end process illegal_instruction_check;
855
 
856
  -- any illegal condition? --
857
  illegal_instr_exc <= illegal_instruction or illegal_register or illegal_compressed;
858
 
859
 
860
  -- Program Counter ------------------------------------------------------------------------
861
  -- -------------------------------------------------------------------------------------------
862
  program_counter: process(rstn_i, clk_i)
863
  begin
864
    if (rstn_i = '0') then
865
      if (BOOTLOADER_USE = true) then -- boot from bootloader ROM
866
        pc_reg         <= boot_base_c;
867
        pc_backup_reg  <= boot_base_c;
868
        pc_backup2_reg <= boot_base_c;
869
      else -- boot from IMEM
870
        pc_reg         <= MEM_ISPACE_BASE;
871
        pc_backup_reg  <= MEM_ISPACE_BASE;
872
        pc_backup2_reg <= MEM_ISPACE_BASE;
873
      end if;
874
    elsif rising_edge(clk_i) then
875
      -- actual PC --
876
      if (exc_cpu_ack = '1') then -- exception start?
877
        pc_reg <= mtvec;
878
      elsif (exc_cpu_end = '1') then -- return from exception
879
        pc_reg <= mepc;
880
      elsif (ctrl(ctrl_csr_pc_we_c) = '1') or ((is_branch and branch_taken) = '1') then -- manual update or taken branch
881
        pc_reg <= alu_add_i;
882
      end if;
883
      -- delayed PC --
884
      if (state = IFETCH_1) then
885
        pc_backup_reg <= pc_reg; -- PC for ALU address computations
886
      end if;
887
      if (state = EXECUTE) then
888
        pc_backup2_reg <= pc_backup_reg; -- PC backup for exceptions
889
      end if;
890
    end if;
891
  end process program_counter;
892
 
893
  -- output --
894
  pc_o     <= pc_reg;
895
  alu_pc_o <= pc_backup_reg;
896
 
897
 
898
  -- Exception Controller -------------------------------------------------------------------
899
  -- -------------------------------------------------------------------------------------------
900
  exception_controller: process(rstn_i, clk_i)
901
  begin
902
    if (rstn_i = '0') then
903
      exc_buf       <= (others => '0');
904
      irq_buf       <= (others => '0');
905
      exc_ack       <= (others => '0');
906
      irq_ack       <= (others => '0');
907 3 zero_gravi
      exc_src       <= (others => '0');
908 2 zero_gravi
      exc_cpu_start <= '0';
909
      exc_cause     <= (others => '0');
910 3 zero_gravi
      mtinst        <= (others => '0');
911 2 zero_gravi
    elsif rising_edge(clk_i) then
912
      if (CPU_EXTENSION_RISCV_Zicsr = true) then
913
        -- exception buffer: misaligned load/store/instruction address
914
        exc_buf(exception_lalign_c) <= (exc_buf(exception_lalign_c) or ma_load_i)  and (not exc_ack(exception_lalign_c));
915
        exc_buf(exception_salign_c) <= (exc_buf(exception_salign_c) or ma_store_i) and (not exc_ack(exception_salign_c));
916
        exc_buf(exception_ialign_c) <= (exc_buf(exception_ialign_c) or ma_instr_i) and (not exc_ack(exception_ialign_c));
917
        -- exception buffer: load/store/instruction bus access error
918
        exc_buf(exception_laccess_c) <= (exc_buf(exception_laccess_c) or be_load_i)  and (not exc_ack(exception_laccess_c));
919
        exc_buf(exception_saccess_c) <= (exc_buf(exception_saccess_c) or be_store_i) and (not exc_ack(exception_saccess_c));
920
        exc_buf(exception_iaccess_c) <= (exc_buf(exception_iaccess_c) or be_instr_i) and (not exc_ack(exception_iaccess_c));
921
        -- exception buffer: illegal instruction / env call / break point
922
        exc_buf(exception_iillegal_c)  <= (exc_buf(exception_iillegal_c)  or illegal_instr_exc) and (not exc_ack(exception_iillegal_c));
923
        exc_buf(exception_m_envcall_c) <= (exc_buf(exception_m_envcall_c) or env_call)          and (not exc_ack(exception_m_envcall_c));
924
        exc_buf(exception_break_c)     <= (exc_buf(exception_break_c)     or break_point)       and (not exc_ack(exception_break_c));
925
        -- interrupt buffer: machine software/external/timer interrupt
926
        irq_buf(interrupt_msw_irq_c) <= mie_msie and (irq_buf(interrupt_msw_irq_c) or mip_msip) and (not irq_ack(interrupt_msw_irq_c));
927
        if (IO_CLIC_USE = true) then
928
          irq_buf(interrupt_mext_irq_c) <= mie_meie and (irq_buf(interrupt_mext_irq_c) or clic_irq_i) and (not irq_ack(interrupt_mext_irq_c));
929
        else
930
          irq_buf(interrupt_mext_irq_c) <= '0';
931
        end if;
932
        if (IO_MTIME_USE = true) then
933
          irq_buf(interrupt_mtime_irq_c) <= mie_mtie and (irq_buf(interrupt_mtime_irq_c) or mtime_irq_i) and (not irq_ack(interrupt_mtime_irq_c));
934
        else
935
          irq_buf(interrupt_mtime_irq_c) <= '0';
936
        end if;
937
 
938
        -- exception control --
939
        if (exc_cpu_start = '0') then -- 
940
           -- exception/interrupt triggered, waiting for IRQ in EXECUTE (make sure at least 1 instr. is executed even for a continous IRQ)
941
          if (exc_fire = '1') or ((irq_fire = '1') and ((state = EXECUTE) or (state = SLEEP))) then
942
            exc_cause     <= exc_cause_nxt; -- capture source for program
943
            mtinst        <= i_reg; -- MTINST NOT FOULLY IMPLEMENTED YET! FIXME
944
            mtinst(1)     <= not is_ci; -- bit is set for uncompressed instruction
945
            exc_src       <= exc_buf; -- capture source for hardware
946
            exc_ack       <= exc_ack_nxt; -- capture and clear with exception ACK mask
947
            irq_ack       <= irq_ack_nxt; -- capture and clear with interrupt ACK mask
948
            exc_cpu_start <= '1';
949
          end if;
950
        else -- waiting for exception handler to get started
951
          if (exc_cpu_ack = '1') then -- handler started?
952
            exc_ack <= (others => '0');
953
            irq_ack <= (others => '0');
954
            exc_cpu_start <= '0';
955
          end if;
956
        end if;
957
      else -- (CPU_EXTENSION_RISCV_Zicsr = false)
958
        exc_buf       <= (others => '0');
959
        irq_buf       <= (others => '0');
960
        exc_ack       <= (others => '0');
961
        irq_ack       <= (others => '0');
962
        exc_src       <= (others => '0');
963
        exc_cpu_start <= '0';
964
        exc_cause     <= (others => '0');
965
        mtinst        <= (others => '0');
966
      end if;
967
    end if;
968
  end process exception_controller;
969
 
970
  -- any exception/interrupt? --
971
  exc_fire <= or_all_f(exc_buf); -- classic exceptions (faults/traps) cannot be masked
972
  irq_fire <= or_all_f(irq_buf) and mstatus_mie; -- classic interrupts can be enabled/disabled
973
 
974
  -- exception acknowledge for bus unit --
975
  bus_exc_ack_o <= exc_cpu_ack;
976
 
977
  -- exception priority encoder --
978
  exc_priority: process(exc_buf, irq_buf)
979
  begin
980
    -- defaults --
981
    exc_cause_nxt <= (others => '0');
982
    exc_ack_nxt   <= (others => '0');
983
    irq_ack_nxt   <= (others => '0');
984
 
985
    -- interrupt: 1.11 machine external interrupt --
986
    if (irq_buf(interrupt_mext_irq_c) = '1') then
987
      exc_cause_nxt(exc_cause_nxt'left) <= '1';
988
      exc_cause_nxt(3 downto 0) <= "1011";
989
      irq_ack_nxt(interrupt_mext_irq_c) <= '1';
990
 
991
    -- interrupt: 1.7 machine timer interrupt --
992
    elsif (irq_buf(interrupt_mtime_irq_c) = '1') then
993
      exc_cause_nxt(exc_cause_nxt'left) <= '1';
994
      exc_cause_nxt(3 downto 0) <= "0111";
995
      irq_ack_nxt(interrupt_mtime_irq_c) <= '1';
996
 
997
    -- interrupt: 1.3 machine SW interrupt --
998
    elsif (irq_buf(interrupt_msw_irq_c) = '1') then
999
      exc_cause_nxt(exc_cause_nxt'left) <= '1';
1000
      exc_cause_nxt(3 downto 0) <= "0011";
1001
      irq_ack_nxt(interrupt_msw_irq_c) <= '1';
1002
 
1003
 
1004
    -- trap/fault: 0.0 instruction address misaligned --
1005
    elsif (exc_buf(exception_ialign_c) = '1') then
1006
      exc_cause_nxt(exc_cause_nxt'left) <= '0';
1007
      exc_cause_nxt(3 downto 0) <= "0000";
1008
      exc_ack_nxt(exception_ialign_c)   <= '1';
1009
      exc_ack_nxt(exception_iaccess_c)  <= '1';
1010
      exc_ack_nxt(exception_iillegal_c) <= '1';
1011
 
1012
    -- trap/fault: 0.1 instruction access fault --
1013
    elsif (exc_buf(exception_iaccess_c) = '1') then
1014
      exc_cause_nxt(exc_cause_nxt'left) <= '0';
1015
      exc_cause_nxt(3 downto 0) <= "0001";
1016
      exc_ack_nxt(exception_ialign_c)   <= '1';
1017
      exc_ack_nxt(exception_iaccess_c)  <= '1';
1018
      exc_ack_nxt(exception_iillegal_c) <= '1';
1019
 
1020
    -- trap/fault: 0.2 illegal instruction --
1021
    elsif (exc_buf(exception_iillegal_c) = '1') then
1022
      exc_cause_nxt(exc_cause_nxt'left) <= '0';
1023
      exc_cause_nxt(3 downto 0) <= "0010";
1024
      exc_ack_nxt(exception_ialign_c)   <= '1';
1025
      exc_ack_nxt(exception_iaccess_c)  <= '1';
1026
      exc_ack_nxt(exception_iillegal_c) <= '1';
1027
 
1028
 
1029
    -- trap/fault: 0.11 environment call from M-mode --
1030
    elsif (exc_buf(exception_m_envcall_c) = '1') then
1031
      exc_cause_nxt(exc_cause_nxt'left) <= '0';
1032
      exc_cause_nxt(3 downto 0) <= "1011";
1033
      exc_ack_nxt(exception_m_envcall_c) <= '1';
1034
 
1035
    -- trap/fault: 0.3 breakpoint --
1036
    elsif (exc_buf(exception_break_c) = '1') then
1037
      exc_cause_nxt(exc_cause_nxt'left) <= '0';
1038
      exc_cause_nxt(3 downto 0) <= "0011";
1039
      exc_ack_nxt(exception_break_c) <= '1';
1040
 
1041
 
1042
    -- trap/fault: 0.6 store address misaligned -
1043
    elsif (exc_buf(exception_salign_c) = '1') then
1044
      exc_cause_nxt(exc_cause_nxt'left) <= '0';
1045
      exc_cause_nxt(3 downto 0) <= "0110";
1046
      exc_ack_nxt(exception_salign_c)  <= '1';
1047
      exc_ack_nxt(exception_lalign_c)  <= '1';
1048
      exc_ack_nxt(exception_saccess_c) <= '1';
1049
      exc_ack_nxt(exception_laccess_c) <= '1';
1050
 
1051
    -- trap/fault: 0.4 load address misaligned --
1052
    elsif (exc_buf(exception_lalign_c) = '1') then
1053
      exc_cause_nxt(exc_cause_nxt'left) <= '0';
1054
      exc_cause_nxt(3 downto 0) <= "0100";
1055
      exc_ack_nxt(exception_salign_c)  <= '1';
1056
      exc_ack_nxt(exception_lalign_c)  <= '1';
1057
      exc_ack_nxt(exception_saccess_c) <= '1';
1058
      exc_ack_nxt(exception_laccess_c) <= '1';
1059
 
1060
    -- trap/fault: 0.7 store access fault --
1061
    elsif (exc_buf(exception_saccess_c) = '1') then
1062
      exc_cause_nxt(exc_cause_nxt'left) <= '0';
1063
      exc_cause_nxt(3 downto 0) <= "0111";
1064
      exc_ack_nxt(exception_salign_c)  <= '1';
1065
      exc_ack_nxt(exception_lalign_c)  <= '1';
1066
      exc_ack_nxt(exception_saccess_c) <= '1';
1067
      exc_ack_nxt(exception_laccess_c) <= '1';
1068
 
1069
    -- trap/fault: 0.5 load access fault --
1070
    elsif (exc_buf(exception_laccess_c) = '1') then
1071
      exc_cause_nxt(exc_cause_nxt'left) <= '0';
1072
      exc_cause_nxt(3 downto 0) <= "0101";
1073
      exc_ack_nxt(exception_salign_c)  <= '1';
1074
      exc_ack_nxt(exception_lalign_c)  <= '1';
1075
      exc_ack_nxt(exception_saccess_c) <= '1';
1076
      exc_ack_nxt(exception_laccess_c) <= '1';
1077
 
1078
    -- undefined / not implemented --
1079
    else
1080
      exc_cause_nxt <= (others => '0'); -- default
1081
      exc_ack_nxt   <= (others => '0'); -- default
1082
    end if;
1083
  end process exc_priority;
1084
 
1085
 
1086
  -- Control and Status Registers Write Access ----------------------------------------------
1087
  -- -------------------------------------------------------------------------------------------
1088
  csr_write_access: process(rstn_i, clk_i)
1089
  begin
1090
    if (rstn_i = '0') then
1091
      mstatus_mie  <= '0';
1092
      mstatus_mpie <= '0';
1093
      mie_msie     <= '0';
1094
      mie_meie     <= '0';
1095
      mie_mtie     <= '0';
1096 3 zero_gravi
      mtvec        <= (others => '0');
1097
      mtval        <= (others => '0');
1098
      mepc         <= (others => '0');
1099
      mip_msip     <= '0';
1100 2 zero_gravi
    elsif rising_edge(clk_i) then
1101
      if (CPU_EXTENSION_RISCV_Zicsr = true) then
1102
        mip_msip <= '0';
1103
        -- register that can be modified by user --
1104
        if (ctrl(ctrl_csr_we_c) = '1') then -- manual update
1105
          case i_reg(31 downto 20) is
1106
            -- machine trap setup --
1107
            when x"300" => -- R/W: mstatus - machine status register
1108
              mstatus_mie  <= csr_wdata_i(03);
1109
              mstatus_mpie <= csr_wdata_i(07);
1110
            when x"304" => -- R/W: mie - machine interrupt-enable register
1111
              mie_msie <= csr_wdata_i(03); -- SW IRQ enable
1112
              if (IO_MTIME_USE = true) then
1113
                mie_mtie <= csr_wdata_i(07); -- TIMER IRQ enable
1114
              end if;
1115
              if (IO_CLIC_USE = true) then
1116
                mie_meie <= csr_wdata_i(11); -- EXT IRQ enable
1117
              end if;
1118
            when x"305" => -- R/W: mtvec - machine trap-handler base address (for ALL exceptions)
1119
              mtvec <= csr_wdata_i;
1120
            -- machine trap handling --
1121
            when x"340" => -- R/W: mscratch - machine scratch register
1122
              mscratch <= csr_wdata_i;
1123
            when x"344" => -- R/W: mip - machine interrupt pending
1124
              mip_msip <= csr_wdata_i(03); -- manual SW IRQ trigger
1125
            -- machine trap handling --
1126
            when x"341" => -- R/W: mepc - machine exception program counter
1127
              mepc <= csr_wdata_i;
1128
            -- undefined/unavailable --
1129
            when others =>
1130
              NULL;
1131
          end case;
1132
 
1133
        else -- automatic update by hardware
1134
          -- machine exception PC & exception value register --
1135
          if (exc_cpu_ack = '1') then -- exception start?
1136
            if (exc_cause(exc_cause_nxt'left) = '1') then -- for INTERRUPTs: mepc = address of next (unclompeted) instruction
1137
              mepc  <= pc_reg;
1138 3 zero_gravi
              mtval <= (others => '0'); -- not specified
1139 2 zero_gravi
            else -- for EXCEPTIONs: mepc = address of next (unclompeted) instruction
1140
              mepc <= pc_backup2_reg;
1141
              if ((exc_src(exception_iaccess_c) or exc_src(exception_ialign_c)) = '1') then -- instruction access error OR misaligned instruction
1142
                mtval <= pc_backup_reg;
1143
              elsif (exc_src(exception_iillegal_c) = '1') then -- illegal instruction
1144
                mtval <= i_reg;
1145
              elsif ((exc_src(exception_lalign_c)  or exc_src(exception_salign_c) or
1146
                      exc_src(exception_laccess_c) or exc_src(exception_saccess_c)) = '1') then -- load/store misaligned / access error
1147
                mtval <= mar_i;
1148
              end if;
1149
            end if;
1150
          end if;
1151
 
1152
          -- context switch in mstatus --
1153
          if (exc_cpu_ack = '1') then -- actually entering trap
1154
            mstatus_mie <= '0';
1155
            if (mstatus_mpie = '0') then -- FIXME: prevent loosing the prev MIE state after several traps
1156
              mstatus_mpie <= mstatus_mie;
1157
            end if;
1158
          elsif (exc_cpu_end = '1') then -- return from exception
1159
            mstatus_mie <= mstatus_mpie;
1160
          end if;
1161
        end if;
1162
 
1163
      else -- CPU_EXTENSION_RISCV_Zicsr = false
1164
        mstatus_mie  <= '0';
1165
        mstatus_mpie <= '0';
1166
        mie_msie     <= '0';
1167
        mie_meie     <= '0';
1168
        mie_mtie     <= '0';
1169
        mtvec        <= (others => '0');
1170
        mtval        <= (others => '0');
1171
        mepc         <= (others => '0');
1172
        mip_msip     <= '0';
1173
      end if;
1174
    end if;
1175
  end process csr_write_access;
1176
 
1177
 
1178
  -- Control and Status Registers Read Access -----------------------------------------------
1179
  -- -------------------------------------------------------------------------------------------
1180
  csr_read_access: process(clk_i)
1181
  begin
1182
    if rising_edge(clk_i) then
1183
      csr_rdata_o <= (others => '0'); -- default
1184
      if (CPU_EXTENSION_RISCV_Zicsr = true) then -- implement CSR access at all?
1185
        if (ctrl(ctrl_csr_re_c) = '1') then
1186
          case i_reg(31 downto 20) is
1187
            -- machine trap setup --
1188
            when x"300" => -- R/W: mstatus - machine status register
1189
              csr_rdata_o(03) <= mstatus_mie; -- MIE
1190
              csr_rdata_o(07) <= mstatus_mpie; -- MPIE
1191
              csr_rdata_o(11) <= '1'; -- MPP low
1192
              csr_rdata_o(12) <= '1'; -- MPP high
1193
            when x"301" => -- R/-: misa - ISA and extensions
1194
              csr_rdata_o(02) <= bool_to_ulogic_f(CPU_EXTENSION_RISCV_C);     -- C CPU extension
1195
              csr_rdata_o(04) <= bool_to_ulogic_f(CPU_EXTENSION_RISCV_E);     -- E CPU extension
1196
              csr_rdata_o(08) <= not bool_to_ulogic_f(CPU_EXTENSION_RISCV_E); -- I CPU extension (if not E)
1197
              csr_rdata_o(12) <= bool_to_ulogic_f(CPU_EXTENSION_RISCV_M);     -- M CPU extension
1198
              csr_rdata_o(23) <= '1';                                         -- X CPU extension: non-standard extensions
1199
              csr_rdata_o(25) <= bool_to_ulogic_f(CPU_EXTENSION_RISCV_Zicsr); -- Z CPU extension
1200
              csr_rdata_o(30) <= '1'; -- 32-bit architecture (MXL lo)
1201
              csr_rdata_o(31) <= '0'; -- 32-bit architecture (MXL hi)
1202
            when x"304" => -- R/W: mie - machine interrupt-enable register
1203
              csr_rdata_o(03) <= mie_msie; -- software IRQ enable
1204
              csr_rdata_o(07) <= mie_mtie; -- timer IRQ enable
1205
              csr_rdata_o(11) <= mie_meie; -- external IRQ enable
1206
            when x"305" => -- R/W: mtvec - machine trap-handler base address (for ALL exceptions)
1207
              csr_rdata_o <= mtvec;
1208
            -- machine trap handling --
1209
            when x"340" => -- R/W: mscratch - machine scratch register
1210
              csr_rdata_o <= mscratch;
1211
            when x"341" => -- R/W: mepc - machine exception program counter
1212
              csr_rdata_o <= mepc;
1213
            when x"342" => -- R/-: mcause - machine trap cause
1214
              csr_rdata_o <= exc_cause;
1215
            when x"343" => -- R/-: mtval - machine bad address or instruction
1216
              csr_rdata_o <= mtval;
1217
            when x"344" => -- R/W: mip - machine interrupt pending
1218
              csr_rdata_o(03) <= irq_buf(interrupt_msw_irq_c);
1219
              csr_rdata_o(07) <= irq_buf(interrupt_mtime_irq_c);
1220
              csr_rdata_o(11) <= irq_buf(interrupt_mext_irq_c);
1221
            when x"34a" => -- R/-: mtinst - machine trap instruction (transformed)
1222
              csr_rdata_o <= mtinst;
1223
            -- counter and timers --
1224
            when x"c00" | x"c01" | x"b00" => -- R/-: cycle/time/mcycle: Cycle counter LOW / Timer LOW
1225
              csr_rdata_o <= cycle_lo(31 downto 0);
1226
            when x"c02" | x"b02" => -- R/-: instret/minstret: Instructions-retired counter LOW
1227
              csr_rdata_o <= instret_lo(31 downto 0);
1228
            when x"c80" | x"c81" | x"b80" => -- R/-: cycleh/timeh/mcycleh: Cycle counter HIGH / Timer HIGH
1229
              csr_rdata_o(15 downto 0) <= cycle_hi; -- counter is only 16-bit wide!
1230
            when x"c82" | x"b82" => -- R/-: instreth/minstreth: Instructions-retired counter HIGH
1231
              csr_rdata_o(15 downto 0) <= instret_hi; -- counter is only 16-bit wide!
1232
            -- machine information registers --
1233
            when x"f13" => -- R/-: mimpid - implementation ID / version
1234
              csr_rdata_o <= hw_version_c;
1235
            when x"f14" => -- R/-: mhartid - hardware thread ID
1236
              csr_rdata_o <= HART_ID;
1237
            when x"fff" => -- R/-: mhwctrl - hardware controller
1238
              csr_rdata_o <= hw_control;
1239
            -- CUSTOM read-only machine CSRs --
1240
            when x"fc0" => -- R/-: mfeatures - implemented processor devices/features
1241
              csr_rdata_o(00) <= bool_to_ulogic_f(BOOTLOADER_USE);   -- implement processor-internal bootloader?
1242
              csr_rdata_o(01) <= bool_to_ulogic_f(MEM_EXT_USE);      -- implement external memory bus interface?
1243
              csr_rdata_o(02) <= bool_to_ulogic_f(MEM_INT_IMEM_USE); -- implement processor-internal instruction memory
1244
              csr_rdata_o(03) <= bool_to_ulogic_f(MEM_INT_IMEM_ROM); -- implement processor-internal instruction memory as ROM
1245
              csr_rdata_o(04) <= bool_to_ulogic_f(MEM_INT_DMEM_USE); -- implement processor-internal data memory
1246
              --
1247 3 zero_gravi
              csr_rdata_o(16) <= bool_to_ulogic_f(IO_GPIO_USE);      -- implement general purpose input/output port unit (GPIO)?
1248
              csr_rdata_o(17) <= bool_to_ulogic_f(IO_MTIME_USE);     -- implement machine system timer (MTIME)?
1249
              csr_rdata_o(18) <= bool_to_ulogic_f(IO_UART_USE);      -- implement universal asynchronous receiver/transmitter (UART)?
1250
              csr_rdata_o(19) <= bool_to_ulogic_f(IO_SPI_USE);       -- implement serial peripheral interface (SPI)?
1251
              csr_rdata_o(20) <= bool_to_ulogic_f(IO_TWI_USE);       -- implement two-wire interface (TWI)?
1252
              csr_rdata_o(21) <= bool_to_ulogic_f(IO_PWM_USE);       -- implement pulse-width modulation unit (PWM)?
1253
              csr_rdata_o(22) <= bool_to_ulogic_f(IO_WDT_USE);       -- implement watch dog timer (WDT)?
1254
              csr_rdata_o(23) <= bool_to_ulogic_f(IO_CLIC_USE);      -- implement core local interrupt controller (CLIC)?
1255
              csr_rdata_o(24) <= bool_to_ulogic_f(IO_TRNG_USE);      -- implement true random number generator (TRNG)?
1256
              csr_rdata_o(25) <= bool_to_ulogic_f(IO_DEVNULL_USE);   -- implement dummy device (DEVNULL)?
1257 2 zero_gravi
            when x"fc1" => -- R/-: mclock - processor clock speed
1258
              csr_rdata_o <= std_ulogic_vector(to_unsigned(CLOCK_FREQUENCY, 32));
1259
            when x"fc4" => -- R/-: mispacebase - Base address of instruction memory space
1260
              csr_rdata_o <= MEM_ISPACE_BASE;
1261
            when x"fc5" => -- R/-: mdspacebase - Base address of data memory space
1262
              csr_rdata_o <= MEM_DSPACE_BASE;
1263
            when x"fc6" => -- R/-: mispacesize - Total size of instruction memory space in byte
1264
              csr_rdata_o <= std_ulogic_vector(to_unsigned(MEM_ISPACE_SIZE, 32));
1265
            when x"fc7" => -- R/-: mdspacesize - Total size of data memory space in byte
1266
              csr_rdata_o <= std_ulogic_vector(to_unsigned(MEM_DSPACE_SIZE, 32));
1267
            -- undefined/unavailable --
1268
            when others =>
1269
              csr_rdata_o <= (others => '0'); -- not implemented (yet)
1270
          end case;
1271
        end if;
1272
      end if;
1273
    end if;
1274
  end process csr_read_access;
1275
 
1276
 
1277
  -- Optional RISC-V CSRs: Counters ---------------------------------------------------------
1278
  -- -------------------------------------------------------------------------------------------
1279
  csr_counters: process(rstn_i, clk_i)
1280
  begin
1281
    if rising_edge(clk_i) then
1282
      if (rstn_i = '0') then
1283
        cycle_lo       <= (others => '0');
1284
        instret_lo     <= (others => '0');
1285
        cycle_hi       <= (others => '0');
1286
        instret_hi     <= (others => '0');
1287
        cycle_lo_msb   <= '0';
1288
        instret_lo_msb <= '0';
1289
      elsif (CPU_EXTENSION_RISCV_E = false) then
1290
        -- low word overflow buffers --
1291
        cycle_lo_msb   <= cycle_lo(cycle_lo'left);
1292
        instret_lo_msb <= instret_lo(instret_lo'left);
1293
        -- low word counters --
1294
        cycle_lo <= std_ulogic_vector(unsigned(cycle_lo) + 1);
1295
        if (state = EXECUTE) then
1296
          instret_lo <= std_ulogic_vector(unsigned(instret_lo) + 1);
1297
        end if;
1298
        -- high word counters --
1299
        if ((cycle_lo_msb xor cycle_lo(cycle_lo'left)) = '1') then
1300
          cycle_hi <= std_ulogic_vector(unsigned(cycle_hi) + 1);
1301
        end if;
1302
        if ((instret_lo_msb xor instret_lo(instret_lo'left)) = '1') then
1303
          instret_hi <= std_ulogic_vector(unsigned(instret_hi) + 1);
1304
        end if;
1305
      else -- counters are not available in embedded mode
1306
        cycle_lo       <= (others => '0');
1307
        instret_lo     <= (others => '0');
1308
        cycle_hi       <= (others => '0');
1309
        instret_hi     <= (others => '0');
1310
        cycle_lo_msb   <= '0';
1311
        instret_lo_msb <= '0';
1312
      end if;
1313
    end if;
1314
  end process csr_counters;
1315
 
1316
 
1317
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.