1 |
2 |
zero_gravi |
-- #################################################################################################
|
2 |
|
|
-- # << NEORV32 - CPU Top Entity >> #
|
3 |
|
|
-- # ********************************************************************************************* #
|
4 |
18 |
zero_gravi |
-- # NEORV32 CPU: #
|
5 |
47 |
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 |
|
|
-- # * neorv32_package.vhd - Main CPU/processor package file #
|
13 |
38 |
zero_gravi |
-- # #
|
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 |
47 |
zero_gravi |
HPM_NUM_CNTS : natural := 0 -- number of implemented 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 |
47 |
zero_gravi |
sleep_o : out std_ulogic; -- cpu is in sleep mode when set
|
83 |
12 |
zero_gravi |
-- instruction bus interface --
|
84 |
|
|
i_bus_addr_o : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
|
85 |
14 |
zero_gravi |
i_bus_rdata_i : in std_ulogic_vector(data_width_c-1 downto 0) := (others => '0'); -- bus read data
|
86 |
12 |
zero_gravi |
i_bus_wdata_o : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
|
87 |
|
|
i_bus_ben_o : out std_ulogic_vector(03 downto 0); -- byte enable
|
88 |
|
|
i_bus_we_o : out std_ulogic; -- write enable
|
89 |
|
|
i_bus_re_o : out std_ulogic; -- read enable
|
90 |
|
|
i_bus_cancel_o : out std_ulogic; -- cancel current bus transaction
|
91 |
14 |
zero_gravi |
i_bus_ack_i : in std_ulogic := '0'; -- bus transfer acknowledge
|
92 |
|
|
i_bus_err_i : in std_ulogic := '0'; -- bus transfer error
|
93 |
12 |
zero_gravi |
i_bus_fence_o : out std_ulogic; -- executed FENCEI operation
|
94 |
35 |
zero_gravi |
i_bus_priv_o : out std_ulogic_vector(1 downto 0); -- privilege level
|
95 |
39 |
zero_gravi |
i_bus_lock_o : out std_ulogic; -- locked/exclusive access
|
96 |
12 |
zero_gravi |
-- data bus interface --
|
97 |
|
|
d_bus_addr_o : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
|
98 |
14 |
zero_gravi |
d_bus_rdata_i : in std_ulogic_vector(data_width_c-1 downto 0) := (others => '0'); -- bus read data
|
99 |
12 |
zero_gravi |
d_bus_wdata_o : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
|
100 |
|
|
d_bus_ben_o : out std_ulogic_vector(03 downto 0); -- byte enable
|
101 |
|
|
d_bus_we_o : out std_ulogic; -- write enable
|
102 |
|
|
d_bus_re_o : out std_ulogic; -- read enable
|
103 |
|
|
d_bus_cancel_o : out std_ulogic; -- cancel current bus transaction
|
104 |
14 |
zero_gravi |
d_bus_ack_i : in std_ulogic := '0'; -- bus transfer acknowledge
|
105 |
|
|
d_bus_err_i : in std_ulogic := '0'; -- bus transfer error
|
106 |
12 |
zero_gravi |
d_bus_fence_o : out std_ulogic; -- executed FENCE operation
|
107 |
35 |
zero_gravi |
d_bus_priv_o : out std_ulogic_vector(1 downto 0); -- privilege level
|
108 |
39 |
zero_gravi |
d_bus_lock_o : out std_ulogic; -- locked/exclusive access
|
109 |
11 |
zero_gravi |
-- system time input from MTIME --
|
110 |
14 |
zero_gravi |
time_i : in std_ulogic_vector(63 downto 0) := (others => '0'); -- current system time
|
111 |
|
|
-- interrupts (risc-v compliant) --
|
112 |
|
|
msw_irq_i : in std_ulogic := '0'; -- machine software interrupt
|
113 |
|
|
mext_irq_i : in std_ulogic := '0'; -- machine external interrupt
|
114 |
|
|
mtime_irq_i : in std_ulogic := '0'; -- machine timer interrupt
|
115 |
|
|
-- fast interrupts (custom) --
|
116 |
47 |
zero_gravi |
firq_i : in std_ulogic_vector(7 downto 0) := (others => '0');
|
117 |
|
|
firq_ack_o : out std_ulogic_vector(7 downto 0)
|
118 |
2 |
zero_gravi |
);
|
119 |
|
|
end neorv32_cpu;
|
120 |
|
|
|
121 |
|
|
architecture neorv32_cpu_rtl of neorv32_cpu is
|
122 |
|
|
|
123 |
|
|
-- local signals --
|
124 |
12 |
zero_gravi |
signal ctrl : std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
|
125 |
47 |
zero_gravi |
signal comparator : std_ulogic_vector(1 downto 0); -- comparator result
|
126 |
12 |
zero_gravi |
signal imm : std_ulogic_vector(data_width_c-1 downto 0); -- immediate
|
127 |
|
|
signal instr : std_ulogic_vector(data_width_c-1 downto 0); -- new instruction
|
128 |
|
|
signal rs1, rs2 : std_ulogic_vector(data_width_c-1 downto 0); -- source registers
|
129 |
|
|
signal alu_res : std_ulogic_vector(data_width_c-1 downto 0); -- alu result
|
130 |
36 |
zero_gravi |
signal alu_add : std_ulogic_vector(data_width_c-1 downto 0); -- alu address result
|
131 |
12 |
zero_gravi |
signal rdata : std_ulogic_vector(data_width_c-1 downto 0); -- memory read data
|
132 |
|
|
signal alu_wait : std_ulogic; -- alu is busy due to iterative unit
|
133 |
|
|
signal bus_i_wait : std_ulogic; -- wait for current bus instruction fetch
|
134 |
|
|
signal bus_d_wait : std_ulogic; -- wait for current bus data access
|
135 |
|
|
signal csr_rdata : std_ulogic_vector(data_width_c-1 downto 0); -- csr read data
|
136 |
|
|
signal mar : std_ulogic_vector(data_width_c-1 downto 0); -- current memory address register
|
137 |
|
|
signal ma_instr : std_ulogic; -- misaligned instruction address
|
138 |
|
|
signal ma_load : std_ulogic; -- misaligned load data address
|
139 |
|
|
signal ma_store : std_ulogic; -- misaligned store data address
|
140 |
|
|
signal be_instr : std_ulogic; -- bus error on instruction access
|
141 |
|
|
signal be_load : std_ulogic; -- bus error on load data access
|
142 |
|
|
signal be_store : std_ulogic; -- bus error on store data access
|
143 |
|
|
signal fetch_pc : std_ulogic_vector(data_width_c-1 downto 0); -- pc for instruction fetch
|
144 |
|
|
signal curr_pc : std_ulogic_vector(data_width_c-1 downto 0); -- current pc (for current executed instruction)
|
145 |
2 |
zero_gravi |
|
146 |
|
|
-- co-processor interface --
|
147 |
36 |
zero_gravi |
signal cp0_data, cp1_data, cp2_data, cp3_data : std_ulogic_vector(data_width_c-1 downto 0);
|
148 |
|
|
signal cp0_valid, cp1_valid, cp2_valid, cp3_valid : std_ulogic;
|
149 |
|
|
signal cp0_start, cp1_start, cp2_start, cp3_start : std_ulogic;
|
150 |
2 |
zero_gravi |
|
151 |
15 |
zero_gravi |
-- pmp interface --
|
152 |
|
|
signal pmp_addr : pmp_addr_if_t;
|
153 |
|
|
signal pmp_ctrl : pmp_ctrl_if_t;
|
154 |
|
|
|
155 |
47 |
zero_gravi |
-- atomic memory access - success? --
|
156 |
|
|
signal atomic_sc_res: std_ulogic;
|
157 |
|
|
|
158 |
2 |
zero_gravi |
begin
|
159 |
|
|
|
160 |
15 |
zero_gravi |
-- Sanity Checks --------------------------------------------------------------------------
|
161 |
|
|
-- -------------------------------------------------------------------------------------------
|
162 |
23 |
zero_gravi |
-- CSR system --
|
163 |
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;
|
164 |
23 |
zero_gravi |
-- U-extension requires Zicsr extension --
|
165 |
|
|
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;
|
166 |
|
|
-- PMP requires Zicsr extension --
|
167 |
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;
|
168 |
40 |
zero_gravi |
|
169 |
41 |
zero_gravi |
-- Bus timeout --
|
170 |
|
|
assert not (BUS_TIMEOUT < 2) report "NEORV32 CPU CONFIG ERROR! Invalid bus access timeout value <BUS_TIMEOUT>. Has to be >= 2." severity error;
|
171 |
|
|
|
172 |
38 |
zero_gravi |
-- Instruction prefetch buffer size --
|
173 |
|
|
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;
|
174 |
45 |
zero_gravi |
-- A extension - only lr.w and sc.w are supported yet --
|
175 |
|
|
assert not (CPU_EXTENSION_RISCV_A = true) report "NEORV32 CPU CONFIG WARNING! Atomic operations extension (A) only supports <lr.w> and <sc.w> instructions." severity warning;
|
176 |
15 |
zero_gravi |
|
177 |
44 |
zero_gravi |
-- Bit manipulation notifier --
|
178 |
|
|
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;
|
179 |
|
|
|
180 |
40 |
zero_gravi |
-- PMP regions check --
|
181 |
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;
|
182 |
40 |
zero_gravi |
-- PMP granulartiy --
|
183 |
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;
|
184 |
|
|
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;
|
185 |
40 |
zero_gravi |
-- PMP notifier --
|
186 |
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;
|
187 |
40 |
zero_gravi |
|
188 |
42 |
zero_gravi |
-- HPM counters check --
|
189 |
|
|
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;
|
190 |
|
|
-- HPM counters notifier --
|
191 |
|
|
assert not (HPM_NUM_CNTS > 0) report "NEORV32 CPU CONFIG NOTE: Implementing " & integer'image(HPM_NUM_CNTS) & " HPM counters." severity note;
|
192 |
44 |
zero_gravi |
-- HPM CNT requires Zicsr extension --
|
193 |
|
|
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;
|
194 |
41 |
zero_gravi |
|
195 |
42 |
zero_gravi |
|
196 |
2 |
zero_gravi |
-- Control Unit ---------------------------------------------------------------------------
|
197 |
|
|
-- -------------------------------------------------------------------------------------------
|
198 |
|
|
neorv32_cpu_control_inst: neorv32_cpu_control
|
199 |
|
|
generic map (
|
200 |
|
|
-- General --
|
201 |
40 |
zero_gravi |
HW_THREAD_ID => HW_THREAD_ID, -- hardware thread id
|
202 |
|
|
CPU_BOOT_ADDR => CPU_BOOT_ADDR, -- cpu boot address
|
203 |
2 |
zero_gravi |
-- RISC-V CPU Extensions --
|
204 |
39 |
zero_gravi |
CPU_EXTENSION_RISCV_A => CPU_EXTENSION_RISCV_A, -- implement atomic extension?
|
205 |
44 |
zero_gravi |
CPU_EXTENSION_RISCV_B => CPU_EXTENSION_RISCV_B, -- implement bit manipulation extensions?
|
206 |
15 |
zero_gravi |
CPU_EXTENSION_RISCV_C => CPU_EXTENSION_RISCV_C, -- implement compressed extension?
|
207 |
|
|
CPU_EXTENSION_RISCV_E => CPU_EXTENSION_RISCV_E, -- implement embedded RF extension?
|
208 |
|
|
CPU_EXTENSION_RISCV_M => CPU_EXTENSION_RISCV_M, -- implement muld/div extension?
|
209 |
|
|
CPU_EXTENSION_RISCV_U => CPU_EXTENSION_RISCV_U, -- implement user mode extension?
|
210 |
|
|
CPU_EXTENSION_RISCV_Zicsr => CPU_EXTENSION_RISCV_Zicsr, -- implement CSR system?
|
211 |
|
|
CPU_EXTENSION_RISCV_Zifencei => CPU_EXTENSION_RISCV_Zifencei, -- implement instruction stream sync.?
|
212 |
|
|
-- Physical memory protection (PMP) --
|
213 |
42 |
zero_gravi |
PMP_NUM_REGIONS => PMP_NUM_REGIONS, -- number of regions (0..64)
|
214 |
|
|
PMP_MIN_GRANULARITY => PMP_MIN_GRANULARITY, -- minimal region granularity in bytes, has to be a power of 2, min 8 bytes
|
215 |
|
|
-- Hardware Performance Monitors (HPM) --
|
216 |
47 |
zero_gravi |
HPM_NUM_CNTS => HPM_NUM_CNTS -- number of implemented HPM counters (0..29)
|
217 |
2 |
zero_gravi |
)
|
218 |
|
|
port map (
|
219 |
|
|
-- global control --
|
220 |
|
|
clk_i => clk_i, -- global clock, rising edge
|
221 |
|
|
rstn_i => rstn_i, -- global reset, low-active, async
|
222 |
|
|
ctrl_o => ctrl, -- main control bus
|
223 |
|
|
-- status input --
|
224 |
|
|
alu_wait_i => alu_wait, -- wait for ALU
|
225 |
12 |
zero_gravi |
bus_i_wait_i => bus_i_wait, -- wait for bus
|
226 |
|
|
bus_d_wait_i => bus_d_wait, -- wait for bus
|
227 |
2 |
zero_gravi |
-- data input --
|
228 |
|
|
instr_i => instr, -- instruction
|
229 |
47 |
zero_gravi |
cmp_i => comparator, -- comparator status
|
230 |
36 |
zero_gravi |
alu_add_i => alu_add, -- ALU address result
|
231 |
|
|
rs1_i => rs1, -- rf source 1
|
232 |
2 |
zero_gravi |
-- data output --
|
233 |
|
|
imm_o => imm, -- immediate
|
234 |
6 |
zero_gravi |
fetch_pc_o => fetch_pc, -- PC for instruction fetch
|
235 |
|
|
curr_pc_o => curr_pc, -- current PC (corresponding to current instruction)
|
236 |
2 |
zero_gravi |
csr_rdata_o => csr_rdata, -- CSR read data
|
237 |
14 |
zero_gravi |
-- interrupts (risc-v compliant) --
|
238 |
|
|
msw_irq_i => msw_irq_i, -- machine software interrupt
|
239 |
|
|
mext_irq_i => mext_irq_i, -- machine external interrupt
|
240 |
2 |
zero_gravi |
mtime_irq_i => mtime_irq_i, -- machine timer interrupt
|
241 |
14 |
zero_gravi |
-- fast interrupts (custom) --
|
242 |
47 |
zero_gravi |
firq_i => firq_i, -- fast interrupt trigger
|
243 |
|
|
firq_ack_o => firq_ack_o, -- fast interrupt acknowledge mask
|
244 |
11 |
zero_gravi |
-- system time input from MTIME --
|
245 |
|
|
time_i => time_i, -- current system time
|
246 |
15 |
zero_gravi |
-- physical memory protection --
|
247 |
|
|
pmp_addr_o => pmp_addr, -- addresses
|
248 |
|
|
pmp_ctrl_o => pmp_ctrl, -- configs
|
249 |
2 |
zero_gravi |
-- bus access exceptions --
|
250 |
|
|
mar_i => mar, -- memory address register
|
251 |
|
|
ma_instr_i => ma_instr, -- misaligned instruction address
|
252 |
|
|
ma_load_i => ma_load, -- misaligned load data address
|
253 |
|
|
ma_store_i => ma_store, -- misaligned store data address
|
254 |
|
|
be_instr_i => be_instr, -- bus error on instruction access
|
255 |
|
|
be_load_i => be_load, -- bus error on load data access
|
256 |
12 |
zero_gravi |
be_store_i => be_store -- bus error on store data access
|
257 |
2 |
zero_gravi |
);
|
258 |
|
|
|
259 |
47 |
zero_gravi |
-- CPU is sleeping? --
|
260 |
|
|
sleep_o <= ctrl(ctrl_sleep_c); -- set when CPU is sleeping (after WFI)
|
261 |
2 |
zero_gravi |
|
262 |
47 |
zero_gravi |
|
263 |
2 |
zero_gravi |
-- Register File --------------------------------------------------------------------------
|
264 |
|
|
-- -------------------------------------------------------------------------------------------
|
265 |
45 |
zero_gravi |
neorv32_cpu_regfile_inst: neorv32_cpu_regfile
|
266 |
2 |
zero_gravi |
generic map (
|
267 |
|
|
CPU_EXTENSION_RISCV_E => CPU_EXTENSION_RISCV_E -- implement embedded RF extension?
|
268 |
|
|
)
|
269 |
|
|
port map (
|
270 |
|
|
-- global control --
|
271 |
|
|
clk_i => clk_i, -- global clock, rising edge
|
272 |
|
|
ctrl_i => ctrl, -- main control bus
|
273 |
|
|
-- data input --
|
274 |
|
|
mem_i => rdata, -- memory read data
|
275 |
|
|
alu_i => alu_res, -- ALU result
|
276 |
|
|
csr_i => csr_rdata, -- CSR read data
|
277 |
|
|
-- data output --
|
278 |
|
|
rs1_o => rs1, -- operand 1
|
279 |
47 |
zero_gravi |
rs2_o => rs2, -- operand 2
|
280 |
|
|
cmp_o => comparator -- comparator status
|
281 |
2 |
zero_gravi |
);
|
282 |
|
|
|
283 |
|
|
|
284 |
|
|
-- ALU ------------------------------------------------------------------------------------
|
285 |
|
|
-- -------------------------------------------------------------------------------------------
|
286 |
|
|
neorv32_cpu_alu_inst: neorv32_cpu_alu
|
287 |
11 |
zero_gravi |
generic map (
|
288 |
34 |
zero_gravi |
CPU_EXTENSION_RISCV_M => CPU_EXTENSION_RISCV_M, -- implement muld/div extension?
|
289 |
|
|
FAST_SHIFT_EN => FAST_SHIFT_EN -- use barrel shifter for shift operations
|
290 |
11 |
zero_gravi |
)
|
291 |
2 |
zero_gravi |
port map (
|
292 |
|
|
-- global control --
|
293 |
|
|
clk_i => clk_i, -- global clock, rising edge
|
294 |
|
|
rstn_i => rstn_i, -- global reset, low-active, async
|
295 |
|
|
ctrl_i => ctrl, -- main control bus
|
296 |
|
|
-- data input --
|
297 |
|
|
rs1_i => rs1, -- rf source 1
|
298 |
|
|
rs2_i => rs2, -- rf source 2
|
299 |
6 |
zero_gravi |
pc2_i => curr_pc, -- delayed PC
|
300 |
2 |
zero_gravi |
imm_i => imm, -- immediate
|
301 |
|
|
-- data output --
|
302 |
|
|
res_o => alu_res, -- ALU result
|
303 |
36 |
zero_gravi |
add_o => alu_add, -- address computation result
|
304 |
2 |
zero_gravi |
-- co-processor interface --
|
305 |
19 |
zero_gravi |
cp0_start_o => cp0_start, -- trigger co-processor 0
|
306 |
2 |
zero_gravi |
cp0_data_i => cp0_data, -- co-processor 0 result
|
307 |
|
|
cp0_valid_i => cp0_valid, -- co-processor 0 result valid
|
308 |
19 |
zero_gravi |
cp1_start_o => cp1_start, -- trigger co-processor 1
|
309 |
2 |
zero_gravi |
cp1_data_i => cp1_data, -- co-processor 1 result
|
310 |
|
|
cp1_valid_i => cp1_valid, -- co-processor 1 result valid
|
311 |
36 |
zero_gravi |
cp2_start_o => cp2_start, -- trigger co-processor 2
|
312 |
|
|
cp2_data_i => cp2_data, -- co-processor 2 result
|
313 |
|
|
cp2_valid_i => cp2_valid, -- co-processor 2 result valid
|
314 |
|
|
cp3_start_o => cp3_start, -- trigger co-processor 3
|
315 |
|
|
cp3_data_i => cp3_data, -- co-processor 3 result
|
316 |
|
|
cp3_valid_i => cp3_valid, -- co-processor 3 result valid
|
317 |
2 |
zero_gravi |
-- status --
|
318 |
|
|
wait_o => alu_wait -- busy due to iterative processing units
|
319 |
|
|
);
|
320 |
|
|
|
321 |
|
|
|
322 |
47 |
zero_gravi |
-- Co-Processor 0: Integer Multiplication/Division ('M' Extension) ------------------------
|
323 |
2 |
zero_gravi |
-- -------------------------------------------------------------------------------------------
|
324 |
|
|
neorv32_cpu_cp_muldiv_inst_true:
|
325 |
|
|
if (CPU_EXTENSION_RISCV_M = true) generate
|
326 |
|
|
neorv32_cpu_cp_muldiv_inst: neorv32_cpu_cp_muldiv
|
327 |
19 |
zero_gravi |
generic map (
|
328 |
38 |
zero_gravi |
FAST_MUL_EN => FAST_MUL_EN -- use DSPs for faster multiplication
|
329 |
19 |
zero_gravi |
)
|
330 |
2 |
zero_gravi |
port map (
|
331 |
|
|
-- global control --
|
332 |
|
|
clk_i => clk_i, -- global clock, rising edge
|
333 |
|
|
rstn_i => rstn_i, -- global reset, low-active, async
|
334 |
|
|
ctrl_i => ctrl, -- main control bus
|
335 |
36 |
zero_gravi |
start_i => cp0_start, -- trigger operation
|
336 |
2 |
zero_gravi |
-- data input --
|
337 |
27 |
zero_gravi |
rs1_i => rs1, -- rf source 1
|
338 |
|
|
rs2_i => rs2, -- rf source 2
|
339 |
2 |
zero_gravi |
-- result and status --
|
340 |
|
|
res_o => cp0_data, -- operation result
|
341 |
|
|
valid_o => cp0_valid -- data output valid
|
342 |
|
|
);
|
343 |
|
|
end generate;
|
344 |
|
|
|
345 |
|
|
neorv32_cpu_cp_muldiv_inst_false:
|
346 |
|
|
if (CPU_EXTENSION_RISCV_M = false) generate
|
347 |
|
|
cp0_data <= (others => '0');
|
348 |
40 |
zero_gravi |
cp0_valid <= cp0_start; -- to make sure CPU does not get stalled if there is an accidental access
|
349 |
2 |
zero_gravi |
end generate;
|
350 |
|
|
|
351 |
|
|
|
352 |
47 |
zero_gravi |
-- Co-Processor 1: Atomic Memory Access ('A' Extension) -----------------------------------
|
353 |
2 |
zero_gravi |
-- -------------------------------------------------------------------------------------------
|
354 |
47 |
zero_gravi |
-- "pseudo" co-processor for atomic operations
|
355 |
|
|
-- used to get the result of a store-conditional operation into the data path
|
356 |
|
|
atomic_op_cp: process(clk_i)
|
357 |
39 |
zero_gravi |
begin
|
358 |
47 |
zero_gravi |
if rising_edge(clk_i) then
|
359 |
40 |
zero_gravi |
if (cp1_start = '1') then
|
360 |
47 |
zero_gravi |
atomic_sc_res <= not ctrl(ctrl_bus_lock_c);
|
361 |
40 |
zero_gravi |
else
|
362 |
47 |
zero_gravi |
atomic_sc_res <= '0';
|
363 |
40 |
zero_gravi |
end if;
|
364 |
39 |
zero_gravi |
end if;
|
365 |
40 |
zero_gravi |
end process atomic_op_cp;
|
366 |
2 |
zero_gravi |
|
367 |
47 |
zero_gravi |
-- CP result --
|
368 |
|
|
cp1_data(data_width_c-1 downto 1) <= (others => '0');
|
369 |
|
|
cp1_data(0) <= atomic_sc_res when (CPU_EXTENSION_RISCV_A = true) else '0';
|
370 |
|
|
cp1_valid <= cp1_start; -- always assigned even if A is disabled to make sure CPU does not get stalled if there is an accidental access
|
371 |
2 |
zero_gravi |
|
372 |
47 |
zero_gravi |
|
373 |
|
|
-- Co-Processor 2: Bit Manipulation ('B' Extension) ---------------------------------------
|
374 |
36 |
zero_gravi |
-- -------------------------------------------------------------------------------------------
|
375 |
44 |
zero_gravi |
neorv32_cpu_cp_bitmanip_inst_true:
|
376 |
|
|
if (CPU_EXTENSION_RISCV_B = true) generate
|
377 |
|
|
neorv32_cpu_cp_bitmanip_inst: neorv32_cpu_cp_bitmanip
|
378 |
|
|
port map (
|
379 |
|
|
-- global control --
|
380 |
|
|
clk_i => clk_i, -- global clock, rising edge
|
381 |
|
|
rstn_i => rstn_i, -- global reset, low-active, async
|
382 |
|
|
ctrl_i => ctrl, -- main control bus
|
383 |
|
|
start_i => cp2_start, -- trigger operation
|
384 |
|
|
-- data input --
|
385 |
47 |
zero_gravi |
cmp_i => comparator, -- comparator status
|
386 |
44 |
zero_gravi |
rs1_i => rs1, -- rf source 1
|
387 |
|
|
rs2_i => rs2, -- rf source 2
|
388 |
|
|
-- result and status --
|
389 |
|
|
res_o => cp2_data, -- operation result
|
390 |
|
|
valid_o => cp2_valid -- data output valid
|
391 |
|
|
);
|
392 |
|
|
end generate;
|
393 |
36 |
zero_gravi |
|
394 |
44 |
zero_gravi |
neorv32_cpu_cp_bitmanip_inst_false:
|
395 |
|
|
if (CPU_EXTENSION_RISCV_B = false) generate
|
396 |
|
|
cp2_data <= (others => '0');
|
397 |
|
|
cp2_valid <= cp2_start; -- to make sure CPU does not get stalled if there is an accidental access
|
398 |
|
|
end generate;
|
399 |
36 |
zero_gravi |
|
400 |
44 |
zero_gravi |
|
401 |
47 |
zero_gravi |
-- Co-Processor 3: Not implemented --------------------------------------------------------
|
402 |
36 |
zero_gravi |
-- -------------------------------------------------------------------------------------------
|
403 |
|
|
cp3_data <= (others => '0');
|
404 |
40 |
zero_gravi |
cp3_valid <= cp3_start; -- to make sure CPU does not get stalled if there is an accidental access
|
405 |
36 |
zero_gravi |
|
406 |
|
|
|
407 |
12 |
zero_gravi |
-- Bus Interface Unit ---------------------------------------------------------------------
|
408 |
2 |
zero_gravi |
-- -------------------------------------------------------------------------------------------
|
409 |
|
|
neorv32_cpu_bus_inst: neorv32_cpu_bus
|
410 |
|
|
generic map (
|
411 |
11 |
zero_gravi |
CPU_EXTENSION_RISCV_C => CPU_EXTENSION_RISCV_C, -- implement compressed extension?
|
412 |
15 |
zero_gravi |
-- Physical memory protection (PMP) --
|
413 |
42 |
zero_gravi |
PMP_NUM_REGIONS => PMP_NUM_REGIONS, -- number of regions (0..64)
|
414 |
|
|
PMP_MIN_GRANULARITY => PMP_MIN_GRANULARITY, -- minimal region granularity in bytes, has to be a power of 2, min 8 bytes
|
415 |
41 |
zero_gravi |
-- Bus Timeout --
|
416 |
|
|
BUS_TIMEOUT => BUS_TIMEOUT -- cycles after an UNACKNOWLEDGED bus access triggers a bus fault exception
|
417 |
2 |
zero_gravi |
)
|
418 |
|
|
port map (
|
419 |
|
|
-- global control --
|
420 |
12 |
zero_gravi |
clk_i => clk_i, -- global clock, rising edge
|
421 |
38 |
zero_gravi |
rstn_i => rstn_i, -- global reset, low-active, async
|
422 |
12 |
zero_gravi |
ctrl_i => ctrl, -- main control bus
|
423 |
|
|
-- cpu instruction fetch interface --
|
424 |
|
|
fetch_pc_i => fetch_pc, -- PC for instruction fetch
|
425 |
|
|
instr_o => instr, -- instruction
|
426 |
|
|
i_wait_o => bus_i_wait, -- wait for fetch to complete
|
427 |
|
|
--
|
428 |
|
|
ma_instr_o => ma_instr, -- misaligned instruction address
|
429 |
|
|
be_instr_o => be_instr, -- bus error on instruction access
|
430 |
|
|
-- cpu data access interface --
|
431 |
39 |
zero_gravi |
addr_i => alu_add, -- ALU.add result -> access address
|
432 |
12 |
zero_gravi |
wdata_i => rs2, -- write data
|
433 |
|
|
rdata_o => rdata, -- read data
|
434 |
|
|
mar_o => mar, -- current memory address register
|
435 |
|
|
d_wait_o => bus_d_wait, -- wait for access to complete
|
436 |
|
|
--
|
437 |
|
|
ma_load_o => ma_load, -- misaligned load data address
|
438 |
|
|
ma_store_o => ma_store, -- misaligned store data address
|
439 |
|
|
be_load_o => be_load, -- bus error on load data access
|
440 |
|
|
be_store_o => be_store, -- bus error on store data access
|
441 |
15 |
zero_gravi |
-- physical memory protection --
|
442 |
|
|
pmp_addr_i => pmp_addr, -- addresses
|
443 |
|
|
pmp_ctrl_i => pmp_ctrl, -- configs
|
444 |
12 |
zero_gravi |
-- instruction bus --
|
445 |
|
|
i_bus_addr_o => i_bus_addr_o, -- bus access address
|
446 |
|
|
i_bus_rdata_i => i_bus_rdata_i, -- bus read data
|
447 |
|
|
i_bus_wdata_o => i_bus_wdata_o, -- bus write data
|
448 |
|
|
i_bus_ben_o => i_bus_ben_o, -- byte enable
|
449 |
|
|
i_bus_we_o => i_bus_we_o, -- write enable
|
450 |
|
|
i_bus_re_o => i_bus_re_o, -- read enable
|
451 |
|
|
i_bus_cancel_o => i_bus_cancel_o, -- cancel current bus transaction
|
452 |
|
|
i_bus_ack_i => i_bus_ack_i, -- bus transfer acknowledge
|
453 |
|
|
i_bus_err_i => i_bus_err_i, -- bus transfer error
|
454 |
|
|
i_bus_fence_o => i_bus_fence_o, -- fence operation
|
455 |
39 |
zero_gravi |
i_bus_lock_o => i_bus_lock_o, -- locked/exclusive access
|
456 |
12 |
zero_gravi |
-- data bus --
|
457 |
|
|
d_bus_addr_o => d_bus_addr_o, -- bus access address
|
458 |
|
|
d_bus_rdata_i => d_bus_rdata_i, -- bus read data
|
459 |
|
|
d_bus_wdata_o => d_bus_wdata_o, -- bus write data
|
460 |
|
|
d_bus_ben_o => d_bus_ben_o, -- byte enable
|
461 |
|
|
d_bus_we_o => d_bus_we_o, -- write enable
|
462 |
|
|
d_bus_re_o => d_bus_re_o, -- read enable
|
463 |
|
|
d_bus_cancel_o => d_bus_cancel_o, -- cancel current bus transaction
|
464 |
|
|
d_bus_ack_i => d_bus_ack_i, -- bus transfer acknowledge
|
465 |
|
|
d_bus_err_i => d_bus_err_i, -- bus transfer error
|
466 |
39 |
zero_gravi |
d_bus_fence_o => d_bus_fence_o, -- fence operation
|
467 |
|
|
d_bus_lock_o => d_bus_lock_o -- locked/exclusive access
|
468 |
2 |
zero_gravi |
);
|
469 |
|
|
|
470 |
35 |
zero_gravi |
-- current privilege level --
|
471 |
36 |
zero_gravi |
i_bus_priv_o <= ctrl(ctrl_priv_lvl_msb_c downto ctrl_priv_lvl_lsb_c);
|
472 |
|
|
d_bus_priv_o <= ctrl(ctrl_priv_lvl_msb_c downto ctrl_priv_lvl_lsb_c);
|
473 |
2 |
zero_gravi |
|
474 |
35 |
zero_gravi |
|
475 |
2 |
zero_gravi |
end neorv32_cpu_rtl;
|