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

Subversion Repositories ion

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

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 63 ja_rd
signal sd_clk_reg :         std_logic;
122
signal sd_cs_reg :          std_logic;
123
signal sd_cmd_reg :         std_logic;
124
signal sd_do_reg :          std_logic;
125
 
126
 
127 59 ja_rd
-- CPU access to hex display
128 116 ja_rd
signal reg_display :        std_logic_vector(31 downto 0);
129 2 ja_rd
 
130
 
131
 
132
--##############################################################################
133
-- DE-1 board interface signals
134
 
135 59 ja_rd
-- Synchronization FF chain for asynchronous reset input
136 116 ja_rd
signal reset_sync :         std_logic_vector(3 downto 0);
137 59 ja_rd
 
138 116 ja_rd
-- Reset pushbutton debouncing logic
139 233 ja_rd
subtype t_debouncer is integer range 0 to CLOCK_FREQ*4;
140
constant DEBOUNCING_DELAY : t_debouncer := 1500;
141 116 ja_rd
signal debouncing_counter : t_debouncer := (CLOCK_FREQ/1000) * DEBOUNCING_DELAY;
142
 
143 2 ja_rd
-- Quad 7-segment display (non multiplexed) & LEDS
144
signal display_data :       std_logic_vector(15 downto 0);
145 59 ja_rd
signal reg_gleds :          std_logic_vector(7 downto 0);
146 2 ja_rd
 
147
-- Clock & reset signals
148
signal clk_1hz :            std_logic;
149
signal clk_master :         std_logic;
150
signal counter_1hz :        std_logic_vector(25 downto 0);
151
signal reset :              std_logic;
152 116 ja_rd
-- Master clock signal
153 2 ja_rd
signal clk :                std_logic;
154 116 ja_rd
-- Clock from PLL, is a PLL is used
155
signal clk_pll :            std_logic;
156
-- '1' when PLL is locked or when no PLL is used
157
signal pll_locked :         std_logic;
158 2 ja_rd
 
