OpenCores
URL https://opencores.org/ocsvn/neorv32/neorv32/trunk

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [rtl/] [core/] [neorv32_icache.vhd] - Blame information for rev 70

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 45 zero_gravi
-- #################################################################################################
2
-- # << NEORV32 - Processor-Internal Instruction Cache >>                                          #
3
-- # ********************************************************************************************* #
4 47 zero_gravi
-- # Direct mapped (ICACHE_NUM_SETS = 1) or 2-way set-associative (ICACHE_NUM_SETS = 2).           #
5
-- # Least recently used replacement policy (if ICACHE_NUM_SETS > 1).                              #
6 45 zero_gravi
-- # ********************************************************************************************* #
7
-- # BSD 3-Clause License                                                                          #
8
-- #                                                                                               #
9 70 zero_gravi
-- # Copyright (c) 2022, Stephan Nolting. All rights reserved.                                     #
10 45 zero_gravi
-- #                                                                                               #
11
-- # Redistribution and use in source and binary forms, with or without modification, are          #
12
-- # permitted provided that the following conditions are met:                                     #
13
-- #                                                                                               #
14
-- # 1. Redistributions of source code must retain the above copyright notice, this list of        #
15
-- #    conditions and the following disclaimer.                                                   #
16
-- #                                                                                               #
17
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
18
-- #    conditions and the following disclaimer in the documentation and/or other materials        #
19
-- #    provided with the distribution.                                                            #
20
-- #                                                                                               #
21
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
22
-- #    endorse or promote products derived from this software without specific prior written      #
23
-- #    permission.                                                                                #
24
-- #                                                                                               #
25
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
26
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
27
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
28
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
29
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
30
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
31
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
32
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
33
-- # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
34
-- # ********************************************************************************************* #
35
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
36
-- #################################################################################################
37
 
38
library ieee;
39
use ieee.std_logic_1164.all;
40
use ieee.numeric_std.all;
41
 
42
library neorv32;
43
use neorv32.neorv32_package.all;
44
 
45
entity neorv32_icache is
46
  generic (
47 62 zero_gravi
    ICACHE_NUM_BLOCKS : natural; -- number of blocks (min 1), has to be a power of 2
48
    ICACHE_BLOCK_SIZE : natural; -- block size in bytes (min 4), has to be a power of 2
49
    ICACHE_NUM_SETS   : natural  -- associativity / number of sets (1=direct_mapped), has to be a power of 2
50 45 zero_gravi
  );
51
  port (
52
    -- global control --
53 70 zero_gravi
    clk_i        : in  std_ulogic; -- global clock, rising edge
54
    rstn_i       : in  std_ulogic; -- global reset, low-active, async
55
    clear_i      : in  std_ulogic; -- cache clear
56 45 zero_gravi
    -- host controller interface --
57 70 zero_gravi
    host_addr_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
58
    host_rdata_o : out std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
59
    host_wdata_i : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
60
    host_ben_i   : in  std_ulogic_vector(03 downto 0); -- byte enable
61
    host_we_i    : in  std_ulogic; -- write enable
62
    host_re_i    : in  std_ulogic; -- read enable
63
    host_ack_o   : out std_ulogic; -- bus transfer acknowledge
64
    host_err_o   : out std_ulogic; -- bus transfer error
65 45 zero_gravi
    -- peripheral bus interface --
66 70 zero_gravi
    bus_addr_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
67
    bus_rdata_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
68
    bus_wdata_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
69
    bus_ben_o    : out std_ulogic_vector(03 downto 0); -- byte enable
70
    bus_we_o     : out std_ulogic; -- write enable
71
    bus_re_o     : out std_ulogic; -- read enable
72
    bus_ack_i    : in  std_ulogic; -- bus transfer acknowledge
73
    bus_err_i    : in  std_ulogic  -- bus transfer error
74 45 zero_gravi
  );
75
end neorv32_icache;
76
 
77
architecture neorv32_icache_rtl of neorv32_icache is
78
 
79
  -- cache layout --
80 47 zero_gravi
  constant cache_offset_size_c : natural := index_size_f(ICACHE_BLOCK_SIZE/4); -- offset addresses full 32-bit words
81
  constant cache_index_size_c  : natural := index_size_f(ICACHE_NUM_BLOCKS);
