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

Subversion Repositories ion

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

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 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 233 ja_rd
subtype t_debouncer is integer range 0 to CLOCK_FREQ*4;
142
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
 
261 63 ja_rd
--##############################################################################
262 234 ja_rd
-- GPIO and LEDs
263 63 ja_rd
--##############################################################################
264 2 ja_rd
 
265 234 ja_rd
---- LEDS -- We'll use the LEDs to display debug info --------------------------
266 63 ja_rd
 
267 234 ja_rd
-- HEX display is mostly unused
268
reg_display <= p0_out(31 downto 16);
269
 
270 63 ja_rd
-- Show the SD interface signals on the green leds for debug
271 234 ja_rd
reg_gleds <= p1_in(0) & "0000" & p0_out(2 downto 0);
272 63 ja_rd
 
273 234 ja_rd
-- Red leds (light with '1') -- some CPU control signals
274 136 ja_rd
red_leds(0) <= debug_info.cache_enabled;
275
red_leds(1) <= debug_info.unmapped_access;
276 2 ja_rd
red_leds(2) <= '0';
277
red_leds(3) <= '0';
278
red_leds(4) <= '0';
279
red_leds(5) <= '0';
280
red_leds(6) <= '0';
281
red_leds(7) <= '0';
282
red_leds(8) <= '0';
283
red_leds(9) <= clk_1hz;
284
 
285
 
286
--##############################################################################
287
-- terasIC Cyclone II STARTER KIT BOARD -- interface to on-board devices
288
--##############################################################################
289
 
290
--##############################################################################
291 75 ja_rd
-- FLASH (connected to the same mup bus as the sram)
292 2 ja_rd
--##############################################################################
293
 
294 75 ja_rd
flash_we_n <= '1'; -- all write control signals inactive
295 2 ja_rd
flash_reset_n <= '1';
296
 
297 75 ja_rd
flash_addr(21 downto 18) <= (others => '0');
298
flash_addr(17 downto  0) <= mpu_sram_address(17 downto 0); -- FIXME
299 2 ja_rd
 
300 75 ja_rd
-- Flash is decoded at 0xb0000000
301
flash_oe_n <= '0'
302
    when mpu_sram_address(31 downto 27)="10110" and mpu_sram_oe_n='0'
303
    else '1';
304
 
305
 
306
 
307 2 ja_rd
--##############################################################################
308 75 ja_rd
-- SRAM
309 2 ja_rd
--##############################################################################
310
 
