This project involves writing a simplified single-player version of the Snake game in assembly language. The game runs on a Nios II processor and is demonstrated on a Gecko4EPFL board. The purpose of this project is to understand computer architecture concepts by implementing a fun and interactive game.
- Nios II Processor
- nios2sim Simulator
- Gecko4EPFL board
- Quartus and VHDL files
- Assembly code file (
snake.asm)
The Snake game consists of a snake that moves around the screen to eat food while avoiding obstacles. The player's goal is to maximize their score by consuming food. The snake grows longer with each food item consumed. The game ends if the snake hits an obstacle or itself.
Ensure you have the following tools installed:
- Quartus II software
- Nios II EDS
- nios2sim simulator
Your project directory should be structured as follows:
project_root/
├── Nios II (Snake Game)/
│ ├── quartus/
│ │ └── [Quartus project files]
│ └── vhdl/
│ │ └── [VHDL files]
├── demo.MOV
├── NiosII.jar
├── README.md
├── snake.asm
└── snake.pdf
- Assemble the Code: Use the Nios II EDS to assemble the
snake.asmfile.nios2-elf-as snake.asm -o snake.o nios2-elf-ld snake.o -o snake.elf nios2-elf-objcopy -O srec snake.elf snake.srec
- Simulate using nios2sim:
- Open the nios2sim simulator.
- Load the assembled code (
snake.srec) into the simulator. - Test the game functionality.
- Load onto FPGA:
- Open the Quartus project in the
quartus/directory. - Compile the project.
- Program the FPGA with the compiled code.
- Load the assembled code (
snake.srec) into the FPGA using Nios II terminal.
- Running on Gecko4EPFL:
- Connect the Gecko4EPFL board to your system.
- Load the assembled code and start the game on the board.
This NiosII simulator has support for the 5th button. It has been compiled under Java 9. The button view is reindexed from 0--4. For backwards compatibility, the jar file was exported for Java 8, and we have tested it under Java 11 (openjdk) and Java 17 (openjdk).
The game state is stored in specific memory locations:
0x1000to0x100C: Head and tail positions of the snake0x1010: Score0x1014to0x1198: Game State Array (GSA) for LED representation0x1200to0x121C: Checkpoint data0x2000to0x2010: LEDs0x2030: Buttons
The following procedures are implemented in the snake.asm file:
clear_leds: Clears the LED displayset_pixel: Lights up a specific LED pixelget_input: Captures button inputs for controlling the snakemove_snake: Updates the snake's position based on the inputdraw_array: Draws the snake and food on the LED displaycreate_food: Generates new food at a random locationhit_test: Detects collisions with boundaries, food, or the snake itselfdisplay_score: Shows the score on the 7-segment displayinit_game: Initializes the game statesave_checkpoint: Saves the current game state at checkpointsrestore_checkpoint: Restores the game state from the last checkpoint
- Initialization:
- The game is initialized with the snake at the top-left corner and moving rightwards.
- Food is placed at a random location.
- Gameplay Loop:
- The snake moves based on player input.
- Collisions are detected to determine game over or score increments.
- The game state is periodically saved at checkpoints.
- The score is displayed and updated.
- Game Over:
- If the snake collides with itself or the boundary, the game restarts.
- The game can be reset to the last checkpoint using a specific button.
A demonstration video showing the game running on the Gecko4EPFL board is included in this repository (demo.MOV). This video provides a quick overview of the gameplay and functionality.
The game was tested using the nios2sim simulator. The simulator's consistent behavior with the Gecko4EPFL board ensures the reliability of the game. The simulator allows you to:
- Test the game logic
- Verify the LED display output
- Debug and validate procedures
This project was developed as part of a computer architecture course at EPFL. Special thanks to the course instructors and teaching assistants for their guidance, we had fun!