**ECSE 222: Digital Logic - Lab 4
Space Invaders!** *Fall 2018* - [McGill University](http://www.mcgill.ca) - [Electrical & Computer Engineering](http://www.ece.mcgill.ca) Prof. [Derek Nowrouzezahrai](http://www.cim.mcgill.ca/~derek), derek@cim.mcgill.ca *TAs responsible for this assignment: * [Alex](zixuan.yin@mail.mcgill.ca) and [Arash](arash.ardakani@mail.mcgill.ca) __Due Date:__ The week of November 26 during your lab section __Guidelines:__ - Carefully and thoroughly read this lab document - Follow the instructions to complete the lab - Notify the TA when you're ready to be evaluated - While this work is conducted in groups, each member is expected to fully understand every component of the solution!

Note:

If Quartus freezes at roughly 10% completion during compilation, check the location of your project and VHDL files: storing files on a network drive (e.g., on the campus.mcgill.ca domain) instead of a local drive (e.g., C:\) will cause this issue.

# Overview In this lab you will implement the logic behind a _Space Invaders_ game clone. If you have never played Space Invaders, we suggest taking a bit of time to play [an online version](http://www.tripletsandus.com/80s/80s_games/html5_SpaceInvaders.htm) in order to form an idea of what your final implementation should look like. Do keep in mind, however, that the version you will implement will be simplified compared to the original game. This lab has __no instructional videos__: instead, we will expect you to use (and build atop) the knowledge you've acquired from the previous labs in order to complete the game spec. Game Rules ---------- Our Space Invaders clone will consist of only one level. In this level, a grid of aliens will move back-and-forth across and down the screen, making their way towards the bottom. A space ship at the bottom of the screen, controlled by the player, can move left and right (based on the user's input). The ship can also fire missiles that can destroy the aliens. The game ends when either all of the aliens have been destroyed, or when the aliens reach the bottom of the screen. Furthermore, there is a scoring system that assigns 1 point to the player for every alien they destroy. Game Requirements ------------------ Here are the game behaviors that we will be looking for during your evaluation: - At the start of the level, the grid of aliens must begin at the top of the screen - Aliens must move across the screen, starting from left to right (until they hit the right "wall") and then from right to left (until they hit the left "wall") - Aliens move one row down each time they reach the edge of the screen (i.e., a "wall") - The player's ship moves left when the left paddle button is pressed - The player's ship moves right when the right paddle button is pressed - The player's ship fires a missile when the missile button is pressed - The missile moves vertically, starting from the ship's position at the moment of the missile's deployment - If a missile collides with an alien, the alien instantaneously vaporizes (i.e., it disappears) - If a missile collides with an alien, the player's score increases by one - The player cannot launch another missile until the first one either: hits an alien, or misses all the aliens and hits the top "wall" - Pressing the reset button resets the game - The game ends if the aliens reach the bottom of the screen - The game ends if the ship destroys all the aliens # Instructions This lab provides source code (see Step 1). Download and unzip the sources onto your computer. These source listings provide facilities to draw objects onto the screen. One of your tasks will be to animate these objects according to the aforementioned game logic. Step 1 ------- Create a project and add the source files to your project. You can find the source files here. Step 2 ------ Create a PLL clock source called `vga_clk` (if necessary, please refer to the relevant Lab 3 instruction video for help). The clock should output at 25 MHz. The `.bdf` file already has the correct symbol for `vga_clk` (which is why you _must_ name it `vga_clk`) connecting it to the circuit. Step 3 ------- Synthesize and analyze the project after setting the given `.bdf` file as your top level entity. You should now be able to open the pin planner and assign pins. Set the input paddles to the push buttons (note that the signals are _active low_). Map the reset to one of the FPGA's slide switches. Lastly, map the VGA outputs as you did in Lab 3. Fully compile and load the configuration onto your FPGA board. Verify that you see a blank screen with a score of zero and an orange bar at the bottom. This will indicate that you have successfully synthesized the starter source code! Step 4 ------- Open the `spriteROM.vhd` file and manually fill in the ROM data to draw a sprite[^footnote1] for the aliens and the player's spaceship. We have included comments to indicate which part of the ROM you need to modify to accomplish this. Step 5 ------- Open the `game_controller.vhd` file and implement a state machine that realizes the game logic as described earlier in this document. We have provided significant guidance, in the form of VHDL code comments, to help you work through this task. Create a test bench to test whether your implementation correctly meets the game logic spec. __HINT__: In the test bench, instantiate only the game controller and feed it the signals you need to change the state. Here's an example of how you can change the internal variables of your instance: `game_controller_instance.alien.x_pos = 500;` You need only use a timing diagram to illustrate the state transitions[^footnote2]. Step 6 ------- Implement the missing game logic behavior in the `update` process in `game_controller.vhd`. The game behaviors are described in the _Game Requirements_ Section. You'll notice that the entry condition for the update loop occurs when the `clk` is high, the state is `gameplay` and `slow_clk` is high. We have two clocks in this design since the faster clock (`clk`) is fed to the drawing portion of the code (given in the starter source) and the slower clock (`slow_clk`) controls the logic. This allow the screen to be updated at a much faster rate (i.e., 60 Hz), while the sprite movement is slowed down to the `slow_clk` rate. Grading -------- Once you have completed your lab, demo your project to a TA. You will be expected to: - explain how the HDL code works, - demonstrate the game working according to the specifications, and - present and discuss the timing dragrams from your test bench. # Evaluation Rubrick: - __[3 pts]__ Explain the technical details of your design - __[6 pts]__ Clearly demonstrate that your game implements all the necessary requirements listed above - __[1 pts]__ Illustrate a correct implementation of your game sprites - __[1 pts]__ Explain the details and output of your test bench - __[3 pts]__ Illustrate the timing diagrams for your game controller test bench - __[1 pts]__ Code comprehension: you will be asked a question pertaining to the _drawing sequence implementation_ [^footnote1]: For those of you who are interested, this is an opportunity to be artistic in your design of the alien and spaceship sprites. Please exercise common sense to ensure that any artistic choices are made in good taste. [^footnote2]: This is roughly the half-way mark for the lab.