311 75 ja_rd
sram_addr <= mpu_sram_address(sram_addr'high+1 downto 1);
312
sram_oe_n <= '0'
313
    when mpu_sram_address(31 downto 27)="00000" and mpu_sram_oe_n='0'
314
    else '1';
315
 
316 46 ja_rd
sram_ub_n <= mpu_sram_byte_we_n(1) and mpu_sram_oe_n;
317
sram_lb_n <= mpu_sram_byte_we_n(0) and mpu_sram_oe_n;
318
sram_ce_n <= '0';
319
sram_we_n <= mpu_sram_byte_we_n(1) and mpu_sram_byte_we_n(0);
320 2 ja_rd
 
321 75 ja_rd
sram_data <= mpu_sram_data_wr when mpu_sram_byte_we_n/="11" else (others => 'Z');
322 2 ja_rd
 
323 75 ja_rd
-- The only reason we need this mux is because we have the static RAM and the
324
-- static flash in separate FPGA pins, whereas in a real world application they
325
-- would be on the same data+address bus
326
mpu_sram_data_rd <=
327
    -- SRAM is decoded at 0x00000000
328
    sram_data when mpu_sram_address(31 downto 27)="00000" else
329
    X"00" & flash_data;
330
 
331
 
332
 
333 2 ja_rd
--##############################################################################
334
-- RESET, CLOCK
335
--##############################################################################
336
 
337 233 ja_rd
 
338 75 ja_rd
-- This FF chain only prevents metastability trouble, it does not help with
339
-- switching bounces.
340 116 ja_rd
-- (NOTE: the anti-metastability logic is probably not needed when we include 
341
-- the debouncing logic)
342 59 ja_rd
reset_synchronization:
343
process(clk)
344
begin
345
    if clk'event and clk='1' then
346 233 ja_rd
        reset_sync(3) <= not switches(9);
347 116 ja_rd
        reset_sync(2) <= reset_sync(3);
348 59 ja_rd
        reset_sync(1) <= reset_sync(2);
349
        reset_sync(0) <= reset_sync(1);
350
    end if;
351
end process reset_synchronization;
352 2 ja_rd
 
353 116 ja_rd
reset_debouncing:
354
process(clk)
355
begin
356
    if clk'event and clk='1' then
357
        if reset_sync(0)='1' and reset_sync(1)='0' then
358
            debouncing_counter <= (CLOCK_FREQ/1000) * DEBOUNCING_DELAY;
359
        else
360
            if debouncing_counter /= 0 then
361
                debouncing_counter <= debouncing_counter - 1;
362
            end if;
363
        end if;
364
    end if;
365
end process reset_debouncing;
366 2 ja_rd
 
367 116 ja_rd
--
368
reset <= '1' when debouncing_counter /= 0 or pll_locked='0' else '0';
369 59 ja_rd
 
370 2 ja_rd
-- Generate a 1-Hz 'clock' to flash a LED for visual reference.
371 116 ja_rd
process(clk)
372 2 ja_rd
begin
373 116 ja_rd
  if clk'event and clk='1' then
374 2 ja_rd
    if reset = '1' then
375
      clk_1hz <= '0';
376
      counter_1hz <= (others => '0');
377
    else
378 116 ja_rd
      if conv_integer(counter_1hz) = CLOCK_FREQ-1 then
379 2 ja_rd
        counter_1hz <= (others => '0');
380
        clk_1hz <= not clk_1hz;
381
      else
382
        counter_1hz <= counter_1hz + 1;
383
      end if;
384
    end if;
385
  end if;
386
end process;
387
 
388 116 ja_rd
-- Master clock is external 50MHz or 27MHz oscillator
389
 
390
slow_clock:
391
if CLOCK_FREQ = 27000000 generate
392
clk <= clk_27MHz;
393
pll_locked <=  '1';
394
end generate;
395
 
396
fast_clock:
397
if CLOCK_FREQ = 50000000 generate
398 2 ja_rd
clk <= clk_50MHz;
399 116 ja_rd
pll_locked <=  '1';
400
end generate;
401 2 ja_rd
 
402 116 ja_rd
--pll_clock:
403
--if CLOCK_FREQ /= 27000000 and CLOCK_FREQ/=50000000 generate
404
---- Assume PLL black box is properly configured for whatever the clock rate is...
405
--input_clock_pll: component pll
406
--    port map(
407
--        areset  => '0',
408
--        inclk0  => clk_50MHz,
409
--        c0      => clk_pll,
410
--        locked  => pll_locked
411
--    );
412
--
413
--clk <= clk_pll;
414
--end generate;
415 2 ja_rd
 
416 116 ja_rd
 
417 2 ja_rd
--##############################################################################
418
-- LEDS, SWITCHES
419
--##############################################################################
420
 
421
-- Display the contents of a debug register at the green leds bar
422 59 ja_rd
green_leds <= reg_gleds;
423 2 ja_rd
 
424
 
425
--##############################################################################
426
-- QUAD 7-SEGMENT DISPLAYS
427
--##############################################################################
428
 
429 59 ja_rd
-- Show contents of debug register in hex display
430 234 ja_rd
display_data <= reg_display;
431
 
432 2 ja_rd
 
433
-- 7-segment encoders; the dev board displays are not multiplexed or encoded
434 59 ja_rd
hex3 <= nibble_to_7seg(display_data(15 downto 12));
435
hex2 <= nibble_to_7seg(display_data(11 downto  8));
436
hex1 <= nibble_to_7seg(display_data( 7 downto  4));
437
hex0 <= nibble_to_7seg(display_data( 3 downto  0));
438 2 ja_rd
 
439
--##############################################################################
440
-- SD card interface
441
--##############################################################################
442
 
443 75 ja_rd
-- Connect to FFs for use in bit-banged interface (still unused)
444 234 ja_rd
sd_cs       <= p0_out(0);       -- SPI CS
445
sd_cmd      <= p0_out(2);       -- SPI DI
446
sd_clk      <= p0_out(1);       -- SPI SCLK
447
p1_in(0)    <= sd_data;         -- SPI DO
448 2 ja_rd
 
449 63 ja_rd
 
450 2 ja_rd
--##############################################################################
451
-- SERIAL
452
--##############################################################################
453
 
454
--  Embedded in the MPU entity
455
 
456
end minimal;
457 116 ja_rd
 
458
--------------------------------------------------------------------------------
459
-- NOTE: Optional use of a PLL
460
-- 
461
-- In order to try the core with any clock other the 50 and 27MHz oscillators 
462
-- readily available onboard we need to use a PLL.
463
-- Unfortunately, Quartus-II won't let you just instantiate a PLL like ISE does.
464
-- Instead, you have to build a PLL module using the MegaWizard tool.
465
-- A nasty consequence of this is that the PLL can't be reconfigured without
466
-- rebuilding it with the MW tool, and a bunch of ugly binary files have to be 
467
-- committed to SVN if the project is to be complete.
468
-- When I figure up what files need to be committed to SVN I will. Meanwhile you
469
-- have to build the module yourself if you want to u se a PLL -- Sorry!
470
-- At least it is very straightforward -- create an ALTPLL variation (from the 
471
-- IO module library) named 'pll' with a 45MHz clock at output c0, that's it.
472
--
473
-- Please note that the system will run at >50MHz when using 'balanced' 
474
-- synthesis. Only the 'area optimized' synthesis may give you trouble.
475
--------------------------------------------------------------------------------

powered by: WebSVN 2.1.0

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