Você está na página 1de 30

HA NOI UNIVERSITY OF SCIENCE AND TECHNOLOGY

SHOOL OF ELECTRONIC AND TELECOMMUNICATION EMBEDDED SYSTEM AND RECONFIGURABLE COMPUTING LAB

VHDL TRAINING COURSE 2013


Project: TANK GAME ON FPGA
Members Hieu Q. Nguyen Dong T. Pham Nhung H. Le

Ha Noi, August 09, 2013

VHDL Training course Tank game Project

Contents

I. Introduction ................................................................................................................4 1. Target .......................................................................................................................4 2. Summary ..................................................................................................................4 3. Requirement.............................................................................................................4 3.1. Functional requirements: .................................................................................4 3.1.1. Hardware: .....................................................................................................4 3.1.2. Software: ............................................................................................................4 3.2. Non functional requirement:..........................................................................5 II. Design system ............................................................................................................6 1.System block diagram ..............................................................................................6 2.Detail system: ...........................................................................................................7 3.Function of each block .............................................................................................8 III. Detail block .............................................................................................................9 1. VGA Monitor ...........................................................................................................9 1.1 Introduction .....................................................................................................9 1.2. Operating principle ...........................................................................................9 2. VGA block. ............................................................................................................12 2.1. Horizontal synchronization ............................................................................12 2.2. Vertical synchronization .................................................................................14 3. PS/2 Keyboard: ......................................................................................................16 3.1.1. Introduction: In our project, we use PS/2 keyboard to communicate between players and games. ..................................................................................16 3.2. Design the PS/2 block: ...................................................................................16 4.Tank: .......................................................................................................................20 4.1.Direction: .........................................................................................................20 4.2. Movement .......................................................................................................20 5. Bullet: ....................................................................................................................22 6. Display block .........................................................................................................23 6.1 Control exchange between screens ..................................................................23 2

VHDL Training course Tank game Project

6.2 Receive the position of object and display it on the screen ............................23 7. Item ........................................................................................................................25 8. Score ......................................................................................................................28 9. Check score: ..........................................................................................................29 IV. Problem: ..................................................................................................................30 V. Development .............................................................................................................30 VI. Conclusion ...............................................................................................................30

VHDL Training course Tank game Project

I. Introduction
1. Target

- Practice VHDL programming skills - Improve peripheral device-communicating skill (VGA,PS/2) - Improve soft skills (teamwork, presentation,) 2. Summary

Based on the classic games tank, we want to create a new tank games with some innovation about the gameplay. The games is design for 2 players, each players control one tank. Mission of each player is to destroy the enemy by shoot enemy, each time one player be shot, the players HP reduce. The players whose HP come to 0 will lose. The innovation of our games is the tank can move and shoot follow 16 directions (when classi tank games has only 4 directions). In addition, the another innovation is the bullet. When bullet collide the wall, it will rebound, that makes the games more excited. In game, there is also many items appear at random place on the map, the items will strengthen the power of players.

3. Requirement 3.1. Functional requirements: 3.1.1. Hardware: - Implement on kit DE1 of Altera - Screen display VGA 640x480 pixels - Keyboard standard PS/2 3.1.2. Software: - Use Quartus 10.1 sp1 - Hardware Description Language: VHDL. - Game is designed for two players playing at the same time. - Items is generated randomly for each time playing. - Map is designed by programmer - Times of game is 5 minutes, game over when HP of one player turn to 0 or if the time is up the winner is the player whose HP is greater. - Players 1 use up, down to move forward or back, use left, right key to rotate the tank 4

VHDL Training course Tank game Project

