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

Subversion Repositories ion

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

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 46 ja_rd
-- KNOWN TROUBLE:
26
-- 
27
-- Apart from the very rough looks of the code, there's a few known faults with 
28
-- it:
29
--
30
-- 1.- Access to unmapped areas wil crash the CPU
31
--      A couple states are missing in the state machine for handling accesses 
32
--      to unmapped areas. I haven't yet decided how to handle that (return 
33
--      zero, trigger trap, mirror another mapped area...)
34
-- 2.- Address decoding is hardcoded in mips_pkg
35
--      It should be done here using module generics and not package constants.
36
-- 3.- Does not work as a real 1-word cache yet
37
--      That functionality is still missing, all accesses 'miss'. It should be
38
--      implemented, as a way to test the real cache logic on a small scale.
39
--
40
--------------------------------------------------------------------------------
41 42 ja_rd
 
42
library ieee;
43
use ieee.std_logic_1164.all;
44
use ieee.std_logic_arith.all;
45
use ieee.std_logic_unsigned.all;
46
use work.mips_pkg.all;
47
 
48
entity mips_cache_stub is
49
    generic (
50
        BRAM_ADDR_SIZE : integer := 10;
51
        SRAM_ADDR_SIZE : integer := 17
52
    );
53
    port(
54
        clk             : in std_logic;
55
        reset           : in std_logic;
56 46 ja_rd
 
57 42 ja_rd
        -- Interface to CPU core
58
        data_rd_addr    : in std_logic_vector(31 downto 0);
59
        data_rd         : out std_logic_vector(31 downto 0);
60
        data_rd_vma     : in std_logic;
61 46 ja_rd
 
62 42 ja_rd
        code_rd_addr    : in std_logic_vector(31 downto 2);
63
        code_rd         : out std_logic_vector(31 downto 0);
64
        code_rd_vma     : in std_logic;
65 46 ja_rd
 
66 42 ja_rd
        data_wr_addr    : in std_logic_vector(31 downto 2);
67
        byte_we         : in std_logic_vector(3 downto 0);
68
        data_wr         : in std_logic_vector(31 downto 0);
69 46 ja_rd
 
70 42 ja_rd
        mem_wait        : out std_logic;
71 46 ja_rd
        cache_enable    : in std_logic;
72
 
73 42 ja_rd
        -- interface to FPGA i/o devices
74
        io_rd_data      : in std_logic_vector(31 downto 0);
75
        io_rd_addr      : out std_logic_vector(31 downto 2);
76
        io_wr_addr      : out std_logic_vector(31 downto 2);
77
        io_wr_data      : out std_logic_vector(31 downto 0);
78
        io_rd_vma       : out std_logic;
79
        io_byte_we      : out std_logic_vector(3 downto 0);
80 46 ja_rd
 
81 42 ja_rd
        -- interface to synchronous 32-bit-wide FPGA BRAM (possibly used as ROM)
82
        bram_rd_data    : in std_logic_vector(31 downto 0);
83
        bram_wr_data    : out std_logic_vector(31 downto 0);
84
        bram_rd_addr    : out std_logic_vector(BRAM_ADDR_SIZE+1 downto 2);
85
        bram_wr_addr    : out std_logic_vector(BRAM_ADDR_SIZE+1 downto 2);
86 46 ja_rd
        bram_byte_we    : out std_logic_vector(3 downto 0);
87
        bram_data_rd_vma: out std_logic;
88
 
89 42 ja_rd
        -- interface to asynchronous 16-bit-wide EXTERNAL SRAM
90 46 ja_rd
        sram_address    : out std_logic_vector(SRAM_ADDR_SIZE downto 1);
91 42 ja_rd
        sram_databus    : inout std_logic_vector(15 downto 0);
92
        sram_byte_we_n  : out std_logic_vector(1 downto 0);
93
        sram_oe_n       : out std_logic
94
    );
95
end entity mips_cache_stub;
96
 
97
 
98
 
99
architecture stub of mips_cache_stub is
100
 
101 46 ja_rd
type t_code_cache_state is (
102
    code_normal,
103
    code_wait_for_dcache,
104
 
105
    code_refill_bram_0,         -- pc in bram_rd_addr
106
    code_refill_bram_1,         -- op in bram_rd
107
    code_refill_bram_2,         -- op in code_rd
108
    code_refill_bram_3,
109
 
110
    code_refill_sram_0,
111
    code_refill_sram_1,
112
    code_refill_sram_2,
113
 
114
    code_bug
115
   );
116
 
117
signal cps, cns :           t_code_cache_state;
118
 
119
 
