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

Subversion Repositories ion

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

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 96 ja_rd
-- memory is just 1 word for each of code and data, and it's missing any tag
6
-- matching logic so all accesses 'miss'.
7 43 ja_rd
--
8 46 ja_rd
-- It interfaces the CPU to the following:
9 42 ja_rd
--
10 46 ja_rd
--  1.- Internal 32-bit-wide BRAM for read only
11 42 ja_rd
--  2.- Internal 32-bit I/O bus
12 96 ja_rd
--  3.- External 16-bit or 8-bit wide static memory (SRAM or FLASH)
13 42 ja_rd
--
14 46 ja_rd
-- The SRAM memory interface signals are meant to connect directly to FPGA pins
15
-- and all outputs are registered (tco should be minimal).
16
-- SRAM data inputs are NOT registered, though. They go through a couple muxes
17
-- before reaching the first register so watch out for tsetup.
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 80 ja_rd
-- interface. They are either direct register outputs or at most with an
29 58 ja_rd
-- 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 80 ja_rd
--
44
-- This means this block will not degrade the timing performance of the system,
45 58 ja_rd
-- as long as its logic is shallower than the current bottleneck (the ALU).
46
--
47
--------------------------------------------------------------------------------
48 46 ja_rd
-- KNOWN TROUBLE:
49 80 ja_rd
--
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 80 ja_rd
--      In order to guarantee setup and hold times for WE controlled write
54 73 ja_rd
--      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 80 ja_rd
--
59 72 ja_rd
-- 2.- Access to unmapped areas will crash the CPU
60 80 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 96 ja_rd
-- 3.- Does not work as a real 1-word cache yet
65 46 ja_rd
--      That functionality is still missing, all accesses 'miss'. It should be
66
--      implemented, as a way to test the real cache logic on a small scale.
67
--
68
--------------------------------------------------------------------------------
69 42 ja_rd
 
70
library ieee;
71
use ieee.std_logic_1164.all;
72
use ieee.std_logic_arith.all;
73
use ieee.std_logic_unsigned.all;
74
use work.mips_pkg.all;
75
 
76 58 ja_rd
 
77 42 ja_rd
entity mips_cache_stub is
78
    generic (
79 96 ja_rd
        BRAM_ADDR_SIZE : integer    := 10;  -- BRAM address size
80
        SRAM_ADDR_SIZE : integer    := 17;  -- Static RAM/Flash address size
81
 
82
        -- these cache parameters are unused in thie implementation, they're
83
        -- here for compatibility to the real cache module.
84
        LINE_SIZE : integer         := 4;   -- Line size in words
85
        CACHE_SIZE : integer        := 256  -- I- and D- cache size in lines
86 42 ja_rd
    );
87
    port(
88
        clk             : in std_logic;
89
        reset           : in std_logic;
90 46 ja_rd
 
91 42 ja_rd
        -- Interface to CPU core
92 96 ja_rd
        data_addr       : in std_logic_vector(31 downto 0);
93
 
94 42 ja_rd
        data_rd         : out std_logic_vector(31 downto 0);
95
        data_rd_vma     : in std_logic;
96 46 ja_rd
 
97 96 ja_rd
        byte_we         : in std_logic_vector(3 downto 0);
98
        data_wr         : in std_logic_vector(31 downto 0);
99
 
100 42 ja_rd
        code_rd_addr    : in std_logic_vector(31 downto 2);
101
        code_rd         : out std_logic_vector(31 downto 0);
102
        code_rd_vma     : in std_logic;
103 46 ja_rd
 
104 42 ja_rd
        mem_wait        : out std_logic;
105 46 ja_rd
        cache_enable    : in std_logic;
106
 
107 42 ja_rd
        -- interface to FPGA i/o devices
108
        io_rd_data      : in std_logic_vector(31 downto 0);
109
        io_rd_addr      : out std_logic_vector(31 downto 2);
110
        io_wr_addr      : out std_logic_vector(31 downto 2);
111
        io_wr_data      : out std_logic_vector(31 downto 0);
112
        io_rd_vma       : out std_logic;
113
        io_byte_we      : out std_logic_vector(3 downto 0);
114 46 ja_rd
 
115 42 ja_rd
        -- interface to synchronous 32-bit-wide FPGA BRAM (possibly used as ROM)
116
        bram_rd_data    : in std_logic_vector(31 downto 0);
117
        bram_wr_data    : out std_logic_vector(31 downto 0);
118
        bram_rd_addr    : out std_logic_vector(BRAM_ADDR_SIZE+1 downto 2);
119
        bram_wr_addr    : out std_logic_vector(BRAM_ADDR_SIZE+1 downto 2);
120 46 ja_rd
        bram_byte_we    : out std_logic_vector(3 downto 0);
121
        bram_data_rd_vma: out std_logic;
122
 
123 75 ja_rd
        -- interface to asynchronous 16-bit-wide or 8-bit-wide static memory
124
        sram_address    : out std_logic_vector(SRAM_ADDR_SIZE-1 downto 0);
125
        sram_data_rd    : in std_logic_vector(15 downto 0);
126
        sram_data_wr    : out std_logic_vector(15 downto 0);
127 42 ja_rd
        sram_byte_we_n  : out std_logic_vector(1 downto 0);
128
        sram_oe_n       : out std_logic
129
    );
130
end entity mips_cache_stub;
131
 
132
 
133
 
134
architecture stub of mips_cache_stub is
135
 
136 72 ja_rd
-- Wait state counter -- we're supporting static memory from 10 to >100 ns
137
subtype t_wait_state_counter is std_logic_vector(2 downto 0);
138
 
139 80 ja_rd
-- State machine ----------------------------------------------------
140 58 ja_rd
 
