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

Subversion Repositories ion

[/] [ion/] [trunk/] [vhdl/] [mips_cache.vhdl] - Blame information for rev 242

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 114 ja_rd
--------------------------------------------------------------------------------
2 145 ja_rd
-- mips_cache.vhdl -- cache + memory interface module
3 114 ja_rd
--
4
-- This module contains both MIPS caches (I-Cache and D-Cache) combined with
5
-- all the glue logic used to decode and interface external memories and
6
-- devices, both synchronous and asynchronous. 
7
-- Everything that goes into or comes from the CPU passes through this module.
8
--
9 145 ja_rd
-- See a list of known problems at the bottom of this header.
10
-- 
11
--------------------------------------------------------------------------------
12 114 ja_rd
-- Main cache parameters:
13
--
14
-- I-Cache: 256 4-word lines, direct mapped.
15 145 ja_rd
-- D-Cache: 256 4-word lines, direct mapped, write-through
16 114 ja_rd
--
17
-- The cache works mostly like the R3000 caches, except for the following 
18
-- traits:
19
--
20
-- 1.- When bit CP0[12].17='0' (reset value) the cache is 'disabled'. In this 
21
-- state, ALL memory reads miss the cache and force a line refill -- even 
22
-- succesive reads from the same line will refill the entire line. This 
23
-- simplifies the cache logic a lot but slows uncached code a lot. Which means 
24
-- you should initialize the cache and enable it ASAP after reset. 
25
-- 
26
-- 2.- When bits CP0[12].17:16 = "01", the CPU can invalidate a cache line N
27 201 ja_rd
-- by writing word N to ANY address. The write will be executed as normal AND
28 114 ja_rd
-- the cache controller will invalidate I-Cache line N.
29
--
30
-- Note that the standard behavior for bits 17 and 16 of the SR is not
31
-- implemented at all -- no cache swapping, etc.
32
--
33
-- 3.- In this version, all areas of memory are cacheable, except those mapped 
34 145 ja_rd
-- as MT_IO_SYNC or MT_UNMAPPED in mips_pkg. 
35 114 ja_rd
-- Since you can enable or disable the cache at will this difference doesn't 
36
-- seem too important.
37
-- There is a 'cacheable' flag in the t_range_attr record which is currently 
38
-- unused.
39
--
40
-- 4.- The tag is only 14 bits long, which means the memory map is severely
41
-- restricted in this version. See @note2.
42
--
43
-- This is not the standard MIPS way but is compatible enough and above all it
44
-- is simple.
45
--
46
--------------------------------------------------------------------------------
47
-- NOTES:
48
--
49
-- @note1: I-Cache initialization and tag format
50
--
51
-- In the tag table (code_tag_table), tags are stored together with a 'valid' 
52
-- bit (MSB), which is '0' for VALID tags.
53
-- When the CPU invalidates a line, it writes a '1' in the proper tag table 
54
-- entry together with the tag value.
55
-- When tags are matched, the valid bit is matched against 
56
--
57
--
58
-- @note2: I-Cache tags and cache mirroring
59
-- 
60
-- To save space in the I-Cache tag table, the tags are shorter than they 
61
-- should -- 14 bits instead of the 20 bits we would need to cover the
62
-- entire 32-bit address:
63
--
64
--             ___________ <-- These address bits are NOT in the tag
65
--            /           \
66
--  31 ..   27| 26 .. 21  |20 ..          12|11  ..        4|3:2|
67
--  +---------+-----------+-----------------+---------------+---+---+
68
--  | 5       |           | 9               | 8             | 2 |   |
69
--  +---------+-----------+-----------------+---------------+---+---+
70
--  ^                     ^                 ^               ^- LINE_INDEX_SIZE
71
--  5 bits                9 bits            LINE_NUMBER_SIZE
72
--
73
-- Since bits 26 downto 21 are not included in the tag, there will be a 
74
-- 'mirror' effect in the cache. We have split the memory space 
75
-- into 32 separate blocks of 1MB which is obviously not enough but will do
76
-- for the initial tests.
77 235 ja_rd
-- In subsequent versions of the cache, the tag size needs to be enlarged AND 
78 114 ja_rd
-- some of the top bits might be omitted when they're not needed to implement 
79
-- the default memory map (namely bit 30 which is always '0').
80
--
81
--
82 212 ja_rd
-- @note3: Synthesis problem in Quartus-II and workaround
83 114 ja_rd
--
84
-- I had to put a 'dummy' mux between the cache line store and the CPU in order 
85 235 ja_rd
-- to get rid of a quirk in Quartus-II synthesizer (several versions).
86 114 ja_rd
-- If we omit this extra dummy layer of logic the synth will fail to infer the 
87
-- tag table as a BRAM and will use logic fabric instead, crippling performance.
88
-- The mux is otherwise useless and hits performance badly, but so far I haven't
89 235 ja_rd
-- found any other way to overcome this bug, not even with the help of the  
90 114 ja_rd
-- Altera support forum.
91 212 ja_rd
-- Probable cause of this behavior: according to the Cyclone-II manual (section 
92
-- 'M4K Routing Interface'), no direct connection is possible between an M4K 
93
-- data output and the address input of another M4K (in this case, the cache 
94
-- line BRAM and the register bank BRAM). And apparently Quartus-2 won't insert 
95
-- intermediate logic itself for some reason.
96
-- This does not happen with ISE on Spartan-3.
97
-- FIXME: Move this comment to the relevant section of the doc.
98 114 ja_rd
--
99 145 ja_rd
-- @note4: Startup values for the cache tables
100
-- 
101
-- The cache tables has been given startup values; these are only for simulation
102 235 ja_rd
-- convenience and have no effect on the cache behaviour (and obviously they
103 145 ja_rd
-- are only used after FPGA config, not after reset). 
104 151 ja_rd
--
105 114 ja_rd
--------------------------------------------------------------------------------
106
-- This module interfaces the CPU to the following:
107
--
108
--  1.- Internal 32-bit-wide BRAM for read only
109
--  2.- Internal 32-bit I/O bus
110
--  3.- External 16-bit or 8-bit wide static memory (SRAM or FLASH)
111
--  4.- External 16-bit wide SDRAM (NOT IMPLEMENTED YET)
112
--
113
-- The SRAM memory interface signals are meant to connect directly to FPGA pins
114
-- and all outputs are registered (tco should be minimal).
115
-- SRAM data inputs are NOT registered, though. They go through a couple muxes
116
-- before reaching the first register so watch out for tsetup.
117
--
118
--------------------------------------------------------------------------------
119
-- External FPGA signals
120
--
121
-- This module has signals meant to connect directly to FPGA pins: the SRAM
122
-- interface. They are either direct register outputs or at most with an
123
-- intervening 2-mux, in order to minimize the Tco (clock-to-output).
124
--
125
-- The Tco of these signals has to be accounted for in the real SRAM interface.
126
-- For example, under Quartus-2 and with a Cyclone-2 grade -7 device, the
127
-- worst Tco for the SRAM data pins is below 5 ns, enough to use a 10ns SRAM
128
-- with a 20 ns clock cycle.
129
-- Anyway, you need to take care of this yourself (synthesis constraints).
130
--
131
--------------------------------------------------------------------------------
132
-- Interface to CPU
133
--
134
-- 1.- All signals coming from the CPU are registered.
135
-- 2.- All CPU inputs come directly from a register, or at most have a 2-mux in
136
--     between.
137
--
138
-- This means this block will not degrade the timing performance of the system,
139
-- as long as its logic is shallower than the current bottleneck (the ALU).
140
--
141
--------------------------------------------------------------------------------
142
-- KNOWN PROBLEMS:
143
--
144
-- 1.- All parameters hardcoded -- generics are almost ignored.
145 145 ja_rd
-- 2.- SRAM read state machine does not guarantee internal FPGA Thold. 
146
--     Currently it works because the FPGA hold tines (including an input mux
147
--     in the parent module) are far smaller than the SRAM response times, but
148
--     it would be better to insert an extra cycle after the wait states in
149
--     the sram read state machine.
150 114 ja_rd
--------------------------------------------------------------------------------
151 162 ja_rd
-- Copyright (C) 2011 Jose A. Ruiz
152 161 ja_rd
--                                                              
153
-- This source file may be used and distributed without         
154
-- restriction provided that this copyright statement is not    
155
-- removed from the file and that any derivative work contains  
156
-- the original copyright notice and the associated disclaimer. 
157
--                                                              
158
-- This source file is free software; you can redistribute it   
159
-- and/or modify it under the terms of the GNU Lesser General   
160
-- Public License as published by the Free Software Foundation; 
161
-- either version 2.1 of the License, or (at your option) any   
162
-- later version.                                               
163
--                                                              
164
-- This source is distributed in the hope that it will be       
165
-- useful, but WITHOUT ANY WARRANTY; without even the implied   
166
-- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      
167
-- PURPOSE.  See the GNU Lesser General Public License for more 
168
-- details.                                                     
169
--                                                              
170
-- You should have received a copy of the GNU Lesser General    
171
-- Public License along with this source; if not, download it   
172
-- from http://www.opencores.org/lgpl.shtml
173
--------------------------------------------------------------------------------
174 114 ja_rd
 
