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

Subversion Repositories ion

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

Go to most recent revision | Details | Compare with Previous | View Log

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