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

Subversion Repositories ion

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

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