175
library ieee;
176
use ieee.std_logic_1164.all;
177
use ieee.std_logic_arith.all;
178
use ieee.std_logic_unsigned.all;
179
use work.mips_pkg.all;
180
 
181
 
182
entity mips_cache is
183
    generic (
184
        BRAM_ADDR_SIZE : integer    := 10;  -- BRAM address size
185
        SRAM_ADDR_SIZE : integer    := 17;  -- Static RAM/Flash address size
186
 
187
        -- these cache parameters are unused in this implementation, they're
188
        -- here for compatibility to the final cache module.
189
        LINE_SIZE : integer         := 4;   -- Line size in words
190
        CACHE_SIZE : integer        := 256  -- I- and D- cache size in lines
191
    );
192
    port(
193
        clk             : in std_logic;
194
        reset           : in std_logic;
195
 
196
        -- Interface to CPU core
197
        data_addr       : in std_logic_vector(31 downto 0);
198
        data_rd         : out std_logic_vector(31 downto 0);
199
        data_rd_vma     : in std_logic;
200
 
201
        code_rd_addr    : in std_logic_vector(31 downto 2);
202
        code_rd         : out std_logic_vector(31 downto 0);
203
        code_rd_vma     : in std_logic;
204
 
205
        byte_we         : in std_logic_vector(3 downto 0);
206
        data_wr         : in std_logic_vector(31 downto 0);
207
 
208
        mem_wait        : out std_logic;
209 242 ja_rd
        cache_ready     : out std_logic;
210 114 ja_rd
        cache_enable    : in std_logic;
211
        ic_invalidate   : in std_logic;
212 134 ja_rd
        unmapped        : out std_logic;
213 114 ja_rd
 
214
        -- interface to FPGA i/o devices
215
        io_rd_data      : in std_logic_vector(31 downto 0);
216
        io_rd_addr      : out std_logic_vector(31 downto 2);
217
        io_wr_addr      : out std_logic_vector(31 downto 2);
218
        io_wr_data      : out std_logic_vector(31 downto 0);
219
        io_rd_vma       : out std_logic;
220
        io_byte_we      : out std_logic_vector(3 downto 0);
221
 
222
        -- interface to synchronous 32-bit-wide FPGA BRAM (possibly used as ROM)
223
        bram_rd_data    : in std_logic_vector(31 downto 0);
224
        bram_wr_data    : out std_logic_vector(31 downto 0);
225
        bram_rd_addr    : out std_logic_vector(BRAM_ADDR_SIZE+1 downto 2);
226
        bram_wr_addr    : out std_logic_vector(BRAM_ADDR_SIZE+1 downto 2);
227
        bram_byte_we    : out std_logic_vector(3 downto 0);
228
        bram_data_rd_vma: out std_logic;
229
 
230
        -- interface to asynchronous 16-bit-wide or 8-bit-wide static memory
231
        sram_address    : out std_logic_vector(SRAM_ADDR_SIZE-1 downto 0);
232
        sram_data_rd    : in std_logic_vector(15 downto 0);
233
        sram_data_wr    : out std_logic_vector(15 downto 0);
234
        sram_byte_we_n  : out std_logic_vector(1 downto 0);
235
        sram_oe_n       : out std_logic
236
    );
237
end entity mips_cache;
238
 
239
 
240
architecture direct of mips_cache is
241
 
242
-- Address of line within line store
243
constant LINE_NUMBER_SIZE : integer := log2(CACHE_SIZE);
244
-- Address of word within line
245
constant LINE_INDEX_SIZE : integer  := log2(LINE_SIZE);
246
-- Address of word within line store
247
constant LINE_ADDR_SIZE : integer   := LINE_NUMBER_SIZE+LINE_INDEX_SIZE;
248
 
249
-- Code tag size, excluding valid bit
250
-- FIXME should be a generic
251
constant CODE_TAG_SIZE : integer    := 14;
252
-- Data tag size, excluding valid bit
253
-- FIXME should be a generic
254
constant DATA_TAG_SIZE : integer    := 14;
255
 
256
 
257
-- Wait state counter -- we're supporting static memory from 10 to >100 ns
258
-- (0 to 7 wait states with realistic clock rates).
259
subtype t_wait_state_counter is std_logic_vector(2 downto 0);
260
 
261
-- State machine ----------------------------------------------------
262
 
263
type t_cache_state is (
264 242 ja_rd
    cache_reset,                -- Between reset and 1st code refill,
265 114 ja_rd
    idle,                       -- Cache is hitting, control machine idle
266
 
267
    -- Code refill --------------------------------------------------
268
    code_refill_bram_0,         -- pc in bram_rd_addr
269
    code_refill_bram_1,         -- op in bram_rd
270
    code_refill_bram_2,         -- op in code_rd
271
 
272
    code_refill_sram_0,         -- rd addr in SRAM addr bus (low hword)
273
    code_refill_sram_1,         -- rd addr in SRAM addr bus (high hword)
274
 
275
    code_refill_sram8_0,        -- rd addr in SRAM addr bus (byte 0)
276
    code_refill_sram8_1,        -- rd addr in SRAM addr bus (byte 1)
277
    code_refill_sram8_2,        -- rd addr in SRAM addr bus (byte 2)
278
    code_refill_sram8_3,        -- rd addr in SRAM addr bus (byte 3)
279
 
280
    code_crash,                 -- tried to run from i/o or something like that
281
 
282
    -- Data refill & write-through ----------------------------------
283
    data_refill_sram_0,         -- rd addr in SRAM addr bus (low hword)
284
    data_refill_sram_1,         -- rd addr in SRAM addr bus (high hword)
285
 
286
    data_refill_sram8_0,        -- rd addr in SRAM addr bus (byte 0)
287
    data_refill_sram8_1,        -- rd addr in SRAM addr bus (byte 1)
288
    data_refill_sram8_2,        -- rd addr in SRAM addr bus (byte 2)
289
    data_refill_sram8_3,        -- rd addr in SRAM addr bus (byte 3)
290
 
291
    data_refill_bram_0,         -- rd addr in bram_rd_addr
292
    data_refill_bram_1,         -- rd data in bram_rd_data
293 145 ja_rd
    data_refill_bram_2,
294 114 ja_rd
 
295
    data_read_io_0,             -- rd addr on io_rd_addr, io_vma active
296
    data_read_io_1,             -- rd data on io_rd_data
297
 
298
    data_write_io_0,            -- wr addr & data in io_wr_*, io_byte_we active
299
 
300
    data_writethrough_sram_0a,  -- wr addr & data in SRAM buses (low hword)
301
    data_writethrough_sram_0b,  -- WE asserted
302
    data_writethrough_sram_0c,  -- WE deasserted
303
    data_writethrough_sram_1a,  -- wr addr & data in SRAM buses (high hword)
304
    data_writethrough_sram_1b,  -- WE asserted
305
    data_writethrough_sram_1c,  -- WE deasserted
306
 
307
    data_ignore_write,          -- hook for raising error flag FIXME untested
308
    data_ignore_read,           -- hook for raising error flag FIXME untested
309
 
310
    -- Other states -------------------------------------------------
311 145 ja_rd
 
312
    --code_wait_for_dcache,       -- wait for D-cache to stop using the buses
313 114 ja_rd
    bug                         -- caught an error in the state machine
314
   );
315
 
316
-- Cache state machine state register & next state
317
signal ps, ns :             t_cache_state;
318
-- Wait state down-counter, formally part of the state machine register
319
signal ws_ctr :             t_wait_state_counter;
320
-- Wait states for memory being accessed
321
signal ws_value :           t_wait_state_counter;
322
-- Asserted to initialize the wait state counter
323
signal load_ws_ctr :        std_logic;
324
-- Asserted when the wait state counter has reached zero
325
signal ws_wait_done :       std_logic;
326
-- Refill word counters
327
signal code_refill_ctr :    integer range 0 to LINE_SIZE-1;
328
signal data_refill_ctr :    integer range 0 to LINE_SIZE-1;
329 145 ja_rd
signal data_refill_start :  std_logic;
330
signal data_refill_end :    std_logic;
331 114 ja_rd
 