82 45 zero_gravi
  constant cache_tag_size_c    : natural := 32 - (cache_offset_size_c + cache_index_size_c + 2); -- 2 additonal bits for byte offset
83
 
84
  -- cache memory --
85
  component neorv32_icache_memory
86
  generic (
87 47 zero_gravi
    ICACHE_NUM_BLOCKS : natural := 4;  -- number of blocks (min 1), has to be a power of 2
88
    ICACHE_BLOCK_SIZE : natural := 16; -- block size in bytes (min 4), has to be a power of 2
89
    ICACHE_NUM_SETS   : natural := 1   -- associativity; 0=direct-mapped, 1=2-way set-associative
90 45 zero_gravi
  );
91
  port (
92
    -- global control --
93
    clk_i          : in  std_ulogic; -- global clock, rising edge
94
    invalidate_i   : in  std_ulogic; -- invalidate whole cache
95
    -- host cache access (read-only) --
96
    host_addr_i    : in  std_ulogic_vector(31 downto 0); -- access address
97
    host_re_i      : in  std_ulogic; -- read enable
98
    host_rdata_o   : out std_ulogic_vector(31 downto 0); -- read data
99
    -- access status (1 cycle delay to access) --
100
    hit_o          : out std_ulogic; -- hit access
101
    -- ctrl cache access (write-only) --
102
    ctrl_en_i      : in  std_ulogic; -- control interface enable
103
    ctrl_addr_i    : in  std_ulogic_vector(31 downto 0); -- access address
104
    ctrl_we_i      : in  std_ulogic; -- write enable (full-word)
105
    ctrl_wdata_i   : in  std_ulogic_vector(31 downto 0); -- write data
106
    ctrl_tag_we_i  : in  std_ulogic; -- write tag to selected block
107
    ctrl_valid_i   : in  std_ulogic; -- make selected block valid
108
    ctrl_invalid_i : in  std_ulogic  -- make selected block invalid
109
  );
110
  end component;
111
 
112
  -- cache interface --
113
  type cache_if_t is record
114
    clear           : std_ulogic; -- cache clear
115
    host_addr       : std_ulogic_vector(31 downto 0); -- cpu access address
116
    host_rdata      : std_ulogic_vector(31 downto 0); -- cpu read data
117
    hit             : std_ulogic; -- hit access
118
    ctrl_en         : std_ulogic; -- control access enable
119
    ctrl_addr       : std_ulogic_vector(31 downto 0); -- control access address
120
    ctrl_we         : std_ulogic; -- control write enable
121
    ctrl_wdata      : std_ulogic_vector(31 downto 0); -- control write data
122
    ctrl_tag_we     : std_ulogic; -- control tag write enabled
123
    ctrl_valid_we   : std_ulogic; -- control valid flag set
124
    ctrl_invalid_we : std_ulogic; -- control valid flag clear
125
  end record;
126
  signal cache : cache_if_t;
127
 
128
  -- control engine --
129
  type ctrl_engine_state_t is (S_IDLE, S_CACHE_CLEAR, S_CACHE_CHECK, S_CACHE_MISS, S_BUS_DOWNLOAD_REQ, S_BUS_DOWNLOAD_GET,
130 57 zero_gravi
                               S_CACHE_RESYNC_0, S_CACHE_RESYNC_1, S_BUS_ERROR);
131 45 zero_gravi
  type ctrl_t is record
132 61 zero_gravi
    state         : ctrl_engine_state_t; -- current state
133
    state_nxt     : ctrl_engine_state_t; -- next state
134
    addr_reg      : std_ulogic_vector(31 downto 0); -- address register for block download
135
    addr_reg_nxt  : std_ulogic_vector(31 downto 0);
136
    re_buf        : std_ulogic; -- read request buffer
137
    re_buf_nxt    : std_ulogic;
138
    clear_buf     : std_ulogic; -- clear request buffer
139
    clear_buf_nxt : std_ulogic;
140 45 zero_gravi
  end record;
141
  signal ctrl : ctrl_t;
142
 
143
begin
144
 
145
  -- Sanity Checks --------------------------------------------------------------------------
146
  -- -------------------------------------------------------------------------------------------
147
  -- configuration --
