WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 77618f5

Browse files
authored
Updated IncreaseScorePackedBCD, closes #127 (#128)
* Simplifying IncreaseScorePackedBCD as per #127 * Updated and extended prose to match code adjustments for #127 Also changed a link because that old link was too much text when gbz80(7) explains the same thing in such a beautifully concise way. This allowed explaining the use of ADD 1 instead of INC. * Improved wording to be clearer * Improved wording to be even clearer
1 parent e6e5236 commit 77618f5

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

src/part2/bcd.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,21 @@ Don't worry about the call to `UpdateScoreBoard`, we'll get into that in a bit.
3838
```
3939

4040
Let's have a look at what's going on there:
41-
We set A to 1 and clear the carry flag
42-
We add the score variable (contents of memory location `wScore`) to a, so now A has our increased score.
41+
42+
First we store the score address (`wScore`) in HL. This allows us to use instructions like `LD A, [HL]` and `LD [HL], A`. In total this is 1 M-cycle and 1 byte more efficient than if we provided the address itself when loading and storing the score (`LD A, [wScore]` and `LD [wScore], A` respectively).
43+
44+
Then we load the the score from that address into register A, the only register to be affected by `ADD`. This means that we can now increment it by 1 using exactly that instruction.
4345

4446
So far so good, but what if the score was 9 and we add 1? The processor thinks in binary only and will do the following math:
4547

4648
`%00001001` + `%00000001` = `%00001010` = `$A`
4749

4850
That's a hexadecimal representation of 10, and we need to adjust it to become decimal. `DAA` or "Decimal Adjust after Addition," does just that.
49-
After executing `DAA` our accumulator will be adjusted from `%00001010` to `%00010000`; a 1 in the left nibble and a 0 in the right one. A more detailed article about `DAA` on the Game Boy can be found [here](https://blog.ollien.com/posts/gb-daa/).
51+
After executing `DAA` our accumulator will be adjusted from `%00001010` to `%00010000`; a 1 in the left nibble and a 0 in the right one. `DAA`'s exact behaviour is described [here](https://rgbds.gbdev.io/docs/master/gbz80.7#DAA).
52+
53+
But why do we increment A using `ADD 1` when `INC A` does the same but is more efficient? That's because `DAA` evaluates the carry flag (see the linked description), but unlike `ADD`, `INC` does not affect that flag. So if the carry flag was still set from a previous operation<!-- note: this cannot happen right now, since IncreaseScorePackedBCD is only ever called after CP set the zero flag, which excludes setting the carry flag -->, `DAA` would add 60 points.
5054

51-
Then we store the score back into `wScore` and finally, we call a function that will update the score board, which we will implement next.
55+
Now that score (in A) has been properly incremented, we store it back into `wScore` and finally, we call a function that will update the score board, which we will implement next.
5256

5357
Of course, we still need to call it on impact. To do this, we add a call to `IncreaseScorePackedBCD` after each collision handler (we had a left and a right collision) in `CheckAndHandleBrick`
5458

@@ -103,4 +107,4 @@ We then add the `DIGIT_OFFSET` constant to the tens digit to calculate the tile
103107

104108
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.
105109

106-
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!
110+
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!

unbricked/bcd/main.asm

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,10 @@ IsWallTile:
294294
; Increase score by 1 and store it as a 1 byte packed BCD number
295295
; changes A and HL
296296
IncreaseScorePackedBCD:
297-
xor a ; clear carry flag and a
298-
inc a ; a = 1
299-
ld hl, wScore ; load score
300-
adc [hl] ; add 1
301-
daa ; convert to BCD
297+
ld hl, wScore ; load score address for faster access
298+
ld a, [hl] ; load score to accumulator
299+
add 1
300+
daa ; make sure it's a BCD
302301
ld [hl], a ; store score
303302
call UpdateScoreBoard
304303
ret
@@ -754,4 +753,4 @@ wBallMomentumY: db
754753
; ANCHOR: score-variable
755754
SECTION "Score", WRAM0
756755
wScore: db
757-
; ANCHOR_END: score-variable
756+
; ANCHOR_END: score-variable

0 commit comments

Comments
 (0)