332 145 ja_rd
 
333 114 ja_rd
-- CPU interface registers ------------------------------------------
334 145 ja_rd
-- Registered CPU addresses
335 114 ja_rd
signal data_rd_addr_reg :   t_pc;
336
signal data_wr_addr_reg :   t_pc;
337
signal code_rd_addr_reg :   t_pc;
338
 
339 145 ja_rd
-- Data write register (data to be written to external RAM)
340 114 ja_rd
signal data_wr_reg :        std_logic_vector(31 downto 0);
341 145 ja_rd
-- Registered byte_we vector
342 114 ja_rd
signal byte_we_reg :        std_logic_vector(3 downto 0);
343
 
344
-- SRAM interface ---------------------------------------------------
345 145 ja_rd
-- Stores first (high) Half-Word read from SRAM
346 114 ja_rd
signal sram_rd_data_reg :   std_logic_vector(31 downto 8);
347
-- Data read from SRAM, valid in refill_1
348
signal sram_rd_data :       t_word;
349
 
350
 
351
-- I-cache ----------------------------------------------------------
352
 
353
subtype t_line_addr is std_logic_vector(LINE_NUMBER_SIZE-1 downto 0);
354
subtype t_word_addr is std_logic_vector(LINE_ADDR_SIZE-1 downto 0);
355 145 ja_rd
 
356 114 ja_rd
subtype t_code_tag is std_logic_vector(CODE_TAG_SIZE+1-1 downto 0);
357
type t_code_tag_table is array(CACHE_SIZE-1 downto 0) of t_code_tag;
358
type t_code_line_table is array((CACHE_SIZE*LINE_SIZE)-1 downto 0) of t_word;
359
 
360 145 ja_rd
-- Code tag table (stores line tags) (@note4)
361 114 ja_rd
signal code_tag_table :     t_code_tag_table   := (others => "000000000000000");
362
-- Code line table  (stores lines)
363
signal code_line_table :    t_code_line_table  := (others => X"00000000");
364
 
365
-- Tag from code fetch address ('target' address, straight from CPU lines)
366
signal code_tag :           t_code_tag;
367
-- Registered code_tag, used matching after reading from code_tag_table
368
signal code_tag_reg :       t_code_tag;
369
-- Tag read from cache (will be matched against code_tag_reg)
370
signal code_cache_tag :     t_code_tag;
371
-- Code cache line address for read and write ports
372
signal code_line_addr :     t_line_addr;
373
-- Code cache word address (read from cache)
374
signal code_word_addr :     t_word_addr;
375
-- Code cache word address (write to cache in refills)
376
signal code_word_addr_wr :  t_word_addr;
377
 
378
-- Word written into code cache
379
signal code_refill_data :   t_word;
380
-- Address the code refill data is fetched from
381
signal code_refill_addr :   t_pc;
382
 
383
-- code word read from cache
384
signal code_cache_rd :      t_word;
385
-- raised when code_cache_rd is not valid due to a cache miss
386
signal code_miss :          std_logic;
387
-- code_miss for accesses to CACHED areas with cache enabled
388
signal code_miss_cached : std_logic;
389
-- code_miss for accesses to UNCACHED areas OR with cache disabled
390
signal code_miss_uncached : std_logic;
391
-- '1' when the I-cache state machine stalls the pipeline (mem_wait)
392
signal code_wait :          std_logic;
393
 
394 242 ja_rd
 
395 145 ja_rd
-- D-cache ----------------------------------------------------------
396
 
397
subtype t_data_tag is std_logic_vector(DATA_TAG_SIZE+1-1 downto 0);
398
type t_data_tag_table is array(CACHE_SIZE-1 downto 0) of t_data_tag;
399
type t_data_line_table is array((CACHE_SIZE*LINE_SIZE)-1 downto 0) of t_word;
400
 
401
-- Data tag table (stores line tags)
402
signal data_tag_table :     t_data_tag_table   := (others => "000000000000000");
403
-- Data line table  (stores lines)
404
signal data_line_table :    t_data_line_table  := (others => X"00000000");
405
 
406
-- Asserted when the D-Cache line table is to be written to
407
signal update_data_line :   std_logic;
408
signal update_data_tag :    std_logic;
409
 
410
-- Tag from data load address ('target' address, straight from CPU lines)
411
signal data_tag :           t_data_tag;
412
-- Registered data_tag, used matching after reading from data_tag_table
413
signal data_tag_reg :       t_data_tag;
414
-- Tag read from cache (will be matched against data_tag_reg)
415 114 ja_rd
signal data_cache_tag :     t_data_tag;
416 145 ja_rd
-- '1' when the read OR write data address tag matches the cache tag
417
signal data_tags_match :    std_logic;
418
-- Data cache line address for read and write ports
419
signal data_line_addr :     t_line_addr;
420
-- Data cache word address (read from cache)
421
signal data_word_addr :     t_word_addr;
422
-- Data cache word address (write to cache in refills)
423
signal data_word_addr_wr :  t_word_addr;
424
 
425
-- Word written into data cache
426
signal data_refill_data :   t_word;
427
-- Address the code refill data is fetched from (word address)
428
signal data_refill_addr :   t_pc;
429
 
430
-- Data word read from cache
431 114 ja_rd
signal data_cache_rd :      t_word;
432 145 ja_rd
-- Raised when data_cache_rd is not valid due to a cache miss
433 114 ja_rd
signal data_miss :          std_logic;
434 145 ja_rd
-- Data miss logic, portion used with cache enabledº
435
signal data_miss_cached :   std_logic;
436
-- Data miss logic, portion used with cach disabled
437
signal data_miss_uncached : std_logic;
438 151 ja_rd
-- Active when LW follows right after a SW (see caveats in code below)
439
signal data_miss_by_invalidation : std_logic;
440 145 ja_rd
-- Active when the data tag comparison result is valid (1 cycle after rd_vma)
441
-- Note: no relation to byte_we. 
442
signal data_tag_match_valid:std_logic;
443
-- Active when the D-cache state machine stalls the pipeline (mem_wait)
444 114 ja_rd
signal data_wait :          std_logic;
445 145 ja_rd
-- Active when there's a write waiting to be done
446
signal write_pending :      std_logic;
447
-- Active when there's a read waiting to be done
448
signal read_pending :       std_logic;
449 114 ja_rd
 
450
 
451
-- Address decoding -------------------------------------------------
452
 
453
-- Address slices used to decode
454
signal code_rd_addr_mask :  t_addr_decode;
455
signal data_rd_addr_mask :  t_addr_decode;
456
signal data_wr_addr_mask :  t_addr_decode;
457
 
458
-- Memory map area being accessed for each of the 3 buses:
459
signal code_rd_attr :       t_range_attr;
460
signal data_rd_attr :       t_range_attr;
461
signal data_wr_attr :       t_range_attr;
462
 
463
--------------------------------------------------------------------------------
464
begin
465
 
466
--------------------------------------------------------------------------------
467
-- Cache control state machine
468
 
469
cache_state_machine_reg:
470
process(clk)
471
begin
472
   if clk'event and clk='1' then
473
        if reset='1' then
474 242 ja_rd
            ps <= cache_reset;
475 114 ja_rd
        else
476
            ps <= ns;
477
        end if;
478
    end if;
479
end process cache_state_machine_reg;
480
 
481
-- Unified control state machine for I-Cache and D-cache -----------------------
482 145 ja_rd
-- FIXME The state machine deals with all supported widths and types of memory, 
483
-- there should be a simpler version with only SRAM/ROM and DRAM.
484 114 ja_rd
control_state_machine_transitions:
485 145 ja_rd
process(ps, code_rd_vma, data_rd_vma, code_miss,
486 114 ja_rd
        data_wr_attr.mem_type, data_rd_attr.mem_type, code_rd_attr.mem_type,
487 145 ja_rd
        ws_wait_done, code_refill_ctr, data_refill_ctr,
488 114 ja_rd
        write_pending, read_pending)
489
begin
490
    case ps is
491 242 ja_rd
 
492
    -- The cache will remain in 'cache_reset' state until the first code miss,
493
    -- at which time the state machine proceeds as usual.
494
    -- The only difference between states idle and cache_reset is that in