148 47 zero_gravi
  assert not (is_power_of_two_f(ICACHE_NUM_BLOCKS) = false) report "NEORV32 PROCESSOR CONFIG ERROR! i-cache number of blocks <ICACHE_NUM_BLOCKS> has to be a power of 2." severity error;
149
  assert not (is_power_of_two_f(ICACHE_BLOCK_SIZE) = false) report "NEORV32 PROCESSOR CONFIG ERROR! i-cache block size <ICACHE_BLOCK_SIZE> has to be a power of 2." severity error;
150
  assert not ((is_power_of_two_f(ICACHE_NUM_SETS) = false)) report "NEORV32 PROCESSOR CONFIG ERROR! i-cache associativity <ICACHE_NUM_SETS> has to be a power of 2." severity error;
151
  assert not (ICACHE_NUM_BLOCKS < 1) report "NEORV32 PROCESSOR CONFIG ERROR! i-cache number of blocks <ICACHE_NUM_BLOCKS> has to be >= 1." severity error;
152
  assert not (ICACHE_BLOCK_SIZE < 4) report "NEORV32 PROCESSOR CONFIG ERROR! i-cache block size <ICACHE_BLOCK_SIZE> has to be >= 4." severity error;
153
  assert not ((ICACHE_NUM_SETS = 0) or (ICACHE_NUM_SETS > 2)) report "NEORV32 PROCESSOR CONFIG ERROR! i-cache associativity <ICACHE_NUM_SETS> has to be 1 (direct-mapped) or 2 (2-way set-associative)." severity error;
154 45 zero_gravi
 
155
 
156
  -- Control Engine FSM Sync ----------------------------------------------------------------
157
  -- -------------------------------------------------------------------------------------------
158 70 zero_gravi
  ctrl_engine_fsm_sync: process(rstn_i, clk_i)
159 45 zero_gravi
  begin
160
    if (rstn_i = '0') then
161 61 zero_gravi
      ctrl.state     <= S_CACHE_CLEAR;
162
      ctrl.re_buf    <= '0';
163
      ctrl.clear_buf <= '0';
164 70 zero_gravi
      ctrl.addr_reg  <= (others => '-');
165 45 zero_gravi
    elsif rising_edge(clk_i) then
166 61 zero_gravi
      ctrl.state     <= ctrl.state_nxt;
167
      ctrl.re_buf    <= ctrl.re_buf_nxt;
168
      ctrl.clear_buf <= ctrl.clear_buf_nxt;
169 70 zero_gravi
      ctrl.addr_reg  <= ctrl.addr_reg_nxt;
170 45 zero_gravi
    end if;
171
  end process ctrl_engine_fsm_sync;
172
 
173
 
174
  -- Control Engine FSM Comb ----------------------------------------------------------------
175
  -- -------------------------------------------------------------------------------------------
176 57 zero_gravi
  ctrl_engine_fsm_comb: process(ctrl, cache, clear_i, host_addr_i, host_re_i, bus_rdata_i, bus_ack_i, bus_err_i)
177 45 zero_gravi
  begin
178
    -- control defaults --
179
    ctrl.state_nxt        <= ctrl.state;
180
    ctrl.addr_reg_nxt     <= ctrl.addr_reg;
181 57 zero_gravi
    ctrl.re_buf_nxt       <= ctrl.re_buf or host_re_i;
182 61 zero_gravi
    ctrl.clear_buf_nxt    <= ctrl.clear_buf or clear_i; -- buffer clear request from CPU
183 45 zero_gravi
 
184
    -- cache defaults --
185
    cache.clear           <= '0';
186
    cache.host_addr       <= host_addr_i;
187
    cache.ctrl_en         <= '0';
188
    cache.ctrl_addr       <= ctrl.addr_reg;
189
    cache.ctrl_we         <= '0';
190
    cache.ctrl_wdata      <= bus_rdata_i;
191
    cache.ctrl_tag_we     <= '0';
192
    cache.ctrl_valid_we   <= '0';
193
    cache.ctrl_invalid_we <= '0';
194
 
195
    -- host interface defaults --
196
    host_ack_o            <= '0';
197
    host_err_o            <= '0';
198
    host_rdata_o          <= cache.host_rdata;
199
 
200
    -- peripheral bus interface defaults --
201
    bus_addr_o            <= ctrl.addr_reg;
202
    bus_wdata_o           <= (others => '0'); -- cache is read-only