141 80 ja_rd
type t_cache_state is (
142
    idle,                       -- Cache hitting, control machine idle
143 46 ja_rd
 
144 80 ja_rd
    -- Code refill --------------------------------------------------
145 46 ja_rd
    code_refill_bram_0,         -- pc in bram_rd_addr
146
    code_refill_bram_1,         -- op in bram_rd
147 80 ja_rd
    code_refill_bram_2,         -- op in code_rd
148 46 ja_rd
 
149 80 ja_rd
    code_refill_sram_0,         -- rd addr in SRAM addr bus (low hword)
150
    code_refill_sram_1,         -- rd addr in SRAM addr bus (high hword)
151 46 ja_rd
 
152 80 ja_rd
    code_refill_sram8_0,        -- rd addr in SRAM addr bus (byte 0)
153
    code_refill_sram8_1,        -- rd addr in SRAM addr bus (byte 1)
154
    code_refill_sram8_2,        -- rd addr in SRAM addr bus (byte 2)
155
    code_refill_sram8_3,        -- rd addr in SRAM addr bus (byte 3)
156 46 ja_rd
 
157 80 ja_rd
    code_crash,                 -- tried to run from i/o or something like that
158 46 ja_rd
 
159 80 ja_rd
    -- Data refill & write-through ----------------------------------
160 58 ja_rd
    data_refill_sram_0,         -- rd addr in SRAM addr bus (low hword)
161
    data_refill_sram_1,         -- rd addr in SRAM addr bus (high hword)
162 46 ja_rd
 
163 75 ja_rd
    data_refill_sram8_0,        -- rd addr in SRAM addr bus (byte 0)
164
    data_refill_sram8_1,        -- rd addr in SRAM addr bus (byte 1)
165
    data_refill_sram8_2,        -- rd addr in SRAM addr bus (byte 2)
166
    data_refill_sram8_3,        -- rd addr in SRAM addr bus (byte 3)
167
 
168 46 ja_rd
    data_refill_bram_0,         -- rd addr in bram_rd_addr
169
    data_refill_bram_1,         -- rd data in bram_rd_data
170 80 ja_rd
 
171 46 ja_rd
    data_read_io_0,             -- rd addr on io_rd_addr, io_vma active
172
    data_read_io_1,             -- rd data on io_rd_data
173 80 ja_rd
 
174 58 ja_rd
    data_write_io_0,            -- wr addr & data in io_wr_*, io_byte_we active
175 46 ja_rd
 
176 72 ja_rd
    data_writethrough_sram_0a,  -- wr addr & data in SRAM buses (low hword)
177
    data_writethrough_sram_0b,  -- WE asserted
178
    data_writethrough_sram_0c,  -- WE deasserted
179
    data_writethrough_sram_1a,  -- wr addr & data in SRAM buses (high hword)
180
    data_writethrough_sram_1b,  -- WE asserted
181
    data_writethrough_sram_1c,  -- WE deasserted
182 80 ja_rd
 
183 58 ja_rd
    data_ignore_write,          -- hook for raising error flag FIXME untested
184 72 ja_rd
    data_ignore_read,           -- hook for raising error flag FIXME untested
185 42 ja_rd
 
186 80 ja_rd
    -- Other states -------------------------------------------------
187
 
188
    --code_wait_for_dcache,       -- wait for D-cache to stop using the buses
189
    bug                         -- caught an error in the state machine
190 42 ja_rd
   );
191
 
192 80 ja_rd
-- Cache state machine state register & next state
193
signal ps, ns :             t_cache_state;
194
-- Wait state down-counter, formally part of the state machine register
195
signal ws_ctr :             t_wait_state_counter;
196
-- Wait states for memory being accessed
197
signal ws_value :           t_wait_state_counter;
198
-- Asserted to initialize the wait state counter
199
signal load_ws_ctr :        std_logic;
200
-- Asserted when the wait state counter has reached zero
201
signal ws_wait_done :       std_logic;
202 42 ja_rd
 
203
 
204 58 ja_rd
-- CPU interface registers ------------------------------------------
205
signal data_rd_addr_reg :   t_pc;
206
signal data_wr_addr_reg :   t_pc;
207
signal code_rd_addr_reg :   t_pc;
208 46 ja_rd
 
209 42 ja_rd
signal data_wr_reg :        std_logic_vector(31 downto 0);
210
signal byte_we_reg :        std_logic_vector(3 downto 0);
211
 
212 58 ja_rd
-- SRAM interface ---------------------------------------------------
213
-- Stores first (high) HW read from SRAM
214 75 ja_rd
signal sram_rd_data_reg :   std_logic_vector(31 downto 8);
215 58 ja_rd
-- Data read from SRAM, valid in refill_1
216
signal sram_rd_data :       t_word;
217 46 ja_rd
 
218
 
219 58 ja_rd
 
220
-- I-cache -- most of this is unimplemented -------------------------
221
 
222 46 ja_rd
subtype t_code_tag is std_logic_vector(23 downto 2);
223
signal code_cache_tag :     t_code_tag;
224
signal code_cache_tag_store : t_code_tag;
225
signal code_cache_store :   t_word;
226 58 ja_rd
-- code word read from cache
227 46 ja_rd
signal code_cache_rd :      t_word;
228 58 ja_rd
-- raised whel code_cache_rd is not valid due to a cache miss
229 46 ja_rd
signal code_miss :          std_logic;
230
 
231 58 ja_rd
-- '1' when the I-cache state machine stalls the pipeline (mem_wait)
232
signal code_wait :          std_logic;
233 46 ja_rd
 
234 58 ja_rd
-- D-cache -- most of this is unimplemented -------------------------
235 46 ja_rd
subtype t_data_tag is std_logic_vector(23 downto 2);
236
signal data_cache_tag :     t_data_tag;
237
signal data_cache_tag_store : t_data_tag;
238
signal data_cache_store :   t_word;
239 58 ja_rd
-- active when there's a write waiting to be done
240 46 ja_rd
signal write_pending :      std_logic;
241 58 ja_rd
-- active when there's a read waiting to be done
242 46 ja_rd
signal read_pending :       std_logic;
243 58 ja_rd
-- data word read from cache
244 46 ja_rd
signal data_cache_rd :      t_word;
245 58 ja_rd
-- '1' when data_cache_rd is not valid due to a cache miss
246 46 ja_rd
signal data_miss :          std_logic;
247
 
