You are on page 1of 38

SPACE INVADER: VHDL

Yuebing Jiang Andrea Wright

Abstract This report contains the development logic of the game Space Invader using VHDL on V2P development board. The inspiring result is that we can use keyboard to control the plane and we have converted bitmap into rom using matlab, and make the graph displayed beautifully. We have also implemented the interaction between the alien and the spaceship. Our game is partly divided into two separate modules. Andrea is incharge of the input (Keyborad) module, and Yuebing Jiang is in charge of the display and game logic (including uart terminal)module.

Contents
1 System overview 1.1 Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.2 Project Speccation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.3 Design scheme . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Dispaly and game logic 2.1 Display . . . . . . . . 2.2 Game logic Overview 2.3 Spaceship . . . . . . 2.4 Alien missile . . . . . 2.5 Spaceship missile . . 2.6 Alien . . . . . . . . . 2.7 Score Display . . . . 2.8 UART . . . . . . . . 3 3 keyboard 4 Synthsis 5 Appendix 5.1 matlab le . . . . 5.2 Spaceship . . . . 5.3 Spaceship missile 5.4 Alien missile . . . 5.5 Alien . . . . . . . 5.6 Score Display . . 5.7 UART logic . . . 5.8 Trival les . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 3 3 4 5 5 6 6 7 8 8 9 9 11 12 13 13 14 17 21 24 29 32 36

List of Figures
1.1 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 3.1 4.1 Overall design schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Alien and Spaceship . . . . Game logic overview . . . . Spaceship Schematic . . . . Alien missile Schematic . . . Spaceship missile Schematic Alien Schematic . . . . . . . Score display Schematic . . Score display Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 5 6 6 7 8 9 9 10 11 12

Keyboard blockgram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Design summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Chapter 1 System overview


1.1 Background

Space Invaders is an arcade video game designed by Tomohiro Nishikado, and released in 1978. It was originally manufactured and sold by Taito in Japan, and was later licensed for production in the United States by the Midway division of Bally. Space Invaders is one of the earliest shooting games and features two-dimensional graphics. The aim is to defeat waves of aliens with a laser cannon and earn as many points as possible. In designing the game, Nishikado drew inspiration from popular media: Breakout, The War of the Worlds, and Star Wars. Though simplistic by todays standards, it was one of the forerunners of modern video gaming and helped expand the video game industry from a novelty to a global industry. When rst released, Space Invaders was very successful and popular. Following its release, the game caused a temporary shortage of 100-yen coins in Japan, and by 2007 had earned Taito US $500 million in revenue. Guinness World Records ranks it the top arcade game.1

1.2

Project Speccation

The requirement is as follows: (1) Aliens start at the top of the screen, move left, right and down in a random fashion, bullets start at the bottom and travel up and tanks move left and right under user control. (2) Add a scoring mechanism to your code and display the score at the bottom of the screen. The bare minimum is Aliens Destroyed: x Tanks Destroyed: y where x and y are updated as the game progresses. (3) Add a feature that sends text back to the hyperterminal as the game progresses. And the feature we have added is (1) Use the keyboard to control the movement of the spaceship(not implemented completely) (2) Display the alien and spaceship in bitmap format. (3) Array of alien.
1

The background was referenced from wiki.com

1.3

Design scheme

The scheme we have developed is as follows:

Figure 1.1: Overall design schematic The overall system is divided into 4 parts: VGA synchronization unit, graph animate unit, uart and keyboard. all the display objects are included in the graph animate unit. The output of dierent units are combined with an or gate and sent to the display ports.

Chapter 2 Dispaly and game logic


2.1 Display

We will disscuss the display of bitmap here. We know a bitmap contains a large sum of data. When we choose a large image to display, the common method is to generate a COE le either use matlab or C (whatever). But in this game, we choose 1 32 32 bitmap to stand for the spaceship and 1 32 32 bitmap to stand for the alien. The image choose is as follows(Note there are some pixels in the image to demonstrate its intensity and we should remove these information before we turn them into rom):

Figure 2.1: Alien and Spaceship Since the image is 32 32 24(8 + 8 + 8) bits. We can turn it into a 1024 24 bits rom. Since we get the relative x pixel and y pixel of the image, we search it by simply concatenate them which is simply because either edge is 25 pixels. The matlab program is listed as appendix. Both the spaceship and Alien hava a rom le instantiated in its inner structure. The location scheme is based on the method mentioned above. However, there are 3 other things to display which have not been mentioned in the game, the score, the Alien missile and the Spaceship missile. The Alien missile and space missile is displayed using the inner rom exactly like how the ball is displayed in the Pingpong game. The text display is based on an outer text rom which contains the ascii talbe in a (128*16)*8 fashion. And the score is displayed in a 32 16 size which enlarged the orgianl pattern by two. The method is to divide the location bit address by two, then we dispaly the same thing on two line which makes the display enlarged by two.

2.2

Game logic Overview

The basic idea of the interaction between these object is depicted in the following diagram:

Figure 2.2: Game logic overview Spaceship provides its position to the missile sends from it. And it get the position from the alien missile. By detecting whether the position of the missile is in the area of the spaceship, we assert whether the spaceship is die or alive. If it dies then the space ship done is asserted and the number of lives in the text decreases 1. As to the alien, the same things happens. If the alien at the position of the space ship missile is reached, then it is set to the die state, it will gain life until all the alien are termanated.

2.3

Spaceship

Figure 2.3: Spaceship Schematic pixel x and pixel y stands for the position of the point currently in concern. ref r tick is the refresh tick which indicates the refresh of the screen. btn is used to control the position of the spaceship. allien missile x and allien missile y is used to determine whether there will be a hit. the other pins are output that will be used for other components.The Space ship x 6

and Space ship y stands for the current position of the spaceship. Space ship done is used to indicate that the spaceship is exploded when this pin is asserted. The behavior of the spaceship is parted as 3 parts: (1)Spaceships movement is controled by the keyboard. I use the button as interface to control the movement of the spaceship. (2)If it is hit by the missile, then it should give the missile reply signal and return its position to the left down area of the screen. (3)If it is hit by the alien missle, it should score board to decrease the life by one. (4) always tell the spaceship missle the position of the spaceship. The code construction of the spaceship is parted for 3 parts: (1) we have to instantiate a bitmap rom for the spaceship, get the relative positon to be displayed. (2)reset part to reset the start position of the spaceship (3)code to determine the position of the spaceship under the condition of button control, screen boundary and hit.

2.4

Alien missile

Figure 2.4: Alien missile Schematic

Alien missile x in and Alien missile y in stand for the position provided by the alien randomly, which means that the missile starts from this point.Alien missile x out and Alien missille y out is the singnal send to the spaceship.Spaces hipd one is the input from spaceship to terminate the current ying of the missile. The alien missile work out its behavior as 2 parts: (1) if the missile is nished its work(either hit the spaceship or reached the bottom), restart another emission based on the position of the missile output from alien. (2) always send to the spaceship its position. The code construction of the missile is parted for 3 parts (1)The reset part of its position, state. 7

(2) The state based on its position, hit or not. (3)The new position based on its state, current position and hit or not.