203
    bus_ben_o             <= (others => '0'); -- cache is read-only
204
    bus_we_o              <= '0'; -- cache is read-only
205
    bus_re_o              <= '0';
206
 
207
    -- fsm --
208
    case ctrl.state is
209
 
210
      when S_IDLE => -- wait for host access request or cache control operation
211
      -- ------------------------------------------------------------
212 61 zero_gravi
        if (ctrl.clear_buf = '1') then -- cache control operation?
213 45 zero_gravi
          ctrl.state_nxt <= S_CACHE_CLEAR;
214
        elsif (host_re_i = '1') or (ctrl.re_buf = '1') then -- cache access
215 57 zero_gravi
          ctrl.re_buf_nxt <= '0';
216
          ctrl.state_nxt  <= S_CACHE_CHECK;
217 45 zero_gravi
        end if;
218
 
219
      when S_CACHE_CLEAR => -- invalidate all cache entries
220
      -- ------------------------------------------------------------
221 61 zero_gravi
        ctrl.clear_buf_nxt <= '0';
222
        cache.clear        <= '1';
223
        ctrl.state_nxt     <= S_IDLE;
224 45 zero_gravi
 
225
      when S_CACHE_CHECK => -- finalize host access if cache hit
226
      -- ------------------------------------------------------------
227
        if (cache.hit = '1') then -- cache HIT
228 57 zero_gravi
          host_ack_o     <= '1';
229 45 zero_gravi
          ctrl.state_nxt <= S_IDLE;
230
        else -- cache MISS
231
          ctrl.state_nxt <= S_CACHE_MISS;
232
        end if;
233
 
234
      when S_CACHE_MISS => -- 
235
      -- ------------------------------------------------------------
236
        -- compute block base address --
237
        ctrl.addr_reg_nxt <= host_addr_i;
238
        ctrl.addr_reg_nxt((2+cache_offset_size_c)-1 downto 2) <= (others => '0'); -- block-aligned
239
        ctrl.addr_reg_nxt(1 downto 0) <= "00"; -- word-aligned
240
        --
241 57 zero_gravi
        ctrl.state_nxt <= S_BUS_DOWNLOAD_REQ;
242 45 zero_gravi
 
243
      when S_BUS_DOWNLOAD_REQ => -- download new cache block: request new word
244
      -- ------------------------------------------------------------
245 57 zero_gravi
        cache.ctrl_en  <= '1'; -- we are in cache control mode
246 45 zero_gravi
        bus_re_o       <= '1'; -- request new read transfer
247
        ctrl.state_nxt <= S_BUS_DOWNLOAD_GET;
248
 
249
      when S_BUS_DOWNLOAD_GET => -- download new cache block: wait for bus response
250
      -- ------------------------------------------------------------
251
        cache.ctrl_en <= '1'; -- we are in cache control mode
252
        --
253
        if (bus_err_i = '1') then -- bus error
254
          ctrl.state_nxt <= S_BUS_ERROR;
255
        elsif (bus_ack_i = '1') then -- ACK = write to cache and get next word
256
          cache.ctrl_we <= '1'; -- write to cache
257 60 zero_gravi
          if (and_reduce_f(ctrl.addr_reg((2+cache_offset_size_c)-1 downto 2)) = '1') then -- block complete?
258 45 zero_gravi
            cache.ctrl_tag_we   <= '1'; -- current block is valid now
259
            cache.ctrl_valid_we <= '1'; -- write tag of current address
260
            ctrl.state_nxt      <= S_CACHE_RESYNC_0;
261
          else -- get next word
262
            ctrl.addr_reg_nxt <= std_ulogic_vector(unsigned(ctrl.addr_reg) + 4);
263
            ctrl.state_nxt    <= S_BUS_DOWNLOAD_REQ;
264
          end if;
265
        end if;
266
 
267
      when S_CACHE_RESYNC_0 => -- re-sync host/cache access: cache read-latency
268
      -- ------------------------------------------------------------
269
        ctrl.state_nxt <= S_CACHE_RESYNC_1;
270
 
271
      when S_CACHE_RESYNC_1 => -- re-sync host/cache access: finalize CPU request
272
      -- ------------------------------------------------------------
273 57 zero_gravi
        host_ack_o     <= '1';
274 45 zero_gravi
        ctrl.state_nxt <= S_IDLE;
