-
Notifications
You must be signed in to change notification settings - Fork 1
/
hiscore.asm
355 lines (287 loc) · 12.5 KB
/
hiscore.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
;********************************************************************
;* *
;* HISCORE.Z80 *
;* *
;* Version 1 *
;* *
;* Copyright H. Mulder, 1999 *
;* *
;* *
;* This unit was made to give a ready-to-use solution for Hiscores. *
;* *
;* HiScores (ie: the number of points a player has scored) have a *
;* tendency to be quite large, making calculations and displaying *
;* not easy when operating in a 8-bit environment. On the other *
;* hand, not much is done with a Hiscore: points are added to it, *
;* it needs to be displayed, and some ranking is needed, but that's *
;* about it. *
;* *
;* This unit introduces a special data-type, called a HiScore: *
;* *
;* A HiScore is a set of bytes, whereby each byte represents one *
;* digit in the decimal number, with the highest digit on the *
;* left. If the contained number is smaller than the allocated *
;* space, it is filled up with $80 on the left side. *
;* *
;* Some examples (the size of a HiScore is 8): *
;* *
;* 100 = 80 80 80 80 80 01 00 00 *
;* 30510 = 80 80 80 03 00 05 01 00 *
;* 99999999 = 09 09 09 09 09 09 09 09 *
;* *
;* *
;* Notes: *
;* *
;* The default size of a Hiscore is 8, but this can be changed by *
;* changing the value of HISCORE_LENGTH in HISCORE.INC. It's value *
;* can be between 6 and 255. Note that ALL Hiscores in your program *
;* use this size, and this size alone. *
;* *
;* If a HiScore overflows, it will keep on counting, ignoring the *
;* overflow. so, adding 4 to 99999999 will result in 00000003. *
;* *
;* *
;* Functions: *
;* *
;* ResetHiScore *
;* *
;* Use this function to reset a Hiscore to 0. Always reset a *
;* Hiscore before using it. *
;* *
;* *
;* AddHiScore *
;* *
;* This function will add one Hiscore to another. *
;* *
;* *
;* AddBCHiScore *
;* *
;* This function will add the value of BC to a Hiscore. *
;* *
;* *
;* CpHiScore *
;* *
;* Use this function to compare two Hiscores. *
;* *
;* *
;* Extra info: *
;* *
;* This unit is Public Domain; you can use it in any way you want. *
;* Use this unit at your own peril; I cannot be blamed for any *
;* damage resulting in it's use. *
;* You are free to redistribute it, as long as you do so using the *
;* original package. *
;* *
;* I can be contacted via hpmulder@casema.net. Goto my site at *
;* http://www.casema.net/~hpmulder for more info about Gameboy *
;* programming. *
;* *
;* *
;* History: *
;* *
;* 7 March 1999 Initial release *
;* *
;********************************************************************
INCLUDE "hiscore.inc"
SECTION "HiScore Code", ROM0
ResetHiScore:
;********************************************************
;* *
;* Resets the Hiscore at location HL to 0. Always reset *
;* a HiScore before usage. *
;* *
;* Parameters: *
;* *
;* [hl] = Destination (should be RAM) *
;* *
;* Destroys: *
;* af, bc, hl *
;* *
;********************************************************
;push hl
;push bc
ld c,HISCORE_LENGTH-1
ld a, $80 ; Set to empty
.L1:ld [hl+],a
dec c
jr nz, .L1
xor a ; last should be "0"
ld [hli],a
; Don't protect HL, since in InitStatus we initialize a variable
; trailing a call to ResetHiScore.
;pop bc
;pop hl
ret
AddHiScore:
;***********************************************************
;* *
;* Adds HiScore at location DE to HiScore at location HL. *
;* *
;* Parameters: *
;* *
;* [hl] = Destination (should be RAM) *
;* [de] = Source *
;* *
;* Destroys: *
;* af *
;* *
;***********************************************************
push hl
push de
push bc
ld bc, HISCORE_LENGTH-1
add hl,bc ; set to right-most number
push hl
ld h,d
ld l,e
add hl,bc ; set to right-most number
ld e,l
ld d,h
pop hl
inc c ; c = HISCORE_LENGTH
; b = 0 = no residue
.L1:ld a,[de] ; addition
and $7F ; set empty to 0
call IntHiScore
dec de
dec c
jr nz, .L1
pop bc
pop de
pop hl
ret
AddBCHiScore:
;***********************************************************
;* *
;* Adds value of BC to HiScore at location HL. *
;* *
;* Parameters: *
;* *
;* [hl] = Destination (should be RAM) *
;* bc = Value to add *
;* *
;* Destroys: *
;* af *
;* *
;***********************************************************
push hl
push de
push bc
ld de, HISCORE_LENGTH-1
add hl,de ; set to right-most number
ld de,IntHiScoreTable ; internal bin2dec table
.L1:srl b
rr c ; bc >> 1
jr nc, .L5
; bit is set; add value from table
push hl
push bc
ld bc,$0005 ; b = no addition, c = table-entry length
.L3: ld a,[de]
inc de
call IntHiScore ; add table-entry
dec c
jr nz, .L3
ld c,HISCORE_LENGTH-5
.L4: xor a
call IntHiScore ; add possible residue
dec c
jr nz, .L4
pop bc
pop hl
jr .L2
.L5: ; bit not set, next table entry
inc de
inc de
inc de
inc de
inc de
.L2:ld a,c
or b ; bc = 0 ?
jr nz, .L1
pop bc
pop de
pop hl
ret
CpHiScore:
;*************************************************
;* *
;* Compares HiScore at location DE with HiScore *
;* at location HL. *
;* *
;* Parameters: *
;* *
;* [hl] = Destination *
;* [de] = Source *
;* *
;* Returns: *
;* *
;* CF = [hl] is lower than [de] *
;* NCF = [hl] is equal or higher than [de] *
;* *
;* Destroys: *
;* af *
;* *
;*************************************************
push hl
push de
push bc
ld c, HISCORE_LENGTH
.L1:ld a,[de]
and $7F
inc de
ld b,a ; b = number in Source
ld a,[hl+]
and $7F ; a = number in dest
cp b
;jr c, .L2 ; a < b => [hl] < [de]
jr nz,.L2 ; at least one non-zero, highest 10s place w/ diff #'s is the deciding place
dec c
jr nz, .L1
.L2:
pop bc
pop de
pop hl
ret
;********** INTERNAL SUPPORT ************
IntHiScore:
; [Internal function]
; a = number to add
; b = residue from previous call
; [hl] = number to add to
add b ; add residue
ld b,a
ld a,[hl] ; base
and $7F ; set empty to 0
add b ; add total addition
ld b,0 ; clear residue
jr nz, .L3 ; jump if number <> "0"
ld a,[hl] ; current number empty ?
and $80 ; Yes/No
jr .L2
.L3:cp 10
jr c, .L2 ; jump if number < 10
sub 10 ; correct number
inc b ; setup residue
.L2:ld [hl-],a ; write number
ret
SECTION "HiScore Lookup Table", ROMX
IntHiScoreTable:
; bin2dec lookup table
db 1,0,0,0,0
db 2,0,0,0,0
db 4,0,0,0,0
db 8,0,0,0,0
db 6,1,0,0,0
db 2,3,0,0,0
db 4,6,0,0,0
db 8,2,1,0,0
db 6,5,2,0,0
db 2,1,5,0,0
db 4,2,0,1,0
db 8,4,0,2,0
db 6,9,0,4,0
db 2,9,1,8,0
db 4,8,3,6,1
db 8,6,7,2,3
;* End of HISCORE.Z80 *