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
You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* 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
Copy file name to clipboardExpand all lines: src/part2/bcd.md
+9-5Lines changed: 9 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -38,17 +38,21 @@ Don't worry about the call to `UpdateScoreBoard`, we'll get into that in a bit.
38
38
```
39
39
40
40
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.
43
45
44
46
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:
45
47
46
48
`%00001001` + `%00000001` = `%00001010` = `$A`
47
49
48
50
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.
50
54
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.
52
56
53
57
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`
54
58
@@ -103,4 +107,4 @@ We then add the `DIGIT_OFFSET` constant to the tens digit to calculate the tile
103
107
104
108
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.
105
109
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!
0 commit comments