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

Subversion Repositories ion

[/] [ion/] [trunk/] [vhdl/] [mips_cache_stub.vhdl] - Blame information for rev 64

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

Line No. Rev Author Line
1 42 ja_rd
--------------------------------------------------------------------------------
2 46 ja_rd
-- mips_cache_stub.vhdl -- 1-word cache module
3 42 ja_rd
--
4 46 ja_rd
-- This module has the same interface and logic as a real cache but the cache
5
-- memory is just 1 word for each of code and data.
6 43 ja_rd
--
7 46 ja_rd
-- It interfaces the CPU to the following:
8 42 ja_rd
--
9 46 ja_rd
--  1.- Internal 32-bit-wide BRAM for read only
10 42 ja_rd
--  2.- Internal 32-bit I/O bus
11
--  3.- External 16-bit wide SRAM
12
--
13 46 ja_rd
-- The SRAM memory interface signals are meant to connect directly to FPGA pins
14
-- and all outputs are registered (tco should be minimal).
15
-- SRAM data inputs are NOT registered, though. They go through a couple muxes
16
-- before reaching the first register so watch out for tsetup.
17
-- The SRAM is assumed to be fast enough to read or write in a clock cycle.
18 42 ja_rd
--
19 46 ja_rd
-- Obviously this module provides no performance gain; on the contrary, by
20
-- coupling the CPU to slow external memory (16 bit bus) it actually slows it
21
-- down. The purpose of this module is just to test the SRAM interface and the
22
-- cache logic and timing.
23
--
24 42 ja_rd
--------------------------------------------------------------------------------
25 58 ja_rd
-- External FPGA signals
26
--
27
-- This module has signals meant to connect directly to FPGA pins: the SRAM
28
-- interface. They are either direct register outputs or at most with an 
29
-- intervening 2-mux, in order to minimize the Tco (clock-to-output).
30
--
31
-- The Tco of these signals has to be accounted for in the real SRAM interface.
32
-- For example, under Quartus-2 and with a Cyclone-2 grade -7 device, the
33
-- worst Tco for the SRAM data pins is below 5 ns, enough to use a 10ns SRAM
34
-- with a 20 ns clock cycle.
35
-- Anyway, you need to take care of this yourself.
36
--
37
--------------------------------------------------------------------------------
38
-- Interface to CPU
39
--
40
-- 1.- All signals coming from the CPU are registered.
41
-- 2.- All CPU inputs come directly from a register, or at most have a 2-mux in
42
--     between.
43
-- 
44
-- This means this block will not degrade the timing performance of the system, 
45
-- as long as its logic is shallower than the current bottleneck (the ALU).
46
--
47
--------------------------------------------------------------------------------
48 46 ja_rd
-- KNOWN TROUBLE:
49
-- 
50 58 ja_rd
-- Apart from the very rough looks of the code, there's a few known problems:
51 46 ja_rd
--
52
-- 1.- Access to unmapped areas wil crash the CPU
53
--      A couple states are missing in the state machine for handling accesses 
54
--      to unmapped areas. I haven't yet decided how to handle that (return 
55
--      zero, trigger trap, mirror another mapped area...)
56 58 ja_rd
-- 2.- Code refills from SRAM is unimplemented yet
57
--      To be done for sheer lack of time.
58
-- 3.- Address decoding is hardcoded in mips_pkg
59 46 ja_rd
--      It should be done here using module generics and not package constants.
60 58 ja_rd
-- 4.- Does not work as a real 1-word cache yet
61 46 ja_rd
--      That functionality is still missing, all accesses 'miss'. It should be
62
--      implemented, as a way to test the real cache logic on a small scale.
63
--
64
--------------------------------------------------------------------------------
65 42 ja_rd
 
66
library ieee;
67
use ieee.std_logic_1164.all;
68
use ieee.std_logic_arith.all;
69
use ieee.std_logic_unsigned.all;
70
use work.mips_pkg.all;
71
 
72 58 ja_rd
 
