Assignment 1: Tetris in MIPS
Aims
- to give you experience writing MIPS assembly code
- to give you experience translating C to MIPS
- to give you experience with data and control structures in MIPS
Getting Started
Create a new directory for this assignment called tetris
, change to this directory, and fetch the provided code by running these commands:
mkdir -m 700 tetris cd tetris 1521 fetch tetris
If you're not working at CSE, you can download the provided files as a zip file or a tar file.
This will add the following files into the directory:
tetris.s
: a stub MIPS assembly file to complete.tetris.c
: a reference implementation of Tetris in C.tetris.simple.c
: a copy of the reference implementation of Tetris, for you to simplify.input.txt
: example input file.tetris.mk
: a make fragment for compilingtetris.c
.
Tetris: The Game
1521 mipsy tetris.s Welcome to 1521 tetris! /= Field =\ SCORE: 0 | IIII | | | NEXT: J | | | | -- CUT -- | | | | \=========/ > S A new piece has appeared: J /= Field =\ SCORE: 0 | J | | JJJ | NEXT: L | | | | -- CUT -- | | | IIII | \=========/ > q Quitting... Goodbye!
tetris.c
is an implementation of Tetris, a widespread and popular video game.
An example game of Tetris can be seen to the right.
A game of Tetris takes place on a 2D field, where the player must fit together descending shapes to make lines.
You can move a piece left (a) and right (d), drop it down (one step with s or all the way with S), and rotate it (r and R).
Once a piece hits the bottom, another piece will appear at the top of the field.
Any horizontal lines in the field that become completely filled will be cleared, and points will be awarded to the player's score based on how many lines are cleared at the same time.
To get a feel for this game, try it out in a terminal:
dcc tetris.c -o tetris ./tetris
You should read through tetris.c
. There are comments throughout it that should help you understand what the program is doing [citation needed] — which you'll need for the next part of the assignment.
tetris.s: The Assignment
Your task in this assignment is to implement tetris.s
in MIPS assembly.
You have been provided with some assembly and some helpful information in tetris.s
. Read through the provided code carefully, then add MIPS assembly so it executes exactly the same as tetris.c
.
The functions show_debug_info
, game_loop
and read_char
have already been translated to MIPS assembly for you.
You have to implement the following functions in MIPS assembly:
main
rotate_left
move_piece
compute_points_for_line
setup_field
choose_next_shape
print_field
piece_hit_test
piece_intersects_field
rotate_right
place_piece
new_piece
consume_lines
You must translate each function separately to MIPS assembler, following the standard calling conventions used in lectures. When translating a function, you must not make any assumptions about the behaviour or side effects of any other function which is called.
Subsets
This assignment is split into four subsets. Later subsets will involve more complex translation.
Subset | Functions | Performance Weight |
---|---|---|
Subset 0 |
| 35% |
Subset 1 |
| 25% |
Subset 2 |
| 25% |
Subset 3 |
| 15% |
Commands
The game_loop
calls various other functions by handling commands from the player. When translating you should follow the exact behaviour of the C code, however when testing you may find it useful to consult the following table of commands.
Command | Description | Function(s) called |
---|---|---|
r | Rotate the current piece clockwise |
|
R | Rotate the current piece counter-clockwise |
|
n | Remove the current piece and add a new one at the top of the field |
|
s | Move the current piece down one row. |
|
S | Drop the current piece to the bottom of the field |
|
a | Move the current piece to the left by one column |
|
d | Move the current piece to the right by one column |
|
p | Place the current piece into the field |
|
c | Allow the player to choose which piece will drop next |
|
? | Output the current state of the game |
|
q | Quit the game | — |
Running & Testing
To run your MIPS code, simply enter the following in your terminal:
1521 mipsy tetris.s
Once you have finished your translation, to test your implementation, you can compile the provided C implementation, run it to collect the expected output, run your assembly implementation to collect observed output, and then compare them.
The game takes a lot of input, so it's a good idea to write a file with the input you want to test, and then pipe that into your program.
You have been given a file called input.txt
as an example.
dcc tetris.c -o tetris cat input.txt | ./tetris | tee c.out cat input.txt | 1521 mipsy tetris.s | tee mips.out diff -s c.out mips.out Files c.out and mips.out are identical
Try this for different sequences of inputs. When testing some functions you may find using the ?
command (which calls show_debug_info
) to be useful.
Hints
You should implement all the functions from one subset before moving on to the next.
You may find the provided
show_debug_info
andgame_loop
function implementation to be useful guidance for your implementation including comments, label names, indentation and register usage.
Simplified C code
You are encouraged to simplify your C code to remove any loop constructs and if-else statements, and test that your simplified code works correctly before translating it to MIPS, in a separate file tetris.simple.c
.
This file will not be marked - you do not need to submit it.
In order to allow you to check that your simplified code works correctly, we have provided a simple set of automated tests.
You can run these tests by running the following command:
1521 autotest tetris.simple