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

Subversion Repositories ion

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

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