248 58 ja_rd
-- '1' when the D-cache state machine stalls the pipeline (mem_wait)
249 46 ja_rd
signal data_wait :          std_logic;
250
 
251
 
252 58 ja_rd
-- Address decoding -------------------------------------------------
253
 
254
-- Address slices used to decode
255 46 ja_rd
signal code_rd_addr_mask :  t_addr_decode;
256
signal data_rd_addr_mask :  t_addr_decode;
257
signal data_wr_addr_mask :  t_addr_decode;
258
 
259 58 ja_rd
-- Memory map area being accessed for each of the 3 buses:
260 64 ja_rd
signal code_rd_attr :       t_range_attr;
261
signal data_rd_attr :       t_range_attr;
262
signal data_wr_attr :       t_range_attr;
263 58 ja_rd
 
264 42 ja_rd
begin
265
 
266 58 ja_rd
--------------------------------------------------------------------------------
267 80 ja_rd
-- Cache control state machine
268 42 ja_rd
 
269 80 ja_rd
cache_state_machine_reg:
270 42 ja_rd
process(clk)
271
begin
272
   if clk'event and clk='1' then
273
        if reset='1' then
274 80 ja_rd
            ps <= idle;
275 42 ja_rd
        else
276 80 ja_rd
            ps <= ns;
277 42 ja_rd
        end if;
278
    end if;
279 80 ja_rd
end process cache_state_machine_reg;
280 42 ja_rd
 
281 80 ja_rd
-- Unified control state machine for I-Cache and D-cache -----------------------
282
control_state_machine_transitions:
283 95 ja_rd
process(ps, code_rd_vma, code_miss,
284
        data_wr_attr.mem_type, data_rd_attr.mem_type, code_rd_attr.mem_type,
285
        ws_wait_done,
286 58 ja_rd
        write_pending, read_pending)
287 42 ja_rd
begin
288 80 ja_rd
    case ps is
289
    when idle =>
290
        if code_miss='1' then
291
            case code_rd_attr.mem_type is
292
            when MT_BRAM        => ns <= code_refill_bram_0;
293
            when MT_SRAM_16B    => ns <= code_refill_sram_0;
294
            when MT_SRAM_8B     => ns <= code_refill_sram8_0;
295
            when others         => ns <= code_crash;
296
            end case;
297
 
298
        elsif write_pending='1' then
299
            case data_wr_attr.mem_type is
300
            when MT_BRAM        => ns <= data_ignore_write;
301
            when MT_SRAM_16B    => ns <= data_writethrough_sram_0a;
302
            when MT_IO_SYNC     => ns <= data_write_io_0;
303
            -- FIXME ignore write to undecoded area (clear pending flag)
304
            when others         => ns <= ps;
305
            end case;
306
 
307
        elsif read_pending='1' then
308
            case data_rd_attr.mem_type is
309
            when MT_BRAM        => ns <= data_refill_bram_0;
310
            when MT_SRAM_16B    => ns <= data_refill_sram_0;
311
            when MT_SRAM_8B     => ns <= data_refill_sram8_0;
312
            when MT_IO_SYNC     => ns <= data_read_io_0;
313
            -- FIXME ignore read from undecoded area (clear pending flag)
314
            when others         => ns <= data_ignore_read;
315
            end case;
316
 
317 42 ja_rd
        else
318 80 ja_rd
            ns <= ps;
319 42 ja_rd
        end if;
320
 
321 80 ja_rd
 
322
    -- Code refill states -------------------------------------------
323
 
324 46 ja_rd
    when code_refill_bram_0 =>
325 80 ja_rd
        ns <= code_refill_bram_1;
326 42 ja_rd
 
327 46 ja_rd
    when code_refill_bram_1 =>
328 80 ja_rd
        ns <= code_refill_bram_2;
329 42 ja_rd
 
330 46 ja_rd
    when code_refill_bram_2 =>
331 80 ja_rd
        -- If there's a data operation pending, do it now
332
        if write_pending='1' then
333
            case data_wr_attr.mem_type is
334
            when MT_BRAM        => ns <= data_ignore_write;
335
            when MT_SRAM_16B    => ns <= data_writethrough_sram_0a;
336
            when MT_IO_SYNC     => ns <= data_write_io_0;
337
            -- FIXME ignore write to undecoded area (clear pending flag)
338
            when others         => ns <= ps;
339
            end case;
340
 
341
        elsif read_pending='1' then
342
            case data_rd_attr.mem_type is
343
            when MT_BRAM        => ns <= data_refill_bram_0;
344
            when MT_SRAM_16B    => ns <= data_refill_sram_0;
345
            when MT_SRAM_8B     => ns <= data_refill_sram8_0;
346
            when MT_IO_SYNC     => ns <= data_read_io_0;
347
            -- FIXME ignore read from undecoded area (clear pending flag)
348
            when others         => ns <= data_ignore_read;
349
            end case;
350
 
351 42 ja_rd
        else
352 80 ja_rd
            ns <= idle;
353 42 ja_rd
        end if;
354 80 ja_rd
 
355
    when code_refill_sram_0 =>
356
        if ws_wait_done='1' then
357
            ns <= code_refill_sram_1;
358 42 ja_rd
        else
359 80 ja_rd
            ns <= ps;
360 42 ja_rd
        end if;
361
 
362 80 ja_rd
    when code_refill_sram_1 =>
363
        if ws_wait_done='1' then
364
            -- If there's a data operation pending, do it now