275
 
276
      when S_BUS_ERROR => -- bus error during download
277
      -- ------------------------------------------------------------
278
        host_err_o     <= '1';
279 57 zero_gravi
        ctrl.state_nxt <= S_IDLE;
280 45 zero_gravi
 
281
      when others => -- undefined
282
      -- ------------------------------------------------------------
283
        ctrl.state_nxt <= S_IDLE;
284
 
285
    end case;
286
  end process ctrl_engine_fsm_comb;
287
 
288
 
289
        -- Cache Memory ---------------------------------------------------------------------------
290
  -- -------------------------------------------------------------------------------------------
291
  neorv32_icache_memory_inst: neorv32_icache_memory
292
  generic map (
293 70 zero_gravi
    ICACHE_NUM_BLOCKS => ICACHE_NUM_BLOCKS, -- number of blocks (min 1), has to be a power of 2
294
    ICACHE_BLOCK_SIZE => ICACHE_BLOCK_SIZE, -- block size in bytes (min 4), has to be a power of 2
295
    ICACHE_NUM_SETS   => ICACHE_NUM_SETS    -- associativity; 0=direct-mapped, 1=2-way set-associative
296 45 zero_gravi
  )
297
  port map (
298
    -- global control --
299 70 zero_gravi
    clk_i          => clk_i,                -- global clock, rising edge
300
    invalidate_i   => cache.clear,          -- invalidate whole cache
301
    -- host cache access (read-only) --
302
    host_addr_i    => cache.host_addr,      -- access address
303
    host_re_i      => host_re_i,            -- read enable
304
    host_rdata_o   => cache.host_rdata,     -- read data
305 45 zero_gravi
    -- access status (1 cycle delay to access) --
306 70 zero_gravi
    hit_o          => cache.hit,            -- hit access
307 45 zero_gravi
    -- ctrl cache access (write-only) --
308 70 zero_gravi
    ctrl_en_i      => cache.ctrl_en,        -- control interface enable
309
    ctrl_addr_i    => cache.ctrl_addr,      -- access address
310
    ctrl_we_i      => cache.ctrl_we,        -- write enable (full-word)
311
    ctrl_wdata_i   => cache.ctrl_wdata,     -- write data
312
    ctrl_tag_we_i  => cache.ctrl_tag_we,    -- write tag to selected block
313
    ctrl_valid_i   => cache.ctrl_valid_we,  -- make selected block valid
314
    ctrl_invalid_i => cache.ctrl_invalid_we -- make selected block invalid
315 45 zero_gravi
  );
316
 
317
end neorv32_icache_rtl;
318
 
319
 
320
-- ###########################################################################################################################################
321
-- ###########################################################################################################################################
322
 
323
 
324
-- #################################################################################################
325
-- # << NEORV32 - Cache Memory >>                                                                  #
326
-- # ********************************************************************************************* #
327 47 zero_gravi
-- # Direct mapped (ICACHE_NUM_SETS = 1) or 2-way set-associative (ICACHE_NUM_SETS = 2).           #
328
-- # Least recently used replacement policy (if ICACHE_NUM_SETS > 1).                              #
329 45 zero_gravi
-- # Read-only for host, write-only for control. All output signals have one cycle latency.        #
330
-- #                                                                                               #
331
-- # Cache sets are mapped to individual memory components - no multi-dimensional memory arrays    #
332
-- # are used as some synthesis tools have problems to map these to actual BRAM primitives.        #
333
-- # ********************************************************************************************* #
334
-- # BSD 3-Clause License                                                                          #
335
-- #                                                                                               #
336 70 zero_gravi
-- # Copyright (c) 2022, Stephan Nolting. All rights reserved.                                     #
337 45 zero_gravi
-- #                                                                                               #
338
-- # Redistribution and use in source and binary forms, with or without modification, are          #
339
-- # permitted provided that the following conditions are met:                                     #
340
-- #                                                                                               #
341
-- # 1. Redistributions of source code must retain the above copyright notice, this list of        #
342
-- #    conditions and the following disclaimer.                                                   #
343
-- #                                                                                               #
344
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
345
-- #    conditions and the following disclaimer in the documentation and/or other materials        #
346
-- #    provided with the distribution.                                                            #
347
-- #                                                                                               #
348
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
349
-- #    endorse or promote products derived from this software without specific prior written      #
350
-- #    permission.                                                                                #
351
-- #                                                                                               #
352
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
353
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
354
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
355
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
356
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
357
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
358
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
359
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
360
-- # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
361
-- # ********************************************************************************************* #
362
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
363
-- #################################################################################################
364
 
