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

Subversion Repositories ion

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

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

powered by: WebSVN 2.1.0

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