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

Subversion Repositories ion

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

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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