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

Subversion Repositories ion

[/] [ion/] [trunk/] [vhdl/] [demo/] [c2sb_demo.vhdl] - Blame information for rev 200

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

Line No. Rev Author Line
1 46 ja_rd
--##############################################################################
2 2 ja_rd
-- ION MIPS-compatible CPU demo on Terasic DE-1 Cyclone-II starter board
3 46 ja_rd
--##############################################################################
4 2 ja_rd
-- This module is little more than a wrapper around the CPU and its memories.
5 116 ja_rd
-- Synthesize with 'balanced' optimization for best results.
6
--------------------------------------------------------------------------------
7
-- NOTE: See note at bottom of file about optional use of PLL.
8 46 ja_rd
--##############################################################################
9 162 ja_rd
-- Copyright (C) 2011 Jose A. Ruiz
10 161 ja_rd
--                                                              
11
-- This source file may be used and distributed without         
12
-- restriction provided that this copyright statement is not    
13
-- removed from the file and that any derivative work contains  
14
-- the original copyright notice and the associated disclaimer. 
15
--                                                              
16
-- This source file is free software; you can redistribute it   
17
-- and/or modify it under the terms of the GNU Lesser General   
18
-- Public License as published by the Free Software Foundation; 
19
-- either version 2.1 of the License, or (at your option) any   
20
-- later version.                                               
21
--                                                              
22
-- This source is distributed in the hope that it will be       
23
-- useful, but WITHOUT ANY WARRANTY; without even the implied   
24
-- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      
25
-- PURPOSE.  See the GNU Lesser General Public License for more 
26
-- details.                                                     
27
--                                                              
28
-- You should have received a copy of the GNU Lesser General    
29
-- Public License along with this source; if not, download it   
30
-- from http://www.opencores.org/lgpl.shtml
31
--##############################################################################
32 2 ja_rd
 
33
library ieee;
34
use ieee.std_logic_1164.all;
35
use ieee.std_logic_arith.all;
36
use ieee.std_logic_unsigned.all;
37 136 ja_rd
use work.mips_pkg.all; -- Only needed if port debug_info is not OPEN
38 2 ja_rd
 
