summaryrefslogtreecommitdiff
path: root/src/home/write_number.asm
diff options
context:
space:
mode:
authorDaniel Harding <33dannye@gmail.com>2021-09-19 00:21:14 -0500
committerGitHub <noreply@github.com>2021-09-19 00:21:14 -0500
commitdf67aac83b466dadf5f74c881bf84dd9ef19bdfc (patch)
tree47501aced2d256052b8f78bc97328d5af5703add /src/home/write_number.asm
parente4bce9b7ee5e89f8edfd921de2379f0fa06af206 (diff)
parent8dee6b7a11e85d6d4b9f8ec9fb9d53a499fd37dc (diff)
Merge pull request #110 from ElectroDeoxys/master
Split Home bank
Diffstat (limited to 'src/home/write_number.asm')
-rw-r--r--src/home/write_number.asm158
1 files changed, 158 insertions, 0 deletions
diff --git a/src/home/write_number.asm b/src/home/write_number.asm
new file mode 100644
index 0000000..d480e27
--- /dev/null
+++ b/src/home/write_number.asm
@@ -0,0 +1,158 @@
+; converts the two-digit BCD number provided in a to text (ascii) format,
+; writes them to [wStringBuffer] and [wStringBuffer + 1], and to the BGMap0 address at bc
+WriteTwoDigitBCDNumber:
+ push hl
+ push bc
+ push de
+ ld hl, wStringBuffer
+ push hl
+ push bc
+ call WriteBCDNumberInTextFormat
+ pop bc
+ call BCCoordToBGMap0Address
+ pop hl
+ ld b, 2
+ call JPHblankCopyDataHLtoDE
+ pop de
+ pop bc
+ pop hl
+ ret
+
+; converts the one-digit BCD number provided in the lower nybble of a to text
+; (ascii) format, and writes it to [wStringBuffer] and to the BGMap0 address at bc
+WriteOneDigitBCDNumber:
+ push hl
+ push bc
+ push de
+ ld hl, wStringBuffer
+ push hl
+ push bc
+ call WriteBCDDigitInTextFormat
+ pop bc
+ call BCCoordToBGMap0Address
+ pop hl
+ ld b, 1
+ call JPHblankCopyDataHLtoDE
+ pop de
+ pop bc
+ pop hl
+ ret
+
+; converts the four-digit BCD number provided in h and l to text (ascii) format,
+; writes them to [wStringBuffer] through [wStringBuffer + 3], and to the BGMap0 address at bc
+WriteFourDigitBCDNumber:
+ push hl
+ push bc
+ push de
+ ld e, l
+ ld d, h
+ ld hl, wStringBuffer
+ push hl
+ push bc
+ ld a, d
+ call WriteBCDNumberInTextFormat
+ ld a, e
+ call WriteBCDNumberInTextFormat
+ pop bc
+ call BCCoordToBGMap0Address
+ pop hl
+ ld b, 4
+ call JPHblankCopyDataHLtoDE
+ pop de
+ pop bc
+ pop hl
+ ret
+
+; given two BCD digits in the two nybbles of register a,
+; write them in text (ascii) format to hl (most significant nybble first).
+; numbers above 9 end up converted to half-width font tiles.
+WriteBCDNumberInTextFormat:
+ push af
+ swap a
+ call WriteBCDDigitInTextFormat
+ pop af
+; fallthrough
+
+; given a BCD digit in the (lower nybble) of register a, write it in text (ascii)
+; format to hl. numbers above 9 end up converted to half-width font tiles.
+WriteBCDDigitInTextFormat:
+ and $0f
+ add "0"
+ cp "9" + 1
+ jr c, .write_num
+ add $07
+.write_num
+ ld [hli], a
+ ret
+
+; converts the one-byte number at a to text (ascii) format,
+; and writes it to [wStringBuffer] and the BGMap0 address at bc
+WriteOneByteNumber:
+ push bc
+ push hl
+ ld l, a
+ ld h, $00
+ ld de, wStringBuffer
+ push de
+ push bc
+ ld bc, -100
+ call TwoByteNumberToText.get_digit
+ ld bc, -10
+ call TwoByteNumberToText.get_digit
+ ld bc, -1
+ call TwoByteNumberToText.get_digit
+ pop bc
+ call BCCoordToBGMap0Address
+ pop hl
+ ld b, 3
+ call JPHblankCopyDataHLtoDE
+ pop hl
+ pop bc
+ ret
+
+; converts the two-byte number at hl to text (ascii) format,
+; and writes it to [wStringBuffer] and the BGMap0 address at bc
+WriteTwoByteNumber:
+ push bc
+ ld de, wStringBuffer
+ push de
+ call TwoByteNumberToText
+ call BCCoordToBGMap0Address
+ pop hl
+ ld b, 5
+ call JPHblankCopyDataHLtoDE
+ pop bc
+ ret
+
+; convert the number at hl to text (ascii) format and write it to de
+TwoByteNumberToText:
+ push bc
+ ld bc, -10000
+ call .get_digit
+ ld bc, -1000
+ call .get_digit
+ ld bc, -100
+ call .get_digit
+ ld bc, -10
+ call .get_digit
+ ld bc, -1
+ call .get_digit
+ xor a ; TX_END
+ ld [de], a
+ pop bc
+ ret
+.get_digit
+ ld a, "0" - 1
+.subtract_loop
+ inc a
+ add hl, bc
+ jr c, .subtract_loop
+ ld [de], a
+ inc de
+ ld a, l
+ sub c
+ ld l, a
+ ld a, h
+ sbc b
+ ld h, a
+ ret