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

Subversion Repositories ion

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

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

powered by: WebSVN 2.1.0

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