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

Subversion Repositories neorv32

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

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