1 |
2 |
zero_gravi |
-- #################################################################################################
|
2 |
|
|
-- # << NEORV32 - CPU Control >> #
|
3 |
|
|
-- # ********************************************************************************************* #
|
4 |
6 |
zero_gravi |
-- # CPU operation is split into a fetch engine (responsible for fetching an decompressing instr- #
|
5 |
|
|
-- # uctions), an execute engine (responsible for actually executing the instructions), an inter- #
|
6 |
|
|
-- # rupt and exception handling controller and the RISC-V status and control registers (CSRs). #
|
7 |
2 |
zero_gravi |
-- # ********************************************************************************************* #
|
8 |
|
|
-- # BSD 3-Clause License #
|
9 |
|
|
-- # #
|
10 |
|
|
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved. #
|
11 |
|
|
-- # #
|
12 |
|
|
-- # Redistribution and use in source and binary forms, with or without modification, are #
|
13 |
|
|
-- # permitted provided that the following conditions are met: #
|
14 |
|
|
-- # #
|
15 |
|
|
-- # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
16 |
|
|
-- # conditions and the following disclaimer. #
|
17 |
|
|
-- # #
|
18 |
|
|
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
19 |
|
|
-- # conditions and the following disclaimer in the documentation and/or other materials #
|
20 |
|
|
-- # provided with the distribution. #
|
21 |
|
|
-- # #
|
22 |
|
|
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
23 |
|
|
-- # endorse or promote products derived from this software without specific prior written #
|
24 |
|
|
-- # permission. #
|
25 |
|
|
-- # #
|
26 |
|
|
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
27 |
|
|
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
28 |
|
|
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
29 |
|
|
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
30 |
|
|
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
31 |
|
|
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
32 |
|
|
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
33 |
|
|
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
34 |
|
|
-- # OF THE POSSIBILITY OF SUCH DAMAGE. #
|
35 |
|
|
-- # ********************************************************************************************* #
|
36 |
|
|
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32 (c) Stephan Nolting #
|
37 |
|
|
-- #################################################################################################
|
38 |
|
|
|
39 |
|
|
library ieee;
|
40 |
|
|
use ieee.std_logic_1164.all;
|
41 |
|
|
use ieee.numeric_std.all;
|
42 |
|
|
|
43 |
|
|
library neorv32;
|
44 |
|
|
use neorv32.neorv32_package.all;
|
45 |
|
|
|
46 |
|
|
entity neorv32_cpu_control is
|
47 |
|
|
generic (
|
48 |
|
|
-- General --
|
49 |
12 |
zero_gravi |
HW_THREAD_ID : std_ulogic_vector(31 downto 0):= x"00000000"; -- hardware thread id
|
50 |
|
|
CPU_BOOT_ADDR : std_ulogic_vector(31 downto 0):= x"00000000"; -- cpu boot address
|
51 |
2 |
zero_gravi |
-- RISC-V CPU Extensions --
|
52 |
12 |
zero_gravi |
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 |
15 |
zero_gravi |
CPU_EXTENSION_RISCV_U : boolean := false; -- implement user mode extension?
|
56 |
12 |
zero_gravi |
CPU_EXTENSION_RISCV_Zicsr : boolean := true; -- implement CSR system?
|
57 |
15 |
zero_gravi |
CPU_EXTENSION_RISCV_Zifencei : boolean := true; -- implement instruction stream sync.?
|
58 |
|
|
-- Physical memory protection (PMP) --
|
59 |
|
|
PMP_USE : boolean := false; -- implement physical memory protection?
|
60 |
|
|
PMP_NUM_REGIONS : natural := 4; -- number of regions (1..4)
|
61 |
|
|
PMP_GRANULARITY : natural := 0 -- granularity (0=none, 1=8B, 2=16B, 3=32B, ...)
|
62 |
2 |
zero_gravi |
);
|
63 |
|
|
port (
|
64 |
|
|
-- global control --
|
65 |
|
|
clk_i : in std_ulogic; -- global clock, rising edge
|
66 |
|
|
rstn_i : in std_ulogic; -- global reset, low-active, async
|
67 |
|
|
ctrl_o : out std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
|
68 |
|
|
-- status input --
|
69 |
|
|
alu_wait_i : in std_ulogic; -- wait for ALU
|
70 |
12 |
zero_gravi |
bus_i_wait_i : in std_ulogic; -- wait for bus
|
71 |
|
|
bus_d_wait_i : in std_ulogic; -- wait for bus
|
72 |
2 |
zero_gravi |
-- data input --
|
73 |
|
|
instr_i : in std_ulogic_vector(data_width_c-1 downto 0); -- instruction
|
74 |
|
|
cmp_i : in std_ulogic_vector(1 downto 0); -- comparator status
|
75 |
|
|
alu_add_i : in std_ulogic_vector(data_width_c-1 downto 0); -- ALU.add result
|
76 |
|
|
-- data output --
|
77 |
|
|
imm_o : out std_ulogic_vector(data_width_c-1 downto 0); -- immediate
|
78 |
6 |
zero_gravi |
fetch_pc_o : out std_ulogic_vector(data_width_c-1 downto 0); -- PC for instruction fetch
|
79 |
|
|
curr_pc_o : out std_ulogic_vector(data_width_c-1 downto 0); -- current PC (corresponding to current instruction)
|
80 |
|
|
next_pc_o : out std_ulogic_vector(data_width_c-1 downto 0); -- next PC (corresponding to current instruction)
|
81 |
2 |
zero_gravi |
-- csr data interface --
|
82 |
|
|
csr_wdata_i : in std_ulogic_vector(data_width_c-1 downto 0); -- CSR write data
|
83 |
|
|
csr_rdata_o : out std_ulogic_vector(data_width_c-1 downto 0); -- CSR read data
|
84 |
14 |
zero_gravi |
-- interrupts (risc-v compliant) --
|
85 |
|
|
msw_irq_i : in std_ulogic; -- machine software interrupt
|
86 |
|
|
mext_irq_i : in std_ulogic; -- machine external interrupt
|
87 |
2 |
zero_gravi |
mtime_irq_i : in std_ulogic; -- machine timer interrupt
|
88 |
14 |
zero_gravi |
-- fast interrupts (custom) --
|
89 |
|
|
firq_i : in std_ulogic_vector(3 downto 0);
|
90 |
11 |
zero_gravi |
-- system time input from MTIME --
|
91 |
|
|
time_i : in std_ulogic_vector(63 downto 0); -- current system time
|
92 |
15 |
zero_gravi |
-- physical memory protection --
|
93 |
23 |
zero_gravi |
pmp_addr_o : out pmp_addr_if_t; -- addresses
|
94 |
|
|
pmp_ctrl_o : out pmp_ctrl_if_t; -- configs
|
95 |
|
|
priv_mode_o : out std_ulogic_vector(1 downto 0); -- current CPU privilege level
|
96 |
2 |
zero_gravi |
-- bus access exceptions --
|
97 |
|
|
mar_i : in std_ulogic_vector(data_width_c-1 downto 0); -- memory address register
|
98 |
|
|
ma_instr_i : in std_ulogic; -- misaligned instruction address
|
99 |
|
|
ma_load_i : in std_ulogic; -- misaligned load data address
|
100 |
|
|
ma_store_i : in std_ulogic; -- misaligned store data address
|
101 |
|
|
be_instr_i : in std_ulogic; -- bus error on instruction access
|
102 |
|
|
be_load_i : in std_ulogic; -- bus error on load data access
|
103 |
12 |
zero_gravi |
be_store_i : in std_ulogic -- bus error on store data access
|
104 |
2 |
zero_gravi |
);
|
105 |
|
|
end neorv32_cpu_control;
|
106 |
|
|
|
107 |
|
|
architecture neorv32_cpu_control_rtl of neorv32_cpu_control is
|
108 |
|
|
|
109 |
6 |
zero_gravi |
-- instruction fetch enginge --
|
110 |
13 |
zero_gravi |
type fetch_engine_state_t is (IFETCH_RESET, IFETCH_0, IFETCH_1, IFETCH_2);
|
111 |
6 |
zero_gravi |
type fetch_engine_t is record
|
112 |
|
|
state : fetch_engine_state_t;
|
113 |
|
|
state_nxt : fetch_engine_state_t;
|
114 |
|
|
i_buf : std_ulogic_vector(33 downto 0);
|
115 |
|
|
i_buf_nxt : std_ulogic_vector(33 downto 0);
|
116 |
|
|
i_buf2 : std_ulogic_vector(33 downto 0);
|
117 |
|
|
i_buf2_nxt : std_ulogic_vector(33 downto 0);
|
118 |
13 |
zero_gravi |
ci_input : std_ulogic_vector(15 downto 0); -- input to compressed instr. decoder
|
119 |
6 |
zero_gravi |
i_buf_state : std_ulogic_vector(01 downto 0);
|
120 |
|
|
i_buf_state_nxt : std_ulogic_vector(01 downto 0);
|
121 |
20 |
zero_gravi |
pc : std_ulogic_vector(data_width_c-1 downto 0);
|
122 |
|
|
pc_add : std_ulogic_vector(data_width_c-1 downto 0);
|
123 |
6 |
zero_gravi |
reset : std_ulogic;
|
124 |
|
|
bus_err_ack : std_ulogic;
|
125 |
|
|
end record;
|
126 |
|
|
signal fetch_engine : fetch_engine_t;
|
127 |
2 |
zero_gravi |
|
128 |
|
|
-- pre-decoder --
|
129 |
|
|
signal ci_instr32 : std_ulogic_vector(31 downto 0);
|
130 |
|
|
signal ci_illegal : std_ulogic;
|
131 |
|
|
|
132 |
6 |
zero_gravi |
-- instrucion prefetch buffer (IPB) --
|
133 |
20 |
zero_gravi |
type ipb_dbuf_t is array (0 to ipb_entries_c-1) of std_ulogic_vector(35 downto 0);
|
134 |
6 |
zero_gravi |
type ipb_t is record
|
135 |
20 |
zero_gravi |
wdata : std_ulogic_vector(35 downto 0); -- data (+ status) to be written
|
136 |
|
|
we : std_ulogic; -- trigger write
|
137 |
|
|
free : std_ulogic; -- free entry available?
|
138 |
|
|
--
|
139 |
|
|
rdata : std_ulogic_vector(35 downto 0); -- read data (+ status)
|
140 |
|
|
re : std_ulogic; -- trigger read
|
141 |
|
|
avail : std_ulogic; -- data available?
|
142 |
|
|
--
|
143 |
|
|
clear : std_ulogic; -- clear all entries
|
144 |
|
|
--
|
145 |
|
|
data : ipb_dbuf_t; -- the data fifo
|
146 |
|
|
w_pnt : std_ulogic_vector(index_size_f(ipb_entries_c) downto 0); -- write pointer
|
147 |
|
|
r_pnt : std_ulogic_vector(index_size_f(ipb_entries_c) downto 0); -- read pointer
|
148 |
|
|
empty : std_ulogic;
|
149 |
|
|
full : std_ulogic;
|
150 |
6 |
zero_gravi |
end record;
|
151 |
|
|
signal ipb : ipb_t;
|
152 |
2 |
zero_gravi |
|
153 |
6 |
zero_gravi |
-- instruction execution engine --
|
154 |
12 |
zero_gravi |
type execute_engine_state_t is (SYS_WAIT, DISPATCH, TRAP, EXECUTE, ALU_WAIT, BRANCH, LOADSTORE_0, LOADSTORE_1, LOADSTORE_2, CSR_ACCESS);
|
155 |
6 |
zero_gravi |
type execute_engine_t is record
|
156 |
|
|
state : execute_engine_state_t;
|
157 |
19 |
zero_gravi |
state_prev : execute_engine_state_t;
|
158 |
6 |
zero_gravi |
state_nxt : execute_engine_state_t;
|
159 |
|
|
i_reg : std_ulogic_vector(31 downto 0);
|
160 |
|
|
i_reg_nxt : std_ulogic_vector(31 downto 0);
|
161 |
|
|
is_ci : std_ulogic; -- current instruction is de-compressed instruction
|
162 |
|
|
is_ci_nxt : std_ulogic;
|
163 |
|
|
is_jump : std_ulogic; -- current instruction is jump instruction
|
164 |
|
|
is_jump_nxt : std_ulogic;
|
165 |
|
|
branch_taken : std_ulogic; -- branch condition fullfilled
|
166 |
|
|
pc : std_ulogic_vector(data_width_c-1 downto 0); -- actual PC, corresponding to current executed instruction
|
167 |
|
|
pc_nxt : std_ulogic_vector(data_width_c-1 downto 0);
|
168 |
|
|
next_pc : std_ulogic_vector(data_width_c-1 downto 0); -- next PC, corresponding to next instruction to be executed
|
169 |
|
|
last_pc : std_ulogic_vector(data_width_c-1 downto 0); -- PC of last executed instruction
|
170 |
11 |
zero_gravi |
sleep : std_ulogic; -- CPU in sleep mode
|
171 |
|
|
sleep_nxt : std_ulogic; -- CPU in sleep mode
|
172 |
20 |
zero_gravi |
if_rst : std_ulogic; -- instruction fetch was reset
|
173 |
|
|
if_rst_nxt : std_ulogic; -- instruction fetch was reset
|
174 |
6 |
zero_gravi |
end record;
|
175 |
|
|
signal execute_engine : execute_engine_t;
|
176 |
2 |
zero_gravi |
|
177 |
12 |
zero_gravi |
signal next_pc_tmp : std_ulogic_vector(data_width_c-1 downto 0);
|
178 |
|
|
|
179 |
6 |
zero_gravi |
-- trap controller --
|
180 |
|
|
type trap_ctrl_t is record
|
181 |
|
|
exc_buf : std_ulogic_vector(exception_width_c-1 downto 0);
|
182 |
|
|
exc_fire : std_ulogic; -- set if there is a valid source in the exception buffer
|
183 |
|
|
irq_buf : std_ulogic_vector(interrupt_width_c-1 downto 0);
|
184 |
|
|
irq_fire : std_ulogic; -- set if there is a valid source in the interrupt buffer
|
185 |
|
|
exc_ack : std_ulogic; -- acknowledge all exceptions
|
186 |
|
|
irq_ack : std_ulogic_vector(interrupt_width_c-1 downto 0); -- acknowledge specific interrupt
|
187 |
|
|
irq_ack_nxt : std_ulogic_vector(interrupt_width_c-1 downto 0);
|
188 |
14 |
zero_gravi |
cause : std_ulogic_vector(5 downto 0); -- trap ID (for "mcause"), only for hw
|
189 |
|
|
cause_nxt : std_ulogic_vector(5 downto 0);
|
190 |
6 |
zero_gravi |
--
|
191 |
|
|
env_start : std_ulogic; -- start trap handler env
|
192 |
|
|
env_start_ack : std_ulogic; -- start of trap handler acknowledged
|
193 |
|
|
env_end : std_ulogic; -- end trap handler env
|
194 |
|
|
--
|
195 |
|
|
instr_be : std_ulogic; -- instruction fetch bus error
|
196 |
|
|
instr_ma : std_ulogic; -- instruction fetch misaligned address
|
197 |
|
|
instr_il : std_ulogic; -- illegal instruction
|
198 |
|
|
env_call : std_ulogic;
|
199 |
|
|
break_point : std_ulogic;
|
200 |
|
|
end record;
|
201 |
|
|
signal trap_ctrl : trap_ctrl_t;
|
202 |
|
|
|
203 |
|
|
-- CPU control signals --
|
204 |
|
|
signal ctrl_nxt, ctrl : std_ulogic_vector(ctrl_width_c-1 downto 0);
|
205 |
2 |
zero_gravi |
|
206 |
6 |
zero_gravi |
-- fast bus access --
|
207 |
|
|
signal bus_fast_ir : std_ulogic;
|
208 |
2 |
zero_gravi |
|
209 |
6 |
zero_gravi |
-- RISC-V control and status registers (CSRs) --
|
210 |
15 |
zero_gravi |
type pmp_ctrl_t is array (0 to PMP_NUM_REGIONS-1) of std_ulogic_vector(7 downto 0);
|
211 |
|
|
type pmp_addr_t is array (0 to PMP_NUM_REGIONS-1) of std_ulogic_vector(data_width_c-1 downto 0);
|
212 |
6 |
zero_gravi |
type csr_t is record
|
213 |
|
|
we : std_ulogic; -- write enable
|
214 |
|
|
we_nxt : std_ulogic;
|
215 |
|
|
re : std_ulogic; -- read enable
|
216 |
|
|
re_nxt : std_ulogic;
|
217 |
|
|
mstatus_mie : std_ulogic; -- mstatus.MIE: global IRQ enable (R/W)
|
218 |
|
|
mstatus_mpie : std_ulogic; -- mstatus.MPIE: previous global IRQ enable (R/-)
|
219 |
|
|
mie_msie : std_ulogic; -- mie.MSIE: machine software interrupt enable (R/W)
|
220 |
|
|
mie_meie : std_ulogic; -- mie.MEIE: machine external interrupt enable (R/W)
|
221 |
14 |
zero_gravi |
mie_mtie : std_ulogic; -- mie.MEIE: machine timer interrupt enable (R/W
|
222 |
|
|
mie_firqe : std_ulogic_vector(3 downto 0); -- mie.firq*e: fast interrupt enabled (R/W)
|
223 |
15 |
zero_gravi |
mpp : std_ulogic_vector(1 downto 0); -- machine previous privilege mode
|
224 |
|
|
privilege : std_ulogic_vector(1 downto 0); -- hart's current previous privilege mode
|
225 |
6 |
zero_gravi |
mepc : std_ulogic_vector(data_width_c-1 downto 0); -- mepc: machine exception pc (R/W)
|
226 |
14 |
zero_gravi |
mcause : std_ulogic_vector(data_width_c-1 downto 0); -- mcause: machine trap cause (R/-)
|
227 |
12 |
zero_gravi |
mtvec : std_ulogic_vector(data_width_c-1 downto 0); -- mtvec: machine trap-handler base address (R/W), bit 1:0 == 00
|
228 |
11 |
zero_gravi |
mtval : std_ulogic_vector(data_width_c-1 downto 0); -- mtval: machine bad address or isntruction (R/W)
|
229 |
6 |
zero_gravi |
mscratch : std_ulogic_vector(data_width_c-1 downto 0); -- mscratch: scratch register (R/W)
|
230 |
11 |
zero_gravi |
mcycle : std_ulogic_vector(32 downto 0); -- mcycle (R/W), plus carry bit
|
231 |
|
|
minstret : std_ulogic_vector(32 downto 0); -- minstret (R/W), plus carry bit
|
232 |
12 |
zero_gravi |
mcycleh : std_ulogic_vector(19 downto 0); -- mcycleh (R/W) - REDUCED BIT-WIDTH!
|
233 |
|
|
minstreth : std_ulogic_vector(19 downto 0); -- minstreth (R/W) - REDUCED BIT-WIDTH!
|
234 |
15 |
zero_gravi |
pmpcfg : pmp_ctrl_t; -- physical memory protection - configuration registers
|
235 |
|
|
pmpaddr : pmp_addr_t; -- physical memory protection - address registers
|
236 |
6 |
zero_gravi |
end record;
|
237 |
|
|
signal csr : csr_t;
|
238 |
2 |
zero_gravi |
|
239 |
11 |
zero_gravi |
signal mcycle_msb : std_ulogic;
|
240 |
|
|
signal minstret_msb : std_ulogic;
|
241 |
2 |
zero_gravi |
|
242 |
6 |
zero_gravi |
-- illegal instruction check --
|
243 |
2 |
zero_gravi |
signal illegal_instruction : std_ulogic;
|
244 |
|
|
signal illegal_register : std_ulogic; -- only for E-extension
|
245 |
|
|
signal illegal_compressed : std_ulogic; -- only fir C-extension
|
246 |
|
|
|
247 |
15 |
zero_gravi |
-- access (privilege) check --
|
248 |
|
|
signal csr_acc_valid : std_ulogic; -- valid CSR access (implemented and valid access rights)
|
249 |
|
|
|
250 |
2 |
zero_gravi |
begin
|
251 |
|
|
|
252 |
6 |
zero_gravi |
-- ****************************************************************************************************************************
|
253 |
|
|
-- Instruction Fetch
|
254 |
|
|
-- ****************************************************************************************************************************
|
255 |
|
|
|
256 |
|
|
-- Fetch Engine FSM Sync ------------------------------------------------------------------
|
257 |
|
|
-- -------------------------------------------------------------------------------------------
|
258 |
23 |
zero_gravi |
-- registers that require a specific reset state --
|
259 |
6 |
zero_gravi |
fetch_engine_fsm_sync_rst: process(rstn_i, clk_i)
|
260 |
|
|
begin
|
261 |
|
|
if (rstn_i = '0') then
|
262 |
|
|
fetch_engine.state <= IFETCH_RESET;
|
263 |
|
|
elsif rising_edge(clk_i) then
|
264 |
|
|
if (fetch_engine.reset = '1') then
|
265 |
|
|
fetch_engine.state <= IFETCH_RESET;
|
266 |
|
|
else
|
267 |
|
|
fetch_engine.state <= fetch_engine.state_nxt;
|
268 |
|
|
end if;
|
269 |
|
|
end if;
|
270 |
|
|
end process fetch_engine_fsm_sync_rst;
|
271 |
|
|
|
272 |
|
|
|
273 |
23 |
zero_gravi |
-- registers that DO NOT require a specific reset state --
|
274 |
6 |
zero_gravi |
fetch_engine_fsm_sync: process(clk_i)
|
275 |
|
|
begin
|
276 |
|
|
if rising_edge(clk_i) then
|
277 |
|
|
if (fetch_engine.state = IFETCH_RESET) then
|
278 |
20 |
zero_gravi |
fetch_engine.pc <= execute_engine.pc(data_width_c-1 downto 1) & '0'; -- initialize with "real" application PC
|
279 |
6 |
zero_gravi |
else
|
280 |
20 |
zero_gravi |
fetch_engine.pc <= std_ulogic_vector(unsigned(fetch_engine.pc(data_width_c-1 downto 1) & '0') + unsigned(fetch_engine.pc_add(data_width_c-1 downto 1) & '0'));
|
281 |
6 |
zero_gravi |
end if;
|
282 |
|
|
--
|
283 |
|
|
fetch_engine.i_buf <= fetch_engine.i_buf_nxt;
|
284 |
|
|
fetch_engine.i_buf2 <= fetch_engine.i_buf2_nxt;
|
285 |
|
|
fetch_engine.i_buf_state <= fetch_engine.i_buf_state_nxt;
|
286 |
|
|
end if;
|
287 |
|
|
end process fetch_engine_fsm_sync;
|
288 |
|
|
|
289 |
12 |
zero_gravi |
-- PC output --
|
290 |
20 |
zero_gravi |
fetch_pc_o <= fetch_engine.pc(data_width_c-1 downto 1) & '0';
|
291 |
6 |
zero_gravi |
|
292 |
12 |
zero_gravi |
|
293 |
6 |
zero_gravi |
-- Fetch Engine FSM Comb ------------------------------------------------------------------
|
294 |
|
|
-- -------------------------------------------------------------------------------------------
|
295 |
13 |
zero_gravi |
fetch_engine_fsm_comb: process(fetch_engine, csr, ipb, instr_i, bus_i_wait_i, ci_instr32, ci_illegal, be_instr_i, ma_instr_i)
|
296 |
6 |
zero_gravi |
begin
|
297 |
|
|
-- arbiter defaults --
|
298 |
13 |
zero_gravi |
bus_fast_ir <= '0';
|
299 |
6 |
zero_gravi |
fetch_engine.state_nxt <= fetch_engine.state;
|
300 |
20 |
zero_gravi |
fetch_engine.pc_add <= (others => '0');
|
301 |
6 |
zero_gravi |
fetch_engine.i_buf_nxt <= fetch_engine.i_buf;
|
302 |
|
|
fetch_engine.i_buf2_nxt <= fetch_engine.i_buf2;
|
303 |
|
|
fetch_engine.i_buf_state_nxt <= fetch_engine.i_buf_state;
|
304 |
13 |
zero_gravi |
fetch_engine.ci_input <= fetch_engine.i_buf2(15 downto 00);
|
305 |
6 |
zero_gravi |
fetch_engine.bus_err_ack <= '0';
|
306 |
|
|
|
307 |
|
|
-- instruction prefetch buffer interface --
|
308 |
|
|
ipb.we <= '0';
|
309 |
|
|
ipb.clear <= '0';
|
310 |
19 |
zero_gravi |
ipb.wdata <= (others => '0');
|
311 |
6 |
zero_gravi |
|
312 |
|
|
-- state machine --
|
313 |
|
|
case fetch_engine.state is
|
314 |
|
|
|
315 |
11 |
zero_gravi |
when IFETCH_RESET => -- reset engine, prefetch buffer, get appilcation PC
|
316 |
6 |
zero_gravi |
-- ------------------------------------------------------------
|
317 |
|
|
fetch_engine.i_buf_state_nxt <= (others => '0');
|
318 |
|
|
ipb.clear <= '1'; -- clear instruction prefetch buffer
|
319 |
|
|
fetch_engine.state_nxt <= IFETCH_0;
|
320 |
|
|
|
321 |
|
|
when IFETCH_0 => -- output current PC to bus system, request 32-bit word
|
322 |
|
|
-- ------------------------------------------------------------
|
323 |
12 |
zero_gravi |
bus_fast_ir <= '1'; -- fast instruction fetch request
|
324 |
|
|
fetch_engine.state_nxt <= IFETCH_1;
|
325 |
6 |
zero_gravi |
|
326 |
|
|
when IFETCH_1 => -- store data from memory to buffer(s)
|
327 |
|
|
-- ------------------------------------------------------------
|
328 |
12 |
zero_gravi |
if (bus_i_wait_i = '0') or (be_instr_i = '1') or (ma_instr_i = '1') then -- wait for bus response
|
329 |
|
|
fetch_engine.i_buf_nxt <= be_instr_i & ma_instr_i & instr_i(31 downto 0); -- store data word and exception info
|
330 |
|
|
fetch_engine.i_buf2_nxt <= fetch_engine.i_buf;
|
331 |
|
|
fetch_engine.i_buf_state_nxt <= fetch_engine.i_buf_state(0) & '1';
|
332 |
|
|
fetch_engine.bus_err_ack <= '1'; -- acknowledge any instruction bus errors, the execute engine has to take care of them
|
333 |
|
|
if (fetch_engine.i_buf_state(0) = '1') then -- buffer filled?
|
334 |
|
|
fetch_engine.state_nxt <= IFETCH_2;
|
335 |
|
|
else
|
336 |
20 |
zero_gravi |
fetch_engine.pc_add <= std_ulogic_vector(to_unsigned(4, data_width_c));
|
337 |
|
|
fetch_engine.state_nxt <= IFETCH_0; -- get another instruction word
|
338 |
12 |
zero_gravi |
end if;
|
339 |
6 |
zero_gravi |
end if;
|
340 |
11 |
zero_gravi |
|
341 |
12 |
zero_gravi |
when IFETCH_2 => -- construct instruction word and issue
|
342 |
6 |
zero_gravi |
-- ------------------------------------------------------------
|
343 |
20 |
zero_gravi |
if (fetch_engine.pc(1) = '0') or (CPU_EXTENSION_RISCV_C = false) then -- 32-bit aligned
|
344 |
13 |
zero_gravi |
fetch_engine.ci_input <= fetch_engine.i_buf2(15 downto 00);
|
345 |
6 |
zero_gravi |
|
346 |
13 |
zero_gravi |
if (ipb.free = '1') then -- free entry in buffer?
|
347 |
|
|
ipb.we <= '1';
|
348 |
|
|
if (fetch_engine.i_buf2(01 downto 00) = "11") or (CPU_EXTENSION_RISCV_C = false) then -- uncompressed
|
349 |
20 |
zero_gravi |
ipb.wdata <= '0' & fetch_engine.i_buf2(33 downto 32) & '0' & fetch_engine.i_buf2(31 downto 0);
|
350 |
|
|
fetch_engine.pc_add <= std_ulogic_vector(to_unsigned(4, data_width_c));
|
351 |
|
|
fetch_engine.state_nxt <= IFETCH_0;
|
352 |
13 |
zero_gravi |
else -- compressed
|
353 |
20 |
zero_gravi |
ipb.wdata <= ci_illegal & fetch_engine.i_buf2(33 downto 32) & '1' & ci_instr32;
|
354 |
|
|
fetch_engine.pc_add <= std_ulogic_vector(to_unsigned(2, data_width_c));
|
355 |
|
|
fetch_engine.state_nxt <= IFETCH_2; -- try to get another 16-bit instruction word in next round
|
356 |
13 |
zero_gravi |
end if;
|
357 |
|
|
end if;
|
358 |
12 |
zero_gravi |
|
359 |
13 |
zero_gravi |
else -- 16-bit aligned
|
360 |
|
|
fetch_engine.ci_input <= fetch_engine.i_buf2(31 downto 16);
|
361 |
12 |
zero_gravi |
|
362 |
13 |
zero_gravi |
if (ipb.free = '1') then -- free entry in buffer?
|
363 |
|
|
ipb.we <= '1';
|
364 |
22 |
zero_gravi |
if (fetch_engine.i_buf2(17 downto 16) = "11") then -- uncompressed and "unaligned"
|
365 |
20 |
zero_gravi |
ipb.wdata <= '0' & fetch_engine.i_buf(33 downto 32) & '0' & fetch_engine.i_buf(15 downto 00) & fetch_engine.i_buf2(31 downto 16);
|
366 |
|
|
fetch_engine.pc_add <= std_ulogic_vector(to_unsigned(4, data_width_c));
|
367 |
|
|
fetch_engine.state_nxt <= IFETCH_0;
|
368 |
19 |
zero_gravi |
else -- compressed
|
369 |
20 |
zero_gravi |
ipb.wdata <= ci_illegal & fetch_engine.i_buf(33 downto 32) & '1' & ci_instr32;
|
370 |
|
|
fetch_engine.pc_add <= std_ulogic_vector(to_unsigned(2, data_width_c));
|
371 |
|
|
fetch_engine.state_nxt <= IFETCH_0;
|
372 |
13 |
zero_gravi |
end if;
|
373 |
6 |
zero_gravi |
end if;
|
374 |
13 |
zero_gravi |
end if;
|
375 |
6 |
zero_gravi |
|
376 |
|
|
when others => -- undefined
|
377 |
|
|
-- ------------------------------------------------------------
|
378 |
|
|
fetch_engine.state_nxt <= IFETCH_RESET;
|
379 |
|
|
|
380 |
|
|
end case;
|
381 |
|
|
end process fetch_engine_fsm_comb;
|
382 |
|
|
|
383 |
|
|
|
384 |
23 |
zero_gravi |
-- Compressed Instructions Recoding -------------------------------------------------------
|
385 |
|
|
-- -------------------------------------------------------------------------------------------
|
386 |
|
|
neorv32_cpu_decompressor_inst_true:
|
387 |
|
|
if (CPU_EXTENSION_RISCV_C = true) generate
|
388 |
|
|
neorv32_cpu_decompressor_inst: neorv32_cpu_decompressor
|
389 |
|
|
port map (
|
390 |
|
|
-- instruction input --
|
391 |
|
|
ci_instr16_i => fetch_engine.ci_input, -- compressed instruction input
|
392 |
|
|
-- instruction output --
|
393 |
|
|
ci_illegal_o => ci_illegal, -- is an illegal compressed instruction
|
394 |
|
|
ci_instr32_o => ci_instr32 -- 32-bit decompressed instruction
|
395 |
|
|
);
|
396 |
|
|
end generate;
|
397 |
|
|
|
398 |
|
|
neorv32_cpu_decompressor_inst_false:
|
399 |
|
|
if (CPU_EXTENSION_RISCV_C = false) generate
|
400 |
|
|
ci_instr32 <= (others => '0');
|
401 |
|
|
ci_illegal <= '0';
|
402 |
|
|
end generate;
|
403 |
|
|
|
404 |
|
|
|
405 |
6 |
zero_gravi |
-- ****************************************************************************************************************************
|
406 |
|
|
-- Instruction Prefetch Buffer
|
407 |
|
|
-- ****************************************************************************************************************************
|
408 |
|
|
|
409 |
|
|
|
410 |
20 |
zero_gravi |
-- Instruction Prefetch Buffer (FIFO) -----------------------------------------------------
|
411 |
6 |
zero_gravi |
-- -------------------------------------------------------------------------------------------
|
412 |
20 |
zero_gravi |
instr_prefetch_buffer_ctrl: process(rstn_i, clk_i)
|
413 |
6 |
zero_gravi |
begin
|
414 |
|
|
if (rstn_i = '0') then
|
415 |
20 |
zero_gravi |
ipb.w_pnt <= (others => '0');
|
416 |
|
|
ipb.r_pnt <= (others => '0');
|
417 |
6 |
zero_gravi |
elsif rising_edge(clk_i) then
|
418 |
20 |
zero_gravi |
-- write port --
|
419 |
6 |
zero_gravi |
if (ipb.clear = '1') then
|
420 |
20 |
zero_gravi |
ipb.w_pnt <= (others => '0');
|
421 |
6 |
zero_gravi |
elsif (ipb.we = '1') then
|
422 |
20 |
zero_gravi |
ipb.w_pnt <= std_ulogic_vector(unsigned(ipb.w_pnt) + 1);
|
423 |
|
|
end if;
|
424 |
|
|
-- read port --
|
425 |
|
|
if (ipb.clear = '1') then
|
426 |
|
|
ipb.r_pnt <= (others => '0');
|
427 |
6 |
zero_gravi |
elsif (ipb.re = '1') then
|
428 |
20 |
zero_gravi |
ipb.r_pnt <= std_ulogic_vector(unsigned(ipb.r_pnt) + 1);
|
429 |
6 |
zero_gravi |
end if;
|
430 |
20 |
zero_gravi |
end if;
|
431 |
|
|
end process instr_prefetch_buffer_ctrl;
|
432 |
|
|
|
433 |
|
|
instr_prefetch_buffer_data: process(clk_i)
|
434 |
|
|
begin
|
435 |
|
|
if rising_edge(clk_i) then
|
436 |
|
|
if (ipb.we = '1') then -- write port
|
437 |
|
|
ipb.data(to_integer(unsigned(ipb.w_pnt(ipb.w_pnt'left-1 downto 0)))) <= ipb.wdata;
|
438 |
6 |
zero_gravi |
end if;
|
439 |
|
|
end if;
|
440 |
20 |
zero_gravi |
end process instr_prefetch_buffer_data;
|
441 |
6 |
zero_gravi |
|
442 |
20 |
zero_gravi |
-- async read --
|
443 |
|
|
ipb.rdata <= ipb.data(to_integer(unsigned(ipb.r_pnt(ipb.w_pnt'left-1 downto 0))));
|
444 |
|
|
|
445 |
6 |
zero_gravi |
-- status --
|
446 |
20 |
zero_gravi |
ipb.full <= '1' when (ipb.r_pnt(ipb.r_pnt'left) /= ipb.w_pnt(ipb.w_pnt'left)) and (ipb.r_pnt(ipb.r_pnt'left-1 downto 0) = ipb.w_pnt(ipb.w_pnt'left-1 downto 0)) else '0';
|
447 |
|
|
ipb.empty <= '1' when (ipb.r_pnt(ipb.r_pnt'left) = ipb.w_pnt(ipb.w_pnt'left)) and (ipb.r_pnt(ipb.r_pnt'left-1 downto 0) = ipb.w_pnt(ipb.w_pnt'left-1 downto 0)) else '0';
|
448 |
|
|
|
449 |
|
|
ipb.free <= not ipb.full;
|
450 |
|
|
ipb.avail <= not ipb.empty;
|
451 |
6 |
zero_gravi |
|
452 |
|
|
|
453 |
|
|
-- ****************************************************************************************************************************
|
454 |
|
|
-- Instruction Execution
|
455 |
|
|
-- ****************************************************************************************************************************
|
456 |
|
|
|
457 |
|
|
|
458 |
2 |
zero_gravi |
-- Immediate Generator --------------------------------------------------------------------
|
459 |
|
|
-- -------------------------------------------------------------------------------------------
|
460 |
|
|
imm_gen: process(clk_i)
|
461 |
|
|
begin
|
462 |
|
|
if rising_edge(clk_i) then
|
463 |
6 |
zero_gravi |
case execute_engine.i_reg(instr_opcode_msb_c downto instr_opcode_lsb_c) is
|
464 |
2 |
zero_gravi |
when opcode_store_c => -- S-immediate
|
465 |
6 |
zero_gravi |
imm_o(31 downto 11) <= (others => execute_engine.i_reg(31)); -- sign extension
|
466 |
|
|
imm_o(10 downto 05) <= execute_engine.i_reg(30 downto 25);
|
467 |
|
|
imm_o(04 downto 01) <= execute_engine.i_reg(11 downto 08);
|
468 |
|
|
imm_o(00) <= execute_engine.i_reg(07);
|
469 |
2 |
zero_gravi |
when opcode_branch_c => -- B-immediate
|
470 |
6 |
zero_gravi |
imm_o(31 downto 12) <= (others => execute_engine.i_reg(31)); -- sign extension
|
471 |
|
|
imm_o(11) <= execute_engine.i_reg(07);
|
472 |
|
|
imm_o(10 downto 05) <= execute_engine.i_reg(30 downto 25);
|
473 |
|
|
imm_o(04 downto 01) <= execute_engine.i_reg(11 downto 08);
|
474 |
|
|
imm_o(00) <= '0';
|
475 |
2 |
zero_gravi |
when opcode_lui_c | opcode_auipc_c => -- U-immediate
|
476 |
6 |
zero_gravi |
imm_o(31 downto 20) <= execute_engine.i_reg(31 downto 20);
|
477 |
|
|
imm_o(19 downto 12) <= execute_engine.i_reg(19 downto 12);
|
478 |
|
|
imm_o(11 downto 00) <= (others => '0');
|
479 |
2 |
zero_gravi |
when opcode_jal_c => -- J-immediate
|
480 |
6 |
zero_gravi |
imm_o(31 downto 20) <= (others => execute_engine.i_reg(31)); -- sign extension
|
481 |
|
|
imm_o(19 downto 12) <= execute_engine.i_reg(19 downto 12);
|
482 |
|
|
imm_o(11) <= execute_engine.i_reg(20);
|
483 |
|
|
imm_o(10 downto 05) <= execute_engine.i_reg(30 downto 25);
|
484 |
|
|
imm_o(04 downto 01) <= execute_engine.i_reg(24 downto 21);
|
485 |
|
|
imm_o(00) <= '0';
|
486 |
2 |
zero_gravi |
when opcode_syscsr_c => -- CSR-immediate
|
487 |
6 |
zero_gravi |
imm_o(31 downto 05) <= (others => '0');
|
488 |
|
|
imm_o(04 downto 00) <= execute_engine.i_reg(19 downto 15);
|
489 |
2 |
zero_gravi |
when others => -- I-immediate
|
490 |
6 |
zero_gravi |
imm_o(31 downto 11) <= (others => execute_engine.i_reg(31)); -- sign extension
|
491 |
|
|
imm_o(10 downto 05) <= execute_engine.i_reg(30 downto 25);
|
492 |
|
|
imm_o(04 downto 01) <= execute_engine.i_reg(24 downto 21);
|
493 |
|
|
imm_o(00) <= execute_engine.i_reg(20);
|
494 |
2 |
zero_gravi |
end case;
|
495 |
|
|
end if;
|
496 |
|
|
end process imm_gen;
|
497 |
|
|
|
498 |
|
|
|
499 |
|
|
-- Branch Condition Check -----------------------------------------------------------------
|
500 |
|
|
-- -------------------------------------------------------------------------------------------
|
501 |
6 |
zero_gravi |
branch_check: process(execute_engine.i_reg, cmp_i)
|
502 |
2 |
zero_gravi |
begin
|
503 |
6 |
zero_gravi |
case execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) is
|
504 |
2 |
zero_gravi |
when funct3_beq_c => -- branch if equal
|
505 |
6 |
zero_gravi |
execute_engine.branch_taken <= cmp_i(alu_cmp_equal_c);
|
506 |
2 |
zero_gravi |
when funct3_bne_c => -- branch if not equal
|
507 |
6 |
zero_gravi |
execute_engine.branch_taken <= not cmp_i(alu_cmp_equal_c);
|
508 |
2 |
zero_gravi |
when funct3_blt_c | funct3_bltu_c => -- branch if less (signed/unsigned)
|
509 |
6 |
zero_gravi |
execute_engine.branch_taken <= cmp_i(alu_cmp_less_c);
|
510 |
2 |
zero_gravi |
when funct3_bge_c | funct3_bgeu_c => -- branch if greater or equal (signed/unsigned)
|
511 |
6 |
zero_gravi |
execute_engine.branch_taken <= not cmp_i(alu_cmp_less_c);
|
512 |
2 |
zero_gravi |
when others => -- undefined
|
513 |
6 |
zero_gravi |
execute_engine.branch_taken <= '0';
|
514 |
2 |
zero_gravi |
end case;
|
515 |
|
|
end process branch_check;
|
516 |
|
|
|
517 |
|
|
|
518 |
6 |
zero_gravi |
-- Execute Engine FSM Sync ----------------------------------------------------------------
|
519 |
2 |
zero_gravi |
-- -------------------------------------------------------------------------------------------
|
520 |
12 |
zero_gravi |
-- for registers that DO require a specific reset state --
|
521 |
6 |
zero_gravi |
execute_engine_fsm_sync_rst: process(rstn_i, clk_i)
|
522 |
2 |
zero_gravi |
begin
|
523 |
|
|
if (rstn_i = '0') then
|
524 |
12 |
zero_gravi |
execute_engine.pc <= CPU_BOOT_ADDR(data_width_c-1 downto 1) & '0';
|
525 |
|
|
execute_engine.last_pc <= CPU_BOOT_ADDR(data_width_c-1 downto 1) & '0';
|
526 |
|
|
execute_engine.state <= SYS_WAIT;
|
527 |
13 |
zero_gravi |
execute_engine.sleep <= '0';
|
528 |
23 |
zero_gravi |
execute_engine.if_rst <= '1'; -- instruction fetch is reset after system reset
|
529 |
2 |
zero_gravi |
elsif rising_edge(clk_i) then
|
530 |
18 |
zero_gravi |
execute_engine.pc <= execute_engine.pc_nxt(data_width_c-1 downto 1) & '0';
|
531 |
6 |
zero_gravi |
if (execute_engine.state = EXECUTE) then
|
532 |
|
|
execute_engine.last_pc <= execute_engine.pc(data_width_c-1 downto 1) & '0';
|
533 |
|
|
end if;
|
534 |
20 |
zero_gravi |
execute_engine.state <= execute_engine.state_nxt;
|
535 |
|
|
execute_engine.sleep <= execute_engine.sleep_nxt;
|
536 |
|
|
execute_engine.if_rst <= execute_engine.if_rst_nxt;
|
537 |
2 |
zero_gravi |
end if;
|
538 |
6 |
zero_gravi |
end process execute_engine_fsm_sync_rst;
|
539 |
2 |
zero_gravi |
|
540 |
6 |
zero_gravi |
|
541 |
12 |
zero_gravi |
-- for registers that do NOT require a specific reset state --
|
542 |
6 |
zero_gravi |
execute_engine_fsm_sync: process(clk_i)
|
543 |
2 |
zero_gravi |
begin
|
544 |
|
|
if rising_edge(clk_i) then
|
545 |
19 |
zero_gravi |
execute_engine.state_prev <= execute_engine.state;
|
546 |
|
|
execute_engine.i_reg <= execute_engine.i_reg_nxt;
|
547 |
|
|
execute_engine.is_ci <= execute_engine.is_ci_nxt;
|
548 |
|
|
execute_engine.is_jump <= execute_engine.is_jump_nxt;
|
549 |
|
|
--
|
550 |
6 |
zero_gravi |
ctrl <= ctrl_nxt;
|
551 |
2 |
zero_gravi |
end if;
|
552 |
6 |
zero_gravi |
end process execute_engine_fsm_sync;
|
553 |
2 |
zero_gravi |
|
554 |
20 |
zero_gravi |
-- next PC --
|
555 |
|
|
next_pc_tmp <= std_ulogic_vector(unsigned(execute_engine.pc) + 2) when (execute_engine.is_ci = '1') else std_ulogic_vector(unsigned(execute_engine.pc) + 4);
|
556 |
12 |
zero_gravi |
execute_engine.next_pc <= next_pc_tmp(data_width_c-1 downto 1) & '0';
|
557 |
6 |
zero_gravi |
|
558 |
20 |
zero_gravi |
-- PC output --
|
559 |
|
|
curr_pc_o <= execute_engine.pc(data_width_c-1 downto 1) & '0';
|
560 |
|
|
next_pc_o <= next_pc_tmp(data_width_c-1 downto 1) & '0';
|
561 |
6 |
zero_gravi |
|
562 |
20 |
zero_gravi |
|
563 |
6 |
zero_gravi |
-- CPU Control Bus Output -----------------------------------------------------------------
|
564 |
|
|
-- -------------------------------------------------------------------------------------------
|
565 |
11 |
zero_gravi |
ctrl_output: process(ctrl, execute_engine, fetch_engine, trap_ctrl, csr, bus_fast_ir)
|
566 |
2 |
zero_gravi |
begin
|
567 |
|
|
ctrl_o <= ctrl;
|
568 |
|
|
-- direct output of register addresses --
|
569 |
6 |
zero_gravi |
ctrl_o(ctrl_rf_rd_adr4_c downto ctrl_rf_rd_adr0_c) <= execute_engine.i_reg(instr_rd_msb_c downto instr_rd_lsb_c);
|
570 |
|
|
ctrl_o(ctrl_rf_rs1_adr4_c downto ctrl_rf_rs1_adr0_c) <= execute_engine.i_reg(instr_rs1_msb_c downto instr_rs1_lsb_c);
|
571 |
|
|
ctrl_o(ctrl_rf_rs2_adr4_c downto ctrl_rf_rs2_adr0_c) <= execute_engine.i_reg(instr_rs2_msb_c downto instr_rs2_lsb_c);
|
572 |
12 |
zero_gravi |
-- fast bus access requests --
|
573 |
6 |
zero_gravi |
ctrl_o(ctrl_bus_if_c) <= ctrl(ctrl_bus_if_c) or bus_fast_ir;
|
574 |
12 |
zero_gravi |
-- bus error control --
|
575 |
|
|
ctrl_o(ctrl_bus_ierr_ack_c) <= fetch_engine.bus_err_ack;
|
576 |
|
|
ctrl_o(ctrl_bus_derr_ack_c) <= trap_ctrl.env_start_ack;
|
577 |
6 |
zero_gravi |
end process ctrl_output;
|
578 |
2 |
zero_gravi |
|
579 |
|
|
|
580 |
6 |
zero_gravi |
-- Execute Engine FSM Comb ----------------------------------------------------------------
|
581 |
|
|
-- -------------------------------------------------------------------------------------------
|
582 |
15 |
zero_gravi |
execute_engine_fsm_comb: process(execute_engine, fetch_engine, ipb, trap_ctrl, csr, ctrl, csr_acc_valid,
|
583 |
12 |
zero_gravi |
alu_add_i, alu_wait_i, bus_d_wait_i, ma_load_i, be_load_i, ma_store_i, be_store_i)
|
584 |
2 |
zero_gravi |
variable alu_immediate_v : std_ulogic;
|
585 |
|
|
variable alu_operation_v : std_ulogic_vector(2 downto 0);
|
586 |
|
|
variable rs1_is_r0_v : std_ulogic;
|
587 |
|
|
begin
|
588 |
|
|
-- arbiter defaults --
|
589 |
6 |
zero_gravi |
execute_engine.state_nxt <= execute_engine.state;
|
590 |
|
|
execute_engine.i_reg_nxt <= execute_engine.i_reg;
|
591 |
|
|
execute_engine.is_jump_nxt <= '0';
|
592 |
|
|
execute_engine.is_ci_nxt <= execute_engine.is_ci;
|
593 |
13 |
zero_gravi |
execute_engine.pc_nxt <= execute_engine.pc;
|
594 |
11 |
zero_gravi |
execute_engine.sleep_nxt <= execute_engine.sleep;
|
595 |
20 |
zero_gravi |
execute_engine.if_rst_nxt <= execute_engine.if_rst;
|
596 |
2 |
zero_gravi |
|
597 |
6 |
zero_gravi |
-- instruction dispatch --
|
598 |
|
|
fetch_engine.reset <= '0';
|
599 |
|
|
ipb.re <= '0';
|
600 |
2 |
zero_gravi |
|
601 |
6 |
zero_gravi |
-- trap environment control --
|
602 |
|
|
trap_ctrl.env_start_ack <= '0';
|
603 |
|
|
trap_ctrl.env_end <= '0';
|
604 |
|
|
|
605 |
2 |
zero_gravi |
-- exception trigger --
|
606 |
6 |
zero_gravi |
trap_ctrl.instr_be <= '0';
|
607 |
|
|
trap_ctrl.instr_ma <= '0';
|
608 |
|
|
trap_ctrl.env_call <= '0';
|
609 |
|
|
trap_ctrl.break_point <= '0';
|
610 |
13 |
zero_gravi |
illegal_compressed <= '0';
|
611 |
2 |
zero_gravi |
|
612 |
6 |
zero_gravi |
-- CSR access --
|
613 |
|
|
csr.we_nxt <= '0';
|
614 |
|
|
csr.re_nxt <= '0';
|
615 |
|
|
|
616 |
2 |
zero_gravi |
-- control defaults --
|
617 |
|
|
ctrl_nxt <= (others => '0'); -- all off at first
|
618 |
6 |
zero_gravi |
if (execute_engine.i_reg(instr_opcode_lsb_c+4) = '1') then -- ALU ops
|
619 |
|
|
ctrl_nxt(ctrl_alu_unsigned_c) <= execute_engine.i_reg(instr_funct3_lsb_c+0); -- unsigned ALU operation (SLTIU, SLTU)
|
620 |
2 |
zero_gravi |
else -- branches
|
621 |
6 |
zero_gravi |
ctrl_nxt(ctrl_alu_unsigned_c) <= execute_engine.i_reg(instr_funct3_lsb_c+1); -- unsigned branches (BLTU, BGEU)
|
622 |
2 |
zero_gravi |
end if;
|
623 |
23 |
zero_gravi |
ctrl_nxt(ctrl_bus_unsigned_c) <= execute_engine.i_reg(instr_funct3_msb_c); -- unsigned LOAD (LBU, LHU)
|
624 |
|
|
ctrl_nxt(ctrl_alu_shift_dir_c) <= execute_engine.i_reg(instr_funct3_msb_c); -- shift direction (left/right)
|
625 |
|
|
ctrl_nxt(ctrl_alu_shift_ar_c) <= execute_engine.i_reg(30); -- is arithmetic shift
|
626 |
|
|
ctrl_nxt(ctrl_bus_size_lsb_c) <= execute_engine.i_reg(instr_funct3_lsb_c+0); -- transfer size lsb (00=byte, 01=half-word)
|
627 |
|
|
ctrl_nxt(ctrl_bus_size_msb_c) <= execute_engine.i_reg(instr_funct3_lsb_c+1); -- transfer size msb (10=word, 11=?)
|
628 |
6 |
zero_gravi |
ctrl_nxt(ctrl_cp_cmd2_c downto ctrl_cp_cmd0_c) <= execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c); -- CP operation
|
629 |
12 |
zero_gravi |
ctrl_nxt(ctrl_cp_id_msb_c downto ctrl_cp_id_lsb_c) <= cp_sel_muldiv_c; -- only CP0 (MULDIV) implemented yet
|
630 |
2 |
zero_gravi |
|
631 |
|
|
-- is immediate operation? --
|
632 |
|
|
alu_immediate_v := '0';
|
633 |
6 |
zero_gravi |
if (execute_engine.i_reg(instr_opcode_msb_c-1) = '0') then
|
634 |
2 |
zero_gravi |
alu_immediate_v := '1';
|
635 |
|
|
end if;
|
636 |
|
|
|
637 |
6 |
zero_gravi |
-- alu operation re-coding --
|
638 |
|
|
case execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) is
|
639 |
2 |
zero_gravi |
when funct3_subadd_c => -- SUB / ADD(I)
|
640 |
6 |
zero_gravi |
if (alu_immediate_v = '0') and (execute_engine.i_reg(instr_funct7_msb_c-1) = '1') then -- not immediate and funct7 = SUB
|
641 |
2 |
zero_gravi |
alu_operation_v := alu_cmd_sub_c;
|
642 |
|
|
else
|
643 |
|
|
alu_operation_v := alu_cmd_add_c;
|
644 |
|
|
end if;
|
645 |
|
|
when funct3_sll_c => alu_operation_v := alu_cmd_shift_c; -- SLL(I)
|
646 |
6 |
zero_gravi |
when funct3_slt_c => alu_operation_v := alu_cmd_slt_c; -- SLT(I)
|
647 |
|
|
when funct3_sltu_c => alu_operation_v := alu_cmd_slt_c; -- SLTU(I)
|
648 |
|
|
when funct3_xor_c => alu_operation_v := alu_cmd_xor_c; -- XOR(I)
|
649 |
2 |
zero_gravi |
when funct3_sr_c => alu_operation_v := alu_cmd_shift_c; -- SRL(I) / SRA(I)
|
650 |
6 |
zero_gravi |
when funct3_or_c => alu_operation_v := alu_cmd_or_c; -- OR(I)
|
651 |
|
|
when funct3_and_c => alu_operation_v := alu_cmd_and_c; -- AND(I)
|
652 |
3 |
zero_gravi |
when others => alu_operation_v := (others => '0'); -- undefined
|
653 |
2 |
zero_gravi |
end case;
|
654 |
|
|
|
655 |
|
|
-- is rs1 = r0? --
|
656 |
|
|
rs1_is_r0_v := '0';
|
657 |
6 |
zero_gravi |
if (execute_engine.i_reg(instr_rs1_msb_c downto instr_rs1_lsb_c) = "00000") then
|
658 |
2 |
zero_gravi |
rs1_is_r0_v := '1';
|
659 |
|
|
end if;
|
660 |
|
|
|
661 |
6 |
zero_gravi |
-- state machine --
|
662 |
|
|
case execute_engine.state is
|
663 |
2 |
zero_gravi |
|
664 |
7 |
zero_gravi |
when SYS_WAIT => -- Delay cycle (used to wait for side effects to kick in)
|
665 |
2 |
zero_gravi |
-- ------------------------------------------------------------
|
666 |
6 |
zero_gravi |
execute_engine.state_nxt <= DISPATCH;
|
667 |
2 |
zero_gravi |
|
668 |
6 |
zero_gravi |
when DISPATCH => -- Get new command from instruction prefetch buffer (IPB)
|
669 |
|
|
-- ------------------------------------------------------------
|
670 |
13 |
zero_gravi |
if (ipb.avail = '1') then -- instruction available?
|
671 |
|
|
ipb.re <= '1';
|
672 |
|
|
trap_ctrl.instr_ma <= ipb.rdata(33); -- misaligned instruction fetch address
|
673 |
20 |
zero_gravi |
trap_ctrl.instr_be <= ipb.rdata(34); -- bus access fault during instrucion fetch
|
674 |
13 |
zero_gravi |
illegal_compressed <= ipb.rdata(35); -- invalid decompressed instruction
|
675 |
21 |
zero_gravi |
execute_engine.is_ci_nxt <= ipb.rdata(32); -- flag to indicate this is a compressed instruction beeing executed
|
676 |
|
|
execute_engine.i_reg_nxt <= ipb.rdata(31 downto 0);
|
677 |
|
|
execute_engine.if_rst_nxt <= '0';
|
678 |
|
|
if (execute_engine.if_rst = '0') then -- if there was no non-linear PC modification
|
679 |
|
|
execute_engine.pc_nxt <= execute_engine.next_pc;
|
680 |
|
|
end if;
|
681 |
|
|
--
|
682 |
14 |
zero_gravi |
if (execute_engine.sleep = '1') or (trap_ctrl.env_start = '1') or ((ipb.rdata(33) or ipb.rdata(34)) = '1') then
|
683 |
13 |
zero_gravi |
execute_engine.state_nxt <= TRAP;
|
684 |
|
|
else
|
685 |
14 |
zero_gravi |
execute_engine.state_nxt <= EXECUTE;
|
686 |
13 |
zero_gravi |
end if;
|
687 |
|
|
end if;
|
688 |
2 |
zero_gravi |
|
689 |
11 |
zero_gravi |
when TRAP => -- Start trap environment (also used as cpu sleep state)
|
690 |
2 |
zero_gravi |
-- ------------------------------------------------------------
|
691 |
20 |
zero_gravi |
fetch_engine.reset <= '1';
|
692 |
|
|
execute_engine.if_rst_nxt <= '1'; -- this is a non-linear PC modification
|
693 |
13 |
zero_gravi |
if (trap_ctrl.env_start = '1') then -- check here again if we came directly from DISPATCH
|
694 |
6 |
zero_gravi |
trap_ctrl.env_start_ack <= '1';
|
695 |
13 |
zero_gravi |
execute_engine.pc_nxt <= csr.mtvec;
|
696 |
11 |
zero_gravi |
execute_engine.sleep_nxt <= '0'; -- waky waky
|
697 |
7 |
zero_gravi |
execute_engine.state_nxt <= SYS_WAIT;
|
698 |
2 |
zero_gravi |
end if;
|
699 |
|
|
|
700 |
6 |
zero_gravi |
when EXECUTE => -- Decode and execute instruction
|
701 |
2 |
zero_gravi |
-- ------------------------------------------------------------
|
702 |
6 |
zero_gravi |
case execute_engine.i_reg(instr_opcode_msb_c downto instr_opcode_lsb_c) is
|
703 |
2 |
zero_gravi |
|
704 |
|
|
when opcode_alu_c | opcode_alui_c => -- ALU operation
|
705 |
|
|
-- ------------------------------------------------------------
|
706 |
|
|
ctrl_nxt(ctrl_alu_opa_mux_lsb_c) <= '0'; -- use RS1 as ALU.OPA
|
707 |
|
|
ctrl_nxt(ctrl_alu_opb_mux_lsb_c) <= alu_immediate_v; -- use IMM as ALU.OPB for immediate operations
|
708 |
|
|
ctrl_nxt(ctrl_alu_opc_mux_c) <= not alu_immediate_v;
|
709 |
|
|
ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_operation_v; -- actual ALU operation
|
710 |
|
|
ctrl_nxt(ctrl_rf_in_mux_msb_c downto ctrl_rf_in_mux_lsb_c) <= "00"; -- RF input = ALU result
|
711 |
11 |
zero_gravi |
-- multi cycle alu operation? --
|
712 |
19 |
zero_gravi |
if (alu_operation_v = alu_cmd_shift_c) or -- shift operation?
|
713 |
23 |
zero_gravi |
((CPU_EXTENSION_RISCV_M = true) and (execute_engine.i_reg(instr_opcode_lsb_c+5) = opcode_alu_c(5)) and
|
714 |
12 |
zero_gravi |
(execute_engine.i_reg(instr_funct7_lsb_c) = '1')) then -- MULDIV?
|
715 |
6 |
zero_gravi |
execute_engine.state_nxt <= ALU_WAIT;
|
716 |
2 |
zero_gravi |
else
|
717 |
|
|
ctrl_nxt(ctrl_rf_wb_en_c) <= '1'; -- valid RF write-back
|
718 |
6 |
zero_gravi |
execute_engine.state_nxt <= DISPATCH;
|
719 |
2 |
zero_gravi |
end if;
|
720 |
11 |
zero_gravi |
-- cp access? --
|
721 |
23 |
zero_gravi |
if (CPU_EXTENSION_RISCV_M = true) and (execute_engine.i_reg(instr_opcode_lsb_c+5) = opcode_alu_c(5)) and
|
722 |
12 |
zero_gravi |
(execute_engine.i_reg(instr_funct7_lsb_c) = '1') then -- MULDIV?
|
723 |
11 |
zero_gravi |
ctrl_nxt(ctrl_cp_use_c) <= '1'; -- use CP
|
724 |
|
|
end if;
|
725 |
2 |
zero_gravi |
|
726 |
|
|
when opcode_lui_c | opcode_auipc_c => -- load upper immediate (add to PC)
|
727 |
|
|
-- ------------------------------------------------------------
|
728 |
12 |
zero_gravi |
ctrl_nxt(ctrl_rf_clear_rs1_c) <= '1'; -- force RS1 = r0 (only relevant for LUI)
|
729 |
23 |
zero_gravi |
if (execute_engine.i_reg(instr_opcode_lsb_c+5) = opcode_auipc_c(5)) then -- AUIPC
|
730 |
2 |
zero_gravi |
ctrl_nxt(ctrl_alu_opa_mux_lsb_c) <= '1'; -- use PC as ALU.OPA
|
731 |
|
|
else -- LUI
|
732 |
|
|
ctrl_nxt(ctrl_alu_opa_mux_lsb_c) <= '0'; -- use RS1 as ALU.OPA
|
733 |
|
|
end if;
|
734 |
6 |
zero_gravi |
ctrl_nxt(ctrl_alu_opb_mux_lsb_c) <= '1'; -- use IMM as ALU.OPB
|
735 |
2 |
zero_gravi |
ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_add_c; -- actual ALU operation
|
736 |
|
|
ctrl_nxt(ctrl_rf_in_mux_msb_c downto ctrl_rf_in_mux_lsb_c) <= "00"; -- RF input = ALU result
|
737 |
|
|
ctrl_nxt(ctrl_rf_wb_en_c) <= '1'; -- valid RF write-back
|
738 |
6 |
zero_gravi |
execute_engine.state_nxt <= DISPATCH;
|
739 |
2 |
zero_gravi |
|
740 |
|
|
when opcode_load_c | opcode_store_c => -- load/store
|
741 |
|
|
-- ------------------------------------------------------------
|
742 |
|
|
ctrl_nxt(ctrl_alu_opa_mux_lsb_c) <= '0'; -- use RS1 as ALU.OPA
|
743 |
|
|
ctrl_nxt(ctrl_alu_opb_mux_lsb_c) <= '1'; -- use IMM as ALU.OPB
|
744 |
|
|
ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_add_c; -- actual ALU operation
|
745 |
6 |
zero_gravi |
ctrl_nxt(ctrl_bus_mar_we_c) <= '1'; -- write to MAR
|
746 |
|
|
ctrl_nxt(ctrl_bus_mdo_we_c) <= '1'; -- write to MDO (only relevant for stores)
|
747 |
12 |
zero_gravi |
execute_engine.state_nxt <= LOADSTORE_0;
|
748 |
2 |
zero_gravi |
|
749 |
|
|
when opcode_branch_c => -- branch instruction
|
750 |
|
|
-- ------------------------------------------------------------
|
751 |
|
|
ctrl_nxt(ctrl_alu_opa_mux_lsb_c) <= '1'; -- use PC as ALU.OPA
|
752 |
|
|
ctrl_nxt(ctrl_alu_opb_mux_lsb_c) <= '1'; -- use IMM as ALU.OPB
|
753 |
6 |
zero_gravi |
ctrl_nxt(ctrl_alu_opc_mux_c) <= '1'; -- use RS2 as ALU.OPC
|
754 |
|
|
execute_engine.state_nxt <= BRANCH;
|
755 |
2 |
zero_gravi |
|
756 |
|
|
when opcode_jal_c | opcode_jalr_c => -- jump and link (with register)
|
757 |
|
|
-- ------------------------------------------------------------
|
758 |
|
|
-- compute target address --
|
759 |
23 |
zero_gravi |
if (execute_engine.i_reg(instr_opcode_lsb_c+3) = opcode_jal_c(3)) then -- JAL
|
760 |
2 |
zero_gravi |
ctrl_nxt(ctrl_alu_opa_mux_lsb_c) <= '1'; -- use PC as ALU.OPA
|
761 |
|
|
else -- JALR
|
762 |
|
|
ctrl_nxt(ctrl_alu_opa_mux_lsb_c) <= '0'; -- use RS1 as ALU.OPA
|
763 |
|
|
end if;
|
764 |
|
|
ctrl_nxt(ctrl_alu_opb_mux_lsb_c) <= '1'; -- use IMM as ALU.OPB
|
765 |
|
|
-- save return address --
|
766 |
13 |
zero_gravi |
ctrl_nxt(ctrl_rf_in_mux_msb_c downto ctrl_rf_in_mux_lsb_c) <= "10"; -- RF input = next PC (save return address)
|
767 |
2 |
zero_gravi |
ctrl_nxt(ctrl_rf_wb_en_c) <= '1'; -- valid RF write-back
|
768 |
6 |
zero_gravi |
--
|
769 |
|
|
execute_engine.is_jump_nxt <= '1'; -- this is a jump operation
|
770 |
|
|
execute_engine.state_nxt <= BRANCH;
|
771 |
2 |
zero_gravi |
|
772 |
8 |
zero_gravi |
when opcode_fence_c => -- fence operations
|
773 |
|
|
-- ------------------------------------------------------------
|
774 |
23 |
zero_gravi |
if (execute_engine.i_reg(instr_funct3_lsb_c) = funct3_fencei_c(0)) and (CPU_EXTENSION_RISCV_Zifencei = true) then -- FENCE.I
|
775 |
12 |
zero_gravi |
fetch_engine.reset <= '1';
|
776 |
20 |
zero_gravi |
execute_engine.if_rst_nxt <= '1'; -- this is a non-linear PC modification
|
777 |
|
|
execute_engine.pc_nxt <= execute_engine.next_pc; -- "refetch" next instruction (only relevant for fence.i)
|
778 |
12 |
zero_gravi |
ctrl_nxt(ctrl_bus_fencei_c) <= '1';
|
779 |
8 |
zero_gravi |
end if;
|
780 |
23 |
zero_gravi |
if (execute_engine.i_reg(instr_funct3_lsb_c) = funct3_fence_c(0)) then -- FENCE
|
781 |
12 |
zero_gravi |
ctrl_nxt(ctrl_bus_fence_c) <= '1';
|
782 |
|
|
end if;
|
783 |
|
|
execute_engine.state_nxt <= SYS_WAIT;
|
784 |
8 |
zero_gravi |
|
785 |
2 |
zero_gravi |
when opcode_syscsr_c => -- system/csr access
|
786 |
|
|
-- ------------------------------------------------------------
|
787 |
15 |
zero_gravi |
csr.re_nxt <= csr_acc_valid; -- read CSR if valid access
|
788 |
6 |
zero_gravi |
if (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_env_c) then -- system
|
789 |
|
|
case execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) is
|
790 |
11 |
zero_gravi |
when funct12_ecall_c => -- ECALL
|
791 |
6 |
zero_gravi |
trap_ctrl.env_call <= '1';
|
792 |
11 |
zero_gravi |
when funct12_ebreak_c => -- EBREAK
|
793 |
6 |
zero_gravi |
trap_ctrl.break_point <= '1';
|
794 |
11 |
zero_gravi |
when funct12_mret_c => -- MRET
|
795 |
20 |
zero_gravi |
trap_ctrl.env_end <= '1';
|
796 |
|
|
execute_engine.pc_nxt <= csr.mepc;
|
797 |
|
|
fetch_engine.reset <= '1';
|
798 |
|
|
execute_engine.if_rst_nxt <= '1'; -- this is a non-linear PC modification
|
799 |
11 |
zero_gravi |
when funct12_wfi_c => -- WFI = "CPU sleep"
|
800 |
|
|
execute_engine.sleep_nxt <= '1'; -- good night
|
801 |
6 |
zero_gravi |
when others => -- undefined
|
802 |
|
|
NULL;
|
803 |
2 |
zero_gravi |
end case;
|
804 |
11 |
zero_gravi |
execute_engine.state_nxt <= SYS_WAIT;
|
805 |
13 |
zero_gravi |
else -- CSR access
|
806 |
|
|
execute_engine.state_nxt <= CSR_ACCESS;
|
807 |
2 |
zero_gravi |
end if;
|
808 |
|
|
|
809 |
|
|
when others => -- undefined
|
810 |
|
|
-- ------------------------------------------------------------
|
811 |
6 |
zero_gravi |
execute_engine.state_nxt <= DISPATCH;
|
812 |
2 |
zero_gravi |
|
813 |
|
|
end case;
|
814 |
|
|
|
815 |
|
|
when CSR_ACCESS => -- write CSR data to RF, write ALU.res to CSR
|
816 |
|
|
-- ------------------------------------------------------------
|
817 |
|
|
ctrl_nxt(ctrl_alu_opa_mux_msb_c) <= '0'; -- default
|
818 |
|
|
ctrl_nxt(ctrl_alu_opa_mux_lsb_c) <= '0'; -- default
|
819 |
|
|
ctrl_nxt(ctrl_alu_opb_mux_msb_c) <= '0'; -- default
|
820 |
|
|
ctrl_nxt(ctrl_alu_opb_mux_lsb_c) <= '0'; -- default
|
821 |
23 |
zero_gravi |
ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_or_c; -- default ALU operation = OR
|
822 |
|
|
-- ctrl_nxt(ctrl_alu_bmop2_c downto ctrl_alu_bmop0_c) <= alu_bm_andn_c; -- bit manipulation operation = ANDN
|
823 |
6 |
zero_gravi |
case execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) is
|
824 |
7 |
zero_gravi |
-- register operations --
|
825 |
6 |
zero_gravi |
when funct3_csrrw_c => -- CSRRW
|
826 |
2 |
zero_gravi |
ctrl_nxt(ctrl_alu_opa_mux_lsb_c) <= '0'; -- OPA = rs1
|
827 |
|
|
ctrl_nxt(ctrl_alu_opb_mux_lsb_c) <= '0'; -- OPB = rs2
|
828 |
|
|
ctrl_nxt(ctrl_rf_clear_rs2_c) <= '1'; -- rs2 = 0
|
829 |
12 |
zero_gravi |
ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_or_c; -- actual ALU operation = OR
|
830 |
15 |
zero_gravi |
csr.we_nxt <= csr_acc_valid; -- always write CSR if valid access
|
831 |
6 |
zero_gravi |
when funct3_csrrs_c => -- CSRRS
|
832 |
2 |
zero_gravi |
ctrl_nxt(ctrl_alu_opa_mux_msb_c) <= '1'; -- OPA = csr
|
833 |
12 |
zero_gravi |
ctrl_nxt(ctrl_alu_opb_mux_msb_c) <= '1'; -- OPB = rs1
|
834 |
2 |
zero_gravi |
ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_or_c; -- actual ALU operation = OR
|
835 |
15 |
zero_gravi |
csr.we_nxt <= (not rs1_is_r0_v) and csr_acc_valid; -- write CSR if rs1 is not zero_reg and if valid access
|
836 |
6 |
zero_gravi |
when funct3_csrrc_c => -- CSRRC
|
837 |
2 |
zero_gravi |
ctrl_nxt(ctrl_alu_opa_mux_msb_c) <= '1'; -- OPA = csr
|
838 |
|
|
ctrl_nxt(ctrl_alu_opb_mux_msb_c) <= '1'; -- OPB = rs1
|
839 |
23 |
zero_gravi |
ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_bitm_c; -- actual ALU operation = bit manipulation (ANDN)
|
840 |
15 |
zero_gravi |
csr.we_nxt <= (not rs1_is_r0_v) and csr_acc_valid; -- write CSR if rs1 is not zero_reg and if valid access
|
841 |
7 |
zero_gravi |
-- immediate operations --
|
842 |
6 |
zero_gravi |
when funct3_csrrwi_c => -- CSRRWI
|
843 |
2 |
zero_gravi |
ctrl_nxt(ctrl_alu_opa_mux_lsb_c) <= '0'; -- OPA = rs1
|
844 |
|
|
ctrl_nxt(ctrl_rf_clear_rs1_c) <= '1'; -- rs1 = 0
|
845 |
|
|
ctrl_nxt(ctrl_alu_opb_mux_lsb_c) <= '1'; -- OPB = immediate
|
846 |
12 |
zero_gravi |
ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_or_c; -- actual ALU operation = OR
|
847 |
15 |
zero_gravi |
csr.we_nxt <= csr_acc_valid; -- always write CSR if valid access
|
848 |
6 |
zero_gravi |
when funct3_csrrsi_c => -- CSRRSI
|
849 |
2 |
zero_gravi |
ctrl_nxt(ctrl_alu_opa_mux_msb_c) <= '1'; -- OPA = csr
|
850 |
|
|
ctrl_nxt(ctrl_alu_opb_mux_lsb_c) <= '1'; -- OPB = immediate
|
851 |
|
|
ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_or_c; -- actual ALU operation = OR
|
852 |
15 |
zero_gravi |
csr.we_nxt <= (not rs1_is_r0_v) and csr_acc_valid; -- write CSR if UIMM5 is not zero (bits from rs1 filed) and if valid access
|
853 |
6 |
zero_gravi |
when funct3_csrrci_c => -- CSRRCI
|
854 |
2 |
zero_gravi |
ctrl_nxt(ctrl_alu_opa_mux_msb_c) <= '1'; -- OPA = csr
|
855 |
|
|
ctrl_nxt(ctrl_alu_opb_mux_lsb_c) <= '1'; -- OPB = immediate
|
856 |
23 |
zero_gravi |
ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_bitm_c; -- actual ALU operation = bit manipulation (ANDN)
|
857 |
15 |
zero_gravi |
csr.we_nxt <= (not rs1_is_r0_v) and csr_acc_valid; -- write CSR if UIMM5 is not zero (bits from rs1 filed) and if valid access
|
858 |
2 |
zero_gravi |
when others => -- undefined
|
859 |
|
|
NULL;
|
860 |
|
|
end case;
|
861 |
|
|
-- RF write back --
|
862 |
12 |
zero_gravi |
ctrl_nxt(ctrl_rf_in_mux_msb_c downto ctrl_rf_in_mux_lsb_c) <= "11"; -- RF input = CSR output
|
863 |
2 |
zero_gravi |
ctrl_nxt(ctrl_rf_wb_en_c) <= '1'; -- valid RF write-back
|
864 |
11 |
zero_gravi |
execute_engine.state_nxt <= DISPATCH; -- FIXME should be SYS_WAIT? have another cycle to let side-effects kick in
|
865 |
2 |
zero_gravi |
|
866 |
19 |
zero_gravi |
when ALU_WAIT => -- wait for multi-cycle ALU operation (shifter or CP) to finish
|
867 |
2 |
zero_gravi |
-- ------------------------------------------------------------
|
868 |
19 |
zero_gravi |
ctrl_nxt(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) <= alu_cmd_shift_c;
|
869 |
6 |
zero_gravi |
ctrl_nxt(ctrl_rf_in_mux_msb_c downto ctrl_rf_in_mux_lsb_c) <= "00"; -- RF input = ALU result
|
870 |
12 |
zero_gravi |
ctrl_nxt(ctrl_rf_wb_en_c) <= '1'; -- valid RF write-back (permanent write-back)
|
871 |
19 |
zero_gravi |
-- cp access? --
|
872 |
|
|
if (CPU_EXTENSION_RISCV_M = true) and (execute_engine.i_reg(instr_funct7_lsb_c) = '1') then -- MULDIV?
|
873 |
|
|
ctrl_nxt(ctrl_cp_use_c) <= '1'; -- use CP
|
874 |
|
|
end if;
|
875 |
|
|
-- wait for result --
|
876 |
6 |
zero_gravi |
if (alu_wait_i = '0') then
|
877 |
12 |
zero_gravi |
execute_engine.state_nxt <= DISPATCH;
|
878 |
2 |
zero_gravi |
end if;
|
879 |
|
|
|
880 |
6 |
zero_gravi |
when BRANCH => -- update PC for taken branches and jumps
|
881 |
|
|
-- ------------------------------------------------------------
|
882 |
|
|
if (execute_engine.is_jump = '1') or (execute_engine.branch_taken = '1') then
|
883 |
20 |
zero_gravi |
execute_engine.pc_nxt <= alu_add_i; -- branch/jump destination
|
884 |
|
|
fetch_engine.reset <= '1'; -- trigger new instruction fetch from modified PC
|
885 |
|
|
execute_engine.if_rst_nxt <= '1'; -- this is a non-linear PC modification
|
886 |
|
|
execute_engine.state_nxt <= SYS_WAIT;
|
887 |
11 |
zero_gravi |
else
|
888 |
|
|
execute_engine.state_nxt <= DISPATCH;
|
889 |
6 |
zero_gravi |
end if;
|
890 |
|
|
|
891 |
12 |
zero_gravi |
when LOADSTORE_0 => -- trigger memory request
|
892 |
6 |
zero_gravi |
-- ------------------------------------------------------------
|
893 |
12 |
zero_gravi |
if (execute_engine.i_reg(instr_opcode_msb_c-1) = '0') then -- LOAD
|
894 |
|
|
ctrl_nxt(ctrl_bus_rd_c) <= '1'; -- read request
|
895 |
|
|
else -- STORE
|
896 |
|
|
ctrl_nxt(ctrl_bus_wr_c) <= '1'; -- write request
|
897 |
|
|
end if;
|
898 |
|
|
execute_engine.state_nxt <= LOADSTORE_1;
|
899 |
6 |
zero_gravi |
|
900 |
12 |
zero_gravi |
when LOADSTORE_1 => -- memory latency
|
901 |
6 |
zero_gravi |
-- ------------------------------------------------------------
|
902 |
|
|
ctrl_nxt(ctrl_bus_mdi_we_c) <= '1'; -- write input data to MDI (only relevant for LOAD)
|
903 |
12 |
zero_gravi |
execute_engine.state_nxt <= LOADSTORE_2;
|
904 |
6 |
zero_gravi |
|
905 |
12 |
zero_gravi |
when LOADSTORE_2 => -- wait for bus transaction to finish
|
906 |
6 |
zero_gravi |
-- ------------------------------------------------------------
|
907 |
|
|
ctrl_nxt(ctrl_bus_mdi_we_c) <= '1'; -- keep writing input data to MDI (only relevant for LOAD)
|
908 |
|
|
ctrl_nxt(ctrl_rf_in_mux_msb_c downto ctrl_rf_in_mux_lsb_c) <= "01"; -- RF input = memory input (only relevant for LOAD)
|
909 |
|
|
if (ma_load_i = '1') or (be_load_i = '1') or (ma_store_i = '1') or (be_store_i = '1') then -- abort if exception
|
910 |
7 |
zero_gravi |
execute_engine.state_nxt <= SYS_WAIT;
|
911 |
12 |
zero_gravi |
elsif (bus_d_wait_i = '0') then -- wait here for bus to finish transaction
|
912 |
23 |
zero_gravi |
if (execute_engine.i_reg(instr_opcode_msb_c-1) = '0') then -- LOAD
|
913 |
6 |
zero_gravi |
ctrl_nxt(ctrl_rf_wb_en_c) <= '1'; -- valid RF write-back
|
914 |
|
|
end if;
|
915 |
|
|
execute_engine.state_nxt <= DISPATCH;
|
916 |
|
|
end if;
|
917 |
|
|
|
918 |
2 |
zero_gravi |
when others => -- undefined
|
919 |
|
|
-- ------------------------------------------------------------
|
920 |
7 |
zero_gravi |
execute_engine.state_nxt <= SYS_WAIT;
|
921 |
2 |
zero_gravi |
|
922 |
|
|
end case;
|
923 |
6 |
zero_gravi |
end process execute_engine_fsm_comb;
|
924 |
2 |
zero_gravi |
|
925 |
|
|
|
926 |
15 |
zero_gravi |
-- ****************************************************************************************************************************
|
927 |
|
|
-- Invalid Instruction / CSR access check
|
928 |
|
|
-- ****************************************************************************************************************************
|
929 |
|
|
|
930 |
|
|
|
931 |
|
|
-- Illegal CSR Access Check ---------------------------------------------------------------
|
932 |
|
|
-- -------------------------------------------------------------------------------------------
|
933 |
|
|
invalid_csr_access_check: process(execute_engine, csr)
|
934 |
|
|
variable is_m_mode_v : std_ulogic;
|
935 |
|
|
begin
|
936 |
|
|
-- are we in machine mode? --
|
937 |
|
|
is_m_mode_v := '0';
|
938 |
|
|
if (csr.privilege = m_priv_mode_c) then
|
939 |
|
|
is_m_mode_v := '1';
|
940 |
|
|
end if;
|
941 |
|
|
|
942 |
|
|
-- check CSR access --
|
943 |
|
|
case execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) is
|
944 |
|
|
when x"300" => csr_acc_valid <= is_m_mode_v; -- mstatus
|
945 |
|
|
when x"301" => csr_acc_valid <= is_m_mode_v; -- misa
|
946 |
|
|
when x"304" => csr_acc_valid <= is_m_mode_v; -- mie
|
947 |
|
|
when x"305" => csr_acc_valid <= is_m_mode_v; -- mtvev
|
948 |
|
|
when x"340" => csr_acc_valid <= is_m_mode_v; -- mscratch
|
949 |
|
|
when x"341" => csr_acc_valid <= is_m_mode_v; -- mepc
|
950 |
|
|
when x"342" => csr_acc_valid <= is_m_mode_v; -- mcause
|
951 |
|
|
when x"343" => csr_acc_valid <= is_m_mode_v; -- mtval
|
952 |
|
|
when x"344" => csr_acc_valid <= is_m_mode_v; -- mip
|
953 |
|
|
--
|
954 |
23 |
zero_gravi |
when x"3a0" => csr_acc_valid <= bool_to_ulogic_f(PMP_USE) and bool_to_ulogic_f(boolean(PMP_NUM_REGIONS >= 1)) and is_m_mode_v; -- pmpacfg0
|
955 |
|
|
when x"3a1" => csr_acc_valid <= bool_to_ulogic_f(PMP_USE) and bool_to_ulogic_f(boolean(PMP_NUM_REGIONS >= 5)) and is_m_mode_v; -- pmpacfg1
|
956 |
15 |
zero_gravi |
--
|
957 |
23 |
zero_gravi |
when x"3b0" => csr_acc_valid <= bool_to_ulogic_f(PMP_USE) and bool_to_ulogic_f(boolean(PMP_NUM_REGIONS >= 1)) and is_m_mode_v; -- pmpaddr0
|
958 |
|
|
when x"3b1" => csr_acc_valid <= bool_to_ulogic_f(PMP_USE) and bool_to_ulogic_f(boolean(PMP_NUM_REGIONS >= 2)) and is_m_mode_v; -- pmpaddr1
|
959 |
|
|
when x"3b2" => csr_acc_valid <= bool_to_ulogic_f(PMP_USE) and bool_to_ulogic_f(boolean(PMP_NUM_REGIONS >= 3)) and is_m_mode_v; -- pmpaddr2
|
960 |
|
|
when x"3b3" => csr_acc_valid <= bool_to_ulogic_f(PMP_USE) and bool_to_ulogic_f(boolean(PMP_NUM_REGIONS >= 4)) and is_m_mode_v; -- pmpaddr3
|
961 |
|
|
when x"3b4" => csr_acc_valid <= bool_to_ulogic_f(PMP_USE) and bool_to_ulogic_f(boolean(PMP_NUM_REGIONS >= 5)) and is_m_mode_v; -- pmpaddr4
|
962 |
|
|
when x"3b5" => csr_acc_valid <= bool_to_ulogic_f(PMP_USE) and bool_to_ulogic_f(boolean(PMP_NUM_REGIONS >= 6)) and is_m_mode_v; -- pmpaddr5
|
963 |
|
|
when x"3b6" => csr_acc_valid <= bool_to_ulogic_f(PMP_USE) and bool_to_ulogic_f(boolean(PMP_NUM_REGIONS >= 7)) and is_m_mode_v; -- pmpaddr6
|
964 |
|
|
when x"3b7" => csr_acc_valid <= bool_to_ulogic_f(PMP_USE) and bool_to_ulogic_f(boolean(PMP_NUM_REGIONS >= 8)) and is_m_mode_v; -- pmpaddr7
|
965 |
15 |
zero_gravi |
--
|
966 |
23 |
zero_gravi |
when x"c00" => csr_acc_valid <= '1'; -- cycle
|
967 |
|
|
when x"c01" => csr_acc_valid <= '1'; -- time
|
968 |
|
|
when x"c02" => csr_acc_valid <= '1'; -- instret
|
969 |
|
|
when x"c80" => csr_acc_valid <= '1'; -- cycleh
|
970 |
|
|
when x"c81" => csr_acc_valid <= '1'; -- timeh
|
971 |
|
|
when x"c82" => csr_acc_valid <= '1'; -- instreth
|
972 |
15 |
zero_gravi |
--
|
973 |
23 |
zero_gravi |
when x"b00" => csr_acc_valid <= is_m_mode_v; -- mcycle
|
974 |
|
|
when x"b02" => csr_acc_valid <= is_m_mode_v; -- minstret
|
975 |
|
|
when x"b80" => csr_acc_valid <= is_m_mode_v; -- mcycleh
|
976 |
|
|
when x"b82" => csr_acc_valid <= is_m_mode_v; -- minstreth
|
977 |
15 |
zero_gravi |
--
|
978 |
|
|
when x"f11" => csr_acc_valid <= is_m_mode_v; -- mvendorid
|
979 |
|
|
when x"f12" => csr_acc_valid <= is_m_mode_v; -- marchid
|
980 |
|
|
when x"f13" => csr_acc_valid <= is_m_mode_v; -- mimpid
|
981 |
|
|
when x"f14" => csr_acc_valid <= is_m_mode_v; -- mhartid
|
982 |
|
|
--
|
983 |
22 |
zero_gravi |
when x"fc0" => csr_acc_valid <= is_m_mode_v; -- mzext (custom CSR)
|
984 |
|
|
--
|
985 |
23 |
zero_gravi |
when others => csr_acc_valid <= '0'; -- undefined, invalid access
|
986 |
15 |
zero_gravi |
end case;
|
987 |
|
|
end process invalid_csr_access_check;
|
988 |
|
|
|
989 |
|
|
|
990 |
2 |
zero_gravi |
-- Illegal Instruction Check --------------------------------------------------------------
|
991 |
|
|
-- -------------------------------------------------------------------------------------------
|
992 |
15 |
zero_gravi |
illegal_instruction_check: process(execute_engine, csr, ctrl_nxt, csr_acc_valid)
|
993 |
2 |
zero_gravi |
begin
|
994 |
11 |
zero_gravi |
-- illegal instructions are checked in the EXECUTE stage
|
995 |
|
|
-- the execute engine will only commit valid instructions
|
996 |
6 |
zero_gravi |
if (execute_engine.state = EXECUTE) then
|
997 |
2 |
zero_gravi |
-- defaults --
|
998 |
|
|
illegal_instruction <= '0';
|
999 |
|
|
illegal_register <= '0';
|
1000 |
|
|
|
1001 |
|
|
-- check instructions --
|
1002 |
6 |
zero_gravi |
case execute_engine.i_reg(instr_opcode_msb_c downto instr_opcode_lsb_c) is
|
1003 |
2 |
zero_gravi |
|
1004 |
|
|
-- OPCODE check sufficient: LUI, UIPC, JAL --
|
1005 |
|
|
when opcode_lui_c | opcode_auipc_c | opcode_jal_c =>
|
1006 |
|
|
illegal_instruction <= '0';
|
1007 |
23 |
zero_gravi |
-- illegal E-CPU register? --
|
1008 |
|
|
if (CPU_EXTENSION_RISCV_E = true) and (execute_engine.i_reg(instr_rd_msb_c) = '1') then
|
1009 |
|
|
illegal_register <= '1';
|
1010 |
|
|
end if;
|
1011 |
2 |
zero_gravi |
|
1012 |
|
|
when opcode_alui_c => -- check ALUI funct7
|
1013 |
6 |
zero_gravi |
if ((execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sll_c) and
|
1014 |
|
|
(execute_engine.i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c) /= "0000000")) or -- shift logical left
|
1015 |
|
|
((execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sr_c) and
|
1016 |
|
|
((execute_engine.i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c) /= "0000000") and
|
1017 |
|
|
(execute_engine.i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c) /= "0100000"))) then -- shift right
|
1018 |
2 |
zero_gravi |
illegal_instruction <= '1';
|
1019 |
|
|
else
|
1020 |
|
|
illegal_instruction <= '0';
|
1021 |
|
|
end if;
|
1022 |
23 |
zero_gravi |
-- illegal E-CPU register? --
|
1023 |
|
|
if (CPU_EXTENSION_RISCV_E = true) and ((execute_engine.i_reg(instr_rs1_msb_c) = '1') or (execute_engine.i_reg(instr_rd_msb_c) = '1')) then
|
1024 |
|
|
illegal_register <= '1';
|
1025 |
|
|
end if;
|
1026 |
2 |
zero_gravi |
|
1027 |
|
|
when opcode_load_c => -- check LOAD funct3
|
1028 |
6 |
zero_gravi |
if (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_lb_c) or
|
1029 |
|
|
(execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_lh_c) or
|
1030 |
|
|
(execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_lw_c) or
|
1031 |
|
|
(execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_lbu_c) or
|
1032 |
|
|
(execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_lhu_c) then
|
1033 |
2 |
zero_gravi |
illegal_instruction <= '0';
|
1034 |
|
|
else
|
1035 |
|
|
illegal_instruction <= '1';
|
1036 |
|
|
end if;
|
1037 |
23 |
zero_gravi |
-- illegal E-CPU register? --
|
1038 |
|
|
if (CPU_EXTENSION_RISCV_E = true) and ((execute_engine.i_reg(instr_rs1_msb_c) = '1') or (execute_engine.i_reg(instr_rd_msb_c) = '1')) then
|
1039 |
|
|
illegal_register <= '1';
|
1040 |
|
|
end if;
|
1041 |
2 |
zero_gravi |
|
1042 |
|
|
when opcode_store_c => -- check STORE funct3
|
1043 |
6 |
zero_gravi |
if (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sb_c) or
|
1044 |
|
|
(execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sh_c) or
|
1045 |
|
|
(execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sw_c) then
|
1046 |
2 |
zero_gravi |
illegal_instruction <= '0';
|
1047 |
|
|
else
|
1048 |
|
|
illegal_instruction <= '1';
|
1049 |
|
|
end if;
|
1050 |
23 |
zero_gravi |
-- illegal E-CPU register? --
|
1051 |
|
|
if (CPU_EXTENSION_RISCV_E = true) and ((execute_engine.i_reg(instr_rs2_msb_c) = '1') or (execute_engine.i_reg(instr_rs1_msb_c) = '1')) then
|
1052 |
|
|
illegal_register <= '1';
|
1053 |
|
|
end if;
|
1054 |
2 |
zero_gravi |
|
1055 |
|
|
when opcode_branch_c => -- check BRANCH funct3
|
1056 |
6 |
zero_gravi |
if (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_beq_c) or
|
1057 |
|
|
(execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_bne_c) or
|
1058 |
|
|
(execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_blt_c) or
|
1059 |
|
|
(execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_bge_c) or
|
1060 |
|
|
(execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_bltu_c) or
|
1061 |
|
|
(execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_bgeu_c) then
|
1062 |
2 |
zero_gravi |
illegal_instruction <= '0';
|
1063 |
|
|
else
|
1064 |
|
|
illegal_instruction <= '1';
|
1065 |
|
|
end if;
|
1066 |
23 |
zero_gravi |
-- illegal E-CPU register? --
|
1067 |
|
|
if (CPU_EXTENSION_RISCV_E = true) and ((execute_engine.i_reg(instr_rs2_msb_c) = '1') or (execute_engine.i_reg(instr_rs1_msb_c) = '1')) then
|
1068 |
|
|
illegal_register <= '1';
|
1069 |
|
|
end if;
|
1070 |
2 |
zero_gravi |
|
1071 |
|
|
when opcode_jalr_c => -- check JALR funct3
|
1072 |
6 |
zero_gravi |
if (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = "000") then
|
1073 |
2 |
zero_gravi |
illegal_instruction <= '0';
|
1074 |
|
|
else
|
1075 |
|
|
illegal_instruction <= '1';
|
1076 |
|
|
end if;
|
1077 |
23 |
zero_gravi |
-- illegal E-CPU register? --
|
1078 |
|
|
if (CPU_EXTENSION_RISCV_E = true) and ((execute_engine.i_reg(instr_rs1_msb_c) = '1') or (execute_engine.i_reg(instr_rd_msb_c) = '1')) then
|
1079 |
|
|
illegal_register <= '1';
|
1080 |
|
|
end if;
|
1081 |
2 |
zero_gravi |
|
1082 |
|
|
when opcode_alu_c => -- check ALU funct3 & funct7
|
1083 |
6 |
zero_gravi |
if (execute_engine.i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c) = "0000001") then -- MULDIV
|
1084 |
11 |
zero_gravi |
if (CPU_EXTENSION_RISCV_M = false) then -- not implemented
|
1085 |
2 |
zero_gravi |
illegal_instruction <= '1';
|
1086 |
|
|
end if;
|
1087 |
6 |
zero_gravi |
elsif ((execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_subadd_c) or
|
1088 |
|
|
(execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_sr_c)) and -- ADD/SUB or SRA/SRL check
|
1089 |
|
|
((execute_engine.i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c) /= "0000000") and
|
1090 |
|
|
(execute_engine.i_reg(instr_funct7_msb_c downto instr_funct7_lsb_c) /= "0100000")) then -- ADD/SUB or SRA/SRL select
|
1091 |
2 |
zero_gravi |
illegal_instruction <= '1';
|
1092 |
|
|
else
|
1093 |
|
|
illegal_instruction <= '0';
|
1094 |
|
|
end if;
|
1095 |
23 |
zero_gravi |
-- illegal E-CPU register? --
|
1096 |
|
|
if (CPU_EXTENSION_RISCV_E = true) and
|
1097 |
|
|
((execute_engine.i_reg(instr_rs2_msb_c) = '1') or (execute_engine.i_reg(instr_rs1_msb_c) = '1') or (execute_engine.i_reg(instr_rd_msb_c) = '1')) then
|
1098 |
|
|
illegal_register <= '1';
|
1099 |
|
|
end if;
|
1100 |
2 |
zero_gravi |
|
1101 |
8 |
zero_gravi |
when opcode_fence_c => -- fence instructions --
|
1102 |
|
|
if (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_fencei_c) and (CPU_EXTENSION_RISCV_Zifencei = true) then -- FENCE.I
|
1103 |
|
|
illegal_instruction <= '0';
|
1104 |
|
|
elsif (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_fence_c) then -- FENCE
|
1105 |
|
|
illegal_instruction <= '0';
|
1106 |
|
|
else
|
1107 |
|
|
illegal_instruction <= '1';
|
1108 |
|
|
end if;
|
1109 |
|
|
|
1110 |
2 |
zero_gravi |
when opcode_syscsr_c => -- check system instructions --
|
1111 |
|
|
-- CSR access --
|
1112 |
6 |
zero_gravi |
if (execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrw_c) or
|
1113 |
|
|
(execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrs_c) or
|
1114 |
|
|
(execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrc_c) or
|
1115 |
|
|
(execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrwi_c) or
|
1116 |
|
|
(execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrsi_c) or
|
1117 |
|
|
(execute_engine.i_reg(instr_funct3_msb_c downto instr_funct3_lsb_c) = funct3_csrrci_c) then
|
1118 |
15 |
zero_gravi |
-- valid CSR access? --
|
1119 |
|
|
if (csr_acc_valid = '1') then
|
1120 |
2 |
zero_gravi |
illegal_instruction <= '0';
|
1121 |
|
|
else
|
1122 |
|
|
illegal_instruction <= '1';
|
1123 |
|
|
end if;
|
1124 |
23 |
zero_gravi |
-- illegal E-CPU register? --
|
1125 |
|
|
if (CPU_EXTENSION_RISCV_E = true) then
|
1126 |
|
|
if (execute_engine.i_reg(instr_funct3_msb_c) = '0') then -- reg-reg CSR
|
1127 |
|
|
illegal_register <= execute_engine.i_reg(instr_rs1_msb_c) or execute_engine.i_reg(instr_rd_msb_c);
|
1128 |
|
|
else -- reg-imm CSR
|
1129 |
|
|
illegal_register <= execute_engine.i_reg(instr_rd_msb_c);
|
1130 |
|
|
end if;
|
1131 |
|
|
end if;
|
1132 |
2 |
zero_gravi |
|
1133 |
|
|
-- ecall, ebreak, mret, wfi --
|
1134 |
6 |
zero_gravi |
elsif (execute_engine.i_reg(instr_rd_msb_c downto instr_rd_lsb_c) = "00000") and
|
1135 |
|
|
(execute_engine.i_reg(instr_rs1_msb_c downto instr_rs1_lsb_c) = "00000") then
|
1136 |
13 |
zero_gravi |
if (execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = funct12_ecall_c) or -- ECALL
|
1137 |
11 |
zero_gravi |
(execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = funct12_ebreak_c) or -- EBREAK
|
1138 |
13 |
zero_gravi |
(execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = funct12_mret_c) or -- MRET
|
1139 |
|
|
(execute_engine.i_reg(instr_funct12_msb_c downto instr_funct12_lsb_c) = funct12_wfi_c) then -- WFI
|
1140 |
2 |
zero_gravi |
illegal_instruction <= '0';
|
1141 |
|
|
else
|
1142 |
|
|
illegal_instruction <= '1';
|
1143 |
|
|
end if;
|
1144 |
|
|
else
|
1145 |
|
|
illegal_instruction <= '1';
|
1146 |
|
|
end if;
|
1147 |
|
|
|
1148 |
|
|
when others => -- compressed instruction or undefined instruction
|
1149 |
6 |
zero_gravi |
if (execute_engine.i_reg(1 downto 0) = "11") then -- undefined/unimplemented opcode
|
1150 |
2 |
zero_gravi |
illegal_instruction <= '1';
|
1151 |
|
|
end if;
|
1152 |
|
|
|
1153 |
|
|
end case;
|
1154 |
|
|
else
|
1155 |
|
|
illegal_instruction <= '0';
|
1156 |
|
|
illegal_register <= '0';
|
1157 |
|
|
end if;
|
1158 |
|
|
end process illegal_instruction_check;
|
1159 |
|
|
|
1160 |
|
|
-- any illegal condition? --
|
1161 |
6 |
zero_gravi |
trap_ctrl.instr_il <= illegal_instruction or illegal_register or illegal_compressed;
|
1162 |
2 |
zero_gravi |
|
1163 |
|
|
|
1164 |
6 |
zero_gravi |
-- ****************************************************************************************************************************
|
1165 |
|
|
-- Exception and Interrupt Control
|
1166 |
|
|
-- ****************************************************************************************************************************
|
1167 |
2 |
zero_gravi |
|
1168 |
|
|
|
1169 |
6 |
zero_gravi |
-- Trap Controller ------------------------------------------------------------------------
|
1170 |
2 |
zero_gravi |
-- -------------------------------------------------------------------------------------------
|
1171 |
6 |
zero_gravi |
trap_controller: process(rstn_i, clk_i)
|
1172 |
2 |
zero_gravi |
begin
|
1173 |
|
|
if (rstn_i = '0') then
|
1174 |
6 |
zero_gravi |
trap_ctrl.exc_buf <= (others => '0');
|
1175 |
|
|
trap_ctrl.irq_buf <= (others => '0');
|
1176 |
|
|
trap_ctrl.exc_ack <= '0';
|
1177 |
|
|
trap_ctrl.irq_ack <= (others => '0');
|
1178 |
|
|
trap_ctrl.cause <= (others => '0');
|
1179 |
|
|
trap_ctrl.env_start <= '0';
|
1180 |
2 |
zero_gravi |
elsif rising_edge(clk_i) then
|
1181 |
|
|
if (CPU_EXTENSION_RISCV_Zicsr = true) then
|
1182 |
|
|
-- exception buffer: misaligned load/store/instruction address
|
1183 |
6 |
zero_gravi |
trap_ctrl.exc_buf(exception_lalign_c) <= (trap_ctrl.exc_buf(exception_lalign_c) or ma_load_i) and (not trap_ctrl.exc_ack);
|
1184 |
|
|
trap_ctrl.exc_buf(exception_salign_c) <= (trap_ctrl.exc_buf(exception_salign_c) or ma_store_i) and (not trap_ctrl.exc_ack);
|
1185 |
|
|
trap_ctrl.exc_buf(exception_ialign_c) <= (trap_ctrl.exc_buf(exception_ialign_c) or trap_ctrl.instr_ma) and (not trap_ctrl.exc_ack);
|
1186 |
2 |
zero_gravi |
-- exception buffer: load/store/instruction bus access error
|
1187 |
6 |
zero_gravi |
trap_ctrl.exc_buf(exception_laccess_c) <= (trap_ctrl.exc_buf(exception_laccess_c) or be_load_i) and (not trap_ctrl.exc_ack);
|
1188 |
|
|
trap_ctrl.exc_buf(exception_saccess_c) <= (trap_ctrl.exc_buf(exception_saccess_c) or be_store_i) and (not trap_ctrl.exc_ack);
|
1189 |
|
|
trap_ctrl.exc_buf(exception_iaccess_c) <= (trap_ctrl.exc_buf(exception_iaccess_c) or trap_ctrl.instr_be) and (not trap_ctrl.exc_ack);
|
1190 |
2 |
zero_gravi |
-- exception buffer: illegal instruction / env call / break point
|
1191 |
6 |
zero_gravi |
trap_ctrl.exc_buf(exception_m_envcall_c) <= (trap_ctrl.exc_buf(exception_m_envcall_c) or trap_ctrl.env_call) and (not trap_ctrl.exc_ack);
|
1192 |
|
|
trap_ctrl.exc_buf(exception_break_c) <= (trap_ctrl.exc_buf(exception_break_c) or trap_ctrl.break_point) and (not trap_ctrl.exc_ack);
|
1193 |
|
|
trap_ctrl.exc_buf(exception_iillegal_c) <= (trap_ctrl.exc_buf(exception_iillegal_c) or trap_ctrl.instr_il) and (not trap_ctrl.exc_ack);
|
1194 |
18 |
zero_gravi |
-- interrupt buffer: machine software/external/timer interrupt
|
1195 |
14 |
zero_gravi |
trap_ctrl.irq_buf(interrupt_msw_irq_c) <= csr.mie_msie and (trap_ctrl.irq_buf(interrupt_msw_irq_c) or msw_irq_i) and (not trap_ctrl.irq_ack(interrupt_msw_irq_c));
|
1196 |
|
|
trap_ctrl.irq_buf(interrupt_mext_irq_c) <= csr.mie_meie and (trap_ctrl.irq_buf(interrupt_mext_irq_c) or mext_irq_i) and (not trap_ctrl.irq_ack(interrupt_mext_irq_c));
|
1197 |
|
|
trap_ctrl.irq_buf(interrupt_mtime_irq_c) <= csr.mie_mtie and (trap_ctrl.irq_buf(interrupt_mtime_irq_c) or mtime_irq_i) and (not trap_ctrl.irq_ack(interrupt_mtime_irq_c));
|
1198 |
18 |
zero_gravi |
-- interrupt buffer: custom fast interrupts
|
1199 |
14 |
zero_gravi |
trap_ctrl.irq_buf(interrupt_firq_0_c) <= csr.mie_firqe(0) and (trap_ctrl.irq_buf(interrupt_firq_0_c) or firq_i(0)) and (not trap_ctrl.irq_ack(interrupt_firq_0_c));
|
1200 |
|
|
trap_ctrl.irq_buf(interrupt_firq_1_c) <= csr.mie_firqe(1) and (trap_ctrl.irq_buf(interrupt_firq_1_c) or firq_i(1)) and (not trap_ctrl.irq_ack(interrupt_firq_1_c));
|
1201 |
|
|
trap_ctrl.irq_buf(interrupt_firq_2_c) <= csr.mie_firqe(2) and (trap_ctrl.irq_buf(interrupt_firq_2_c) or firq_i(2)) and (not trap_ctrl.irq_ack(interrupt_firq_2_c));
|
1202 |
|
|
trap_ctrl.irq_buf(interrupt_firq_3_c) <= csr.mie_firqe(3) and (trap_ctrl.irq_buf(interrupt_firq_3_c) or firq_i(3)) and (not trap_ctrl.irq_ack(interrupt_firq_3_c));
|
1203 |
2 |
zero_gravi |
|
1204 |
6 |
zero_gravi |
-- trap control --
|
1205 |
|
|
if (trap_ctrl.env_start = '0') then -- no started trap handler
|
1206 |
11 |
zero_gravi |
if (trap_ctrl.exc_fire = '1') or ((trap_ctrl.irq_fire = '1') and -- exception/IRQ detected!
|
1207 |
13 |
zero_gravi |
((execute_engine.state = EXECUTE) or (execute_engine.state = TRAP))) then -- sample IRQs in EXECUTE or TRAP state only -> continue execution even if permanent IRQ
|
1208 |
|
|
trap_ctrl.cause <= trap_ctrl.cause_nxt; -- capture source ID for program (for mcause csr)
|
1209 |
7 |
zero_gravi |
trap_ctrl.exc_ack <= '1'; -- clear execption
|
1210 |
|
|
trap_ctrl.irq_ack <= trap_ctrl.irq_ack_nxt; -- capture and clear with interrupt ACK mask
|
1211 |
13 |
zero_gravi |
trap_ctrl.env_start <= '1'; -- now execute engine can start trap handler
|
1212 |
23 |
zero_gravi |
-- assert false report "NEORV32.CPU TRAP: mcause=" & integer'image(to_integer(unsigned(trap_ctrl.cause_nxt))) severity note; -- for debugging
|
1213 |
2 |
zero_gravi |
end if;
|
1214 |
6 |
zero_gravi |
else -- trap waiting to get started
|
1215 |
|
|
if (trap_ctrl.env_start_ack = '1') then -- start of trap handler acknowledged by execution engine
|
1216 |
|
|
trap_ctrl.exc_ack <= '0';
|
1217 |
|
|
trap_ctrl.irq_ack <= (others => '0');
|
1218 |
|
|
trap_ctrl.env_start <= '0';
|
1219 |
2 |
zero_gravi |
end if;
|
1220 |
|
|
end if;
|
1221 |
|
|
end if;
|
1222 |
|
|
end if;
|
1223 |
6 |
zero_gravi |
end process trap_controller;
|
1224 |
2 |
zero_gravi |
|
1225 |
|
|
-- any exception/interrupt? --
|
1226 |
13 |
zero_gravi |
trap_ctrl.exc_fire <= or_all_f(trap_ctrl.exc_buf); -- exceptions/faults cannot be masked
|
1227 |
|
|
trap_ctrl.irq_fire <= or_all_f(trap_ctrl.irq_buf) and csr.mstatus_mie; -- interrupts can be masked
|
1228 |
2 |
zero_gravi |
|
1229 |
|
|
|
1230 |
6 |
zero_gravi |
-- Trap Priority Detector -----------------------------------------------------------------
|
1231 |
|
|
-- -------------------------------------------------------------------------------------------
|
1232 |
|
|
trap_priority: process(trap_ctrl)
|
1233 |
2 |
zero_gravi |
begin
|
1234 |
|
|
-- defaults --
|
1235 |
6 |
zero_gravi |
trap_ctrl.cause_nxt <= (others => '0');
|
1236 |
|
|
trap_ctrl.irq_ack_nxt <= (others => '0');
|
1237 |
2 |
zero_gravi |
|
1238 |
9 |
zero_gravi |
-- the following traps are caused by asynchronous exceptions (-> interrupts)
|
1239 |
12 |
zero_gravi |
-- here we do need a specific acknowledge mask since several sources can trigger at once
|
1240 |
9 |
zero_gravi |
|
1241 |
2 |
zero_gravi |
-- interrupt: 1.11 machine external interrupt --
|
1242 |
6 |
zero_gravi |
if (trap_ctrl.irq_buf(interrupt_mext_irq_c) = '1') then
|
1243 |
12 |
zero_gravi |
trap_ctrl.cause_nxt <= trap_mei_c;
|
1244 |
6 |
zero_gravi |
trap_ctrl.irq_ack_nxt(interrupt_mext_irq_c) <= '1';
|
1245 |
2 |
zero_gravi |
|
1246 |
|
|
-- interrupt: 1.7 machine timer interrupt --
|
1247 |
6 |
zero_gravi |
elsif (trap_ctrl.irq_buf(interrupt_mtime_irq_c) = '1') then
|
1248 |
12 |
zero_gravi |
trap_ctrl.cause_nxt <= trap_mti_c;
|
1249 |
6 |
zero_gravi |
trap_ctrl.irq_ack_nxt(interrupt_mtime_irq_c) <= '1';
|
1250 |
2 |
zero_gravi |
|
1251 |
|
|
-- interrupt: 1.3 machine SW interrupt --
|
1252 |
6 |
zero_gravi |
elsif (trap_ctrl.irq_buf(interrupt_msw_irq_c) = '1') then
|
1253 |
12 |
zero_gravi |
trap_ctrl.cause_nxt <= trap_msi_c;
|
1254 |
6 |
zero_gravi |
trap_ctrl.irq_ack_nxt(interrupt_msw_irq_c) <= '1';
|
1255 |
2 |
zero_gravi |
|
1256 |
|
|
|
1257 |
14 |
zero_gravi |
-- interrupt: 1.16 fast interrupt channel 0 --
|
1258 |
|
|
elsif (trap_ctrl.irq_buf(interrupt_firq_0_c) = '1') then
|
1259 |
|
|
trap_ctrl.cause_nxt <= trap_firq0_c;
|
1260 |
|
|
trap_ctrl.irq_ack_nxt(interrupt_firq_0_c) <= '1';
|
1261 |
|
|
|
1262 |
|
|
-- interrupt: 1.17 fast interrupt channel 1 --
|
1263 |
|
|
elsif (trap_ctrl.irq_buf(interrupt_firq_1_c) = '1') then
|
1264 |
|
|
trap_ctrl.cause_nxt <= trap_firq1_c;
|
1265 |
|
|
trap_ctrl.irq_ack_nxt(interrupt_firq_1_c) <= '1';
|
1266 |
|
|
|
1267 |
|
|
-- interrupt: 1.18 fast interrupt channel 2 --
|
1268 |
|
|
elsif (trap_ctrl.irq_buf(interrupt_firq_2_c) = '1') then
|
1269 |
|
|
trap_ctrl.cause_nxt <= trap_firq2_c;
|
1270 |
|
|
trap_ctrl.irq_ack_nxt(interrupt_firq_2_c) <= '1';
|
1271 |
|
|
|
1272 |
|
|
-- interrupt: 1.19 fast interrupt channel 3 --
|
1273 |
|
|
elsif (trap_ctrl.irq_buf(interrupt_firq_3_c) = '1') then
|
1274 |
|
|
trap_ctrl.cause_nxt <= trap_firq3_c;
|
1275 |
|
|
trap_ctrl.irq_ack_nxt(interrupt_firq_3_c) <= '1';
|
1276 |
|
|
|
1277 |
|
|
|
1278 |
4 |
zero_gravi |
-- the following traps are caused by synchronous exceptions
|
1279 |
12 |
zero_gravi |
-- here we do not need a specific acknowledge mask since only one exception (the one
|
1280 |
9 |
zero_gravi |
-- with highest priority) can trigger at once
|
1281 |
4 |
zero_gravi |
|
1282 |
2 |
zero_gravi |
-- trap/fault: 0.1 instruction access fault --
|
1283 |
6 |
zero_gravi |
elsif (trap_ctrl.exc_buf(exception_iaccess_c) = '1') then
|
1284 |
12 |
zero_gravi |
trap_ctrl.cause_nxt <= trap_iba_c;
|
1285 |
2 |
zero_gravi |
|
1286 |
|
|
-- trap/fault: 0.2 illegal instruction --
|
1287 |
6 |
zero_gravi |
elsif (trap_ctrl.exc_buf(exception_iillegal_c) = '1') then
|
1288 |
12 |
zero_gravi |
trap_ctrl.cause_nxt <= trap_iil_c;
|
1289 |
2 |
zero_gravi |
|
1290 |
12 |
zero_gravi |
-- trap/fault: 0.0 instruction address misaligned --
|
1291 |
|
|
elsif (trap_ctrl.exc_buf(exception_ialign_c) = '1') then
|
1292 |
|
|
trap_ctrl.cause_nxt <= trap_ima_c;
|
1293 |
2 |
zero_gravi |
|
1294 |
12 |
zero_gravi |
|
1295 |
2 |
zero_gravi |
-- trap/fault: 0.11 environment call from M-mode --
|
1296 |
6 |
zero_gravi |
elsif (trap_ctrl.exc_buf(exception_m_envcall_c) = '1') then
|
1297 |
14 |
zero_gravi |
trap_ctrl.cause_nxt <= trap_menv_c;
|
1298 |
2 |
zero_gravi |
|
1299 |
|
|
-- trap/fault: 0.3 breakpoint --
|
1300 |
6 |
zero_gravi |
elsif (trap_ctrl.exc_buf(exception_break_c) = '1') then
|
1301 |
12 |
zero_gravi |
trap_ctrl.cause_nxt <= trap_brk_c;
|
1302 |
2 |
zero_gravi |
|
1303 |
|
|
|
1304 |
|
|
-- trap/fault: 0.6 store address misaligned -
|
1305 |
6 |
zero_gravi |
elsif (trap_ctrl.exc_buf(exception_salign_c) = '1') then
|
1306 |
12 |
zero_gravi |
trap_ctrl.cause_nxt <= trap_sma_c;
|
1307 |
2 |
zero_gravi |
|
1308 |
|
|
-- trap/fault: 0.4 load address misaligned --
|
1309 |
6 |
zero_gravi |
elsif (trap_ctrl.exc_buf(exception_lalign_c) = '1') then
|
1310 |
12 |
zero_gravi |
trap_ctrl.cause_nxt <= trap_lma_c;
|
1311 |
2 |
zero_gravi |
|
1312 |
|
|
-- trap/fault: 0.7 store access fault --
|
1313 |
6 |
zero_gravi |
elsif (trap_ctrl.exc_buf(exception_saccess_c) = '1') then
|
1314 |
12 |
zero_gravi |
trap_ctrl.cause_nxt <= trap_sbe_c;
|
1315 |
2 |
zero_gravi |
|
1316 |
|
|
-- trap/fault: 0.5 load access fault --
|
1317 |
6 |
zero_gravi |
elsif (trap_ctrl.exc_buf(exception_laccess_c) = '1') then
|
1318 |
12 |
zero_gravi |
trap_ctrl.cause_nxt <= trap_lbe_c;
|
1319 |
2 |
zero_gravi |
|
1320 |
|
|
-- undefined / not implemented --
|
1321 |
|
|
else
|
1322 |
6 |
zero_gravi |
trap_ctrl.cause_nxt <= (others => '0');
|
1323 |
|
|
trap_ctrl.irq_ack_nxt <= (others => '0');
|
1324 |
2 |
zero_gravi |
end if;
|
1325 |
6 |
zero_gravi |
end process trap_priority;
|
1326 |
|
|
|
1327 |
2 |
zero_gravi |
|
1328 |
6 |
zero_gravi |
-- ****************************************************************************************************************************
|
1329 |
|
|
-- Control and Status Registers (CSRs)
|
1330 |
|
|
-- ****************************************************************************************************************************
|
1331 |
2 |
zero_gravi |
|
1332 |
|
|
-- Control and Status Registers Write Access ----------------------------------------------
|
1333 |
|
|
-- -------------------------------------------------------------------------------------------
|
1334 |
|
|
csr_write_access: process(rstn_i, clk_i)
|
1335 |
|
|
begin
|
1336 |
|
|
if (rstn_i = '0') then
|
1337 |
11 |
zero_gravi |
csr.we <= '0';
|
1338 |
|
|
csr.re <= '0';
|
1339 |
|
|
--
|
1340 |
6 |
zero_gravi |
csr.mstatus_mie <= '0';
|
1341 |
|
|
csr.mstatus_mpie <= '0';
|
1342 |
|
|
csr.mie_msie <= '0';
|
1343 |
|
|
csr.mie_meie <= '0';
|
1344 |
|
|
csr.mie_mtie <= '0';
|
1345 |
14 |
zero_gravi |
csr.mie_firqe <= (others => '0');
|
1346 |
6 |
zero_gravi |
csr.mtvec <= (others => '0');
|
1347 |
12 |
zero_gravi |
csr.mscratch <= (others => '0');
|
1348 |
|
|
csr.mepc <= (others => '0');
|
1349 |
|
|
csr.mcause <= (others => '0');
|
1350 |
6 |
zero_gravi |
csr.mtval <= (others => '0');
|
1351 |
15 |
zero_gravi |
csr.mpp <= m_priv_mode_c; -- start in MACHINE mode
|
1352 |
|
|
csr.privilege <= m_priv_mode_c; -- start in MACHINE mode
|
1353 |
|
|
csr.pmpcfg <= (others => (others => '0'));
|
1354 |
|
|
csr.pmpaddr <= (others => (others => '0'));
|
1355 |
2 |
zero_gravi |
elsif rising_edge(clk_i) then
|
1356 |
|
|
if (CPU_EXTENSION_RISCV_Zicsr = true) then
|
1357 |
11 |
zero_gravi |
-- access --
|
1358 |
|
|
csr.we <= csr.we_nxt;
|
1359 |
|
|
csr.re <= csr.re_nxt;
|
1360 |
|
|
|
1361 |
|
|
-- registers that can be modified by user --
|
1362 |
6 |
zero_gravi |
if (csr.we = '1') then -- manual update
|
1363 |
4 |
zero_gravi |
|
1364 |
15 |
zero_gravi |
-- Machine CSRs --
|
1365 |
11 |
zero_gravi |
if (execute_engine.i_reg(31 downto 28) = x"3") then
|
1366 |
|
|
-- machine trap setup --
|
1367 |
|
|
if (execute_engine.i_reg(27 downto 24) = x"0") then
|
1368 |
|
|
case execute_engine.i_reg(23 downto 20) is
|
1369 |
12 |
zero_gravi |
when x"0" => -- R/W: mstatus - machine status register
|
1370 |
|
|
csr.mstatus_mie <= csr_wdata_i(03);
|
1371 |
|
|
csr.mstatus_mpie <= csr_wdata_i(07);
|
1372 |
15 |
zero_gravi |
--
|
1373 |
|
|
if (CPU_EXTENSION_RISCV_U = true) then -- user mode implemented
|
1374 |
|
|
csr.mpp(0) <= csr_wdata_i(11) and csr_wdata_i(12);
|
1375 |
|
|
csr.mpp(1) <= csr_wdata_i(11) and csr_wdata_i(12);
|
1376 |
|
|
end if;
|
1377 |
12 |
zero_gravi |
when x"4" => -- R/W: mie - machine interrupt-enable register
|
1378 |
14 |
zero_gravi |
csr.mie_msie <= csr_wdata_i(03); -- machine SW IRQ enable
|
1379 |
|
|
csr.mie_mtie <= csr_wdata_i(07); -- machine TIMER IRQ enable
|
1380 |
|
|
csr.mie_meie <= csr_wdata_i(11); -- machine EXT IRQ enable
|
1381 |
|
|
--
|
1382 |
|
|
csr.mie_firqe(0) <= csr_wdata_i(16); -- fast interrupt channel 0
|
1383 |
|
|
csr.mie_firqe(1) <= csr_wdata_i(17); -- fast interrupt channel 1
|
1384 |
|
|
csr.mie_firqe(2) <= csr_wdata_i(18); -- fast interrupt channel 2
|
1385 |
|
|
csr.mie_firqe(3) <= csr_wdata_i(19); -- fast interrupt channel 3
|
1386 |
12 |
zero_gravi |
when x"5" => -- R/W: mtvec - machine trap-handler base address (for ALL exceptions)
|
1387 |
|
|
csr.mtvec <= csr_wdata_i(data_width_c-1 downto 2) & "00"; -- mtvec.MODE=0
|
1388 |
|
|
when others =>
|
1389 |
|
|
NULL;
|
1390 |
11 |
zero_gravi |
end case;
|
1391 |
4 |
zero_gravi |
end if;
|
1392 |
11 |
zero_gravi |
-- machine trap handling --
|
1393 |
|
|
if (execute_engine.i_reg(27 downto 24) = x"4") then
|
1394 |
|
|
case execute_engine.i_reg(23 downto 20) is
|
1395 |
|
|
when x"0" => -- R/W: mscratch - machine scratch register
|
1396 |
|
|
csr.mscratch <= csr_wdata_i;
|
1397 |
|
|
when x"1" => -- R/W: mepc - machine exception program counter
|
1398 |
12 |
zero_gravi |
csr.mepc <= csr_wdata_i(data_width_c-1 downto 1) & '0';
|
1399 |
11 |
zero_gravi |
when x"3" => -- R/W: mtval - machine bad address or instruction
|
1400 |
|
|
csr.mtval <= csr_wdata_i;
|
1401 |
|
|
when others =>
|
1402 |
|
|
NULL;
|
1403 |
|
|
end case;
|
1404 |
4 |
zero_gravi |
end if;
|
1405 |
15 |
zero_gravi |
-- machine physical memory protection (pmp) --
|
1406 |
|
|
if (PMP_USE = true) then
|
1407 |
|
|
-- pmpcfg --
|
1408 |
|
|
if (execute_engine.i_reg(27 downto 24) = x"a") then
|
1409 |
|
|
if (PMP_NUM_REGIONS >= 1) then
|
1410 |
|
|
if (execute_engine.i_reg(23 downto 20) = x"0") then -- pmpcfg0
|
1411 |
|
|
for j in 0 to 3 loop -- bytes in pmpcfg CSR
|
1412 |
|
|
if ((j+1) <= PMP_NUM_REGIONS) then
|
1413 |
|
|
if (csr.pmpcfg(0+j)(7) = '0') then -- unlocked pmpcfg access
|
1414 |
|
|
csr.pmpcfg(0+j)(0) <= csr_wdata_i(j*8+0); -- R
|
1415 |
|
|
csr.pmpcfg(0+j)(1) <= csr_wdata_i(j*8+1); -- W
|
1416 |
|
|
csr.pmpcfg(0+j)(2) <= csr_wdata_i(j*8+2); -- X
|
1417 |
|
|
csr.pmpcfg(0+j)(3) <= csr_wdata_i(j*8+3) and csr_wdata_i(j*8+4); -- A_L
|
1418 |
|
|
csr.pmpcfg(0+j)(4) <= csr_wdata_i(j*8+3) and csr_wdata_i(j*8+4); -- A_H - NAPOT/OFF only
|
1419 |
|
|
csr.pmpcfg(0+j)(5) <= '0'; -- reserved
|
1420 |
|
|
csr.pmpcfg(0+j)(6) <= '0'; -- reserved
|
1421 |
|
|
csr.pmpcfg(0+j)(7) <= csr_wdata_i(j*8+7); -- L
|
1422 |
|
|
end if;
|
1423 |
|
|
end if;
|
1424 |
|
|
end loop; -- j (bytes in CSR)
|
1425 |
|
|
end if;
|
1426 |
|
|
end if;
|
1427 |
|
|
if (PMP_NUM_REGIONS >= 5) then
|
1428 |
|
|
if (execute_engine.i_reg(23 downto 20) = x"1") then -- pmpcfg1
|
1429 |
|
|
for j in 0 to 3 loop -- bytes in pmpcfg CSR
|
1430 |
|
|
if ((j+1+4) <= PMP_NUM_REGIONS) then
|
1431 |
|
|
if (csr.pmpcfg(4+j)(7) = '0') then -- unlocked pmpcfg access
|
1432 |
|
|
csr.pmpcfg(4+j)(0) <= csr_wdata_i(j*8+0); -- R
|
1433 |
|
|
csr.pmpcfg(4+j)(1) <= csr_wdata_i(j*8+1); -- W
|
1434 |
|
|
csr.pmpcfg(4+j)(2) <= csr_wdata_i(j*8+2); -- X
|
1435 |
|
|
csr.pmpcfg(4+j)(3) <= csr_wdata_i(j*8+3) and csr_wdata_i(j*8+4); -- A_L
|
1436 |
|
|
csr.pmpcfg(4+j)(4) <= csr_wdata_i(j*8+3) and csr_wdata_i(j*8+4); -- A_H - NAPOT/OFF only
|
1437 |
|
|
csr.pmpcfg(4+j)(5) <= '0'; -- reserved
|
1438 |
|
|
csr.pmpcfg(4+j)(6) <= '0'; -- reserved
|
1439 |
|
|
csr.pmpcfg(4+j)(7) <= csr_wdata_i(j*8+7); -- L
|
1440 |
|
|
end if;
|
1441 |
|
|
end if;
|
1442 |
|
|
end loop; -- j (bytes in CSR)
|
1443 |
|
|
end if;
|
1444 |
|
|
end if;
|
1445 |
|
|
end if;
|
1446 |
|
|
-- pmpaddr --
|
1447 |
|
|
if (execute_engine.i_reg(27 downto 24) = x"b") then
|
1448 |
|
|
for i in 0 to PMP_NUM_REGIONS-1 loop
|
1449 |
|
|
if (execute_engine.i_reg(23 downto 20) = std_ulogic_vector(to_unsigned(i, 4))) and (csr.pmpcfg(i)(7) = '0') then -- unlocked pmpaddr access
|
1450 |
20 |
zero_gravi |
csr.pmpaddr(i) <= csr_wdata_i(31 downto 1) & '0'; -- min granularity is 8 bytes -> bit zero cannot be configured
|
1451 |
15 |
zero_gravi |
end if;
|
1452 |
|
|
end loop; -- i (CSRs)
|
1453 |
|
|
end if;
|
1454 |
|
|
end if; -- implement PMP at all?
|
1455 |
4 |
zero_gravi |
end if;
|
1456 |
|
|
|
1457 |
11 |
zero_gravi |
-- automatic update by hardware --
|
1458 |
|
|
else
|
1459 |
2 |
zero_gravi |
|
1460 |
14 |
zero_gravi |
-- machine exception PC & machine trap value register --
|
1461 |
12 |
zero_gravi |
if (trap_ctrl.env_start_ack = '1') then -- trap handler starting?
|
1462 |
14 |
zero_gravi |
csr.mcause <= trap_ctrl.cause(trap_ctrl.cause'left) & "000" & x"00000" & "000" & trap_ctrl.cause(4 downto 0);
|
1463 |
|
|
if (trap_ctrl.cause(trap_ctrl.cause'left) = '1') then -- for INTERRUPTS only (is mcause(31))
|
1464 |
6 |
zero_gravi |
csr.mepc <= execute_engine.pc(data_width_c-1 downto 1) & '0'; -- this is the CURRENT pc = interrupted instruction
|
1465 |
12 |
zero_gravi |
csr.mtval <= (others => '0'); -- mtval is zero for interrupts
|
1466 |
9 |
zero_gravi |
else -- for EXCEPTIONS (according to their priority)
|
1467 |
6 |
zero_gravi |
csr.mepc <= execute_engine.last_pc(data_width_c-1 downto 1) & '0'; -- this is the LAST pc = last executed instruction
|
1468 |
14 |
zero_gravi |
if (trap_ctrl.cause(4 downto 0) = trap_iba_c(4 downto 0)) or -- instr access error OR
|
1469 |
|
|
(trap_ctrl.cause(4 downto 0) = trap_ima_c(4 downto 0)) or -- misaligned instruction OR
|
1470 |
|
|
(trap_ctrl.cause(4 downto 0) = trap_brk_c(4 downto 0)) or -- breakpoint OR
|
1471 |
|
|
(trap_ctrl.cause(4 downto 0) = trap_menv_c(4 downto 0)) then -- env call OR
|
1472 |
9 |
zero_gravi |
csr.mtval <= execute_engine.pc(data_width_c-1 downto 1) & '0'; -- address of faulting instruction
|
1473 |
14 |
zero_gravi |
elsif (trap_ctrl.cause(4 downto 0) = trap_iil_c(4 downto 0)) then -- illegal instruction
|
1474 |
12 |
zero_gravi |
csr.mtval <= execute_engine.i_reg; -- faulting instruction itself
|
1475 |
|
|
else -- load/store misalignments/access errors
|
1476 |
9 |
zero_gravi |
csr.mtval <= mar_i; -- faulting data access address
|
1477 |
2 |
zero_gravi |
end if;
|
1478 |
|
|
end if;
|
1479 |
|
|
end if;
|
1480 |
|
|
|
1481 |
|
|
-- context switch in mstatus --
|
1482 |
15 |
zero_gravi |
if (trap_ctrl.env_start_ack = '1') then -- ENTER: trap handler starting?
|
1483 |
|
|
csr.mstatus_mie <= '0'; -- disable interrupts
|
1484 |
|
|
csr.mstatus_mpie <= csr.mstatus_mie; -- buffer previous mie state
|
1485 |
|
|
if (CPU_EXTENSION_RISCV_U = true) then -- implement user mode
|
1486 |
|
|
csr.privilege <= m_priv_mode_c; -- execute trap in machine mode
|
1487 |
|
|
csr.mpp <= csr.privilege; -- buffer previous privilege mode
|
1488 |
2 |
zero_gravi |
end if;
|
1489 |
15 |
zero_gravi |
elsif (trap_ctrl.env_end = '1') then -- EXIT: return from exception
|
1490 |
|
|
csr.mstatus_mie <= csr.mstatus_mpie; -- restore global IRQ enable flag
|
1491 |
|
|
csr.mstatus_mpie <= '1';
|
1492 |
|
|
if (CPU_EXTENSION_RISCV_U = true) then -- implement user mode
|
1493 |
|
|
csr.privilege <= csr.mpp; -- go back to previous privilege mode
|
1494 |
|
|
csr.mpp <= u_priv_mode_c;
|
1495 |
|
|
end if;
|
1496 |
2 |
zero_gravi |
end if;
|
1497 |
9 |
zero_gravi |
|
1498 |
15 |
zero_gravi |
-- user mode NOT implemented --
|
1499 |
|
|
if (CPU_EXTENSION_RISCV_U = false) then -- implement user mode
|
1500 |
|
|
csr.privilege <= m_priv_mode_c;
|
1501 |
|
|
csr.mpp <= m_priv_mode_c;
|
1502 |
|
|
end if;
|
1503 |
2 |
zero_gravi |
end if;
|
1504 |
|
|
end if;
|
1505 |
|
|
end if;
|
1506 |
|
|
end process csr_write_access;
|
1507 |
|
|
|
1508 |
|
|
|
1509 |
|
|
-- Control and Status Registers Read Access -----------------------------------------------
|
1510 |
|
|
-- -------------------------------------------------------------------------------------------
|
1511 |
|
|
csr_read_access: process(clk_i)
|
1512 |
|
|
begin
|
1513 |
|
|
if rising_edge(clk_i) then
|
1514 |
|
|
csr_rdata_o <= (others => '0'); -- default
|
1515 |
11 |
zero_gravi |
if (CPU_EXTENSION_RISCV_Zicsr = true) and (csr.re = '1') then
|
1516 |
|
|
case execute_engine.i_reg(31 downto 20) is
|
1517 |
|
|
|
1518 |
|
|
-- machine trap setup --
|
1519 |
|
|
when x"300" => -- R/W: mstatus - machine status register
|
1520 |
15 |
zero_gravi |
csr_rdata_o(03) <= csr.mstatus_mie; -- MIE
|
1521 |
11 |
zero_gravi |
csr_rdata_o(07) <= csr.mstatus_mpie; -- MPIE
|
1522 |
15 |
zero_gravi |
csr_rdata_o(11) <= csr.mpp(0); -- MPP: machine previous privilege mode low
|
1523 |
|
|
csr_rdata_o(12) <= csr.mpp(1); -- MPP: machine previous privilege mode high
|
1524 |
11 |
zero_gravi |
when x"301" => -- R/-: misa - ISA and extensions
|
1525 |
|
|
csr_rdata_o(02) <= bool_to_ulogic_f(CPU_EXTENSION_RISCV_C); -- C CPU extension
|
1526 |
|
|
csr_rdata_o(04) <= bool_to_ulogic_f(CPU_EXTENSION_RISCV_E); -- E CPU extension
|
1527 |
|
|
csr_rdata_o(08) <= not bool_to_ulogic_f(CPU_EXTENSION_RISCV_E); -- I CPU extension (if not E)
|
1528 |
|
|
csr_rdata_o(12) <= bool_to_ulogic_f(CPU_EXTENSION_RISCV_M); -- M CPU extension
|
1529 |
15 |
zero_gravi |
csr_rdata_o(20) <= bool_to_ulogic_f(CPU_EXTENSION_RISCV_U); -- U CPU extension
|
1530 |
14 |
zero_gravi |
csr_rdata_o(23) <= '1'; -- X CPU extension (non-std extensions)
|
1531 |
11 |
zero_gravi |
csr_rdata_o(30) <= '1'; -- 32-bit architecture (MXL lo)
|
1532 |
|
|
csr_rdata_o(31) <= '0'; -- 32-bit architecture (MXL hi)
|
1533 |
|
|
when x"304" => -- R/W: mie - machine interrupt-enable register
|
1534 |
14 |
zero_gravi |
csr_rdata_o(03) <= csr.mie_msie; -- machine software IRQ enable
|
1535 |
|
|
csr_rdata_o(07) <= csr.mie_mtie; -- machine timer IRQ enable
|
1536 |
|
|
csr_rdata_o(11) <= csr.mie_meie; -- machine external IRQ enable
|
1537 |
|
|
--
|
1538 |
|
|
csr_rdata_o(16) <= csr.mie_firqe(0); -- fast interrupt channel 0
|
1539 |
|
|
csr_rdata_o(17) <= csr.mie_firqe(1); -- fast interrupt channel 1
|
1540 |
|
|
csr_rdata_o(18) <= csr.mie_firqe(2); -- fast interrupt channel 2
|
1541 |
|
|
csr_rdata_o(19) <= csr.mie_firqe(3); -- fast interrupt channel 3
|
1542 |
11 |
zero_gravi |
when x"305" => -- R/W: mtvec - machine trap-handler base address (for ALL exceptions)
|
1543 |
12 |
zero_gravi |
csr_rdata_o <= csr.mtvec(data_width_c-1 downto 2) & "00"; -- mtvec.MODE=0
|
1544 |
11 |
zero_gravi |
|
1545 |
|
|
-- machine trap handling --
|
1546 |
|
|
when x"340" => -- R/W: mscratch - machine scratch register
|
1547 |
|
|
csr_rdata_o <= csr.mscratch;
|
1548 |
|
|
when x"341" => -- R/W: mepc - machine exception program counter
|
1549 |
12 |
zero_gravi |
csr_rdata_o <= csr.mepc(data_width_c-1 downto 1) & '0';
|
1550 |
14 |
zero_gravi |
when x"342" => -- R/-: mcause - machine trap cause
|
1551 |
11 |
zero_gravi |
csr_rdata_o <= csr.mcause;
|
1552 |
|
|
when x"343" => -- R/W: mtval - machine bad address or instruction
|
1553 |
|
|
csr_rdata_o <= csr.mtval;
|
1554 |
|
|
when x"344" => -- R/W: mip - machine interrupt pending
|
1555 |
|
|
csr_rdata_o(03) <= trap_ctrl.irq_buf(interrupt_msw_irq_c);
|
1556 |
|
|
csr_rdata_o(07) <= trap_ctrl.irq_buf(interrupt_mtime_irq_c);
|
1557 |
|
|
csr_rdata_o(11) <= trap_ctrl.irq_buf(interrupt_mext_irq_c);
|
1558 |
14 |
zero_gravi |
--
|
1559 |
|
|
csr_rdata_o(16) <= trap_ctrl.irq_buf(interrupt_firq_0_c);
|
1560 |
|
|
csr_rdata_o(17) <= trap_ctrl.irq_buf(interrupt_firq_1_c);
|
1561 |
|
|
csr_rdata_o(18) <= trap_ctrl.irq_buf(interrupt_firq_2_c);
|
1562 |
|
|
csr_rdata_o(19) <= trap_ctrl.irq_buf(interrupt_firq_3_c);
|
1563 |
11 |
zero_gravi |
|
1564 |
15 |
zero_gravi |
-- physical memory protection --
|
1565 |
|
|
when x"3a0" => -- R/W: pmpcfg0 - physical memory protection configuration register 0
|
1566 |
|
|
if (PMP_USE = true) then
|
1567 |
|
|
if (PMP_NUM_REGIONS >= 1) then
|
1568 |
|
|
csr_rdata_o(07 downto 00) <= csr.pmpcfg(0);
|
1569 |
|
|
end if;
|
1570 |
|
|
if (PMP_NUM_REGIONS >= 2) then
|
1571 |
|
|
csr_rdata_o(15 downto 08) <= csr.pmpcfg(1);
|
1572 |
|
|
end if;
|
1573 |
|
|
if (PMP_NUM_REGIONS >= 3) then
|
1574 |
|
|
csr_rdata_o(23 downto 16) <= csr.pmpcfg(2);
|
1575 |
|
|
end if;
|
1576 |
|
|
if (PMP_NUM_REGIONS >= 4) then
|
1577 |
|
|
csr_rdata_o(31 downto 24) <= csr.pmpcfg(3);
|
1578 |
|
|
end if;
|
1579 |
|
|
end if;
|
1580 |
|
|
when x"3a1" => -- R/W: pmpcfg1 - physical memory protection configuration register 1
|
1581 |
|
|
if (PMP_USE = true) then
|
1582 |
|
|
if (PMP_NUM_REGIONS >= 5) then
|
1583 |
|
|
csr_rdata_o(07 downto 00) <= csr.pmpcfg(4);
|
1584 |
|
|
end if;
|
1585 |
|
|
if (PMP_NUM_REGIONS >= 6) then
|
1586 |
|
|
csr_rdata_o(15 downto 08) <= csr.pmpcfg(5);
|
1587 |
|
|
end if;
|
1588 |
|
|
if (PMP_NUM_REGIONS >= 7) then
|
1589 |
|
|
csr_rdata_o(23 downto 16) <= csr.pmpcfg(6);
|
1590 |
|
|
end if;
|
1591 |
|
|
if (PMP_NUM_REGIONS >= 8) then
|
1592 |
|
|
csr_rdata_o(31 downto 24) <= csr.pmpcfg(7);
|
1593 |
|
|
end if;
|
1594 |
|
|
end if;
|
1595 |
|
|
|
1596 |
|
|
when x"3b0" => -- R/W: pmpaddr0 - physical memory protection address register 0
|
1597 |
|
|
if (PMP_USE = true) and (PMP_NUM_REGIONS >= 1) then
|
1598 |
16 |
zero_gravi |
csr_rdata_o <= csr.pmpaddr(0);
|
1599 |
15 |
zero_gravi |
if (csr.pmpcfg(0)(4 downto 3) = "00") then -- mode = off
|
1600 |
|
|
csr_rdata_o(PMP_GRANULARITY-1 downto 0) <= (others => '0'); -- required for granularity check by SW
|
1601 |
|
|
else -- mode = NAPOT
|
1602 |
|
|
csr_rdata_o(PMP_GRANULARITY-2 downto 0) <= (others => '1');
|
1603 |
|
|
end if;
|
1604 |
|
|
end if;
|
1605 |
|
|
when x"3b1" => -- R/W: pmpaddr1 - physical memory protection address register 1
|
1606 |
|
|
if (PMP_USE = true) and (PMP_NUM_REGIONS >= 2) then
|
1607 |
16 |
zero_gravi |
csr_rdata_o <= csr.pmpaddr(1);
|
1608 |
15 |
zero_gravi |
if (csr.pmpcfg(1)(4 downto 3) = "00") then -- mode = off
|
1609 |
|
|
csr_rdata_o(PMP_GRANULARITY-1 downto 0) <= (others => '0'); -- required for granularity check by SW
|
1610 |
|
|
else -- mode = NAPOT
|
1611 |
|
|
csr_rdata_o(PMP_GRANULARITY-2 downto 0) <= (others => '1');
|
1612 |
|
|
end if;
|
1613 |
|
|
end if;
|
1614 |
|
|
when x"3b2" => -- R/W: pmpaddr2 - physical memory protection address register 2
|
1615 |
|
|
if (PMP_USE = true) and (PMP_NUM_REGIONS >= 3) then
|
1616 |
16 |
zero_gravi |
csr_rdata_o <= csr.pmpaddr(2);
|
1617 |
15 |
zero_gravi |
if (csr.pmpcfg(2)(4 downto 3) = "00") then -- mode = off
|
1618 |
|
|
csr_rdata_o(PMP_GRANULARITY-1 downto 0) <= (others => '0'); -- required for granularity check by SW
|
1619 |
|
|
else -- mode = NAPOT
|
1620 |
|
|
csr_rdata_o(PMP_GRANULARITY-2 downto 0) <= (others => '1');
|
1621 |
|
|
end if;
|
1622 |
|
|
end if;
|
1623 |
|
|
when x"3b3" => -- R/W: pmpaddr3 - physical memory protection address register 3
|
1624 |
|
|
if (PMP_USE = true) and (PMP_NUM_REGIONS >= 4) then
|
1625 |
16 |
zero_gravi |
csr_rdata_o <= csr.pmpaddr(3);
|
1626 |
15 |
zero_gravi |
if (csr.pmpcfg(3)(4 downto 3) = "00") then -- mode = off
|
1627 |
|
|
csr_rdata_o(PMP_GRANULARITY-1 downto 0) <= (others => '0'); -- required for granularity check by SW
|
1628 |
|
|
else -- mode = NAPOT
|
1629 |
|
|
csr_rdata_o(PMP_GRANULARITY-2 downto 0) <= (others => '1');
|
1630 |
|
|
end if;
|
1631 |
|
|
end if;
|
1632 |
|
|
when x"3b4" => -- R/W: pmpaddr4 - physical memory protection address register 4
|
1633 |
|
|
if (PMP_USE = true) and (PMP_NUM_REGIONS >= 5) then
|
1634 |
16 |
zero_gravi |
csr_rdata_o <= csr.pmpaddr(4);
|
1635 |
15 |
zero_gravi |
if (csr.pmpcfg(4)(4 downto 3) = "00") then -- mode = off
|
1636 |
|
|
csr_rdata_o(PMP_GRANULARITY-1 downto 0) <= (others => '0'); -- required for granularity check by SW
|
1637 |
|
|
else -- mode = NAPOT
|
1638 |
|
|
csr_rdata_o(PMP_GRANULARITY-2 downto 0) <= (others => '1');
|
1639 |
|
|
end if;
|
1640 |
|
|
end if;
|
1641 |
|
|
when x"3b5" => -- R/W: pmpaddr5 - physical memory protection address register 5
|
1642 |
|
|
if (PMP_USE = true) and (PMP_NUM_REGIONS >= 6) then
|
1643 |
16 |
zero_gravi |
csr_rdata_o <= csr.pmpaddr(5);
|
1644 |
15 |
zero_gravi |
if (csr.pmpcfg(5)(4 downto 3) = "00") then -- mode = off
|
1645 |
|
|
csr_rdata_o(PMP_GRANULARITY-1 downto 0) <= (others => '0'); -- required for granularity check by SW
|
1646 |
|
|
else -- mode = NAPOT
|
1647 |
|
|
csr_rdata_o(PMP_GRANULARITY-2 downto 0) <= (others => '1');
|
1648 |
|
|
end if;
|
1649 |
|
|
end if;
|
1650 |
|
|
when x"3b6" => -- R/W: pmpaddr6 - physical memory protection address register 6
|
1651 |
|
|
if (PMP_USE = true) and (PMP_NUM_REGIONS >= 7) then
|
1652 |
16 |
zero_gravi |
csr_rdata_o <= csr.pmpaddr(6);
|
1653 |
15 |
zero_gravi |
if (csr.pmpcfg(6)(4 downto 3) = "00") then -- mode = off
|
1654 |
|
|
csr_rdata_o(PMP_GRANULARITY-1 downto 0) <= (others => '0'); -- required for granularity check by SW
|
1655 |
|
|
else -- mode = NAPOT
|
1656 |
|
|
csr_rdata_o(PMP_GRANULARITY-2 downto 0) <= (others => '1');
|
1657 |
|
|
end if;
|
1658 |
|
|
end if;
|
1659 |
|
|
when x"3b7" => -- R/W: pmpaddr7 - physical memory protection address register 7
|
1660 |
|
|
if (PMP_USE = true) and (PMP_NUM_REGIONS >= 8) then
|
1661 |
16 |
zero_gravi |
csr_rdata_o <= csr.pmpaddr(7);
|
1662 |
15 |
zero_gravi |
if (csr.pmpcfg(7)(4 downto 3) = "00") then -- mode = off
|
1663 |
|
|
csr_rdata_o(PMP_GRANULARITY-1 downto 0) <= (others => '0'); -- required for granularity check by SW
|
1664 |
|
|
else -- mode = NAPOT
|
1665 |
|
|
csr_rdata_o(PMP_GRANULARITY-2 downto 0) <= (others => '1');
|
1666 |
|
|
end if;
|
1667 |
|
|
end if;
|
1668 |
|
|
|
1669 |
11 |
zero_gravi |
-- counter and timers --
|
1670 |
|
|
when x"c00" | x"b00" => -- R/(W): cycle/mcycle: Cycle counter LOW
|
1671 |
|
|
csr_rdata_o <= csr.mcycle(31 downto 0);
|
1672 |
12 |
zero_gravi |
when x"c01" => -- R/-: time: System time LOW (from MTIME unit)
|
1673 |
23 |
zero_gravi |
csr_rdata_o <= time_i(31 downto 0);
|
1674 |
11 |
zero_gravi |
when x"c02" | x"b02" => -- R/(W): instret/minstret: Instructions-retired counter LOW
|
1675 |
|
|
csr_rdata_o <= csr.minstret(31 downto 0);
|
1676 |
|
|
when x"c80" | x"b80" => -- R/(W): cycleh/mcycleh: Cycle counter HIGH
|
1677 |
12 |
zero_gravi |
csr_rdata_o <= x"000" & csr.mcycleh(19 downto 0); -- only the lowest 20 bit!
|
1678 |
|
|
when x"c81" => -- R/-: timeh: System time HIGH (from MTIME unit)
|
1679 |
23 |
zero_gravi |
csr_rdata_o <= time_i(63 downto 32);
|
1680 |
11 |
zero_gravi |
when x"c82" | x"b82" => -- R/(W): instreth/minstreth: Instructions-retired counter HIGH
|
1681 |
12 |
zero_gravi |
csr_rdata_o <= x"000" & csr.minstreth(19 downto 0); -- only the lowest 20 bit!
|
1682 |
11 |
zero_gravi |
|
1683 |
|
|
-- machine information registers --
|
1684 |
12 |
zero_gravi |
when x"f11" => -- R/-: mvendorid
|
1685 |
23 |
zero_gravi |
csr_rdata_o <= (others => '0'); -- not assigned
|
1686 |
12 |
zero_gravi |
when x"f12" => -- R/-: marchid
|
1687 |
23 |
zero_gravi |
csr_rdata_o <= (others => '0'); -- not assigned
|
1688 |
|
|
when x"f13" => -- R/-: mimpid - implementation ID / NEORV32 version
|
1689 |
11 |
zero_gravi |
csr_rdata_o <= hw_version_c;
|
1690 |
|
|
when x"f14" => -- R/-: mhartid - hardware thread ID
|
1691 |
12 |
zero_gravi |
csr_rdata_o <= HW_THREAD_ID;
|
1692 |
11 |
zero_gravi |
|
1693 |
22 |
zero_gravi |
-- custom machine read-only CSRs --
|
1694 |
|
|
when x"fc0" => -- R/-: mzext
|
1695 |
|
|
csr_rdata_o(0) <= bool_to_ulogic_f(CPU_EXTENSION_RISCV_Zicsr); -- Zicsr CPU extension
|
1696 |
|
|
csr_rdata_o(1) <= bool_to_ulogic_f(CPU_EXTENSION_RISCV_Zifencei); -- Zifencei CPU extension
|
1697 |
|
|
|
1698 |
11 |
zero_gravi |
-- undefined/unavailable --
|
1699 |
|
|
when others =>
|
1700 |
|
|
csr_rdata_o <= (others => '0'); -- not implemented
|
1701 |
|
|
|
1702 |
|
|
end case;
|
1703 |
12 |
zero_gravi |
else
|
1704 |
|
|
csr_rdata_o <= (others => '0');
|
1705 |
2 |
zero_gravi |
end if;
|
1706 |
|
|
end if;
|
1707 |
|
|
end process csr_read_access;
|
1708 |
|
|
|
1709 |
15 |
zero_gravi |
-- CPU's current privilege level --
|
1710 |
|
|
priv_mode_o <= csr.privilege;
|
1711 |
12 |
zero_gravi |
|
1712 |
15 |
zero_gravi |
-- PMP output --
|
1713 |
|
|
pmp_output: process(csr)
|
1714 |
|
|
begin
|
1715 |
|
|
pmp_addr_o <= (others => (others => '0'));
|
1716 |
|
|
pmp_ctrl_o <= (others => (others => '0'));
|
1717 |
|
|
if (PMP_USE = true) then
|
1718 |
|
|
for i in 0 to PMP_NUM_REGIONS-1 loop
|
1719 |
|
|
pmp_addr_o(i) <= csr.pmpaddr(i) & "00";
|
1720 |
|
|
pmp_ctrl_o(i) <= csr.pmpcfg(i);
|
1721 |
|
|
end loop; -- i
|
1722 |
|
|
end if;
|
1723 |
|
|
end process pmp_output;
|
1724 |
|
|
|
1725 |
|
|
|
1726 |
6 |
zero_gravi |
-- RISC-V Counter CSRs --------------------------------------------------------------------
|
1727 |
2 |
zero_gravi |
-- -------------------------------------------------------------------------------------------
|
1728 |
|
|
csr_counters: process(rstn_i, clk_i)
|
1729 |
|
|
begin
|
1730 |
6 |
zero_gravi |
if (rstn_i = '0') then
|
1731 |
11 |
zero_gravi |
csr.mcycle <= (others => '0');
|
1732 |
|
|
csr.minstret <= (others => '0');
|
1733 |
|
|
csr.mcycleh <= (others => '0');
|
1734 |
|
|
csr.minstreth <= (others => '0');
|
1735 |
|
|
mcycle_msb <= '0';
|
1736 |
|
|
minstret_msb <= '0';
|
1737 |
6 |
zero_gravi |
elsif rising_edge(clk_i) then
|
1738 |
11 |
zero_gravi |
|
1739 |
23 |
zero_gravi |
-- mcycle (cycle) --
|
1740 |
|
|
mcycle_msb <= csr.mcycle(csr.mcycle'left);
|
1741 |
|
|
if (csr.we = '1') and (execute_engine.i_reg(31 downto 20) = x"b00") then -- write access
|
1742 |
|
|
csr.mcycle(31 downto 0) <= csr_wdata_i;
|
1743 |
|
|
csr.mcycle(32) <= '0';
|
1744 |
|
|
elsif (execute_engine.sleep = '0') then -- automatic update (if CPU is not in sleep mode)
|
1745 |
|
|
csr.mcycle <= std_ulogic_vector(unsigned(csr.mcycle) + 1);
|
1746 |
|
|
end if;
|
1747 |
11 |
zero_gravi |
|
1748 |
23 |
zero_gravi |
-- mcycleh (cycleh) --
|
1749 |
|
|
if (csr.we = '1') and (execute_engine.i_reg(31 downto 20) = x"b80") then -- write access
|
1750 |
|
|
csr.mcycleh <= csr_wdata_i(csr.mcycleh'left downto 0);
|
1751 |
|
|
elsif ((mcycle_msb xor csr.mcycle(csr.mcycle'left)) = '1') then -- automatic update
|
1752 |
|
|
csr.mcycleh <= std_ulogic_vector(unsigned(csr.mcycleh) + 1);
|
1753 |
|
|
end if;
|
1754 |
11 |
zero_gravi |
|
1755 |
23 |
zero_gravi |
-- minstret (instret) --
|
1756 |
|
|
minstret_msb <= csr.minstret(csr.minstret'left);
|
1757 |
|
|
if (csr.we = '1') and (execute_engine.i_reg(31 downto 20) = x"b02") then -- write access
|
1758 |
|
|
csr.minstret(31 downto 0) <= csr_wdata_i;
|
1759 |
|
|
csr.minstret(32) <= '0';
|
1760 |
|
|
elsif (execute_engine.state_prev /= EXECUTE) and (execute_engine.state = EXECUTE) then -- automatic update
|
1761 |
|
|
csr.minstret <= std_ulogic_vector(unsigned(csr.minstret) + 1);
|
1762 |
|
|
end if;
|
1763 |
11 |
zero_gravi |
|
1764 |
23 |
zero_gravi |
-- minstreth (instreth) --
|
1765 |
|
|
if (csr.we = '1') and (execute_engine.i_reg(31 downto 20) = x"b82") then -- write access
|
1766 |
|
|
csr.minstreth <= csr_wdata_i(csr.minstreth'left downto 0);
|
1767 |
|
|
elsif ((minstret_msb xor csr.minstret(csr.minstret'left)) = '1') then -- automatic update
|
1768 |
|
|
csr.minstreth <= std_ulogic_vector(unsigned(csr.minstreth) + 1);
|
1769 |
2 |
zero_gravi |
end if;
|
1770 |
|
|
end if;
|
1771 |
|
|
end process csr_counters;
|
1772 |
|
|
|
1773 |
|
|
|
1774 |
|
|
end neorv32_cpu_control_rtl;
|