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

Subversion Repositories ion

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

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

powered by: WebSVN 2.1.0

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