365
library ieee;
366
use ieee.std_logic_1164.all;
367
use ieee.numeric_std.all;
368
 
369
library neorv32;
370
use neorv32.neorv32_package.all;
371
 
372
entity neorv32_icache_memory is
373
  generic (
374 47 zero_gravi
    ICACHE_NUM_BLOCKS : natural := 4;  -- number of blocks (min 1), has to be a power of 2
375
    ICACHE_BLOCK_SIZE : natural := 16; -- block size in bytes (min 4), has to be a power of 2
376
    ICACHE_NUM_SETS   : natural := 1   -- associativity; 1=direct-mapped, 2=2-way set-associative
377 45 zero_gravi
  );
378
  port (
379
    -- global control --
380 70 zero_gravi
    clk_i          : in  std_ulogic; -- global clock, rising edge
381
    invalidate_i   : in  std_ulogic; -- invalidate whole cache
382 45 zero_gravi
    -- host cache access (read-only) --
383 70 zero_gravi
    host_addr_i    : in  std_ulogic_vector(31 downto 0); -- access address
384
    host_re_i      : in  std_ulogic; -- read enable
385
    host_rdata_o   : out std_ulogic_vector(31 downto 0); -- read data
386 45 zero_gravi
    -- access status (1 cycle delay to access) --
387 70 zero_gravi
    hit_o          : out std_ulogic; -- hit access
388 45 zero_gravi
    -- ctrl cache access (write-only) --
389 70 zero_gravi
    ctrl_en_i      : in  std_ulogic; -- control interface enable
390
    ctrl_addr_i    : in  std_ulogic_vector(31 downto 0); -- access address
391
    ctrl_we_i      : in  std_ulogic; -- write enable (full-word)
392
    ctrl_wdata_i   : in  std_ulogic_vector(31 downto 0); -- write data
393
    ctrl_tag_we_i  : in  std_ulogic; -- write tag to selected block
394
    ctrl_valid_i   : in  std_ulogic; -- make selected block valid
395
    ctrl_invalid_i : in  std_ulogic  -- make selected block invalid
396 45 zero_gravi
  );
397
end neorv32_icache_memory;
398
 
399
architecture neorv32_icache_memory_rtl of neorv32_icache_memory is
400
 
401
  -- cache layout --
402 47 zero_gravi
  constant cache_offset_size_c : natural := index_size_f(ICACHE_BLOCK_SIZE/4); -- offset addresses full 32-bit words
403
  constant cache_index_size_c  : natural := index_size_f(ICACHE_NUM_BLOCKS);
404 70 zero_gravi
  constant cache_tag_size_c    : natural := 32 - (cache_offset_size_c + cache_index_size_c + 2); -- 2 additional bits for byte offset
405 47 zero_gravi
  constant cache_entries_c     : natural := ICACHE_NUM_BLOCKS * (ICACHE_BLOCK_SIZE/4); -- number of 32-bit entries (per set)
406 45 zero_gravi
 
407
  -- status flag memory --
408 47 zero_gravi
  signal valid_flag_s0 : std_ulogic_vector(ICACHE_NUM_BLOCKS-1 downto 0);
409
  signal valid_flag_s1 : std_ulogic_vector(ICACHE_NUM_BLOCKS-1 downto 0);
410 45 zero_gravi
  signal valid         : std_ulogic_vector(1 downto 0); -- valid flag read data
411
 
412
  -- tag memory --
413 47 zero_gravi
  type tag_mem_t is array (0 to ICACHE_NUM_BLOCKS-1) of std_ulogic_vector(cache_tag_size_c-1 downto 0);
414 45 zero_gravi
  signal tag_mem_s0 : tag_mem_t;
415
  signal tag_mem_s1 : tag_mem_t;
416
  type tag_rd_t is array (0 to 1) of std_ulogic_vector(cache_tag_size_c-1 downto 0);
417
  signal tag : tag_rd_t; -- tag read data
418
 
419
  -- access status --
420
  signal hit : std_ulogic_vector(1 downto 0);