120
type t_data_cache_state is (
121
    data_normal,
122
 
123
    data_refill_sram_0,
124
    data_refill_sram_1,
125
 
126
    data_refill_bram_0,         -- rd addr in bram_rd_addr
127
    data_refill_bram_1,         -- rd data in bram_rd_data
128 42 ja_rd
 
129 46 ja_rd
    data_read_io_0,             -- rd addr on io_rd_addr, io_vma active
130
    data_read_io_1,             -- rd data on io_rd_data
131
    data_write_io_0,
132
    data_write_io_1,
133
 
134
    data_writethrough_sram_0,
135
    data_writethrough_sram_1,
136 42 ja_rd
 
137 46 ja_rd
    data_ignore_write,
138 42 ja_rd
 
139 46 ja_rd
    data_bug
140 42 ja_rd
   );
141
 
142
 
143 46 ja_rd
signal dps, dns :           t_data_cache_state;
144 42 ja_rd
 
145 46 ja_rd
 
146 42 ja_rd
signal use_sram_wr :        std_logic;
147
signal use_sram_rd :        std_logic;
148
signal use_io_wr :          std_logic;
149
signal use_io_rd :          std_logic;
150 46 ja_rd
signal data_addr_reg :      std_logic_vector(SRAM_ADDR_SIZE downto 2);
151 42 ja_rd
signal data_wr_reg :        std_logic_vector(31 downto 0);
152
signal data_input_reg :     std_logic_vector(15 downto 0);
153
signal bram_rd_data_reg :   std_logic_vector(31 downto 0);
154 46 ja_rd
signal io_rd_data_reg :     std_logic_vector(31 downto 0);
155 42 ja_rd
signal byte_we_reg :        std_logic_vector(3 downto 0);
156
 
157 46 ja_rd
 
158
signal code_rd_addr_reg :   t_pc;
159
 
160
subtype t_code_tag is std_logic_vector(23 downto 2);
161
signal code_cache_tag :     t_code_tag;
162
signal code_cache_tag_store : t_code_tag;
163
signal code_cache_store :   t_word;
164
 
165
signal code_cache_rd :      t_word;
166
signal code_miss :          std_logic;
167
 
168
 
169
signal data_rd_addr_reg :   t_pc;
170
signal data_wr_addr_reg :   t_pc;
171
 
172
subtype t_data_tag is std_logic_vector(23 downto 2);
173
signal data_cache_tag :     t_data_tag;
174
signal data_cache_tag_store : t_data_tag;
175
signal data_cache_store :   t_word;
176
-- Stores first (high) HW read from SRAM
177
signal sram_rd_data_reg :   std_logic_vector(31 downto 16);
178
-- Data read from SRAM, valid in refill_1
179
signal sram_rd_data :       t_word;
180
 
181
signal write_pending :      std_logic;
182
signal read_pending :       std_logic;
183
 
184
signal data_cache_rd :      t_word;
185
signal data_miss :          std_logic;
186
 
187
signal code_wait :          std_logic;
188
signal data_wait :          std_logic;
189
 
190
 
191
signal code_rd_addr_mask :  t_addr_decode;
192
signal data_rd_addr_mask :  t_addr_decode;
193
signal data_wr_addr_mask :  t_addr_decode;
194
 
195
signal code_rd_area :       std_logic_vector(1 downto 0);
196
signal data_rd_area :       std_logic_vector(1 downto 0);
197
signal data_wr_area :       std_logic_vector(1 downto 0);
198
 
199 42 ja_rd
begin
200
 
201
 
202 46 ja_rd
cache_state_machine_regs:
203 42 ja_rd
process(clk)
204
begin
205
   if clk'event and clk='1' then
206
        if reset='1' then
207 46 ja_rd
            cps <= code_normal;
208
            dps <= data_normal;
209 42 ja_rd
        else
210 46 ja_rd
            cps <= cns;
211
            dps <= dns;
212 42 ja_rd
        end if;
213
    end if;
214 46 ja_rd
end process cache_state_machine_regs;
215 42 ja_rd
 
216
 
217 46 ja_rd
code_state_machine_transitions:
218
process(cps, dps, code_rd_vma, code_miss, code_rd_area, write_pending, read_pending)
219 42 ja_rd
begin
220 46 ja_rd
    case cps is
221
    when code_normal =>
222
        if code_rd_vma='1' and code_miss='1' and read_pending='0' and write_pending='0' then
223
            cns <= code_refill_bram_0; -- FIXME check memory area
224 42 ja_rd
        else
225 46 ja_rd
            cns <= cps;
226 42 ja_rd
        end if;
227
 
228 46 ja_rd
    when code_refill_bram_0 =>