2.5

Spaceship missile

Figure 2.5: Spaceship missile Schematic The spaceship missile is mostly the same with the the alien missile. The Spaceship missile acts in the following segements: (1) The start of the missile is controled by the button. If the button is asserted then the missile start to run to the target. (2)If the missile hit one alien, then it should turn to start state at the position of the spaceship. (3) always tell alien its position. So we can tell the code structrue of the Spaceship missile into following parts: (1) Reset of all the state to the waiting, reset the position to the position of the spaceship. (2)Dispaly part of the spaceship missile. (3) Change the state of the spaceship missile according to the reply of the alien, the state and position of the spaceship missile. (4)Change the position of the spaceship missile according to the state, reply of the alien and its state.

2.6

Alien

Alien is the most complex component. First we have to make a conlusion of its behavior: (1) Start at the top left of the screen and move randomly. (2) Give a random position at the position of one alien to the alien missile at any time. (3) If one alien was hit, then it should turn its state to the diapear state, and do not display. (4) If all the alien were terminated, then all the state of the alien should turn to the live state. (5) Display itself according its state. According to the behavior of the alien. we should 8

Figure 2.6: Alien Schematic

divide the code structure into following parts: (1) Reset the state of all alien to alive, their position , speed. (2) Code for displaying the alien. (3) Change the alines state according to the position of the missile, the state of the alien.

2.7

Score Display

Figure 2.7: Score display Schematic The score display is just reading the rom le which is exactly told on the textbook. We can select an intereting area. set it as the display on, Then we can scale the font by cutting the LSB of the address to t the area.

2.8

UART

The UART is only used to display the hit incidents. So we can put it in the second top level and input two signals report the hit incidents between alien missile and spaceship and spaceship and the alien. Actually we only used the tx part. But since it is easier to adopt

Figure 2.8: Score display Schematic

a black box method. I have include a full UART to make sure using the least design eort. And this works well actually.

10

Chapter 3 3 keyboard
The PS/2 port on the FPGA board is an interface for the keyboard to communicate with the V2Pro board. We implement the project design to be from the keyboard to the V2Pro. The data is transferred to the FPGA in a stream that is similar to UART transmission. The design is also very similar to that of the UART receiver. The keyboard itself uses scan codes to send the data to the FPGA. Based on the keyboard buttons used, the PS2 sends a sequence of codes to the board. The PS2 Keyboard comes with its own data and clock. These are ps2c (for clock) and ps2d (for data) The output should be through the dout signal. This signal is sent through the scan code as the key code to the system. Note: The ps2c and ps2d are input signals in the ps2 rx block. The PS2 clock period is between 60 and 100 seconds.

Figure 3.1: Keyboard blockgram

11

Chapter 4 Synthsis
The whole design takes the resouces as follows,

Figure 4.1: Design summary The whole system is so complex that I do not want to show.

12

Chapter 5 Appendix
5.1 matlab le

%s p a c e s h i p 32 32 3 %i n v a d e r 0 32 32 3 %m a tl a b f i l e t o t u r n a bitmap i n t o a rom f i l e c lear figur e image=imread ( i n v a d e r . png ) ; image ( 1 : 2 , : , : ) = 0 ; imshow ( image ) figur e dum=image dum ( : , : , [ 2 , 3 ] ) = 0 ; imshow (dum ) ; figur e dum=image ; dum ( : , : , [ 1 , 3 ] ) = 0 ; imshow (dum ) ; figur e dum=image ; dum ( : , : , 1 : 2 ) = 0 ; imshow (dum ) ; f i d=fopen ( InvaderRom . vhd , w ) ; for j =1:32 for i =1:32 f p r i n t f ( f i d , \ ) ; t =[]; for k =1:3 for l =1:8 t =[ t , mod( image ( i , j , k ) , 2 ) ] ; image ( i , j , k)=image ( i , j , k ) / 2 ; 13

end end f p r i n t f ( f i d , %d%d%d%d%d%d%d%d%d%d%d%d%d%d %d%d%d%d%d%d%d%d%d%d , t ( 1 : 8 ) , t ( 9 : 1 6 ) , t ( 1 7 : 2 4 ) ) ; fprintf ( f i d , \ ,\n ) ; end end

5.2

Spaceship

libr ar y IEEE ; use IEEE . STD LOGIC 1164 . ALL ; use IEEE .NUMERIC STD . ALL ; entity S p a c e s h i p i s port ( c l k : in s t d l o g i c ; r e s e t : in s t d l o g i c ; p i x e l x : in s t d l o g i c v e c t o r ( 9 downto 0 ) ; p i x e l y : in s t d l o g i c v e c t o r ( 9 downto 0 ) ; r e f r t i c k : in s t d l o g i c ; btn : in s t d l o g i c v e c t o r ( 1 downto 0 ) ; a l i e n m i s s i l e x : in s t d l o g i c v e c t o r ( 9 downto 0 ) ; a l i e n m i s s i l e y : in s t d l o g i c v e c t o r ( 9 downto 0 ) ; S p a c e s h i p d o n e : out s t d l o g i c ; S p a c e s h i p x : out s t d l o g i c v e c t o r ( 9 downto 0 ) ; S p a c e s h i p y : out s t d l o g i c v e c t o r ( 9 downto 0 ) ; S p a c e s h i p R : out s t d l o g i c v e c t o r ( 7 downto 0 ) ; S p a c e s h i p G : out s t d l o g i c v e c t o r ( 7 downto 0 ) ; S p a c e s h i p B : out s t d l o g i c v e c t o r ( 7 downto 0 ) ); end S p a c e s h i p ; architecture B e h a v i o r a l of S p a c e s h i p i s signal S p a c e s h i p x r e g , S p a c e s h i p x n e x t : unsig ned ( 9 downto 0 ) ; signal S p a c e s h i p y r e g , S p a c e s h i p y n e x t : unsig ned ( 9 downto 0 ) ; signal S p a c e s h i p x l , S p a c e s h i p x r : unsig ned ( 9 downto 0 ) ; S p a c e s h i p L e f t and r i g h t b o u n d a r i e s . 14

signal S p a c e s h i p y t , S p a c e s h i p y b : unsig ned ( 9 downto 0 ) ; signal x d e l t a r e g , x d e l t a n e x t : unsig ned ( 9 downto 0 ) ; constant S p a c e s h i p V : i n t e g e r : = 4 ; constant S p a c e s h i p h e i g h t : i n t e g e r : = 3 2 ; constant S p a c e s h i p w i d t h : i n t e g e r : = 3 2 ; s p a c e s h i p 32 32 signal S p a c e s h i p o n : s t d L o g i c ; signal pix x , p i x y : unsig ned ( 9 downto 0 ) ; signal x , y : s t d l o g i c v e c t o r ( 4 downto 0 ) ; signal h i t : s t d l o g i c ; begin componentRom : entity work . SpaceshipRom port map ( v i d e o o n=>S p a c e s h i p o n , c l k=>c l k , x=>x , y=>y , R=>Spa ce ship R , G=>Spa ce ship G , B=>S p a c e s h i p B ); S p a c e s h i p p o s i t i o n pix x <=unsig ned ( p i x e l x ) ; pix y <=unsig ned ( p i x e l y ) ; boundary Space ship Space ship Space ship Space ship

