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

Subversion Repositories ion

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

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

powered by: WebSVN 2.1.0

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