73 42 ja_rd
entity mips_cache_stub is
74
    generic (
75
        BRAM_ADDR_SIZE : integer := 10;
76
        SRAM_ADDR_SIZE : integer := 17
77
    );
78
    port(
79
        clk             : in std_logic;
80
        reset           : in std_logic;
81 46 ja_rd
 
82 42 ja_rd
        -- Interface to CPU core
83
        data_rd_addr    : in std_logic_vector(31 downto 0);
84
        data_rd         : out std_logic_vector(31 downto 0);
85
        data_rd_vma     : in std_logic;
86 46 ja_rd
 
87 42 ja_rd
        code_rd_addr    : in std_logic_vector(31 downto 2);
88
        code_rd         : out std_logic_vector(31 downto 0);
89
        code_rd_vma     : in std_logic;
90 46 ja_rd
 
91 42 ja_rd
        data_wr_addr    : in std_logic_vector(31 downto 2);
92
        byte_we         : in std_logic_vector(3 downto 0);
93
        data_wr         : in std_logic_vector(31 downto 0);
94 46 ja_rd
 
95 42 ja_rd
        mem_wait        : out std_logic;
96 46 ja_rd
        cache_enable    : in std_logic;
97
 
98 42 ja_rd
        -- interface to FPGA i/o devices
99
        io_rd_data      : in std_logic_vector(31 downto 0);
100
        io_rd_addr      : out std_logic_vector(31 downto 2);
101
        io_wr_addr      : out std_logic_vector(31 downto 2);
102
        io_wr_data      : out std_logic_vector(31 downto 0);
103
        io_rd_vma       : out std_logic;
104
        io_byte_we      : out std_logic_vector(3 downto 0);
105 46 ja_rd
 
106 42 ja_rd
        -- interface to synchronous 32-bit-wide FPGA BRAM (possibly used as ROM)
107
        bram_rd_data    : in std_logic_vector(31 downto 0);
108
        bram_wr_data    : out std_logic_vector(31 downto 0);
109
        bram_rd_addr    : out std_logic_vector(BRAM_ADDR_SIZE+1 downto 2);
110
        bram_wr_addr    : out std_logic_vector(BRAM_ADDR_SIZE+1 downto 2);
111 46 ja_rd
        bram_byte_we    : out std_logic_vector(3 downto 0);
112
        bram_data_rd_vma: out std_logic;
113
 
114 42 ja_rd
        -- interface to asynchronous 16-bit-wide EXTERNAL SRAM
115 46 ja_rd
        sram_address    : out std_logic_vector(SRAM_ADDR_SIZE downto 1);
116 42 ja_rd
        sram_databus    : inout std_logic_vector(15 downto 0);
117
        sram_byte_we_n  : out std_logic_vector(1 downto 0);
118
        sram_oe_n       : out std_logic
119
    );
120
end entity mips_cache_stub;
121
 
122
 
123
 
124
architecture stub of mips_cache_stub is
125
 
126 58 ja_rd
-- state machines: definition of states -----------------------------
127
 
128 46 ja_rd
type t_code_cache_state is (
129 58 ja_rd
    code_normal,                -- 
130
    code_wait_for_dcache,       -- wait for D-cache to stop using the buses
131 46 ja_rd
 
132
    code_refill_bram_0,         -- pc in bram_rd_addr
133
    code_refill_bram_1,         -- op in bram_rd
134 58 ja_rd
    code_refill_bram_2,         -- op in code_rd 
135 46 ja_rd
 
136 58 ja_rd
    code_refill_sram_0,         -- FIXME code refill from SRAM unimplemented
137 46 ja_rd
    code_refill_sram_1,
138
    code_refill_sram_2,
139
 
140 58 ja_rd
    code_bug                    -- caught an error in the state machine
141 46 ja_rd
   );
142
 
143 58 ja_rd
-- I-cache state machine state register & next state
144 46 ja_rd
signal cps, cns :           t_code_cache_state;
145
 
146
 
