2 use ieee.std_logic_1164.all;
3 use ieee.numeric_std.all;
7 use work.wishbone_types.all;
9 -- 64 bit direct mapped icache. All instructions are 4B aligned.
14 LINE_SIZE : natural := 64;
16 NUM_LINES : natural := 32
22 i_in : in Fetch1ToIcacheType;
23 i_out : out IcacheToFetch2Type;
25 stall_out : out std_ulogic;
26 flush_in : in std_ulogic;
28 wishbone_out : out wishbone_master_out;
29 wishbone_in : in wishbone_slave_out
33 architecture rtl of icache is
34 function log2(i : natural) return integer is
35 variable tmp : integer := i;
36 variable ret : integer := 0;
45 function ispow2(i : integer) return boolean is
47 if to_integer(to_unsigned(i, 32) and to_unsigned(i - 1, 32)) = 0 then
54 -- BRAM organisation: We never access more than wishbone_data_bits at
55 -- a time so to save resources we make the array only that wide, and
56 -- use consecutive indices for to make a cache "line"
58 -- ROW_SIZE is the width in bytes of the BRAM (based on WB, so 64-bits)
59 constant ROW_SIZE : natural := wishbone_data_bits / 8;
60 -- ROW_PER_LINE is the number of row (wishbone transactions) in a line
61 constant ROW_PER_LINE : natural := LINE_SIZE / ROW_SIZE;
62 -- BRAM_ROWS is the number of rows in BRAM needed to represent the full
64 constant BRAM_ROWS : natural := NUM_LINES * ROW_PER_LINE;
65 -- INSN_PER_ROW is the number of 32bit instructions per BRAM row
66 constant INSN_PER_ROW : natural := wishbone_data_bits / 32;
67 -- Bit fields counts in the address
69 -- INSN_BITS is the number of bits to select an instruction in a row
70 constant INSN_BITS : natural := log2(INSN_PER_ROW);
71 -- ROW_BITS is the number of bits to select a row
72 constant ROW_BITS : natural := log2(BRAM_ROWS);
73 -- ROW_LINEBITS is the number of bits to select a row within a line
74 constant ROW_LINEBITS : natural := log2(ROW_PER_LINE);
75 -- LINE_OFF_BITS is the number of bits for the offset in a cache line
76 constant LINE_OFF_BITS : natural := log2(LINE_SIZE);
77 -- ROW_OFF_BITS is the number of bits for the offset in a row
78 constant ROW_OFF_BITS : natural := log2(ROW_SIZE);
79 -- INDEX_BITS is the number if bits to select a cache line
80 constant INDEX_BITS : natural := log2(NUM_LINES);
81 -- TAG_BITS is the number of bits of the tag part of the address
82 constant TAG_BITS : natural := 64 - LINE_OFF_BITS - INDEX_BITS;
84 -- Example of layout for 32 lines of 64 bytes:
86 -- .. tag |index| line |
88 -- .. | | | |00| zero (2)
89 -- .. | | |-| | INSN_BITS (1)
90 -- .. | |---| | ROW_LINEBITS (3)
91 -- .. | |--- - --| LINE_OFF_BITS (6)
92 -- .. | |- --| ROW_OFF_BITS (3)
93 -- .. |----- ---| | ROW_BITS (8)
94 -- .. |-----| | INDEX_BITS (5)
95 -- .. --------| | TAG_BITS (53)
97 subtype row_t is integer range 0 to BRAM_ROWS-1;
98 subtype index_t is integer range 0 to NUM_LINES-1;
100 -- The cache data BRAM organized as described above
101 subtype cache_row_t is std_logic_vector(wishbone_data_bits-1 downto 0);
102 type cache_array is array(row_t) of cache_row_t;
104 -- The cache tags LUTRAM has a row per cache line
105 subtype cache_tag_t is std_logic_vector(TAG_BITS-1 downto 0);
106 type cache_tags_array is array(index_t) of cache_tag_t;
108 -- Storage. Hopefully "cache_rows" is a BRAM, the rest is LUTs
109 signal cache_rows : cache_array;
110 signal tags : cache_tags_array;
111 signal tags_valid : std_ulogic_vector(NUM_LINES-1 downto 0);
112 attribute ram_style : string;
113 attribute ram_style of cache_rows : signal is "block";
114 attribute ram_decomp : string;
115 attribute ram_decomp of cache_rows : signal is "power";
117 -- Cache reload state machine
118 type state_t is (IDLE, WAIT_ACK);
120 type reg_internal_t is record
121 -- Cache hit state (1 cycle BRAM access)
122 hit_row : cache_row_t;
123 hit_nia : std_ulogic_vector(63 downto 0);
124 hit_smark : std_ulogic;
125 hit_valid : std_ulogic;
127 -- Cache miss state (reload state machine)
129 wb : wishbone_master_out;
130 store_index : index_t;
133 signal r : reg_internal_t;
135 -- Async signals on incoming request
136 signal req_index : index_t;
137 signal req_row : row_t;
138 signal req_tag : cache_tag_t;
139 signal req_is_hit : std_ulogic;
141 -- Return the cache line index (tag index) for an address
142 function get_index(addr: std_ulogic_vector(63 downto 0)) return index_t is
144 return to_integer(unsigned(addr(63-TAG_BITS downto LINE_OFF_BITS)));
147 -- Return the cache row index (data memory) for an address
148 function get_row(addr: std_ulogic_vector(63 downto 0)) return row_t is
150 return to_integer(unsigned(addr(63-TAG_BITS downto ROW_OFF_BITS)));
153 -- Returns whether this is the last row of a line
154 function is_last_row(addr: std_ulogic_vector(63 downto 0)) return boolean is
155 constant ones : std_ulogic_vector(ROW_LINEBITS-1 downto 0) := (others => '1');
157 return addr(LINE_OFF_BITS-1 downto ROW_OFF_BITS) = ones;
160 -- Return the address of the next row in the current cache line
161 function next_row_addr(addr: std_ulogic_vector(63 downto 0)) return std_ulogic_vector is
162 variable row_idx : std_ulogic_vector(ROW_LINEBITS-1 downto 0);
163 variable result : std_ulogic_vector(63 downto 0);
165 -- Is there no simpler way in VHDL to generate that 3 bits adder ?
166 row_idx := addr(LINE_OFF_BITS-1 downto ROW_OFF_BITS);
167 row_idx := std_ulogic_vector(unsigned(row_idx) + 1);
169 result(LINE_OFF_BITS-1 downto ROW_OFF_BITS) := row_idx;
173 -- Read the instruction word for the given address in the current cache row
174 function read_insn_word(addr: std_ulogic_vector(63 downto 0);
175 data: cache_row_t) return std_ulogic_vector is
176 variable word: integer range 0 to INSN_PER_ROW-1;
178 word := to_integer(unsigned(addr(INSN_BITS+2-1 downto 2)));
179 return data(31+word*32 downto word*32);
182 -- Get the tag value from the address
183 function get_tag(addr: std_ulogic_vector(63 downto 0)) return cache_tag_t is
185 return addr(63 downto 64-TAG_BITS);
189 assert ispow2(LINE_SIZE) report "LINE_SIZE not power of 2" severity FAILURE;
190 assert ispow2(NUM_LINES) report "NUM_LINES not power of 2" severity FAILURE;
191 assert ispow2(ROW_PER_LINE) report "ROW_PER_LINE not power of 2" severity FAILURE;
192 assert ispow2(INSN_PER_ROW) report "INSN_PER_ROW not power of 2" severity FAILURE;
193 assert (ROW_BITS = INDEX_BITS + ROW_LINEBITS)
194 report "geometry bits don't add up" severity FAILURE;
195 assert (LINE_OFF_BITS = ROW_OFF_BITS + ROW_LINEBITS)
196 report "geometry bits don't add up" severity FAILURE;
197 assert (64 = TAG_BITS + INDEX_BITS + LINE_OFF_BITS)
198 report "geometry bits don't add up" severity FAILURE;
199 assert (64 = TAG_BITS + ROW_BITS + ROW_OFF_BITS)
200 report "geometry bits don't add up" severity FAILURE;
204 report "ROW_SIZE = " & natural'image(ROW_SIZE);
205 report "ROW_PER_LINE = " & natural'image(ROW_PER_LINE);
206 report "BRAM_ROWS = " & natural'image(BRAM_ROWS);
207 report "INSN_PER_ROW = " & natural'image(INSN_PER_ROW);
208 report "INSN_BITS = " & natural'image(INSN_BITS);
209 report "ROW_BITS = " & natural'image(ROW_BITS);
210 report "ROW_LINEBITS = " & natural'image(ROW_LINEBITS);
211 report "LINE_OFF_BITS = " & natural'image(LINE_OFF_BITS);
212 report "ROW_OFF_BITS = " & natural'image(ROW_OFF_BITS);
213 report "INDEX_BITS = " & natural'image(INDEX_BITS);
214 report "TAG_BITS = " & natural'image(TAG_BITS);
218 icache_comb : process(all)
220 -- Extract line, row and tag from request
221 req_index <= get_index(i_in.nia);
222 req_row <= get_row(i_in.nia);
223 req_tag <= get_tag(i_in.nia);
225 -- Test if pending request is a hit
226 if tags(req_index) = req_tag then
227 req_is_hit <= tags_valid(req_index);
232 -- Output instruction from current cache row
234 -- Note: This is a mild violation of our design principle of having pipeline
235 -- stages output from a clean latch. In this case we output the result
236 -- of a mux. The alternative would be output an entire cache line
237 -- which I prefer not to do just yet.
239 i_out.insn <= read_insn_word(r.hit_nia, r.hit_row);
240 i_out.valid <= r.hit_valid;
241 i_out.nia <= r.hit_nia;
242 i_out.stop_mark <= r.hit_smark;
244 -- This needs to match the latching of a new request in process icache_hit
245 stall_out <= not req_is_hit;
247 -- Wishbone requests output (from the cache miss reload machine)
248 wishbone_out <= r.wb;
251 icache_hit : process(clk)
253 if rising_edge(clk) then
255 if i_in.req = '1' then
256 report "cache search for " & to_hstring(i_in.nia) &
257 " index:" & integer'image(req_index) &
258 " row:" & integer'image(req_row) &
259 " want_tag:" & to_hstring(req_tag) & " got_tag:" & to_hstring(req_tag) &
260 " valid:" & std_ulogic'image(tags_valid(req_index));
261 if req_is_hit = '1' then
268 -- Are we free to latch a new request ?
270 -- Note: this test needs to match the equation for generating stall_out
272 if i_in.req = '1' and req_is_hit = '1' and flush_in = '0' then
273 -- Read the cache line (BRAM read port) and remember the NIA
274 r.hit_row <= cache_rows(req_row);
275 r.hit_nia <= i_in.nia;
276 r.hit_smark <= i_in.stop_mark;
279 report "cache hit nia:" & to_hstring(i_in.nia) &
280 " SM:" & std_ulogic'image(i_in.stop_mark) &
281 " idx:" & integer'image(req_index) &
282 " tag:" & to_hstring(req_tag);
285 -- Send stop marks down regardless of validity
286 r.hit_smark <= i_in.stop_mark;
291 icache_miss : process(clk)
293 if rising_edge(clk) then
295 tags_valid <= (others => '0');
300 -- We only ever do reads on wishbone
301 r.wb.dat <= (others => '0');
302 r.wb.sel <= "11111111";
308 -- We need to read a cache line
309 if i_in.req = '1' and req_is_hit = '0' then
310 report "cache miss nia:" & to_hstring(i_in.nia) &
311 " SM:" & std_ulogic'image(i_in.stop_mark) &
312 " idx:" & integer'image(req_index) &
313 " tag:" & to_hstring(req_tag);
315 -- Force misses while reloading that line
316 tags_valid(req_index) <= '0';
317 tags(req_index) <= req_tag;
318 r.store_index <= req_index;
320 -- Prep for first wishbone read. We calculate the address off
321 -- the start of the cache line
322 r.wb.adr <= i_in.nia(63 downto LINE_OFF_BITS) &
323 (LINE_OFF_BITS-1 downto 0 => '0');
330 if wishbone_in.ack = '1' then
331 -- Store the current dword in both the cache
332 cache_rows(get_row(r.wb.adr)) <= wishbone_in.dat;
334 -- That was the last word ? We are done
335 if is_last_row(r.wb.adr) then
336 tags_valid(r.store_index) <= '1';
341 -- Otherwise, calculate the next row address
342 r.wb.adr <= next_row_addr(r.wb.adr);