229
        cns <= code_refill_bram_1;
230 42 ja_rd
 
231 46 ja_rd
    when code_refill_bram_1 =>
232
        cns <= code_refill_bram_2;
233 42 ja_rd
 
234 46 ja_rd
    when code_refill_bram_2 =>
235
        if dps/=data_normal and read_pending='0' and write_pending='0' then
236
            cns <= code_wait_for_dcache;
237 42 ja_rd
        else
238 46 ja_rd
            cns <= code_normal;
239 42 ja_rd
        end if;
240
 
241 46 ja_rd
    when code_refill_bram_3 =>
242
        cns <= code_normal;
243
 
244
    when code_wait_for_dcache =>
245
        -- if D-cache is busy, wait for it to become idle
246
        if dps/=data_normal then
247
            cns <= cps;
248
        elsif code_miss='1' then
249
            cns <= code_refill_bram_1; -- FIXME check memory area
250 42 ja_rd
        else
251 46 ja_rd
            cns <= code_normal;
252 42 ja_rd
        end if;
253
 
254 46 ja_rd
    when code_bug =>
255
        cns <= code_normal;
256
 
257 42 ja_rd
    when others =>
258 46 ja_rd
        cns <= code_bug;
259 42 ja_rd
    end case;
260 46 ja_rd
end process code_state_machine_transitions;
261 42 ja_rd
 
262
 
263 46 ja_rd
data_state_machine_transitions:
264
process(dps, write_pending, read_pending, data_rd_area, data_wr_area)
265
begin
266
    case dps is
267
    when data_normal =>
268
        if write_pending='1' then
269
            case data_wr_area is
270
            when "00"   => dns <= data_ignore_write; -- Write to BRAM ignored
271
            when "01"   => dns <= data_writethrough_sram_0;
272
            when "10"   => dns <= data_write_io_0;
273
            when others => dns <= dps; -- Write to undecoded area ignored
274
            end case;
275
 
276
        elsif read_pending='1' then
277
            case data_rd_area is
278
            when "00"   => dns <= data_refill_bram_0;
279
            when "01"   => dns <= data_refill_sram_0;
280
            when "10"   => dns <= data_read_io_0;
281
            when others => dns <= dps; -- ignore read from undecoded area
282
                           -- FIXME should raise debug flag 
283
            end case;
284
        else
285
            dns <= dps;
286
        end if;
287 42 ja_rd
 
288 46 ja_rd
    when data_write_io_0 =>
289
        dns <= data_normal;
290
 
291
    when data_read_io_0 =>
292
        dns <= data_read_io_1;
293
 
294
    when data_read_io_1 =>
295
        dns <= data_normal;
296 42 ja_rd
 
297 46 ja_rd
    when data_refill_sram_0 =>
298
        dns <= data_refill_sram_1;
299 42 ja_rd
 
300 46 ja_rd
    when data_refill_sram_1 =>
301
        dns <= data_normal;
302 42 ja_rd
 
303 46 ja_rd
    when data_refill_bram_0 =>
304
        dns <= data_refill_bram_1;
305
 
306
    when data_refill_bram_1 =>
307
        dns <= data_normal;
308
 
309
    when data_writethrough_sram_0 =>
310
        dns <= data_writethrough_sram_1;
311
 
312
    when data_writethrough_sram_1 =>
313
        dns <= data_normal;
314
 
315
    when data_ignore_write =>
316
        dns <= data_normal;
317
 
318
    when data_bug =>
319
        dns <= data_normal;
320
 
321
    when others =>
322
        dns <= data_bug;
323
    end case;
324
end process data_state_machine_transitions;
325
 
326
 
327
--------------------------------------------------------------------------------
328
-- CPU interface registers and address decoding --------------------------------
329
 
330
 
331
-- Everything coming and going to the CPU is registered, so that the CPU has
332
-- some timing marging.
333
 
334
cpu_interface_registers:
335 42 ja_rd
process(clk)
336
begin
337
    if clk'event and clk='1' then
338
        if reset='1' then
339 46 ja_rd
            write_pending <= '0';
340
            read_pending <= '0';
341
            byte_we_reg <= "0000";
342 42 ja_rd
        else
343 46 ja_rd
            -- Raise 'read_pending' at the 1st cycle of a read, clear it when
344
            -- the read (and/or refill) operation has been done.
345
            -- data_rd_addr_reg always has the addr of any pending read
346 42 ja_rd
            if data_rd_vma='1' then
347 46 ja_rd
                read_pending <= '1';
348
                data_rd_addr_reg <= data_rd_addr(31 downto 2);