147
type t_data_cache_state is (
148
    data_normal,
149
 
150 58 ja_rd
    data_refill_sram_0,         -- rd addr in SRAM addr bus (low hword)
151
    data_refill_sram_1,         -- rd addr in SRAM addr bus (high hword)
152 46 ja_rd
 
153
    data_refill_bram_0,         -- rd addr in bram_rd_addr
154
    data_refill_bram_1,         -- rd data in bram_rd_data
155 42 ja_rd
 
156 46 ja_rd
    data_read_io_0,             -- rd addr on io_rd_addr, io_vma active
157
    data_read_io_1,             -- rd data on io_rd_data
158 58 ja_rd
 
159
    data_write_io_0,            -- wr addr & data in io_wr_*, io_byte_we active
160 46 ja_rd
 
161 58 ja_rd
    data_writethrough_sram_0,   -- wr addr & data in SRAM buses (low hword)
162
    data_writethrough_sram_1,   -- wr addr & data in SRAM buses (high hword)
163 42 ja_rd
 
164 58 ja_rd
    data_ignore_write,          -- hook for raising error flag FIXME untested
165 42 ja_rd
 
166 58 ja_rd
    data_bug                    -- caught an error in the state machine
167 42 ja_rd
   );
168
 
169
 
170 58 ja_rd
-- D-cache state machine state register & next state
171 46 ja_rd
signal dps, dns :           t_data_cache_state;
172 42 ja_rd
 
173 58 ja_rd
-- CPU interface registers ------------------------------------------
174
signal data_rd_addr_reg :   t_pc;
175
signal data_wr_addr_reg :   t_pc;
176
signal code_rd_addr_reg :   t_pc;
177 46 ja_rd
 
178 42 ja_rd
signal data_wr_reg :        std_logic_vector(31 downto 0);
179
signal byte_we_reg :        std_logic_vector(3 downto 0);
180
 
181 58 ja_rd
-- SRAM interface ---------------------------------------------------
182
-- Stores first (high) HW read from SRAM
183
signal sram_rd_data_reg :   std_logic_vector(31 downto 16);
184
-- Data read from SRAM, valid in refill_1
185
signal sram_rd_data :       t_word;
186 46 ja_rd
 
187
 
188 58 ja_rd
 
189
-- I-cache -- most of this is unimplemented -------------------------
190
 
191 46 ja_rd
subtype t_code_tag is std_logic_vector(23 downto 2);
192
signal code_cache_tag :     t_code_tag;
193
signal code_cache_tag_store : t_code_tag;
194
signal code_cache_store :   t_word;
195 58 ja_rd
-- code word read from cache
196 46 ja_rd
signal code_cache_rd :      t_word;
197 58 ja_rd
-- raised whel code_cache_rd is not valid due to a cache miss
198 46 ja_rd
signal code_miss :          std_logic;
199
 
200 58 ja_rd
-- '1' when the I-cache state machine stalls the pipeline (mem_wait)
201
signal code_wait :          std_logic;
202 46 ja_rd
 
203 58 ja_rd
-- D-cache -- most of this is unimplemented -------------------------
204 46 ja_rd
subtype t_data_tag is std_logic_vector(23 downto 2);
205
signal data_cache_tag :     t_data_tag;
206
signal data_cache_tag_store : t_data_tag;
207
signal data_cache_store :   t_word;
208 58 ja_rd
-- active when there's a write waiting to be done
209 46 ja_rd
signal write_pending :      std_logic;
210 58 ja_rd
-- active when there's a read waiting to be done
211 46 ja_rd
signal read_pending :       std_logic;
212 58 ja_rd
-- data word read from cache
213 46 ja_rd
signal data_cache_rd :      t_word;
214 58 ja_rd
-- '1' when data_cache_rd is not valid due to a cache miss
215 46 ja_rd
signal data_miss :          std_logic;
216
 
217 58 ja_rd
-- '1' when the D-cache state machine stalls the pipeline (mem_wait)
218 46 ja_rd
signal data_wait :          std_logic;
219
 
220
 
221 58 ja_rd
-- Address decoding -------------------------------------------------
222
 