495
    -- cache_reset the output cache_ready is '0', which will prevent the CPU
496
    -- from loading its IR with the cache output -- which is known invalid.
497
    when idle | cache_reset =>
498 114 ja_rd
        if code_miss='1' then
499
            case code_rd_attr.mem_type is
500
            when MT_BRAM        => ns <= code_refill_bram_0;
501
            when MT_SRAM_16B    => ns <= code_refill_sram_0;
502
            when MT_SRAM_8B     => ns <= code_refill_sram8_0;
503
            when others         => ns <= code_crash;
504
            end case;
505
 
506
        elsif write_pending='1' then
507
            case data_wr_attr.mem_type is
508
            when MT_BRAM        => ns <= data_ignore_write;
509
            when MT_SRAM_16B    => ns <= data_writethrough_sram_0a;
510
            when MT_IO_SYNC     => ns <= data_write_io_0;
511
            -- FIXME ignore write to undecoded area (clear pending flag)
512 134 ja_rd
            when others         => ns <= data_ignore_write;
513 114 ja_rd
            end case;
514
 
515
        elsif read_pending='1' then
516
            case data_rd_attr.mem_type is
517
            when MT_BRAM        => ns <= data_refill_bram_0;
518
            when MT_SRAM_16B    => ns <= data_refill_sram_0;
519
            when MT_SRAM_8B     => ns <= data_refill_sram8_0;
520
            when MT_IO_SYNC     => ns <= data_read_io_0;
521
            -- FIXME ignore read from undecoded area (clear pending flag)
522
            when others         => ns <= data_ignore_read;
523
            end case;
524
 
525
        else
526
            ns <= ps;
527
        end if;
528
 
529
 
530
    -- Code refill states -------------------------------------------
531
 
532
    when code_refill_bram_0 =>
533
        ns <= code_refill_bram_1;
534
 
535
    when code_refill_bram_1 =>
536
        ns <= code_refill_bram_2;
537
 
538
    when code_refill_bram_2 =>
539
        if code_refill_ctr/=0 then
540
            -- Still not finished refilling line, go for next word
541
            ns <= code_refill_bram_0;
542
        else
543
            -- If there's a data operation pending, do it now
544
            if write_pending='1' then
545
                case data_wr_attr.mem_type is
546
                when MT_BRAM        => ns <= data_ignore_write;
547
                when MT_SRAM_16B    => ns <= data_writethrough_sram_0a;
548
                when MT_IO_SYNC     => ns <= data_write_io_0;
549
                -- FIXME ignore write to undecoded area (clear pending flag)
550 151 ja_rd
                when others         => ns <= data_ignore_write;
551 114 ja_rd
                end case;
552
 
553
            elsif read_pending='1' then
554
                case data_rd_attr.mem_type is
555
                when MT_BRAM        => ns <= data_refill_bram_0;
556
                when MT_SRAM_16B    => ns <= data_refill_sram_0;
557
                when MT_SRAM_8B     => ns <= data_refill_sram8_0;
558
                when MT_IO_SYNC     => ns <= data_read_io_0;
559
                -- FIXME ignore read from undecoded area (clear pending flag)
560
                when others         => ns <= data_ignore_read;
561
                end case;
562
 
563
            else
564
                ns <= idle;
565
            end if;
566
        end if;
567
 
568
    when code_refill_sram_0 =>
569
        if ws_wait_done='1' then
570
            ns <= code_refill_sram_1;
571
        else
572
            ns <= ps;
573
        end if;
574
 
575
    when code_refill_sram_1 =>
576
        if code_refill_ctr/=0 and ws_wait_done='1' then
577
            -- Still not finished refilling line, go for next word
578
            ns <= code_refill_sram_0;
579
        else
580
            if ws_wait_done='1' then
581
                -- If there's a data operation pending, do it now
582
                if write_pending='1' then
583
                    case data_wr_attr.mem_type is
584
                    when MT_BRAM        => ns <= data_ignore_write;
585
                    when MT_SRAM_16B    => ns <= data_writethrough_sram_0a;
586
                    when MT_IO_SYNC     => ns <= data_write_io_0;
587
                    -- FIXME ignore write to undecoded area (clear pending flag)
588 151 ja_rd
                    when others         => ns <= data_ignore_write;
589 114 ja_rd
                    end case;
590
 
591
                elsif read_pending='1' then
592
                    case data_rd_attr.mem_type is
593
                    when MT_BRAM        => ns <= data_refill_bram_0;
594
                    when MT_SRAM_16B    => ns <= data_refill_sram_0;
595
                    when MT_SRAM_8B     => ns <= data_refill_sram8_0;
596
                    when MT_IO_SYNC     => ns <= data_read_io_0;
597
                    -- FIXME ignore read from undecoded area (clear pending flag)
598
                    when others         => ns <= data_ignore_read;
599
                    end case;
600
 
601
                else
602
                    ns <= idle;
603
                end if;
604
            else
605
                ns <= ps;
606
            end if;
607
        end if;
608
 
609
    when code_refill_sram8_0 =>
610
        if ws_wait_done='1' then
611
            ns <= code_refill_sram8_1;
612
        else
613
            ns <= ps;
614
        end if;
615
 
616
    when code_refill_sram8_1 =>
617
        if ws_wait_done='1' then
618
            ns <= code_refill_sram8_2;
619
        else
620
            ns <= ps;
621
        end if;
622
 
623
    when code_refill_sram8_2 =>
624
        if ws_wait_done='1' then
625
            ns <= code_refill_sram8_3;
626
        else
627
            ns <= ps;
628
        end if;
629
 
630
    when code_refill_sram8_3 =>
631
        if code_refill_ctr/=0 and ws_wait_done='1' then
632
            -- Still not finished refilling line, go for next word
633
            ns <= code_refill_sram8_0;
634
        else
635
            if ws_wait_done='1' then
636
                -- If there's a data operation pending, do it now
637
                if write_pending='1' then
638
                    case data_wr_attr.mem_type is
639
                    when MT_BRAM        => ns <= data_ignore_write;
640
                    when MT_SRAM_16B    => ns <= data_writethrough_sram_0a;
641
                    when MT_IO_SYNC     => ns <= data_write_io_0;
642
                    -- FIXME ignore write to undecoded area (clear pending flag)
643
                    when others         => ns <= data_ignore_write;
644
                    end case;
645
 
646
                elsif read_pending='1' then
647
                    case data_rd_attr.mem_type is
648
                    when MT_BRAM        => ns <= data_refill_bram_0;
649
                    when MT_SRAM_16B    => ns <= data_refill_sram_0;
650
                    when MT_SRAM_8B     => ns <= data_refill_sram8_0;
651
                    when MT_IO_SYNC     => ns <= data_read_io_0;
652
                    -- FIXME ignore read from undecoded area (clear pending flag)
653
                    when others         => ns <= data_ignore_read;
654
                    end case;
655
 
656
                else
657
                    ns <= idle;
658
                end if;
659
            else
660
                ns <= ps;
661
            end if;
662
        end if;
663
 
664
    -- Data refill & write-through states ---------------------------
665
 
666
    when data_write_io_0 =>
667
        ns <= idle;
668
 
669
    when data_read_io_0 =>
670
        ns <= data_read_io_1;
671
 
672
    when data_read_io_1 =>
673
        ns <= idle;
674
 
675
    when data_refill_sram8_0 =>
676
        if ws_wait_done='1' then
677
            ns <= data_refill_sram8_1;
678
        else
679
            ns <= ps;
680
        end if;
681
 
682
    when data_refill_sram8_1 =>
683
        if ws_wait_done='1' then
684
            ns <= data_refill_sram8_2;
685
        else
686
            ns <= ps;
687
        end if;
688
 
689
    when data_refill_sram8_2 =>
690
        if ws_wait_done='1' then
691
            ns <= data_refill_sram8_3;
692
        else
693
            ns <= ps;
694
        end if;
695
 
696
    when data_refill_sram8_3 =>
697
        if ws_wait_done='1' then
698 145 ja_rd
            if data_refill_ctr/=LINE_SIZE-1 then
699
                ns <= data_refill_sram8_0;
700
            else
701
                ns <= idle;
702
            end if;
703 114 ja_rd
        else
704
            ns <= ps;
705
        end if;
706
 
707
    when data_refill_sram_0 =>
708
        if ws_wait_done='1' then
709
            ns <= data_refill_sram_1;
710
        else
711
            ns <= ps;
712
        end if;
713
 
714
    when data_refill_sram_1 =>
715
        if ws_wait_done='1' then
