Sie sind auf Seite 1von 4

Tetris game design based on the FPGA

Kai Liu, Yuliang Yang, Yanlin Zhu


Dept. of Communication Engineering, School of Computer & Communication Engineering, USTB, Beijing, China
AbstractTetris game is a classic game of logic control. This article
gives a programming design of Tetris game based on the FPGA
using VHDL. Game players can move and rotate blocks with the
PS/2 interface keyboard, and the game video is showed in a VGA
monitor. The game realized the function of the movement and
rotation of blocks, randomly generating next blocks, eliminating
rows, getting scores and speeding up. It also contained the normal
mode corresponding to 7 types of blocks and the expert mode
corresponding to 11 types of blocks. The successful transplant of
Tetris game provides a template for the development of other
visual control systems in the FPGA.
Keywords- FPGA; VHDL; Tetris game;
I. INTRODUCTION
Tetris game is a popular television and handheld game.
Because of its strict logic, simple operation, appropriate
programming difficulty, strong entertainment and other
characteristics. Tetris game has been transplanted to various
platforms, such as personal computers, mobile phones, handheld
game consoles. It spawns a variety of different versions, and
becomes one of the classic programming training and game
development theme.
This programming of Tetris game on the FPGA can provide
templates for the similar development of visual control system
and game development.
II. OVERVIEW OF THE GAME DESIGN
The main body of the game is composed of three parts:
controlling input, processing logic, and displaying output.
Controlling input part includes PS/2 interface keyboard.
Players use keyboards to select the game mode, control the game
process, and complete the movement and rotation of the blocks.
Logic processing part is using a FPGA chip to complete. The
program is totally realized in the FPGA chip, and communicates
with the controlling input and displaying output part through the
corresponding interface.
Display part transmits the VGA signals to outer. With an
external monitor shows games screen, visual operation is
complete.
III. STRUCTURE OF THE GAME MODULES
The game is composed of 7 modules: keyboard control
module, game control module, text display module, and graphic
display module, storage unit, multiplexing unit and VGA
modules. The graphic display module and text display module
share VGA control through multiplexing units; game control
modules, graphic display module and text display module
complete data exchange through the storage units. The specific
module structure diagram is shown in Figure 1.
l)|oud
outo'
_um
outo'
mmo)
uu`t
_uh`
d`:'u)
txt
d`:'u)
mu't`'x`u_
uu`t
\0^
modu'
Figure 1. Structure of the program module
A. Keyboard control module
The keyboard control module is identified by the keyboard
recognition and the filter of key value. Among them, the
keyboard recognition part completes one-way communication
from PS/2 keyboard to FPGA. When reading the scan code, tip
signal an turns from 0 to 1, or from 1 to 0; Components
which been connected with this module, can determine whether
the scan codes has been read through the value of the signal
an change.
The key value filter can filter the scan codes of the keyboard.
And then other values are filtered out with the needed scan
codes left. Keys that are used in table 1.
Table 1. Game key function table
Keys Function description
F1 Stop game
F2 Normal game mode
F3 Expert game mode
A Move blocks left
D Move blocks right
space Rotate blocks
B. Game control module
The game control module is the main body of the game.
Through the state machine describes the process of the game.
A total of 15 States are in the game, which means 15 kinds of
values of the corresponding state. Figure 2 shows the game state
transition diagram. Table 2 describes state machine.
Whether in the normal or expert mode, the blocks are
composed of four sub-blocks. All blocks operation is achieved
by changing the coordinate values of sub-blocks.
This work was supported by the National Natural Science Foundation of
China (N.S.F.C) grant funded by the china government (No.61170225).
2925
978-1-4577-1415-3/12/$26.00 2012 IEEE
0000
000! 00!0
00!!
0!00
!00! 0!!!
!0!0 !000
!0!!
!!00
0!0!
0!!0
!!!0 !!0!
Figure 2. Game state transition diagram
Table 2. Game state machine description
State No. State State description
0000 stop Press F1 or game over
0001 normal Press F2 to enter normal mode
0010 master Press F3 to enter expert mode
0011 wait Wait for cntClk signal; then turn to
test_down state or read command form
keyboard
0101 test_down Try to make the current active blocks fall to
the next line; if there have obstacles, turn to
test_stop, or turn to step_down.
0110 step_down The current active blocks drop a line.
0111 test_right Try to make the current activation block
move right a column; if there are barriers,
jump to wait, or jump to step right.
1000 step_right The current active block moves right a
column.
1001 test_left Try to make the current activation block
move left a column; if there are barriers ,
jump to wait, or jump to step_left
1010 step_left The current active block moves left a
column.
1011 test_rotate Try to make the current activation block
rotate as presets; if there are barriers , jump
to wait, or jump to rotate
1100 step_rotate The current active block rotates as presets.
1101 test_stop Determine whether y-coordinate is 1, if so
jump to stop, or jump to del_rows.
1110 del_rows Determine the rows eliminating conditions,
if it meets, then execute rows elimination
processing.
1) Method of randomly generating blocks
After the current block dropped down, it needs to randomly
generate a new blocks for players to operate in the course of the
game.Each block is assigned a random value during initialization,
that means the blocks newly generated gets a randomly value
within its range. The method to get a random value is that we
make the random value accumulate with the game clock signals
instead of using the pseudo random sequence. Due to each
blocks lifetime varying from the others, it causes the random
value staying in a random value within its scope.
if (cntCLk(0)'event and cntCLk(0) = '1') then
if random="110" then
random<="000";
else
random<=random + 1;
end if;
2) Method of changing blocks state
During the game, blocks need to complete the actions of
dropping, moving around, and rotating. Attempting action is
required before completing the actions. Namely it judges
whether the coordinates of the position already exists obstacles
before moving. When there is no obstacle, blocks can move
under the command. Otherwise the blocks enter a waiting state.
Take a T shape block in the normal mode for a example:
|+
|! |? |3
The game judges whether the position just below B2 is
available before doing rotating action. That means to judge the
location: cx<=b2x ;
cy<=b2y + 1;
If there exists obstacle, the storage unit returned rectin(3) ='1',
otherwise it returns '0' .
When it satisfies rectin(3)= '0' , do this rollover action:
b1x<=b1x + 1; b1y<=b1y + 1;
b3x<=b3x 1; b3y<=b3y 1;
b4x<=b4x 1; b4y<=b4y + 1;
3) Scoring module and speed control
Eight registers are used by the games scoring module for
storing the current score and the highest score. When del_rows
state sent one elimination instruction n1, the current score
increases by 1.When the score reach 100, 200, 300, the dropping
speed of blocks will accelerate.
C. Graphics and text display module
This module produces pixel graphics and text information
for displaying and communicates with dual-port block memory
(BRAM). Display module with pin CX and CY completes
addressing operation of BRAM; the game control module gets
feedback by reading the corresponding address of the memory
location; display signal is generated by writing the
corresponding memory address.
1) Method of displaying blocks graphics
Graphics needed in the game display all in one virtual
coordinate zone of 10*20.CX and CY pin signal corresponds to
the virtual coordinate of the location. BRAM are addressed
through the virtual coordinate operations. Specific graphic
display method is shown in Figure 3.
2926
x 9,0
0,!9 9,!9

)
0,0
Figure 3. Graphic display method
In the virtual coordinate system, each square is a 16 * 16 set
of pixels. Each of the 16 * 16 pixel set is regarded as a graphic
display unit. In a graphic display unit, the outermost layer (white
square area) pixel is set to:
pix (2 downto 0) <= 000;
The outer layer (light gray area) pixels set to:
pix(2) <= mclk when rect(0)= '1' else '0';
pix(1) <= mclk when rect(0)= '1' else '0';
pix(0) <= mclk when rect(0)= '1' else '0';
The inner layer (dark gray area) pixels set to:
pix (2 downto 0) <= rect(2 downto 0);
2) Method of displaying text character
We design a text display area, in which players can see key
tips and scores information. The display principle of the text
characters display area is similar to graphic displaying. Regard a
collection of 8*8 pixels as a characters display unit. Pixel display
effect appears as shown in Figure 4.
Figure 4. Character display method
Set the pixel white squares area to:
pix (2 downto 0) <= 000;
Set the pixel gray squares area to:
pix (2 downto 0) <= 111;
By fast read/write to the storage unit, graphic display module
and text display module, constantly transmits new display signal
to VGA mode to complete the refreshment of the screen, giving
game operator the right visual feedback.
D. VGA module
The function of VGA module in the game is to translate
coordinate and pixel settings information received from the
graphic display module and the text display module into VGA
control signal, and then correctly display the image information
by a monitor.
This module also helps divide game and feature area on the
screen, as shown in Figure 4.
HC and VC, is respectively the VGA Line synchronizing
signal Field synchronizing signal.When HC and VC scanned are
into two specific areas, return '1' as vid and text values, which
means this two range is a legal area. Graphic display module
operates the area vid='1', and text display module operates the
area text='1'.
0uh`
d`:'u) uu
v`d !`
txt d`:'u)
uu
txt !`
0,0 800,0
h
v

