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

Subversion Repositories ion

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

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 2 ja_rd
 
10
library ieee;
11
use ieee.std_logic_1164.all;
12
use ieee.std_logic_arith.all;
13
use ieee.std_logic_unsigned.all;
14
 
15
-- FPGA i/o for Terasic DE-1 board
16
-- (Many of the board's i/o devices will go unused in this demo)
17
entity c2sb_demo is
18 59 ja_rd
    port (
19 2 ja_rd
        -- ***** Clocks
20
        clk_50MHz     : in std_logic;
21 116 ja_rd
        clk_27MHz     : in std_logic;
22 2 ja_rd
 
23
        -- ***** Flash 4MB
24
        flash_addr    : out std_logic_vector(21 downto 0);
25
        flash_data    : in std_logic_vector(7 downto 0);
26
        flash_oe_n    : out std_logic;
27
        flash_we_n    : out std_logic;
28
        flash_reset_n : out std_logic;
29
 
30
        -- ***** SRAM 256K x 16
31
        sram_addr     : out std_logic_vector(17 downto 0);
32
        sram_data     : inout std_logic_vector(15 downto 0);
33
        sram_oe_n     : out std_logic;
34
        sram_ub_n     : out std_logic;
35 59 ja_rd
        sram_lb_n     : out std_logic;
36 2 ja_rd
        sram_ce_n     : out std_logic;
37 59 ja_rd
        sram_we_n     : out std_logic;
38 2 ja_rd
 
39
        -- ***** RS-232
40
        rxd           : in std_logic;
41
        txd           : out std_logic;
42
 
43
        -- ***** Switches and buttons
44
        switches      : in std_logic_vector(9 downto 0);
45
        buttons       : in std_logic_vector(3 downto 0);
46
 
47
        -- ***** Quad 7-seg displays
48
        hex0          : out std_logic_vector(0 to 6);
49
        hex1          : out std_logic_vector(0 to 6);
50
        hex2          : out std_logic_vector(0 to 6);
51
        hex3          : out std_logic_vector(0 to 6);
52
 
53
        -- ***** Leds
54
        red_leds      : out std_logic_vector(9 downto 0);
55
        green_leds    : out std_logic_vector(7 downto 0);
56
 
57
        -- ***** SD Card
58
        sd_data       : in  std_logic;
59
        sd_cs         : out std_logic;
60
        sd_cmd        : out std_logic;
61 59 ja_rd
        sd_clk        : out std_logic
62 2 ja_rd
    );
63
end c2sb_demo;
64
 
65
architecture minimal of c2sb_demo is
66
 
67
 
68
--##############################################################################
69 116 ja_rd
-- Parameters
70 2 ja_rd
 
71 116 ja_rd
-- Address size (FIXME: not tested with other values)
72 75 ja_rd
constant SRAM_ADDR_SIZE : integer := 32;
73 46 ja_rd
 
74 116 ja_rd
-- Clock rate selection (affects UART configuration)
75
-- Acceptable values: {27000000, 50000000, 45000000(pll config)}
76
constant CLOCK_FREQ : integer := 50000000;
77
 
78 2 ja_rd
--##############################################################################
79
-- RS232 interface signals
80
 
81
signal rx_rdy :             std_logic;
82
signal tx_rdy :             std_logic;
83
signal rs232_data_rx :      std_logic_vector(7 downto 0);
84
signal rs232_status :       std_logic_vector(7 downto 0);
85
signal data_io_out :        std_logic_vector(7 downto 0);
86
signal io_port :            std_logic_vector(7 downto 0);
87
signal read_rx :            std_logic;
88
signal write_tx :           std_logic;
89
 
90
 
91
--##############################################################################
92 63 ja_rd
-- I/O registers
93 2 ja_rd
 
94
 
95 63 ja_rd
signal sd_clk_reg :         std_logic;
96
signal sd_cs_reg :          std_logic;
97
signal sd_cmd_reg :         std_logic;
98
signal sd_do_reg :          std_logic;
99
 
100
 
101 59 ja_rd
-- CPU access to hex display
102 116 ja_rd
signal reg_display :        std_logic_vector(31 downto 0);
103 2 ja_rd
 
104
 
105
 
106
--##############################################################################
107
-- DE-1 board interface signals
108
 
109 59 ja_rd
-- Synchronization FF chain for asynchronous reset input
110 116 ja_rd
signal reset_sync :         std_logic_vector(3 downto 0);
111 59 ja_rd
 
112 116 ja_rd
-- Reset pushbutton debouncing logic
113
subtype t_debouncer is integer range 0 to CLOCK_FREQ;
114
constant DEBOUNCING_DELAY : t_debouncer := 500;
115
signal debouncing_counter : t_debouncer := (CLOCK_FREQ/1000) * DEBOUNCING_DELAY;
116
 
117 2 ja_rd
-- Quad 7-segment display (non multiplexed) & LEDS
118
signal display_data :       std_logic_vector(15 downto 0);
119 59 ja_rd
signal reg_gleds :          std_logic_vector(7 downto 0);
120 2 ja_rd
 
121
-- Clock & reset signals
122
signal clk_1hz :            std_logic;
123
signal clk_master :         std_logic;
124
signal counter_1hz :        std_logic_vector(25 downto 0);
125
signal reset :              std_logic;
126 116 ja_rd
-- Master clock signal
127 2 ja_rd
signal clk :                std_logic;
128 116 ja_rd
-- Clock from PLL, is a PLL is used
129
signal clk_pll :            std_logic;
130
-- '1' when PLL is locked or when no PLL is used
131
signal pll_locked :         std_logic;
132 2 ja_rd
 
133 116 ja_rd
-- Altera PLL component declaration (in case it's used)
134
-- Note that the MegaWizard component needs to be called 'pll' or the component
135
-- name should be changed in this file.
136
--component pll
137
--    port (
138
--        areset      : in std_logic  := '0';
139
--        inclk0      : in std_logic  := '0';
140
--        c0          : out std_logic ;
141
--        locked      : out std_logic
142
--    );
143
--end component;
144
 
145 2 ja_rd
-- SD control signals
146
signal sd_in :              std_logic;
147
signal reg_sd_dout :        std_logic;
148
signal reg_sd_clk :         std_logic;
149
signal reg_sd_cs :          std_logic;
150
 
151 46 ja_rd
-- MPU interface signals
152 2 ja_rd
signal data_uart :          std_logic_vector(31 downto 0);
153
signal data_uart_status :   std_logic_vector(31 downto 0);
154
signal uart_tx_rdy :        std_logic := '1';
155
signal uart_rx_rdy :        std_logic := '1';
156
 
157 46 ja_rd
signal io_rd_data :         std_logic_vector(31 downto 0);
158
signal io_rd_addr :         std_logic_vector(31 downto 2);
159
signal io_wr_addr :         std_logic_vector(31 downto 2);
160
signal io_wr_data :         std_logic_vector(31 downto 0);
161
signal io_rd_vma :          std_logic;
162
signal io_byte_we :         std_logic_vector(3 downto 0);
163 2 ja_rd
 
164 75 ja_rd
signal mpu_sram_address :   std_logic_vector(SRAM_ADDR_SIZE-1 downto 0);
165
signal mpu_sram_data_rd :   std_logic_vector(15 downto 0);
166
signal mpu_sram_data_wr :   std_logic_vector(15 downto 0);
167 46 ja_rd
signal mpu_sram_byte_we_n : std_logic_vector(1 downto 0);
168
signal mpu_sram_oe_n :      std_logic;
169
 
170 59 ja_rd
-- Converts hex nibble to 7-segment
171
-- Segments ordered as "GFEDCBA"; '0' is ON, '1' is OFF
172
function nibble_to_7seg(nibble : std_logic_vector(3 downto 0))
173
                        return std_logic_vector is
174
begin
175
    case nibble is
176
    when X"0"       => return "0000001";
177
    when X"1"       => return "1001111";
178
    when X"2"       => return "0010010";
179
    when X"3"       => return "0000110";
180
    when X"4"       => return "1001100";
181
    when X"5"       => return "0100100";
182
    when X"6"       => return "0100000";
183
    when X"7"       => return "0001111";
184
    when X"8"       => return "0000000";
185
    when X"9"       => return "0000100";
186
    when X"a"       => return "0001000";
187
    when X"b"       => return "1100000";
188
    when X"c"       => return "0110001";
189
    when X"d"       => return "1000010";
190
    when X"e"       => return "0110000";
191
    when X"f"       => return "0111000";
192
    when others     => return "0111111"; -- can't happen
193
    end case;
194
end function nibble_to_7seg;
195 46 ja_rd
 
196
 
197 2 ja_rd
begin
198
 
199
    mpu: entity work.mips_mpu
200 46 ja_rd
    generic map (
201 116 ja_rd
        CLOCK_FREQ     => CLOCK_FREQ,
202 46 ja_rd
        SRAM_ADDR_SIZE => SRAM_ADDR_SIZE
203
    )
204 2 ja_rd
    port map (
205
        interrupt   => '0',
206 59 ja_rd
 
207 46 ja_rd
        -- interface to FPGA i/o devices
208
        io_rd_data  => io_rd_data,
209
        io_rd_addr  => io_rd_addr,
210
        io_wr_addr  => io_wr_addr,
211
        io_wr_data  => io_wr_data,
212
        io_rd_vma   => io_rd_vma,
213
        io_byte_we  => io_byte_we,
214 59 ja_rd
 
215 46 ja_rd
        -- interface to asynchronous 16-bit-wide EXTERNAL SRAM
216
        sram_address    => mpu_sram_address,
217 75 ja_rd
        sram_data_rd    => mpu_sram_data_rd,
218
        sram_data_wr    => mpu_sram_data_wr,
219 46 ja_rd
        sram_byte_we_n  => mpu_sram_byte_we_n,
220
        sram_oe_n       => mpu_sram_oe_n,
221 2 ja_rd
 
222
        uart_rxd    => rxd,
223
        uart_txd    => txd,
224 59 ja_rd
 
225 2 ja_rd
        clk         => clk,
226
        reset       => reset
227
    );
228
 
229
 
230 63 ja_rd
--##############################################################################
231
-- I/O registers
232
--##############################################################################
233 2 ja_rd
 
234 63 ja_rd
hex_display_register:
235
process(clk)
236
begin
237
    if clk'event and clk='1' then
238
        if io_byte_we/="0000" and io_wr_addr(15 downto 12)=X"2" then
239 116 ja_rd
            reg_display(15 downto 0) <= io_wr_data(15 downto 0);
240
            --reg_display <= mpu_sram_address;
241 63 ja_rd
        end if;
242
    end if;
243
end process hex_display_register;
244
 
245
sd_control_register:
246
process(clk)
247
begin
248
    if clk'event and clk='1' then
249
        if io_byte_we/="0000" and io_wr_addr(15 downto 12)=X"1" then
250
            if io_wr_addr(5)='1' then
251
                sd_clk_reg <= io_wr_addr(4);
252
            end if;
253
            if io_wr_addr(7)='1' then
254
                sd_cs_reg <= io_wr_addr(6);
255
            end if;
256
            if io_wr_addr(11)='1' then
257
                sd_do_reg <= io_wr_data(0);
258
            end if;
259
        end if;
260
    end if;
261
end process sd_control_register;
262
 
263
 
264
-- Show the SD interface signals on the green leds for debug
265
reg_gleds <= sd_clk_reg & sd_in & sd_do_reg & "000" & sd_cmd_reg & sd_cs_reg;
266
 
267
io_rd_data(0) <= sd_in;
268
io_rd_data(31 downto 22) <= switches;
269
 
270
 
271
 
272 59 ja_rd
-- red leds (light with '1') -- some CPU control signals
273 2 ja_rd
red_leds(0) <= '0';
274
red_leds(1) <= '0';
275
red_leds(2) <= '0';
276
red_leds(3) <= '0';
277
red_leds(4) <= '0';
278
red_leds(5) <= '0';
279
red_leds(6) <= '0';
280
red_leds(7) <= '0';
281
red_leds(8) <= '0';
282
red_leds(9) <= clk_1hz;
283
 
284
 
285
--##############################################################################
286
-- terasIC Cyclone II STARTER KIT BOARD -- interface to on-board devices
287
--##############################################################################
288
 
289
--##############################################################################
290 75 ja_rd
-- FLASH (connected to the same mup bus as the sram)
291 2 ja_rd
--##############################################################################
292
 
293 75 ja_rd
flash_we_n <= '1'; -- all write control signals inactive
294 2 ja_rd
flash_reset_n <= '1';
295
 
296 75 ja_rd
flash_addr(21 downto 18) <= (others => '0');
297
flash_addr(17 downto  0) <= mpu_sram_address(17 downto 0); -- FIXME
298 2 ja_rd
 
299 75 ja_rd
-- Flash is decoded at 0xb0000000
300
flash_oe_n <= '0'
301
    when mpu_sram_address(31 downto 27)="10110" and mpu_sram_oe_n='0'
302
    else '1';
303
 
304
 
305
 
306 2 ja_rd
--##############################################################################
307 75 ja_rd
-- SRAM
308 2 ja_rd
--##############################################################################
309
 
310 75 ja_rd
sram_addr <= mpu_sram_address(sram_addr'high+1 downto 1);
311
sram_oe_n <= '0'
312
    when mpu_sram_address(31 downto 27)="00000" and mpu_sram_oe_n='0'
313
    else '1';
314
 
315 46 ja_rd
sram_ub_n <= mpu_sram_byte_we_n(1) and mpu_sram_oe_n;
316
sram_lb_n <= mpu_sram_byte_we_n(0) and mpu_sram_oe_n;
317
sram_ce_n <= '0';
318
sram_we_n <= mpu_sram_byte_we_n(1) and mpu_sram_byte_we_n(0);
319 2 ja_rd
 
320 75 ja_rd
sram_data <= mpu_sram_data_wr when mpu_sram_byte_we_n/="11" else (others => 'Z');
321 2 ja_rd
 
322 75 ja_rd
-- The only reason we need this mux is because we have the static RAM and the
323
-- static flash in separate FPGA pins, whereas in a real world application they
324
-- would be on the same data+address bus
325
mpu_sram_data_rd <=
326
    -- SRAM is decoded at 0x00000000
327
    sram_data when mpu_sram_address(31 downto 27)="00000" else
328
    X"00" & flash_data;
329
 
330
 
331
 
332 2 ja_rd
--##############################################################################
333
-- RESET, CLOCK
334
--##############################################################################
335
 
336
-- Use button 3 as reset
337 75 ja_rd
-- This FF chain only prevents metastability trouble, it does not help with
338
-- switching bounces.
339 116 ja_rd
-- (NOTE: the anti-metastability logic is probably not needed when we include 
340
-- the debouncing logic)
341 59 ja_rd
reset_synchronization:
342
process(clk)
343
begin
344
    if clk'event and clk='1' then
345 116 ja_rd
        reset_sync(3) <= not buttons(2);
346
        reset_sync(2) <= reset_sync(3);
347 59 ja_rd
        reset_sync(1) <= reset_sync(2);
348
        reset_sync(0) <= reset_sync(1);
349
    end if;
350
end process reset_synchronization;
351 2 ja_rd
 
352 116 ja_rd
reset_debouncing:
353
process(clk)
354
begin
355
    if clk'event and clk='1' then
356
        if reset_sync(0)='1' and reset_sync(1)='0' then
357
            debouncing_counter <= (CLOCK_FREQ/1000) * DEBOUNCING_DELAY;
358
        else
359
            if debouncing_counter /= 0 then
360
                debouncing_counter <= debouncing_counter - 1;
361
            end if;
362
        end if;
363
    end if;
364
end process reset_debouncing;
365 2 ja_rd
 
366 116 ja_rd
--
367
reset <= '1' when debouncing_counter /= 0 or pll_locked='0' else '0';
368 59 ja_rd
 
369 2 ja_rd
-- Generate a 1-Hz 'clock' to flash a LED for visual reference.
370 116 ja_rd
process(clk)
371 2 ja_rd
begin
372 116 ja_rd
  if clk'event and clk='1' then
373 2 ja_rd
    if reset = '1' then
374
      clk_1hz <= '0';
375
      counter_1hz <= (others => '0');
376
    else
377 116 ja_rd
      if conv_integer(counter_1hz) = CLOCK_FREQ-1 then
378 2 ja_rd
        counter_1hz <= (others => '0');
379
        clk_1hz <= not clk_1hz;
380
      else
381
        counter_1hz <= counter_1hz + 1;
382
      end if;
383
    end if;
384
  end if;
385
end process;
386
 
387 116 ja_rd
-- Master clock is external 50MHz or 27MHz oscillator
388
 
389
slow_clock:
390
if CLOCK_FREQ = 27000000 generate
391
clk <= clk_27MHz;
392
pll_locked <=  '1';
393
end generate;
394
 
395
fast_clock:
396
if CLOCK_FREQ = 50000000 generate
397 2 ja_rd
clk <= clk_50MHz;
398 116 ja_rd
pll_locked <=  '1';
399
end generate;
400 2 ja_rd
 
401 116 ja_rd
--pll_clock:
402
--if CLOCK_FREQ /= 27000000 and CLOCK_FREQ/=50000000 generate
403
---- Assume PLL black box is properly configured for whatever the clock rate is...
404
--input_clock_pll: component pll
405
--    port map(
406
--        areset  => '0',
407
--        inclk0  => clk_50MHz,
408
--        c0      => clk_pll,
409
--        locked  => pll_locked
410
--    );
411
--
412
----clk <= clk_1hz when reg_display(31 downto 27)="10110" else clk_pll;
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 116 ja_rd
display_data <=
431
    reg_display(15 downto 0);-- when switches(0)='0' else 
432
    --reg_display(31 downto 16);
433 2 ja_rd
 
434 59 ja_rd
 
435 2 ja_rd
-- 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 63 ja_rd
sd_cs       <= sd_cs_reg;
447
sd_cmd      <= sd_do_reg;
448
sd_clk      <= sd_clk_reg;
449
sd_in       <= sd_data;
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.