716 145 ja_rd
            if data_refill_ctr=LINE_SIZE-1 then
717
                ns <= idle;
718
            else
719
                ns <= data_refill_sram_0;
720
            end if;
721 114 ja_rd
        else
722
            ns <= ps;
723
        end if;
724
 
725
    when data_refill_bram_0 =>
726
        ns <= data_refill_bram_1;
727
 
728
    when data_refill_bram_1 =>
729 145 ja_rd
        ns <= data_refill_bram_2;
730 114 ja_rd
 
731 145 ja_rd
    when data_refill_bram_2 =>
732
        if data_refill_ctr/=(LINE_SIZE-1) then
733
            -- Still not finished refilling line, go for next word
734
            ns <= data_refill_bram_0;
735
        else
736
            if read_pending='1' then
737
                case data_rd_attr.mem_type is
738
                when MT_BRAM        => ns <= data_refill_bram_0;
739
                when MT_SRAM_16B    => ns <= data_refill_sram_0;
740
                when MT_SRAM_8B     => ns <= data_refill_sram8_0;
741
                when MT_IO_SYNC     => ns <= data_read_io_0;
742
                -- FIXME ignore read from undecoded area (clear pending flag)
743
                when others         => ns <= data_ignore_read;
744
                end case;
745
            else
746
                ns <= idle;
747
            end if;
748
        end if;
749
 
750
 
751
 
752 114 ja_rd
    when data_writethrough_sram_0a =>
753
        ns <= data_writethrough_sram_0b;
754
 
755
    when data_writethrough_sram_0b =>
756
        if ws_wait_done='1' then
757
            ns <= data_writethrough_sram_0c;
758
        else
759
            ns <= ps;
760
        end if;
761
 
762
    when data_writethrough_sram_0c =>
763
        ns <= data_writethrough_sram_1a;
764
 
765
    when data_writethrough_sram_1a =>
766
        ns <= data_writethrough_sram_1b;
767
 
768
    when data_writethrough_sram_1b =>
769
        if ws_wait_done='1' then
770
            ns <= data_writethrough_sram_1c;
771
        else
772
            ns <= ps;
773
        end if;
774
 
775
    when data_writethrough_sram_1c =>
776
        if read_pending='1' then
777
            case data_rd_attr.mem_type is
778
            when MT_BRAM        => ns <= data_refill_bram_0;
779
            when MT_SRAM_16B    => ns <= data_refill_sram_0;
780
            when MT_SRAM_8B     => ns <= data_refill_sram8_0;
781
            when MT_IO_SYNC     => ns <= data_read_io_0;
782
            -- FIXME ignore read from undecoded area (clear pending flag)
783
            when others         => ns <= data_ignore_read;
784
            end case;
785
        else
786
            ns <= idle;
787
        end if;
788
 
789
    when data_ignore_write =>
790
        ns <= idle;
791
 
792
    when data_ignore_read =>
793
        ns <= idle;
794
 
795
    -- Exception states (something went wrong) ----------------------
796
 
797
    when code_crash =>
798
        -- Attempted to fetch from i/o area. This is a software bug, probably,
799
        -- and should trigger a trap. We have 1 cycle to do something about it.
800 145 ja_rd
        -- FIXME do something about wrong fetch: trap, etc.
801 114 ja_rd
        -- After this cycle, back to normal.
802
        ns <= idle;
803
 
804
    when bug =>
805
        -- Something weird happened, we have 1 cycle to do something like raise
806
        -- an error flag, etc. After 1 cycle, back to normal.
807
        -- FIXME raise trap or flag or something
808
        ns <= idle;
809
 
810
    when others =>
811
        -- We should never arrive here. If we do we handle it in state bug.
812
        ns <= bug;
813
    end case;
814
end process control_state_machine_transitions;
815
 
816
 
817
--------------------------------------------------------------------------------
818
-- Wait state logic
819
 
820
-- load wait state counter when we're entering the state we will wait on
821
load_ws_ctr <= '1' when
822
    (ns=code_refill_sram_0  and ps/=code_refill_sram_0) or
823
    (ns=code_refill_sram_1  and ps/=code_refill_sram_1) or
824
    (ns=code_refill_sram8_0 and ps/=code_refill_sram8_0) or
825
    (ns=code_refill_sram8_1 and ps/=code_refill_sram8_1) or
826
    (ns=code_refill_sram8_2 and ps/=code_refill_sram8_2) or
827
    (ns=code_refill_sram8_3 and ps/=code_refill_sram8_3) or
828
    (ns=data_refill_sram_0  and ps/=data_refill_sram_0) or
829
    (ns=data_refill_sram_1  and ps/=data_refill_sram_1) or
830
    (ns=data_refill_sram8_0 and ps/=data_refill_sram8_0) or
831
    (ns=data_refill_sram8_1 and ps/=data_refill_sram8_1) or
832
    (ns=data_refill_sram8_2 and ps/=data_refill_sram8_2) or
833
    (ns=data_refill_sram8_3 and ps/=data_refill_sram8_3) or
834
    (ns=data_writethrough_sram_0a) or
835
    (ns=data_writethrough_sram_1a)
836
    else '0';
837
 
838
 
839
-- select the wait state counter value as that of read address or write address
840
with ns select ws_value <=
841
    data_rd_attr.wait_states    when data_refill_sram_0,
842
    data_rd_attr.wait_states    when data_refill_sram_1,
843
    data_rd_attr.wait_states    when data_refill_sram8_0,
844
    data_rd_attr.wait_states    when data_refill_sram8_1,
845
    data_rd_attr.wait_states    when data_refill_sram8_2,
846
    data_rd_attr.wait_states    when data_refill_sram8_3,
847
    data_wr_attr.wait_states    when data_writethrough_sram_0a,
848
    data_wr_attr.wait_states    when data_writethrough_sram_1a,
849
    code_rd_attr.wait_states    when code_refill_sram_0,
850
    code_rd_attr.wait_states    when code_refill_sram_1,
851
    code_rd_attr.wait_states    when code_refill_sram8_0,
852
    code_rd_attr.wait_states    when code_refill_sram8_1,
853
    code_rd_attr.wait_states    when code_refill_sram8_2,
854
    code_rd_attr.wait_states    when code_refill_sram8_3,
855
    data_wr_attr.wait_states    when others;
856
 
857
 
858
wait_state_counter_reg:
859
process(clk)
860
begin
861
    if clk'event and clk='1' then
862
        if reset='1' then
863
            ws_ctr <= (others => '0');
864
        else
865
            if load_ws_ctr='1' then
866
                ws_ctr <= ws_value;
867
            elsif ws_wait_done='0' then
868
                ws_ctr <= ws_ctr - 1;
869
            end if;
870
        end if;
871
    end if;
872
end process wait_state_counter_reg;
873
 
874
ws_wait_done <= '1' when ws_ctr="000" else '0';
875
 
876
--------------------------------------------------------------------------------
877
-- Refill word counters
878
 
879
code_refill_word_counter:
880
process(clk)
881
begin
882
    if clk'event and clk='1' then
883
        if reset='1' or (code_miss='1' and ps=idle) then
884
            code_refill_ctr <= LINE_SIZE-1;
885
        else
886
            if (ps=code_refill_bram_2 or
887
               ps=code_refill_sram_1 or
888
               ps=code_refill_sram8_3) and
889
               ws_wait_done='1'  and
890
               code_refill_ctr/=0 then
891 145 ja_rd
            code_refill_ctr <= code_refill_ctr-1; --  FIXME explain downcount
892 114 ja_rd
            end if;
893
        end if;
894
    end if;
895
end process code_refill_word_counter;
896
 
897 145 ja_rd
with ps select data_refill_end <=
898
    '1' when data_refill_bram_2,
899
    '1' when data_refill_sram_1,
900
    '1' when data_refill_sram8_3,
901
    '0' when others;
902
 
903
data_refill_word_counter:
904
process(clk)
905
begin
906
    if clk'event and clk='1' then
907
        if reset='1' or (data_miss='1' and ps=idle) then
908
            data_refill_ctr <= 0;
909
        else
910
            if data_refill_end='1' and ws_wait_done='1' then
911
                if data_refill_ctr=(LINE_SIZE-1) then
912
                    data_refill_ctr <= 0;
913
                else
914
                    data_refill_ctr <= data_refill_ctr + 1;
915
                end if;
916
            end if;
917
        end if;
918
    end if;
919
end process data_refill_word_counter;
920
 
