Lab Snake Game Slides

Introduction to Lab Snake Game
Jos¨ Nelson Amaral

Snake Games
¡ñ Snake games available online
¡ñ Very famous games

Our Snake Game is much simpler
¡ñ We don’t consider the situation when the snake hits its own body. The players just need to avoid this situation while playing this game.
¡ñ After the snake eats the apple, the size of the snake body doesn’t increase.

Two Screens
¡ñ Preparation Screen:
¡ð Reads the level of the game
¡ñ Game Screen:
¡ð Number of points
¡ð Time remaining

浙大学霸代写 加微信 cstutorcs
Random Coordinate
The coordinate (row, col) of the apple is randomly generated.
We need a random number generator.
程序代写 CS代考 加微信: cstutorcs
Linear Congruential Generator

Linear Congruential Generator Example
m=5 X1=((5*4)+13)%5 = 3 X2=((5*6)+13)%5 = ?

Exceptions/Interruptions
¡ñ Enable interrupts for both the timer and the keyboard
¡ñ Create an exception handler

Enable Interrupts
¡ñ Keyboard:
¡ð Keyboard Control – Bit 1 Must be 1 in order for the keyboard to be enabled
¡ö Must be reenabled after every keyboard interrupt
¡ð Keyboard Data: Contains the ASCII character after a key is pressed
¡ð Timer: Contains the current time
¡ð TimeCMP: User-specified value. When matched by the timer an interrupt is generated
¡ñ InterruptionControl:
¡ð Ustatus register: bit 0 must be 1 in order for user interrupts to be allowed
¡ð Uie: Bits 4 and 8 must be 1 in order to enable keyboard and timer
Code Help, Add WeChat: cstutorcs
Exception Handler
The snake.s already contains the Handler Terminate section The common.s file will already have the iTrapData section

Saving Registers
An interrupt handler must save all the registers that it uses.
¡ñ The label iTrapData designates a section of memory allocated for saving registers in the handler.
¡ñ Outside of the handler, uscratch (CSR #64) should contain the address of the iTrapData section.
¡ñ Use the cssrw instruction to swap a register with the uscratch and save all the required registers.

There’s no change
Modified Forever Loop
Check for Change
There’s a change
The game doesn’t end
Execute Functions and Check for End of Game conditions
The game should end