1 |
2 |
madsilicon |
-----------------------------------------------------------------
|
2 |
|
|
-- --
|
3 |
|
|
-----------------------------------------------------------------
|
4 |
|
|
-- --
|
5 |
|
|
-- Copyright (C) 2015 Stefano Tonello --
|
6 |
|
|
-- --
|
7 |
|
|
-- This source file may be used and distributed without --
|
8 |
|
|
-- restriction provided that this copyright statement is not --
|
9 |
|
|
-- removed from the file and that any derivative work contains --
|
10 |
|
|
-- the original copyright notice and the associated disclaimer.--
|
11 |
|
|
-- --
|
12 |
|
|
-- THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY --
|
13 |
|
|
-- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED --
|
14 |
|
|
-- TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS --
|
15 |
|
|
-- FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR --
|
16 |
|
|
-- OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, --
|
17 |
|
|
-- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES --
|
18 |
|
|
-- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE --
|
19 |
|
|
-- GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR --
|
20 |
|
|
-- BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF --
|
21 |
|
|
-- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT --
|
22 |
|
|
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT --
|
23 |
|
|
-- OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE --
|
24 |
|
|
-- POSSIBILITY OF SUCH DAMAGE. --
|
25 |
|
|
-- --
|
26 |
|
|
-----------------------------------------------------------------
|
27 |
|
|
|
28 |
|
|
---------------------------------------------------------------
|
29 |
|
|
-- RV01 Exception processing logic
|
30 |
|
|
---------------------------------------------------------------
|
31 |
|
|
|
32 |
|
|
library IEEE;
|
33 |
|
|
use IEEE.std_logic_1164.all;
|
34 |
|
|
use IEEE.numeric_std.all;
|
35 |
|
|
|
36 |
|
|
library work;
|
37 |
|
|
use work.RV01_CONSTS_PKG.all;
|
38 |
|
|
use work.RV01_TYPES_PKG.all;
|
39 |
|
|
use work.RV01_OP_PKG.all;
|
40 |
|
|
use work.RV01_IDEC_PKG.all;
|
41 |
|
|
use work.RV01_CSR_PKG.all;
|
42 |
|
|
--use work.RV01_CFG_PKG.all;
|
43 |
|
|
|
44 |
|
|
entity RV01_EXCPLOG_IX2 is
|
45 |
|
|
generic(
|
46 |
|
|
NW : natural := 2
|
47 |
|
|
);
|
48 |
|
|
port(
|
49 |
|
|
V_i : in std_logic_vector(2-1 downto 0); -- slot #0,1 valid flag
|
50 |
|
|
INSTR_i : in DEC_INSTR_VEC_T(NW-1 downto 0); -- slot #0/1 inst.
|
51 |
|
|
PC0_i : in ADR_T; -- slot #0 pc
|
52 |
|
|
PC1_i : in ADR_T; -- slot #1 pc
|
53 |
|
|
DADR0_i : in ADR_T; -- slot #0 L/S addr.
|
54 |
|
|
DADR1_i : in ADR_T; -- slot #1 L/S addr.
|
55 |
|
|
HALT_i : in std_logic_vector(2-1 downto 0); -- halt request flag
|
56 |
|
|
RSM_i : in std_logic; -- resume flag
|
57 |
|
|
DRSM_i : in std_logic; -- debug resume flag
|
58 |
|
|
EXT_INT_i : in std_logic; -- external int. flag
|
59 |
|
|
SFT_INT_i : in std_logic; -- soft int. flag
|
60 |
|
|
TMR_INT_i : in std_logic; -- timer int. flag
|
61 |
|
|
ETVA_i : in ADR_T; -- exc. target vector addr.
|
62 |
|
|
MEPC_i : in ADR_T; -- mepc CSR
|
63 |
|
|
DADR0_ERR_i : in std_logic; --
|
64 |
|
|
DADR1_ERR_i : in std_logic; --
|
65 |
|
|
CSR_ILLG_i : in std_logic;
|
66 |
|
|
IE_i : in std_logic;
|
67 |
|
|
STEP_i : in std_logic;
|
68 |
|
|
|
69 |
|
|
V_o : out std_logic_vector(2-1 downto 0); -- slot #0,1 valid flag
|
70 |
|
|
EV_o : out std_logic_vector(2-1 downto 0); -- slot #0,1 excp. valid flag
|
71 |
|
|
INSTR_o : out DEC_INSTR_VEC_T(NW-1 downto 0); -- slot #0/1 inst.
|
72 |
|
|
EERTA_o : out ADR_T -- exception, eret and re-fetch target addr.
|
73 |
|
|
);
|
74 |
|
|
end RV01_EXCPLOG_IX2;
|
75 |
|
|
|
76 |
|
|
architecture ARC of RV01_EXCPLOG_IX2 is
|
77 |
|
|
|
78 |
|
|
signal INSTR : DEC_INSTR_VEC_T(NW-1 downto 0);
|
79 |
|
|
signal EER0 : std_logic;
|
80 |
|
|
|
81 |
|
|
function is_store(OP : LS_OP_T) return std_logic is
|
82 |
|
|
variable S : std_logic;
|
83 |
|
|
begin
|
84 |
|
|
if(OP = LS_SB or OP = LS_SH or OP = LS_SW) then
|
85 |
|
|
S := '1';
|
86 |
|
|
else
|
87 |
|
|
S := '0';
|
88 |
|
|
end if;
|
89 |
|
|
return(S);
|
90 |
|
|
end function;
|
91 |
|
|
|
92 |
|
|
function is_load(OP : LS_OP_T) return std_logic is
|
93 |
|
|
variable S : std_logic;
|
94 |
|
|
begin
|
95 |
|
|
if(OP = LS_LB or OP = LS_LH or OP = LS_LW) then
|
96 |
|
|
S := '1';
|
97 |
|
|
else
|
98 |
|
|
S := '0';
|
99 |
|
|
end if;
|
100 |
|
|
return(S);
|
101 |
|
|
end function;
|
102 |
|
|
|
103 |
|
|
begin
|
104 |
|
|
|
105 |
|
|
------------------------------------
|
106 |
|
|
-- Notes
|
107 |
|
|
------------------------------------
|
108 |
|
|
|
109 |
|
|
-- This module handles exceptions generated in stage IX2.
|
110 |
|
|
-- Exceptions:
|
111 |
|
|
-- 1) L/S access fault.
|
112 |
|
|
-- 2) timer interrupt.
|
113 |
|
|
|
114 |
|
|
-- This module also pre-calculate some signal needed
|
115 |
|
|
-- by exception processing in stage IX3.
|
116 |
|
|
|
117 |
|
|
-- Soft, Timer and external interrups are implicitly
|
118 |
|
|
-- associated to instruction #0.
|
119 |
|
|
|
120 |
|
|
-- Re-fetching of an instructions is given priority
|
121 |
|
|
-- over raising an exception on the same instruction, so
|
122 |
|
|
-- that exception flag gets cleared if re-fetch one is set.
|
123 |
|
|
|
124 |
|
|
-- In-order execution constraint requires, in addition,
|
125 |
|
|
-- than instruction #1 exception flag is cleared even when
|
126 |
|
|
-- instruction #0 re-fetch flag is set.
|
127 |
|
|
|
128 |
|
|
-- Instructions triggering exception or re-fetching are
|
129 |
|
|
-- prevented from altering arch. state be clearing their
|
130 |
|
|
-- valid bits. Their ab-normal state is recorded by setting
|
131 |
|
|
-- exception valid bits (EV).
|
132 |
|
|
|
133 |
|
|
process(INSTR_i,DADR0_ERR_i,DADR1_ERR_i,TMR_INT_i,SFT_INT_i,
|
134 |
|
|
EXT_INT_i,CSR_ILLG_i,V_i,IE_i)
|
135 |
|
|
begin
|
136 |
|
|
INSTR(0) <= INSTR_i(0);
|
137 |
|
|
if(INSTR_i(0).RFTCH = '1') then
|
138 |
|
|
INSTR(0).EXCP <= '0';
|
139 |
|
|
elsif(EXT_INT_i = '1') then
|
140 |
|
|
INSTR(0).EXCP <= '1';
|
141 |
|
|
INSTR(0).ECAUSE <= EXTINT;
|
142 |
|
|
elsif(SFT_INT_i = '1') then
|
143 |
|
|
INSTR(0).EXCP <= '1';
|
144 |
|
|
INSTR(0).ECAUSE <= SOFTINT;
|
145 |
|
|
elsif(TMR_INT_i = '1') then
|
146 |
|
|
INSTR(0).EXCP <= '1';
|
147 |
|
|
INSTR(0).ECAUSE <= TIMRINT;
|
148 |
|
|
elsif(INSTR_i(0).EXCP = '0') then
|
149 |
|
|
if(CSR_ILLG_i = '1') then
|
150 |
|
|
INSTR(0).EXCP <= IE_i;
|
151 |
|
|
INSTR(0).ECAUSE <= ILLGINS;
|
152 |
|
|
elsif(DADR0_ERR_i = '1') then
|
153 |
|
|
if(is_store(INSTR_i(0).LS_OP) = '1') then
|
154 |
|
|
INSTR(0).EXCP <= IE_i;
|
155 |
|
|
INSTR(0).ECAUSE <= SACCFLT;
|
156 |
|
|
elsif(is_load(INSTR_i(0).LS_OP) = '1') then
|
157 |
|
|
INSTR(0).EXCP <= IE_i;
|
158 |
|
|
INSTR(0).ECAUSE <= LACCFLT;
|
159 |
|
|
end if;
|
160 |
|
|
end if;
|
161 |
|
|
end if;
|
162 |
|
|
INSTR(1) <= INSTR_i(1);
|
163 |
|
|
if((INSTR_i(0).RFTCH = '1' and V_i(0) = '1') or INSTR_i(1).RFTCH = '1') then
|
164 |
|
|
INSTR(1).EXCP <= '0';
|
165 |
|
|
elsif(INSTR_i(1).EXCP = '0') then
|
166 |
|
|
if(DADR1_ERR_i = '1') then
|
167 |
|
|
if(is_store(INSTR_i(1).LS_OP) = '1') then
|
168 |
|
|
INSTR(1).EXCP <= IE_i;
|
169 |
|
|
INSTR(1).ECAUSE <= SACCFLT;
|
170 |
|
|
elsif(is_load(INSTR_i(1).LS_OP) = '1') then
|
171 |
|
|
INSTR(1).EXCP <= IE_i;
|
172 |
|
|
INSTR(1).ECAUSE <= LACCFLT;
|
173 |
|
|
end if;
|
174 |
|
|
end if;
|
175 |
|
|
end if;
|
176 |
|
|
end process;
|
177 |
|
|
|
178 |
|
|
INSTR_o <= INSTR;
|
179 |
|
|
|
180 |
|
|
-- Instruction #0 valid flag keeps IX1 value unless:
|
181 |
|
|
-- 1) instruction #0 has to raise an exception, OR
|
182 |
|
|
-- 2) instruction #0 has to be re-fetched, OR
|
183 |
|
|
-- 3) instruction #0 has to be halted
|
184 |
|
|
|
185 |
|
|
V_o(0) <= '0' when (
|
186 |
|
|
(INSTR(0).EXCP = '1') or
|
187 |
|
|
(INSTR(0).RFTCH = '1') or
|
188 |
|
|
(HALT_i(0) = '1' and STEP_i = '0')
|
189 |
|
|
) else V_i(0);
|
190 |
|
|
|
191 |
|
|
-- Instruction #1 valid flag keeps IX1 value unless:
|
192 |
|
|
-- 1) instruction #0 has to raise an exception, OR
|
193 |
|
|
-- 2) instruction #0 is a valid eret one, OR
|
194 |
|
|
-- 3) instruction #0 has to be re-fetched, OR
|
195 |
|
|
-- 4) instruction #0 has to be halted, OR
|
196 |
|
|
-- 5) instruction #1 has to raise an exception, OR
|
197 |
|
|
-- 6) instruction #1 has to be re-fetched, OR
|
198 |
|
|
-- 7) instruction #1 has to be halted.
|
199 |
|
|
|
200 |
|
|
-- Conditions 1-4 are imposed by program order
|
201 |
|
|
-- issue constraint.
|
202 |
|
|
|
203 |
|
|
V_o(1) <= '0' when (
|
204 |
|
|
(V_i(0) = '1' and INSTR(0).EXCP = '1') or
|
205 |
|
|
(V_i(0) = '1' and INSTR_i(0).IMNMC = IM_ERET) or
|
206 |
|
|
(V_i(0) = '1' and INSTR(0).RFTCH = '1') or
|
207 |
|
|
(V_i(0) = '1' and HALT_i(0) = '1') or
|
208 |
|
|
(INSTR(1).EXCP = '1') or
|
209 |
|
|
(INSTR(1).RFTCH = '1') or
|
210 |
|
|
(HALT_i(1) = '1' and (STEP_i = '0' or V_i(0) = '1'))
|
211 |
|
|
) else V_i(1);
|
212 |
|
|
|
213 |
|
|
-- Instruction #0 excp. valid flag value is '0' unless:
|
214 |
|
|
-- 1) instruction #0 has to raise an exception, OR
|
215 |
|
|
-- 2) instruction #0 has to be re-fetched, OR
|
216 |
|
|
-- 3) instruction #0 has to be halted
|
217 |
|
|
|
218 |
|
|
EV_o(0) <= V_i(0) when (
|
219 |
|
|
(INSTR(0).EXCP = '1') or
|
220 |
|
|
(INSTR(0).RFTCH = '1') or
|
221 |
|
|
(HALT_i(0) = '1')
|
222 |
|
|
) else '0';
|
223 |
|
|
|
224 |
|
|
-- Value of EER0 flag is '0' unless:
|
225 |
|
|
-- 1) instruction #0 has to raise an exception, OR
|
226 |
|
|
-- 2) instruction #0 is a valid eret one, OR
|
227 |
|
|
-- 3) instruction #0 has to be re-fetched, OR
|
228 |
|
|
-- 4) instruction #0 has to be halted
|
229 |
|
|
|
230 |
|
|
EER0 <= V_i(0) when (
|
231 |
|
|
(INSTR(0).EXCP = '1') or
|
232 |
|
|
(INSTR_i(0).IMNMC = IM_ERET) or
|
233 |
|
|
(INSTR(0).RFTCH = '1') or
|
234 |
|
|
(HALT_i(0) = '1')
|
235 |
|
|
) else '0';
|
236 |
|
|
|
237 |
|
|
-- Instruction #1 excp. valid flag value is '0' unless:
|
238 |
|
|
-- [
|
239 |
|
|
-- 1) instruction #1 has to raise an exception, OR
|
240 |
|
|
-- 2) instruction #1 has to be re-fetched, OR
|
241 |
|
|
-- 3) instruction #1 has to be halted
|
242 |
|
|
-- ] AND
|
243 |
|
|
-- 4) EER0 is negated
|
244 |
|
|
|
245 |
|
|
-- Condition 4 is imposed by program order
|
246 |
|
|
-- issue constraint.
|
247 |
|
|
|
248 |
|
|
EV_o(1) <= V_i(1) when (
|
249 |
|
|
(
|
250 |
|
|
(INSTR(1).EXCP = '1') or
|
251 |
|
|
(INSTR(1).RFTCH = '1') or
|
252 |
|
|
(HALT_i(1) = '1' and (STEP_i = '0' or V_i(0) = '1'))
|
253 |
|
|
--(HALT_i(1) = '1')
|
254 |
|
|
) and (
|
255 |
|
|
EER0 = '0'
|
256 |
|
|
)
|
257 |
|
|
) else '0';
|
258 |
|
|
|
259 |
|
|
-- Exception, ERET & Re-fetch target address
|
260 |
|
|
-- (pre-calculated in IX2 to save time in IX3)
|
261 |
|
|
|
262 |
|
|
EERTA_o <=
|
263 |
|
|
MEPC_i when (
|
264 |
|
|
(V_i(0) = '1' and INSTR_i(0).IMNMC = IM_ERET) or
|
265 |
|
|
(RSM_i = '1') or (DRSM_i = '1')
|
266 |
|
|
) else
|
267 |
|
|
PC0_i when (V_i(0) = '1' and INSTR(0).RFTCH = '1') else
|
268 |
|
|
ETVA_i when (V_i(0) = '1' and INSTR(0).EXCP = '1') else
|
269 |
|
|
PC1_i when (V_i(1) = '1' and INSTR(1).RFTCH = '1') else
|
270 |
|
|
ETVA_i;
|
271 |
|
|
|
272 |
|
|
end ARC;
|