921 114 ja_rd
--------------------------------------------------------------------------------
922
-- CPU interface registers and address decoding --------------------------------
923
 
924 145 ja_rd
data_refill_start <=
925
    '1' when ((ps=data_refill_sram_0 or ps=data_refill_sram8_0 or
926
            ps=data_refill_bram_0) and data_refill_ctr=0)
927
    else '0';
928 114 ja_rd
 
929
-- Everything coming and going to the CPU is registered, so that the CPU has
930
-- some timing marging. These are those registers.
931
-- Besides, we have here a couple of read/write pending flags used to properly
932
-- sequence the cache accesses (first fetch, then any pending r/w).
933
cpu_data_interface_registers:
934
process(clk)
935
begin
936
    if clk'event and clk='1' then
937
        if reset='1' then
938
            write_pending <= '0';
939
            read_pending <= '0';
940
            byte_we_reg <= "0000";
941
        else
942 145 ja_rd
            -- Raise 'read_pending' as soon as we know a read is to be done.
943
            -- Clear it as soon as the read/refill has STARTED. 
944
            -- Can be raised again after a read is started and before it's done.
945
            -- data_rd_addr_reg always has the addr of any pending read.
946 212 ja_rd
            if data_miss='1' then
947 114 ja_rd
                read_pending <= '1';
948
                data_rd_addr_reg <= data_addr(31 downto 2);
949 145 ja_rd
            elsif data_refill_start='1' or ps=data_read_io_0 or
950 114 ja_rd
                  ps=data_ignore_read then
951
                read_pending <= '0';
952
            end if;
953
 
954
            -- Raise 'write_pending' at the 1st cycle of a write, clear it when
955
            -- the write (writethrough actually) operation has been done.
956
            -- data_wr_addr_reg always has the addr of any pending write
957 212 ja_rd
            if byte_we/="0000" then
958 114 ja_rd
                byte_we_reg <= byte_we;
959
                data_wr_reg <= data_wr;
960
                data_wr_addr_reg <= data_addr(31 downto 2);
961
                write_pending <= '1';
962
            elsif ps=data_writethrough_sram_1b or
963
                  ps=data_write_io_0 or
964
                  ps=data_ignore_write then
965
                write_pending <= '0';
966
                byte_we_reg <= "0000";
967
            end if;
968
 
969
        end if;
970
    end if;
971
end process cpu_data_interface_registers;
972
 
973
cpu_code_interface_registers:
974
process(clk)
975
begin
976
    if clk'event and clk='1' then
977
        -- Register code fetch addresses only when they are valid; so that
978
        -- code_rd_addr_reg always holds the last fetch address.
979
        if code_rd_vma='1' then
980
            code_rd_addr_reg <= code_rd_addr;
981
        end if;
982
    end if;
983
end process cpu_code_interface_registers;
984
 
