-
Notifications
You must be signed in to change notification settings - Fork 0
/
highscore.asm
499 lines (377 loc) · 7.06 KB
/
highscore.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
define HIGH_SCORE_TAB 26 * 8
;
; Displays the high score table.
;
display_high_scores
ld b, 10
ld a, b
dec a
ld (v_row), a
ld ix, high_score_names
ld iy, high_score_table
high_score_names_loop
push bc
push ix
push iy
xor a
ld (v_column), a
ld hl, ix
ld a, %01000110
ld (v_attr), a
call print
ld a, %01000110
ld (v_attr), a
call set_fixed_font
ld a, HIGH_SCORE_TAB
ld (v_column), a
ld hl, iy
call print
call set_proportional_font
pop iy
pop ix
or a
ld bc, 40
ld hl, ix
add hl, bc
ld ix, hl
ld bc, 8
ld hl, iy
add hl, bc
ld iy, hl
pop bc
djnz high_score_names_loop
ret
;
; Checks to see if the current score in v_score
; merits a place in the high score table.
; Displays screen and accepts high score entry
; if required.
;
check_high_score
ld de, v_score
ld hl, high_score_table + (8 * 9)
call compare_high_score
; Return if high score lower or equal to the bottom of
; the table
ret nc
; Copy the score into slot number 10, it belongs there
; at the very least
ld hl, v_score
ld de, high_score_table + (8 * 9)
ld bc, 6
ldir
; And blank out the associated high score name (really just set)
; the termination byte
ld hl, high_score_names + (40 * 9) + 7
xor a
ld (hl), a
ld a, 9
ld (v_high_score_index), a
; Loop to bubble the latest high score to its rightful place
check_high_score_2
xor a
ld h, a
ld a, (v_high_score_index)
ld l, a
or a
add hl, hl
add hl, hl
add hl, hl
ld de, high_score_table
add hl, de
push hl
pop de
; Latest high score is now in DE
ld bc, 8
sub hl, bc
; Current high score - 1 is in HL
push hl
push de
call compare_high_score
pop de
pop hl
jr nc, check_high_score_sort_complete
; Lower high score is bigger (again), swap them
ld b, 6
check_high_score_sort_swap
ld a, (de)
ld c, a
ld a, (hl)
ld (de), a
ld a, c
ld (hl), a
inc hl
inc de
djnz check_high_score_sort_swap
; Now swap the corresponding high score names
xor a
ld h, a
ld a, (v_high_score_index)
ld l, a
push hl
add hl, hl
add hl, hl
add hl, hl
add hl, hl
add hl, hl
ex de, hl
pop hl
add hl, hl
add hl, hl
add hl, hl
add hl, de
ld de, 7
add hl, de
ld de, high_score_names
add hl, de
push hl
ld de, 40
sub hl, de
ex de, hl
pop hl
; HL contains current high score name, DE is high score - 1 name.
; Both offset by 7 bytes to start of string
ld b, 32
check_high_score_name_swap
ld a, (de)
ld c, a
ld a, (hl)
ld (de), a
ld a, c
ld (hl), a
inc hl
inc de
djnz check_high_score_name_swap
ld a, (v_high_score_index)
dec a
ld (v_high_score_index), a
cp 0
jr nz, check_high_score_2
check_high_score_sort_complete
;
; Display the high score screen and allow the user to enter their
; name.
;
ld hl, AY_HIGH_SCORE_TUNE
ld a, 3
call init_music
call set_print_shadow_screen
call cls
call main_menu_logo
call set_proportional_font
ld hl, str_high_score_achieved
call print
call display_high_scores
call set_print_main_screen
call copy_shadow_screen_pixels
call restart_music
call fade_in_attrs
ld a, (v_high_score_index)
add 9
ld (v_row), a
ld a, 20
ld (v_column), a
; Calculate position in high score name table and set IX
; as a pointer
ld a, (v_high_score_index)
ld l, a
push hl
add hl, hl
add hl, hl
add hl, hl
add hl, hl
add hl, hl
ex de, hl
pop hl
add hl, hl
add hl, hl
add hl, hl
add hl, de
ld de, 7
add hl, de
ld de, high_score_names
add hl, de
ld ix, hl
; B = character count. Must not exceed 31.
; C = cursor flash status.
ld c, 0
ld b, 0
ld a, %01000111
ld (v_attr), a
di
ld hl, high_score_isr
ld (v_isr_location), hl
ei
; B' register, bit 0 - debounce required?
exx
ld b, 0
exx
enter_high_score_name
exx
push bc
exx
halt
exx
pop bc
exx
inc c
ld a, c
sra a
sra a
sra a
and 0x3
cp 0
jr z, enter_high_score_cursor_on
cp 1
jr z, enter_high_score_cursor_half
ld a, ' '
jr enter_high_score_name_2
enter_high_score_cursor_on
ld a, 'n'
jr enter_high_score_name_2
enter_high_score_cursor_half
ld a, 'm'
enter_high_score_name_2
push af
call set_fixed_font
pop af
call putchar
call set_proportional_font
enter_high_score_name_2a
ld a, 1
call scan_keys
jr c, enter_high_score_name_2b
; Nothing pressed, set debounce required flag to 0
enter_high_score_name_debounce
exx
res 0, b
exx
jr enter_high_score_name
enter_high_score_name_2b
; Check if debounce is required, ignore this key if so
exx
bit 0, b
exx
jr nz, enter_high_score_name
; Key was pressed, force debounce next time around
exx
set 0, b
exx
cp DELETE
jr nz, enter_high_score_name_3
; Is there anything to delete?
ld a, b
cp 0
jr z, enter_high_score_name
; Clear cursor
call set_fixed_font
ld a, ' '
call putchar
call set_proportional_font
; Reposition pointers
dec ix
dec b
; Work out width of character we're deleting
ld a, (ix)
ld l, a
xor a
ld h, a
ld de, proportional_data
add hl, de
ld a, (hl)
ld d, a
; Subtract width of last character from print position
ld a, (v_column)
sub d
ld (v_column), a
jr enter_high_score_name
enter_high_score_name_3
cp ENTER
jr z, enter_high_score_end
cp BREAK
jr nz, enter_high_score_name_4
ld a, ' '
; If delete or enter isn't pressed, don't
; allow any further key entry
enter_high_score_name_4
ld (v_buffer), a
ld a, b
cp 31
jp z, enter_high_score_name
; Also don't allow any further entry
; if there's physically no room.
ld a, (v_column)
cp HIGH_SCORE_TAB - 32
jp nc, enter_high_score_name
ld a, (v_buffer)
; Store pressed character in IX and print it
ld (ix), a
push af
call putchar
pop af
inc ix
inc b
; Advance the cursor position by the number of
; pixels in the character
ld l, a
xor a
ld h, a
ld de, proportional_data
add hl, de
ld a, (hl)
ld d, a
ld a, (v_column)
add d
ld (v_column), a
jp enter_high_score_name
enter_high_score_end
; Kill the ISR
di
ld hl, 0
ld (v_isr_location), hl
ei
; Terminate the high score string just entered
ld (ix), 0
ld a, b
cp 0
jr nz, enter_high_score_end_2
push ix
pop de
ld hl, str_anonymous_coward
ld bc, str_anonymous_coward_str_end - str_anonymous_coward
ldir
enter_high_score_end_2
call fade_out_attrs
call mute_music
ret
high_score_isr
call move_logo_attrs
ret
;
; Compares the high score pointed to by DE
; against that in HL. If DE is higher, returns
; carry set, else reset.
;
compare_high_score
ld b, 6
compare_high_score_loop
ld a, (de)
cp (hl)
jr c, compare_high_score_not_met
jr z, compare_high_score_loop_next
scf
ret
compare_high_score_loop_next
inc hl
inc de
djnz compare_high_score_loop
compare_high_score_not_met
or a
ret
str_high_score_achieved
defb AT, 7, 60, INK, 6, BRIGHT, 1, "You have a high score!"
defb AT, 21, 24, "Please type in your name and press"
defb AT, 22, 60, "ENTER when complete.", 0
str_anonymous_coward
defb "(Anonymous coward)", 0
str_anonymous_coward_str_end