Lab3 VGA
Lab3 VGA
Lab3 VGA
2. VGA Display
Video Graphics Array (VGA) is a standard that was originally developed for driving CRT
displays from a PC. The original image format was 640x480 RGB color. Over the years, VGA
has been extended to accommodate much higher resolution displays. In the lab, we will limit
ourselves to the 640x480 format.
The VGA protocol was designed to drive a cathode ray tube (CRT) display in which an electron
beam is raster scanned across the screen as shown in Figure 1. Each video frame, the beam is
scanned across the screen 480 times to create 480 lines of displayable information. We divide, in
turn, each horizontal line into 640 pixels of displayable information. Each pixel is also defined
by a red, green and blue intensity which defines the brightness and the color of that pixel. The
display runs at a frame rate of 60 complete frames per second. This is fast enough for your eye to
see a continuous (rather than a flickering) image.
The PC (or FPGA in our case) generates horizontal and vertical synchronization signals to
control the raster scanning of the display. A horizontal (HSYNC) pulse triggers the horizontal
scanning of the next line. A VSYNC pulse brings the beam back up to the top of the display (row
0) to begin a new frame. In addition to the sync pulses, the controller must supply red, green and
blue video signals that describe the intensity of the current pixel. These are analog signals that
range between 0V and 0.7V. These are generated on the Nexys2 board using an 8-bit video
signal and a simple resistor based D/A converter as shown in Figure 2. The resistor values are
chosen to work in conjunction with the 75Ω termination resistance of the VGA display. The 8-bit
video signal includes 3-bits of red and green intensity and 2-bits of blue intensity (your eye is
less sensitive to small changes in blue levels).
System timings for 640x480 60 Hz. operation are shown in Figure 3. The waveform describes
both the vertical and horizontal sync signals. Note that the horizontal (line) period contains
display time for the 640 pixels Tdisp, the HSYNC pulse Tpw and two blanking periods Tfp and Tbp
which allow time for the beam retrace. Similarly the vertical (frame) period contains time for the
480 lines, the VSYNC pulse and extra time for the vertical retrace. The time periods shown in
terms of Clks assume a 25 MHz clock.
2
3. Hardware Setup
Connect the VGA display to the VGA port on the Nexys2 board as shown in Figure 4.
4. Configuring FPGA
4.1 Create a New Project
Use the Xilinx ISE software to create a new project named VGAball using the same project
settings as in Labs 1 and 2.
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity vga_sync is
Port ( clock_25MHz : in STD_LOGIC;
red : in STD_LOGIC;
green : in STD_LOGIC;
blue : in STD_LOGIC;
red_out : out STD_LOGIC;
green_out : out STD_LOGIC;
blue_out : out STD_LOGIC;
3
hsync : out STD_LOGIC;
vsync : out STD_LOGIC;
pixel_row : out STD_LOGIC_VECTOR (9 downto 0);
pixel_col : out STD_LOGIC_VECTOR (9 downto 0));
end vga_sync;
-- Register video to clock edge and suppress video during blanking and sync periods
red_out <= red and video_on;
4
green_out <= green and video_on;
blue_out <= blue and video_on;
end process;
end Behavioral;
Expand the Synthesize command in the Process window and run Check Syntax to verify that
you have entered the code correctly.
This module uses a 25MHz clock to drive horizontal and vertical counters h_cnt and v_cnt
respectively. These counters are then used to generate the various timing signals. vsync and
hsync are the vertical and horizontal sync waveforms that will go directly to the VGA display.
pixel_col and pixel_row are the column and row address of the current pixel being displayed.
This module also takes as input the current red, blue and video data and gates it with a signal
called video_on. This ensures that no video is sent to the display during the sync and blanking
periods. Note that red, green and blue video are each represented as 1-bit (on-off) quantities. This
is sufficient resolution for our application.
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity ball is
Port ( v_sync : in STD_LOGIC;
pixel_row : in STD_LOGIC_VECTOR(9 downto 0);
pixel_col : in STD_LOGIC_VECTOR(9 downto 0);
red : out STD_LOGIC;
green : out STD_LOGIC;
blue : out STD_LOGIC);
end ball;
5
-- current ball motion - initialized to +4 pixels/frame
signal ball_y_motion: STD_LOGIC_VECTOR(9 downto 0):= "0000000100";
begin
red <= '1'; -- color setup for red ball on white background
green <= not ball_on;
blue <= not ball_on;
-- process to move ball once every frame (i.e. once every vsync pulse)
mball: process
begin
wait until rising_edge(v_sync);
-- allow for bounce off top or bottom of screen
if ball_y + size >= 480 then
ball_y_motion <= "1111111100"; -- -4 pixels
elsif ball_y <= size then
ball_y_motion <= "0000000100"; -- +4 pixels
end if;
ball_y <= ball_y + ball_y_motion; -- compute next ball position
end process;
end Behavioral;
________________________________________________________________________
Highlight the ball module in the Hierarchy window, expand the Design Utilities command in the
Process window and run Check Syntax to verify that you have entered the code correctly
This module maintains signals ball_x and ball_y which represent the current position of the ball
on the screen. These are initialized to (320,240) to start the ball in the center of the screen. The
module also maintains a signal ball_y_motion that represents the number of pixels that the ball
should move in one frame period. This is initialized to +4 pixels/frame. The module generates
one-bit red, green and blue video signals which are normally all set to ‘1’. This produces a white
screen background. When the signal ball_on is set, the green and blue signals go to ‘0’ which
makes those pixels red.
6
The module takes as input the current pixel row and column address which is generated by the
vga_sync module. Whenever the ball position is within 8 pixels of the current pixel address (in
both x and y directions), the process bdraw sets the signal ball_on. This paints a red ball around
the current pixel address.
A second process mball (activated by the vsync signal) updates the ball position once every
frame. When the ball reaches the top of the screen, it changes the ball motion to -4 pixels per
frame. When it reaches the bottom of the screen it changes the ball motion to +4 pixels per
frame.
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity vga_top is
Port ( clk_50MHz : in STD_LOGIC;
vga_red : out STD_LOGIC_VECTOR (2 downto 0);
vga_green : out STD_LOGIC_VECTOR (2 downto 0);
vga_blue : out STD_LOGIC_VECTOR (1 downto 0);
vga_hsync : out STD_LOGIC;
vga_vsync : out STD_LOGIC);
end vga_top;
component ball is
Port ( v_sync : in STD_LOGIC;
pixel_row : in STD_LOGIC_VECTOR(9 downto 0);
pixel_col : in STD_LOGIC_VECTOR(9 downto 0);
red : out STD_LOGIC;
green : out STD_LOGIC;
blue : out STD_LOGIC);
end component;
7
component vga_sync is
Port ( clock_25MHz : in STD_LOGIC;
red : in STD_LOGIC;
green : in STD_LOGIC;
blue : in STD_LOGIC;
red_out : out STD_LOGIC;
green_out : out STD_LOGIC;
blue_out : out STD_LOGIC;
hsync : out STD_LOGIC;
vsync : out STD_LOGIC;
pixel_row : out STD_LOGIC_VECTOR (9 downto 0);
pixel_col : out STD_LOGIC_VECTOR (9 downto 0));
end component;
begin
8
pixel_row => S_pixel_row,
pixel_col => S_pixel_col,
hsync => vga_hsync,
vsync => S_vsync);
end Behavioral;
____________________________________________________________________________
Right click on the module vga_top in the Hierarchy window and select Set as Top Module.
9
Now highlight the vga_top module in the Hierarchy window and run Implement Design
followed by Generate Programming File.
3. Introduce a new signal ball_x_motion to allow the ball to move horizontally as well as
vertically and add code so that it will also bounce of the left and right side walls.
10