s direction, use p to fire - Players 2 use a, s to move forward or back, use a, d to rotate the tank s direction, use left shift to fire - The game can pause when press f - Three kind of items + Boot : Enhance movement speed of tank + Bottle: Immortal in 7 seconds + Double damage: Increase damage of tank 3.2. Non functional requirement: - Short responding time for keyboard signal to help players to control character smoothly. - Frequency use 50MHz from CLOCK_50 on kit DE1, divide into 25 MHz - Screen refresh rate: 60Hz - Playing screen: + Playing area: size 640 x 384 pixel, element stone and grass display in a block of size is 16x16 and tank size is a block 32x32. + Information area: size 640 x 96 pixel, display items, names, HP of 2 players. Text size is 16 x 16.

VHDL Training course Tank game Project

II. Design system

Block diagram

1.System block diagram This game receives and processes data from keyboard, displays images on VGA 640x480 monitor. The main block of this program is tank game. The input and output of this block are: The input signal: - CLOCK_50: 50 MHz clock from kit DE1. - PS2_DAT: data signal of keyboard through PS2 pin. - PS2_CLK: special clock of signal transferring type through PS2 gate. The output signal: - VGA_HS: horizontal synchronous signal of monitor. - VGA_VS: vertical synchronous signal of monitor. - VGA_RGB: color signal of the pixel that is being processed

VHDL Training course Tank game Project

2.Detail system:

VHDL Training course Tank game Project

3.Function of each block Block VGA_sync

Function Control VGA synchronous Receive signal from keyboard and send control signal Exchange between screen Display text, display map, tank, bullet, HP Control tank s direction, position Control bullet s position Calculate tank s HP Determine result of the game Generate item on map randomly

PS/2

Display

Tank Bullet Score Check score Item

VHDL Training course Tank game Project

III. Detail block


1. VGA Monitor 1.1 Introduction VGA (video graphics array) is a video display standard introduced in the late 1980s by IBM and is widely supported by PC graphics hardware and monitor. We will design a basic 4096-color 640-by-480 resolution interface for CRT (cathode ray tube) monitors. 1.2. Operating principle

Conceptual diagram of a CRT Monitor


A vertical deflection coil and a horizontal deflection coil outside the tube produce magnetic-field to control how the electron beam travels and to determine where on the screen the electrons hit. In todays monitors, the electron beam traverses the screen systematically in a fixed pattern, from left to right and from top to bottom.

VHDL Training course Tank game Project

CRT scanning pattern

10

VHDL Training course Tank game Project

The electron beam moves from the left edge to the right edge as the voltage applied to the horizontal deflection coil gradually increases. After reaching the right edge, the beam returns rapidly to the left edge when the voltage changes to 0. Example for three-bit VGA color combination

11

VHDL Training course Tank game Project

2. VGA block.

640x480 are the number of pixels that can be displayed on the screen. Each horizontal line has 640 pixels and each vertical line has 480 pixels. In fact, there are 800 pixels in horizontal line and 525 pixels in vertical line that need to be scaned. The pixels and lines which are not displayed are called black border. 2.1. Horizontal synchronization A period of horizontal synchronous signal contains 800 pixels and can be divided into four regions:

12

VHDL Training course Tank game Project

Display: region where the pixels are actually displayed on the screen. The length of this region is 640 pixels. Retrace: region in which the electron beams return to the left edge. The video signal should be disabled (i.e., black), and the length of this region is 96 pixels. Right border: region that forms the right border of the display region. It is also known as the front porch (i.e., porch before retrace). The video signal should be disabled, and the length of this region is 16 pixels. Left border: region that forms the left border of the display region. It is also known as the back porch (i.e., porch after retrace). The video signal should be disabled, and the length of this region is 48 pixels. Note that the lengths of the right and left borders may vary for different brands of monitors.

13

VHDL Training course Tank game Project

The horizontal synchronous signal can be obtained by a special mod-800 counter and a decoding circuit. The counts are marked on the top of the horizontal synchronous signa. We intentionally start the counting from the beginning of the display region. This allows us to use the counter output as the horizontal (x-axis) coordinate. This output constitutes the pixel-x signal. The horizontal synchronous signal goes low when the counters output is between 656 and 751. 2.2. Vertical synchronization During the vertical scan, the electron beams move gradually from top to bottom and then return to the top. This corresponds to the time required to refresh the entire screen. The format of the vertical synchronous signal is similar to that of the horizontal synchronous signal. The time unit of the movement is represented in terms of horizontal scan lines. A period of the vertical synchronous signal is 525 lines and can be divided into four regions:

Display: region where the horizontal lines are actually displayed on the screen. The length of this region is 480 lines. Retrace: region that the electron beams return to the top of the screen. The video signal should be disabled, and the length of this region is 2 lines.

14

VHDL Training course Tank game Project

Bottom border: region that forms the bottom border of the display region. It is also known as the front porch (i.e., porch before retrace). The video signal should be disabled, and the length of this region is 10 lines. Top border: region that forms the top border of the display region. It is also known as the back porch (i.e., porch after retrace). The video signal should be disabled, and the length of this region is 33 lines. The vertical synchronous signal can be obtained by a special mod-525 counter and a decoding circuit. Again, we intentionally start counting from the beginning of the display region. This allows us to use the counter output as the vertical (y-axis) coordinate. This output constitutes the pixel-y signal. The vertical synchronous signal goes low when the line count is 490 or 491.

15

VHDL Training course Tank game Project

3. PS/2 Keyboard: 3.1.1. Introduction: In our project, we use PS/2 keyboard to communicate between players and games.

Operating principle: - When press a key, the make code is transmitted. If a key is pressed continuously the make code is transmitted repeatedly. - When release a key, the break code of key is transmitted. 3.2. Design the PS/2 block: The design of the PS2 port receiving subsystem is somewhat similar to that of a UART receiver. Instead of using the oversampling scheme, the falling-edge of the ps2c signal is used as the reference point to retrieve data. The subsystem includes a falling edge detection circuit, which generates a one-clock-cycle tick at the falling edge of the ps2c signal, and the receiver, which shifts in and assembles the serial bits.

16

VHDL Training course Tank game Project

Timing Diagram of a PS/2 port

The flow chart show behind is the ASMD chart of the PS/2 receiver block. . The receiver is initially in the idle state. After the first falling-edge tick the FSMD shifts in the start bit and moves to the dps state. Since the received data is in fixed format, we shift in the remaining 10 bits in a single state rather than using separate data, parity, and stop states. The FSMD then moves to the load state, in which one extra clock cycle is provided to complete the shifting of the stop bit, and the psrxdone-tick signal is asserted for one clock cycle

17

VHDL Training course Tank game Project

ASMD of PS/2 receiver

Our PS/2 block have 3 mini block ps2_receiver, 2 buffer, and keyboard controller. -PS/2 receiver receive scan code from the keyboard. Scan code is sent to 2 buffer and then to keyboard controller. Keyboard controller will read scan code and identify which key is pressed and send it to output. This figure behind is the block diagram of

18

VHDL Training course Tank game Project

PS/2 keyboard.

Block diagram of PS/2 block

19

VHDL Training course Tank game Project

4.Tank: 4.1.Direction:

Tank s direction

- Tank can rotate 16 directions - Each direction is numbered - There is a signal name dir_reg which manage direction of tank - Each time, right key(with player 1) or d key( with player 2) is press, direction plus one - Each time, left key(with player 1) or a key( with player 2) is press, direction decrease one - With each different dir_reg, the game will load the suitable image of tank to the screen 4.2. Movement - With different direction, the velocity according to x-ordinate and velocity according y-ordinate is different - To resolve this problem, we use 2 array is velocity_x and velocity_y with index of two arrays is signal dir_reg to save the velocity - The value of 2 array will show here 20

VHDL Training course Tank game Project