349
            elsif dps=data_refill_sram_1 or
350
                  dps=data_refill_bram_1 or
351
                  dps=data_read_io_0 then
352
                read_pending <= '0';
353 42 ja_rd
            end if;
354 46 ja_rd
 
355
            -- Raise 'write_pending' at the 1st cycle of a read, clear it when
356
            -- the write (writethrough actually) operation has been done.
357
            -- data_wr_addr_reg always has the addr of any pending write
358
            if byte_we/="0000" and dps=data_normal then
359
                byte_we_reg <= byte_we;
360
                data_wr_reg <= data_wr;
361
                data_wr_addr_reg <= data_wr_addr;
362
                write_pending <= '1';
363
            elsif dps=data_writethrough_sram_1 or
364
                  dps=data_write_io_0 or
365
                  dps=data_ignore_write then
366
                write_pending <= '0';
367
                byte_we_reg <= "0000";
368
            end if;
369
 
370
            -- Register code fetch addresses only when they are valid; so that
371
            -- code_rd_addr_reg always holds the last fetch address.
372
            if (cps=code_normal and code_rd_vma='1') or cps=code_refill_bram_2 then
373
                code_rd_addr_reg <= code_rd_addr;
374
            end if;
375 42 ja_rd
        end if;
376
    end if;
377 46 ja_rd
end process cpu_interface_registers;
378 42 ja_rd
 
379 46 ja_rd
-- Address decoding ------------------------------------------------------------
380 42 ja_rd
 
381 46 ja_rd
-- Decoding is done on the high bits of the address only, there'll be mirroring.
382
-- Write to areas not explicitly decoded will be silently ignored. Reads will
383
-- get undefined data.
384
 