365
            if write_pending='1' then
366
                case data_wr_attr.mem_type is
367
                when MT_BRAM        => ns <= data_ignore_write;
368
                when MT_SRAM_16B    => ns <= data_writethrough_sram_0a;
369
                when MT_IO_SYNC     => ns <= data_write_io_0;
370
                -- FIXME ignore write to undecoded area (clear pending flag)
371
                when others         => ns <= ps;
372
                end case;
373 46 ja_rd
 
374 80 ja_rd
            elsif read_pending='1' then
375
                case data_rd_attr.mem_type is
376
                when MT_BRAM        => ns <= data_refill_bram_0;
377
                when MT_SRAM_16B    => ns <= data_refill_sram_0;
378
                when MT_SRAM_8B     => ns <= data_refill_sram8_0;
379
                when MT_IO_SYNC     => ns <= data_read_io_0;
380
                -- FIXME ignore read from undecoded area (clear pending flag)
381
                when others         => ns <= data_ignore_read;
382
                end case;
383 42 ja_rd
 
384 80 ja_rd
            else
385
                ns <= idle;
386
            end if;
387
        else
388
            ns <= ps;
389
        end if;
390 42 ja_rd
 
391 80 ja_rd
    when code_refill_sram8_0 =>
392
        if ws_wait_done='1' then
393
            ns <= code_refill_sram8_1;
394
        else
395
            ns <= ps;
396
        end if;
397 58 ja_rd
 
398 80 ja_rd
    when code_refill_sram8_1 =>
399
        if ws_wait_done='1' then
400
            ns <= code_refill_sram8_2;
401 46 ja_rd
        else
402 80 ja_rd
            ns <= ps;
403 46 ja_rd
        end if;
404 42 ja_rd
 
405 80 ja_rd
    when code_refill_sram8_2 =>
406
        if ws_wait_done='1' then
407
            ns <= code_refill_sram8_3;
408
        else
409
            ns <= ps;
410
        end if;
411
 
412
    when code_refill_sram8_3 =>
413
        if ws_wait_done='1' then
414
            -- If there's a data operation pending, do it now
415
            if write_pending='1' then
416
                case data_wr_attr.mem_type is
417
                when MT_BRAM        => ns <= data_ignore_write;
418
                when MT_SRAM_16B    => ns <= data_writethrough_sram_0a;
419
                when MT_IO_SYNC     => ns <= data_write_io_0;
420
                -- FIXME ignore write to undecoded area (clear pending flag)
421 95 ja_rd
                when others         => ns <= data_ignore_write;
422 80 ja_rd
                end case;
423
 
424
            elsif read_pending='1' then
425
                case data_rd_attr.mem_type is
426
                when MT_BRAM        => ns <= data_refill_bram_0;
427
                when MT_SRAM_16B    => ns <= data_refill_sram_0;
428
                when MT_SRAM_8B     => ns <= data_refill_sram8_0;
429
                when MT_IO_SYNC     => ns <= data_read_io_0;
430
                -- FIXME ignore read from undecoded area (clear pending flag)
431
                when others         => ns <= data_ignore_read;
432
                end case;
433
 
434
            else
435
                ns <= idle;
436
            end if;
437
        else
438
            ns <= ps;
439
        end if;
440
 
441
    -- Data refill & write-through states ---------------------------
442
 
443 46 ja_rd
    when data_write_io_0 =>
444 80 ja_rd
        ns <= idle;
445
 
446 46 ja_rd
    when data_read_io_0 =>
447 80 ja_rd
        ns <= data_read_io_1;
448
 
449 46 ja_rd
    when data_read_io_1 =>
450 80 ja_rd
        ns <= idle;
451 42 ja_rd
 
452 75 ja_rd
    when data_refill_sram8_0 =>
453 80 ja_rd
        if ws_wait_done='1' then
454
            ns <= data_refill_sram8_1;
455 75 ja_rd
        else
456 80 ja_rd
            ns <= ps;
457 75 ja_rd
        end if;
458
 
459
    when data_refill_sram8_1 =>
460 80 ja_rd
        if ws_wait_done='1' then
461
            ns <= data_refill_sram8_2;
462 75 ja_rd
        else
463 80 ja_rd
            ns <= ps;
464 75 ja_rd
        end if;
465
 
466
    when data_refill_sram8_2 =>
467 80 ja_rd
        if ws_wait_done='1' then
468
            ns <= data_refill_sram8_3;
469 75 ja_rd
        else
470 80 ja_rd
            ns <= ps;
471 75 ja_rd
        end if;
472
 
473
    when data_refill_sram8_3 =>
474 80 ja_rd
        if ws_wait_done='1' then
475
            ns <= idle;
476 75 ja_rd
        else
477 80 ja_rd
            ns <= ps;
478 75 ja_rd
        end if;
479
 
480 46 ja_rd
    when data_refill_sram_0 =>
481 80 ja_rd
        if ws_wait_done='1' then
482
            ns <= data_refill_sram_1;
483 72 ja_rd
        else
484 80 ja_rd
            ns <= ps;
485 72 ja_rd
        end if;
486 42 ja_rd
 
487 46 ja_rd
    when data_refill_sram_1 =>
488 80 ja_rd
        if ws_wait_done='1' then
489
            ns <= idle;
490 72 ja_rd
        else
491 80 ja_rd
            ns <= ps;
492 72 ja_rd
        end if;
493 42 ja_rd
 
494 46 ja_rd
    when data_refill_bram_0 =>
495 80 ja_rd
        ns <= data_refill_bram_1;
496 46 ja_rd
 
497
    when data_refill_bram_1 =>
498 80 ja_rd
        ns <= idle;
499 46 ja_rd
 
500 72 ja_rd
    when data_writethrough_sram_0a =>
501 80 ja_rd
        ns <= data_writethrough_sram_0b;
502
 
503 72 ja_rd
    when data_writethrough_sram_0b =>