x x y y

l <=S p a c e r <=S p a c e t <=S p a c e b <=S p a c e

ship ship ship ship

x x y y

reg ; l+S p a c e s h i p w i d t h 1; reg ; t+S p a c e s h i p h e i g h t 1;

S p a c e s h i p o n < =1 when ( ( S p a c e s h i p x l <=p i x x ) and ( pix x <=S p a c e s h i p x r ) and ( S p a c e s h i p y t <=p i x y ) and ( pix y <=S p a c e s h i p y b ) ) e l s e 0 ; h i t < =1 when ( ( S p a c e s h i p x l <=unsig ned ( A l i e n m i s s i l e x ) ) and ( unsig ned ( A l i e n m i s s i l e x )<= S p a c e s h i p x r ) and ( S p a c e s h i p y t <=unsig ned ( A l i e n m i s s i l e y ) ) and ( unsig ned ( A l i e n m i s s i l e y )<= S p a c e s h i p y b ) ) e l s e 15

0 ; S p a c e s h i p d o n e <=h i t ; process ( S p a c e s h i p o n , pix x , pix y , s p a c e s h i p x r e g , space ship y reg ) begin i f ( S p a c e s h i p o n = 1 ) then x<=s t d l o g i c v e c t o r ( p i x x ( 4 downto 0) s p a c e s h i p x r e g ( 4 downto 0 ) ) ; y<=s t d l o g i c v e c t o r ( p i x y ( 4 downto 0) s p a c e s h i p y r e g ( 4 downto 0 ) ) ; end i f ; end process ; r e s e t process ( r e s e t , c l k ) begin i f ( r e s e t = 1 ) then S p a c e s h i p x r e g <=( 0000010001 ) ; S p a c e s h i p y r e g <=( 0111000000 ) ; e l s i f ( c l k event and c l k = 1 ) then S p a c e s h i p x r e g <=S p a c e s h i p x n e x t ; S p a c e s h i p Y r e g <=S p a c e s h i p y n e x t ; end i f ; end process ; S p a c e s h i p new p o s t i t i o n process ( S p a c e s h i p x r e g , S p a c e s h i p x l , S p a c e s h i p x r , r e f r t i c k , btn ) begin S p a c e s h i p x n e x t <=S p a c e s h i p x r e g ; i f r e f r t i c k = 1 then i f ( h i t = 1 ) then S p a c e s h i p x n e x t <= 0000010001 ; e l s i f ( btn= 10 and S p a c e s h i p x l > 10) then S p a c e s h i p x n e x t <=S p a c e s h i p x r e g S p a c e s h i p V ; e l s i f ( btn= 10 and S p a c e s h i p x l < 10) then S p a c e s h i p x n e x t <=S p a c e s h i p x r e g ; e l s i f ( btn= 01 and S p a c e s h i p x r < 630) then S p a c e s h i p x n e x t <=S p a c e s h i p x r e g+S p a c e s h i p V ; e l s i f ( btn= 01 and S p a c e s h i p x r > 630) then S p a c e s h i p x n e x t <=S p a c e s h i p x r e g ; end i f ; end i f ; 16

end process ; S p a c e s h i p y n e x t <=S p a c e s h i p y r e g ; p o s i t i o n o u t p u t s p a c e s h i p x <=s t d l o g i c v e c t o r ( s p a c e s h i p x r e g ) ; s p a c e s h i p y <=s t d l o g i c v e c t o r ( s p a c e s h i p y r e g ) ; end B e h a v i o r a l ;

5.3

Spaceship missile

libr ar y IEEE ; use IEEE . STD LOGIC 1164 . ALL ; use IEEE .NUMERIC STD . ALL ; entity M y m i s s i l e i s port ( c l k : in s t d l o g i c ; r e s e t : in s t d l o g i c ; p i x e l x : in s t d l o g i c v e c t o r ( 9 downto 0 ) ; p i x e l y : in s t d l o g i c v e c t o r ( 9 downto 0 ) ; S p a c e s h i p x : in s t d l o g i c v e c t o r ( 9 downto 0 ) ; S p a c e s h i p y : in s t d l o g i c v e c t o r ( 9 downto 0 ) ; r e f r t i c k : in s t d l o g i c ; btn : in s t d l o g i c ; S t a t e t i c k : in s t d l o g i c ; M y m i s s i l e R : out s t d l o g i c v e c t o r ( 7 downto 0 ) ; My missile G : out s t d l o g i c v e c t o r ( 7 downto 0 ) ; M y m i s s i l e B : out s t d l o g i c v e c t o r ( 7 downto 0 ) ; M y m i s s i l e x : out s t d l o g i c v e c t o r ( 9 downto 0 ) ; M y m i s s i l e y : out s t d l o g i c v e c t o r ( 9 downto 0 ) ); end M y m i s s i l e ; architecture B e h a v i o r a l of M y m i s s i l e i s signal M y m i s s i l e x r e g , M y m i s s i l e x n e x t : unsig ned ( 9 downto 0 ) ; signal M y m i s s i l e y r e g , M y m i s s i l e y n e x t : unsig ned ( 9 downto 0 ) ; signal M y m i s s i l e x l , M y m i s s i l e x r : unsig ned ( 9 downto 0 ) ; my m i s s i l e L e f t and r i g h t b o u n d a r i e s . signal M y m i s s i l e y t , M y m i s s i l e y b : 17

unsig ned ( 9 downto 0 ) ; signal x d e l t a r e g , x d e l t a n e x t : unsig ned ( 9 downto 0 ) ; constant My missile V : i n t e g e r : = 1 ; constant M y m i s s i l e h e i g h t : i n t e g e r : = 8 ; constant M y m i s s i l e w i d t h : i n t e g e r : = 8 ; s p a c e s h i p 1 6 32 type rom type i s array ( 0 to 7 ) of s t d l o g i c v e c t o r ( 0 to 7 ) ; ROM d e f i n i n t i o n constant My missile ROM : rom type := ( 00011000 , 00111100 , 01111110 , 11111111 , 00111100 , 00111100 , 00111100 , 00111100 ); signal rom addr : unsig ned ( 2 downto 0 ) ; signal r o m c o l : unsig ned ( 2 downto 0 ) ; signal rom data : s t d l o g i c v e c t o r ( 7 downto 0 ) ; signal r o m b i t : s t d l o g i c ; signal M y m i s s i l e s t a t e r e g , M y m i s s i l e s t a t e n e x t : unsig ned ( 1 downto 0 ) ; 00 i s run , 11 u n s t a r t , signal M y M i s s i l e o n : s t d L o g i c ; signal pix x , p i x y : unsig ned ( 9 downto 0 ) ; begin Re s e t a l l t h e s t a t e process ( r e s e t , c l k ) begin i f ( r e s e t = 1 ) then M y m i s s i l e x r e g <=unsig ned ( S p a c e s h i p x ) ; M y m i s s i l e y r e g <=unsig ned ( S p a c e s h i p y ) ; M y m i s s i l e s t a t e r e g <= 11 ; e l s i f ( c l k event and c l k = 1 ) then M y m i s s i l e x r e g <=M y m i s s i l e x n e x t ; M y m i s s i l e Y r e g <=M y m i s s i l e y n e x t ; M y m i s s i l e s t a t e r e g <=M y m i s s i l e s t a t e n e x t ; end i f ; end process ; 18