39
-- FPGA i/o for Terasic DE-1 board
40
-- (Many of the board's i/o devices will go unused in this demo)
41
entity c2sb_demo is
42 59 ja_rd
    port (
43 2 ja_rd
        -- ***** Clocks
44
        clk_50MHz     : in std_logic;
45 116 ja_rd
        clk_27MHz     : in std_logic;
46 2 ja_rd
 
47
        -- ***** Flash 4MB
48
        flash_addr    : out std_logic_vector(21 downto 0);
49
        flash_data    : in std_logic_vector(7 downto 0);
50
        flash_oe_n    : out std_logic;
51
        flash_we_n    : out std_logic;
52
        flash_reset_n : out std_logic;
53
 
54
        -- ***** SRAM 256K x 16
55
        sram_addr     : out std_logic_vector(17 downto 0);
56
        sram_data     : inout std_logic_vector(15 downto 0);
57
        sram_oe_n     : out std_logic;
58
        sram_ub_n     : out std_logic;
59 59 ja_rd
        sram_lb_n     : out std_logic;
60 2 ja_rd
        sram_ce_n     : out std_logic;
61 59 ja_rd
        sram_we_n     : out std_logic;
62 2 ja_rd
 
63
        -- ***** RS-232
64
        rxd           : in std_logic;
65
        txd           : out std_logic;
66
 
67
        -- ***** Switches and buttons
68
        switches      : in std_logic_vector(9 downto 0);
69
        buttons       : in std_logic_vector(3 downto 0);
70
 
71
        -- ***** Quad 7-seg displays
72
        hex0          : out std_logic_vector(0 to 6);
73
        hex1          : out std_logic_vector(0 to 6);
74
        hex2          : out std_logic_vector(0 to 6);
75
        hex3          : out std_logic_vector(0 to 6);
76
 
77
        -- ***** Leds
78
        red_leds      : out std_logic_vector(9 downto 0);
79
        green_leds    : out std_logic_vector(7 downto 0);
80
 
81
        -- ***** SD Card
82
        sd_data       : in  std_logic;
83
        sd_cs         : out std_logic;
84
        sd_cmd        : out std_logic;
85 59 ja_rd
        sd_clk        : out std_logic
86 2 ja_rd
    );
87
end c2sb_demo;
88
 
89
architecture minimal of c2sb_demo is
90
 
91
 
92
--##############################################################################
93 116 ja_rd
-- Parameters
94 2 ja_rd
 
95 116 ja_rd
-- Address size (FIXME: not tested with other values)
96 75 ja_rd
constant SRAM_ADDR_SIZE : integer := 32;
97 46 ja_rd
 
98 116 ja_rd
-- Clock rate selection (affects UART configuration)
99
-- Acceptable values: {27000000, 50000000, 45000000(pll config)}
100
constant CLOCK_FREQ : integer := 50000000;
101
 
102 2 ja_rd
--##############################################################################
103
-- RS232 interface signals
104
 
105
signal rx_rdy :             std_logic;
106
signal tx_rdy :             std_logic;
107
signal rs232_data_rx :      std_logic_vector(7 downto 0);
108
signal rs232_status :       std_logic_vector(7 downto 0);
109
signal data_io_out :        std_logic_vector(7 downto 0);
110
signal io_port :            std_logic_vector(7 downto 0);
111
signal read_rx :            std_logic;
112
signal write_tx :           std_logic;
113
 
114
 
115
--##############################################################################
116 63 ja_rd
-- I/O registers
117 2 ja_rd
 
118
 
119 63 ja_rd
signal sd_clk_reg :         std_logic;
120
signal sd_cs_reg :          std_logic;
121
signal sd_cmd_reg :         std_logic;
122
signal sd_do_reg :          std_logic;
123
 
124
 
125 59 ja_rd
-- CPU access to hex display
126 116 ja_rd
signal reg_display :        std_logic_vector(31 downto 0);
127 2 ja_rd
 
128
 
129
 
130
--##############################################################################
131
-- DE-1 board interface signals
132
 
133 59 ja_rd
-- Synchronization FF chain for asynchronous reset input
134 116 ja_rd
signal reset_sync :         std_logic_vector(3 downto 0);
135 59 ja_rd
 
136 116 ja_rd
-- Reset pushbutton debouncing logic
137
subtype t_debouncer is integer range 0 to CLOCK_FREQ;
138
constant DEBOUNCING_DELAY : t_debouncer := 500;
139
signal debouncing_counter : t_debouncer := (CLOCK_FREQ/1000) * DEBOUNCING_DELAY;
140
 
141 2 ja_rd
-- Quad 7-segment display (non multiplexed) & LEDS
142
signal display_data :       std_logic_vector(15 downto 0);
143 59 ja_rd
signal reg_gleds :          std_logic_vector(7 downto 0);
144 2 ja_rd
 
145
-- Clock & reset signals
146
signal clk_1hz :            std_logic;
147
signal clk_master :         std_logic;
148
signal counter_1hz :        std_logic_vector(25 downto 0);
149
signal reset :              std_logic;
150 116 ja_rd
-- Master clock signal
151 2 ja_rd
signal clk :                std_logic;
152 116 ja_rd
-- Clock from PLL, is a PLL is used
153
signal clk_pll :            std_logic;
154
-- '1' when PLL is locked or when no PLL is used
155
signal pll_locked :         std_logic;
156 2 ja_rd
 
157 116 ja_rd
-- Altera PLL component declaration (in case it's used)
158
-- Note that the MegaWizard component needs to be called 'pll' or the component
159
-- name should be changed in this file.
160
--component pll
161
--    port (
162
--        areset      : in std_logic  := '0';
163
--        inclk0      : in std_logic  := '0';
164
--        c0          : out std_logic ;
165
--        locked      : out std_logic
166
--    );
167
--end component;
168
 
169 2 ja_rd
-- SD control signals
170
signal sd_in :              std_logic;
171
signal reg_sd_dout :        std_logic;
172
signal reg_sd_clk :         std_logic;
173
signal reg_sd_cs :          std_logic;
174
 
175 46 ja_rd
-- MPU interface signals
176 2 ja_rd
signal data_uart :          std_logic_vector(31 downto 0);
177
signal data_uart_status :   std_logic_vector(31 downto 0);
178
signal uart_tx_rdy :        std_logic := '1';
179
signal uart_rx_rdy :        std_logic := '1';
180
 
181 46 ja_rd
signal io_rd_data :         std_logic_vector(31 downto 0);
182
signal io_rd_addr :         std_logic_vector(31 downto 2);
183
signal io_wr_addr :         std_logic_vector(31 downto 2);
184
signal io_wr_data :         std_logic_vector(31 downto 0);
185
signal io_rd_vma :          std_logic;
186
signal io_byte_we :         std_logic_vector(3 downto 0);
187 2 ja_rd
 
188 75 ja_rd
signal mpu_sram_address :   std_logic_vector(SRAM_ADDR_SIZE-1 downto 0);
189
signal mpu_sram_data_rd :   std_logic_vector(15 downto 0);
190
signal mpu_sram_data_wr :   std_logic_vector(15 downto 0);
191 46 ja_rd
signal mpu_sram_byte_we_n : std_logic_vector(1 downto 0);
192
signal mpu_sram_oe_n :      std_logic;
193
 
194 136 ja_rd
signal debug_info :         t_debug_info;
195
 
196 59 ja_rd
-- Converts hex nibble to 7-segment
197
-- Segments ordered as "GFEDCBA"; '0' is ON, '1' is OFF
198
function nibble_to_7seg(nibble : std_logic_vector(3 downto 0))
199
                        return std_logic_vector is
200
begin
201
    case nibble is
202
    when X"0"       => return "0000001";
203
    when X"1"       => return "1001111";
204
    when X"2"       => return "0010010";
205
    when X"3"       => return "0000110";
206
    when X"4"       => return "1001100";
207
    when X"5"       => return "0100100";
208
    when X"6"       => return "0100000";
209
    when X"7"       => return "0001111";
210
    when X"8"       => return "0000000";
211
    when X"9"       => return "0000100";
212
    when X"a"       => return "0001000";
213
    when X"b"       => return "1100000";
214
    when X"c"       => return "0110001";
215
    when X"d"       => return "1000010";
216
    when X"e"       => return "0110000";
217
    when X"f"       => return "0111000";
218
    when others     => return "0111111"; -- can't happen
219
    end case;
220
end function nibble_to_7seg;
221 46 ja_rd
 
222
 
223 2 ja_rd
begin
224
 
225
    mpu: entity work.mips_mpu
226 46 ja_rd
    generic map (
227 116 ja_rd
        CLOCK_FREQ     => CLOCK_FREQ,
228 46 ja_rd
        SRAM_ADDR_SIZE => SRAM_ADDR_SIZE
229
    )
230 2 ja_rd
    port map (
231 200 ja_rd
        interrupt   => "00000000",
232 59 ja_rd
 
233 46 ja_rd
        -- interface to FPGA i/o devices
234
        io_rd_data  => io_rd_data,
235
        io_rd_addr  => io_rd_addr,
236
        io_wr_addr  => io_wr_addr,
237
        io_wr_data  => io_wr_data,
238
        io_rd_vma   => io_rd_vma,
239
        io_byte_we  => io_byte_we,
240 59 ja_rd
 
241 46 ja_rd
        -- interface to asynchronous 16-bit-wide EXTERNAL SRAM
242
        sram_address    => mpu_sram_address,
243 75 ja_rd
        sram_data_rd    => mpu_sram_data_rd,
244
        sram_data_wr    => mpu_sram_data_wr,
245 46 ja_rd
        sram_byte_we_n  => mpu_sram_byte_we_n,
246
        sram_oe_n       => mpu_sram_oe_n,
247 2 ja_rd
 
248
        uart_rxd    => rxd,
249
        uart_txd    => txd,
250 59 ja_rd
 
251 136 ja_rd
        debug_info  => debug_info,
252
 
253 2 ja_rd
        clk         => clk,
254
        reset       => reset
255
    );
256
 
257
 
258 63 ja_rd
--##############################################################################
259
-- I/O registers
260
--##############################################################################
261 2 ja_rd
 
262 63 ja_rd
hex_display_register:
263
process(clk)
264
begin
265
    if clk'event and clk='1' then
266
        if io_byte_we/="0000" and io_wr_addr(15 downto 12)=X"2" then
267 116 ja_rd
            reg_display(15 downto 0) <= io_wr_data(15 downto 0);
268
            --reg_display <= mpu_sram_address;
269 63 ja_rd
        end if;
270
    end if;
271
end process hex_display_register;
272
 
273
sd_control_register:
274
process(clk)
275
begin
276
    if clk'event and clk='1' then
277
        if io_byte_we/="0000" and io_wr_addr(15 downto 12)=X"1" then
278
            if io_wr_addr(5)='1' then
279
                sd_clk_reg <= io_wr_addr(4);
280
            end if;
281
            if io_wr_addr(7)='1' then
282
                sd_cs_reg <= io_wr_addr(6);
283
            end if;
284
            if io_wr_addr(11)='1' then
285
                sd_do_reg <= io_wr_data(0);
286
            end if;
287
        end if;
288
    end if;
289
end process sd_control_register;
290
 
291
 
292
-- Show the SD interface signals on the green leds for debug
293
reg_gleds <= sd_clk_reg & sd_in & sd_do_reg & "000" & sd_cmd_reg & sd_cs_reg;
294
 
295
io_rd_data(0) <= sd_in;
296
io_rd_data(31 downto 22) <= switches;
297
 
298
 
299
 
300 59 ja_rd
-- red leds (light with '1') -- some CPU control signals
301 136 ja_rd
red_leds(0) <= debug_info.cache_enabled;
302
red_leds(1) <= debug_info.unmapped_access;
303 2 ja_rd
red_leds(2) <= '0';
304
red_leds(3) <= '0';
305
red_leds(4) <= '0';
306
red_leds(5) <= '0';
307
red_leds(6) <= '0';
308
red_leds(7) <= '0';
309
red_leds(8) <= '0';
310
red_leds(9) <= clk_1hz;
311
 
312
 
313
--##############################################################################
314
-- terasIC Cyclone II STARTER KIT BOARD -- interface to on-board devices
315
--##############################################################################
316
 
317
--##############################################################################
318 75 ja_rd
-- FLASH (connected to the same mup bus as the sram)
319 2 ja_rd
--##############################################################################
320
 
321 75 ja_rd
flash_we_n <= '1'; -- all write control signals inactive
322 2 ja_rd
flash_reset_n <= '1';
323
 
324 75 ja_rd
flash_addr(21 downto 18) <= (others => '0');
325
flash_addr(17 downto  0) <= mpu_sram_address(17 downto 0); -- FIXME
326 2 ja_rd
 
327 75 ja_rd
-- Flash is decoded at 0xb0000000
328
flash_oe_n <= '0'
329
    when mpu_sram_address(31 downto 27)="10110" and mpu_sram_oe_n='0'
330
    else '1';
331
 
332
 
333
 
334 2 ja_rd
--##############################################################################
335 75 ja_rd
-- SRAM
336 2 ja_rd
--##############################################################################
337
 
338 75 ja_rd
sram_addr <= mpu_sram_address(sram_addr'high+1 downto 1);
339
sram_oe_n <= '0'
340
    when mpu_sram_address(31 downto 27)="00000" and mpu_sram_oe_n='0'
341
    else '1';
342
 
343 46 ja_rd
sram_ub_n <= mpu_sram_byte_we_n(1) and mpu_sram_oe_n;
344
sram_lb_n <= mpu_sram_byte_we_n(0) and mpu_sram_oe_n;
345
sram_ce_n <= '0';
346
sram_we_n <= mpu_sram_byte_we_n(1) and mpu_sram_byte_we_n(0);
347 2 ja_rd
 
348 75 ja_rd
sram_data <= mpu_sram_data_wr when mpu_sram_byte_we_n/="11" else (others => 'Z');
349 2 ja_rd
 
350 75 ja_rd
-- The only reason we need this mux is because we have the static RAM and the
351
-- static flash in separate FPGA pins, whereas in a real world application they
352
-- would be on the same data+address bus
353
mpu_sram_data_rd <=
354
    -- SRAM is decoded at 0x00000000
355
    sram_data when mpu_sram_address(31 downto 27)="00000" else
356
    X"00" & flash_data;
357
 
358
 
359
 
360 2 ja_rd
--##############################################################################
361
-- RESET, CLOCK
362
--##############################################################################
363
 
364
-- Use button 3 as reset
365 75 ja_rd
-- This FF chain only prevents metastability trouble, it does not help with
366
-- switching bounces.
367 116 ja_rd
-- (NOTE: the anti-metastability logic is probably not needed when we include 
368
-- the debouncing logic)
369 59 ja_rd
reset_synchronization:
370
process(clk)
371
begin
372
    if clk'event and clk='1' then
373 116 ja_rd
        reset_sync(3) <= not buttons(2);
374
        reset_sync(2) <= reset_sync(3);
375 59 ja_rd
        reset_sync(1) <= reset_sync(2);
376
        reset_sync(0) <= reset_sync(1);
377
    end if;
378
end process reset_synchronization;
379 2 ja_rd
 
380 116 ja_rd
reset_debouncing:
381
process(clk)
382
begin
383
    if clk'event and clk='1' then
384
        if reset_sync(0)='1' and reset_sync(1)='0' then
385
            debouncing_counter <= (CLOCK_FREQ/1000) * DEBOUNCING_DELAY;
386
        else
387
            if debouncing_counter /= 0 then
388
                debouncing_counter <= debouncing_counter - 1;
389
            end if;
390
        end if;
391
    end if;
392
end process reset_debouncing;
393 2 ja_rd
 
394 116 ja_rd
--
395
reset <= '1' when debouncing_counter /= 0 or pll_locked='0' else '0';
396 59 ja_rd
 
397 2 ja_rd
-- Generate a 1-Hz 'clock' to flash a LED for visual reference.
398 116 ja_rd
process(clk)
399 2 ja_rd
begin
400 116 ja_rd
  if clk'event and clk='1' then
401 2 ja_rd
    if reset = '1' then
402
      clk_1hz <= '0';
403
      counter_1hz <= (others => '0');
404
    else
405 116 ja_rd
      if conv_integer(counter_1hz) = CLOCK_FREQ-1 then
406 2 ja_rd
        counter_1hz <= (others => '0');
407
        clk_1hz <= not clk_1hz;
408
      else
409
        counter_1hz <= counter_1hz + 1;
410
      end if;
411
    end if;
412
  end if;
413
end process;
414
 
415 116 ja_rd
-- Master clock is external 50MHz or 27MHz oscillator
416
 
417
slow_clock:
418
if CLOCK_FREQ = 27000000 generate
419
clk <= clk_27MHz;
420
pll_locked <=  '1';
421
end generate;
422
 
423
fast_clock:
424
if CLOCK_FREQ = 50000000 generate
425 2 ja_rd
clk <= clk_50MHz;
426 116 ja_rd
pll_locked <=  '1';
427
end generate;
428 2 ja_rd
 
429 116 ja_rd
--pll_clock:
430
--if CLOCK_FREQ /= 27000000 and CLOCK_FREQ/=50000000 generate
431
---- Assume PLL black box is properly configured for whatever the clock rate is...
432
--input_clock_pll: component pll
433
--    port map(
434
--        areset  => '0',
435
--        inclk0  => clk_50MHz,
436
--        c0      => clk_pll,
437
--        locked  => pll_locked
438
--    );
439
--
440
----clk <= clk_1hz when reg_display(31 downto 27)="10110" else clk_pll;
441
--clk <= clk_pll;
442
--end generate;
443 2 ja_rd
 
444 116 ja_rd
 
445 2 ja_rd
--##############################################################################
446
-- LEDS, SWITCHES
447
--##############################################################################
448
 
449
-- Display the contents of a debug register at the green leds bar
450 59 ja_rd
green_leds <= reg_gleds;
451 2 ja_rd
 
452
 
453
--##############################################################################
454
-- QUAD 7-SEGMENT DISPLAYS
455
--##############################################################################
456
 
457 59 ja_rd
-- Show contents of debug register in hex display
458 116 ja_rd
display_data <=
459
    reg_display(15 downto 0);-- when switches(0)='0' else 
460
    --reg_display(31 downto 16);
461 2 ja_rd
 
462 59 ja_rd
 
463 2 ja_rd
-- 7-segment encoders; the dev board displays are not multiplexed or encoded
464 59 ja_rd
hex3 <= nibble_to_7seg(display_data(15 downto 12));
465
hex2 <= nibble_to_7seg(display_data(11 downto  8));
466
hex1 <= nibble_to_7seg(display_data( 7 downto  4));
467
hex0 <= nibble_to_7seg(display_data( 3 downto  0));
468 2 ja_rd
 
469
--##############################################################################
470
-- SD card interface
471
--##############################################################################
472
 
473 75 ja_rd
-- Connect to FFs for use in bit-banged interface (still unused)
474 63 ja_rd
sd_cs       <= sd_cs_reg;
475
sd_cmd      <= sd_do_reg;
476
sd_clk      <= sd_clk_reg;
477
sd_in       <= sd_data;
478 2 ja_rd
 
479 63 ja_rd
 
480 2 ja_rd
--##############################################################################
481
-- SERIAL
482
--##############################################################################
483
 
484
--  Embedded in the MPU entity
485
 
486
end minimal;
487 116 ja_rd
 
488
--------------------------------------------------------------------------------
489
-- NOTE: Optional use of a PLL
490
-- 
491
-- In order to try the core with any clock other the 50 and 27MHz oscillators 
492
-- readily available onboard we need to use a PLL.
493
-- Unfortunately, Quartus-II won't let you just instantiate a PLL like ISE does.
494
-- Instead, you have to build a PLL module using the MegaWizard tool.
495
-- A nasty consequence of this is that the PLL can't be reconfigured without
496
-- rebuilding it with the MW tool, and a bunch of ugly binary files have to be 
497
-- committed to SVN if the project is to be complete.
498
-- When I figure up what files need to be committed to SVN I will. Meanwhile you
499
-- have to build the module yourself if you want to u se a PLL -- Sorry!
500
-- At least it is very straightforward -- create an ALTPLL variation (from the 
501
-- IO module library) named 'pll' with a 45MHz clock at output c0, that's it.
502
--
503
-- Please note that the system will run at >50MHz when using 'balanced' 
504
-- synthesis. Only the 'area optimized' synthesis may give you trouble.
505
--------------------------------------------------------------------------------

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.