385
code_rd_addr_mask <= code_rd_addr_reg(31 downto t_addr_decode'low);
386
data_rd_addr_mask <= data_rd_addr_reg(31 downto t_addr_decode'low);
387
data_wr_addr_mask <= data_wr_addr_reg(31 downto t_addr_decode'low);
388
 
389
 
390
with code_rd_addr_mask select code_rd_area <=
391
    "00"    when ADDR_BOOT,
392
    "01"    when ADDR_XRAM,
393
    "11"    when others;
394
 
395
with data_rd_addr_mask select data_rd_area <=
396
    "00"    when ADDR_BOOT,
397
    "01"    when ADDR_XRAM,
398
    "10"    when ADDR_IO,
399
    "11"    when others;
400
 
401
with data_wr_addr_mask select data_wr_area <=
402
    "01"    when ADDR_XRAM,
403
    "10"    when ADDR_IO,
404
    "11"    when others;
405
 
406
 
407
--------------------------------------------------------------------------------
408
-- Code cache
409
 
410
bram_rd_addr <= data_rd_addr_reg(bram_rd_addr'high downto 2)
411
    when dps=data_refill_bram_0
412
    else code_rd_addr_reg(bram_rd_addr'high downto 2) ;
413
 
414
code_rd <= code_cache_rd;
415
 
416
-- FIXME Actual 1-word cache functionality is unimplemented yet
417
code_miss <= '1'; --code_rd_vma;
418
 
419
 
420
-- Read cache code and tag from code store
421
code_cache_rd <= code_cache_store;
422
code_cache_tag <= code_cache_tag_store;
423
 
424
code_cache_memory:
425 42 ja_rd
process(clk)
426
begin
427
    if clk'event and clk='1' then
428 46 ja_rd
 
429
 
430 42 ja_rd
        if reset='1' then
431 46 ja_rd
            -- in the real hardware the tag store can't be reset and it's up
432
            -- to the SW to initialize the cache.
433
            code_cache_tag_store <= (others => '0');
434
            code_cache_store <= (others => '0');
435 42 ja_rd
        else
436 46 ja_rd
            -- Refill cache if necessary
437
            if cps=code_refill_bram_1 then
438
                code_cache_tag_store <=
439
                    "01" & code_rd_addr_reg(t_code_tag'high-2 downto t_code_tag'low);
440
                code_cache_store <= bram_rd_data;
441
            --elsif cps=code_refill_sram_2 then
442
            --    code_cache_tag_store <=
443
            --        "01" & code_rd_addr_reg(t_code_tag'high-2 downto t_code_tag'low);
444
            --    code_cache_store <= sram_rd_data;
445 42 ja_rd
            end if;
446 46 ja_rd
        end if;
447
    end if;
448
end process code_cache_memory;
449
 
450
 
451
--------------------------------------------------------------------------------
452
-- Data cache
453
 
454
with dps select data_rd <=
455
    io_rd_data      when data_read_io_1,
456
    data_cache_rd   when others;
457
 
458
 
459
data_cache_rd <= data_cache_store;
460
data_cache_tag <= data_cache_tag_store;
461
 
462
data_cache_memory:
463
process(clk)
464
begin
465
    if clk'event and clk='1' then
466
 
467
 
468
        if reset='1' then
469
            -- in the real hardware the tag store can't be reset and it's up
470
            -- to the SW to initialize the cache.
471
            data_cache_tag_store <= (others => '0');
472
            data_cache_store <= (others => '0');
473
        else
474
            -- Refill data cache if necessary
475
            if dps=data_refill_sram_1 then
476
                data_cache_tag_store <=
477
                    "01" & data_rd_addr_reg(t_data_tag'high-2 downto t_data_tag'low);
478
                data_cache_store <= sram_rd_data;
479
            elsif dps=data_refill_bram_1 then
480
                data_cache_tag_store <=
481
                    "01" & data_rd_addr_reg(t_data_tag'high-2 downto t_data_tag'low);
482
                data_cache_store <= bram_rd_data;
483 42 ja_rd
            end if;
484
        end if;
485
    end if;
486 46 ja_rd
end process data_cache_memory;
487 42 ja_rd
 
488 46 ja_rd
with dps select sram_address(sram_address'high downto 2) <=
489
    data_rd_addr_reg(sram_address'high downto 2)    when data_refill_sram_0,
490
    data_rd_addr_reg(sram_address'high downto 2)    when data_refill_sram_1,
491
    data_wr_addr_reg(sram_address'high downto 2)    when others;
492 42 ja_rd
 
493 46 ja_rd
with dps select sram_address(1) <=
494
    '0'     when data_writethrough_sram_0,
495
    '1'     when data_writethrough_sram_1,
496
    '0'     when data_refill_sram_0,
497
    '1'     when data_refill_sram_1,
498
    '0'     when others;
499 42 ja_rd
 
500 46 ja_rd
with dps select sram_databus <=
501
    data_wr_reg(31 downto 16)   when data_writethrough_sram_0,
502
    data_wr_reg(15 downto  0)   when data_writethrough_sram_1,
503
    (others => 'Z')             when others;
504 42 ja_rd
 
505 46 ja_rd
with dps select sram_byte_we_n <=
506
    not byte_we_reg(3 downto 2) when data_writethrough_sram_0,
507
    not byte_we_reg(1 downto 0) when data_writethrough_sram_1,
508
    "11"                        when others;
509 42 ja_rd
 
510 46 ja_rd
with dps select sram_oe_n <=
511
    '0' when data_refill_sram_0,
512
    '0' when data_refill_sram_1,
513
    '1' when others;
514 42 ja_rd
 
515 46 ja_rd
sram_rd_data <= sram_rd_data_reg & sram_databus;
516 42 ja_rd
 
517 46 ja_rd
process(clk)
518
begin
519
    if clk'event and clk='1' then
520
        --if ps=xxx then
521
            sram_rd_data_reg <= sram_databus;
522
        --end if;
523
    end if;
524
end process;
525 42 ja_rd
 
526 46 ja_rd
bram_data_rd_vma <= '1' when dps=data_refill_bram_1 else '0';
527 42 ja_rd
 
528
 
529
--------------------------------------------------------------------------------
530
 
531 46 ja_rd
io_byte_we <= byte_we_reg when dps=data_write_io_0 else "0000";
532
io_rd_addr <= data_rd_addr_reg;
533
io_wr_addr <= data_wr_addr_reg;
534
io_wr_data <= data_wr_reg;
535
io_rd_vma <= '1' when dps=data_read_io_0 else '0';
536 42 ja_rd
 
537 46 ja_rd
--------------------------------------------------------------------------------
538 42 ja_rd
 
539 46 ja_rd
mem_wait <= (code_wait or data_wait) and not reset;
540 42 ja_rd
 
541 46 ja_rd
with cps select code_wait <=
542
    '1' when code_refill_bram_0,
543
    '1' when code_refill_bram_1,
544
    '1' when code_refill_bram_2,
545
    '1' when code_wait_for_dcache,
546
    '0' when others;
547 42 ja_rd
 
548 46 ja_rd
with dps select data_wait <=
549
    '1' when data_writethrough_sram_0,
550
    '1' when data_writethrough_sram_1,
551
    '1' when data_refill_sram_0,
552
    '1' when data_refill_sram_1,
553
    '1' when data_refill_bram_0,
554
    '1' when data_refill_bram_1,
555
    '1' when data_read_io_0,
556
    '0' when others;
557
 
558
 
559 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.