223
-- Address slices used to decode
224 46 ja_rd
signal code_rd_addr_mask :  t_addr_decode;
225
signal data_rd_addr_mask :  t_addr_decode;
226
signal data_wr_addr_mask :  t_addr_decode;
227
 
228 58 ja_rd
-- Memory map area being accessed for each of the 3 buses:
229
-- 00 -> BRAM (read only)
230
-- 01 -> SRAM
231
-- 10 -> IO
232
-- 11 -> Unmapped
233 46 ja_rd
 
234 64 ja_rd
signal code_rd_attr :       t_range_attr;
235
signal data_rd_attr :       t_range_attr;
236
signal data_wr_attr :       t_range_attr;
237
signal code_rd_type :       t_memory_type;
238
signal data_rd_type :       t_memory_type;
239
signal data_wr_type :       t_memory_type;
240 58 ja_rd
 
241
 
242 64 ja_rd
 
243 42 ja_rd
begin
244
 
245 58 ja_rd
--------------------------------------------------------------------------------
246
-- Cache control state machines 
247 42 ja_rd
 
248 46 ja_rd
cache_state_machine_regs:
249 42 ja_rd
process(clk)
250
begin
251
   if clk'event and clk='1' then
252
        if reset='1' then
253 46 ja_rd
            cps <= code_normal;
254
            dps <= data_normal;
255 42 ja_rd
        else
256 46 ja_rd
            cps <= cns;
257
            dps <= dns;
258 42 ja_rd
        end if;
259
    end if;
260 46 ja_rd
end process cache_state_machine_regs;
261 42 ja_rd
 
262 64 ja_rd
-- (The code state machine occasionally 'waits' for the D-cache)
263 46 ja_rd
code_state_machine_transitions:
264 64 ja_rd
process(cps, dps, code_rd_vma, code_miss, code_rd_type,
265 58 ja_rd
        write_pending, read_pending)
266 42 ja_rd
begin
267 46 ja_rd
    case cps is
268
    when code_normal =>
269 64 ja_rd
        -- FIXME wrong logic, these signals are not active in the same cycle
270 58 ja_rd
        if code_rd_vma='1' and code_miss='1' and
271
           read_pending='0' and write_pending='0' then
272
            cns <= code_refill_bram_0; -- FIXME check memory area, SRAM!
273 42 ja_rd
        else
274 46 ja_rd
            cns <= cps;
275 42 ja_rd
        end if;
276
 
277 46 ja_rd
    when code_refill_bram_0 =>
278
        cns <= code_refill_bram_1;
279 42 ja_rd
 
280 46 ja_rd
    when code_refill_bram_1 =>
281
        cns <= code_refill_bram_2;
282 42 ja_rd
 
283 46 ja_rd
    when code_refill_bram_2 =>
284
        if dps/=data_normal and read_pending='0' and write_pending='0' then
285
            cns <= code_wait_for_dcache;
286 42 ja_rd
        else
287 46 ja_rd
            cns <= code_normal;
288 42 ja_rd
        end if;
289
 
290 46 ja_rd
    when code_wait_for_dcache =>
291
        -- if D-cache is busy, wait for it to become idle
292
        if dps/=data_normal then
293
            cns <= cps;
294
        elsif code_miss='1' then
295
            cns <= code_refill_bram_1; -- FIXME check memory area
296 42 ja_rd
        else
297 46 ja_rd
            cns <= code_normal;
298 42 ja_rd
        end if;
299
 
300 58 ja_rd
    when code_bug =>
301
        -- Something weird happened, we have 1 cycle to do something like raise
302
        -- an error flag, etc. After 1 cycle, back to normal.
303 46 ja_rd
        cns <= code_normal;
304
 
305 42 ja_rd
    when others =>
306 58 ja_rd
        -- We should never arrive here. If we do we handle it in state code_bug.
307 46 ja_rd
        cns <= code_bug;
308 42 ja_rd
    end case;
309 46 ja_rd
end process code_state_machine_transitions;
310 42 ja_rd
 
311
 
312 58 ja_rd
-- This state machine does not overlap IO/BRAM/SRAM accesses for simplicity.
313
 
