Professional Documents
Culture Documents
Fpga Based Vga Driver and Arcade Game: Armandas Jarusauskas Third Year Individual Project 2009/2010
Fpga Based Vga Driver and Arcade Game: Armandas Jarusauskas Third Year Individual Project 2009/2010
ARMANDAS JARUSAUSKAS
THIRD YEAR INDIVIDUAL PROJECT
2009/2010
FieldProgrammable GateArray (FPGA) technology is gaining popularity among ApplicationSpecific
Integrated Circuit (ASIC) designers. Ease of development and maintenance makes FPGAs an attractive
solution to many speed and efficiencycritical applications. The purpose of this project is to explore the
world of FPGAs by implementing an arcade game on top of a VGA driver. The project was implemented
on Xilinx Spartan3E development board using VHDL hardware description language.
The project was started by learning VHDL as well as familiarising with the Spartan3E development
board and Xilinx ISE WebPACK design software. A number of simple applications were developed in
order to comfortably proceed on to investigation of working principles of a VGA driver. It turned out
that the synchronisation logic was rather trivial and that the custom implementation would not add
much value to the project.
VHDL implementation of the classic Pong game was the first major task of the project. Plong, as it was
named, is a twoplayer game, where each player tries to to hit a ball towards the opponent. A number
of basic features, such as acceleration of the ball, sound output and textual information display, have
been implemented. As this game was developed in the early stage of the project, complexity was
consciously avoided.
The objectives the second game, on the other hand, were focused on technical features and other
improvements. Still monochrome images were exchanged for colourful animations, sound output was
made more sophisticated by adding a tune player, Nintendo game pads replaced onboard switches as a
user input interface and a number of other modules were developed. The theme was chosen to
resemble the Space Invaders game, but all graphic elements were designed from scratch.
To make the project more complete and self contained, the two games were combined into a single
application, with a menu screen to allow user to choose which game to play.
Lastly, an adaptor board, for connecting NES game pads to the development board, was designed and
built. It facilitates the communication between the controllers and the FPGA. The board also hosts a
piezoelectric buzzer for sound output and a reset switch.
The project was complete and fully functional before the deadline. While there is still a lot of room for
improvement, all set objectives, and more, were met.
Statement of originality
I hereby declare that this report is my own work and to the best of my knowledge it contains no
materials previously published or written by another person, or which have been accepted for the
award of any degree or diploma at any educational institution, except where due acknowledgement is
made in the report itself.
Acknowledgements
First supervisor: Dr Ahmet Aydin
Second supervisor: Dr Christopher Harland
I would like to thank the following people for all the help and support:
• Ahmet Aydin for his time and support during the project and for letting me borrow the FPGA
development board.
• Sam Beardsmore-Rust for general support and help with project proposal.
• Philip Watson for 100-pin connector design files and other help.
• Martin Nock for help with PCB manufacture.
• David Smith, from the mechanical workshop, for his excellent work on a plastic case for my
adaptor board.
Table of Contents
1 INTRODUCTION 9
1.1 About this report..............................................................................................................9
1.2 Project aims and plan.......................................................................................................9
2 BACKGROUND 9
2.1 Field-Programmable Gate Arrays....................................................................................9
Xilinx versus Altera..........................................................................................................................9
2.2 Hardware description languages...................................................................................10
VHDL...............................................................................................................................................10
Digital systems theory..............................................................................................................10
Other HDLs.....................................................................................................................................10
2.3 Development board........................................................................................................10
2.4 VGA...................................................................................................................................11
Hardware........................................................................................................................................11
Timing.............................................................................................................................................11
2.5 Nintendo controllers......................................................................................................12
2.6 Principles of game implementation..............................................................................12
Logic blocks....................................................................................................................................12
Objects............................................................................................................................................12
3 IMPLEMENTATION 13
3.1 VGA...................................................................................................................................13
Code................................................................................................................................................13
Results............................................................................................................................................13
3.2 NES controller interface.................................................................................................14
Code................................................................................................................................................14
Results............................................................................................................................................14
3.3 Graphics...........................................................................................................................14
Storing images...............................................................................................................................15
Getting the pixels...........................................................................................................................15
Moving images...............................................................................................................................16
Colour image storage....................................................................................................................16
3.4 Text generation...............................................................................................................17
Font.................................................................................................................................................17
Grid.................................................................................................................................................17
3.5 Binary to BCD conversion...............................................................................................17
Theory.............................................................................................................................................17
VHDL implementation...................................................................................................................18
3.6 Sound...............................................................................................................................18
Frequency generator.....................................................................................................................18
Sound ROM....................................................................................................................................18
Player..............................................................................................................................................18
4 PONG 19
4.1 Structure..........................................................................................................................19
4.2 Graphics...........................................................................................................................19
Main logic.......................................................................................................................................19
Paddle logic....................................................................................................................................20
Ball logic.........................................................................................................................................20
Scores.............................................................................................................................................20
4.3 Sounds & User input.......................................................................................................21
Sounds............................................................................................................................................21
User input.......................................................................................................................................21
5 SPACE SHOOTER 21
5.1 Gameplay.........................................................................................................................21
5.2 Graphics...........................................................................................................................22
Main logic.......................................................................................................................................22
Aliens..............................................................................................................................................22
Spaceship and missile...................................................................................................................22
Explosion logic...............................................................................................................................22
Score and level display..................................................................................................................23
7 ADAPTOR BOARD 23
7.1 Reverse engineering the controllers.............................................................................23
7.2 TTL / CMOS level conversion..........................................................................................24
FPGA to Shift register.....................................................................................................................24
Shift register to FPGA.....................................................................................................................24
7.3 Board design....................................................................................................................24
Schematic capture.........................................................................................................................24
PCB layout design..........................................................................................................................24
Manufacture...................................................................................................................................24
8 DISCUSSION 25
8.1 Issues...............................................................................................................................25
Pong ball acceleration...................................................................................................................25
Synchronisation problems............................................................................................................25
Adaptor board................................................................................................................................26
Binary-to-BCD conversion.............................................................................................................26
8.2 Further work...................................................................................................................26
9 CONCLUSION 26
REFERENCES 27
BIBLIOGRAPHY 27
APPENDIX A 28
Initial technical proposal....................................................................................................28
APPENDIX B 29
Project parts list...................................................................................................................29
APPENDIX C 30
Project plan..........................................................................................................................30
APPENDIX D 31
NES Controller: equivalent schematic diagram................................................................31
APPENDIX E 32
Python script for generating VHDL ROM from images.....................................................32
APPENDIX F 33
Adaptor board schematic diagram.....................................................................................33
APPENDIX G 34
Adaptor board PCB: top and bottom layer masks.............................................................34
APPENDIX H 35
Photographs.........................................................................................................................35
FPGA BASED VGA DRIVER
AND ARCADE GAME
and milestones was created to give a rough
1 INTRODUCTION estimate of duration of each part of the project.
9
FPGA BASED VGA DRIVER
AND ARCADE GAME
2.2 Hardware description languages Other HDLs
As with the FPGAs, there are two predominant
VHDL hardware description languages – VHDL and
Verilog.
“VHDL is a language for describing digital
electronic systems. It arose out of the United And just like with FPGAs, HDL usage is more a
States government's Very High Speed Integrated matter of preference. Capabilities are pretty much
Circuit (VHSIC) program. In the course of this equal. The difference lies in how the hardware is
program, it became clear that there was a need for described. Verilog is somewhat similar to C
a standard language for describing the structure programming language, whereas VHDL is based
and function of Integrated Circuits (ICs). Hence on Ada.
the VHSIC Hardware Description Language According to Wakerly [3], it is best to “learn one
(VHDL) was developed” (Ashenden, 1996) [2]. well and, only if necessary, tackle the other later”.
VHDL became an IEEE standard in 1987. Several
revisions have been done since then.
2.3 Development board
VHDL can be used not only to describe digital
hardware but also to create test benches for
testing the designs.
VHDL supports many builtin and userdefined
data types. Some of most often used are
• std_logic (single bit)
• std_logic_vector (bit vector)
• numerical types such as integer
• arrays, enumerated lists, etc.
Unlike imperative programming languages, such
as C++, VHDL statements are concurrent (i.e. run
in parallel). Concurrency is very useful in HDLs, as
it resembles the way hardware works. Sequential
statements can be implemented using a special FIGURE 2.1: SPARTAN-3E BOARD
construct called process.
The development board used in this project is
Every VHDL design must have an entity and an shown in Figure 2.1. It uses a Spartan3E series
architecture. The Entity contains a declaration of FPGA (model XC3S500E) with the following
I/O ports while the actual design code resides in characteristics [1]:
the architecture part.
• 500,000 system gates, making over ten
Signals declared in the entity are referred to as thousand equivalent logic cells.
external and are usually tied to I/O pins on the
FPGA. The mapping of signals to pins is done in • 20 RAM blocks totalling 360kb of memory.
the User Constraints File (UCF).
• 20 dedicated multipliers.
Internal signals are declared in the architecture
• A maximum of 232 I/O pins.
body and cannot be connected to the outside
world. The board has a 15pin Dsubminiature connector
to plug in a VGA monitor.
Digital systems theory Also, an onboard 100pin expansion port is used
to connect an adaptor board. Full list of features
VHDL is a good example of practical application of
can be found in the user guide [4].
digital circuit theory. Many of the basic concepts,
such as gate and registertransfer level design,
combinational logic and finite state machines have
been used throughout the project.
10
FPGA BASED VGA DRIVER
AND ARCADE GAME
2.4 VGA
Hardware
VGA is an analogue video standard, that is mostly
used in personal computers. VGA can also refer to
a piece of display hardware developed by IBM
(Video Graphics Array) or a display mode, that
uses 640 x 480 pixels resolution.
VGA connector uses a total of 15 pins, but only 5
signals are needed for operation:
• HSYNC – horizontal synchronization
signal. This signal controls the horizontal
position of the active pixel FIGURE 2.2: PARTS OF A SCREEN
• VSYNC – vertical synchronization signal. Figure 2.2 shows parts that are required for an
This signal controls vertical position of the operation of a standard monitor.
active pixel. VSYNC rate can also be
referred to as a refresh rate (i.e. number Visible part of the screen is shown as the white
of times per second the screen is redrawn) area. It has a resolution of 640 by 480 pixels.
• RED – red colour channel The black and grey borders denote parts of the
screen that are not visible, but required for
• GREEN – green colour channel synchronization. With these parts, the total width
• BLUE – blue colour channel of the screen is 800 pixels, and the total height is
524 pixels. Below is the short description of each
Other pins include ground, return paths and I2C part of the screen, followed by a table of
clock/data or are reserved [5]. dimensions.
HSYNC and VSYNC are TTL signals, so logic one is Active video. This is the visible part of the screen,
represented by 5V and logic zero is represented by video output is enabled.
0V [5].
Front porch. When the trace reaches the end of
RED, GREEN and BLUE signals are analogue. The the visible part of the screen, the video output is
maximum voltage that can be used is 0.7V and disabled. These areas are denoted as VPF (Vertical
will result in full intensity of that colour [5]. Front Porch) and HPF (Horizontal Front Porch) in
Xilinx Spartan3E board is only capable of Figure 2.2.
producing eight colours (3bits) as a digitalto Sync pulse. In case of HSYNC, the trace goes back
analogue converter (DAC) is not used. The board to column zero. If pulse is VSYNC, the trace goes
uses 270Ω resistors, which form a potential back to row zero. This part is also known as the
divider with internal 75Ω termination. This retrace period.
divider scales the 3.3V signal from FPGA to
required 0.7V [4]. Back porch. This is the part that goes before the
active video starts. These areas are denoted as
VBF (Vertical Back Porch) and HBF (Horizontal
Timing Back Porch) in Figure 2.2.
Pixels on the screen are drawn in sequence, one Table 2.4.1 lists the dimensions for each part of
by one. Rows are arranged top to bottom, and the screen. These numbers are only valid for
columns go from left to right. The row and resolution of 640 x 480 and refresh rate of 60Hz.
column addresses are constantly incremented thus Other modes are described in the source [6].
changing the position of currently drawn pixel.
Synchronisation signals are used to tell the
monitor to return the pixel back to the first row
(VSYNC) or the first column (HSYNC).
Sequence and duration of these signals are
discussed below.
11
FPGA BASED VGA DRIVER
AND ARCADE GAME
detail in Chapter 7.
TABLE 2.4.1: VGA TIMINGS
An equivalent schematic diagram of the controller
25MHz pixel Active Front Sync Back is shown in Appendix D.
clock video porch pulse porch
12
FPGA BASED VGA DRIVER
AND ARCADE GAME
disables the objects. are checked against the predefined constants.
SYNC signals are produced during the sync stage
(i.e. between the front porch and back porch).
Objects
The statement, therefore, can be described in
In the game, each graphic element is treated as an
pseudocode like this:
object. The object has a source of pixel values,
position coordinates, dimensions, logic and so sync_pulse = 1 when
forth. counter >= (display_area +
front_porch)
The idea here is to provide another layer of logic and
separation. Each object is responsible for its counter < (display_area +
graphical appearance, position on the screen, front_porch +
movement etc. sync_pulse)
A ball in the game of Pong, is an example of an Note that separate sync signals are used for
object. The graphical representation of the ball is vertical and horizontal synchronisation. The
stored in the ROM, the object knows its current corresponding VHDL code can be found in the
location, and can update its coordinates to move vga_sync.vhd, lines 95 to 100.
or bounce of the wall.
The circuit also outputs x and y coordinates,
Object generates a pixel value, which is used by named px_x and px_y, respectively. These
graphics generation circuit to display the object on coordinates are provided as 10bit vectors.
the screen. The value of the pixel is deduced from
screen pixel coordinate. More on this in Chapter
3.3. Results
To get a visual representation of the generated
3 IMPLEMENTATION synchronization signals, the outputs were
measured with an oscilloscope.
Channel 1 (top) in Figure 3.1 shows the HSYNC
3.1 VGA
trace. The frequency needed to traverse 524 lines
in 1/60th of a second is about 31kHz.
Code
The VSYNC signal is shown as channel 2.
vga_sync.vhd
The VHDL code for driving a VGA monitor is taken
from FPGA prototyping by VHDL examples, by Pong
P. Chu [7].
It is one of the simplest parts in the project and, in
this case, there is not much to gain from writing a
custom implementation. This section will briefly
explain the operation of the VGA controller logic.
Comments in the code provide more detailed
explanation.
Three major components are needed for the
driver: a 25MHz clock, counters (for the vertical
and horizontal pixel coordinates) and constants FIGURE 3.1: VGA SIGNALS
representing the timing values listed in Table Figure 3.2 shows a zoomedout version of the
2.4.1. VSYNC trace. The HSYNC signal is left out, since
The horizontal and vertical counters count to 799 no detail can be seen due to much higher
and 523 respectively. These counters are updated frequency. Note the frequency of around 60Hz.
at the speed of the pixel clock – 25MHz in this
case.
During operation, the values in the two counters
13
FPGA BASED VGA DRIVER
AND ARCADE GAME
The latch (ps_counter in the image) value is also
FIGURE 3.2: VSYNC SIGNAL used to assign the received value to the right bit in
the data byte.
Code
Results
Figure 3.4 shows the waveforms captured by an
controller.vhd
oscilloscope. Channel 1 shows CLK and channel 2
shows latch signal.
The purpose of the driver is to provide control
signals for game pads, as well as read the data
from them.
Controllers need external clock and latch signals.
The clock frequency should not, under normal
conditions, exceed 1.5MHz [8].
The latch signal must be eight times slower than
the CLK in order to get all eight bits shifted out.
When this signal is high, parallel data is loaded
into the registers, when it is low, bits are shifted
out.
Latch signal frequency was chosen to be 100Hz.
This type of application does not require a very
fast sample rate, so 100 samples per second
seemed reasonable. Clock signal, therefore, FIGURE 3.4: CLK AND P/S SIGNALS
needed to be 800Hz. This is much lower than the Note the frequencies of 800Hz and 100Hz.
maximum given in the datasheet.
The output signals are inverted to match the
The CLK and latch signals are derived from the operation of the adaptor board (described in
master counter, which is running at four times the Chapter 7).
CLK speed (3.2kHz). This is done so that the
sampling point can be chosen with greater
accuracy. 3.3 Graphics
Figure 3.3 shows a part of simulated operation of Section Principles of game implementation in
the interface. Three vertical lines show the points Chapter 2, gave a brief introduction into the
at which data is sampled (master counter value is structure of a game. This chapter takes a deeper
changing from zero to one). This point was look into objects and graphics generation.
chosen, as it is in the middle of high state of CLK
signal (shown as clk_out in the image) and,
therefore, the voltages are stable.
14
FPGA BASED VGA DRIVER
AND ARCADE GAME
Storing images
The graphics generation circuit gets a pixel
coordinate from the VGA driver. Based on that
coordinate, it generates a colour value for that
pixel and outputs it to the RGB port.
For more complex graphics, an image for example,
pixel values have to be stored in memory. One
simple way of doing this, is to use the internal
block RAM.
For storage of a simple monochrome image, a
twodimensional array is sufficient. Array rows
represent image rows, and array indices represent
individual pixels. In VHDL, this array can be FIGURE 3.5: BALL IMAGE
described as follows:
The size of the example image is 16 x 16 pixels,
type rom_type is array(0 to 1) of therefore, we need the address to be 4bits wide in
std_logic_vector(0 to 3); order to be able to access each row and column.
constant SQUARES: rom_type := The first pixel with coordinate (0, 0) is located at
( the topleft corner. Likewise, the last pixel is
"1010", located at the bottomright corner.
"0101"
); To begin with, we need to define the boundaries
of the image. In our case, it is 16x16 square:
This array defines a fourbytwo pixel image like
this: ▚▚. Note that one denotes the foreground rgb <= img_data when px_x < 16 and
(black) and zero denotes the background (white). px_y < 16 else
"111";
Even though, a 3bit RGB scheme is used, we only
need a single bit to describe monochrome images So when the pixel coordinates are less than 16,
(same bit is used for Red, Green and Blue image data is sent to RGB output. Otherwise,
channels). white colour is used as a background.
For simple cases, the synthesizer will infer a Now we need to set the image data. To do this, we
correct type of RAM based on the structure of the need to derive the row and column addresses.
code. No special code to define RAM options was Because the screen pixel coordinates correspond
used in this project. directly to the image pixel coordinates, it is
sufficient to just take four least significant bits
(LSBs) of the pixel coordinates provided by the
Getting the pixels
VGA driver.
Row and pixel addresses are derived from pixel
row_addr <= px_y(3 downto 0);
coordinates. Lets do a simple task of displaying an
col_addr <= px_x(3 downto 0);
image on the screen.
pixel <= pixel_data(col_addr);
Figure 3.5 below shows ball graphics used in the
Pong game. We want this image to appear in the img_data <= "000" when pixel = '1'
topleft corner of the screen. else "111";
The ROM instance (not shown here) automatically
takes the row address and outputs the appropriate
row as a pixel_data vector. The active pixel is
then retrieved from this vector.
Lastly, single bit is converted to three bits for
output at the RGB port.
To keep the example as simple as possible, ROM
instantiation and data type conversions are not
15
FPGA BASED VGA DRIVER
AND ARCADE GAME
shown here. Reader should refer to code listing coordinates are updated in a continuous manner,
for full details. See the next section for a pointer. a motion will be created.
16
FPGA BASED VGA DRIVER
AND ARCADE GAME
wide vector, which can be used like this: As the font has been chosen and the code page
generated, we can now talk about placing the text
rgb_row <= SQUARES_2(addr(2));
on to the screen.
data <= rgb_row(addr(1 downto 0));
The easy way is to treat each letter as a tile. Our
tiles are 8x8 pixels each, which means that
3.4 Text generation 80 x 60 letter matrix can fit on a 640 x 480
Oftentimes, games need to convey some textual resolution screen.
information to the user. This chapter will describe Each character has its own address within the
the process of text generation in VGA applications. ROM. The address of the first symbol, a space, is
0. The address of the second symbol, an
Font exclamation mark, is 8 and so on. These
addresses are used to define text.
codepage.vhd For large amounts of text, a ROM storage is
appropriate, but a simple multiplexer is sufficient
To begin with, a typeface has to be chosen. The when only a few letters are needed.
font, used by the two games, was borrowed from Addressing is very simple, provided the characters
Uzebox Project [9] as it resembles the typeface are placed strictly on the grid:
used by many old arcade games. The source
image is shown in Figure 3.7. • To get a row address, we take the character
address and add three lower bits of px_y
(the screen pixel coordinate) to it.
• Three lower bits of px_x will select the
right pixel in the row (i.e. the column
address).
FIGURE 3.7: ARCADE TYPEFACE If text is moving or otherwise offset, a proper
When a suitable font is chosen, it has to be subtraction is needed, as described in Chapter 3.3.
converted to a code page or, to say simply, a ROM. Also, an additional logic may be required to select
The size of each symbol is 8 x 8 pixels, and there the character address if ROM structure is used to
are 64 symbols in total. This results in 512 rows store the text.
and 8 columns, or 4k bits of data in a ROM.
Entering this information by hand would be 3.5 Binary to BCD conversion
tedious and timeconsuming. A simple Python In order to display multidigit numbers as a text, a
script was written for this purpose and is provided binarytoBCD (Binary Coded Decimal) converter
in Appendix E. is needed. This section will go through the theory
The ROM is no different from that described in and implementation of such converter.
Chapter 3.3. This is how letter A looks after
conversion. Theory
"00111000", -- ### Binary numbers can be converted to BCD using a
"01101100", -- ## ## “Shift and add 3” algorithm, described in page
"11000110", -- ## ## 147 of source [7].
"11000110", -- ## ##
"11111110", -- ####### 1. Bits are shifted to the left, so that the most
"11000110", -- ## ## significant bit (MSB) of a binary number
"11000110", -- ## ## becomes the least significant bit (LSB) of
"00000000", -- BCD0 digit, then the MSB of BCD0 digit
becomes the LSB of BCD1 digit and so on.
Grid 2. After each shift, all BCD registers are
checked; if value in the register is greater
than four, three is added to that register.
Arcade - menu.vhd
3. Go to 1 until all bits have been shifted.
17
FPGA BASED VGA DRIVER
AND ARCADE GAME
VHDL implementation volume input.
The output can be switched on or off by setting
FPGalaxy - bin2bcd.vhd the enable signal.
Frequency generator
sounds.vhd
The frequency generator is a lowlevel logic, that
creates a square wave. It can produce different
frequencies and duty cycles depending on the
parameter values provided.
An internal counter is running at 50MHz. When FIGURE 3.8: SOUND CONTROL FSMD
the value of the counter matches the value of the
period input, the counter will reset. Output (enable signal) only depends on the state,
and can be said to use the Moore model.
The duty cycle determines how long the output
stays high during the period. The pulse width can en <= '1' when state = playing else
be set as a fraction of the period by setting the '0';
18
FPGA BASED VGA DRIVER
AND ARCADE GAME
Table 3.6.1 summarises operation of multiplexers Figure 4.1 shows the file structure of the Pong
used to transform the data from the sound ROM game. The structure follows the pattern described
to appropriate values. The Bits column represents in Chapter 2.6, but the objects (ball and paddles)
the data in the ROM, other columns show the are not separated from the main graphics logic.
corresponding output from the multiplexer. This resulted in a large allinone file.
TABLE 3.6.1
4.2 Graphics
Bits Frequency Duration Duty cycle
Main logic
000 N/A
4.1 Structure
Development of the Pong game was a learning
exercise in its way. There was not much planning
involved and the main goal was to get it working
and to grasp the basics game development.
19
FPGA BASED VGA DRIVER
AND ARCADE GAME
changes to waiting.
Winner is announced when the FSMD state
changes to Game over.
Paddle logic
This Pong implementation is a twoplayer game,
so two paddles are needed. Dimensions and
graphics are the same for both bars, but they have
separate position coordinates.
Since the two paddles cannot appear in the same
location, it was possible to use shared enable,
ROM address, data and pixel signals.
FIGURE 4.3: CHANGES IN TRAJECTORY
Paddle control logic is fairly simple – it responds
to user input by increasing or decreasing the top The part a) of Figure 4.3 shows the ball going
coordinate of each bar. When a paddle is at the down and to the right. When the ball bounces, it
top or bottom, no further movement is allowed in starts going up and to the right. The bold arrow
that direction. denotes the direction that does not change after
the bounce.
Scores
The pong game uses a text generation logic,
20
FPGA BASED VGA DRIVER
AND ARCADE GAME
described in Chapter 3.4, to render points and a
gameover message.
The score font address is acquired using this
formula:
addr = 128 + (score_value * 8)
Offset of 128 is needed because numbers start at
this address in the memory. Multiplication by 8 is
used to get the right digit (characters are 8 x 8
pixels in size).
The score value was deliberately limited to 7 for
two reasons:
1. Simple implementation, no need for a
binarytoBCD converter.
2. Duration of a match is well balanced.
4.3 Sounds & User input FIGURE 5.1: FPGALAXY FILE STRUCTURE
While being more more complex, the game is less
Sounds complicated. More work has been done on the
design side – the file structure was improved, each
Two types of sounds are used in this Pong game. A object is logically separated from all other objects.
short, high pitch sound is played when a ball In fact, it would be possible to take the code that
bounces, and a longer, low pitch sound is played generates a spaceship, and put it into the Pong
when the ball is missed. game with a minimum effort.
Initially, only the square wave generator was used Figure 5.1 shows the improved file structure. As
to make these sounds, so only single frequency can be seen, graphics generation circuit now has
was used. The tones remained after the sound many child files (objects). To conserve the space,
logic was updated. files belonging to objects (e.g. ROM files) are not
shown in the figure. Full file tree can be found in
User input the source code listing.
Initially, four onboard tactile switches were used
to control the movement of paddles. At the end of 5.1 Gameplay
the project development, the game was updated
The point of the game is to destroy the alien
to make use of the Nintendo controllers.
invaders. A player controls a spaceship located at
There are three buttons, that the game uses: UP, the bottom of the screen; it can more sideways,
DOWN and START. The purpose of these buttons but not up or down. The spaceship is able to shoot
should be obvious. one missile at a time. When the missile hits the
target or leaves the screen, the player can shoot
Since the functionality is very basic, there is no
another one.
need for switch debouncing logic.
The aliens are located at the top of the screen.
There are three rows, with eight aliens in a row.
5 SPACE SHOOTER Level 1 aliens hover in front (bottom row), they
The second game, a space shooter, was developed are weak and need only one hit to be destroyed.
as a complement to the Pong game. Level 2 aliens reside in the middle, they are
stronger than Level 1, and need two shots to get
Space shooter is more complex than Pong – visual killed. At the end, there are Level 3 aliens. It takes
appeal improved by use of colour and animated three hits to destroy a Level 3 alien. Moreover,
graphics, enhanced sound logic is able to play they can turn invisible and avoid being hit by a
simple tunes, proper game pads used to control missile.
the game.
When all the aliens are destroyed, another fleet
21
FPGA BASED VGA DRIVER
AND ARCADE GAME
shows up and the game continues. FPGalaxy - alien_generator.vhd
Explosion logic
The frame size was chosen to be 32 x 32 pixels.
The width of all aliens had to be the same, in
order to make it impossible to destroy higher level
aliens first. There is enough room for a missile to
fly between two aliens. FIGURE 5.3: COLOUR MASKING
These images were converted to ROM files for use A more interesting design solution is implemented
by an alien generator logic. The logic takes master in the explosion generation logic. There is only a
coordinate and generates pixel values based on single frame in the ROM file and yet the explosion
that and some other signals. is animated in the game.
22
FPGA BASED VGA DRIVER
AND ARCADE GAME
Arcade - menu.vhd
7 ADAPTOR BOARD
After the decision to use NES controllers was
When both games were finished, it seemed to be a made, a couple of issues had to be resolved:
good idea to combine the two games into a single
application and provide a nice menu to select a • how to connect Nintendo controllers to
game. Xilinx development board and
The menu has to display some text, respond to the • how to make two devices, using different
user input by changing the selection and provide voltage levels, communicate with each
an output signal, that indicates the current choice. other?
There are four pieces of text to be generated on The obvious solution was to design another piece
the screen: of hardware that will have proper connectors to fit
the development board and controllers and also
• title (text: 2 IN 1) host level conversion circuitry.
• game 1 (text: PLONG) Extra functionality, such as reset button and
speaker, was added to make better use of available
• game 2 (text: FPGALAXY)
board space. The list of parts can be found in
• credits (author and date) Appendix B.
The title text is about eight times larger than
normal letters, but is generated from the same 7.1 Reverse engineering the
source. Font scaling is very easy to implement and controllers
does not require any additional code.
Some research into the internal construction of
23
FPGA BASED VGA DRIVER
AND ARCADE GAME
Nintendo controllers was done beforehand, in Shift register to FPGA
order to confirm the fitness for the purpose. After
A 74LCX125 lowvoltage buffer with 5V tolerant
the game pads were acquired a proper schematic
inputs was chosen to transform 5V output from a
diagram of the internal construction had to be
controller to 3.3V. This part is specifically made to
done.
interface 5V systems to 3V systems.
At the heart of the device is a 4021B 8bit static
The data lines are not pulled high inside the game
shift register used for paralleltoserial data
pads, so a 10k pullup resistor was used on the
conversion. Each of eight parallel inputs has a
board.
button assigned to it. There are also power, clock,
latch and serial data lines. The connections were
found using a multimeter in a continuity test 7.3 Board design
mode. Equivalent schematic diagram can be found
in Appendix D.
Schematic capture
Schematic capture was done using EasyPC EDA
7.2 TTL / CMOS level conversion software. The resulting schematic diagram is
The shift register inside the controller operates at shown in Appendix F.
TTL logic levels – 0V to 5V. The FPGA, however,
On the left there is a 100pin Hirose FX2
uses lowvoltage TTL/CMOS thresholds (0V to
connector, which plugs into the expansion port on
3.3V).
Xilinx development board.
While 3.3V from FPGA could be interpreted as a
The FPGA I/O pins were chosen to make PCB
logic high by TTL circuitry, 5V from the controller
design easier by reducing the length of tracks.
could possibly damage the FPGA. In order to stay
on the safe side, some sort of level conversion had A piezoelectric transducer and a RESET switch are
to be implemented. located at the top, near the FX2 connector.
There are many ways to combine TTL and CMOS On the right side there are level conversion chips
circuits. Approach used in this project is described and on the left – NES controller ports.
below. Both Nintendo and Hirose connector design files
were custom made. Design files for FX2
FPGA to Shift register connector were kindly provided by Philip Watson.
24
FPGA BASED VGA DRIVER
AND ARCADE GAME
Numerically Controlled (CNC) machine at
university. Soldering was done by hand, using a
hot air pencil for SMD components and soldering
iron for throughhole parts.
The case was machined from a sheet of clear
plastic with holes for controller ports and reset
switch. As the switch is located on the board,
extension rod was glued to improve accessibility. FIGURE 8.1: BALL DISTORTION
8.1 Issues
Synchronisation problems
During the course of the project, there has been a
number of issues with various things. Some
problems were minor, others required a bit more
effort to solve. This chapter discusses issues that
are most noteworthy.
25
FPGA BASED VGA DRIVER
AND ARCADE GAME
inspection with an oscilloscope. converted properly, since all bits would be zero
after just one shift.
Figure 8.2 shows two screen shots taken at
different times. Top trace shows the data from the The problem could not be identified by just
controller. Bottom trace is the enable signal. Note inspecting the code, so the Modelsim simulation
that the width of the enable signal was increased software was used to track the bug down.
in order to be viewed on the oscilloscope. It is
Stepbystep inspection made it trivial to notice
clear, that the the two signals drift in and out of
the early termination of the conversion. A counter
sync.
was added to track the number of bits shifted and
It was then, that it became clear, that the problem the problem was gone.
is really within the controller interface. User input
was not supposed to be retained between samples.
8.2 Further work
This caused malfunction and was the reason why
the enable signal had to be added in the first While most of the set objectives have been
place. When the problem was fixed, there was no achieved, there is still room for further
need to have the enable signal any more and improvement and experimentation.
spaceship control started to work properly.
If more time was given to work on this project, a
If there was still an need to limit the speed of the number of different goals could be considered.
spaceship, a simple counter could be used to
One possible approach to systems development on
divide the sample rate to appropriate value.
an FPGA is to use a softcore microprocessor along
with VHDL logic. Xilinx offers an 8bit RISC
Adaptor board microcontroller – PicoBlaze.
This one can be writtenoff to silly mistakes. After It would be possible to use a VHDL based VGA
the initial circuit was designed, time was not driver while implementing game logic on a soft
taken to doublecheck that the functionality of the core processor. This could potentially give more
adaptor matches the functionality of Nintendo flexibility and reduce the development time.
game pads.
Another possibility is to focus more on improving
The clock and latch signals were internally pulled the game features, or even develop an emulator
up, and, therefore, were activelow. It was falsely able to run more than a couple of games.
assumed, that the line was activehigh and the
switch in Figure 7.1 was tied to 5V instead of
GND. Temporary fix was to cut the track and 9 CONCLUSION
solder some wire between the pad and the ground The project was completed successfully – both
plane. aims and set objectives have been achieved.
The second mistake, was to omit the pullup The project was started by learning the basics of
resistors on the data lines. While the absence of FPGA development, going on to produce simple
these resistors did not affect the operation of a applications, before moving on to work with the
controller, behaviour was undefined when a VGA graphics and game development.
gamepad was not plugged in.
A number of modules have been developed, to
The second revision of the board was made to fix give extra functionality to the games. These
the remaining problems. include sound and text generation logic, a data
converter and an interface for game pads.
Binary-to-BCD conversion An adaptor board, connecting the game pads to
While implementing the “Shift and add 3” the development board, was designed and built.
algorithm, described in Chapter 3.5, a wrong As well as providing a nice integration between
condition was chosen for stopping the conversion. two very different devices, this task gave an
opportunity to work on circuit and PCB design.
When the bits were shifted to the left, a '0' was
appended in the place of the old LSB. This The project provided valuable experience in FPGA
implied, that the conversion should be finished based system design and was really enjoyable.
when all bits were zero. Such a condition meant
that numbers like 1000002 would not be
26
References
1. Xilinx, Inc., 2008. Xilinx Spartan-3E FPGA Family Datasheet. Available at:
http://www.xilinx.com/support/documentation/data_sheets/ds312.pdf [Accessed 1 March 2010]
2. Ashenden, Peter J., 1996. The Designers Guide to VHDL. San Francisco: Morgan Kaufmann
Publishers, Inc.
3. Wakerly, John F., 2006. Digital Design: principles and practices. New Jersey: Pearson Education, Inc.
4. Xilinx, Inc., 2008. Xilinx Spartan-3E FPGA Starter Kit User Guide. Available at:
http://www.xilinx.com/support/documentation/boards_and_kits/ug230.pdf [Accessed 26 February
2010]
5. Myers, Robert L., 2002. Display interfaces: fundamentals and standards. Chichester: John Wiley and
Sons
6. http://martin.hinner.info/vga/timing.html [Accessed 27 February 2010]
7. Chu, Pong P., 2008. FPGA prototyping by VHDL examples. New Jersey: John Wiley & Sons
8. ON Semiconductor, 2005. MC14021B: 8-Bit Static Shift Register. Available at:
http://www.onsemi.com/PowerSolutions/product.do?id=MC14021BCP [Accessed 9 March 2010]
9. http://code.google.com/p/uzebox/ [Accessed 26 March 2010]
Bibliography
10. Holdsworth, Brian., Woods, Clive., 2002. Digital Logic Design. Newnes
11. Short, Kenneth L., 2008. VHDL for Engineers. Prentice Hall
APPENDIX A
AASP1
Build a simple arcade game, using an FPGA development board. The project will involve exploring the
capabilities of the board, and FPGAs in general, before moving on to develop a custom made arcade
game. The project will involve the use of a Xilinx Spartan3E development board and will be based
around the use of VHDL as a tool to describe and implement digital hardware designs onto the
development board.
The following objectives would form the bulk of the project
• To complete and fully understand the capabilities of the development board
• To implement a VGA interface module capable of displaying visual information on a computer
screen
• To implement control i/o functionality capable of interfacing to a controller
• To design and implement a custom arcade game, using a VGA monitor and custom made
controller
If time allows, the following objectives will be completed
• To design and build a simple controller
• To design and implement sound effect functionality
• To implement more complex game functions, including a non volatile high score table, two
player mode, etc. etc.
APPENDIX B
74LCX125 £0.44
74HCT4066 £0.42
Total £22.70
APPENDIX C
Project plan
APPENDIX D
Photographs