ref: 2eabbc26a905aec8a1794140fdf72e692749aa43
parent: 274809b723ba6ec1645c972233aaeb3deb30ed62
parent: 39d31cecb263498e286c1fba3b8e0c6d439244b9
author: Sanqui <gsanky@gmail.com>
date: Tue Mar 21 12:35:19 EDT 2017
Merge branch 'label-naming' of https://github.com/joefarebrother/pokered into joefarebrother-label-naming
--- a/engine/add_mon.asm
+++ b/engine/add_mon.asm
@@ -340,13 +340,14 @@
_MoveMon:
ld a, [wMoveMonType]
- and a
+ and a ; BOX_TO_PARTY
jr z, .checkPartyMonSlots
cp DAYCARE_TO_PARTY
jr z, .checkPartyMonSlots
cp PARTY_TO_DAYCARE
ld hl, wDayCareMon
- jr z, .asm_f575
+ jr z, .findMonDataSrc
+ ; else it's PARTY_TO_BOX
ld hl, wNumInBox
ld a, [hl]
cp MONS_PER_BOX
@@ -369,24 +370,26 @@
ld a, [wMoveMonType]
cp DAYCARE_TO_PARTY
ld a, [wDayCareMon]
- jr z, .asm_f556
+ jr z, .copySpecies
ld a, [wcf91]
-.asm_f556
+.copySpecies
ld [hli], a ; write new mon ID
ld [hl], $ff ; write new sentinel
+.findMonDataDest
ld a, [wMoveMonType]
dec a
ld hl, wPartyMons
ld bc, wPartyMon2 - wPartyMon1 ; $2c
ld a, [wPartyCount]
- jr nz, .skipToNewMonEntry
+ jr nz, .addMonOffset
+ ; if it's PARTY_TO_BOX
ld hl, wBoxMons
ld bc, wBoxMon2 - wBoxMon1 ; $21
ld a, [wNumInBox]
-.skipToNewMonEntry
+.addMonOffset
dec a
call AddNTimes
-.asm_f575
+.findMonDataSrc
push hl
ld e, l
ld d, h
@@ -394,16 +397,16 @@
and a
ld hl, wBoxMons
ld bc, wBoxMon2 - wBoxMon1 ; $21
- jr z, .asm_f591
+ jr z, .addMonOffset2
cp DAYCARE_TO_PARTY
ld hl, wDayCareMon
- jr z, .asm_f597
+ jr z, .copyMonData
ld hl, wPartyMons
ld bc, wPartyMon2 - wPartyMon1 ; $2c
-.asm_f591
+.addMonOffset2
ld a, [wWhichPokemon]
call AddNTimes
-.asm_f597
+.copyMonData
push hl
push de
ld bc, wBoxMon2 - wBoxMon1
@@ -411,84 +414,85 @@
pop de
pop hl
ld a, [wMoveMonType]
- and a
- jr z, .asm_f5b4
+ and a ; BOX_TO_PARTY
+ jr z, .findOTdest
cp DAYCARE_TO_PARTY
- jr z, .asm_f5b4
+ jr z, .findOTdest
ld bc, wBoxMon2 - wBoxMon1
add hl, bc
- ld a, [hl]
+ ld a, [hl] ; hl = Level
inc de
inc de
inc de
- ld [de], a
-.asm_f5b4
+ ld [de], a ; de = BoxLevel
+.findOTdest
ld a, [wMoveMonType]
cp PARTY_TO_DAYCARE
ld de, wDayCareMonOT
- jr z, .asm_f5d3
- dec a
+ jr z, .findOTsrc
+ dec a
ld hl, wPartyMonOT
ld a, [wPartyCount]
- jr nz, .asm_f5cd
+ jr nz, .addOToffset
ld hl, wBoxMonOT
ld a, [wNumInBox]
-.asm_f5cd
+.addOToffset
dec a
call SkipFixedLengthTextEntries
ld d, h
ld e, l
-.asm_f5d3
+.findOTsrc
ld hl, wBoxMonOT
ld a, [wMoveMonType]
and a
- jr z, .asm_f5e6
+ jr z, .addOToffset2
ld hl, wDayCareMonOT
cp DAYCARE_TO_PARTY
- jr z, .asm_f5ec
+ jr z, .copyOT
ld hl, wPartyMonOT
-.asm_f5e6
+.addOToffset2
ld a, [wWhichPokemon]
call SkipFixedLengthTextEntries
-.asm_f5ec
+.copyOT
ld bc, NAME_LENGTH
call CopyData
ld a, [wMoveMonType]
+.findNickDest
cp PARTY_TO_DAYCARE
ld de, wDayCareMonName
- jr z, .asm_f611
+ jr z, .findNickSrc
dec a
ld hl, wPartyMonNicks
ld a, [wPartyCount]
- jr nz, .asm_f60b
+ jr nz, .addNickOffset
ld hl, wBoxMonNicks
ld a, [wNumInBox]
-.asm_f60b
+.addNickOffset
dec a
call SkipFixedLengthTextEntries
ld d, h
ld e, l
-.asm_f611
+.findNickSrc
ld hl, wBoxMonNicks
ld a, [wMoveMonType]
and a
- jr z, .asm_f624
+ jr z, .addNickOffset2
ld hl, wDayCareMonName
cp DAYCARE_TO_PARTY
- jr z, .asm_f62a
+ jr z, .copyNick
ld hl, wPartyMonNicks
-.asm_f624
+.addNickOffset2
ld a, [wWhichPokemon]
call SkipFixedLengthTextEntries
-.asm_f62a
+.copyNick
ld bc, NAME_LENGTH
call CopyData
pop hl
ld a, [wMoveMonType]
cp PARTY_TO_BOX
- jr z, .asm_f664
+ jr z, .done
cp PARTY_TO_DAYCARE
- jr z, .asm_f664
+ jr z, .done
push hl
srl a
add $2
@@ -507,6 +511,6 @@
add hl, bc
ld b, $1
call CalcStats
-.asm_f664
+.done
and a
ret
--- a/engine/bcd.asm
+++ b/engine/bcd.asm
@@ -6,155 +6,157 @@
DivideBCD::
xor a
- ld [$ffa5], a
- ld [$ffa6], a
- ld [$ffa7], a
+ ld [hDivideBCDBuffer], a
+ ld [hDivideBCDBuffer+1], a
+ ld [hDivideBCDBuffer+2], a
ld d, $1
-.asm_f72a
- ld a, [$ffa2]
+.mulBy10Loop
+; multiply the divisor by 10 until the leading digit is nonzero
+; to set up the standard long division algorithm
+ ld a, [hDivideBCDDivisor]
and $f0
- jr nz, .asm_f75b
+ jr nz, .next
inc d
- ld a, [$ffa2]
+ ld a, [hDivideBCDDivisor]
swap a
and $f0
ld b, a
- ld a, [$ffa3]
+ ld a, [hDivideBCDDivisor+1]
swap a
- ld [$ffa3], a
+ ld [hDivideBCDDivisor+1], a
and $f
or b
- ld [$ffa2], a
- ld a, [$ffa3]
+ ld [hDivideBCDDivisor], a
+ ld a, [hDivideBCDDivisor+1]
and $f0
ld b, a
- ld a, [$ffa4]
+ ld a, [hDivideBCDDivisor+2]
swap a
- ld [$ffa4], a
+ ld [hDivideBCDDivisor+2], a
and $f
or b
- ld [$ffa3], a
- ld a, [$ffa4]
+ ld [hDivideBCDDivisor+1], a
+ ld a, [hDivideBCDDivisor+2]
and $f0
- ld [$ffa4], a
- jr .asm_f72a
-.asm_f75b
+ ld [hDivideBCDDivisor+2], a
+ jr .mulBy10Loop
+.next
push de
push de
- call DivideBCD_f800
+ call DivideBCD_getNextDigit
pop de
ld a, b
swap a
and $f0
- ld [$ffa5], a
+ ld [hDivideBCDBuffer], a
dec d
- jr z, .asm_f7bc
+ jr z, .next2
push de
- call DivideBCD_f7d7
- call DivideBCD_f800
+ call DivideBCD_divDivisorBy10
+ call DivideBCD_getNextDigit
pop de
- ld a, [$ffa5]
+ ld a, [hDivideBCDBuffer]
or b
- ld [$ffa5], a
+ ld [hDivideBCDBuffer], a
dec d
- jr z, .asm_f7bc
+ jr z, .next2
push de
- call DivideBCD_f7d7
- call DivideBCD_f800
+ call DivideBCD_divDivisorBy10
+ call DivideBCD_getNextDigit
pop de
ld a, b
swap a
and $f0
- ld [$ffa6], a
+ ld [hDivideBCDBuffer+1], a
dec d
- jr z, .asm_f7bc
+ jr z, .next2
push de
- call DivideBCD_f7d7
- call DivideBCD_f800
+ call DivideBCD_divDivisorBy10
+ call DivideBCD_getNextDigit
pop de
- ld a, [$ffa6]
+ ld a, [hDivideBCDBuffer+1]
or b
- ld [$ffa6], a
+ ld [hDivideBCDBuffer+1], a
dec d
- jr z, .asm_f7bc
+ jr z, .next2
push de
- call DivideBCD_f7d7
- call DivideBCD_f800
+ call DivideBCD_divDivisorBy10
+ call DivideBCD_getNextDigit
pop de
ld a, b
swap a
and $f0
- ld [$ffa7], a
+ ld [hDivideBCDBuffer+2], a
dec d
- jr z, .asm_f7bc
+ jr z, .next2
push de
- call DivideBCD_f7d7
- call DivideBCD_f800
+ call DivideBCD_divDivisorBy10
+ call DivideBCD_getNextDigit
pop de
- ld a, [$ffa7]
+ ld a, [hDivideBCDBuffer+2]
or b
- ld [$ffa7], a
-.asm_f7bc
- ld a, [$ffa5]
- ld [$ffa2], a
- ld a, [$ffa6]
- ld [$ffa3], a
- ld a, [$ffa7]
- ld [$ffa4], a
+ ld [hDivideBCDBuffer+2], a
+.next2
+ ld a, [hDivideBCDBuffer]
+ ld [hDivideBCDQuotient], a ; the same memory location as hDivideBCDDivisor
+ ld a, [hDivideBCDBuffer+1]
+ ld [hDivideBCDQuotient+1], a
+ ld a, [hDivideBCDBuffer+2]
+ ld [hDivideBCDQuotient+2], a
pop de
- ld a, $6
+ ld a, $6
sub d
and a
ret z
-.asm_f7ce
+.divResultBy10loop
push af
- call DivideBCD_f7d7
+ call DivideBCD_divDivisorBy10
pop af
dec a
- jr nz, .asm_f7ce
+ jr nz, .divResultBy10loop
ret
-DivideBCD_f7d7:
- ld a, [$ffa4]
+DivideBCD_divDivisorBy10:
+ ld a, [hDivideBCDDivisor+2]
swap a
and $f
ld b, a
- ld a, [$ffa3]
+ ld a, [hDivideBCDDivisor+1]
swap a
- ld [$ffa3], a
+ ld [hDivideBCDDivisor+1], a
and $f0
or b
- ld [$ffa4], a
- ld a, [$ffa3]
+ ld [hDivideBCDDivisor+2], a
+ ld a, [hDivideBCDDivisor+1]
and $f
ld b, a
- ld a, [$ffa2]
+ ld a, [hDivideBCDDivisor]
swap a
- ld [$ffa2], a
+ ld [hDivideBCDDivisor], a
and $f0
or b
- ld [$ffa3], a
- ld a, [$ffa2]
+ ld [hDivideBCDDivisor+1], a
+ ld a, [hDivideBCDDivisor]
and $f
- ld [$ffa2], a
+ ld [hDivideBCDDivisor], a
ret
-DivideBCD_f800:
+DivideBCD_getNextDigit:
ld bc, $3
-.asm_f803
- ld de, $ff9f
- ld hl, $ffa2
+.loop
+ ld de, hMoney ; the dividend
+ ld hl, hDivideBCDDivisor
push bc
call StringCmp
pop bc
ret c
inc b
- ld de, $ffa1
- ld hl, $ffa4
+ ld de, hMoney+2 ; since SubBCD works starting from the least significant digit
+ ld hl, hDivideBCDDivisor+2
push bc
call SubBCD
pop bc
- jr .asm_f803
+ jr .loop
AddBCDPredef::
--- a/hram.asm
+++ b/hram.asm
@@ -155,6 +155,7 @@
hDivideBCDDivisor EQU $FFA2 ; 3-byte BCD number
hDivideBCDQuotient EQU $FFA2 ; 3-byte BCD number
+hDivideBCDBuffer EQU $FFA5 ; 3-byte BCD number
hSerialReceivedNewData EQU $FFA9