c o l o r t h i n g s process ( M y m i s s i l e o n , ROM bit , M y m i s s i l e s t a t e r e g ) begin My missile R <= 00000000 ; My missile G <= 00000000 ; My missile B <= 00000000 ; i f ( M y m i s s i l e o n = 1 and ROM bit = 1 ) then My missile R <= 11111111 ; My missile G <= 00000000 ; My missile B <= 00000000 ; end i f ; end process ; o u t p u t t h e s t a t e o f t h e m i s s i l e s t a t e M y m i s s i l e s t a t e <=s t d l o g i c v e c t o r ( M y m i s s i l e s t a t e r e g ); o u t p u t t h e s t a t e o f t h e m i s s i l e p o s i t i o n M y m i s s i l e x <=s t d l o g i c v e c t o r ( M y m i s s i l e x r e g ) ; M y m i s s i l e y <=s t d l o g i c v e c t o r ( M y m i s s i l e y r e g ) ; g e t t h e u n s i g n e d p i x e l p o s i t i o n pix x <=unsig ned ( p i x e l x ) ; pix y <=unsig ned ( p i x e l y ) ; boundary M y m i s s i l e x l <=M y m i s s i l e x r e g ; M y m i s s i l e x r <=M y m i s s i l e x l+M y m i s s i l e w i d t h 1; M y m i s s i l e y t <=M y m i s s i l e y r e g ; M y m i s s i l e y b <=M y m i s s i l e y t+M y m i s s i l e h e i g h t 1; M y m i s s i l e o n < =1 when ( ( M y m i s s i l e x l <=p i x x ) and ( M y m i s s i l e x r >=p i x x ) and ( M y m i s s i l e y t <=p i x y ) and ( pix y <=M y m i s s i l e y b ) and ( M y m i s s i l e s t a t e r e g= 00 ) ) else 19

0 ; Get t h e rom a d d r e s s rom addr <=p i x y ( 2 downto 0) M y m i s s i l e y t ( 2 downto 0 ) ; r o m co l <=p i x x ( 2 downto 0) M y m i s s i l e x l ( 2 downto 0 ) ; rom data<=My missile ROM ( t o i n t e g e r ( rom addr ) ) ; r o m bit <=rom data ( t o i n t e g e r ( r o m c o l ) ) ; m i s s i l e p o s t i t i o n process ( M y m i s s i l e x r e g , M y m i s s i l e x l , M y m i s s i l e x r , r e f r t i c k , btn , s t a t e t i c k ) begin M y m i s s i l e x n e x t <=M y m i s s i l e x r e g ; M y m i s s i l e y n e x t <=M y m i s s i l e y r e g ; i f r e f r t i c k = 1 then i f ( M y m i s s i l e s t a t e r e g= 00 ) then 00 makes s u r e i t i s ok M y m i s s i l e y n e x t <=M y m i s s i l e y r e g My missile V ; e l s i f ( M y m i s s i l e s t a t e r e g= 11 or M y m i s s i l e y r e g <20 or S t a t e t i c k = 1 ) then11 s t a n d s f o r i t i s n o t send y e t . M y m i s s i l e x n e x t <=unsig ned ( S p a c e s h i p x )+16; M y m i s s i l e y n e x t <=unsig ned ( S p a c e s h i p y ) ; end i f ; end i f ; end process ; S t a t e t h i n g s process ( btn , M y m i s s i l e s t a t e r e g , S t a t e t i c k ) begin i f ( btn = 0 and M y m i s s i l e s t a t e r e g= 11 ) then M y m i s s i l e s t a t e n e x t <= 00 ; e l s i f ( M y m i s s i l e y r e g <20 or S t a t e t i c k = 1 ) then M y m i s s i l e s t a t e n e x t < =10; e l s i f ( M y m i s s i l e s t a t e r e g =10) th e n M y m i s s i l e s t a t e n e x t <= 11 ; else M y m i s s i l e s t a t e n e x t <=M y m i s s i l e s t a t e r e g ; end i f ; end process ; end B e h a v i o r a l ;

20

5.4

Alien missile

libr ar y IEEE ; use IEEE . STD LOGIC 1164 . ALL ; use IEEE .NUMERIC STD . ALL ; entity A l i e n m i s s i l e i s port ( c l k : in s t d l o g i c ; r e s e t : in s t d l o g i c ; p i x e l x : in s t d l o g i c v e c t o r ( 9 downto 0 ) ; p i x e l y : in s t d l o g i c v e c t o r ( 9 downto 0 ) ; r e f r t i c k : in s t d l o g i c ; A l i e n m i s s i l e x i n : in s t d l o g i c v e c t o r ( 9 downto 0 ) ; A l i e n m i s s i l e y i n : in s t d l o g i c v e c t o r ( 9 downto 0 ) ; A l i e n m i s s i l e x o u t : out s t d l o g i c v e c t o r ( 9 downto 0 ) ; A l i e n m i s s i l e y o u t : out s t d l o g i c v e c t o r ( 9 downto 0 ) ; A l i e n m i s s i l e R : out s t d l o g i c v e c t o r ( 7 downto 0 ) ; A l i e n m i s s i l e G : out s t d l o g i c v e c t o r ( 7 downto 0 ) ; A l i e n m i s s i l e B : out s t d l o g i c v e c t o r ( 7 downto 0 ) ; S p a c e s h i p d o n e : in s t d l o g i c ); end A l i e n m i s s i l e ; architecture B e h a v i o r a l of A l i e n m i s s i l e i s signal A l i e n m i s s i l e x r e g , A l i e n m i s s i l e x n e x t : unsig ned ( 9 downto 0 ) ; signal A l i e n m i s s i l e y r e g , A l i e n m i s s i l e y n e x t : unsig ned ( 9 downto 0 ) ; signal A l i e n m i s s i l e x l , A l i e n m i s s i l e x r : unsig ned ( 9 downto 0 ) ; my m i s s i l e L e f t and r i g h t b o u n d a r i e s . signal A l i e n m i s s i l e y t , A l i e n m i s s i l e y b : unsig ned ( 9 downto 0 ) ; signal x d e l t a r e g , x d e l t a n e x t : unsig ned ( 9 downto 0 ) ; constant A l i e n m i s s i l e V : i n t e g e r : = 1 ; constant A l i e n m i s s i l e h e i g h t : i n t e g e r : = 8 ; constant A l i e n m i s s i l e w i d t h : i n t e g e r : = 8 ; type rom type i s array ( 0 to 7 ) of s t d l o g i c v e c t o r ( 0 to 7 ) ; ROM d e f i n i n t i o n constant Alien missile R O M : rom type := ( 00111100 , 00111100 , 21

00111100 , 00111100 , 11111111 , 01111110 , 00111100 , 00011000 ); signal signal signal signal signal rom addr : unsig ned ( 2 downto 0 ) ; r o m c o l : unsig ned ( 2 downto 0 ) ; rom data : s t d l o g i c v e c t o r ( 7 downto 0 ) ; rom bit : s t d l o g i c ; Alien missile state reg , Alien missile state next : s t d l o g i c v e c t o r ( 1 downto 0 ) ; i s run , 11 u n s t a r t , Alien missile on : std Logic ; pix x , p i x y : unsig ned ( 9 downto 0 ) ; hit : std logic ;

