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

Subversion Repositories neorv32

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

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