504 80 ja_rd
        if ws_wait_done='1' then
505
            ns <= data_writethrough_sram_0c;
506 72 ja_rd
        else
507 80 ja_rd
            ns <= ps;
508 72 ja_rd
        end if;
509 80 ja_rd
 
510 72 ja_rd
    when data_writethrough_sram_0c =>
511 80 ja_rd
        ns <= data_writethrough_sram_1a;
512
 
513 72 ja_rd
    when data_writethrough_sram_1a =>
514 80 ja_rd
        ns <= data_writethrough_sram_1b;
515
 
516 72 ja_rd
    when data_writethrough_sram_1b =>
517 80 ja_rd
        if ws_wait_done='1' then
518
            ns <= data_writethrough_sram_1c;
519 72 ja_rd
        else
520 80 ja_rd
            ns <= ps;
521
        end if;
522 46 ja_rd
 
523 72 ja_rd
    when data_writethrough_sram_1c =>
524 80 ja_rd
        ns <= idle;
525 46 ja_rd
 
526 80 ja_rd
 
527 46 ja_rd
    when data_ignore_write =>
528 80 ja_rd
        ns <= idle;
529 46 ja_rd
 
530 72 ja_rd
    when data_ignore_read =>
531 80 ja_rd
        ns <= idle;
532 72 ja_rd
 
533 80 ja_rd
    -- Exception states (something went wrong) ----------------------
534
 
535
    when code_crash =>
536
        -- Attempted to fetch from i/o area. This is a software bug, probably,
537
        -- and should trigger a trap. We have 1 cycle to do something about it.
538
        -- After this cycle, back to normal.
539
        ns <= idle;
540
 
541
    when bug =>
542 58 ja_rd
        -- Something weird happened, we have 1 cycle to do something like raise
543 80 ja_rd
        -- an error flag, etc. After 1 cycle, back to normal.
544
        ns <= idle;
545 46 ja_rd
 
546
    when others =>
547 80 ja_rd
        -- We should never arrive here. If we do we handle it in state bug.
548
        ns <= bug;
549 46 ja_rd
    end case;
550 80 ja_rd
end process control_state_machine_transitions;
551 46 ja_rd
 
552 80 ja_rd
-- load wait state counter when we're entering the state we will wait on
553
load_ws_ctr <= '1' when
554
    (ns=code_refill_sram_0  and ps/=code_refill_sram_0) or
555
    (ns=code_refill_sram_1  and ps/=code_refill_sram_1) or
556
    (ns=code_refill_sram8_0 and ps/=code_refill_sram8_0) or
557
    (ns=code_refill_sram8_1 and ps/=code_refill_sram8_1) or
558
    (ns=code_refill_sram8_2 and ps/=code_refill_sram8_2) or
559
    (ns=code_refill_sram8_3 and ps/=code_refill_sram8_3) or
560
    (ns=data_refill_sram_0  and ps/=data_refill_sram_0) or
561
    (ns=data_refill_sram_1  and ps/=data_refill_sram_1) or
562
    (ns=data_refill_sram8_0 and ps/=data_refill_sram8_0) or
563
    (ns=data_refill_sram8_1 and ps/=data_refill_sram8_1) or
564
    (ns=data_refill_sram8_2 and ps/=data_refill_sram8_2) or
565
    (ns=data_refill_sram8_3 and ps/=data_refill_sram8_3) or
566
    (ns=data_writethrough_sram_0a) or
567
    (ns=data_writethrough_sram_1a)
568 72 ja_rd
    else '0';
569 46 ja_rd
 
570 80 ja_rd
 
571
-- select the wait state counter value as that of read address or write address
572
with ns select ws_value <=
573 72 ja_rd
    data_rd_attr.wait_states    when data_refill_sram_0,
574 75 ja_rd
    data_rd_attr.wait_states    when data_refill_sram_1,
575
    data_rd_attr.wait_states    when data_refill_sram8_0,
576
    data_rd_attr.wait_states    when data_refill_sram8_1,
577
    data_rd_attr.wait_states    when data_refill_sram8_2,
578
    data_rd_attr.wait_states    when data_refill_sram8_3,
579 72 ja_rd
    data_wr_attr.wait_states    when data_writethrough_sram_0a,
580
    data_wr_attr.wait_states    when data_writethrough_sram_1a,
581 80 ja_rd
    code_rd_attr.wait_states    when code_refill_sram_0,
582
    code_rd_attr.wait_states    when code_refill_sram_1,
583
    code_rd_attr.wait_states    when code_refill_sram8_0,
584
    code_rd_attr.wait_states    when code_refill_sram8_1,
585
    code_rd_attr.wait_states    when code_refill_sram8_2,
586
    code_rd_attr.wait_states    when code_refill_sram8_3,
587 72 ja_rd
    data_wr_attr.wait_states    when others;
588 80 ja_rd
 
589
 
590
wait_state_counter_reg:
591 72 ja_rd
process(clk)
592
begin
593
    if clk'event and clk='1' then
594
        if reset='1' then
595 80 ja_rd
            ws_ctr <= (others => '0');
596 72 ja_rd
        else
597 80 ja_rd
            if load_ws_ctr='1' then
598
                ws_ctr <= ws_value;
599
            elsif ws_wait_done='0' then
600
                ws_ctr <= ws_ctr - 1;
601 72 ja_rd
            end if;
602
        end if;
603
    end if;
604 80 ja_rd
end process wait_state_counter_reg;
605 72 ja_rd
 
606 80 ja_rd
ws_wait_done <= '1' when ws_ctr="000" else '0';
607 72 ja_rd
 
608 80 ja_rd
 
609 46 ja_rd
--------------------------------------------------------------------------------
610
-- CPU interface registers and address decoding --------------------------------
611
 
612
 
613
-- Everything coming and going to the CPU is registered, so that the CPU has
614
-- some timing marging.
615
 
