Sei sulla pagina 1di 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

Abstract—Tetris game is a classic game of logic control. This article share VGA control through multiplexing units; game control
gives a programming design of Tetris game based on the FPGA modules, graphic display module and text display module
using VHDL. Game players can move and rotate blocks with the complete data exchange through the storage units. The specific
PS/2 interface keyboard, and the game video is showed in a VGA module structure diagram is shown in Figure 1.
monitor. The game realized the function of the movement and
JUDSKLF
rotation of blocks, randomly generating next blocks, eliminating GLVSOD\
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
NH\ERDUG JDPH PHPRU\ PXOWLSOH[LQJ 9*$
Tetris game provides a template for the development of other FRQWURO FRQWURO XQLW XQLW PRGXOH
visual control systems in the FPGA.

Keywords- FPGA; VHDL; Tetris game; WH[W


GLVSOD\

I. INTRODUCTION Figure 1. Structure of the program module

Tetris game is a popular television and handheld game. A. Keyboard control module
Because of its strict logic, simple operation, appropriate The keyboard control module is identified by the keyboard
programming difficulty, strong entertainment and other recognition and the filter of key value. Among them, the
characteristics. Tetris game has been transplanted to various keyboard recognition part completes one-way communication
platforms, such as personal computers, mobile phones, handheld from PS/2 keyboard to FPGA. When reading the scan code, tip
game consoles. It spawns a variety of different versions, and signal “an” turns from ‘0’ to ‘1’, or from ‘1’ to ‘0’; Components
becomes one of the classic programming training and game which been connected with this module, can determine whether
development theme. the scan codes has been read through the value of the signal
This programming of Tetris game on the FPGA can provide “an” change.
templates for the similar development of visual control system The key value filter can filter the scan codes of the keyboard.
and game development. And then other values are filtered out with the needed scan
codes left. Keys that are used in table 1.
II. OVERVIEW OF THE GAME DESIGN
Table 1. Game key function table
The main body of the game is composed of three parts: Keys Function description
controlling input, processing logic, and displaying output. F1 Stop game
Controlling input part includes PS/2 interface keyboard.
F2 Normal game mode
Players use keyboards to select the game mode, control the game
process, and complete the movement and rotation of the blocks. F3 Expert game mode
Logic processing part is using a FPGA chip to complete. The A Move blocks left
program is totally realized in the FPGA chip, and communicates D Move blocks right
with the controlling input and displaying output part through the space Rotate blocks
corresponding interface.
Display part transmits the VGA signals to outer. With an B. Game control module
external monitor shows games screen, visual operation is The game control module is the main body of the game.
complete. Through the “state machine” describes the process of the game.
A total of 15 States are in the game, which means 15 kinds of
III. STRUCTURE OF THE GAME MODULES
values of the corresponding state. Figure 2 shows the game state
The game is composed of 7 modules: keyboard control transition diagram. Table 2 describes state machine.
module, game control module, text display module, and graphic Whether in the normal or expert mode, the blocks are
display module, storage unit, multiplexing unit and VGA composed of four sub-blocks. All blocks operation is achieved
modules. The graphic display module and text display module 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).
978-1-4577-1415-3/12/$26.00 ©2012 IEEE
2925
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
  
block’s 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
Figure 2. Game state transition diagram
required before completing the actions. Namely it judges
Table 2. Game state machine description
whether the coordinates of the position already exists obstacles
before moving. When there is no obstacle, blocks can move
State No. State State description
under the command. Otherwise the blocks enter a “waiting” state.
0000 stop Press F1 or game over Take a “T” shape block in the normal mode for a example:
0001 normal Press F2 to enter normal mode
E
0010 master Press F3 to enter expert mode
E E E
0011 wait Wait for cntClk signal; then turn to
“test_down” state or read command form The game judges whether the position just below B2 is
keyboard available before doing rotating action. That means to judge the
0101 test_down Try to make the current active blocks fall to location: cx<=b2x ;
the next line; if there have obstacles, turn to cy<=b2y + 1;
“test_stop”, or turn to “step_down”.
If there exists obstacle, the storage unit returned rectin(3) ='1',
otherwise it returns '0' .
0110 step_down The current active blocks drop a line.
When it satisfies rectin(3)= '0' , do this rollover action:
0111 test_right Try to make the current activation block b1x<=b1x + 1; b1y<=b1y + 1;
move right a column; if there are barriers, b3x<=b3x – 1; b3y<=b3y – 1;
jump to “wait”, or jump to “step right”. b4x<=b4x – 1; b4y<=b4y + 1;
1000 step_right The current active block moves right a 3) Scoring module and speed control
column. Eight registers are used by the games scoring module for
1001 test_left Try to make the current activation block storing the current score and the highest score. When “del_rows”
move left a column; if there are barriers ,
state sent one elimination instruction “n1”, the current score
increases by 1.When the score reach 100, 200, 300, the dropping
jump to “wait”, or jump to “step_left”
speed of blocks will accelerate.
1010 step_left The current active block moves left a
column. C. Graphics and text display module
1011 test_rotate Try to make the current activation block This module produces pixel graphics and text information
rotate as presets; if there are barriers , jump for displaying and communicates with dual-port block memory
to “wait”, or jump to “rotate” (BRAM). Display module with pin CX and CY completes
1100 step_rotate The current active block rotates as presets. addressing operation of BRAM; the game control module gets
feedback by reading the corresponding address of the memory
1101 test_stop Determine whether y-coordinate is 1, if so location; display signal is generated by writing the
jump to “stop”, or jump to “del_rows”. corresponding memory address.
1110 del_rows Determine the rows eliminating conditions, 1) Method of displaying blocks graphics
if it meets, then execute rows elimination Graphics needed in the game display all in one virtual
processing. coordinate zone of 10*20.CX and CY pin signal corresponds to
1) Method of randomly generating blocks the virtual coordinate of the location. BRAM are addressed
After the current block dropped down, it needs to randomly through the virtual coordinate operations. Specific graphic
generate a new blocks for players to operate in the course of the display method is shown in Figure 3.