velocity_x = { 0 , 1, 2, 2, 2, 2, 2, 1, 0 , -1 , -2 , -2, ,2 , -2, -2, -1, 0 }; velocity_y = { 2, 2, 2, 1, 0, -1, -2, -2, -2, -2, -2, -1, 0, 1, 2, 2, 2, 2 }; - Each time key up is pressed and ref_tick = 1 then tank_next_x <= tank_reg_x + vx(to_integer(dir_reg)); tank_next_y <= tank_reg_y + vy(to_integer(dir_reg)); - The flow chart show here is show tank s processing:

21

VHDL Training course Tank game Project

5. Bullet: - Each tank can fire 4 bullets at maximum, if a tank has already fire all 4 bullets, this tank have to wait for a while till the bullet disappears then tank can continue to fire - Bullet will move follow the current direction of tank - If wall is detected, the bullet will rebound - If bullet move to corner of the wall, hit the tank or rebound four or five times, the bullet will disappears

22

VHDL Training course Tank game Project

6. Display block Function of display block is - Control exchange between screen - Receive the position of objects (tank, bullet, map, text) and display it on the screen 6.1 Control exchange between screens This figure behind now show you the FSM of screens

FSM state of screens

6.2 Receive the position of object and display it on the screen Display block receive the position of objects and turn it into signal tank_on, bullet_on, text_on, grass_on, wall_on and display it in different layer. 6.2.1 Grass and wall display - Wall and grass is displayed in a 16x16 block - We store image of each element in ROM using ROM 1 port LPM in Quartus II. Input of component grass and stone is 4 bit least of pixel x and pixel y, output is 12 bit equivalent to 12 bit colors. Base on control signal of each element, color of each pixel will be assigned by output rgb from corresponding ROM. 6.2.2 Tank display

Tank s image

23

VHDL Training course Tank game Project

- Tank is displayed in a 32x32 block - We need to create 16 images equivalent 16 directions of each tank, there are two tanks so we need totally 32 images of tank - We also use ROM 1 port LPM in Quartus to store image - There are total 32 images of tank, so we need to reduce quality of images of tank from 12 bit to 3 bit to fit the size of memory

- So we use Photoshop to create images 3 bit colors so we can resolve this problem 6.2.3 Text display Each character is 8x8 bit word of pixel data. To display an "A" the character ROM would contain only the starting address 000001 for the font table for "A". The 8x8 font in the character generation ROM would generate the letter "A" using the following eight memory words:

24

VHDL Training course Tank game Project

7. Item Item block has four blocks include item1_gen block, item2_gen block, item3_gen block and item_process block. Item1 is double damage item. Item2 is heal item. Item3 is fast boots item.

25

VHDL Training course Tank game Project

In three blocks item1_gen, item2_gen, item3_gen is designed in order to test appearance of three item. We use two signal pix_x and pix_y so appear items in identified position. Signal tank_got_item is used so dispear items.

26

VHDL Training course Tank game Project

Algorithms of eat item and appear items Block item process show eat item process. Time implementation features of items is decided by tank_got_item . After times tank_got_item =1 then item will appear and features of items is finished. Item_on =1 and tank_on=1 in this position then tank_got_item=1.

27

VHDL Training course Tank game Project

8. Score Score block show score of two player in playing process.

Signal in is tank_got_item and signal out is output in order to display score in play screen. Tank_got_item has three type include HP_got_item, damage_got_item, HP_decrease.

Score max =100. If HP_decrease =1 then score <= score -10. If HP_decrease and damage_got_item =1 then score <= score -20 . If HP_got_item =1 then score <= 100.

28

VHDL Training course Tank game Project

9. Check score:

29

VHDL Training course Tank game Project

IV. Problem:
- Bullet dont rebound exactly - Tank s movement is extremely not smooth

V. Development
- Make sound for games - Improve items make game more interesting

VI. Conclusion
After a month, our groups finish project about tank game. The games can be played extremely well though still have some problems. After this, we improve so much about VHDL skills, teamwork, soft skill Finally, we thanks so much to Mr. Trong, Mr. Luc, Mr. Tung helped me so much in this time! Group 3 K55

30

Você também pode gostar