616 58 ja_rd
cpu_data_interface_registers:
617 42 ja_rd
process(clk)
618
begin
619
    if clk'event and clk='1' then
620
        if reset='1' then
621 46 ja_rd
            write_pending <= '0';
622
            read_pending <= '0';
623
            byte_we_reg <= "0000";
624 42 ja_rd
        else
625 80 ja_rd
            -- Raise 'read_pending' at 1st cycle of a data read, clear it when
626 46 ja_rd
            -- the read (and/or refill) operation has been done.
627
            -- data_rd_addr_reg always has the addr of any pending read
628 42 ja_rd
            if data_rd_vma='1' then
629 46 ja_rd
                read_pending <= '1';
630 96 ja_rd
                data_rd_addr_reg <= data_addr(31 downto 2);
631 80 ja_rd
            elsif ps=data_refill_sram_1 or
632
                  ps=data_refill_sram8_3 or
633
                  ps=data_refill_bram_1 or
634
                  ps=data_read_io_0 or
635
                  ps=data_ignore_read then
636 46 ja_rd
                read_pending <= '0';
637 42 ja_rd
            end if;
638 46 ja_rd
 
639 80 ja_rd
            -- Raise 'write_pending' at the 1st cycle of a write, clear it when
640 46 ja_rd
            -- the write (writethrough actually) operation has been done.
641
            -- data_wr_addr_reg always has the addr of any pending write
642 80 ja_rd
            if byte_we/="0000" and ps=idle then
643 46 ja_rd
                byte_we_reg <= byte_we;
644
                data_wr_reg <= data_wr;
645 96 ja_rd
                data_wr_addr_reg <= data_addr(31 downto 2);
646 46 ja_rd
                write_pending <= '1';
647 80 ja_rd
            elsif ps=data_writethrough_sram_1b or
648
                  ps=data_write_io_0 or
649
                  ps=data_ignore_write then
650 46 ja_rd
                write_pending <= '0';
651
                byte_we_reg <= "0000";
652
            end if;
653 80 ja_rd
 
654 58 ja_rd
        end if;
655
    end if;
656
end process cpu_data_interface_registers;
657 46 ja_rd
 
658 58 ja_rd
cpu_code_interface_registers:
659
process(clk)
660
begin
661
    if clk'event and clk='1' then
662
        -- Register code fetch addresses only when they are valid; so that
663
        -- code_rd_addr_reg always holds the last fetch address.
664 80 ja_rd
        if code_rd_vma='1' then
665 58 ja_rd
            code_rd_addr_reg <= code_rd_addr;
666 42 ja_rd
        end if;
667
    end if;
668 58 ja_rd
end process cpu_code_interface_registers;
669 42 ja_rd
 
670 58 ja_rd
 
671 46 ja_rd
-- Address decoding ------------------------------------------------------------
672 42 ja_rd
 
673 46 ja_rd
-- Decoding is done on the high bits of the address only, there'll be mirroring.
674
-- Write to areas not explicitly decoded will be silently ignored. Reads will
675
-- get undefined data.
676
 