2926
 F[ 
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
\
F
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'.
 KF 

   

  WH[WGLVSOD\


Figure 3. Graphic display method DUHD
In the virtual coordinate system, each square is a 16 * 16 set *UDSKLF
WH[W þÿ

of pixels. Each of the 16 * 16 pixel set is regarded as a graphic F


Y GLVSOD\DUHD  

display unit. In a graphic display unit, the outermost layer (white YLG þÿ
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); Figure 5. Overall layout of game screen
2) Method of displaying text character
We design a text display area, in which players can see key IV. VERIFIED IMPLEMENTATION OF GAME DESIGN AND
tips and scores information. The display principle of the text RESULT ANALYSIS
characters display area is similar to graphic displaying. Regard a
collection of 8*8 pixels as a characters display unit. Pixel display The game program is done in ISE12.3 environments using
effect appears as shown in Figure 4. 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
Figure 4. Character display method blocks as preset. The solution is to unify the clock of the change
Set the pixel white squares area to: of the “random” value and the game control.
pix (2 downto 0) <= “000”; Eventually, the game runs perfectly, with keyboard
Set the pixel gray squares area to: controlling without delay, image displaying accurately, and
pix (2 downto 0) <= “111”; achieving all the program settings.
By fast read/write to the storage unit, graphic display module REFERENCES
and text display module, constantly transmits new display signal
to VGA mode to complete the refreshment of the screen, giving [1] Xu Xin, Hongqi Yu, Yi Fan, Lu Qizhong. based on FPGA in embedded
system design. Beijing: China machine press, 2004. 9.
game operator the right visual feedback.
[2] Du Yong. FPGA/VHDL Design entry and advanced. Beijing: China
D. VGA module machine press, 2011
[3] Liu Tao, Xinghua Lou. FGPA digital electronic systems design and
The function of VGA module in the game is to translate development. Beijing: people's Publishing House of posts and
coordinate and pixel settings information received from the telecommunications, 2005.6.
graphic display module and the text display module into VGA [4] Xilinx. Spartan-3 Generation FPGA User Guide.
control signal, and then correctly display the image information [5] Wayne Wolf. FPGA-based system design. Beijing:Chnia Machine
by a monitor. Press,2005.

2927
[6] Fuqi Liu. FPGA embedded project development practice. Beijing: Publishing House of electronics industry, 2009.

2928

Potrebbero piacerti anche