1 |
5 |
sergeykhbr |
-----------------------------------------------------------------------------
|
2 |
|
|
--! @file
|
3 |
|
|
--! @copyright Copyright 2018 GNSS Sensor Ltd. All right reserved.
|
4 |
|
|
--! @author Sergey Khabarov - sergeykhbr@gmail.com
|
5 |
|
|
--! @brief CPU Fetch Instruction stage.
|
6 |
|
|
------------------------------------------------------------------------------
|
7 |
|
|
|
8 |
|
|
library ieee;
|
9 |
|
|
use ieee.std_logic_1164.all;
|
10 |
|
|
library commonlib;
|
11 |
|
|
use commonlib.types_common.all;
|
12 |
|
|
--! RIVER CPU specific library.
|
13 |
|
|
library riverlib;
|
14 |
|
|
--! RIVER CPU configuration constants.
|
15 |
|
|
use riverlib.river_cfg.all;
|
16 |
|
|
|
17 |
|
|
|
18 |
|
|
entity InstrFetch is
|
19 |
|
|
port (
|
20 |
|
|
i_clk : in std_logic;
|
21 |
|
|
i_nrst : in std_logic;
|
22 |
|
|
i_pipeline_hold : in std_logic;
|
23 |
|
|
i_mem_req_ready : in std_logic;
|
24 |
|
|
o_mem_addr_valid : out std_logic;
|
25 |
|
|
o_mem_addr : out std_logic_vector(BUS_ADDR_WIDTH-1 downto 0);
|
26 |
|
|
i_mem_data_valid : in std_logic;
|
27 |
|
|
i_mem_data_addr : in std_logic_vector(BUS_ADDR_WIDTH-1 downto 0);
|
28 |
|
|
i_mem_data : in std_logic_vector(31 downto 0);
|
29 |
|
|
o_mem_resp_ready : out std_logic;
|
30 |
|
|
|
31 |
|
|
i_e_npc : in std_logic_vector(BUS_ADDR_WIDTH-1 downto 0);
|
32 |
|
|
i_predict_npc : in std_logic_vector(BUS_ADDR_WIDTH-1 downto 0);
|
33 |
|
|
o_predict_miss : out std_logic;
|
34 |
|
|
|
35 |
|
|
o_mem_req_fire : out std_logic; -- used by branch predictor to form new npc value
|
36 |
|
|
o_valid : out std_logic;
|
37 |
|
|
o_pc : out std_logic_vector(BUS_ADDR_WIDTH-1 downto 0);
|
38 |
|
|
o_instr : out std_logic_vector(31 downto 0);
|
39 |
|
|
o_hold : out std_logic; -- Hold due no response from icache yet
|
40 |
|
|
i_br_fetch_valid : in std_logic; -- Fetch injection address/instr are valid
|
41 |
|
|
i_br_address_fetch : in std_logic_vector(BUS_ADDR_WIDTH-1 downto 0); -- Fetch injection address to skip ebreak instruciton only once
|
42 |
|
|
i_br_instr_fetch : in std_logic_vector(31 downto 0); -- Real instruction value that was replaced by ebreak
|
43 |
|
|
o_instr_buf : out std_logic_vector(DBG_FETCH_TRACE_SIZE*64-1 downto 0) -- trace last fetched instructions
|
44 |
|
|
);
|
45 |
|
|
end;
|
46 |
|
|
|
47 |
|
|
architecture arch_InstrFetch of InstrFetch is
|
48 |
|
|
|
49 |
|
|
type RegistersType is record
|
50 |
|
|
wait_resp : std_logic;
|
51 |
|
|
pipeline_init : std_logic_vector(4 downto 0);
|
52 |
|
|
pc_z1 : std_logic_vector(BUS_ADDR_WIDTH-1 downto 0);
|
53 |
|
|
raddr_not_resp_yet : std_logic_vector(BUS_ADDR_WIDTH-1 downto 0);
|
54 |
|
|
br_address : std_logic_vector(BUS_ADDR_WIDTH-1 downto 0);
|
55 |
|
|
br_instr : std_logic_vector(31 downto 0);
|
56 |
|
|
instr_buf : std_logic_vector(DBG_FETCH_TRACE_SIZE*64-1 downto 0);
|
57 |
|
|
end record;
|
58 |
|
|
|
59 |
|
|
signal r, rin : RegistersType;
|
60 |
|
|
|
61 |
|
|
begin
|
62 |
|
|
|
63 |
|
|
comb : process(i_nrst, i_pipeline_hold, i_mem_req_ready, i_mem_data_valid,
|
64 |
|
|
i_mem_data_addr, i_mem_data, i_e_npc,
|
65 |
|
|
i_predict_npc, i_br_fetch_valid, i_br_address_fetch,
|
66 |
|
|
i_br_instr_fetch, r)
|
67 |
|
|
variable v : RegistersType;
|
68 |
|
|
variable wb_o_addr_req : std_logic_vector(BUS_ADDR_WIDTH-1 downto 0);
|
69 |
|
|
variable w_predict_miss : std_logic;
|
70 |
|
|
variable w_o_req_valid : std_logic;
|
71 |
|
|
variable w_o_req_fire : std_logic;
|
72 |
|
|
variable w_resp_fire : std_logic;
|
73 |
|
|
variable w_o_hold : std_logic;
|
74 |
|
|
variable w_o_mem_resp_ready : std_logic;
|
75 |
|
|
variable wb_o_pc : std_logic_vector(BUS_ADDR_WIDTH-1 downto 0);
|
76 |
|
|
variable wb_o_instr : std_logic_vector(31 downto 0);
|
77 |
|
|
begin
|
78 |
|
|
|
79 |
|
|
v := r;
|
80 |
|
|
|
81 |
|
|
w_o_req_valid := i_nrst and not i_pipeline_hold
|
82 |
|
|
and not (r.wait_resp and not i_mem_data_valid);
|
83 |
|
|
w_o_req_fire := w_o_req_valid and i_mem_req_ready;
|
84 |
|
|
|
85 |
|
|
w_o_mem_resp_ready := not i_pipeline_hold;
|
86 |
|
|
w_resp_fire := i_mem_data_valid and w_o_mem_resp_ready;
|
87 |
|
|
|
88 |
|
|
w_predict_miss := '1';
|
89 |
|
|
if (i_e_npc = r.pc_z1)
|
90 |
|
|
or (i_e_npc = r.raddr_not_resp_yet) then
|
91 |
|
|
w_predict_miss := '0';
|
92 |
|
|
end if;
|
93 |
|
|
|
94 |
|
|
if w_predict_miss = '1' then
|
95 |
|
|
wb_o_addr_req := i_e_npc;
|
96 |
|
|
else
|
97 |
|
|
wb_o_addr_req := i_predict_npc;
|
98 |
|
|
end if;
|
99 |
|
|
|
100 |
|
|
-- Debug last fetched instructions buffer:
|
101 |
|
|
if w_o_req_fire = '1' then
|
102 |
|
|
v.instr_buf(DBG_FETCH_TRACE_SIZE*64-1 downto 64) :=
|
103 |
|
|
r.instr_buf((DBG_FETCH_TRACE_SIZE-1)*64-1 downto 0);
|
104 |
|
|
if w_resp_fire = '1' then
|
105 |
|
|
v.instr_buf(95 downto 64) := i_mem_data;
|
106 |
|
|
end if;
|
107 |
|
|
v.instr_buf(63 downto 32) := wb_o_addr_req;
|
108 |
|
|
v.instr_buf(31 downto 0) := (others => '0');
|
109 |
|
|
elsif w_resp_fire = '1' then
|
110 |
|
|
v.instr_buf(31 downto 0) := i_mem_data;
|
111 |
|
|
end if;
|
112 |
|
|
|
113 |
|
|
|
114 |
|
|
if w_o_req_fire = '1'then
|
115 |
|
|
v.wait_resp := '1';
|
116 |
|
|
v.pc_z1 := r.raddr_not_resp_yet;
|
117 |
|
|
v.raddr_not_resp_yet := wb_o_addr_req; -- Address already requested but probably not responded yet.
|
118 |
|
|
-- Avoid marking such request as 'miss'.
|
119 |
|
|
v.pipeline_init := r.pipeline_init(3 downto 0) & '1';
|
120 |
|
|
elsif (i_mem_data_valid and not w_o_req_fire and not i_pipeline_hold) = '1' then
|
121 |
|
|
v.wait_resp := '0';
|
122 |
|
|
end if;
|
123 |
|
|
|
124 |
|
|
if i_br_fetch_valid = '1' then
|
125 |
|
|
v.br_address := i_br_address_fetch;
|
126 |
|
|
v.br_instr := i_br_instr_fetch;
|
127 |
|
|
end if;
|
128 |
|
|
|
129 |
|
|
if i_mem_data_addr = r.br_address then
|
130 |
|
|
wb_o_pc := r.br_address;
|
131 |
|
|
wb_o_instr := r.br_instr;
|
132 |
|
|
if w_resp_fire = '1' then
|
133 |
|
|
v.br_address := (others => '1');
|
134 |
|
|
end if;
|
135 |
|
|
else
|
136 |
|
|
wb_o_pc := i_mem_data_addr;
|
137 |
|
|
wb_o_instr := i_mem_data;
|
138 |
|
|
end if;
|
139 |
|
|
|
140 |
|
|
if i_nrst = '0' then
|
141 |
|
|
v.wait_resp := '0';
|
142 |
|
|
v.pipeline_init := (others => '0');
|
143 |
|
|
v.pc_z1 := (others => '0');
|
144 |
|
|
v.raddr_not_resp_yet := (others => '0');
|
145 |
|
|
v.br_address := (others => '1');
|
146 |
|
|
v.br_instr := (others => '0');
|
147 |
|
|
v.instr_buf := (others => '0');
|
148 |
|
|
end if;
|
149 |
|
|
|
150 |
|
|
o_mem_addr_valid <= w_o_req_valid;
|
151 |
|
|
o_mem_addr <= wb_o_addr_req;
|
152 |
|
|
o_mem_req_fire <= w_o_req_fire;
|
153 |
|
|
o_valid <= w_resp_fire;
|
154 |
|
|
o_pc <= wb_o_pc;
|
155 |
|
|
o_instr <= wb_o_instr;
|
156 |
|
|
o_predict_miss <= w_predict_miss;
|
157 |
|
|
o_mem_resp_ready <= w_o_mem_resp_ready;
|
158 |
|
|
o_hold <= not w_resp_fire;
|
159 |
|
|
o_instr_buf <= r.instr_buf;
|
160 |
|
|
|
161 |
|
|
rin <= v;
|
162 |
|
|
end process;
|
163 |
|
|
|
164 |
|
|
-- registers:
|
165 |
|
|
regs : process(i_clk)
|
166 |
|
|
begin
|
167 |
|
|
if rising_edge(i_clk) then
|
168 |
|
|
r <= rin;
|
169 |
|
|
end if;
|
170 |
|
|
end process;
|
171 |
|
|
|
172 |
|
|
end;
|