| 1 |
2 |
zero_gravi |
-- #################################################################################################
|
| 2 |
|
|
-- # << NEORV32 - CPU Top Entity >> #
|
| 3 |
|
|
-- # ********************************************************************************************* #
|
| 4 |
18 |
zero_gravi |
-- # NEORV32 CPU: #
|
| 5 |
36 |
zero_gravi |
-- # * neorv32_cpu.vhd - CPU top entity #
|
| 6 |
|
|
-- # * neorv32_cpu_alu.vhd - Arithmetic/logic unit #
|
| 7 |
|
|
-- # * neorv32_cpu_bus.vhd - Instruction and data bus interface unit #
|
| 8 |
|
|
-- # * neorv32_cpu_cp_muldiv.vhd - MULDIV co-processor #
|
| 9 |
|
|
-- # * neorv32_cpu_ctrl.vhd - CPU control and CSR system #
|
| 10 |
|
|
-- # * neorv32_cpu_decompressor.vhd - Compressed instructions decoder #
|
| 11 |
|
|
-- # * neorv32_cpu_regfile.vhd - Data register file #
|
| 12 |
38 |
zero_gravi |
-- # * neorv32_package.vhd - Main CPU/processor package file #
|
| 13 |
|
|
-- # #
|
| 14 |
29 |
zero_gravi |
-- # Check out the processor's data sheet for more information: docs/NEORV32.pdf #
|
| 15 |
2 |
zero_gravi |
-- # ********************************************************************************************* #
|
| 16 |
|
|
-- # BSD 3-Clause License #
|
| 17 |
|
|
-- # #
|
| 18 |
42 |
zero_gravi |
-- # Copyright (c) 2021, Stephan Nolting. All rights reserved. #
|
| 19 |
2 |
zero_gravi |
-- # #
|
| 20 |
|
|
-- # Redistribution and use in source and binary forms, with or without modification, are #
|
| 21 |
|
|
-- # permitted provided that the following conditions are met: #
|
| 22 |
|
|
-- # #
|
| 23 |
|
|
-- # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
| 24 |
|
|
-- # conditions and the following disclaimer. #
|
| 25 |
|
|
-- # #
|
| 26 |
|
|
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
| 27 |
|
|
-- # conditions and the following disclaimer in the documentation and/or other materials #
|
| 28 |
|
|
-- # provided with the distribution. #
|
| 29 |
|
|
-- # #
|
| 30 |
|
|
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
| 31 |
|
|
-- # endorse or promote products derived from this software without specific prior written #
|
| 32 |
|
|
-- # permission. #
|
| 33 |
|
|
-- # #
|
| 34 |
|
|
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
| 35 |
|
|
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
| 36 |
|
|
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
| 37 |
|
|
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
| 38 |
|
|
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
| 39 |
|
|
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
| 40 |
|
|
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
| 41 |
|
|
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
| 42 |
|
|
-- # OF THE POSSIBILITY OF SUCH DAMAGE. #
|
| 43 |
|
|
-- # ********************************************************************************************* #
|
| 44 |
|
|
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32 (c) Stephan Nolting #
|
| 45 |
|
|
-- #################################################################################################
|
| 46 |
|
|
|
| 47 |
|
|
library ieee;
|
| 48 |
|
|
use ieee.std_logic_1164.all;
|
| 49 |
|
|
use ieee.numeric_std.all;
|
| 50 |
|
|
|
| 51 |
|
|
library neorv32;
|
| 52 |
|
|
use neorv32.neorv32_package.all;
|
| 53 |
|
|
|
| 54 |
|
|
entity neorv32_cpu is
|
| 55 |
|
|
generic (
|
| 56 |
|
|
-- General --
|
| 57 |
14 |
zero_gravi |
HW_THREAD_ID : std_ulogic_vector(31 downto 0):= (others => '0'); -- hardware thread id
|
| 58 |
|
|
CPU_BOOT_ADDR : std_ulogic_vector(31 downto 0):= (others => '0'); -- cpu boot address
|
| 59 |
41 |
zero_gravi |
BUS_TIMEOUT : natural := 63; -- cycles after an UNACKNOWLEDGED bus access triggers a bus fault exception
|
| 60 |
2 |
zero_gravi |
-- RISC-V CPU Extensions --
|
| 61 |
39 |
zero_gravi |
CPU_EXTENSION_RISCV_A : boolean := false; -- implement atomic extension?
|
| 62 |
44 |
zero_gravi |
CPU_EXTENSION_RISCV_B : boolean := false; -- implement bit manipulation extensions?
|
| 63 |
12 |
zero_gravi |
CPU_EXTENSION_RISCV_C : boolean := false; -- implement compressed extension?
|
| 64 |
|
|
CPU_EXTENSION_RISCV_E : boolean := false; -- implement embedded RF extension?
|
| 65 |
|
|
CPU_EXTENSION_RISCV_M : boolean := false; -- implement muld/div extension?
|
| 66 |
15 |
zero_gravi |
CPU_EXTENSION_RISCV_U : boolean := false; -- implement user mode extension?
|
| 67 |
12 |
zero_gravi |
CPU_EXTENSION_RISCV_Zicsr : boolean := true; -- implement CSR system?
|
| 68 |
|
|
CPU_EXTENSION_RISCV_Zifencei : boolean := true; -- implement instruction stream sync.?
|
| 69 |
19 |
zero_gravi |
-- Extension Options --
|
| 70 |
|
|
FAST_MUL_EN : boolean := false; -- use DSPs for M extension's multiplier
|
| 71 |
34 |
zero_gravi |
FAST_SHIFT_EN : boolean := false; -- use barrel shifter for shift operations
|
| 72 |
15 |
zero_gravi |
-- Physical Memory Protection (PMP) --
|
| 73 |
42 |
zero_gravi |
PMP_NUM_REGIONS : natural := 0; -- number of regions (0..64)
|
| 74 |
|
|
PMP_MIN_GRANULARITY : natural := 64*1024; -- minimal region granularity in bytes, has to be a power of 2, min 8 bytes
|
| 75 |
|
|
-- Hardware Performance Monitors (HPM) --
|
| 76 |
|
|
HPM_NUM_CNTS : natural := 0 -- number of inmplemnted HPM counters (0..29)
|
| 77 |
2 |
zero_gravi |
);
|
| 78 |
|
|
port (
|
| 79 |
|
|
-- global control --
|
| 80 |
14 |
zero_gravi |
clk_i : in std_ulogic := '0'; -- global clock, rising edge
|
| 81 |
|
|
rstn_i : in std_ulogic := '0'; -- global reset, low-active, async
|
| 82 |
12 |
zero_gravi |
-- instruction bus interface --
|
| 83 |
|
|
i_bus_addr_o : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
|
| 84 |
14 |
zero_gravi |
i_bus_rdata_i : in std_ulogic_vector(data_width_c-1 downto 0) := (others => '0'); -- bus read data
|
| 85 |
12 |
zero_gravi |
i_bus_wdata_o : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
|
| 86 |
|
|
i_bus_ben_o : out std_ulogic_vector(03 downto 0); -- byte enable
|
| 87 |
|
|
i_bus_we_o : out std_ulogic; -- write enable
|
| 88 |
|
|
i_bus_re_o : out std_ulogic; -- read enable
|
| 89 |
|
|
i_bus_cancel_o : out std_ulogic; -- cancel current bus transaction
|
| 90 |
14 |
zero_gravi |
i_bus_ack_i : in std_ulogic := '0'; -- bus transfer acknowledge
|
| 91 |
|
|
i_bus_err_i : in std_ulogic := '0'; -- bus transfer error
|
| 92 |
12 |
zero_gravi |
i_bus_fence_o : out std_ulogic; -- executed FENCEI operation
|
| 93 |
35 |
zero_gravi |
i_bus_priv_o : out std_ulogic_vector(1 downto 0); -- privilege level
|
| 94 |
39 |
zero_gravi |
i_bus_lock_o : out std_ulogic; -- locked/exclusive access
|
| 95 |
12 |
zero_gravi |
-- data bus interface --
|
| 96 |
|
|
d_bus_addr_o : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
|
| 97 |
14 |
zero_gravi |
d_bus_rdata_i : in std_ulogic_vector(data_width_c-1 downto 0) := (others => '0'); -- bus read data
|
| 98 |
12 |
zero_gravi |
d_bus_wdata_o : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
|
| 99 |
|
|
d_bus_ben_o : out std_ulogic_vector(03 downto 0); -- byte enable
|
| 100 |
|
|
d_bus_we_o : out std_ulogic; -- write enable
|
| 101 |
|
|
d_bus_re_o : out std_ulogic; -- read enable
|
| 102 |
|
|
d_bus_cancel_o : out std_ulogic; -- cancel current bus transaction
|
| 103 |
14 |
zero_gravi |
d_bus_ack_i : in std_ulogic := '0'; -- bus transfer acknowledge
|
| 104 |
|
|
d_bus_err_i : in std_ulogic := '0'; -- bus transfer error
|
| 105 |
12 |
zero_gravi |
d_bus_fence_o : out std_ulogic; -- executed FENCE operation
|
| 106 |
35 |
zero_gravi |
d_bus_priv_o : out std_ulogic_vector(1 downto 0); -- privilege level
|
| 107 |
39 |
zero_gravi |
d_bus_lock_o : out std_ulogic; -- locked/exclusive access
|
| 108 |
11 |
zero_gravi |
-- system time input from MTIME --
|
| 109 |
14 |
zero_gravi |
time_i : in std_ulogic_vector(63 downto 0) := (others => '0'); -- current system time
|
| 110 |
|
|
-- interrupts (risc-v compliant) --
|
| 111 |
|
|
msw_irq_i : in std_ulogic := '0'; -- machine software interrupt
|
| 112 |
|
|
mext_irq_i : in std_ulogic := '0'; -- machine external interrupt
|
| 113 |
|
|
mtime_irq_i : in std_ulogic := '0'; -- machine timer interrupt
|
| 114 |
|
|
-- fast interrupts (custom) --
|
| 115 |
|
|
firq_i : in std_ulogic_vector(3 downto 0) := (others => '0')
|
| 116 |
2 |
zero_gravi |
);
|
| 117 |
|
|
end neorv32_cpu;
|
| 118 |
|
|
|
| 119 |
|
|
architecture neorv32_cpu_rtl of neorv32_cpu is
|
| 120 |
|
|
|
| 121 |
|
|
-- local signals --
|
| 122 |
12 |
zero_gravi |
signal ctrl : std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
|
| 123 |
|
|
signal alu_cmp : std_ulogic_vector(1 downto 0); -- alu comparator result
|
| 124 |
|
|
signal imm : std_ulogic_vector(data_width_c-1 downto 0); -- immediate
|
| 125 |
|
|
signal instr : std_ulogic_vector(data_width_c-1 downto 0); -- new instruction
|
| 126 |
|
|
signal rs1, rs2 : std_ulogic_vector(data_width_c-1 downto 0); -- source registers
|
| 127 |
|
|
signal alu_res : std_ulogic_vector(data_width_c-1 downto 0); -- alu result
|
| 128 |
36 |
zero_gravi |
signal alu_add : std_ulogic_vector(data_width_c-1 downto 0); -- alu address result
|
| 129 |
12 |
zero_gravi |
signal rdata : std_ulogic_vector(data_width_c-1 downto 0); -- memory read data
|
| 130 |
|
|
signal alu_wait : std_ulogic; -- alu is busy due to iterative unit
|
| 131 |
|
|
signal bus_i_wait : std_ulogic; -- wait for current bus instruction fetch
|
| 132 |
|
|
signal bus_d_wait : std_ulogic; -- wait for current bus data access
|
| 133 |
|
|
signal csr_rdata : std_ulogic_vector(data_width_c-1 downto 0); -- csr read data
|
| 134 |
|
|
signal mar : std_ulogic_vector(data_width_c-1 downto 0); -- current memory address register
|
| 135 |
|
|
signal ma_instr : std_ulogic; -- misaligned instruction address
|
| 136 |
|
|
signal ma_load : std_ulogic; -- misaligned load data address
|
| 137 |
|
|
signal ma_store : std_ulogic; -- misaligned store data address
|
| 138 |
|
|
signal be_instr : std_ulogic; -- bus error on instruction access
|
| 139 |
|
|
signal be_load : std_ulogic; -- bus error on load data access
|
| 140 |
|
|
signal be_store : std_ulogic; -- bus error on store data access
|
| 141 |
|
|
signal fetch_pc : std_ulogic_vector(data_width_c-1 downto 0); -- pc for instruction fetch
|
| 142 |
|
|
signal curr_pc : std_ulogic_vector(data_width_c-1 downto 0); -- current pc (for current executed instruction)
|
| 143 |
2 |
zero_gravi |
|
| 144 |
|
|
-- co-processor interface --
|
| 145 |
36 |
zero_gravi |
signal cp0_data, cp1_data, cp2_data, cp3_data : std_ulogic_vector(data_width_c-1 downto 0);
|
| 146 |
|
|
signal cp0_valid, cp1_valid, cp2_valid, cp3_valid : std_ulogic;
|
| 147 |
|
|
signal cp0_start, cp1_start, cp2_start, cp3_start : std_ulogic;
|
| 148 |
2 |
zero_gravi |
|
| 149 |
15 |
zero_gravi |
-- pmp interface --
|
| 150 |
|
|
signal pmp_addr : pmp_addr_if_t;
|
| 151 |
|
|
signal pmp_ctrl : pmp_ctrl_if_t;
|
| 152 |
|
|
|
| 153 |
2 |
zero_gravi |
begin
|
| 154 |
|
|
|
| 155 |
15 |
zero_gravi |
-- Sanity Checks --------------------------------------------------------------------------
|
| 156 |
|
|
-- -------------------------------------------------------------------------------------------
|
| 157 |
23 |
zero_gravi |
-- CSR system --
|
| 158 |
41 |
zero_gravi |
assert not (CPU_EXTENSION_RISCV_Zicsr = false) report "NEORV32 CPU CONFIG WARNING! No exception/interrupt/trap/privileged features available when CPU_EXTENSION_RISCV_Zicsr = false." severity warning;
|
| 159 |
23 |
zero_gravi |
-- U-extension requires Zicsr extension --
|
| 160 |
|
|
assert not ((CPU_EXTENSION_RISCV_Zicsr = false) and (CPU_EXTENSION_RISCV_U = true)) report "NEORV32 CPU CONFIG ERROR! User mode requires CPU_EXTENSION_RISCV_Zicsr extension." severity error;
|
| 161 |
|
|
-- PMP requires Zicsr extension --
|
| 162 |
42 |
zero_gravi |
assert not ((CPU_EXTENSION_RISCV_Zicsr = false) and (PMP_NUM_REGIONS > 0)) report "NEORV32 CPU CONFIG ERROR! Physical memory protection (PMP) requires CPU_EXTENSION_RISCV_Zicsr extension." severity error;
|
| 163 |
40 |
zero_gravi |
|
| 164 |
41 |
zero_gravi |
-- Bus timeout --
|
| 165 |
|
|
assert not (BUS_TIMEOUT < 2) report "NEORV32 CPU CONFIG ERROR! Invalid bus access timeout value <BUS_TIMEOUT>. Has to be >= 2." severity error;
|
| 166 |
|
|
|
| 167 |
38 |
zero_gravi |
-- Instruction prefetch buffer size --
|
| 168 |
|
|
assert not (is_power_of_two_f(ipb_entries_c) = false) report "NEORV32 CPU CONFIG ERROR! Number of entries in instruction prefetch buffer <ipb_entries_c> has to be a power of two." severity error;
|
| 169 |
39 |
zero_gravi |
-- A extension - only lr.w and sc.w supported yet --
|
| 170 |
|
|
assert not (CPU_EXTENSION_RISCV_A = true) report "NEORV32 CPU CONFIG WARNING! Atomic operations extension (A) only supports >lr.w< and >sc.w< instructions yet." severity warning;
|
| 171 |
15 |
zero_gravi |
|
| 172 |
44 |
zero_gravi |
-- Bit manipulation notifier --
|
| 173 |
|
|
assert not (CPU_EXTENSION_RISCV_B = true) report "NEORV32 CPU CONFIG WARNING! Bit manipulation extension (B) only supports 'base' instruction sub-set (Zbb) yet and is still 'unofficial' (not-ratified)." severity warning;
|
| 174 |
|
|
|
| 175 |
40 |
zero_gravi |
-- PMP regions check --
|
| 176 |
42 |
zero_gravi |
assert not (PMP_NUM_REGIONS > 64) report "NEORV32 CPU CONFIG ERROR! Number of PMP regions <PMP_NUM_REGIONS> out of valid range (0..64)." severity error;
|
| 177 |
40 |
zero_gravi |
-- PMP granulartiy --
|
| 178 |
42 |
zero_gravi |
assert not ((is_power_of_two_f(PMP_MIN_GRANULARITY) = false) and (PMP_NUM_REGIONS > 0)) report "NEORV32 CPU CONFIG ERROR! PMP granulartiy has to be a power of two." severity error;
|
| 179 |
|
|
assert not ((PMP_MIN_GRANULARITY < 8) and (PMP_NUM_REGIONS > 0)) report "NEORV32 CPU CONFIG ERROR! PMP granulartiy has to be >= 8 bytes." severity error;
|
| 180 |
40 |
zero_gravi |
-- PMP notifier --
|
| 181 |
42 |
zero_gravi |
assert not (PMP_NUM_REGIONS > 0) report "NEORV32 CPU CONFIG NOTE: Implementing physical memory protection (PMP) with " & integer'image(PMP_NUM_REGIONS) & " regions and a minimal granularity of " & integer'image(PMP_MIN_GRANULARITY) & " bytes." severity note;
|
| 182 |
40 |
zero_gravi |
|
| 183 |
42 |
zero_gravi |
-- HPM counters check --
|
| 184 |
|
|
assert not (HPM_NUM_CNTS > 29) report "NEORV32 CPU CONFIG ERROR! Number of HPM counters <HPM_NUM_CNTS> out of valid range (0..29)." severity error;
|
| 185 |
|
|
-- HPM counters notifier --
|
| 186 |
|
|
assert not (HPM_NUM_CNTS > 0) report "NEORV32 CPU CONFIG NOTE: Implementing " & integer'image(HPM_NUM_CNTS) & " HPM counters." severity note;
|
| 187 |
44 |
zero_gravi |
-- HPM CNT requires Zicsr extension --
|
| 188 |
|
|
assert not ((CPU_EXTENSION_RISCV_Zicsr = false) and (HPM_NUM_CNTS > 0)) report "NEORV32 CPU CONFIG ERROR! Performance monitors (HMP) require CPU_EXTENSION_RISCV_Zicsr extension." severity error;
|
| 189 |
41 |
zero_gravi |
|
| 190 |
42 |
zero_gravi |
|
| 191 |
2 |
zero_gravi |
-- Control Unit ---------------------------------------------------------------------------
|
| 192 |
|
|
-- -------------------------------------------------------------------------------------------
|
| 193 |
|
|
neorv32_cpu_control_inst: neorv32_cpu_control
|
| 194 |
|
|
generic map (
|
| 195 |
|
|
-- General --
|
| 196 |
40 |
zero_gravi |
HW_THREAD_ID => HW_THREAD_ID, -- hardware thread id
|
| 197 |
|
|
CPU_BOOT_ADDR => CPU_BOOT_ADDR, -- cpu boot address
|
| 198 |
2 |
zero_gravi |
-- RISC-V CPU Extensions --
|
| 199 |
39 |
zero_gravi |
CPU_EXTENSION_RISCV_A => CPU_EXTENSION_RISCV_A, -- implement atomic extension?
|
| 200 |
44 |
zero_gravi |
CPU_EXTENSION_RISCV_B => CPU_EXTENSION_RISCV_B, -- implement bit manipulation extensions?
|
| 201 |
15 |
zero_gravi |
CPU_EXTENSION_RISCV_C => CPU_EXTENSION_RISCV_C, -- implement compressed extension?
|
| 202 |
|
|
CPU_EXTENSION_RISCV_E => CPU_EXTENSION_RISCV_E, -- implement embedded RF extension?
|
| 203 |
|
|
CPU_EXTENSION_RISCV_M => CPU_EXTENSION_RISCV_M, -- implement muld/div extension?
|
| 204 |
|
|
CPU_EXTENSION_RISCV_U => CPU_EXTENSION_RISCV_U, -- implement user mode extension?
|
| 205 |
|
|
CPU_EXTENSION_RISCV_Zicsr => CPU_EXTENSION_RISCV_Zicsr, -- implement CSR system?
|
| 206 |
|
|
CPU_EXTENSION_RISCV_Zifencei => CPU_EXTENSION_RISCV_Zifencei, -- implement instruction stream sync.?
|
| 207 |
|
|
-- Physical memory protection (PMP) --
|
| 208 |
42 |
zero_gravi |
PMP_NUM_REGIONS => PMP_NUM_REGIONS, -- number of regions (0..64)
|
| 209 |
|
|
PMP_MIN_GRANULARITY => PMP_MIN_GRANULARITY, -- minimal region granularity in bytes, has to be a power of 2, min 8 bytes
|
| 210 |
|
|
-- Hardware Performance Monitors (HPM) --
|
| 211 |
|
|
HPM_NUM_CNTS => HPM_NUM_CNTS -- number of inmplemnted HPM counters (0..29)
|
| 212 |
2 |
zero_gravi |
)
|
| 213 |
|
|
port map (
|
| 214 |
|
|
-- global control --
|
| 215 |
|
|
clk_i => clk_i, -- global clock, rising edge
|
| 216 |
|
|
rstn_i => rstn_i, -- global reset, low-active, async
|
| 217 |
|
|
ctrl_o => ctrl, -- main control bus
|
| 218 |
|
|
-- status input --
|
| 219 |
|
|
alu_wait_i => alu_wait, -- wait for ALU
|
| 220 |
12 |
zero_gravi |
bus_i_wait_i => bus_i_wait, -- wait for bus
|
| 221 |
|
|
bus_d_wait_i => bus_d_wait, -- wait for bus
|
| 222 |
2 |
zero_gravi |
-- data input --
|
| 223 |
|
|
instr_i => instr, -- instruction
|
| 224 |
|
|
cmp_i => alu_cmp, -- comparator status
|
| 225 |
36 |
zero_gravi |
alu_add_i => alu_add, -- ALU address result
|
| 226 |
|
|
rs1_i => rs1, -- rf source 1
|
| 227 |
2 |
zero_gravi |
-- data output --
|
| 228 |
|
|
imm_o => imm, -- immediate
|
| 229 |
6 |
zero_gravi |
fetch_pc_o => fetch_pc, -- PC for instruction fetch
|
| 230 |
|
|
curr_pc_o => curr_pc, -- current PC (corresponding to current instruction)
|
| 231 |
2 |
zero_gravi |
csr_rdata_o => csr_rdata, -- CSR read data
|
| 232 |
14 |
zero_gravi |
-- interrupts (risc-v compliant) --
|
| 233 |
|
|
msw_irq_i => msw_irq_i, -- machine software interrupt
|
| 234 |
|
|
mext_irq_i => mext_irq_i, -- machine external interrupt
|
| 235 |
2 |
zero_gravi |
mtime_irq_i => mtime_irq_i, -- machine timer interrupt
|
| 236 |
14 |
zero_gravi |
-- fast interrupts (custom) --
|
| 237 |
|
|
firq_i => firq_i,
|
| 238 |
11 |
zero_gravi |
-- system time input from MTIME --
|
| 239 |
|
|
time_i => time_i, -- current system time
|
| 240 |
15 |
zero_gravi |
-- physical memory protection --
|
| 241 |
|
|
pmp_addr_o => pmp_addr, -- addresses
|
| 242 |
|
|
pmp_ctrl_o => pmp_ctrl, -- configs
|
| 243 |
2 |
zero_gravi |
-- bus access exceptions --
|
| 244 |
|
|
mar_i => mar, -- memory address register
|
| 245 |
|
|
ma_instr_i => ma_instr, -- misaligned instruction address
|
| 246 |
|
|
ma_load_i => ma_load, -- misaligned load data address
|
| 247 |
|
|
ma_store_i => ma_store, -- misaligned store data address
|
| 248 |
|
|
be_instr_i => be_instr, -- bus error on instruction access
|
| 249 |
|
|
be_load_i => be_load, -- bus error on load data access
|
| 250 |
12 |
zero_gravi |
be_store_i => be_store -- bus error on store data access
|
| 251 |
2 |
zero_gravi |
);
|
| 252 |
|
|
|
| 253 |
|
|
|
| 254 |
|
|
-- Register File --------------------------------------------------------------------------
|
| 255 |
|
|
-- -------------------------------------------------------------------------------------------
|
| 256 |
|
|
neorv32_regfile_inst: neorv32_cpu_regfile
|
| 257 |
|
|
generic map (
|
| 258 |
|
|
CPU_EXTENSION_RISCV_E => CPU_EXTENSION_RISCV_E -- implement embedded RF extension?
|
| 259 |
|
|
)
|
| 260 |
|
|
port map (
|
| 261 |
|
|
-- global control --
|
| 262 |
|
|
clk_i => clk_i, -- global clock, rising edge
|
| 263 |
|
|
ctrl_i => ctrl, -- main control bus
|
| 264 |
|
|
-- data input --
|
| 265 |
|
|
mem_i => rdata, -- memory read data
|
| 266 |
|
|
alu_i => alu_res, -- ALU result
|
| 267 |
|
|
csr_i => csr_rdata, -- CSR read data
|
| 268 |
|
|
-- data output --
|
| 269 |
|
|
rs1_o => rs1, -- operand 1
|
| 270 |
|
|
rs2_o => rs2 -- operand 2
|
| 271 |
|
|
);
|
| 272 |
|
|
|
| 273 |
|
|
|
| 274 |
|
|
-- ALU ------------------------------------------------------------------------------------
|
| 275 |
|
|
-- -------------------------------------------------------------------------------------------
|
| 276 |
|
|
neorv32_cpu_alu_inst: neorv32_cpu_alu
|
| 277 |
11 |
zero_gravi |
generic map (
|
| 278 |
34 |
zero_gravi |
CPU_EXTENSION_RISCV_M => CPU_EXTENSION_RISCV_M, -- implement muld/div extension?
|
| 279 |
|
|
FAST_SHIFT_EN => FAST_SHIFT_EN -- use barrel shifter for shift operations
|
| 280 |
11 |
zero_gravi |
)
|
| 281 |
2 |
zero_gravi |
port map (
|
| 282 |
|
|
-- global control --
|
| 283 |
|
|
clk_i => clk_i, -- global clock, rising edge
|
| 284 |
|
|
rstn_i => rstn_i, -- global reset, low-active, async
|
| 285 |
|
|
ctrl_i => ctrl, -- main control bus
|
| 286 |
|
|
-- data input --
|
| 287 |
|
|
rs1_i => rs1, -- rf source 1
|
| 288 |
|
|
rs2_i => rs2, -- rf source 2
|
| 289 |
6 |
zero_gravi |
pc2_i => curr_pc, -- delayed PC
|
| 290 |
2 |
zero_gravi |
imm_i => imm, -- immediate
|
| 291 |
|
|
-- data output --
|
| 292 |
|
|
cmp_o => alu_cmp, -- comparator status
|
| 293 |
|
|
res_o => alu_res, -- ALU result
|
| 294 |
36 |
zero_gravi |
add_o => alu_add, -- address computation result
|
| 295 |
2 |
zero_gravi |
-- co-processor interface --
|
| 296 |
19 |
zero_gravi |
cp0_start_o => cp0_start, -- trigger co-processor 0
|
| 297 |
2 |
zero_gravi |
cp0_data_i => cp0_data, -- co-processor 0 result
|
| 298 |
|
|
cp0_valid_i => cp0_valid, -- co-processor 0 result valid
|
| 299 |
19 |
zero_gravi |
cp1_start_o => cp1_start, -- trigger co-processor 1
|
| 300 |
2 |
zero_gravi |
cp1_data_i => cp1_data, -- co-processor 1 result
|
| 301 |
|
|
cp1_valid_i => cp1_valid, -- co-processor 1 result valid
|
| 302 |
36 |
zero_gravi |
cp2_start_o => cp2_start, -- trigger co-processor 2
|
| 303 |
|
|
cp2_data_i => cp2_data, -- co-processor 2 result
|
| 304 |
|
|
cp2_valid_i => cp2_valid, -- co-processor 2 result valid
|
| 305 |
|
|
cp3_start_o => cp3_start, -- trigger co-processor 3
|
| 306 |
|
|
cp3_data_i => cp3_data, -- co-processor 3 result
|
| 307 |
|
|
cp3_valid_i => cp3_valid, -- co-processor 3 result valid
|
| 308 |
2 |
zero_gravi |
-- status --
|
| 309 |
|
|
wait_o => alu_wait -- busy due to iterative processing units
|
| 310 |
|
|
);
|
| 311 |
|
|
|
| 312 |
|
|
|
| 313 |
|
|
-- Co-Processor 0: MULDIV Unit ------------------------------------------------------------
|
| 314 |
|
|
-- -------------------------------------------------------------------------------------------
|
| 315 |
|
|
neorv32_cpu_cp_muldiv_inst_true:
|
| 316 |
|
|
if (CPU_EXTENSION_RISCV_M = true) generate
|
| 317 |
|
|
neorv32_cpu_cp_muldiv_inst: neorv32_cpu_cp_muldiv
|
| 318 |
19 |
zero_gravi |
generic map (
|
| 319 |
38 |
zero_gravi |
FAST_MUL_EN => FAST_MUL_EN -- use DSPs for faster multiplication
|
| 320 |
19 |
zero_gravi |
)
|
| 321 |
2 |
zero_gravi |
port map (
|
| 322 |
|
|
-- global control --
|
| 323 |
|
|
clk_i => clk_i, -- global clock, rising edge
|
| 324 |
|
|
rstn_i => rstn_i, -- global reset, low-active, async
|
| 325 |
|
|
ctrl_i => ctrl, -- main control bus
|
| 326 |
36 |
zero_gravi |
start_i => cp0_start, -- trigger operation
|
| 327 |
2 |
zero_gravi |
-- data input --
|
| 328 |
27 |
zero_gravi |
rs1_i => rs1, -- rf source 1
|
| 329 |
|
|
rs2_i => rs2, -- rf source 2
|
| 330 |
2 |
zero_gravi |
-- result and status --
|
| 331 |
|
|
res_o => cp0_data, -- operation result
|
| 332 |
|
|
valid_o => cp0_valid -- data output valid
|
| 333 |
|
|
);
|
| 334 |
|
|
end generate;
|
| 335 |
|
|
|
| 336 |
|
|
neorv32_cpu_cp_muldiv_inst_false:
|
| 337 |
|
|
if (CPU_EXTENSION_RISCV_M = false) generate
|
| 338 |
|
|
cp0_data <= (others => '0');
|
| 339 |
40 |
zero_gravi |
cp0_valid <= cp0_start; -- to make sure CPU does not get stalled if there is an accidental access
|
| 340 |
2 |
zero_gravi |
end generate;
|
| 341 |
|
|
|
| 342 |
|
|
|
| 343 |
44 |
zero_gravi |
-- Co-Processor 1: Atomic Memory Access, SC - store-conditional ('M' extension) -----------
|
| 344 |
2 |
zero_gravi |
-- -------------------------------------------------------------------------------------------
|
| 345 |
40 |
zero_gravi |
atomic_op_cp: process(cp1_start, ctrl)
|
| 346 |
39 |
zero_gravi |
begin
|
| 347 |
|
|
-- "fake" co-processor for atomic operations
|
| 348 |
|
|
-- used to get the result of a store-conditional operation into the data path
|
| 349 |
40 |
zero_gravi |
if (CPU_EXTENSION_RISCV_A = true) then
|
| 350 |
|
|
if (cp1_start = '1') then
|
| 351 |
|
|
cp1_data <= (others => '0');
|
| 352 |
|
|
cp1_data(0) <= not ctrl(ctrl_bus_lock_c);
|
| 353 |
|
|
cp1_valid <= '1';
|
| 354 |
|
|
else
|
| 355 |
|
|
cp1_data <= (others => '0');
|
| 356 |
|
|
cp1_valid <= '0';
|
| 357 |
|
|
end if;
|
| 358 |
39 |
zero_gravi |
else
|
| 359 |
|
|
cp1_data <= (others => '0');
|
| 360 |
40 |
zero_gravi |
cp1_valid <= cp1_start; -- to make sure CPU does not get stalled if there is an accidental access
|
| 361 |
39 |
zero_gravi |
end if;
|
| 362 |
40 |
zero_gravi |
end process atomic_op_cp;
|
| 363 |
2 |
zero_gravi |
|
| 364 |
|
|
|
| 365 |
38 |
zero_gravi |
-- Co-Processor 2: Not implemented (yet) --------------------------------------------------
|
| 366 |
36 |
zero_gravi |
-- -------------------------------------------------------------------------------------------
|
| 367 |
44 |
zero_gravi |
neorv32_cpu_cp_bitmanip_inst_true:
|
| 368 |
|
|
if (CPU_EXTENSION_RISCV_B = true) generate
|
| 369 |
|
|
neorv32_cpu_cp_bitmanip_inst: neorv32_cpu_cp_bitmanip
|
| 370 |
|
|
port map (
|
| 371 |
|
|
-- global control --
|
| 372 |
|
|
clk_i => clk_i, -- global clock, rising edge
|
| 373 |
|
|
rstn_i => rstn_i, -- global reset, low-active, async
|
| 374 |
|
|
ctrl_i => ctrl, -- main control bus
|
| 375 |
|
|
start_i => cp2_start, -- trigger operation
|
| 376 |
|
|
-- data input --
|
| 377 |
|
|
cmp_i => alu_cmp, -- comparator status
|
| 378 |
|
|
rs1_i => rs1, -- rf source 1
|
| 379 |
|
|
rs2_i => rs2, -- rf source 2
|
| 380 |
|
|
-- result and status --
|
| 381 |
|
|
res_o => cp2_data, -- operation result
|
| 382 |
|
|
valid_o => cp2_valid -- data output valid
|
| 383 |
|
|
);
|
| 384 |
|
|
end generate;
|
| 385 |
36 |
zero_gravi |
|
| 386 |
44 |
zero_gravi |
neorv32_cpu_cp_bitmanip_inst_false:
|
| 387 |
|
|
if (CPU_EXTENSION_RISCV_B = false) generate
|
| 388 |
|
|
cp2_data <= (others => '0');
|
| 389 |
|
|
cp2_valid <= cp2_start; -- to make sure CPU does not get stalled if there is an accidental access
|
| 390 |
|
|
end generate;
|
| 391 |
36 |
zero_gravi |
|
| 392 |
44 |
zero_gravi |
|
| 393 |
38 |
zero_gravi |
-- Co-Processor 3: Not implemented (yet) --------------------------------------------------
|
| 394 |
36 |
zero_gravi |
-- -------------------------------------------------------------------------------------------
|
| 395 |
|
|
-- control: ctrl cp3_start
|
| 396 |
44 |
zero_gravi |
-- inputs: rs1 rs2 alu_cmp
|
| 397 |
36 |
zero_gravi |
cp3_data <= (others => '0');
|
| 398 |
40 |
zero_gravi |
cp3_valid <= cp3_start; -- to make sure CPU does not get stalled if there is an accidental access
|
| 399 |
36 |
zero_gravi |
|
| 400 |
|
|
|
| 401 |
12 |
zero_gravi |
-- Bus Interface Unit ---------------------------------------------------------------------
|
| 402 |
2 |
zero_gravi |
-- -------------------------------------------------------------------------------------------
|
| 403 |
|
|
neorv32_cpu_bus_inst: neorv32_cpu_bus
|
| 404 |
|
|
generic map (
|
| 405 |
11 |
zero_gravi |
CPU_EXTENSION_RISCV_C => CPU_EXTENSION_RISCV_C, -- implement compressed extension?
|
| 406 |
15 |
zero_gravi |
-- Physical memory protection (PMP) --
|
| 407 |
42 |
zero_gravi |
PMP_NUM_REGIONS => PMP_NUM_REGIONS, -- number of regions (0..64)
|
| 408 |
|
|
PMP_MIN_GRANULARITY => PMP_MIN_GRANULARITY, -- minimal region granularity in bytes, has to be a power of 2, min 8 bytes
|
| 409 |
41 |
zero_gravi |
-- Bus Timeout --
|
| 410 |
|
|
BUS_TIMEOUT => BUS_TIMEOUT -- cycles after an UNACKNOWLEDGED bus access triggers a bus fault exception
|
| 411 |
2 |
zero_gravi |
)
|
| 412 |
|
|
port map (
|
| 413 |
|
|
-- global control --
|
| 414 |
12 |
zero_gravi |
clk_i => clk_i, -- global clock, rising edge
|
| 415 |
38 |
zero_gravi |
rstn_i => rstn_i, -- global reset, low-active, async
|
| 416 |
12 |
zero_gravi |
ctrl_i => ctrl, -- main control bus
|
| 417 |
|
|
-- cpu instruction fetch interface --
|
| 418 |
|
|
fetch_pc_i => fetch_pc, -- PC for instruction fetch
|
| 419 |
|
|
instr_o => instr, -- instruction
|
| 420 |
|
|
i_wait_o => bus_i_wait, -- wait for fetch to complete
|
| 421 |
|
|
--
|
| 422 |
|
|
ma_instr_o => ma_instr, -- misaligned instruction address
|
| 423 |
|
|
be_instr_o => be_instr, -- bus error on instruction access
|
| 424 |
|
|
-- cpu data access interface --
|
| 425 |
39 |
zero_gravi |
addr_i => alu_add, -- ALU.add result -> access address
|
| 426 |
12 |
zero_gravi |
wdata_i => rs2, -- write data
|
| 427 |
|
|
rdata_o => rdata, -- read data
|
| 428 |
|
|
mar_o => mar, -- current memory address register
|
| 429 |
|
|
d_wait_o => bus_d_wait, -- wait for access to complete
|
| 430 |
|
|
--
|
| 431 |
|
|
ma_load_o => ma_load, -- misaligned load data address
|
| 432 |
|
|
ma_store_o => ma_store, -- misaligned store data address
|
| 433 |
|
|
be_load_o => be_load, -- bus error on load data access
|
| 434 |
|
|
be_store_o => be_store, -- bus error on store data access
|
| 435 |
15 |
zero_gravi |
-- physical memory protection --
|
| 436 |
|
|
pmp_addr_i => pmp_addr, -- addresses
|
| 437 |
|
|
pmp_ctrl_i => pmp_ctrl, -- configs
|
| 438 |
12 |
zero_gravi |
-- instruction bus --
|
| 439 |
|
|
i_bus_addr_o => i_bus_addr_o, -- bus access address
|
| 440 |
|
|
i_bus_rdata_i => i_bus_rdata_i, -- bus read data
|
| 441 |
|
|
i_bus_wdata_o => i_bus_wdata_o, -- bus write data
|
| 442 |
|
|
i_bus_ben_o => i_bus_ben_o, -- byte enable
|
| 443 |
|
|
i_bus_we_o => i_bus_we_o, -- write enable
|
| 444 |
|
|
i_bus_re_o => i_bus_re_o, -- read enable
|
| 445 |
|
|
i_bus_cancel_o => i_bus_cancel_o, -- cancel current bus transaction
|
| 446 |
|
|
i_bus_ack_i => i_bus_ack_i, -- bus transfer acknowledge
|
| 447 |
|
|
i_bus_err_i => i_bus_err_i, -- bus transfer error
|
| 448 |
|
|
i_bus_fence_o => i_bus_fence_o, -- fence operation
|
| 449 |
39 |
zero_gravi |
i_bus_lock_o => i_bus_lock_o, -- locked/exclusive access
|
| 450 |
12 |
zero_gravi |
-- data bus --
|
| 451 |
|
|
d_bus_addr_o => d_bus_addr_o, -- bus access address
|
| 452 |
|
|
d_bus_rdata_i => d_bus_rdata_i, -- bus read data
|
| 453 |
|
|
d_bus_wdata_o => d_bus_wdata_o, -- bus write data
|
| 454 |
|
|
d_bus_ben_o => d_bus_ben_o, -- byte enable
|
| 455 |
|
|
d_bus_we_o => d_bus_we_o, -- write enable
|
| 456 |
|
|
d_bus_re_o => d_bus_re_o, -- read enable
|
| 457 |
|
|
d_bus_cancel_o => d_bus_cancel_o, -- cancel current bus transaction
|
| 458 |
|
|
d_bus_ack_i => d_bus_ack_i, -- bus transfer acknowledge
|
| 459 |
|
|
d_bus_err_i => d_bus_err_i, -- bus transfer error
|
| 460 |
39 |
zero_gravi |
d_bus_fence_o => d_bus_fence_o, -- fence operation
|
| 461 |
|
|
d_bus_lock_o => d_bus_lock_o -- locked/exclusive access
|
| 462 |
2 |
zero_gravi |
);
|
| 463 |
|
|
|
| 464 |
35 |
zero_gravi |
-- current privilege level --
|
| 465 |
36 |
zero_gravi |
i_bus_priv_o <= ctrl(ctrl_priv_lvl_msb_c downto ctrl_priv_lvl_lsb_c);
|
| 466 |
|
|
d_bus_priv_o <= ctrl(ctrl_priv_lvl_msb_c downto ctrl_priv_lvl_lsb_c);
|
| 467 |
2 |
zero_gravi |
|
| 468 |
35 |
zero_gravi |
|
| 469 |
2 |
zero_gravi |
end neorv32_cpu_rtl;
|