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

Subversion Repositories ion

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

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

powered by: WebSVN 2.1.0

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