1 |
2 |
madsilicon |
-----------------------------------------------------------------
|
2 |
|
|
-- --
|
3 |
|
|
-----------------------------------------------------------------
|
4 |
|
|
-- --
|
5 |
|
|
-- Copyright (C) 2017 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 Instruction Fecthing 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_CSR_PKG.all;
|
40 |
|
|
|
41 |
|
|
entity RV01_FTCHLOG_2W is
|
42 |
|
|
port(
|
43 |
|
|
CLK_i : in std_logic;
|
44 |
|
|
RST_i : in std_logic;
|
45 |
|
|
STRT_i : in std_logic;
|
46 |
|
|
STRTPC_i : in ADR_T;
|
47 |
|
|
HALT_i : in std_logic;
|
48 |
|
|
BJX_i : in std_logic;
|
49 |
|
|
BJTA_i : in ADR_T;
|
50 |
|
|
PBX_i : in std_logic;
|
51 |
|
|
PBTA_i : in ADR_T;
|
52 |
|
|
KLL1_i : in std_logic;
|
53 |
|
|
PJRX_i : std_logic;
|
54 |
|
|
PJRTA_i : in ADR_T;
|
55 |
|
|
EXCP_i : in std_logic;
|
56 |
|
|
ERET_i : in std_logic;
|
57 |
|
|
RFTCH_i : in std_logic;
|
58 |
|
|
ETVA_i : in ADR_T;
|
59 |
|
|
PSTALL_i : in std_logic;
|
60 |
|
|
-- Debug interface
|
61 |
|
|
DHALT_i : in std_logic;
|
62 |
|
|
|
63 |
|
|
IFV_o : out std_logic_vector(2-1 downto 0);
|
64 |
|
|
IADR0_o : out ADR_T;
|
65 |
|
|
IADR1_o : out ADR_T;
|
66 |
|
|
IADR_MIS_o : out std_logic
|
67 |
|
|
);
|
68 |
|
|
end RV01_FTCHLOG_2W;
|
69 |
|
|
|
70 |
|
|
architecture ARC of RV01_FTCHLOG_2W is
|
71 |
|
|
|
72 |
|
|
signal SZERO : ADR_T := (others => '0');
|
73 |
|
|
signal ONE : std_logic := '1';
|
74 |
|
|
signal PC,PC_q : unsigned(ALEN-4 downto 0);
|
75 |
|
|
signal PC_NS : unsigned(ALEN-4 downto 0);
|
76 |
|
|
signal PCP8,PCP8_q : unsigned(ALEN-4 downto 0);
|
77 |
|
|
signal HALT_q : std_logic;
|
78 |
|
|
signal EVEN_PC : std_logic;
|
79 |
|
|
signal EVEN_PC_NS : std_logic;
|
80 |
|
|
signal FC : std_logic;
|
81 |
|
|
signal PC_NS_FC : unsigned(ALEN-4 downto 0);
|
82 |
|
|
signal EVEN_PC_NS_FC : std_logic;
|
83 |
|
|
|
84 |
|
|
begin
|
85 |
|
|
|
86 |
|
|
-- Halt flag register
|
87 |
|
|
process(CLK_i)
|
88 |
|
|
begin
|
89 |
|
|
if(CLK_i = '1' and CLK_i'event) then
|
90 |
|
|
if(RST_i = '1' or HALT_i = '1') then
|
91 |
|
|
HALT_q <= '1';
|
92 |
|
|
elsif(STRT_i = '1') then
|
93 |
|
|
HALT_q <= '0';
|
94 |
|
|
end if;
|
95 |
|
|
end if;
|
96 |
|
|
end process;
|
97 |
|
|
|
98 |
|
|
-- Instructions are always fetched in pair from an address
|
99 |
|
|
-- aligned on a two-word boundary, so that PC LS 3 bits are
|
100 |
|
|
-- guaranteed to be zero and are therefore not physically
|
101 |
|
|
-- implemented.
|
102 |
|
|
|
103 |
|
|
-- When start or B/J address is an odd word address, fetch
|
104 |
|
|
-- address is still an even word address, but instruction #0
|
105 |
|
|
-- gets nullified.
|
106 |
|
|
|
107 |
|
|
-- Fetched instruction #0 is always valid, unless processor is
|
108 |
|
|
-- halted or fetch address is odd.
|
109 |
|
|
|
110 |
|
|
IFV_o(0) <= (not(HALT_q) or STRT_i) and EVEN_PC;
|
111 |
|
|
|
112 |
|
|
-- Fetched instruction #1 is always valid, unless processor is
|
113 |
|
|
-- halted.
|
114 |
|
|
|
115 |
|
|
IFV_o(1) <= (not(HALT_q) or STRT_i);
|
116 |
|
|
|
117 |
|
|
-- PC register reset value is set to reset exception vector
|
118 |
|
|
-- "low" address.
|
119 |
|
|
|
120 |
|
|
-- PC register is incremented only if pipeline is not
|
121 |
|
|
-- stalled (i.e. when PSTALL_i = '0').
|
122 |
|
|
|
123 |
|
|
-- PC+8 value is pre-calculated in current cycle and stored
|
124 |
|
|
-- into PCP8 to remove addition from address critical path.
|
125 |
|
|
|
126 |
|
|
-- Program Counter register
|
127 |
|
|
process(CLK_i)
|
128 |
|
|
begin
|
129 |
|
|
if(CLK_i = '1' and CLK_i'event) then
|
130 |
|
|
if(RST_i = '1') then
|
131 |
|
|
PC_q <= to_unsigned(0,SDLEN-3);
|
132 |
|
|
PCP8_q <= to_unsigned(1,SDLEN-3);
|
133 |
|
|
elsif((HALT_q = '0') or (STRT_i = '1')) then
|
134 |
|
|
PC_q <= PC;
|
135 |
|
|
PCP8_q <= PCP8;
|
136 |
|
|
end if;
|
137 |
|
|
end if;
|
138 |
|
|
end process;
|
139 |
|
|
|
140 |
|
|
-- PC plus 8
|
141 |
|
|
PCP8 <= PC_NS + 1 when (
|
142 |
|
|
PSTALL_i = '0' or
|
143 |
|
|
EXCP_i = '1' or
|
144 |
|
|
ERET_i = '1' or
|
145 |
|
|
RFTCH_i = '1'
|
146 |
|
|
) else PCP8_q;
|
147 |
|
|
|
148 |
|
|
-- Flow change flag
|
149 |
|
|
FC <= EXCP_i or ERET_i or RFTCH_i or BJX_i;
|
150 |
|
|
|
151 |
|
|
-- PC no-stall value
|
152 |
|
|
process(PBX_i,PJRX_i,FC,PC_NS_FC,PBTA_i,PJRTA_i,PSTALL_i,PC_q,KLL1_i)
|
153 |
|
|
begin
|
154 |
|
|
if(PBX_i = '1' and FC = '0' and PSTALL_i = '0' and KLL1_i = '0') then
|
155 |
|
|
PC_NS <= PBTA_i(ALEN-1 downto 3);
|
156 |
|
|
elsif(PJRX_i = '1' and FC = '0' and PSTALL_i = '0') then
|
157 |
|
|
PC_NS <= PJRTA_i(ALEN-1 downto 3);
|
158 |
|
|
elsif(FC = '1' or PSTALL_i = '0') then
|
159 |
|
|
PC_NS <= PC_NS_FC;
|
160 |
|
|
else
|
161 |
|
|
PC_NS <= PC_q;
|
162 |
|
|
end if;
|
163 |
|
|
end process;
|
164 |
|
|
|
165 |
|
|
-- PC no-stall even flag
|
166 |
|
|
process(PBX_i,PJRX_i,FC,EVEN_PC_NS_FC,PBTA_i,PJRTA_i,PSTALL_i,PC_q,KLL1_i)
|
167 |
|
|
begin
|
168 |
|
|
if(PBX_i = '1' and FC = '0' and PSTALL_i = '0' and KLL1_i = '0') then
|
169 |
|
|
EVEN_PC_NS <= not(PBTA_i(2));
|
170 |
|
|
elsif(PJRX_i = '1' and FC = '0' and PSTALL_i = '0') then
|
171 |
|
|
EVEN_PC_NS <= not(PJRTA_i(2));
|
172 |
|
|
elsif(FC = '1' or PSTALL_i = '0') then
|
173 |
|
|
EVEN_PC_NS <= EVEN_PC_NS_FC;
|
174 |
|
|
else
|
175 |
|
|
EVEN_PC_NS <= '1';
|
176 |
|
|
end if;
|
177 |
|
|
end process;
|
178 |
|
|
|
179 |
|
|
-- Current cycle PC value mux.
|
180 |
|
|
|
181 |
|
|
-- ETVA_i can be true exception vector, an address from CSR MEPC,
|
182 |
|
|
-- a re-fetch address, debug memory address or a resume address.
|
183 |
|
|
-- STRTPC_i can be the reset vector or an address from CSR MRV01HA.
|
184 |
|
|
|
185 |
|
|
process(STRT_i,EXCP_i,ERET_i,RFTCH_i,ETVA_i,BJX_i,BJTA_i,PBX_i,PBTA_i,
|
186 |
|
|
PCP8_q,DHALT_i,STRTPC_i)
|
187 |
|
|
begin
|
188 |
|
|
if(
|
189 |
|
|
BJX_i = '1' and
|
190 |
|
|
(EXCP_i = '0' and ERET_i = '0' and RFTCH_i = '0' and DHALT_i = '0')
|
191 |
|
|
) then
|
192 |
|
|
-- Note: the complicated if-condition allows to remove one
|
193 |
|
|
-- muxing level on BJTA_i path.
|
194 |
|
|
PC_NS_FC <= BJTA_i(ALEN-1 downto 3);
|
195 |
|
|
EVEN_PC_NS_FC <= not(BJTA_i(2));
|
196 |
|
|
elsif(
|
197 |
|
|
EXCP_i = '1' or ERET_i = '1' or RFTCH_i = '1' or DHALT_i = '1'
|
198 |
|
|
) then
|
199 |
|
|
PC_NS_FC <= ETVA_i(ALEN-1 downto 3);
|
200 |
|
|
EVEN_PC_NS_FC <= not(ETVA_i(2));
|
201 |
|
|
elsif(STRT_i = '1') then
|
202 |
|
|
PC_NS_FC <= STRTPC_i(ALEN-1 downto 3);
|
203 |
|
|
EVEN_PC_NS_FC <= not(STRTPC_i(2));
|
204 |
|
|
else
|
205 |
|
|
PC_NS_FC <= PCP8_q;
|
206 |
|
|
EVEN_PC_NS_FC <= '1';
|
207 |
|
|
end if;
|
208 |
|
|
end process;
|
209 |
|
|
|
210 |
|
|
-- Current cycle PC
|
211 |
|
|
PC <= PC_NS;
|
212 |
|
|
|
213 |
|
|
-- Even PC flag
|
214 |
|
|
EVEN_PC <= EVEN_PC_NS;
|
215 |
|
|
|
216 |
|
|
-- Fetch addresses
|
217 |
|
|
|
218 |
|
|
-- instruction #0 address is always an even word address, while
|
219 |
|
|
-- instruction #1 address is always an odd word one.
|
220 |
|
|
|
221 |
|
|
IADR0_o <= PC & "000";
|
222 |
|
|
IADR1_o <= PC & "100";
|
223 |
|
|
|
224 |
|
|
-- A misaligned instruction address can be generated only
|
225 |
|
|
-- by a B/J instruction.
|
226 |
|
|
|
227 |
|
|
process(BJX_i,PBX_i,PJRX_i,BJTA_i,PJRTA_i,PBTA_i,KLL1_i)
|
228 |
|
|
begin
|
229 |
|
|
IADR_MIS_o <= '0';
|
230 |
|
|
if(BJX_i = '1' and BJTA_i(1 downto 0) /= "00") then
|
231 |
|
|
IADR_MIS_o <= '1';
|
232 |
|
|
elsif(PBX_i = '1' and PBTA_i(1 downto 0) /= "00" and KLL1_i = '0') then
|
233 |
|
|
IADR_MIS_o <= '1';
|
234 |
|
|
elsif(PJRX_i = '1' and PJRTA_i(1 downto 0) /= "00") then
|
235 |
|
|
IADR_MIS_o <= '1';
|
236 |
|
|
end if;
|
237 |
|
|
end process;
|
238 |
|
|
|
239 |
|
|
end ARC;
|