begin Re s e t a l l t h e s t a t e process ( r e s e t , c l k ) begin i f ( r e s e t = 1 ) then A l i e n m i s s i l e x r e g <=unsig ned ( A l i e n m i s s i l e x i n ) ; A l i e n m i s s i l e y r e g <=unsig ned ( A l i e n m i s s i l e y i n ) ; A l i e n m i s s i l e s t a t e r e g <= 00 ; e l s i f ( c l k event and c l k = 1 ) then A l i e n m i s s i l e x r e g <=A l i e n m i s s i l e x n e x t ; A l i e n m i s s i l e Y r e g <=A l i e n m i s s i l e y n e x t ; A l i e n m i s s i l e s t a t e r e g <=A l i e n m i s s i l e s t a t e n e x t ; end i f ; end process ; A l i e n m i s s i l e x o u t <= s t d l o g i c v e c t o r ( A l i e n m i s s i l e x r e g +4); A l i e n m i s s i l e y o u t <= std logic vector ( Alien missile y reg ); g e t t h e u n s i g n e d p i x e l p o s i t i o n pix x <=unsig ned ( p i x e l x ) ; pix y <=unsig ned ( p i x e l y ) ;

00 signal signal signal

22

boundary A l i e n m i s s i l e x l <=A l i e n m i s s i l e x r e g ; A l i e n m i s s i l e x r <=A l i e n m i s s i l e x l +A l i e n m i s s i l e w i d t h 1; A l i e n m i s s i l e y t <=A l i e n m i s s i l e y r e g ; A l i e n m i s s i l e y b <=A l i e n m i s s i l e y t +A l i e n m i s s i l e h e i g h t 1; A l i e n m i s s i l e o n < =1 when ( ( A l i e n m i s s i l e x l <=p i x x ) and ( A l i e n m i s s i l e x r >=p i x x ) and ( A l i e n m i s s i l e y t <=p i x y ) and ( pix y <=A l i e n m i s s i l e y b ) and ( A l i e n m i s s i l e s t a t e r e g= 00 ) ) else 0 ; Get t h e rom a d d r e s s rom addr <=p i x y ( 2 downto 0 ) A l i e n m i s s i l e y t ( 2 downto 0 ) ; r o m co l <=p i x x ( 2 downto 0 ) A l i e n m i s s i l e x l ( 2 downto 0 ) ; rom data<=Alien missile R O M ( t o i n t e g e r ( rom addr ) ) ; r o m bit <=rom data ( t o i n t e g e r ( r o m c o l ) ) ; c o l o r t h i n g s process ( A l i e n m i s s i l e o n , ROM bit , Alien missile state reg ) begin A l i e n m i s s i l e R <= 00000000 ; A l i e n m i s s i l e G <= 00000000 ; A l i e n m i s s i l e B <= 00000000 ; i f ( A l i e n m i s s i l e o n = 1 and ROM bit = 1 ) then A l i e n m i s s i l e R <= 11111111 ; A l i e n m i s s i l e G <= 00000000 ; A l i e n m i s s i l e B <= 00000000 ; end i f ; end process ;

23

m i s s i l e p o s t i t i o n process ( A l i e n m i s s i l e x r e g , A l i e n m i s s i l e x l , Alien missile x r , refr t ick ) begin A l i e n m i s s i l e x n e x t <=A l i e n m i s s i l e x r e g ; A l i e n m i s s i l e y n e x t <=A l i e n m i s s i l e y r e g ; i f r e f r t i c k = 1 then i f ( A l i e n m i s s i l e s t a t e r e g= 00 ) then 00 makes s u r e i t i s ok A l i e n m i s s i l e y n e x t <=A l i e n m i s s i l e y r e g+A l i e n m i s s i l e V ; e l s i f ( A l i e n m i s s i l e s t a t e r e g= 11 or A l i e n m i s s i l e y r e g > 470) then11 s t a n d s f o r i t i s n o t send y e t . A l i e n m i s s i l e x n e x t <=unsig ned ( A l i e n m i s s i l e x i n ) ; A l i e n m i s s i l e y n e x t <=unsig ned ( A l i e n m i s s i l e y i n ) ; e l s i f ( A l i e n m i s s i l e s t a t e r e g= 11 ) then A l i e n m i s s i l e x n e x t <=unsig ned ( A l i e n m i s s i l e x i n ) ; A l i e n m i s s i l e y n e x t <=unsig ned ( A l i e n m i s s i l e y i n ) ; end i f ; end i f ; end process ; S t a t e t h i n g s process ( A l i e n m i s s i l e s t a t e r e g ) begin A l i e n m i s s i l e s t a t e n e x t <=A l i e n m i s s i l e s t a t e r e g ; i f ( A l i e n m i s s i l e s t a t e r e g= 11 ) then A l i e n m i s s i l e s t a t e n e x t <= 00 ; e l s i f ( A l i e n m i s s i l e y r e g >470 or S p a c e s h i p d o n e = 1 ) then or S t a t e t i c k = 1 A l i e n m i s s i l e s t a t e n e x t <= 10 ; e l s i f ( A l i e n m i s s i l e s t a t e r e g= 10 ) then A l i e n m i s s i l e s t a t e n e x t <= 11 ; else A l i e n m i s s i l e s t a t e n e x t <=A l i e n m i s s i l e s t a t e r e g ; end i f ; end process ; end B e h a v i o r a l ;

5.5

Alien

libr ar y IEEE ; 24