314 46 ja_rd
data_state_machine_transitions:
315 64 ja_rd
process(dps, write_pending, read_pending, data_rd_type, data_wr_type)
316 46 ja_rd
begin
317
    case dps is
318
    when data_normal =>
319
        if write_pending='1' then
320 64 ja_rd
            case data_wr_type is
321
            when MT_BRAM        => dns <= data_ignore_write;
322
            when MT_SRAM_16B    => dns <= data_writethrough_sram_0;
323
            when MT_IO_SYNC     => dns <= data_write_io_0;
324
            when others         => dns <= dps; -- ignore write to undecoded area
325 46 ja_rd
            end case;
326
 
327
        elsif read_pending='1' then
328 64 ja_rd
            case data_rd_type is
329
            when MT_BRAM        => dns <= data_refill_bram_0;
330
            when MT_SRAM_16B    => dns <= data_refill_sram_0;
331
            when MT_IO_SYNC     => dns <= data_read_io_0;
332
            when others         => dns <= dps; -- ignore read from undec. area
333 46 ja_rd
                           -- FIXME should raise debug flag 
334
            end case;
335
        else
336
            dns <= dps;
337
        end if;
338 42 ja_rd
 
339 46 ja_rd
    when data_write_io_0 =>
340
        dns <= data_normal;
341
 
342
    when data_read_io_0 =>
343
        dns <= data_read_io_1;
344
 
345
    when data_read_io_1 =>
346
        dns <= data_normal;
347 42 ja_rd
 
348 46 ja_rd
    when data_refill_sram_0 =>
349
        dns <= data_refill_sram_1;
350 42 ja_rd
 
351 46 ja_rd
    when data_refill_sram_1 =>
352
        dns <= data_normal;
353 42 ja_rd
 
354 46 ja_rd
    when data_refill_bram_0 =>
355
        dns <= data_refill_bram_1;
356
 
357
    when data_refill_bram_1 =>
358
        dns <= data_normal;
359
 
360
    when data_writethrough_sram_0 =>
361
        dns <= data_writethrough_sram_1;
362
 
363
    when data_writethrough_sram_1 =>
364
        dns <= data_normal;
365
 
366
    when data_ignore_write =>
367
        dns <= data_normal;
368
 
369
    when data_bug =>
370 58 ja_rd
        -- Something weird happened, we have 1 cycle to do something like raise
371
        -- an error flag, etc. After 1 cycle, back to normal.    
372 46 ja_rd
        dns <= data_normal;
373
 
374
    when others =>
375 58 ja_rd
        -- Should never arrive here. If we do, we handle it in state data_bug.
376 46 ja_rd
        dns <= data_bug;
377
    end case;
378
end process data_state_machine_transitions;
379
 
380
 
381
--------------------------------------------------------------------------------
382
-- CPU interface registers and address decoding --------------------------------
383
 
384
 
385
-- Everything coming and going to the CPU is registered, so that the CPU has
386
-- some timing marging.
387
 
388 58 ja_rd
cpu_data_interface_registers:
389 42 ja_rd
process(clk)
390
begin
391
    if clk'event and clk='1' then
392
        if reset='1' then
393 46 ja_rd
            write_pending <= '0';
394
            read_pending <= '0';
395
            byte_we_reg <= "0000";
396 42 ja_rd
        else
397 46 ja_rd
            -- Raise 'read_pending' at the 1st cycle of a read, clear it when
398
            -- the read (and/or refill) operation has been done.
399
            -- data_rd_addr_reg always has the addr of any pending read
400 42 ja_rd
            if data_rd_vma='1' then
401 46 ja_rd
                read_pending <= '1';
402
                data_rd_addr_reg <= data_rd_addr(31 downto 2);
403
            elsif dps=data_refill_sram_1 or
404
                  dps=data_refill_bram_1 or
405
                  dps=data_read_io_0 then
406
                read_pending <= '0';
407 42 ja_rd
            end if;
408 46 ja_rd
 
409
            -- Raise 'write_pending' at the 1st cycle of a read, clear it when
410
            -- the write (writethrough actually) operation has been done.
