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

Subversion Repositories ion

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

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

powered by: WebSVN 2.1.0

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