421
 
422
  -- access address decomposition --
423
  type acc_addr_t is record
424
    tag    : std_ulogic_vector(cache_tag_size_c-1 downto 0);
425
    index  : std_ulogic_vector(cache_index_size_c-1 downto 0);
426
    offset : std_ulogic_vector(cache_offset_size_c-1 downto 0);
427
  end record;
428
  signal host_acc_addr, ctrl_acc_addr : acc_addr_t;
429
 
430
  -- cache data memory --
431
  type cache_mem_t is array (0 to cache_entries_c-1) of std_ulogic_vector(31 downto 0);
432
  signal cache_data_memory_s0 : cache_mem_t; -- set 0
433
  signal cache_data_memory_s1 : cache_mem_t; -- set 1
434
 
435
  -- cache data memory access --
436
  type cache_rdata_t is array (0 to 1) of std_ulogic_vector(31 downto 0);
437
  signal cache_rdata  : cache_rdata_t;
438
  signal cache_index  : std_ulogic_vector(cache_index_size_c-1 downto 0);
439
  signal cache_offset : std_ulogic_vector(cache_offset_size_c-1 downto 0);
440
  signal cache_addr   : std_ulogic_vector((cache_index_size_c+cache_offset_size_c)-1 downto 0); -- index & offset
441
  signal cache_we     : std_ulogic; -- write enable (full-word)
442
  signal set_select   : std_ulogic;
443
 
444
  -- access history --
445
  type history_t is record
446
    re_ff          : std_ulogic;
447 47 zero_gravi
    last_used_set  : std_ulogic_vector(ICACHE_NUM_BLOCKS-1 downto 0);
448 45 zero_gravi
    to_be_replaced : std_ulogic;
449
  end record;
450
  signal history : history_t;
451
 
452
begin
453
 
454
        -- Access Address Decomposition -----------------------------------------------------------
455
  -- -------------------------------------------------------------------------------------------
456
  host_acc_addr.tag    <= host_addr_i(31 downto 31-(cache_tag_size_c-1));
457
  host_acc_addr.index  <= host_addr_i(31-cache_tag_size_c downto 2+cache_offset_size_c);
458
  host_acc_addr.offset <= host_addr_i(2+(cache_offset_size_c-1) downto 2); -- discard byte offset
459
 
460
  ctrl_acc_addr.tag    <= ctrl_addr_i(31 downto 31-(cache_tag_size_c-1));
461
  ctrl_acc_addr.index  <= ctrl_addr_i(31-cache_tag_size_c downto 2+cache_offset_size_c);
462
  ctrl_acc_addr.offset <= ctrl_addr_i(2+(cache_offset_size_c-1) downto 2); -- discard byte offset
463
 
464
 
465
        -- Cache Access History -------------------------------------------------------------------
466
  -- -------------------------------------------------------------------------------------------
467
  access_history: process(clk_i)
468
  begin
469
    if rising_edge(clk_i) then
470
      history.re_ff <= host_re_i;
471
      if (invalidate_i = '1') then -- invalidate whole cache
472
        history.last_used_set <= (others => '1');
473 60 zero_gravi
      elsif (history.re_ff = '1') and (or_reduce_f(hit) = '1') and (ctrl_en_i = '0') then -- store last accessed set that caused a hit
474 45 zero_gravi
        history.last_used_set(to_integer(unsigned(cache_index))) <= not hit(0);
475
      end if;
476
      history.to_be_replaced <= history.last_used_set(to_integer(unsigned(cache_index)));
477
    end if;
478
  end process access_history;
479
 
480
  -- which set is going to be replaced? -> opposite of last used set = least recently used set --
481 47 zero_gravi
  set_select <= '0' when (ICACHE_NUM_SETS = 1) else (not history.to_be_replaced);
482 45 zero_gravi
 
483
 
484
        -- Status flag memory ---------------------------------------------------------------------
485
  -- -------------------------------------------------------------------------------------------
486
  status_memory: process(clk_i)
487
  begin
488
    if rising_edge(clk_i) then
489
      -- write access --
490
      if (invalidate_i = '1') then -- invalidate whole cache
491
        valid_flag_s0 <= (others => '0');
492
        valid_flag_s1 <= (others => '0');
493
      elsif (ctrl_en_i = '1') then
