/
home.asm
12002 lines (11406 loc) · 231 KB
/
home.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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; rst vectors
SECTION "rst00", ROM0
ret
SECTION "rst08", ROM0
ret
SECTION "rst10", ROM0
ret
SECTION "rst18", ROM0
jp Bank1Call
SECTION "rst20", ROM0
jp RST20
SECTION "rst28", ROM0
jp FarCall
SECTION "rst30", ROM0
ret
SECTION "rst38", ROM0
ret
; interrupts
SECTION "vblank", ROM0
jp VBlankHandler
SECTION "lcdc", ROM0
call wLCDCFunctionTrampoline
reti
SECTION "timer", ROM0
jp TimerHandler
SECTION "serial", ROM0
jp SerialHandler
SECTION "joypad", ROM0
reti
SECTION "romheader", ROM0
nop
jp Start
SECTION "start", ROM0
Start: ; 0150 (0:0150)
di
ld sp, $fffe
push af
xor a
ld [rIF], a
ld [rIE], a
call ZeroRAM
ld a, $1
call BankswitchROM
xor a
call BankswitchSRAM
call BankswitchVRAM0
call DisableLCD
pop af
ld [wInitialA], a
call DetectConsole
ld a, $20
ld [wTileMapFill], a
call SetupVRAM
call SetupRegisters
call SetupPalettes
call SetupSound
call SetupTimer
call ResetSerial
call CopyDMAFunction
call ValidateSRAM
ld a, BANK(GameLoop)
call BankswitchROM
ld sp, $e000
jp GameLoop
; vblank interrupt handler
VBlankHandler: ; 019b (0:019b)
push af
push bc
push de
push hl
ldh a, [hBankROM]
push af
ld hl, wReentrancyFlag
bit IN_VBLANK, [hl]
jr nz, .done
set IN_VBLANK, [hl]
ld a, [wVBlankOAMCopyToggle]
or a
jr z, .no_oam_copy
call hDMAFunction ; DMA-copy $ca00-$ca9f to OAM memory
xor a
ld [wVBlankOAMCopyToggle], a
.no_oam_copy
; flush scaling/windowing parameters
ldh a, [hSCX]
ld [rSCX], a
ldh a, [hSCY]
ld [rSCY], a
ldh a, [hWX]
ld [rWX], a
ldh a, [hWY]
ld [rWY], a
; flush LCDC
ld a, [wLCDC]
ld [rLCDC], a
ei
call wVBlankFunctionTrampoline
call FlushPalettesIfRequested
ld hl, wVBlankCounter
inc [hl]
ld hl, wReentrancyFlag
res IN_VBLANK, [hl]
.done
pop af
call BankswitchROM
pop hl
pop de
pop bc
pop af
reti
; timer interrupt handler
TimerHandler: ; 01e6 (0:01e6)
push af
push hl
push de
push bc
ei
call SerialTimerHandler
; only trigger every fourth interrupt ≈ 60.24 Hz
ld hl, wTimerCounter
ld a, [hl]
inc [hl]
and $3
jr nz, .done
; increment the 60-60-60-255-255 counter
call IncrementPlayTimeCounter
; check in-timer flag
ld hl, wReentrancyFlag
bit IN_TIMER, [hl]
jr nz, .done
set IN_TIMER, [hl]
ldh a, [hBankROM]
push af
ld a, BANK(SoundTimerHandler)
call BankswitchROM
call SoundTimerHandler
pop af
call BankswitchROM
; clear in-timer flag
ld hl, wReentrancyFlag
res IN_TIMER, [hl]
.done
pop bc
pop de
pop hl
pop af
reti
; increment play time counter by a tick
IncrementPlayTimeCounter: ; 021c (0:021c)
ld a, [wPlayTimeCounterEnable]
or a
ret z
ld hl, wPlayTimeCounter
inc [hl]
ld a, [hl]
cp 60
ret c
ld [hl], $0
inc hl
inc [hl]
ld a, [hl]
cp 60
ret c
ld [hl], $0
inc hl
inc [hl]
ld a, [hl]
cp 60
ret c
ld [hl], $0
inc hl
inc [hl]
ret nz
inc hl
inc [hl]
ret
; setup timer to 16384/68 ≈ 240.94 Hz
SetupTimer: ; 0241 (0:0241)
ld b, -68 ; Value for Normal Speed
call CheckForCGB
jr c, .set_timer
ld a, [rKEY1]
and $80
jr z, .set_timer
ld b, $100 - 2 * 68 ; Value for CGB Double Speed
.set_timer
ld a, b
ld [rTMA], a
ld a, TAC_16384_HZ
ld [rTAC], a
ld a, TAC_START | TAC_16384_HZ
ld [rTAC], a
ret
; return carry if not CGB
CheckForCGB: ; 025c (0:025c)
ld a, [wConsole]
cp CONSOLE_CGB
ret z
scf
ret
; wait for VBlankHandler to finish unless lcd is off
WaitForVBlank: ; 0264 (0:0264)
push hl
ld a, [wLCDC]
bit LCDC_ENABLE_F, a
jr z, .lcd_off
ld hl, wVBlankCounter
ld a, [hl]
.wait_vblank
halt
nop
cp [hl]
jr z, .wait_vblank
.lcd_off
pop hl
ret
; turn LCD on
EnableLCD: ; 0277 (0:0277)
ld a, [wLCDC] ;
bit LCDC_ENABLE_F, a ;
ret nz ; assert that LCD is off
or LCDC_ON ;
ld [wLCDC], a ;
ld [rLCDC], a ; turn LCD on
ld a, FLUSH_ALL_PALS
ld [wFlushPaletteFlags], a
ret
; wait for vblank, then turn LCD off
DisableLCD: ; 028a (0:028a)
ld a, [rLCDC] ;
bit LCDC_ENABLE_F, a ;
ret z ; assert that LCD is on
ld a, [rIE]
ld [wIE], a
res INT_VBLANK, a ;
ld [rIE], a ; disable vblank interrupt
.wait_vblank
ld a, [rLY] ;
cp LY_VBLANK ;
jr nz, .wait_vblank ; wait for vblank
ld a, [rLCDC] ;
and LCDC_OFF ;
ld [rLCDC], a ;
ld a, [wLCDC] ;
and LCDC_OFF ;
ld [wLCDC], a ; turn LCD off
xor a
ld [rBGP], a
ld [rOBP0], a
ld [rOBP1], a
ld a, [wIE]
ld [rIE], a
ret
; set OBJ size: 8x8
Set_OBJ_8x8: ; 02b9 (0:02b9)
ld a, [wLCDC]
and LCDC_OBJ8
ld [wLCDC], a
ret
; set OBJ size: 8x16
Set_OBJ_8x16: ; 02c2 (0:02c2)
ld a, [wLCDC]
or LCDC_OBJ16
ld [wLCDC], a
ret
; set Window Display on
Set_WD_on: ; 02cb (0:02cb)
ld a, [wLCDC]
or LCDC_WINON
ld [wLCDC], a
ret
; set Window Display off
Set_WD_off: ; 02d4 (0:02d4)
ld a, [wLCDC]
and LCDC_WINOFF
ld [wLCDC], a
ret
; enable timer interrupt
EnableInt_Timer: ; 02dd (0:02dd)
ld a, [rIE]
or 1 << INT_TIMER
ld [rIE], a
ret
; enable vblank interrupt
EnableInt_VBlank: ; 02e4 (0:02e4)
ld a, [rIE]
or 1 << INT_VBLANK
ld [rIE], a
ret
; enable lcdc interrupt on hblank mode
EnableInt_HBlank: ; 02eb (0:02eb)
ld a, [rSTAT]
or 1 << STAT_MODE_HBLANK
ld [rSTAT], a
xor a
ld [rIF], a
ld a, [rIE]
or 1 << INT_LCD_STAT
ld [rIE], a
ret
; disable lcdc interrupt and the hblank mode trigger
DisableInt_HBlank: ; 02fb (0:02fb)
ld a, [rSTAT]
and ~(1 << STAT_MODE_HBLANK)
ld [rSTAT], a
xor a
ld [rIF], a
ld a, [rIE]
and ~(1 << INT_LCD_STAT)
ld [rIE], a
ret
; initialize scroll, window, and lcdc registers, set trampoline functions
; for the lcdc and vblank interrupts, latch clock data, and enable SRAM/RTC
SetupRegisters: ; 030b (0:030b)
xor a
ld [rSCY], a
ld [rSCX], a
ld [rWY], a
ld [rWX], a
ld [wcab0], a
ld [wcab1], a
ld [wcab2], a
ldh [hSCX], a
ldh [hSCY], a
ldh [hWX], a
ldh [hWY], a
xor a
ld [wReentrancyFlag], a
ld a, $c3 ; $c3 = jp nn
ld [wLCDCFunctionTrampoline], a
ld [wVBlankFunctionTrampoline], a
ld hl, wVBlankFunctionTrampoline + 1
ld [hl], LOW(NoOp) ;
inc hl ; load `jp NoOp`
ld [hl], HIGH(NoOp) ;
ld a, LCDC_BGON | LCDC_OBJON | LCDC_OBJ16 | LCDC_WIN9C00
ld [wLCDC], a
ld a, $1
ld [MBC3LatchClock], a
ld a, SRAM_ENABLE
ld [MBC3SRamEnable], a
NoOp: ; 0348 (0:0348)
ret
; sets wConsole and, if CGB, selects WRAM bank 1 and switches to double speed mode
DetectConsole: ; 0349 (0:0349)
ld b, CONSOLE_CGB
cp GBC
jr z, .got_console
call DetectSGB
ld b, CONSOLE_DMG
jr nc, .got_console
call InitSGB
ld b, CONSOLE_SGB
.got_console
ld a, b
ld [wConsole], a
cp CONSOLE_CGB
ret nz
ld a, $01
ld [rSVBK], a
call SwitchToCGBDoubleSpeed
ret
; initialize the palettes (both monochrome and color)
SetupPalettes: ; 036a (0:036a)
ld hl, wBGP
ld a, %11100100
ld [rBGP], a
ld [hli], a ; wBGP
ld [rOBP0], a
ld [rOBP1], a
ld [hli], a ; wOBP0
ld [hl], a ; wOBP1
xor a
ld [wFlushPaletteFlags], a
ld a, [wConsole]
cp CONSOLE_CGB
ret nz
ld de, wBackgroundPalettesCGB
ld c, 16
.copy_pals_loop
ld hl, InitialPalette
ld b, CGB_PAL_SIZE
.copy_bytes_loop
ld a, [hli]
ld [de], a
inc de
dec b
jr nz, .copy_bytes_loop
dec c
jr nz, .copy_pals_loop
call FlushAllCGBPalettes
ret
InitialPalette: ; 0399 (0:0399)
rgb 28,28,24
rgb 21,21,16
rgb 10,10,08
rgb 00,00,00
; clear VRAM tile data ([wTileMapFill] should be an empty tile)
SetupVRAM: ; 03a1 (0:03a1)
call FillTileMap
call CheckForCGB
jr c, .vram0
call BankswitchVRAM1
call .vram0
call BankswitchVRAM0
.vram0
ld hl, v0Tiles0
ld bc, v0BGMap0 - v0Tiles0
.loop
xor a
ld [hli], a
dec bc
ld a, b
or c
jr nz, .loop
ret
; fill VRAM0 BG map 0 with [wTileMapFill] and VRAM1 BG map 0 with $00
FillTileMap: ; 03c0 (0:03c0)
call BankswitchVRAM0
ld hl, v0BGMap0
ld bc, v0BGMap1 - v0BGMap0
.vram0_loop
ld a, [wTileMapFill]
ld [hli], a
dec bc
ld a, c
or b
jr nz, .vram0_loop
ld a, [wConsole]
cp CONSOLE_CGB
ret nz
call BankswitchVRAM1
ld hl, v1BGMap0
ld bc, v1BGMap1 - v1BGMap0
.vram1_loop
xor a
ld [hli], a
dec bc
ld a, c
or b
jr nz, .vram1_loop
call BankswitchVRAM0
ret
; zero work RAM, stack area, and high RAM ($C000-$DFFF, $FF80-$FFEF)
ZeroRAM: ; 03ec (0:03ec)
ld hl, $c000
ld bc, $e000 - $c000
.zero_wram_loop
xor a
ld [hli], a
dec bc
ld a, c
or b
jr nz, .zero_wram_loop
ld c, LOW($ff80)
ld b, $fff0 - $ff80
xor a
.zero_hram_loop
ld [$ff00+c], a
inc c
dec b
jr nz, .zero_hram_loop
ret
; Flush all non-CGB and CGB palettes
FlushAllPalettes: ; 0404 (0:0404)
ld a, FLUSH_ALL_PALS
jr FlushPalettes
; Flush non-CGB palettes and a single CGB palette,
; provided in a as an index between 0-7 (BGP) or 8-15 (OBP)
FlushPalette: ; 0408 (0:0408)
or FLUSH_ONE_PAL
jr FlushPalettes
; Set wBGP to the specified value, flush non-CGB palettes, and the first CGB palette.
SetBGP: ; 040c (0:040c)
ld [wBGP], a
; fallthrough
; Flush non-CGB palettes and the first CGB palette
FlushPalette0:
ld a, FLUSH_ONE_PAL
; fallthrough
FlushPalettes:
ld [wFlushPaletteFlags], a
ld a, [wLCDC]
rla
ret c
push hl
push de
push bc
call FlushPalettesIfRequested
pop bc
pop de
pop hl
ret
; Set wOBP0 to the specified value, flush non-CGB palettes, and the first CGB palette.
SetOBP0: ; 0423 (0:0423)
ld [wOBP0], a
jr FlushPalette0
; Set wOBP1 to the specified value, flush non-CGB palettes, and the first CGB palette.
SetOBP1: ; 0428 (0:0428)
ld [wOBP1], a
jr FlushPalette0
; Flushes non-CGB palettes from [wBGP], [wOBP0], [wOBP1] as well as CGB
; palettes from [wBackgroundPalettesCGB..wBackgroundPalettesCGB+$3f] (BG palette)
; and [wObjectPalettesCGB+$00..wObjectPalettesCGB+$3f] (sprite palette).
; Only flushes if [wFlushPaletteFlags] is nonzero, and only flushes
; a single CGB palette if bit6 of that location is reset.
FlushPalettesIfRequested: ; 042d (0:042d)
ld a, [wFlushPaletteFlags]
or a
ret z
; flush grayscale (non-CGB) palettes
ld hl, wBGP
ld a, [hli]
ld [rBGP], a
ld a, [hli]
ld [rOBP0], a
ld a, [hl]
ld [rOBP1], a
ld a, [wConsole]
cp CONSOLE_CGB
jr z, .CGB
.done
xor a
ld [wFlushPaletteFlags], a
ret
.CGB
; flush a single CGB BG or OB palette
; if bit6 (FLUSH_ALL_PALS_F) of [wFlushPaletteFlags] is set, flush all 16 of them
ld a, [wFlushPaletteFlags]
bit FLUSH_ALL_PALS_F, a
jr nz, FlushAllCGBPalettes
ld b, CGB_PAL_SIZE
call CopyCGBPalettes
jr .done
FlushAllCGBPalettes: ; 0458 (0:0458)
; flush 8 BGP palettes
xor a
ld b, 8 palettes
call CopyCGBPalettes
; flush 8 OBP palettes
ld a, CGB_PAL_SIZE
ld b, 8 palettes
call CopyCGBPalettes
jr FlushPalettesIfRequested.done
; copy b bytes of CGB palette data starting at
; (wBackgroundPalettesCGB + a palettes) into rBGPD or rOGPD.
CopyCGBPalettes: ; 0467 (0:0467)
add a
add a
add a
ld e, a
ld d, $0
ld hl, wBackgroundPalettesCGB
add hl, de
ld c, LOW(rBGPI)
bit 6, a ; was a between 0-7 (BGP), or between 8-15 (OBP)?
jr z, .copy
ld c, LOW(rOBPI)
.copy
and %10111111
ld e, a
.next_byte
ld a, e
ld [$ff00+c], a
inc c
.wait
ld a, [rSTAT]
and 1 << STAT_BUSY ; wait until hblank or vblank
jr nz, .wait
ld a, [hl]
ld [$ff00+c], a
ld a, [$ff00+c]
cp [hl]
jr nz, .wait
inc hl
dec c
inc e
dec b
jr nz, .next_byte
ret
; reads struct:
; x (1 byte), y (1 byte), data (n bytes), $00
; writes data to BGMap0-translated x,y
; important: make sure VRAM can be accessed first, else use WriteDataBlockToBGMap0
UnsafeWriteDataBlockToBGMap0: ; 0492 (0:0492)
ld a, [hli]
ld b, a
ld a, [hli]
ld c, a
call BCCoordToBGMap0Address
jr .next
.loop
ld [de], a
inc de
.next
ld a, [hli]
or a
jr nz, .loop
ret
; initialize the screen by emptying the tilemap. used during screen transitions
EmptyScreen: ; 04a2 (0:04a2)
call DisableLCD
call FillTileMap
xor a
ld [wDuelDisplayedScreen], a
ld a, [wConsole]
cp CONSOLE_SGB
ret nz
call EnableLCD
ld hl, AttrBlkPacket_EmptyScreen
call SendSGB
call DisableLCD
ret
AttrBlkPacket_EmptyScreen: ; 04bf (0:04bf)
sgb ATTR_BLK, 1 ; sgb_command, length
db 1 ; number of data sets
; Control Code, Color Palette Designation, X1, Y1, X2, Y2
db ATTR_BLK_CTRL_INSIDE + ATTR_BLK_CTRL_LINE, 0 << 0 + 0 << 2, 0, 0, 19, 17 ; data set 1
ds 6 ; data set 2
ds 2 ; data set 3
; returns v*BGMap0 + BG_MAP_WIDTH * c + b in de.
; used to map coordinates at bc to a BGMap0 address.
BCCoordToBGMap0Address: ; 04cf (0:04cf)
ld l, c
ld h, $0
add hl, hl
add hl, hl
add hl, hl
add hl, hl
add hl, hl
ld c, b
ld b, HIGH(v0BGMap0)
add hl, bc
ld e, l
ld d, h
ret
; read joypad data to refresh hKeysHeld, hKeysPressed, and hKeysReleased
; the A + B + Start + Select combination resets the game
ReadJoypad: ; 04de (0:04de)
ld a, JOY_BTNS_SELECT
ld [rJOYP], a
ld a, [rJOYP]
ld a, [rJOYP]
cpl
and JOY_INPUT_MASK
swap a
ld b, a ; buttons data
ld a, JOY_DPAD_SELECT
ld [rJOYP], a
ld a, [rJOYP]
ld a, [rJOYP]
ld a, [rJOYP]
ld a, [rJOYP]
ld a, [rJOYP]
ld a, [rJOYP]
cpl
and JOY_INPUT_MASK
or b
ld c, a ; dpad data
cpl
ld b, a ; buttons data
ldh a, [hKeysHeld]
xor c
and b
ldh [hKeysReleased], a
ldh a, [hKeysHeld]
xor c
and c
ld b, a
ldh [hKeysPressed], a
ldh a, [hKeysHeld]
and BUTTONS
cp BUTTONS
jr nz, SaveButtonsHeld
; A + B + Start + Select: reset game
call ResetSerial
; fallthrough
Reset: ; 051b (0:051b)
ld a, [wInitialA]
di
jp Start
SaveButtonsHeld:
ld a, c
ldh [hKeysHeld], a
ld a, JOY_BTNS_SELECT | JOY_DPAD_SELECT
ld [rJOYP], a
ret
; clear joypad hmem data
ClearJoypad: ; 052a (0:052a)
push hl
ld hl, hDPadRepeat
xor a
ld [hli], a ; hDPadRepeat
ld [hli], a ; hKeysReleased
ld [hli], a ; hDPadHeld
ld [hli], a ; hKeysHeld
ld [hli], a ; hKeysPressed
pop hl
ret
; calls DoFrame a times
DoAFrames: ; 0536 (0:0536)
.loop
push af
call DoFrame
pop af
dec a
jr nz, .loop
ret
; updates background, sprites and other game variables, halts until vblank, and reads user input
; if wcad5 is not 0, the game can be paused (and resumed) by pressing the SELECT button
DoFrame: ; 053f (0:053f)
push af
push hl
push de
push bc
ld hl, wDoFrameFunction ; context-specific function
call CallIndirect
call WaitForVBlank
call ReadJoypad
call HandleDPadRepeat
ld a, [wcad5]
or a
jr z, .done
ldh a, [hKeysPressed]
and SELECT
jr z, .done
.game_paused_loop
call WaitForVBlank
call ReadJoypad
call HandleDPadRepeat
ldh a, [hKeysPressed]
and SELECT
jr z, .game_paused_loop
.done
pop bc
pop de
pop hl
pop af
ret
; handle D-pad repeat counter
; used to quickly scroll through menus when a relevant D-pad key is held
HandleDPadRepeat: ; 0572 (0:0572)
ldh a, [hKeysHeld]
ldh [hDPadHeld], a
and D_PAD
jr z, .done
ld hl, hDPadRepeat
ldh a, [hKeysPressed]
and D_PAD
jr z, .dpad_key_held
ld [hl], 24
ret
.dpad_key_held
dec [hl]
jr nz, .done
ld [hl], 6
ret
.done
ldh a, [hKeysPressed]
and BUTTONS
ldh [hDPadHeld], a
ret
; copy DMA to hDMAFunction
CopyDMAFunction: ; 0593 (0:0593)
ld c, LOW(hDMAFunction)
ld b, JumpToFunctionInTable - DMA
ld hl, DMA
.loop
ld a, [hli]
ld [$ff00+c], a
inc c
dec b
jr nz, .loop
ret
; CopyDMAFunction copies this function to hDMAFunction ($ff83)
DMA: ; 05a1 (0:05a1)
ld a, HIGH(wOAM)
ld [rDMA], a
ld a, $28
.wait
dec a
jr nz, .wait
ret
; jumps to index a in pointer table hl
JumpToFunctionInTable: ; 05ab (0:05ab)
add a
add l
ld l, a
ld a, $0
adc h
ld h, a
ld a, [hli]
ld h, [hl]
ld l, a
jp hl
; call function at [hl] if non-NULL
CallIndirect: ; 05b6 (0:05b6)
push af
ld a, [hli]
or [hl]
jr nz, .call_hl
pop af
ret
.call_hl
ld a, [hld]
ld l, [hl]
ld h, a
pop af
; fallthrough
CallHL: ; 05c1 (0:05c1)
jp hl
; 0x5c2
; converts the two-digit BCD number provided in a to text (ascii) format,
; writes them to [wStringBuffer] and [wStringBuffer + 1], and to the BGMap0 address at bc
WriteTwoDigitBCDNumber: ; 05c2 (0:05c2)
push hl
push bc
push de
ld hl, wStringBuffer
push hl
push bc
call WriteBCDNumberInTextFormat
pop bc
call BCCoordToBGMap0Address
pop hl
ld b, 2
call JPHblankCopyDataHLtoDE
pop de
pop bc
pop hl
ret
; 0x5db
; converts the one-digit BCD number provided in the lower nybble of a to text
; (ascii) format, and writes it to [wStringBuffer] and to the BGMap0 address at bc
WriteOneDigitBCDNumber: ; 05db (0:05db)
push hl
push bc
push de
ld hl, wStringBuffer
push hl
push bc
call WriteBCDDigitInTextFormat
pop bc
call BCCoordToBGMap0Address
pop hl
ld b, 1
call JPHblankCopyDataHLtoDE
pop de
pop bc
pop hl
ret
; 0x5f4
; converts the four-digit BCD number provided in h and l to text (ascii) format,
; writes them to [wStringBuffer] through [wStringBuffer + 3], and to the BGMap0 address at bc
WriteFourDigitBCDNumber: ; 05f4 (0:05f4)
push hl
push bc
push de
ld e, l
ld d, h
ld hl, wStringBuffer
push hl
push bc
ld a, d
call WriteBCDNumberInTextFormat
ld a, e
call WriteBCDNumberInTextFormat
pop bc
call BCCoordToBGMap0Address
pop hl
ld b, 4
call JPHblankCopyDataHLtoDE
pop de
pop bc
pop hl
ret
; 0x614
; given two BCD digits in the two nybbles of register a,
; write them in text (ascii) format to hl (most significant nybble first).
; numbers above 9 end up converted to half-width font tiles.
WriteBCDNumberInTextFormat: ; 0614 (0:0614)
push af
swap a
call WriteBCDDigitInTextFormat
pop af
; fallthrough
; given a BCD digit in the (lower nybble) of register a, write it in text (ascii)
; format to hl. numbers above 9 end up converted to half-width font tiles.
WriteBCDDigitInTextFormat:
and $0f
add "0"
cp "9" + 1
jr c, .write_num
add $07
.write_num
ld [hli], a
ret
; 0x627
; converts the one-byte number at a to text (ascii) format,
; and writes it to [wStringBuffer] and the BGMap0 address at bc
WriteOneByteNumber: ; 0627 (0:0627)
push bc
push hl
ld l, a
ld h, $00
ld de, wStringBuffer
push de
push bc
ld bc, -100
call TwoByteNumberToText.get_digit
ld bc, -10
call TwoByteNumberToText.get_digit
ld bc, -1
call TwoByteNumberToText.get_digit
pop bc
call BCCoordToBGMap0Address
pop hl
ld b, 3
call JPHblankCopyDataHLtoDE
pop hl
pop bc
ret
; 0x650
; converts the two-byte number at hl to text (ascii) format,
; and writes it to [wStringBuffer] and the BGMap0 address at bc
WriteTwoByteNumber: ; 0650 (0:0650)
push bc
ld de, wStringBuffer
push de
call TwoByteNumberToText
call BCCoordToBGMap0Address
pop hl
ld b, 5
call JPHblankCopyDataHLtoDE
pop bc
ret
; 0x663
; convert the number at hl to text (ascii) format and write it to de
TwoByteNumberToText: ; 0663 (0:0663)
push bc