985
-- The code refill address is that of the current code line, with the running
986
-- refill counter appended: we will read all the words from the line in sequence
987
-- (in REVERSE sequence, actually, see below).
988
code_refill_addr <=
989
    code_rd_addr_reg(code_rd_addr_reg'high downto 4) &
990
    conv_std_logic_vector(code_refill_ctr,LINE_INDEX_SIZE);
991
 
992 145 ja_rd
data_refill_addr <=
993
    data_rd_addr_reg(data_rd_addr_reg'high downto 4) &
994
    conv_std_logic_vector(data_refill_ctr,LINE_INDEX_SIZE);
995 114 ja_rd
 
996 145 ja_rd
 
997
 
998 114 ja_rd
-- Address decoding ------------------------------------------------------------
999
 
1000
-- Decoding is done on the high bits of the address only, there'll be mirroring.
1001
-- Write to areas not explicitly decoded will be silently ignored. Reads will
1002
-- get undefined data.
1003
 
1004
code_rd_addr_mask <= code_rd_addr_reg(31 downto t_addr_decode'low);
1005
data_rd_addr_mask <= data_rd_addr_reg(31 downto t_addr_decode'low);
1006
data_wr_addr_mask <= data_wr_addr_reg(31 downto t_addr_decode'low);
1007
 
1008
 
1009
code_rd_attr <= decode_addr(code_rd_addr_mask);
1010
data_rd_attr <= decode_addr(data_rd_addr_mask);
1011
data_wr_attr <= decode_addr(data_wr_addr_mask);
1012
 
1013 134 ja_rd
-- Unmapped area access flag, raised for 1 cycle only after each wrong access
1014
with ps select unmapped <=
1015
    '1' when code_crash,
1016
    '1' when data_ignore_read,
1017
    '1' when data_ignore_write,
1018
    '0' when others;
1019 114 ja_rd
 
1020 145 ja_rd
 
1021 114 ja_rd
--------------------------------------------------------------------------------
1022
-- BRAM interface (BRAM is FPGA Block RAM)
1023
 
1024
-- BRAM address can come from code or data buses, we support code execution
1025
-- and data r/w from BRAM.
1026
-- (note both inputs to this mux are register outputs)
1027
bram_rd_addr <=
1028 145 ja_rd
    --data_rd_addr_reg(bram_rd_addr'high downto 2)
1029
    data_refill_addr(bram_rd_addr'high downto 2)
1030 114 ja_rd
        when ps=data_refill_bram_0 else
1031
    code_refill_addr(bram_rd_addr'high downto 2) ;
1032
 
1033
bram_data_rd_vma <= '1' when ps=data_refill_bram_1 else '0';
1034
 
1035
 
1036
--------------------------------------------------------------------------------
1037
--------------------------------------------------------------------------------
1038
-- Code cache
1039
 
1040
-- CPU is wired directly to cache output, no muxes -- or at least is SHOULD. 
1041 212 ja_rd
-- Due to some unknowk reason, if we omit this extra dummy layer of logic the 
1042
-- synth (Quartus-II) will fail to infer the tag table as a BRAM.
1043 114 ja_rd
-- (@note3)
1044
code_rd <= code_cache_rd when reset='0' else X"00000000";
1045
 
1046
-- Register here the requested code tag so we can compare it to the tag in the
1047
-- cache store. Note we register and match the 'line valid' bit together with
1048
-- the rest of the tag.
1049
code_tag_register:
1050
process(clk)
1051
begin
1052
    if clk'event and clk='1' then
1053
        -- Together with the tag value, we register the valid bit against which 
1054
        -- we will match after reading the tag table.
1055
        -- The valid bit will be '0' for normal accesses or '1' when the cache 
1056
        -- is disabled OR we're invalidating lines. This ensures that the cache
1057
        -- will miss in those cases.
1058
        code_tag_reg <= (ic_invalidate or (not cache_enable)) &
1059
                        code_tag(code_tag'high-1 downto 0);
1060
    end if;
1061
end process code_tag_register;
1062
 
1063
-- The I-Cache misses when the tag in the cache is not the tag we want or 
1064
-- it is not valid.
1065
code_miss_cached <= '1' when (code_tag_reg /= code_cache_tag) else '0';
1066
 
1067
-- When cache is disabled, ALL code fetches will miss
1068
uncached_code_miss_logic:
1069
process(clk)
1070
begin
1071
    if clk'event and clk='1' then
1072
        if reset='1' then
1073
            code_miss_uncached <= '0';
1074
        else
1075
            code_miss_uncached <= code_rd_vma; -- always miss
1076
        end if;
1077
    end if;
1078
end process uncached_code_miss_logic;
1079
 
1080
-- Select the proper code_miss signal
1081
code_miss <= code_miss_uncached when cache_enable='0' else code_miss_cached;
1082
 
1083
 
1084
-- Code line address used for both read and write into the table
1085
code_line_addr <=
1086
    -- when the CPU wants to invalidate I-Cache lines, the addr comes from the
1087
    -- data bus (see @note1)
1088
    data_wr(7 downto 0) when byte_we(3)='1' and ic_invalidate='1'
1089
    -- otherwise the addr comes from the code address as usual
1090
    else code_rd_addr(11 downto 4);
1091
 
1092
code_word_addr <= code_rd_addr(11 downto 2);
1093
code_word_addr_wr <= code_line_addr & conv_std_logic_vector(code_refill_ctr,LINE_INDEX_SIZE);
1094
-- NOTE: the tag will be marked as INVALID ('1') when the CPU is invalidating 
1095
-- code lines (@note1)
1096
code_tag <=
1097
    (ic_invalidate) &
1098
    code_rd_addr(31 downto 27) &
1099
    code_rd_addr(11+CODE_TAG_SIZE-5 downto 11+1);
1100
 
1101
 
1102
code_tag_memory:
1103
process(clk)
1104
begin
1105
    if clk'event and clk='1' then
1106
        if ps=code_refill_bram_1 or ps=code_refill_sram8_3 or ps=code_refill_sram_1 then
1107
            code_tag_table(conv_integer(code_line_addr)) <= code_tag;
1108
        end if;
1109
 
1110
        code_cache_tag <= code_tag_table(conv_integer(code_line_addr));
1111
    end if;
1112
end process code_tag_memory;
1113
 
1114
 
1115
code_line_memory:
1116
process(clk)
1117
begin
1118
    if clk'event and clk='1' then
1119
        if ps=code_refill_bram_1 or ps=code_refill_sram8_3 or ps=code_refill_sram_1 then
1120
            code_line_table(conv_integer(code_word_addr_wr)) <= code_refill_data;
1121
        end if;
1122
 
1123
        code_cache_rd <= code_line_table(conv_integer(code_word_addr));
1124
    end if;
1125
end process code_line_memory;
1126
 
1127
-- Code can only come from BRAM or SRAM (including 16- and 8- bit interfaces)
1128
with ps select code_refill_data <=
1129
    bram_rd_data    when code_refill_bram_1,
1130
    sram_rd_data    when others;
1131
 
1132
 
1133
--------------------------------------------------------------------------------
1134
--------------------------------------------------------------------------------
1135 145 ja_rd
-- Data cache (direct mapped, nearly identical to code cache)
1136 114 ja_rd
 
1137 145 ja_rd
 
1138
-- (@note3)
1139
with ps select data_rd <=
1140 114 ja_rd
    io_rd_data      when data_read_io_1,
1141
    data_cache_rd   when others;
1142
 
1143 145 ja_rd
-- Register here the requested data tag so we can compare it to the tag in the
1144
-- cache store. Note we register and match the 'line valid' bit together with
1145
-- the rest of the tag.
1146
data_tag_register:
1147
process(clk)
1148
begin
1149
    if clk'event and clk='1' then
1150
        -- Together with the tag value, we register the valid bit against which 
1151
        -- we will match after reading the tag table.
1152
        -- The valid bit will be '0' for normal accesses or '1' when the cache 
1153
        -- is disabled OR we're invalidating lines. This ensures that the cache
1154
        -- will miss in those cases.
1155
        data_tag_reg <= (ic_invalidate or (not cache_enable)) &
1156
                        data_tag(data_tag'high-1 downto data_tag'low);
1157
    end if;
1158
end process data_tag_register;
1159 114 ja_rd
 
1160 145 ja_rd
 
1161
-- The tags are 'compared' the cycle after data_rd_vma. 
1162
-- FIXME explain role of ic_invalidate in this.
1163
-- Note: writethroughs use the tag match result at a different moment.
1164
data_tag_comparison_validation:
1165 114 ja_rd
process(clk)
1166
begin
1167
    if clk'event and clk='1' then
1168
        if reset='1' then
1169 145 ja_rd
            data_tag_match_valid <= '0';
1170 114 ja_rd
        else
1171 145 ja_rd
            data_tag_match_valid <= data_rd_vma and not ic_invalidate;
1172 114 ja_rd
        end if;
1173
    end if;
1174 145 ja_rd
end process data_tag_comparison_validation;
1175 114 ja_rd
 
1176
 
1177 145 ja_rd
-- The D-Cache misses when the tag in the cache is not the tag we want or 
1178
-- it is not valid.
1179
 
1180 151 ja_rd
-- When we write to a line right before we read from it, we have a RAW data 
1181
-- hazard: the data cache will (usually) hit because the tag match will be done
1182
-- before the writethrough. To prevent this, we do an additional tag match.
1183
data_miss_by_invalidation <= '1' when
1184
    data_tag_match_valid='1' and update_data_tag='1' --and
1185
    -- FIXME skip additional tag match, it's too slow. Do later as registered
1186
    -- match and update state machine.
1187
    -- This means that a sequence SW + LW will ALWAYS produce a data miss,
1188
    -- even if the written lines are different. This needs fixing.
1189
--    data_tag_reg=data_tag
1190
    else '0';
1191
 
1192 145 ja_rd
-- When cache is disabled, assert 'miss' after vma 
1193
data_miss_uncached <= data_tag_match_valid and not ic_invalidate;
1194
-- When cache is enabled, assert 'miss' after the comparison is done.
1195
data_tags_match <= '1' when (data_tag_reg = data_cache_tag) else '0';
1196 151 ja_rd
data_miss_cached <= '1' when
1197
    (data_tag_match_valid='1' and data_tags_match='0') or
1198
    data_miss_by_invalidation='1'
1199
    else '0';
1200 145 ja_rd
 
1201 212 ja_rd
-- Select the proper data_miss source with a mux
1202 145 ja_rd
data_miss <= data_miss_uncached when cache_enable='0' else data_miss_cached;
1203
 
1204
 
1205 212 ja_rd
-- Data line address used for both read and write into the table
1206 145 ja_rd
data_line_addr <=
1207 212 ja_rd
    -- When the CPU wants to invalidate D-Cache lines, the addr comes from the
1208 145 ja_rd
    -- data bus (see @note1)
1209
    data_wr(7 downto 0) when byte_we(3)='1' and ic_invalidate='1'
1210
    -- otherwise the addr comes from the code address as usual
1211
    else data_addr(11 downto 4);
1212
 
1213
data_word_addr <= data_addr(11 downto 2);
1214
data_word_addr_wr <= data_line_addr & conv_std_logic_vector(data_refill_ctr,LINE_INDEX_SIZE);
1215
-- NOTE: the tag will be marked as INVALID ('1') when the CPU is invalidating 
1216
-- code lines (@note1)
1217 212 ja_rd
-- FIXME explain role of ic_invalidate in this logic
1218 145 ja_rd
data_tag <=
1219
    (ic_invalidate or not data_tag_match_valid) &
1220
    data_addr(31 downto 27) &
1221
    data_addr(11+DATA_TAG_SIZE-5 downto 11+1);
1222
 
1223
-- The data tag table will be written to...
1224
update_data_tag <= '1' when
1225
    -- ...when a refill word is read (redundant writes) or...
1226
    (ps=data_refill_sram8_3 or ps=data_refill_sram_1 or ps=data_refill_bram_1) or
1227
    -- ...when writing through a line which is cached or...
1228
    (ps=data_writethrough_sram_0a and data_tags_match='1') or
1229
    -- ...when a D-Cache line invalidation access is made
1230
    (data_rd_vma='1' and ic_invalidate='1')
1231
    else '0';
1232
 
1233
data_tag_memory:
1234
process(clk)
1235
begin
1236
    if clk'event and clk='1' then
1237
        if update_data_tag='1' then
1238
            data_tag_table(conv_integer(data_line_addr)) <= data_tag;
1239
        end if;
1240
 
1241
        data_cache_tag <= data_tag_table(conv_integer(data_line_addr));
1242
    end if;
1243
end process data_tag_memory;
1244
 
1245
 
1246
update_data_line <= '1' when ps=data_refill_sram8_3 or ps=data_refill_sram_1 or ps=data_refill_bram_1
1247
                    else '0';
1248
 
1249
data_line_memory:
1250
process(clk)
1251
begin
1252
    if clk'event and clk='1' then
1253
        if update_data_line='1' then
1254
            --assert 1=0
1255
            --report "D-Cache["& str(conv_integer(data_word_addr_wr),10) & "] = 0x"& hstr(data_refill_data)
1256
            --severity note;
1257
            data_line_table(conv_integer(data_word_addr_wr)) <= data_refill_data;
1258
        end if;
1259
 
1260
        data_cache_rd <= data_line_table(conv_integer(data_word_addr));
1261
    end if;
1262
end process data_line_memory;
1263
 
1264
-- Data can only come from SRAM (including 16- and 8- bit interfaces)
1265
with ps select data_refill_data <=
1266
    bram_rd_data    when data_refill_bram_1,
1267
    sram_rd_data    when others;
1268
 
1269 212 ja_rd
------------------------------------------------------------------------------
1270 114 ja_rd
--------------------------------------------------------------------------------
1271
-- SRAM interface
1272
 
1273
-- Note this signals are meant to be connected directly to FPGA pins (and then
1274
-- to a SRAM, of course). They are the only signals whose tco we care about.
1275
 
1276
-- FIXME should add a SRAM CE\ signal
1277
 
1278
-- SRAM address bus (except for LSB) comes from cpu code or data addr registers
1279
 
1280
sram_address(sram_address'high downto 2) <=
1281 151 ja_rd
    data_refill_addr(sram_address'high downto 2)
1282 114 ja_rd
        when   (ps=data_refill_sram_0  or ps=data_refill_sram_1 or
1283
                ps=data_refill_sram8_0 or ps=data_refill_sram8_1 or
1284
                ps=data_refill_sram8_2 or ps=data_refill_sram8_3) else
1285
    code_refill_addr(sram_address'high downto 2)
1286
        when   (ps=code_refill_sram_0  or ps=code_refill_sram_1 or
1287
                ps=code_refill_sram8_0 or ps=code_refill_sram8_1 or
1288
                ps=code_refill_sram8_2 or ps=code_refill_sram8_3) else
1289
    data_wr_addr_reg(sram_address'high downto 2);
1290
 
1291
-- SRAM addr bus LSB depends on the D-cache state because we read/write the
1292
-- halfwords sequentially in successive cycles.
1293
sram_address(1) <=
1294
    '0'     when   (ps=data_writethrough_sram_0a or
1295
                    ps=data_writethrough_sram_0b or
1296
                    ps=data_writethrough_sram_0c or
1297
                    ps=data_refill_sram8_0 or
1298
                    ps=data_refill_sram8_1 or
1299
                    ps=data_refill_sram_0 or
1300
                    ps=code_refill_sram8_0 or
1301
                    ps=code_refill_sram8_1 or
1302
                    ps=code_refill_sram_0) else
1303
    '1'     when   (ps=data_writethrough_sram_1a or
1304
                    ps=data_writethrough_sram_1b or
1305
                    ps=data_writethrough_sram_1c or
1306
                    ps=data_refill_sram8_2 or
1307
                    ps=data_refill_sram8_3 or
1308
                    ps=data_refill_sram_1 or
1309
                    ps=code_refill_sram8_2 or
1310
                    ps=code_refill_sram8_3 or
1311
                    ps=code_refill_sram_1)
1312
    else '0';
1313
 
1314
-- The lowest addr bit will only be used when accessing byte-wide memory, and
1315
-- even when we're reading word-aligned code (because we need to read the four 
1316
-- bytes one by one).
1317
sram_address(0) <=
1318
    '0'     when (ps=data_refill_sram8_0 or ps=data_refill_sram8_2 or
1319
                  ps=code_refill_sram8_0 or ps=code_refill_sram8_2) else
1320
    '1';
1321
 
1322
 
1323
-- SRAM databus (when used for output) comes from either hword of the data
1324
-- write register.
1325
with ps select sram_data_wr <=
1326
    data_wr_reg(31 downto 16)   when data_writethrough_sram_0a,
1327
    data_wr_reg(31 downto 16)   when data_writethrough_sram_0b,
1328
    data_wr_reg(31 downto 16)   when data_writethrough_sram_0c,
1329
    data_wr_reg(15 downto  0)   when data_writethrough_sram_1a,
1330
    data_wr_reg(15 downto  0)   when data_writethrough_sram_1b,
1331
    data_wr_reg(15 downto  0)   when data_writethrough_sram_1c,
1332
    (others => 'Z')             when others;
1333
 
1334
-- The byte_we is split in two similarly.
1335
with ps select sram_byte_we_n <=
1336
    not byte_we_reg(3 downto 2) when data_writethrough_sram_0b,
1337
    not byte_we_reg(1 downto 0) when data_writethrough_sram_1b,
1338
    "11"                        when others;
1339
 
1340
-- SRAM OE\ is only asserted low for read cycles
1341
sram_oe_n <=
1342
    '0' when   (ps=data_refill_sram_0  or ps=data_refill_sram_1 or
1343
                ps=data_refill_sram8_0 or ps=data_refill_sram8_1 or
1344
                ps=data_refill_sram8_2 or ps=data_refill_sram8_3 or
1345
                ps=code_refill_sram_0  or ps=code_refill_sram_1 or
1346
                ps=code_refill_sram8_0 or ps=code_refill_sram8_1 or
1347
                ps=code_refill_sram8_2 or ps=code_refill_sram8_3) else
1348
    '1';
1349
 
1350
-- When reading from the SRAM, read word comes from read hword register and
1351
-- SRAM bus (read register is loaded in previous cycle).
1352
sram_rd_data <=
1353
    sram_rd_data_reg & sram_data_rd(7 downto 0)
1354
            when ps=data_refill_sram8_3 or ps=code_refill_sram8_3 else
1355
    sram_rd_data_reg(31 downto 16) & sram_data_rd;
1356
 
1357
sram_input_halfword_register:
1358
process(clk)
1359
begin
1360
    if clk'event and clk='1' then
1361
        if ps=data_refill_sram_0 or ps=code_refill_sram_0 then
1362
            sram_rd_data_reg(31 downto 16) <= sram_data_rd;
1363
        elsif ps=data_refill_sram8_0 or ps=code_refill_sram8_0 then
1364
            sram_rd_data_reg(31 downto 24) <= sram_data_rd(7 downto 0);
1365
        elsif ps=data_refill_sram8_1 or ps=code_refill_sram8_1 then
1366
            sram_rd_data_reg(23 downto 16) <= sram_data_rd(7 downto 0);
1367
        elsif ps=data_refill_sram8_2 or ps=code_refill_sram8_2 then
1368
            sram_rd_data_reg(15 downto  8) <= sram_data_rd(7 downto 0);
1369
        end if;
1370
    end if;
1371
end process sram_input_halfword_register;
1372
 
1373
 
1374
--------------------------------------------------------------------------------
1375
-- I/O interface -- IO is assumed to behave like synchronous memory
1376
 
1377
io_byte_we <= byte_we_reg when ps=data_write_io_0 else "0000";
1378
io_rd_addr <= data_rd_addr_reg;
1379
io_wr_addr <= data_wr_addr_reg;
1380
io_wr_data <= data_wr_reg;
1381
io_rd_vma <= '1' when ps=data_read_io_0 else '0';
1382
 
1383
 
1384
--------------------------------------------------------------------------------
1385
-- CPU stall control
1386
 
1387
-- Stall the CPU when either state machine needs it
1388
mem_wait <=
1389
    (code_wait or data_wait or  -- code or data refill in course
1390
     code_miss or data_miss     -- code or data miss
1391
     ) and not reset; -- FIXME stub
1392
 
1393
-- Assert code_wait until the cycle where the CPU has valid code word on its
1394
-- code bus
1395
with ps select code_wait <=
1396
    '1' when code_refill_bram_0,
1397
    '1' when code_refill_bram_1,
1398
    '1' when code_refill_bram_2,
1399
    '1' when code_refill_sram_0,
1400
    '1' when code_refill_sram_1,
1401
    '1' when code_refill_sram8_0,
1402
    '1' when code_refill_sram8_1,
1403
    '1' when code_refill_sram8_2,
1404
    '1' when code_refill_sram8_3,
1405
    '0' when others;
1406
 
1407
-- Assert data_wait until the cycle where the CPU has valid data word on its
1408
-- code bus AND no other operations are ongoing that may use the external buses.
1409
with ps select data_wait <=
1410
    '1' when data_writethrough_sram_0a,
1411
    '1' when data_writethrough_sram_0b,
1412
    '1' when data_writethrough_sram_0c,
1413
    '1' when data_writethrough_sram_1a,
1414
    '1' when data_writethrough_sram_1b,
1415
    '1' when data_writethrough_sram_1c,
1416
    '1' when data_refill_sram_0,
1417
    '1' when data_refill_sram_1,
1418
    '1' when data_refill_sram8_0,
1419
    '1' when data_refill_sram8_1,
1420
    '1' when data_refill_sram8_2,
1421
    '1' when data_refill_sram8_3,
1422
    '1' when data_refill_bram_0,
1423
    '1' when data_refill_bram_1,
1424 145 ja_rd
    '1' when data_refill_bram_2,
1425 114 ja_rd
    '1' when data_read_io_0,
1426 212 ja_rd
    -- In any other state, stall CPU only if there's a RD/WR pending.
1427
    read_pending or write_pending when others;
1428 114 ja_rd
 
1429 212 ja_rd
 
1430 242 ja_rd
    -- The cache will be ready only after the first code refill.
1431
    -- This will prevent the CPU from loading junk into the IR.
1432
    with ps select cache_ready <=
1433
        '0' when cache_reset,
1434
        '1' when others;
1435
 
1436
 
1437 114 ja_rd
end architecture direct;

powered by: WebSVN 2.1.0

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