use IEEE . STD LOGIC 1164 . ALL ; use IEEE .NUMERIC STD . ALL ; entity A l i e n m i s s i l e i s port ( c l k : in s t d l o g i c ; r e s e t : in s t d l o g i c ; p i x e l x : in s t d l o g i c v e c t o r ( 9 downto 0 ) ; p i x e l y : in s t d l o g i c v e c t o r ( 9 downto 0 ) ; r e f r t i c k : in s t d l o g i c ; A l i e n m i s s i l e x i n : in s t d l o g i c v e c t o r ( 9 downto 0 ) ; A l i e n m i s s i l e y i n : in s t d l o g i c v e c t o r ( 9 downto 0 ) ; A l i e n m i s s i l e x o u t : out s t d l o g i c v e c t o r ( 9 downto 0 ) ; A l i e n m i s s i l e y o u t : out s t d l o g i c v e c t o r ( 9 downto 0 ) ; A l i e n m i s s i l e R : out s t d l o g i c v e c t o r ( 7 downto 0 ) ; A l i e n m i s s i l e G : out s t d l o g i c v e c t o r ( 7 downto 0 ) ; A l i e n m i s s i l e B : out s t d l o g i c v e c t o r ( 7 downto 0 ) ; S p a c e s h i p d o n e : in s t d l o g i c ); end A l i e n m i s s i l e ; architecture B e h a v i o r a l of A l i e n m i s s i l e i s signal A l i e n m i s s i l e x r e g , A l i e n m i s s i l e x n e x t : unsig ned ( 9 downto 0 ) ; signal A l i e n m i s s i l e y r e g , A l i e n m i s s i l e y n e x t : unsig ned ( 9 downto 0 ) ; signal A l i e n m i s s i l e x l , A l i e n m i s s i l e x r : unsig ned ( 9 downto 0 ) ; my m i s s i l e L e f t and r i g h t b o u n d a r i e s . signal A l i e n m i s s i l e y t , A l i e n m i s s i l e y b : unsig ned ( 9 downto 0 ) ; signal x d e l t a r e g , x d e l t a n e x t : unsig ned ( 9 downto 0 ) ; constant A l i e n m i s s i l e V : i n t e g e r : = 1 ; constant A l i e n m i s s i l e h e i g h t : i n t e g e r : = 8 ; constant A l i e n m i s s i l e w i d t h : i n t e g e r : = 8 ; type rom type i s array ( 0 to 7 ) of s t d l o g i c v e c t o r ( 0 to 7 ) ; ROM d e f i n i n t i o n constant Alien missile R O M : rom type := ( 00111100 , 00111100 , 00111100 , 00111100 , 11111111 , 01111110 , 25

00111100 , 00011000 ); signal signal signal signal signal

begin Re s e t a l l t h e s t a t e process ( r e s e t , c l k ) begin i f ( r e s e t = 1 ) then A l i e n m i s s i l e x r e g <=unsig ned ( A l i e n m i s s i l e x i n ) ; A l i e n m i s s i l e y r e g <=unsig ned ( A l i e n m i s s i l e y i n ) ; A l i e n m i s s i l e s t a t e r e g <= 00 ; e l s i f ( c l k event and c l k = 1 ) then A l i e n m i s s i l e x r e g <=A l i e n m i s s i l e x n e x t ; A l i e n m i s s i l e Y r e g <=A l i e n m i s s i l e y n e x t ; A l i e n m i s s i l e s t a t e r e g <=A l i e n m i s s i l e s t a t e n e x t ; end i f ; end process ; A l i e n m i s s i l e x o u t <= s t d l o g i c v e c t o r ( A l i e n m i s s i l e x r e g +4); A l i e n m i s s i l e y o u t <= std logic vector ( Alien missile y reg ); g e t t h e u n s i g n e d p i x e l p o s i t i o n pix x <=unsig ned ( p i x e l x ) ; pix y <=unsig ned ( p i x e l y ) ; boundary A l i e n m i s s i l e x l <=A l i e n m i s s i l e x r e g ; 26

00 signal signal signal

rom addr : unsig ned ( 2 downto 0 ) ; r o m c o l : unsig ned ( 2 downto 0 ) ; rom data : s t d l o g i c v e c t o r ( 7 downto 0 ) ; rom bit : s t d l o g i c ; Alien missile state reg , Alien missile state next : s t d l o g i c v e c t o r ( 1 downto 0 ) ; i s run , 11 u n s t a r t , Alien missile on : std Logic ; pix x , p i x y : unsig ned ( 9 downto 0 ) ; hit : std logic ;

A l i e n m i s s i l e x r <=A l i e n m i s s i l e x l +A l i e n m i s s i l e w i d t h 1; A l i e n m i s s i l e y t <=A l i e n m i s s i l e y r e g ; A l i e n m i s s i l e y b <=A l i e n m i s s i l e y t +A l i e n m i s s i l e h e i g h t 1; A l i e n m i s s i l e o n < =1 when ( ( A l i e n m i s s i l e x l <=p i x x ) and ( A l i e n m i s s i l e x r >=p i x x ) and ( A l i e n m i s s i l e y t <=p i x y ) and ( pix y <=A l i e n m i s s i l e y b ) and ( A l i e n m i s s i l e s t a t e r e g= 00 ) ) else 0 ; Get t h e rom a d d r e s s rom addr <=p i x y ( 2 downto 0 ) A l i e n m i s s i l e y t ( 2 downto 0 ) ; r o m co l <=p i x x ( 2 downto 0 ) A l i e n m i s s i l e x l ( 2 downto 0 ) ; rom data<=Alien missile R O M ( t o i n t e g e r ( rom addr ) ) ; r o m bit <=rom data ( t o i n t e g e r ( r o m c o l ) ) ; c o l o r t h i n g s process ( A l i e n m i s s i l e o n , ROM bit , Alien missile state reg ) begin A l i e n m i s s i l e R <= 00000000 ; A l i e n m i s s i l e G <= 00000000 ; A l i e n m i s s i l e B <= 00000000 ; i f ( A l i e n m i s s i l e o n = 1 and ROM bit = 1 ) then A l i e n m i s s i l e R <= 11111111 ; A l i e n m i s s i l e G <= 00000000 ; A l i e n m i s s i l e B <= 00000000 ; end i f ; end process ; m i s s i l e p o s t i t i o n process ( A l i e n m i s s i l e x r e g , A l i e n m i s s i l e x l , 27

Alien missile x r , refr t ick ) begin A l i e n m i s s i l e x n e x t <=A l i e n m i s s i l e x A l i e n m i s s i l e y n e x t <=A l i e n m i s s i l e y i f r e f r t i c k = 1 then i f ( A l i e n m i s s i l e s t a t e r e g= 00 ) then A l i e n m i s s i l e y n e x t <=A l i e n m i s s i l e y +A l i e n m i s s i l e V ; e l s i f ( A l i e n m i s s i l e s t a t e r e g= 11 or A l i e n m i s s i l e y r e g > 470) then 11 s t a n d s f o r i t i s n o t send y e t . A l i e n m i s s i l e x n e x t <= unsig ned ( A l i e n m i s s i l e x i n ) ; A l i e n m i s s i l e y n e x t <= unsig ned ( A l i e n m i s s i l e y i n ) ; e l s i f ( A l i e n m i s s i l e s t a t e r e g= 11 ) then A l i e n m i s s i l e x n e x t <= unsig ned ( A l i e n m i s s i l e x i n ) ; A l i e n m i s s i l e y n e x t <= unsig ned ( A l i e n m i s s i l e y i n ) ; end i f ; end i f ; end process ; S t a t e t h i n g s process ( A l i e n m i s s i l e s t a t e r e g ) begin A l i e n m i s s i l e s t a t e n e x t <=A l i e n m i s s i l e s t a t e i f ( A l i e n m i s s i l e s t a t e r e g= 11 ) then A l i e n m i s s i l e s t a t e n e x t <= 00 ; e l s i f ( A l i e n m i s s i l e y r e g >470 or S p a c e s h i p d o n e = 1 ) then or S t a t e t i c k = 1 A l i e n m i s s i l e s t a t e n e x t <= 10 ; e l s i f ( A l i e n m i s s i l e s t a t e r e g= 10 ) then A l i e n m i s s i l e s t a t e n e x t <= 11 ; else A l i e n m i s s i l e s t a t e n e x t <=A l i e n m i s s i l e s t a t e end i f ; end process ; end B e h a v i o r a l ;