411
            -- data_wr_addr_reg always has the addr of any pending write
412
            if byte_we/="0000" and dps=data_normal then
413
                byte_we_reg <= byte_we;
414
                data_wr_reg <= data_wr;
415
                data_wr_addr_reg <= data_wr_addr;
416
                write_pending <= '1';
417
            elsif dps=data_writethrough_sram_1 or
418
                  dps=data_write_io_0 or
419
                  dps=data_ignore_write then
420
                write_pending <= '0';
421
                byte_we_reg <= "0000";
422
            end if;
423 58 ja_rd
 
424
        end if;
425
    end if;
426
end process cpu_data_interface_registers;
427 46 ja_rd
 
428 58 ja_rd
cpu_code_interface_registers:
429
process(clk)
430
begin
431
    if clk'event and clk='1' then
432
        -- Register code fetch addresses only when they are valid; so that
433
        -- code_rd_addr_reg always holds the last fetch address.
434
        if (cps=code_normal and code_rd_vma='1') or
435
            cps=code_refill_bram_2 then -- FIXME explain this term
436
            code_rd_addr_reg <= code_rd_addr;
437 42 ja_rd
        end if;
438
    end if;
439 58 ja_rd
end process cpu_code_interface_registers;
440 42 ja_rd
 
441 58 ja_rd
 
442 46 ja_rd
-- Address decoding ------------------------------------------------------------
443 42 ja_rd
 
444 46 ja_rd
-- Decoding is done on the high bits of the address only, there'll be mirroring.
445
-- Write to areas not explicitly decoded will be silently ignored. Reads will
446
-- get undefined data.
447
 