0,?! 800,?!
!99,99 3b0,99
399,99 bb,99
399,??8 bb,??8
!99,+?0 3b0,+?0
Figure 5. Overall layout of game screen
IV. VERIFIED IMPLEMENTATION OF GAME DESIGN AND
RESULT ANALYSIS
The game program is done in ISE12.3 environments using
VHDL language. In the validation experiment we used the
Xilinx's spartan-3AN FPGA starter boards, a PS/2 interface
keyboard, and a CRT monitor.
During the initial validation experiments, blocks appear
garbled. Each sub-block does not display in predefined
coordinate. Dropping and moving action can work, but rotating
action cannot be completed. The reason is that the signal
random is not synchronized with game control program clocks,
and when generating new blocks, assignment delay leads to
temporal disorder and being unable to initialize the shapes of
blocks as preset. The solution is to unify the clock of the change
of the random value and the game control.
Eventually, the game runs perfectly, with keyboard
controlling without delay, image displaying accurately, and
achieving all the program settings.
REFERENCES
[1] Xu Xin, Hongqi Yu, Yi Fan, Lu Qizhong. based on FPGA in embedded
system design. Beijing: China machine press, 2004. 9.
[2] Du Yong. FPGA/VHDL Design entry and advanced. Beijing: China
machine press, 2011
[3] Liu Tao, Xinghua Lou. FGPA digital electronic systems design and
development. Beijing: people's Publishing House of posts and
telecommunications, 2005.6.
[4] Xilinx. Spartan-3 Generation FPGA User Guide.
[5] Wayne Wolf. FPGA-based system design. Beijing:Chnia Machine
Press,2005.
2927
[6] Fuqi Liu. FPGA embedded project development practice. Beijing: Publishing House of electronics industry, 2009.
2928

Das könnte Ihnen auch gefallen