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

Subversion Repositories ion

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

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

powered by: WebSVN 2.1.0

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