reg ; reg ;

reg

reg ;

reg ;

28

5.6

Score Display

libr ar y IEEE ; use IEEE . STD LOGIC 1164 . ALL ; use i e e e . n u m e r i c s t d . a l l ; entity t e x t d i s p i s port ( c l k : in s t d l o g i c ; r e s e t : in s t d l o g i c ; p i x e l x : in s t d l o g i c v e c t o r ( 9 downto 0 ) ; p i x e l y : in s t d l o g i c v e c t o r ( 9 downto 0 ) ; Text da t a : in s t d l o g i c v e c t o r ( 7 downto 0 ) ; S t a t e t i c k : in s t d l o g i c ; S p a c e s h i p d o n e : in s t d l o g i c ; Text on : out s t d l o g i c ; Text R : out s t d l o g i c v e c t o r ( 7 downto 0 ) ; Text G : out s t d l o g i c v e c t o r ( 7 downto 0 ) ; Text B : out s t d l o g i c v e c t o r ( 7 downto 0 ) ; Text addr : out s t d l o g i c v e c t o r ( 1 0 downto 0 ) ); end t e x t d i s p ; architecture B e h a v i o r a l of t e x t d i s p i s signal r o w a ddr s , row addr : s t d l o g i c v e c t o r ( 3 downto 0 ) ; signal b i t a d d r s , b i t a d d r : s t d l o g i c v e c t o r ( 2 downto 0 ) ; signal c h a r a d d r s , c h a r a d d r : s t d l o g i c v e c t o r ( 6 downto 0 ) ; signal l i f e , l i f e n e x t : s t d l o g i c v e c t o r ( 1 downto 0 ) ; signal dig0 , dig1 , d i g 0 n e x t , d i g 1 n e x t : s t d l o g i c v e c t o r ( 3 downto 0 ) ; signal S c o r e o n : s t d l o g i c ; signal f o n t b i t : s t d l o g i c ; signal T e x t d a t a b u f f e r : s t d l o g i c v e c t o r ( 7 downto 0 ) ; begin T e x t d a t a b u f f e r <=Text da t a ; Text on < =1 when ( S c o r e o n = 1 ) e l s e 0 ; s c o r e r e g i o n D i s p l a y a l i e n d e s t r o y e d and l i f e on t o p l e f t s c a l e t o 16 32 f o n t S c o r e o n <= 29

when ( p i x e l y ( 9 downto 5)= 01110 and ( t o i n t e g e r ( unsig ned ( p i x e l x ( 9 downto 4 ) ) ) ) < 1 6 )

els

0 ; I t i s t h e a re a ( 0 , 0 ) t o ( 3 1 , 2 5 5 ) r o w a ddr s <=p i x e l y ( 4 downto 1 ) ; we want t h e f o n t 1 6 3 2 . b i t a d d r s <=p i x e l x ( 3 downto 1 ) ; with p i x e l x ( 7 downto 4 ) s e l e c t c h a r a d d r s <= 1000001when 0000 ,1 word ,A, x41 1101100when 0001 ,2 word , l , x6c 1101001when 0010 ,3 word , i , x69 1100101when 0011 ,4 word , e , x65 1101110when 0100 ,5 word , n , x6e 0111010when 0101 ,6 word , : , x3a 011 &d i g 1 when 0110 , d i g i t 10 011 &d i g 0 when 0111 , d i g 1 0000000 when 1000 , 0000000 when 1001 , 1001100when 1010 ,L x4c 1101001when 1011 , i x69 1100110when 1100 , f x66 1100101when 1101 ,e x65 0111010when 1110 , : 01100 & l i f e when others ; process ( r e s e t , d i g 0 n e x t , d i g 1 n e x t ) begin i f r e s e t = 1 then l i f e <= 11 ; dig0 <= 0000 ; dig1 <= 0000 ; else l i f e <=l i f e n e x t ; dig0 <=d i g 0 n e x t ; dig1 <=d i g 1 n e x t ; end i f ; end process ; A l i e n d e s t r o y e d process ( S t a t e t i c k , dig0 , d i g 1 ) begin i f ( S t a t e t i c k event and S t a t e t i c k = 1 ) then i f ( dig0 < 1001 ) then 30

d i g 0 n e x t <=s t d l o g i c v e c t o r ( unsig ned ( d i g 0 ) + 1 ) ; e l s i f ( dig1 < 1001 ) then d i g 0 n e x t <= 0000 ; d i g 1 n e x t <=s t d l o g i c v e c t o r ( unsig ned ( d i g 1 ) + 1 ) ; else d i g 0 n e x t <= 0000 ; d i g 1 n e x t <= 0000 ; end i f ; end i f ; end process ; S p a c e s h i p d e s t r o y e d Process ( S p a c e s h i p d o n e ) begin i f ( S p a c e s h i p d o n e event and S p a c e s h i p d o n e = 1 ) then l i f e n e x t <=s t d l o g i c v e c t o r ( unsig ned ( l i f e ) 1); end i f ; end i f ; end process ; mux f o r f o n t Rom a d d r e s s and r g b process ( s c o r e o n , f o n t b i t , c h a r a d d r s , r o w a ddr s , b i t a d d r s ) begin Text R<= 00000000 ; Text G<= 00000000 ; Text B<= 00000000 ; i f s c o r e o n = 1 then cha r a ddr <=c h a r a d d r s ; row addr <=r o w a d d r s ; b i t a d d r <=b i t a d d r s ; i f f o n t b i t = 1 then Text R<= 11111111 ; Text G<= 11111111 ; Text B<= 11111111 ; else Text R<= 00000000 ; Text G<= 11111111 ; Text B<= 00000000 ; end i f ; e l s i f ( p i x e l y ( 9 downto 5)= 01110 ) then Text R<= 11111111 ; 31

Text G<= 11111111 ; Text B<= 00000000 ; end i f ; end process ; f o n t ROM i n t e r f a c e Text addr <=c h a r a d d r & row addr ; f o n t b i t <=T e x t d a t a b u f f e r ( to integer ( unsig ned ( ( not b i t a d d r ))+1) ); end B e h a v i o r a l ;

5.7

UART logic