159 116 ja_rd
-- Altera PLL component declaration (in case it's used)
160
-- Note that the MegaWizard component needs to be called 'pll' or the component
161
-- name should be changed in this file.
162
--component pll
163
--    port (
164
--        areset      : in std_logic  := '0';
165
--        inclk0      : in std_logic  := '0';
166
--        c0          : out std_logic ;
167
--        locked      : out std_logic
168
--    );
169
--end component;
170
 
171 2 ja_rd
-- SD control signals
172
signal sd_in :              std_logic;
173
signal reg_sd_dout :        std_logic;
174
signal reg_sd_clk :         std_logic;
175
signal reg_sd_cs :          std_logic;
176
 
177 46 ja_rd
-- MPU interface signals
178 2 ja_rd
signal data_uart :          std_logic_vector(31 downto 0);
179
signal data_uart_status :   std_logic_vector(31 downto 0);
180
signal uart_tx_rdy :        std_logic := '1';
181
signal uart_rx_rdy :        std_logic := '1';
182
 
183 46 ja_rd
signal io_rd_data :         std_logic_vector(31 downto 0);
184
signal io_rd_addr :         std_logic_vector(31 downto 2);
185
signal io_wr_addr :         std_logic_vector(31 downto 2);
186
signal io_wr_data :         std_logic_vector(31 downto 0);
187
signal io_rd_vma :          std_logic;
188
signal io_byte_we :         std_logic_vector(3 downto 0);
189 2 ja_rd
 
190 75 ja_rd
signal mpu_sram_address :   std_logic_vector(SRAM_ADDR_SIZE-1 downto 0);
191
signal mpu_sram_data_rd :   std_logic_vector(15 downto 0);
192
signal mpu_sram_data_wr :   std_logic_vector(15 downto 0);
193 46 ja_rd
signal mpu_sram_byte_we_n : std_logic_vector(1 downto 0);
194
signal mpu_sram_oe_n :      std_logic;
195
 
196 136 ja_rd
signal debug_info :         t_debug_info;
197
 
198 59 ja_rd
-- Converts hex nibble to 7-segment
199
-- Segments ordered as "GFEDCBA"; '0' is ON, '1' is OFF
200
function nibble_to_7seg(nibble : std_logic_vector(3 downto 0))
201
                        return std_logic_vector is
202
begin
203
    case nibble is
204
    when X"0"       => return "0000001";
205
    when X"1"       => return "1001111";
206
    when X"2"       => return "0010010";
207
    when X"3"       => return "0000110";
208
    when X"4"       => return "1001100";
209
    when X"5"       => return "0100100";
210
    when X"6"       => return "0100000";
211
    when X"7"       => return "0001111";
212
    when X"8"       => return "0000000";
213
    when X"9"       => return "0000100";
214
    when X"a"       => return "0001000";
215
    when X"b"       => return "1100000";
216
    when X"c"       => return "0110001";
217
    when X"d"       => return "1000010";
218
    when X"e"       => return "0110000";
219
    when X"f"       => return "0111000";
220
    when others     => return "0111111"; -- can't happen
221
    end case;
222
end function nibble_to_7seg;
223 46 ja_rd
 
224
 
225 2 ja_rd
begin
226
 
227 226 ja_rd
    mpu: entity work.mips_soc
228 46 ja_rd
    generic map (
229 233 ja_rd
        OBJECT_CODE    => obj_code,
230
        BOOT_BRAM_SIZE => work.obj_code_pkg.BRAM_SIZE,
231 116 ja_rd
        CLOCK_FREQ     => CLOCK_FREQ,
232 46 ja_rd
        SRAM_ADDR_SIZE => SRAM_ADDR_SIZE
233
    )
234 2 ja_rd
    port map (
235 200 ja_rd
        interrupt   => "00000000",
236 59 ja_rd
 
237 46 ja_rd
        -- interface to FPGA i/o devices
238
        io_rd_data  => io_rd_data,
239
        io_rd_addr  => io_rd_addr,
240
        io_wr_addr  => io_wr_addr,
241
        io_wr_data  => io_wr_data,
242
        io_rd_vma   => io_rd_vma,
243
        io_byte_we  => io_byte_we,
244 59 ja_rd
 
245 46 ja_rd
        -- interface to asynchronous 16-bit-wide EXTERNAL SRAM
246
        sram_address    => mpu_sram_address,
247 75 ja_rd
        sram_data_rd    => mpu_sram_data_rd,
248
        sram_data_wr    => mpu_sram_data_wr,
249 46 ja_rd
        sram_byte_we_n  => mpu_sram_byte_we_n,
250
        sram_oe_n       => mpu_sram_oe_n,
251 2 ja_rd
 
252
        uart_rxd    => rxd,
253
        uart_txd    => txd,
254 59 ja_rd
 
255 136 ja_rd
        debug_info  => debug_info,
256
 
257 2 ja_rd
        clk         => clk,
258
        reset       => reset
259
    );
260
 
261
 
262 63 ja_rd
--##############################################################################
263
-- I/O registers
264
--##############################################################################
265 2 ja_rd
 
266 63 ja_rd
hex_display_register:
267
process(clk)
268
begin
269
    if clk'event and clk='1' then
270
        if io_byte_we/="0000" and io_wr_addr(15 downto 12)=X"2" then
271 226 ja_rd
            --reg_display(15 downto 0) <= io_wr_data(15 downto 0);
272
            reg_display <= mpu_sram_address;
273 63 ja_rd
        end if;
274
    end if;
275
end process hex_display_register;
276
 
277
sd_control_register:
278
process(clk)
279
begin
280
    if clk'event and clk='1' then
281
        if io_byte_we/="0000" and io_wr_addr(15 downto 12)=X"1" then
282
            if io_wr_addr(5)='1' then
283
                sd_clk_reg <= io_wr_addr(4);
284
            end if;
285
            if io_wr_addr(7)='1' then
286
                sd_cs_reg <= io_wr_addr(6);
287
            end if;
288
            if io_wr_addr(11)='1' then
289
                sd_do_reg <= io_wr_data(0);
290
            end if;
291
        end if;
292
    end if;
293
end process sd_control_register;
294
 
295
 
296
-- Show the SD interface signals on the green leds for debug
297
reg_gleds <= sd_clk_reg & sd_in & sd_do_reg & "000" & sd_cmd_reg & sd_cs_reg;
298
 
299
io_rd_data(0) <= sd_in;
300
io_rd_data(31 downto 22) <= switches;
301
 
302
 
303
 
304 59 ja_rd
-- red leds (light with '1') -- some CPU control signals
305 136 ja_rd
red_leds(0) <= debug_info.cache_enabled;
306
red_leds(1) <= debug_info.unmapped_access;
307 2 ja_rd
red_leds(2) <= '0';
308
red_leds(3) <= '0';
309
red_leds(4) <= '0';
310
red_leds(5) <= '0';
311
red_leds(6) <= '0';
312
red_leds(7) <= '0';
313
red_leds(8) <= '0';
314
red_leds(9) <= clk_1hz;
315
 
316
 
317
--##############################################################################
318
-- terasIC Cyclone II STARTER KIT BOARD -- interface to on-board devices
319
--##############################################################################
320
 
321
--##############################################################################
322 75 ja_rd
-- FLASH (connected to the same mup bus as the sram)
323 2 ja_rd
--##############################################################################
324
 
325 75 ja_rd
flash_we_n <= '1'; -- all write control signals inactive
326 2 ja_rd
flash_reset_n <= '1';
327
 
328 75 ja_rd
flash_addr(21 downto 18) <= (others => '0');
329
flash_addr(17 downto  0) <= mpu_sram_address(17 downto 0); -- FIXME
330 2 ja_rd
 
331 75 ja_rd
-- Flash is decoded at 0xb0000000
332
flash_oe_n <= '0'
333
    when mpu_sram_address(31 downto 27)="10110" and mpu_sram_oe_n='0'
334
    else '1';
335
 
336
 
337
 
338 2 ja_rd
--##############################################################################
339 75 ja_rd
-- SRAM
340 2 ja_rd
--##############################################################################
341
 
342 75 ja_rd
sram_addr <= mpu_sram_address(sram_addr'high+1 downto 1);
343
sram_oe_n <= '0'
344
    when mpu_sram_address(31 downto 27)="00000" and mpu_sram_oe_n='0'
345
    else '1';
346
 
347 46 ja_rd
sram_ub_n <= mpu_sram_byte_we_n(1) and mpu_sram_oe_n;
348
sram_lb_n <= mpu_sram_byte_we_n(0) and mpu_sram_oe_n;
349
sram_ce_n <= '0';
350
sram_we_n <= mpu_sram_byte_we_n(1) and mpu_sram_byte_we_n(0);
351 2 ja_rd
 
352 75 ja_rd
sram_data <= mpu_sram_data_wr when mpu_sram_byte_we_n/="11" else (others => 'Z');
353 2 ja_rd
 
354 75 ja_rd
-- The only reason we need this mux is because we have the static RAM and the
355
-- static flash in separate FPGA pins, whereas in a real world application they
356
-- would be on the same data+address bus
357
mpu_sram_data_rd <=
358
    -- SRAM is decoded at 0x00000000
359
    sram_data when mpu_sram_address(31 downto 27)="00000" else
360
    X"00" & flash_data;
361
 
362
 
363
 
364 2 ja_rd
--##############################################################################
365
-- RESET, CLOCK
366
--##############################################################################
367
 
368 233 ja_rd
 
369 75 ja_rd
-- This FF chain only prevents metastability trouble, it does not help with
370
-- switching bounces.
371 116 ja_rd
-- (NOTE: the anti-metastability logic is probably not needed when we include 
372
-- the debouncing logic)
373 59 ja_rd
reset_synchronization:
374
process(clk)
375
begin
376
    if clk'event and clk='1' then
377 233 ja_rd
        reset_sync(3) <= not switches(9);
378 116 ja_rd
        reset_sync(2) <= reset_sync(3);
379 59 ja_rd
        reset_sync(1) <= reset_sync(2);
380
        reset_sync(0) <= reset_sync(1);
381
    end if;
382
end process reset_synchronization;
383 2 ja_rd
 
384 116 ja_rd
reset_debouncing:
385
process(clk)
386
begin
387
    if clk'event and clk='1' then
388
        if reset_sync(0)='1' and reset_sync(1)='0' then
389
            debouncing_counter <= (CLOCK_FREQ/1000) * DEBOUNCING_DELAY;
390
        else
391
            if debouncing_counter /= 0 then
392
                debouncing_counter <= debouncing_counter - 1;
393
            end if;
394
        end if;
395
    end if;
396
end process reset_debouncing;
397 2 ja_rd
 
398 116 ja_rd
--
399
reset <= '1' when debouncing_counter /= 0 or pll_locked='0' else '0';
400 59 ja_rd
 
401 2 ja_rd
-- Generate a 1-Hz 'clock' to flash a LED for visual reference.
402 116 ja_rd
process(clk)
403 2 ja_rd
begin
404 116 ja_rd
  if clk'event and clk='1' then
405 2 ja_rd
    if reset = '1' then
406
      clk_1hz <= '0';
407
      counter_1hz <= (others => '0');
408
    else
409 116 ja_rd
      if conv_integer(counter_1hz) = CLOCK_FREQ-1 then
410 2 ja_rd
        counter_1hz <= (others => '0');
411
        clk_1hz <= not clk_1hz;
412
      else
413
        counter_1hz <= counter_1hz + 1;
414
      end if;
415
    end if;
416
  end if;
417
end process;
418
 
419 116 ja_rd
-- Master clock is external 50MHz or 27MHz oscillator
420
 
421
slow_clock:
422
if CLOCK_FREQ = 27000000 generate
423
clk <= clk_27MHz;
424
pll_locked <=  '1';
425
end generate;
426
 
427
fast_clock:
428
if CLOCK_FREQ = 50000000 generate
429 2 ja_rd
clk <= clk_50MHz;
430 116 ja_rd
pll_locked <=  '1';
431
end generate;
432 2 ja_rd
 
433 116 ja_rd
--pll_clock:
434
--if CLOCK_FREQ /= 27000000 and CLOCK_FREQ/=50000000 generate
435
---- Assume PLL black box is properly configured for whatever the clock rate is...
436
--input_clock_pll: component pll
437
--    port map(
438
--        areset  => '0',
439
--        inclk0  => clk_50MHz,
440
--        c0      => clk_pll,
441
--        locked  => pll_locked
442
--    );
443
--
444
----clk <= clk_1hz when reg_display(31 downto 27)="10110" else clk_pll;
445
--clk <= clk_pll;
446
--end generate;
447 2 ja_rd
 
448 116 ja_rd
 
449 2 ja_rd
--##############################################################################
450
-- LEDS, SWITCHES
451
--##############################################################################
452
 
453
-- Display the contents of a debug register at the green leds bar
454 59 ja_rd
green_leds <= reg_gleds;
455 2 ja_rd
 
456
 
457
--##############################################################################
458
-- QUAD 7-SEGMENT DISPLAYS
459
--##############################################################################
460
 
461 59 ja_rd
-- Show contents of debug register in hex display
462 116 ja_rd
display_data <=
463 226 ja_rd
    reg_display(15 downto 0) when switches(0)='0' else
464
    reg_display(31 downto 16);
465 2 ja_rd
 
466 59 ja_rd
 
467 2 ja_rd
-- 7-segment encoders; the dev board displays are not multiplexed or encoded
468 59 ja_rd
hex3 <= nibble_to_7seg(display_data(15 downto 12));
469
hex2 <= nibble_to_7seg(display_data(11 downto  8));
470
hex1 <= nibble_to_7seg(display_data( 7 downto  4));
471
hex0 <= nibble_to_7seg(display_data( 3 downto  0));
472 2 ja_rd
 
473
--##############################################################################
474
-- SD card interface
475
--##############################################################################
476
 
477 75 ja_rd
-- Connect to FFs for use in bit-banged interface (still unused)
478 63 ja_rd
sd_cs       <= sd_cs_reg;
479
sd_cmd      <= sd_do_reg;
480
sd_clk      <= sd_clk_reg;
481
sd_in       <= sd_data;
482 2 ja_rd
 
483 63 ja_rd
 
484 2 ja_rd
--##############################################################################
485
-- SERIAL
486
--##############################################################################
487
 
488
--  Embedded in the MPU entity
489
 
490
end minimal;
491 116 ja_rd
 
492
--------------------------------------------------------------------------------
493
-- NOTE: Optional use of a PLL
494
-- 
495
-- In order to try the core with any clock other the 50 and 27MHz oscillators 
496
-- readily available onboard we need to use a PLL.
497
-- Unfortunately, Quartus-II won't let you just instantiate a PLL like ISE does.
498
-- Instead, you have to build a PLL module using the MegaWizard tool.
499
-- A nasty consequence of this is that the PLL can't be reconfigured without
500
-- rebuilding it with the MW tool, and a bunch of ugly binary files have to be 
501
-- committed to SVN if the project is to be complete.
502
-- When I figure up what files need to be committed to SVN I will. Meanwhile you
503
-- have to build the module yourself if you want to u se a PLL -- Sorry!
504
-- At least it is very straightforward -- create an ALTPLL variation (from the 
505
-- IO module library) named 'pll' with a 45MHz clock at output c0, that's it.
506
--
507
-- Please note that the system will run at >50MHz when using 'balanced' 
508
-- synthesis. Only the 'area optimized' synthesis may give you trouble.
509
--------------------------------------------------------------------------------

powered by: WebSVN 2.1.0

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