pokered/engine/math/bcd.asm

215 lines
3.2 KiB
NASM
Raw Normal View History

2017-01-01 01:23:54 +00:00
DivideBCDPredef::
DivideBCDPredef2::
DivideBCDPredef3::
DivideBCDPredef4::
call GetPredefRegisters
DivideBCD::
xor a
ldh [hDivideBCDBuffer], a
ldh [hDivideBCDBuffer+1], a
ldh [hDivideBCDBuffer+2], a
2017-01-01 01:23:54 +00:00
ld d, $1
.mulBy10Loop
; multiply the divisor by 10 until the leading digit is nonzero
; to set up the standard long division algorithm
ldh a, [hDivideBCDDivisor]
2017-01-01 01:23:54 +00:00
and $f0
jr nz, .next
2017-01-01 01:23:54 +00:00
inc d
ldh a, [hDivideBCDDivisor]
2017-01-01 01:23:54 +00:00
swap a
and $f0
ld b, a
ldh a, [hDivideBCDDivisor+1]
2017-01-01 01:23:54 +00:00
swap a
ldh [hDivideBCDDivisor+1], a
2017-01-01 01:23:54 +00:00
and $f
or b
ldh [hDivideBCDDivisor], a
ldh a, [hDivideBCDDivisor+1]
2017-01-01 01:23:54 +00:00
and $f0
ld b, a
ldh a, [hDivideBCDDivisor+2]
2017-01-01 01:23:54 +00:00
swap a
ldh [hDivideBCDDivisor+2], a
2017-01-01 01:23:54 +00:00
and $f
or b
ldh [hDivideBCDDivisor+1], a
ldh a, [hDivideBCDDivisor+2]
2017-01-01 01:23:54 +00:00
and $f0
ldh [hDivideBCDDivisor+2], a
jr .mulBy10Loop
.next
2017-01-01 01:23:54 +00:00
push de
push de
call DivideBCD_getNextDigit
2017-01-01 01:23:54 +00:00
pop de
ld a, b
swap a
and $f0
ldh [hDivideBCDBuffer], a
2017-01-01 01:23:54 +00:00
dec d
jr z, .next2
2017-01-01 01:23:54 +00:00
push de
call DivideBCD_divDivisorBy10
call DivideBCD_getNextDigit
2017-01-01 01:23:54 +00:00
pop de
ldh a, [hDivideBCDBuffer]
2017-01-01 01:23:54 +00:00
or b
ldh [hDivideBCDBuffer], a
2017-01-01 01:23:54 +00:00
dec d
jr z, .next2
2017-01-01 01:23:54 +00:00
push de
call DivideBCD_divDivisorBy10
call DivideBCD_getNextDigit
2017-01-01 01:23:54 +00:00
pop de
ld a, b
swap a
and $f0
ldh [hDivideBCDBuffer+1], a
2017-01-01 01:23:54 +00:00
dec d
jr z, .next2
2017-01-01 01:23:54 +00:00
push de
call DivideBCD_divDivisorBy10
call DivideBCD_getNextDigit
2017-01-01 01:23:54 +00:00
pop de
ldh a, [hDivideBCDBuffer+1]
2017-01-01 01:23:54 +00:00
or b
ldh [hDivideBCDBuffer+1], a
2017-01-01 01:23:54 +00:00
dec d
jr z, .next2
2017-01-01 01:23:54 +00:00
push de
call DivideBCD_divDivisorBy10
call DivideBCD_getNextDigit
2017-01-01 01:23:54 +00:00
pop de
ld a, b
swap a
and $f0
ldh [hDivideBCDBuffer+2], a
2017-01-01 01:23:54 +00:00
dec d
jr z, .next2
2017-01-01 01:23:54 +00:00
push de
call DivideBCD_divDivisorBy10
call DivideBCD_getNextDigit
2017-01-01 01:23:54 +00:00
pop de
ldh a, [hDivideBCDBuffer+2]
2017-01-01 01:23:54 +00:00
or b
ldh [hDivideBCDBuffer+2], a
.next2
ldh a, [hDivideBCDBuffer]
ldh [hDivideBCDQuotient], a ; the same memory location as hDivideBCDDivisor
ldh a, [hDivideBCDBuffer+1]
ldh [hDivideBCDQuotient+1], a
ldh a, [hDivideBCDBuffer+2]
ldh [hDivideBCDQuotient+2], a
2017-01-01 01:23:54 +00:00
pop de
ld a, $6
2017-01-01 01:23:54 +00:00
sub d
and a
ret z
.divResultBy10loop
2017-01-01 01:23:54 +00:00
push af
call DivideBCD_divDivisorBy10
2017-01-01 01:23:54 +00:00
pop af
dec a
jr nz, .divResultBy10loop
2017-01-01 01:23:54 +00:00
ret
DivideBCD_divDivisorBy10:
ldh a, [hDivideBCDDivisor+2]
2017-01-01 01:23:54 +00:00
swap a
and $f
ld b, a
ldh a, [hDivideBCDDivisor+1]
2017-01-01 01:23:54 +00:00
swap a
ldh [hDivideBCDDivisor+1], a
2017-01-01 01:23:54 +00:00
and $f0
or b
ldh [hDivideBCDDivisor+2], a
ldh a, [hDivideBCDDivisor+1]
2017-01-01 01:23:54 +00:00
and $f
ld b, a
ldh a, [hDivideBCDDivisor]
2017-01-01 01:23:54 +00:00
swap a
ldh [hDivideBCDDivisor], a
2017-01-01 01:23:54 +00:00
and $f0
or b
ldh [hDivideBCDDivisor+1], a
ldh a, [hDivideBCDDivisor]
2017-01-01 01:23:54 +00:00
and $f
ldh [hDivideBCDDivisor], a
2017-01-01 01:23:54 +00:00
ret
DivideBCD_getNextDigit:
2017-01-01 01:23:54 +00:00
ld bc, $3
.loop
ld de, hMoney ; the dividend
ld hl, hDivideBCDDivisor
2017-01-01 01:23:54 +00:00
push bc
call StringCmp
pop bc
ret c
inc b
ld de, hMoney+2 ; since SubBCD works starting from the least significant digit
ld hl, hDivideBCDDivisor+2
2017-01-01 01:23:54 +00:00
push bc
call SubBCD
pop bc
jr .loop
2017-01-01 01:23:54 +00:00
AddBCDPredef::
call GetPredefRegisters
AddBCD::
and a
ld b, c
.add
ld a, [de]
adc [hl]
daa
ld [de], a
dec de
dec hl
dec c
jr nz, .add
jr nc, .done
ld a, $99
inc de
.fill
ld [de], a
inc de
dec b
jr nz, .fill
.done
ret
SubBCDPredef::
call GetPredefRegisters
SubBCD::
and a
ld b, c
.sub
ld a, [de]
sbc [hl]
daa
ld [de], a
dec de
dec hl
dec c
jr nz, .sub
jr nc, .done
ld a, $00
inc de
.fill
ld [de], a
inc de
dec b
jr nz, .fill
scf
.done
ret