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