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

Subversion Repositories neorv32

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

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