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

Subversion Repositories ion

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

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