As usual, the code can be found on github. Warning! The code can be a bit unstable at the moment because the final bits are tweaked to get rid of the minor annoyances that still remain, and I am tweaking all kinds of stuff at this moment.
Here is a screenshot of the old Tetris code, as seen in VisAss in VICE, and the code being ported to kick assembler.
Lots of code follows! :) I decided to not add new all code to this post as it is already a very long one. The complete files can be found on GitHub as I said. I have posted the most interesting bits here and it already is quite a lot.
As Tetris is a score attack game it is essential that progress is saved. A hiscore table is mandatory for this type of game. It's even better if it is saved and re-loaded as well. So let's get to it.
The table itself is very simple. It is a data area, and each entry has a score length of 3 bytes (as we use decimal mode for scoring, we can have a score up to 999999) and then we have a name length of 7. Why? Seven bytes fit in the window that holds the score display. Here is the table definition:
.const TABLE_ENTRIES = 3
.const TABLE_NAMELENGTH = 7
.const TABLE_SCORELENGTH = 3
.const ENTRY_LENGTH = TABLE_NAMELENGTH + TABLE_SCORELENGTH
.fill ENTRY_LENGTH*TABLE_ENTRIES, 0
.fill ENTRY_LENGTH, 0
We add an extra entry at the back of the table to ensure we have a buffer when moving data. This might not be required, but at least it guarantees we do not overwrite any code or data we put behind it. As you might have guessed, this moving entries code might require a rewrite to ensure only valid data is moved. We'll get to that later on, I don't like loose ends. Loose ends in assembly code tend to have consequences!! :)
Initializing the table goes as follows: We go through the data area, and we add a default entry for as often as defined by TABLE_ENTRIES:
// counter for RESET_HISCORES_TABLE and PRINT_HISCORE_TABLE
Printing the table requires quite a few steps. We are re-using the level select screen when printing the hiscore table, so when that is done, we add the table information over the data in that screen. We only need to print the scores and the names, as the level select screen already has an empty table in it with an index.
We set the x and y registers to the correct screen location and call this routine:
// save coordinates
// reset table data offset
ldx #TABLE_SCORELENGTH // this amount of bytes in score
lda hiscore_table_start, y
pha // store value
lsr // shift right 4 times
adc #$30 // add $30 to get a screen code
jsr PRINT // print it
pla // retrieve original value
and #001111 // get rid of leftmost bits
dex // dec number counter
// print the name
// save memory pointer offset
// go one line down
// position cursor
// restore memory pointer offset
// x and y positions for PRINT_HISCORE_TABLE
Game Over And New Hiscore?
Looking back at the code from 23 years ago, I quite liked the approach. Instead of using sorting methods, like bubble sort, an entry comparison is done on all the bytes in each table entry.
Each byte is checked against the new score and the difference is logged in a corresponding flag. This flag is set to $ff if the new score was lower, $00 if it was the same and $01 if it was higher. Depending on the byte checked a new hiscore is detected or rejected. If a new score is detected a new entry is inserted at the current location, else the next entry is tested:
// start with no hiscore
// reset entries counter
// reset table data offset
// reset byte compare flags for this entry
//reset the compare flag counter
// check each byte in the new_score with current entry
// set compare flag accordingly
beq byte_compared // score is same. skip
bpl byte_is_higher // score is higher
dec compare_flags,x // score is lower
iny // inc data counter
inx // inc byte counter
// lets see if new_score was higher than the table entry
// this is fixed to a score length of 3 bytes !!
beq !skip+ // this byte was the same, check next
bpl found_hiscore // higher! :)
jmp no_hiscore // lower :(
beq !skip+ // same, check 3rd byte
bpl found_hiscore // :)
jmp no_hiscore // :(
bmi no_hiscore // last byte is lower. so no new hi
jmp found_hiscore // all 3 digits the same or last higher. new hiscore!
// new_score was lower than this entry.
// check the rest of the entries if not yet all done.
beq all_entries_compared // all entries compared
// so no hiscore at all! exit
// goto start of next table entry
jsr GET_ENTRY_OFFSET // this uses X register.
ldy entry_offset // get offset to beginnig of next entry
jmp entry_compare // do the next entry
// hiscore found and its position is in current_entry
// add the score to the entry
// clear the name. add dots
// mark that a new hiscore has been detected at this entry.
Some data manipulation is required to insert a new entry in the list:
// first we need to move the data down.
// point memory offset to end of table.
ldy #hiscore_table_end - hiscore_table_start
// move data until we're on the wanted offset.
lda hiscore_table_start - ENTRY_LENGTH,y
bpl !loop- // keep going
lda #$01 // insert some values
When this is all done we print the level select screen and we overwrite the level select text with a happy or sad message to indicate a new hiscore or a loser attempt:
.text " a new hiscore!! "
.text " enter your name "
.text " too bad :( "
.text " game over "
Code to enter the name is also added, in the file controlled_input.asm. It uses the kernal routine at $FFE4 (GETIN) to scan for keyboard entry and only the accepted characters are added to the name buffer:
beq !exit+ //input
Load and Save
Saving and loading data on the C64 is relatively easy. It is memory based, so you need to know which memory parts need to be written to the open device. The open device is #8 (the disk drive)
First, we need to open the device and set the file name to load:
.label SETLFS = $ffba
.label SETNAM = $ffbd
.label LOAD = $ffd5
// set logical device
// get length of file name
lda #filename_end - str_filename
.text "filename here"
Then we point to the memory address we want to load the data before calling the load kernal routine:
// set memory destination and load
load_destination must point to hiscore_table_start.
Don't forget to close the file afterwards. We need to do this as we might save several times during one play session.
Saving is done as loading, except we also have to set the end memory address. This is easy, as we labeled it in the source: data_start should point to hiscore_table_start and data_end should point to hiscore_table_end.
// set pointers to the memory block to save
// save up until to the end address
Adding a hiscore to a game is not trivial. It requires quite a bit of code, certainly more than I expected when I started this addition. But it gives the game an important feature and it is appreciated by everyone who plays the game, we can be sure of that.
Next time we will start adding the final touches to the game. We will be adding more robust sound features, some colour options and tweaks to try and finalize the game.
Remember: to view the full code --this post contains only excerpts-- visit my github page linked at the top of this post.
Happy coding and see you later!!