Skip to content

Commit

Permalink
feat: updating the score board
Browse files Browse the repository at this point in the history
  • Loading branch information
hollannikas committed Dec 26, 2024
1 parent cbca29b commit 88294ef
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/part2/bcd.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,22 @@ Let's remember their positions by defining a constant for VRAM location of the 1
```rgbasm,linenos,start={{#line_no_of "" ../../unbricked/bcd/main.asm:score-tile-location}}
{{#include ../../unbricked/bcd/main.asm:score-tile-location}}
```
## Displaying the score

Now we need to write the missing `UpdateScoreBoard` function that will update the score board:

```rgbasm,linenos,start={{#line_no_of "" ../../unbricked/bcd/main.asm:update-score-board}}
{{#include ../../unbricked/bcd/main.asm:update-score-board}}
```

First we load the score (stored in the wScore memory location) into register A. Recall that the score is stored in packed BCD format, where the upper nibble contains the tens digit and the lower nibble contains the ones digit.

The `and %11110000` operation masks the lower nibble (the ones digit) so that only the upper nibble (the tens digit) remains in `A`.

The `rrca` instructions perform a rotate right operation on `A` four times. This effectively shifts the tens digit to the lower nibble, making it ready to map to a digit tile.

We then add the `DIGIT_OFFSET` constant to the tens digit to calculate the tile address for the digit. This address is stored in the `SCORE_TENS` VRAM location, which updates the display to show the tens digit.

Finally, we repeat the process for the ones digit: We mask the tens digit from `A` using `and %00001111`, no need to rotate this time.

Now we can display the score on the screen! We'll need to call `UpdateScoreBoard` after each time the score is updated. We've already done this in the `IncreaseScorePackedBCD` function, so we're all set!
19 changes: 19 additions & 0 deletions unbricked/bcd/main.asm
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,25 @@ IncreaseScorePackedBCD:
ret
; ANCHOR_END: increase-score

; ANCHOR: update-score-board
; Read the packed BCD score from wScore and updates the score display
UpdateScoreBoard:
ld a, [wScore] ; Get the Packed score
and %11110000 ; Mask the lower nibble
rrca ; Move the upper nibble to the lower nibble (divide by 16)
rrca
rrca
rrca
add a, DIGIT_OFFSET ; Offset + add to get the digit tile
ld [SCORE_TENS], a ; Show the digit on screen

ld a, [wScore] ; Get the packed score again
and %00001111 ; Mask the upper nibble
add a, DIGIT_OFFSET ; Offset + add to get the digit tile again
ld [SCORE_ONES], a ; Show the digit on screen
ret
; ANCHOR_END: update-score-board

; ANCHOR: check-for-brick
; Checks if a brick was collided with and breaks it if possible.
; @param hl: address of tile.
Expand Down

0 comments on commit 88294ef

Please sign in to comment.