677
code_rd_addr_mask <= code_rd_addr_reg(31 downto t_addr_decode'low);
678
data_rd_addr_mask <= data_rd_addr_reg(31 downto t_addr_decode'low);
679
data_wr_addr_mask <= data_wr_addr_reg(31 downto t_addr_decode'low);
680
 
681
 
682 64 ja_rd
code_rd_attr <= decode_addr(code_rd_addr_mask);
683
data_rd_attr <= decode_addr(data_rd_addr_mask);
684
data_wr_attr <= decode_addr(data_wr_addr_mask);
685 46 ja_rd
 
686 64 ja_rd
 
687 46 ja_rd
--------------------------------------------------------------------------------
688 58 ja_rd
-- BRAM interface
689 46 ja_rd
 
690 58 ja_rd
 
691 80 ja_rd
-- BRAM address can come from code or data buses
692 58 ja_rd
-- (note both inputs to this mux are register outputs)
693 80 ja_rd
bram_rd_addr <=
694
    data_rd_addr_reg(bram_rd_addr'high downto 2)
695
        when ps=data_refill_bram_0 else
696
    code_rd_addr_reg(bram_rd_addr'high downto 2) ;
697 46 ja_rd
 
698 80 ja_rd
bram_data_rd_vma <= '1' when ps=data_refill_bram_1 else '0';
699 58 ja_rd
 
700
 
701
 
702
--------------------------------------------------------------------------------
703 80 ja_rd
-- Code cache
704 58 ja_rd
 
705
-- All the tag match logic is unfinished and will be simplified away in synth.
706
 
707
-- CPU is wired directly to cache output, no muxes
708 46 ja_rd
code_rd <= code_cache_rd;
709
 
710
-- FIXME Actual 1-word cache functionality is unimplemented yet
711 80 ja_rd
process(clk)
712
begin
713
    if clk'event and clk='1' then
714
        if reset='1' then
715
            code_miss <= '0';
716
        else
717
            code_miss <= code_rd_vma; -- always miss
718
        end if;
719
    end if;
720
end process;
721 46 ja_rd
 
722
-- Read cache code and tag from code store
723
code_cache_rd <= code_cache_store;
724
code_cache_tag <= code_cache_tag_store;
725
 
726
code_cache_memory:
727 42 ja_rd
process(clk)
728
begin
729
    if clk'event and clk='1' then
730
        if reset='1' then
731 46 ja_rd
            -- in the real hardware the tag store can't be reset and it's up
732
            -- to the SW to initialize the cache.
733
            code_cache_tag_store <= (others => '0');
734
            code_cache_store <= (others => '0');
735 42 ja_rd
        else
736 46 ja_rd
            -- Refill cache if necessary
737 80 ja_rd
            if ps=code_refill_bram_1 then
738 46 ja_rd
                code_cache_tag_store <=
739
                    "01" & code_rd_addr_reg(t_code_tag'high-2 downto t_code_tag'low);
740
                code_cache_store <= bram_rd_data;
741 80 ja_rd
            elsif ps=code_refill_sram_1 or ps=code_refill_sram8_3 then
742
                code_cache_tag_store <=
743
                    "01" & code_rd_addr_reg(t_code_tag'high-2 downto t_code_tag'low);
744
                code_cache_store <= sram_rd_data;
745 42 ja_rd
            end if;
746 46 ja_rd
        end if;
747
    end if;
748
end process code_cache_memory;
749
 
750
 
751
--------------------------------------------------------------------------------
752
-- Data cache
753
 
754 58 ja_rd
-- CPU data input mux: direct cache output OR uncached io input
755 80 ja_rd
with ps select data_rd <=
756
    io_rd_data      when data_read_io_1,
757 46 ja_rd
    data_cache_rd   when others;
758
 
759 58 ja_rd
-- All the tag match logic is unfinished and will be simplified away in synth.
760
-- The 'cache' is really a single register.
761 46 ja_rd
data_cache_rd <= data_cache_store;
762
data_cache_tag <= data_cache_tag_store;
763
 
764
data_cache_memory:
765
process(clk)
766
begin
767
    if clk'event and clk='1' then
768
        if reset='1' then
769
            -- in the real hardware the tag store can't be reset and it's up
770
            -- to the SW to initialize the cache.
771
            data_cache_tag_store <= (others => '0');
772
            data_cache_store <= (others => '0');
773
        else
774
            -- Refill data cache if necessary
775 80 ja_rd
            if ps=data_refill_sram_1 or ps=data_refill_sram8_3 then
776 46 ja_rd
                data_cache_tag_store <=
777
                    "01" & data_rd_addr_reg(t_data_tag'high-2 downto t_data_tag'low);
778
                data_cache_store <= sram_rd_data;
779 80 ja_rd
            elsif ps=data_refill_bram_1 then
780 46 ja_rd
                data_cache_tag_store <=
781
                    "01" & data_rd_addr_reg(t_data_tag'high-2 downto t_data_tag'low);
782 80 ja_rd
                data_cache_store <= bram_rd_data;
783 42 ja_rd
            end if;
784
        end if;
785
    end if;
786 46 ja_rd
end process data_cache_memory;
787 42 ja_rd
 
788 58 ja_rd
 
789
--------------------------------------------------------------------------------
790
-- SRAM interface
791
 
792 80 ja_rd
-- Note this signals are meant to be connected directly to FPGA pins (and then
793 58 ja_rd
-- to a SRAM, of course). They are the only signals whose tco we care about.
794
 
795
-- FIXME should add a SRAM CE\ signal
796
 
797
-- SRAM address bus (except for LSB) comes from cpu code or data addr registers
798 42 ja_rd
 
799 80 ja_rd
sram_address(sram_address'high downto 2) <=
800
    data_rd_addr_reg(sram_address'high downto 2)
801
        when   (ps=data_refill_sram_0  or ps=data_refill_sram_1 or
802
                ps=data_refill_sram8_0 or ps=data_refill_sram8_1 or
803
                ps=data_refill_sram8_2 or ps=data_refill_sram8_3) else
804
    code_rd_addr_reg(sram_address'high downto 2)
805
        when   (ps=code_refill_sram_0  or ps=code_refill_sram_1 or
806
                ps=code_refill_sram8_0 or ps=code_refill_sram8_1 or
807
                ps=code_refill_sram8_2 or ps=code_refill_sram8_3) else
808
    data_wr_addr_reg(sram_address'high downto 2);
809
 
810 58 ja_rd
-- SRAM addr bus LSB depends on the D-cache state because we read/write the
811
-- halfwords sequentially in successive cycles.
812 80 ja_rd
sram_address(1) <=
813
    '0'     when   (ps=data_writethrough_sram_0a or
814
                    ps=data_writethrough_sram_0b or
815
                    ps=data_writethrough_sram_0c or
816
                    ps=data_refill_sram8_0 or
817
                    ps=data_refill_sram8_1 or
818
                    ps=data_refill_sram_0 or
819
                    ps=code_refill_sram8_0 or
820
                    ps=code_refill_sram8_1 or
821
                    ps=code_refill_sram_0) else
822
    '1'     when   (ps=data_writethrough_sram_1a or
823
                    ps=data_writethrough_sram_1b or
824
                    ps=data_writethrough_sram_1c or
825
                    ps=data_refill_sram8_2 or
826
                    ps=data_refill_sram8_3 or
827
                    ps=data_refill_sram_1 or
828
                    ps=code_refill_sram8_2 or
829
                    ps=code_refill_sram8_3 or
830
                    ps=code_refill_sram_1)
831
    else '0';
832 42 ja_rd
 
833 80 ja_rd
-- The lowest addr bit will only be used when accessing byte-wide memory, and
834 96 ja_rd
-- even when we're reading word-aligned code (because we need to read the four 
835
-- bytes one by one).
836 80 ja_rd
sram_address(0) <=
837
    '0'     when (ps=data_refill_sram8_0 or ps=data_refill_sram8_2 or
838
                  ps=code_refill_sram8_0 or ps=code_refill_sram8_2) else
839
    '1';
840 75 ja_rd
 
841 80 ja_rd
 
842
-- SRAM databus (when used for output) comes from either hword of the data
843 58 ja_rd
-- write register.
844 80 ja_rd
with ps select sram_data_wr <=
845 72 ja_rd
    data_wr_reg(31 downto 16)   when data_writethrough_sram_0a,
846
    data_wr_reg(31 downto 16)   when data_writethrough_sram_0b,
847
    data_wr_reg(31 downto 16)   when data_writethrough_sram_0c,
848
    data_wr_reg(15 downto  0)   when data_writethrough_sram_1a,
849
    data_wr_reg(15 downto  0)   when data_writethrough_sram_1b,
850 80 ja_rd
    data_wr_reg(15 downto  0)   when data_writethrough_sram_1c,
851 46 ja_rd
    (others => 'Z')             when others;
852 42 ja_rd
 
853 58 ja_rd
-- The byte_we is split in two similarly.
854 80 ja_rd
with ps select sram_byte_we_n <=
855 72 ja_rd
    not byte_we_reg(3 downto 2) when data_writethrough_sram_0b,
856
    not byte_we_reg(1 downto 0) when data_writethrough_sram_1b,
857 46 ja_rd
    "11"                        when others;
858 42 ja_rd
 
859 58 ja_rd
-- SRAM OE\ is only asserted low for read cycles
860 80 ja_rd
sram_oe_n <=
861
    '0' when   (ps=data_refill_sram_0  or ps=data_refill_sram_1 or
862
                ps=data_refill_sram8_0 or ps=data_refill_sram8_1 or
863
                ps=data_refill_sram8_2 or ps=data_refill_sram8_3 or
864
                ps=code_refill_sram_0  or ps=code_refill_sram_1 or
865
                ps=code_refill_sram8_0 or ps=code_refill_sram8_1 or
866
                ps=code_refill_sram8_2 or ps=code_refill_sram8_3) else
867
    '1';
868 42 ja_rd
 
869 80 ja_rd
-- When reading from the SRAM, read word comes from read hword register and
870 58 ja_rd
-- SRAM bus (read register is loaded in previous cycle).
871 80 ja_rd
sram_rd_data <=
872
    sram_rd_data_reg & sram_data_rd(7 downto 0)
873
            when ps=data_refill_sram8_3 or ps=code_refill_sram8_3 else
874
    sram_rd_data_reg(31 downto 16) & sram_data_rd;
875 42 ja_rd
 
876 58 ja_rd
sram_input_halfword_register:
877 46 ja_rd
process(clk)
878
begin
879
    if clk'event and clk='1' then
880 80 ja_rd
        if ps=data_refill_sram_0 or ps=code_refill_sram_0 then
881 75 ja_rd
            sram_rd_data_reg(31 downto 16) <= sram_data_rd;
882 80 ja_rd
        elsif ps=data_refill_sram8_0 or ps=code_refill_sram8_0 then
883 75 ja_rd
            sram_rd_data_reg(31 downto 24) <= sram_data_rd(7 downto 0);
884 80 ja_rd
        elsif ps=data_refill_sram8_1 or ps=code_refill_sram8_1 then
885 75 ja_rd
            sram_rd_data_reg(23 downto 16) <= sram_data_rd(7 downto 0);
886 80 ja_rd
        elsif ps=data_refill_sram8_2 or ps=code_refill_sram8_2 then
887 75 ja_rd
            sram_rd_data_reg(15 downto  8) <= sram_data_rd(7 downto 0);
888 72 ja_rd
        end if;
889 46 ja_rd
    end if;
890 58 ja_rd
end process sram_input_halfword_register;
891 42 ja_rd
 
892
 
893
--------------------------------------------------------------------------------
894 58 ja_rd
-- I/O interface -- IO is assumed to behave like synchronous memory
895 42 ja_rd
 
896 80 ja_rd
io_byte_we <= byte_we_reg when ps=data_write_io_0 else "0000";
897 46 ja_rd
io_rd_addr <= data_rd_addr_reg;
898
io_wr_addr <= data_wr_addr_reg;
899
io_wr_data <= data_wr_reg;
900 80 ja_rd
io_rd_vma <= '1' when ps=data_read_io_0 else '0';
901 42 ja_rd
 
902 58 ja_rd
 
903 46 ja_rd
--------------------------------------------------------------------------------
904 58 ja_rd
-- CPU stall control
905 42 ja_rd
 
906 58 ja_rd
-- Stall the CPU when either state machine needs it
907 80 ja_rd
mem_wait <= (code_wait or data_wait or code_miss) and not reset; -- FIXME
908 42 ja_rd
 
909 58 ja_rd
-- Assert code_wait until the cycle where the CPU has valid code word on its
910
-- code bus
911 80 ja_rd
with ps select code_wait <=
912 46 ja_rd
    '1' when code_refill_bram_0,
913
    '1' when code_refill_bram_1,
914
    '1' when code_refill_bram_2,
915 80 ja_rd
    '1' when code_refill_sram_0,
916
    '1' when code_refill_sram_1,
917
    '1' when code_refill_sram8_0,
918
    '1' when code_refill_sram8_1,
919
    '1' when code_refill_sram8_2,
920
    '1' when code_refill_sram8_3,
921 46 ja_rd
    '0' when others;
922 42 ja_rd
 
923 80 ja_rd
-- Assert data_wait until the cycle where the CPU has valid data word on its
924 58 ja_rd
-- code bus AND no other operations are ongoing that may use the external buses.
925 80 ja_rd
with ps select data_wait <=
926 72 ja_rd
    '1' when data_writethrough_sram_0a,
927
    '1' when data_writethrough_sram_0b,
928
    '1' when data_writethrough_sram_0c,
929
    '1' when data_writethrough_sram_1a,
930
    '1' when data_writethrough_sram_1b,
931
    '1' when data_writethrough_sram_1c,
932 46 ja_rd
    '1' when data_refill_sram_0,
933
    '1' when data_refill_sram_1,
934 75 ja_rd
    '1' when data_refill_sram8_0,
935
    '1' when data_refill_sram8_1,
936
    '1' when data_refill_sram8_2,
937
    '1' when data_refill_sram8_3,
938 46 ja_rd
    '1' when data_refill_bram_0,
939
    '1' when data_refill_bram_1,
940
    '1' when data_read_io_0,
941
    '0' when others;
942
 
943 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.