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

Subversion Repositories ion

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

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