448
code_rd_addr_mask <= code_rd_addr_reg(31 downto t_addr_decode'low);
449
data_rd_addr_mask <= data_rd_addr_reg(31 downto t_addr_decode'low);
450
data_wr_addr_mask <= data_wr_addr_reg(31 downto t_addr_decode'low);
451
 
452
 
453 64 ja_rd
code_rd_attr <= decode_addr(code_rd_addr_mask);
454
code_rd_type <= code_rd_attr(6 downto 4);
455 46 ja_rd
 
456 64 ja_rd
data_rd_attr <= decode_addr(data_rd_addr_mask);
457
data_rd_type <= data_rd_attr(6 downto 4);
458 46 ja_rd
 
459 64 ja_rd
data_wr_attr <= decode_addr(data_wr_addr_mask);
460
data_wr_type <= data_wr_attr(6 downto 4);
461 46 ja_rd
 
462 64 ja_rd
 
463
--with code_rd_addr_mask select code_rd_type <=
464
--    "000"   when ADDR_BOOT,
465
--    "001"   when ADDR_XRAM,
466
--    "011"   when others;
467
--
468
--with data_rd_addr_mask select data_rd_type <=
469
--    "000"   when ADDR_BOOT,
470
--    "001"   when ADDR_XRAM,
471
--    "010"   when ADDR_IO,
472
--    "011"   when others;
473
--
474
--with data_wr_addr_mask select data_wr_type <=
475
--    "001"   when ADDR_XRAM,
476
--    "010"   when ADDR_IO,
477
--    "011"   when others;
478
 
479 46 ja_rd
--------------------------------------------------------------------------------
480 58 ja_rd
-- BRAM interface
481 46 ja_rd
 
482 58 ja_rd
 
483
-- BRAMm address can come from code or data buses
484
-- (note both inputs to this mux are register outputs)
485
bram_rd_addr <=
486
    data_rd_addr_reg(bram_rd_addr'high downto 2) when dps=data_refill_bram_0
487 46 ja_rd
    else code_rd_addr_reg(bram_rd_addr'high downto 2) ;
488
 
489 58 ja_rd
bram_data_rd_vma <= '1' when dps=data_refill_bram_1 else '0';
490
 
491
 
492
 
493
--------------------------------------------------------------------------------
494
-- Code cache 
495
 
496
-- All the tag match logic is unfinished and will be simplified away in synth.
497
 
498
-- CPU is wired directly to cache output, no muxes
499 46 ja_rd
code_rd <= code_cache_rd;
500
 
501
-- FIXME Actual 1-word cache functionality is unimplemented yet
502 58 ja_rd
code_miss <= '1'; -- always miss
503 46 ja_rd
 
504
-- Read cache code and tag from code store
505
code_cache_rd <= code_cache_store;
506
code_cache_tag <= code_cache_tag_store;
507
 
508
code_cache_memory:
509 42 ja_rd
process(clk)
510
begin
511
    if clk'event and clk='1' then
512 46 ja_rd
 
513
 
514 42 ja_rd
        if reset='1' then
515 46 ja_rd
            -- in the real hardware the tag store can't be reset and it's up
516
            -- to the SW to initialize the cache.
517
            code_cache_tag_store <= (others => '0');
518
            code_cache_store <= (others => '0');
519 42 ja_rd
        else
520 46 ja_rd
            -- Refill cache if necessary
521
            if cps=code_refill_bram_1 then
522
                code_cache_tag_store <=
523
                    "01" & code_rd_addr_reg(t_code_tag'high-2 downto t_code_tag'low);
524
                code_cache_store <= bram_rd_data;
525
            --elsif cps=code_refill_sram_2 then
526
            --    code_cache_tag_store <=
527
            --        "01" & code_rd_addr_reg(t_code_tag'high-2 downto t_code_tag'low);
528
            --    code_cache_store <= sram_rd_data;
529 42 ja_rd
            end if;
530 46 ja_rd
        end if;
531
    end if;
532
end process code_cache_memory;
533
 
534
 
535
--------------------------------------------------------------------------------
536
-- Data cache
537
 
538 58 ja_rd
-- CPU data input mux: direct cache output OR uncached io input
539 46 ja_rd
with dps select data_rd <=
540
    io_rd_data      when data_read_io_1,
541
    data_cache_rd   when others;
542
 
543 58 ja_rd
-- All the tag match logic is unfinished and will be simplified away in synth.
544
-- The 'cache' is really a single register.
545 46 ja_rd
data_cache_rd <= data_cache_store;
546
data_cache_tag <= data_cache_tag_store;
547
 
548
data_cache_memory:
549
process(clk)
550
begin
551
    if clk'event and clk='1' then
552
        if reset='1' then
553
            -- in the real hardware the tag store can't be reset and it's up
554
            -- to the SW to initialize the cache.
555
            data_cache_tag_store <= (others => '0');
556
            data_cache_store <= (others => '0');
557
        else
558
            -- Refill data cache if necessary
559
            if dps=data_refill_sram_1 then
560
                data_cache_tag_store <=
561
                    "01" & data_rd_addr_reg(t_data_tag'high-2 downto t_data_tag'low);
562
                data_cache_store <= sram_rd_data;
563
            elsif dps=data_refill_bram_1 then
564
                data_cache_tag_store <=
565
                    "01" & data_rd_addr_reg(t_data_tag'high-2 downto t_data_tag'low);
566
                data_cache_store <= bram_rd_data;
567 42 ja_rd
            end if;
568
        end if;
569
    end if;
570 46 ja_rd
end process data_cache_memory;
571 42 ja_rd
 
572 58 ja_rd
 
573
--------------------------------------------------------------------------------
574
-- SRAM interface
575
 
576
-- Note this signals are meantto be connected directly to FPGA pins (and then
577
-- to a SRAM, of course). They are the only signals whose tco we care about.
578
 
579
-- FIXME should add a SRAM CE\ signal
580
 
581
-- SRAM address bus (except for LSB) comes from cpu code or data addr registers
582 46 ja_rd
with dps select sram_address(sram_address'high downto 2) <=
583
    data_rd_addr_reg(sram_address'high downto 2)    when data_refill_sram_0,
584
    data_rd_addr_reg(sram_address'high downto 2)    when data_refill_sram_1,
585
    data_wr_addr_reg(sram_address'high downto 2)    when others;
586 42 ja_rd
 
587 58 ja_rd
-- SRAM addr bus LSB depends on the D-cache state because we read/write the
588
-- halfwords sequentially in successive cycles.
589 46 ja_rd
with dps select sram_address(1) <=
590
    '0'     when data_writethrough_sram_0,
591
    '1'     when data_writethrough_sram_1,
592
    '0'     when data_refill_sram_0,
593
    '1'     when data_refill_sram_1,
594
    '0'     when others;
595 42 ja_rd
 
596 58 ja_rd
-- SRAM databus i(when used for output) comes from either hword of the data
597
-- write register.
598 46 ja_rd
with dps select sram_databus <=
599
    data_wr_reg(31 downto 16)   when data_writethrough_sram_0,
600
    data_wr_reg(15 downto  0)   when data_writethrough_sram_1,
601
    (others => 'Z')             when others;
602 42 ja_rd
 
603 58 ja_rd
-- The byte_we is split in two similarly.
604 46 ja_rd
with dps select sram_byte_we_n <=
605
    not byte_we_reg(3 downto 2) when data_writethrough_sram_0,
606
    not byte_we_reg(1 downto 0) when data_writethrough_sram_1,
607
    "11"                        when others;
608 42 ja_rd
 
609 58 ja_rd
-- SRAM OE\ is only asserted low for read cycles
610 46 ja_rd
with dps select sram_oe_n <=
611
    '0' when data_refill_sram_0,
612
    '0' when data_refill_sram_1,
613
    '1' when others;
614 42 ja_rd
 
615 58 ja_rd
-- When eading from the SRAM, read word comes from read hword register and 
616
-- SRAM bus (read register is loaded in previous cycle).
617 46 ja_rd
sram_rd_data <= sram_rd_data_reg & sram_databus;
618 42 ja_rd
 
619 58 ja_rd
sram_input_halfword_register:
620 46 ja_rd
process(clk)
621
begin
622
    if clk'event and clk='1' then
623 58 ja_rd
        sram_rd_data_reg <= sram_databus;
624 46 ja_rd
    end if;
625 58 ja_rd
end process sram_input_halfword_register;
626 42 ja_rd
 
627
 
628
--------------------------------------------------------------------------------
629 58 ja_rd
-- I/O interface -- IO is assumed to behave like synchronous memory
630 42 ja_rd
 
631 46 ja_rd
io_byte_we <= byte_we_reg when dps=data_write_io_0 else "0000";
632
io_rd_addr <= data_rd_addr_reg;
633
io_wr_addr <= data_wr_addr_reg;
634
io_wr_data <= data_wr_reg;
635
io_rd_vma <= '1' when dps=data_read_io_0 else '0';
636 42 ja_rd
 
637 58 ja_rd
 
638 46 ja_rd
--------------------------------------------------------------------------------
639 58 ja_rd
-- CPU stall control
640 42 ja_rd
 
641 58 ja_rd
-- Stall the CPU when either state machine needs it
642 46 ja_rd
mem_wait <= (code_wait or data_wait) and not reset;
643 42 ja_rd
 
644 58 ja_rd
-- Assert code_wait until the cycle where the CPU has valid code word on its
645
-- code bus
646 46 ja_rd
with cps select code_wait <=
647
    '1' when code_refill_bram_0,
648
    '1' when code_refill_bram_1,
649
    '1' when code_refill_bram_2,
650
    '1' when code_wait_for_dcache,
651
    '0' when others;
652 42 ja_rd
 
653 58 ja_rd
-- Assert code_wait until the cycle where the CPU has valid data word on its
654
-- code bus AND no other operations are ongoing that may use the external buses.
655 46 ja_rd
with dps select data_wait <=
656
    '1' when data_writethrough_sram_0,
657
    '1' when data_writethrough_sram_1,
658
    '1' when data_refill_sram_0,
659
    '1' when data_refill_sram_1,
660
    '1' when data_refill_bram_0,
661
    '1' when data_refill_bram_1,
662
    '1' when data_read_io_0,
663
    '0' when others;
664
 
665 42 ja_rd
end architecture stub;

powered by: WebSVN 2.1.0

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