libr ar y IEEE ; use IEEE . STD LOGIC 1164 . ALL ; us e IEEE . STD LOGIC ARITH . ALL; us e IEEE . STD LOGIC UNSIGNED .ALL ; use i e e e . n u m e r i c s t d . a l l ; entity u a r t u n i t i s generic ( DBIT : i n t e g e r : = 8 ; # d a ta b i t s number i s 8 SB TICK : i n t e g e r : = 1 6 ; # t i c k s f o r s t o p b i t s , 16/24/32 f o r 1/1.5/2 stop b i t s DVSR: i n t e g e r := 3 2 6 ; baud r a t e d i v i s o r DVSR = 100M/(16 baud r a t e )=100M/ ( 1 6 1 9 2 0 0 ) DVSR BIT : i n t e g e r :=9 # b i t s o f DVSR, so we can g e t 0 512 FIFO W : i n t e g e r :=2 # addr b i t s o f FIFO # words i n FIFO=2FIFO W ); port ( c l k , r e s e t : in s t d l o g i c ; c l o c k and r e s e t s p a c e d o n e : in s t d l o g i c ; a l i e n d o n e : in s t d l o g i c ; rx : in s t d l o g i c ; l i n e i n tx : out s t d l o g i c l i n e o u t s ); end u a r t u n i t ;

32

architecture B e h a v i o r a l of u a r t u n i t i s signal r x d o n e l o g i c , t x d o n e t i c k : s t d l o g i c ; signal send da t a , w r i t e d a t a : s t d l o g i c v e c t o r ( 7 downto 0 ) ; signal t x s t a r t r e g , t x s t a r t n e x t : s t d l o g i c ; type s t a t e t y p e i s ( i n i t , s e n d a l i e n 1 , s e n d a l i e n 2 , s e n d s h i p 1 , s e n d s h i p 2 ) ; signal s t a t e r e g , s t a t e n e x t : s t a t e t y p e ; type twod i s array ( 0 to 1 ) of s t d l o g i c v e c t o r ( 7 downto 0 ) ; signal a l i e n n u m b e r : twod ; type rom type i s array ( 0 to 4 1 ) of s t d l o g i c v e c t o r ( 7 downto 0 ) ; signal dig0 , dig1 , d i g 0 n e x t , d i g 1 n e x t : s t d l o g i c v e c t o r ( 3 downto 0 ) ; constant rom : rom type :=( 01000001 ,A 01101100 , l 01101001 , i 01100101 ,e 01101110 ,n 00100000 , 01100100 ,d 01100101 ,e 01110011 ,s 01110100 , t 01110010 ,r 01101111 ,o 01111001 ,y 01100101 ,e 01100100 ,d 00100000 , 01110100 , t 01101111 ,o 01110100 , t 01100001 ,a 01101100 , l 01010011 ,S 01101000 ,h 01100001 ,a 01110000 ,p 00100000 , 00100000 , 01100100 ,d 01100101 ,e 01110011 ,s 01110100 , t 01110010 ,r 01101111 ,o 33

01111001 ,y 01100101 ,e 01100100 ,d 00100000 , 01110100 , t 01101111 ,o 01110100 , t 01100001 ,a 01101100 l ); signal a d d r s e n d r e g , a d d r s e n d n e x t , a d d r s e n d s u c c : s t d l o g i c v e c t o r ( 5 downt signal r x d o n e t i c k : s t d l o g i c ; begin u a r t u n i t : entity work . u a r t ( s t r a r c h ) generic map (DBIT => DBIT , SB TICK=>SB TICK , DVSR=>DVSR, DVSR BIT=>DVSR BIT) port map ( c l k=>c l k , r e s e t=> r e s e t , rx=>rx , dout=>w r i t e d a t a , din=>send da t a , tx=>tx , t x s t a r t=> t x s t a r t r e g , r x d o n e t i c k=> r x d o n e t i c k , t x d o n e t i c k=> t x d o n e t i c k ) ; process ( r e s e t , c l k ) begin i f ( r e s e t = 1 ) then s t a t e r e g <=i n i t ; a d d r s e n d r e g <=( others = > 0 ); t x s t a r t r e g < = 0 ; dig1 <= 0000 ; dig0 <= 0000 ; e l s i f ( c l k event and c l k = 1 ) then s t a t e r e g <=s t a t e n e x t ; a d d r s e n d r e g <=a d d r s e n d n e x t ; t x s t a r t r e g <=t x s t a r t n e x t ; dig1 <=d i g 1 n e x t ; dig0 <=d i g 0 n e x t ; end i f ; end process ; a l i e n n u m b e r (1) <= 0011 &d i g 1 ; a l i e n n u m b e r (0) <= 0011 &d i g 0 ; 34

a d d r s e n d s u c c <=s t d l o g i c v e c t o r ( unsig ned ( a d d r s e n d r e g ) + 1 ) ; send da t a <=rom ( t o i n t e g e r ( unsig ned ( a d d r s e n d r e g ) ) ) ;

process ( s t a t e r e g , a d d r s e n d r e g ) begin s t a t e n e x t <=s t a t e r e g ; a d d r s e n d n e x t <=a d d r s e n d r e g ; t x s t a r t n e x t <=t x s t a r t r e g ; case s t a t e r e g i s when i n i t=> i f ( a l i e n d o n e = 1 ) then a d d r s e n d n e x t <=( others = > 0 ); s t a t e n e x t <=( s e n d a l i e n 1 ) ; e l s i f ( s p a c e d o n e = 1 ) then a d d r s e n d n e x t <= 010011 ; s t a t e n e x t <=s e n d s h i p 1 ; else s t a t e n e x t <=i n i t ; end i f ; when s e n d a l i e n 1=> i f ( unsig ned ( a d d r s e n d n e x t ) < =20) then t x s t a r t n e x t < = 1 ; s t a t e n e x t <=s e n d a l i e n 2 ; else s t a t e n e x t <=i n i t ; end i f ; when s e n d a l i e n 2=> t x s t a r t n e x t < = 0 ; i f ( t x d o n e t i c k = 1 ) then a d d r s e n d n e x t <=a d d r s e n d s u c c ; s t a t e n e x t <=s e n d a l i e n 1 ; e l s i f ( t x d o n e t i c k = 0 ) then s t a t e n e x t <=s e n d a l i e n 2 ; end i f ; when s e n d s h i p 1=> i f ( unsig ned ( a d d r s e n d n e x t ) < =41) then t x s t a r t n e x t < = 1 ; s t a t e n e x t <=s e n d s h i p 2 ; else s t a t e n e x t <=i n i t ; end i f ; when s e n d s h i p 2=> t x s t a r t n e x t < = 0 ; i f ( t x d o n e t i c k = 1 ) then 35

a d d r s e n d n e x t <=a d d r s e n d s u c c ; s t a t e n e x t <=s e n d s h i p 1 ; e l s i f ( t x d o n e t i c k = 0 ) then s t a t e n e x t <=s e n d s h i p 2 ; end i f ; end case ; end process ; process ( a l i e n d o n e , dig0 , d i g 1 ) begin i f ( a l i e n d o n e event and a l i e n d o n e = 1 ) then i f ( dig0 < 1001 ) then d i g 0 n e x t <=s t d l o g i c v e c t o r ( unsig ned ( d i g 0 ) + 1 ) ; e l s i f ( dig1 < 1001 ) then d i g 0 n e x t <= 0000 ; d i g 1 n e x t <=s t d l o g i c v e c t o r ( unsig ned ( d i g 1 ) + 1 ) ; else d i g 0 n e x t <= 0000 ; d i g 1 n e x t <= 0000 ; end i f ; end i f ; end process ; end B e h a v i o r a l ;

5.8

Trival les

There are some other les such as implementation of the UART, the ROM of the alien and spaceship which are too large and contain little logic information. So we just notify that we need them here, but we are not going to write them as appendix.

36

You might also like