-
Notifications
You must be signed in to change notification settings - Fork 3
/
icache.vhd
290 lines (226 loc) · 7.04 KB
/
icache.vhd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_unsigned.all;
use IEEE.std_logic_arith.all;
use work.types.all;
use work.util.all;
entity icache is
port (
clk : in std_logic;
rst : in std_logic;
icache_in : in icache_in_type;
icache_out : out icache_out_type);
end entity;
architecture Behavioral of icache is
type state_type is (NO_OP, VMM_REQ, VMM, FETCH_REQ, FETCH);
type reg_type is record
ack : std_logic;
state : state_type;
-- inst cache
valid : std_logic_vector(0 to 255);
tag : std_logic_vector(17 downto 0);
index : std_logic_vector(7 downto 0);
offset : std_logic_vector(3 downto 0);
pdi : std_logic_vector(9 downto 0);
pti : std_logic_vector(9 downto 0);
off : std_logic_vector(11 downto 0);
vmm_n : integer range 0 to 5;
fetch_n : integer range -2 to 15;
hazard : std_logic;
ram_req : std_logic;
ram_addr : std_logic_vector(31 downto 0);
bram_we : std_logic;
bram_di : std_logic_vector(31 downto 0);
bram_addr : std_logic_vector(11 downto 0);
end record;
constant rzero : reg_type := (
ack => '0',
state => NO_OP,
valid => (others => '0'),
tag => (others => '0'),
index => (others => '0'),
offset => (others => '0'),
pdi => (others => '0'),
pti => (others => '0'),
off => (others => '0'),
vmm_n => 0,
fetch_n => -2,
hazard => '0',
ram_req => '0',
ram_addr => (others => '0'),
bram_we => '0',
bram_di => (others => '0'),
bram_addr => (others => '0'));
signal r, rin : reg_type := rzero;
type tag_array_type is
array(0 to 255) of std_logic_vector(17 downto 0);
signal tag_array : tag_array_type := (others => (others => '0'));
signal tag_array_we : std_logic := '0';
signal tag_array_idx : std_logic_vector(7 downto 0) := (others => '0');
signal tag_array_tag : std_logic_vector(17 downto 0) := (others => '0');
impure function detect_miss(addr : std_logic_vector(31 downto 0))
return std_logic is
variable index : integer;
variable tag : std_logic_vector(17 downto 0);
variable miss : std_logic;
begin
index := conv_integer(addr(13 downto 6));
tag := addr(31 downto 14);
if r.valid(index) = '1' and tag_array(index) = tag then
miss := '0';
else
miss := '1';
end if;
return miss;
end function;
begin
comb : process(r, icache_in) is
variable v : reg_type;
variable miss : std_logic;
begin
v := r;
-- inst cache
if icache_in.addr < x"80000000" or icache_in.addr >= x"80002000" then
v.ack := '1';
else
v.ack := '0';
end if;
v.hazard := '0';
v.ram_req := '0';
v.bram_we := '0';
miss := detect_miss(icache_in.addr);
if icache_in.co_we = '1' and detect_miss(icache_in.co_addr) = '0' then
v.valid(conv_integer(icache_in.co_addr(13 downto 6))) := '0';
end if;
case r.state is
when NO_OP =>
if v.ack = '0' then
-- pass
elsif icache_in.re = '1' and miss = '1' then
v.tag := icache_in.addr(31 downto 14);
v.index := icache_in.addr(13 downto 6);
v.offset := icache_in.addr(5 downto 2);
v.pdi := icache_in.addr(31 downto 22);
v.pti := icache_in.addr(21 downto 12);
v.off := icache_in.addr(11 downto 0);
v.ram_req := '1';
v.hazard := '1';
if icache_in.vmm_en = '0' then
v.state := FETCH_REQ;
else
v.state := VMM_REQ;
end if;
elsif icache_in.re = '1' and miss = '0' then
v.bram_addr := icache_in.addr(13 downto 2);
end if;
when VMM_REQ =>
v.ram_req := '1';
v.hazard := '1';
if icache_in.ram_grnt = '1' then
v.ram_addr := icache_in.vmm_pd(31 downto 12) & r.pdi & "00";
v.vmm_n := 0;
v.state := VMM;
end if;
when VMM =>
v.ram_req := '1';
v.hazard := '1';
assert icache_in.ram_grnt = '1' severity failure;
case r.vmm_n is
when 0 | 1 =>
v.vmm_n := r.vmm_n + 1;
when 2 =>
v.vmm_n := r.vmm_n + 1;
v.ram_addr := icache_in.ram_data(31 downto 12) & r.pti & "00";
when 3 | 4 =>
v.vmm_n := r.vmm_n + 1;
when 5 =>
v.vmm_n := 0;
v.ram_addr := icache_in.ram_data(31 downto 12) & r.off(11 downto 6) & "0000" & "00";
v.fetch_n := -2;
v.state := FETCH;
when others =>
assert false;
v.state := NO_OP;
end case;
when FETCH_REQ =>
v.ram_req := '1';
v.hazard := '1';
if icache_in.ram_grnt = '1' then
v.ram_addr := r.tag & r.index & "0000" & "00";
v.fetch_n := -2;
v.state := FETCH;
end if;
when FETCH =>
v.ram_req := '1';
v.hazard := '1';
assert icache_in.ram_grnt = '1' severity failure;
case r.fetch_n is
when -2 | -1 =>
v.ram_addr := r.ram_addr + 4;
v.fetch_n := r.fetch_n + 1;
when 0 to 12 =>
v.ram_addr := r.ram_addr + 4;
v.bram_addr := r.index & conv_std_logic_vector(r.fetch_n, 4);
v.bram_we := '1';
v.bram_di := icache_in.ram_data;
v.fetch_n := r.fetch_n + 1;
when 13 | 14 =>
v.bram_addr := r.index & conv_std_logic_vector(r.fetch_n, 4);
v.bram_we := '1';
v.bram_di := icache_in.ram_data;
v.fetch_n := r.fetch_n + 1;
when 15 =>
v.ram_req := '0';
v.bram_addr := r.index & conv_std_logic_vector(r.fetch_n, 4);
v.bram_we := '1';
v.bram_di := icache_in.ram_data;
v.fetch_n := -2;
v.valid(conv_integer(r.index)) := '1';
v.state := NO_OP;
when others =>
assert false;
v.state := NO_OP;
end case;
when others =>
assert false;
v.state := NO_OP;
end case;
if v.ack = '0' then
v.hazard := '0';
end if;
if icache_in.cai = '1' then
v.valid := (others => '0');
end if;
-- end
rin <= v;
icache_out.stall <= r.hazard;
if r.ack = '1' then
icache_out.rx <= icache_in.bram_do;
else
icache_out.rx <= (others => 'Z');
end if;
if r.fetch_n = 15 then
tag_array_we <= '1';
else
tag_array_we <= '0';
end if;
tag_array_idx <= r.index;
tag_array_tag <= r.tag;
icache_out.bram_we <= v.bram_we;
icache_out.bram_addr <= v.bram_addr;
icache_out.bram_di <= v.bram_di;
icache_out.ram_req <= r.ram_req;
icache_out.ram_addr <= r.ram_addr;
end process;
regs : process(clk, rst) is
begin
if rst = '1' then
r <= rzero;
elsif rising_edge(clk) then
r <= rin;
if tag_array_we = '1' then
tag_array(conv_integer(tag_array_idx)) <= tag_array_tag;
end if;
end if;
end process;
end architecture;