494
        if (ctrl_invalid_i = '1') then -- make current block invalid
495
          if (set_select = '0') then
496
            valid_flag_s0(to_integer(unsigned(cache_index))) <= '0';
497
          else
498
            valid_flag_s1(to_integer(unsigned(cache_index))) <= '0';
499
          end if;
500
        elsif (ctrl_valid_i = '1') then -- make current block valid
501
          if (set_select = '0') then
502
            valid_flag_s0(to_integer(unsigned(cache_index))) <= '1';
503
          else
504
            valid_flag_s1(to_integer(unsigned(cache_index))) <= '1';
505
          end if;
506
        end if;
507
      end if;
508
      -- read access (sync) --
509
      valid(0) <= valid_flag_s0(to_integer(unsigned(cache_index)));
510
      valid(1) <= valid_flag_s1(to_integer(unsigned(cache_index)));
511
    end if;
512
  end process status_memory;
513
 
514
 
515
        -- Tag memory -----------------------------------------------------------------------------
516
  -- -------------------------------------------------------------------------------------------
517
  tag_memory: process(clk_i)
518
  begin
519
    if rising_edge(clk_i) then
520
      if (ctrl_en_i = '1') and (ctrl_tag_we_i = '1') then -- write access
521
        if (set_select = '0') then
522
          tag_mem_s0(to_integer(unsigned(cache_index))) <= ctrl_acc_addr.tag;
523
        else
524
          tag_mem_s1(to_integer(unsigned(cache_index))) <= ctrl_acc_addr.tag;
525
        end if;
526
      end if;
527 56 zero_gravi
      tag(0) <= tag_mem_s0(to_integer(unsigned(cache_index)));
528
      tag(1) <= tag_mem_s1(to_integer(unsigned(cache_index)));
529 45 zero_gravi
    end if;
530
  end process tag_memory;
531
 
532
  -- comparator --
533
  comparator: process(host_acc_addr, tag, valid)
534
  begin
535
    hit <= (others => '0');
536 47 zero_gravi
    for i in 0 to ICACHE_NUM_SETS-1 loop
537 45 zero_gravi
      if (host_acc_addr.tag = tag(i)) and (valid(i) = '1') then
538
        hit(i) <= '1';
539
      end if;
540
    end loop; -- i
541
  end process comparator;
542
 
543
  -- global hit --
544 60 zero_gravi
  hit_o <= or_reduce_f(hit);
545 45 zero_gravi
 
546
 
547
        -- Cache Data Memory ----------------------------------------------------------------------
548
  -- -------------------------------------------------------------------------------------------
549
  cache_mem_access: process(clk_i)
550
  begin
551
    if rising_edge(clk_i) then
552
      if (cache_we = '1') then -- write access from control (full-word)
553 56 zero_gravi
        if (set_select = '0') or (ICACHE_NUM_SETS = 1) then
554 45 zero_gravi
          cache_data_memory_s0(to_integer(unsigned(cache_addr))) <= ctrl_wdata_i;
555
        else
556
          cache_data_memory_s1(to_integer(unsigned(cache_addr))) <= ctrl_wdata_i;
557
        end if;
558
      end if;
559 56 zero_gravi
      -- read access from host (full-word) --
560
      cache_rdata(0) <= cache_data_memory_s0(to_integer(unsigned(cache_addr)));
561
      cache_rdata(1) <= cache_data_memory_s1(to_integer(unsigned(cache_addr)));
562 45 zero_gravi
    end if;
563
  end process cache_mem_access;
564
 
565
  -- data output --
566 47 zero_gravi
  host_rdata_o <= cache_rdata(0) when (hit(0) = '1') or (ICACHE_NUM_SETS = 1) else cache_rdata(1);
567 45 zero_gravi
 
568
  -- cache block ram access address --
569
  cache_addr <= cache_index & cache_offset;
570
 
571
  -- cache access select --
572
  cache_index  <= host_acc_addr.index  when (ctrl_en_i = '0') else ctrl_acc_addr.index;
573
  cache_offset <= host_acc_addr.offset when (ctrl_en_i = '0') else ctrl_acc_addr.offset;
574
  cache_we     <= '0'                  when (ctrl_en_i = '0') else ctrl_we_i;
575
 
576
 
577
end neorv32_icache_memory_rtl;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.