Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pret
GitHub Repository: pret/pokered
Path: blob/master/engine/math/bcd.asm
1271 views
1
; divide hMoney by hDivideBCDDivisor
2
; return output in hDivideBCDQuotient (same as hDivideBCDDivisor)
3
; used only to halve player money upon losing a fight
4
DivideBCDPredef::
5
DivideBCDPredef2::
6
DivideBCDPredef3:: ; only used function
7
DivideBCDPredef4::
8
call GetPredefRegisters
9
10
DivideBCD::
11
xor a
12
ldh [hDivideBCDBuffer], a
13
ldh [hDivideBCDBuffer+1], a
14
ldh [hDivideBCDBuffer+2], a
15
ld d, $1
16
.mulBy10Loop
17
; multiply the divisor by 10 until the leading digit is nonzero
18
; to set up the standard long division algorithm
19
ldh a, [hDivideBCDDivisor]
20
and $f0
21
jr nz, .next
22
inc d
23
ldh a, [hDivideBCDDivisor]
24
swap a
25
and $f0
26
ld b, a
27
ldh a, [hDivideBCDDivisor+1]
28
swap a
29
ldh [hDivideBCDDivisor+1], a
30
and $f
31
or b
32
ldh [hDivideBCDDivisor], a
33
ldh a, [hDivideBCDDivisor+1]
34
and $f0
35
ld b, a
36
ldh a, [hDivideBCDDivisor+2]
37
swap a
38
ldh [hDivideBCDDivisor+2], a
39
and $f
40
or b
41
ldh [hDivideBCDDivisor+1], a
42
ldh a, [hDivideBCDDivisor+2]
43
and $f0
44
ldh [hDivideBCDDivisor+2], a
45
jr .mulBy10Loop
46
47
.next
48
push de
49
push de
50
call DivideBCD_getNextDigit
51
pop de
52
ld a, b
53
swap a
54
and $f0
55
ldh [hDivideBCDBuffer], a
56
dec d
57
jr z, .next2
58
push de
59
call DivideBCD_divDivisorBy10
60
call DivideBCD_getNextDigit
61
pop de
62
ldh a, [hDivideBCDBuffer]
63
or b
64
ldh [hDivideBCDBuffer], a
65
dec d
66
jr z, .next2
67
push de
68
call DivideBCD_divDivisorBy10
69
call DivideBCD_getNextDigit
70
pop de
71
ld a, b
72
swap a
73
and $f0
74
ldh [hDivideBCDBuffer+1], a
75
dec d
76
jr z, .next2
77
push de
78
call DivideBCD_divDivisorBy10
79
call DivideBCD_getNextDigit
80
pop de
81
ldh a, [hDivideBCDBuffer+1]
82
or b
83
ldh [hDivideBCDBuffer+1], a
84
dec d
85
jr z, .next2
86
push de
87
call DivideBCD_divDivisorBy10
88
call DivideBCD_getNextDigit
89
pop de
90
ld a, b
91
swap a
92
and $f0
93
ldh [hDivideBCDBuffer+2], a
94
dec d
95
jr z, .next2
96
push de
97
call DivideBCD_divDivisorBy10
98
call DivideBCD_getNextDigit
99
pop de
100
ldh a, [hDivideBCDBuffer+2]
101
or b
102
ldh [hDivideBCDBuffer+2], a
103
.next2
104
ldh a, [hDivideBCDBuffer]
105
ldh [hDivideBCDQuotient], a ; the same memory location as hDivideBCDDivisor
106
ldh a, [hDivideBCDBuffer+1]
107
ldh [hDivideBCDQuotient+1], a
108
ldh a, [hDivideBCDBuffer+2]
109
ldh [hDivideBCDQuotient+2], a
110
pop de
111
ld a, $6
112
sub d
113
and a
114
ret z
115
.divResultBy10loop
116
push af
117
call DivideBCD_divDivisorBy10
118
pop af
119
dec a
120
jr nz, .divResultBy10loop
121
ret
122
123
DivideBCD_divDivisorBy10:
124
ldh a, [hDivideBCDDivisor+2]
125
swap a
126
and $f
127
ld b, a
128
ldh a, [hDivideBCDDivisor+1]
129
swap a
130
ldh [hDivideBCDDivisor+1], a
131
and $f0
132
or b
133
ldh [hDivideBCDDivisor+2], a
134
ldh a, [hDivideBCDDivisor+1]
135
and $f
136
ld b, a
137
ldh a, [hDivideBCDDivisor]
138
swap a
139
ldh [hDivideBCDDivisor], a
140
and $f0
141
or b
142
ldh [hDivideBCDDivisor+1], a
143
ldh a, [hDivideBCDDivisor]
144
and $f
145
ldh [hDivideBCDDivisor], a
146
ret
147
148
DivideBCD_getNextDigit:
149
ld bc, $3
150
.loop
151
ld de, hMoney ; the dividend
152
ld hl, hDivideBCDDivisor
153
push bc
154
call StringCmp
155
pop bc
156
ret c
157
inc b
158
ld de, hMoney + 2 ; since SubBCD works starting from the least significant digit
159
ld hl, hDivideBCDDivisor + 2
160
push bc
161
call SubBCD
162
pop bc
163
jr .loop
164
165
166
AddBCDPredef::
167
call GetPredefRegisters
168
169
AddBCD::
170
and a
171
ld b, c
172
.add
173
ld a, [de]
174
adc [hl]
175
daa
176
ld [de], a
177
dec de
178
dec hl
179
dec c
180
jr nz, .add
181
jr nc, .done
182
ld a, $99
183
inc de
184
.fill
185
ld [de], a
186
inc de
187
dec b
188
jr nz, .fill
189
.done
190
ret
191
192
193
SubBCDPredef::
194
call GetPredefRegisters
195
196
SubBCD::
197
and a
198
ld b, c
199
.sub
200
ld a, [de]
201
sbc [hl]
202
daa
203
ld [de], a
204
dec de
205
dec hl
206
dec c
207
jr nz, .sub
208
jr nc, .done
209
ld a, $00
210
inc de
211
.fill
212
ld [de], a
213
inc de
214
dec b
215
jr nz, .fill
216
scf
217
.done
218
ret
219
220