-
Notifications
You must be signed in to change notification settings - Fork 3
/
home.asm
4447 lines (4227 loc) · 56.6 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
INCLUDE "includes.asm"
INCLUDE "charmap.asm"
INCLUDE "rst.asm"
INCLUDE "interrupts.asm"
SECTION "High Home", ROM0 [$68]
Predef:: ; 68 (0:0068)
add sp, -$5
push af
push de
push hl
; 1-byte argument off the stack
ld hl, sp+$b ; 5 + 6
ld e, [hl]
inc hl
ld d, [hl]
ld a, [de]
inc de
ld [hl], d
dec hl
ld [hl], e
; If >= $4a, it's a far call
ld e, a
ld a, [Byte_000d] ; 4a
cp e
jr c, .asm_0093
; Jump to a function from the table.
ld d, $0
ld hl, Pointers_01e0
add hl, de
add hl, de
ld e, [hl]
inc hl
ld d, [hl]
ld hl, sp+$9
ld [hl], e
inc hl
ld [hl], d
pop hl
pop de
pop af
add sp, $3
ret
.asm_0093
ld d, e
; store the return bank and address on the stack
dec hl
ld a, [hROMBank]
ld [hld], a
ld [hl], high(.Return)
dec hl
ld [hl], low(.Return)
; get the far pointer
sla e
ld a, d
rlca
and $1
add Bank_000e
ld l, a
ld h, $0
ld a, [hl]
call BankSwitch_00f7
ld hl, Pointers_78096 - 2 * $4b
ld d, l
add hl, de
ld e, [hl]
inc hl
ld d, [hl]
ld hl, sp+$6
ld [hl], e
inc hl
ld [hl], d
pop hl
pop de
pop af
ret
.Return
push af
push hl
ld hl, sp+$4
ld a, [hl]
call BankSwitch_00f7
pop hl
pop af
inc sp
ret
Func_00c9:: ; c9 (0:00c9)
; Get a short from hl+[hl] and put it on the stack above the return address.
push af
push af
push de
push hl
ld hl, sp+$8
ld a, [hli]
ld h, [hl]
ld l, a
ld e, [hl]
inc hl
ld d, [hl]
push hl
dec hl
add hl, de
ld e, l
ld d, h
ld hl, sp+$b
ld [hl], d
dec hl
ld [hl], e
pop de
inc de
dec hl
ld [hl], d
dec hl
ld [hl], e
pop hl
pop de
pop af
ret
InitCartIRMode:: ; 00e9
xor a
ld [hSRAMBank], a
inc a
ld [HuC3LatchClock], a
xor a
ld [HuC3SRamMode], a
ld a, [Bank_000c]
BankSwitch_00f7:: ; f7 (0:00f7)
ld [hROMBank], a
ld [HuC3RomBank], a
ret
SECTION "Init", ROM0 [$100]
Init::
nop
jp Start
SECTION "Header", ROM0 [$104]
ds $150 - $104
SECTION "Home", ROM0 [$150]
Func_0150:: ; 0150
ld a, [Bank_000b]
ld [hROMBank], a
ld [HuC3RomBank], a
jp Func_78100
CopyPredef::
ld a, b
or c
ret z
ld a, [hFF86]
or a
jr z, .copy
ld a, h
and $e0
cp $80
jr z, .to_vram
ld a, d
and $e0
cp $80
jr nz, .copy
predef CopyFromVRAMPredef
ret
.copy
ld a, [de]
inc de
ld [hli], a
dec bc
ld a, b
or c
jr nz, .copy
ret
.to_vram
ld a, b
or a
.loop1
push af
predef CopyToVRAMPredef
pop af
push af
jr nz, .okay
ld a, b
cp c
jr nc, .next
.okay
push bc
push hl
ld c, b
ld b, $0
push bc
call .HBlankCopy
pop bc
pop hl
add hl, bc
pop bc
pop af
push hl
ld l, b
ld b, a
ld a, c
sub l
ld c, a
ld a, b
sbc $0
ld b, a
pop hl
jr .loop1
.next
pop af
.HBlankCopy: ; 1a6 (0:01a6)
predef Func_7ce36
.hblank_copy_loop
ld a, [de]
inc de
ld [hl], a
inc l
dec c
jr nz, .hblank_copy_loop
di
ld a, l
ld [hFF87], a
ld a, [hFF89]
add b
ld [hFF89], a
ei
NullPredef::
ret
SECTION "01e0", ROM0 [$1e0]
Pointers_01e0:: ; 01e0
dw InitCartIRMode
dw Func_0150
dw CopyPredef
dw Func_7af00
dw Func_7af30
dw Func_7af39
dw AnonJumpRelativeTable
dw AnonJumpRelativeTableLookup
dw AnonJumpRelativeTableLookup2
dw Func_02fd
dw NullPredef
dw NullPredef
dw NullPredef
dw NullPredef
dw NullPredef
dw NullPredef
dw Func_0296
dw SoundOffPredef
dw UpdateSoundPredef
dw StartSongPredef
dw StartSFXPredef
dw SelectChannelsPredef
dw CheckSongFinishedPredef
dw CheckSFXFinishedPredef
dw ToggleMusicPredef
dw SetVolumePredef
dw Func_0218
dw Func_026c
Func_0218:: ; 0218
ld de, $1c7d
ret
Func_021c:: ; 21c (0:021c)
push af
push bc
push de
push hl
ld a, [hFF99]
bit 2, a
call nz, UpdateSoundPredef
pop hl
pop de
pop bc
pop af
reti
INCLUDE "home/predef_audio.asm"
Func_026c:: ; 26c (0:026c)
ld a, d
cp $80
ret c
cp $a0
jp c, Func_03d3
cp $c0
jr c, .asm_0286
cp $e0
jp nc, Func_03d3
sub $c0
predef Func_7b25b
jp c, Func_03d3
ret
.asm_0286
sub $a0
predef Func_7c100
jp c, Func_03d3
ret
Func_028e:: ; 028e
di
ld sp, wStackTop
predef InitCartIRMode
predef Func_7b1e1
Func_0296:: ; 296 (0:0296)
ld hl, Func_0296
predef Func_7b133
jp Func_0300
SECTION "02fd", ROM0 [$2fd]
Func_02fd:: ; 2fd (0:02fd)
jp Func_0309
Func_0300:: ; 300 (0:0300)
jp Func_030a
Func_0303:: ; 0303
jp Func_03a4
Func_0306:: ; 0306
jp Func_03d1
Func_0309:: ; 309 (0:0309)
ret
Func_030a:: ; 30a (0:030a)
ld bc, $a0000 / $10000
.asm_030d
ld hl, $10000 % $10000
.asm_0310
dec hl
ld a, l
or h
jr nz, .asm_0310
dec bc
ld a, c
or b
jr nz, .asm_030d
di
ld sp, wStackTop
predef InitCartIRMode
call SoundOff
Start:: ; 323 (0:0323)
di
ld sp, wStackTop
ld [hSystemType], a
push af
Func_032a:: ; 32a (0:032a)
ld a, [rLY]
cp $91
jp nc, Func_032a
Func_0331:: ; 331 (0:0331)
ld a, [rLY]
cp $91
jp c, Func_0331
ld a, [rLCDC]
and $7f
ld [rLCDC], a
hlbgcoord 0, 0
ld bc, $400
.asm_0344
xor a
ld [hli], a
dec bc
ld a, b
or c
jr nz, .asm_0344
ld a, [rLCDC]
or $80
ld [rLCDC], a
pop af
di
ld sp, wStackTop
push af
predef InitCartIRMode
predef SoundOffPredef
ld a, BANK(Func_63141)
call BankSwitch
pop af
push af
call Func_63141
di
pop af
ld [wSystemType], a
di
ld sp, wStackTop
ld a, BANK(Func_4064)
call BankSwitch
ld a, $3
call GetSRAMBank
call Func_4064
ld a, BANK(Func_fe102)
call BankSwitch
call Func_fe102
ld a, [wSystemType]
ld [hSystemType], a
Func_0388:: ; 388 (0:0388)
di
ld sp, wStackTop
ld a, BANK(Func_4064)
call BankSwitch
ld a, $3
call GetSRAMBank
call Func_4064
call SoundOff
ld a, BANK(Func_4000)
call BankSwitch
jp Func_4000
Func_03a4:: ; 3a4 (0:03a4)
ld a, [hSystemType]
ld [wSystemType], a
di
ld sp, wStackTop
ld a, BANK(Func_4064)
call BankSwitch
ld a, $3
call GetSRAMBank
call Func_4064
call SoundOff
ld a, $ff
ld [wc213], a
ld a, BANK(Func_4000)
call BankSwitch
call Func_4000
xor a
ld [wc213], a
jp Func_1e4d
Func_03d1:: ; 3d1 (0:03d1)
scf
ret
Func_03d3:: ; 3d3 (0:03d3)
scf
ret
GetSRAMBank:: ; 3d5 (0:03d5)
; a = SRAM bank
; bit 7: read-only if set, read/write else
bit 7, a
jr nz, GetSRAMBank_ReadOnly
ld [hSRAMBank], a
ld [HuC3SRamBank], a
ld a, SRAM_READWRITE
ld [HuC3SRamMode], a
ret
GetSRAMBank_ReadOnly::
set 7, a
ld [hSRAMBank], a
res 7, a
ld [HuC3SRamBank], a
xor a ; SRAM_READONLY
ld [HuC3SRamMode], a
ret
BankSwitch::
ld [hROMBank], a
ld [HuC3RomBank], a
ret
FarCall::
; call [c212]:[c21b]
; preserves registers
; does not preserve flags
; stack overflow check
push hl
push bc
ld hl, sp+$0
ld bc, $10000 - wStackBottom
add hl, bc
bit 7, h
jr z, .stack_okay
di
ld hl, sp+$4
ld e, [hl]
inc hl
ld d, [hl]
ld a, [hROMBank]
ld c, a
ld b, $ff
ld hl, sp+$0
ld sp, wStackTop
jp Crash
.stack_okay
pop bc
pop hl
ld [wFarCallSavedA], a
ld a, [hROMBank]
push af
ld a, [wFarCallDestBank]
call BankSwitch
ld a, l
ld [wFarCallSavedHL], a
ld a, h
ld [wFarCallSavedHL + 1], a
ld hl, .Return
push hl
ld a, [wFarCallDestAddr]
ld l, a
ld a, [wFarCallDestAddr + 1]
ld h, a
push hl
ld a, [wFarCallSavedHL + 1]
ld h, a
ld a, [wFarCallSavedHL]
ld l, a
ld a, [wFarCallSavedA]
ret
.Return:
ld [wFarCallSavedA], a
pop af
call BankSwitch
ld a, [wFarCallSavedA]
ret
NextBattleFrame:: ; 0451
push bc
ld a, [wc39e]
push af
xor a
ld [wc39e], a
call NextOverworldFrame
ld c, a
pop af
ld [wc39e], a
ld a, c
pop bc
ret
NextOverworldFrame:: ; 465 (0:0465)
push bc
ld a, [wTextBlinkerFrameCounter]
ld c, a
.loop
halt
nop
ld a, [wJoyHeld]
ld b, a
ld a, [wTextBlinkerFrameCounter]
cp c
jr z, .loop
ld a, b
push af
call GetTime
pop af
pop bc
ret
CopyFromDEtoHL::
; copy bc bytes from de to hl
.asm_047e
ld a, [de]
inc de
ld [hli], a
dec bc
ld a, b
or c
jr nz, .asm_047e
ret
FillMemory::
; fill bc bytes at hl with e
.asm_0487
ld a, e
ld [hli], a
dec bc
ld a, b
or c
jr nz, .asm_0487
ret
SECTION "0500", ROM0 [$500]
INCLUDE "home/gbkiss.asm"
SECTION "0e00", ROM0 [$e00]
INCLUDE "home/interrupts.asm"
INCLUDE "home/graphics_copy.asm"
INCLUDE "home/text.asm"
RightShiftULong:: ; 1a06
inc e
ld b, h
ld c, l
jr .handleLoop
.loop
ld a, [hl]
srl a
ld [hli], a
ld a, [hl]
rr a
ld [hli], a
ld a, [hl]
rr a
ld [hli], a
rr [hl]
.handleLoop
ld l, c
ld h, b
dec e
jr nz, .loop
ret
MultiplyULongAtHLByUShortDE:: ; 1a1f
push bc
ld c, l
ld b, h
add sp, -$4
ld hl, sp+$0
ld a, $4
.copy
push af
ld a, [bc]
ld [hli], a
inc bc
pop af
dec a
jr nz, .copy
dec hl
ld c, l
ld b, h
push de
read_hl_from_sp_plus $8
pop de
xor a
ld [hli], a
ld [hli], a
ld [hli], a
ld [hl], a
reg8rot a, l, c
reg8rot a, h, b
.main_loop
ld a, e
or d
jr z, .done
srl d
rr e
push de
jr nc, .next
ccf
push bc
push hl
ld e, $4
.add_loop
ld a, [bc]
adc [hl]
ld [bc], a
dec bc
dec hl
dec e
jr nz, .add_loop
pop hl
pop bc
.next
push hl
ld e, $4
scf
ccf
.rotate_left
ld a, [hl]
rla
ld [hld], a
dec e
jr nz, .rotate_left
pop hl
pop de
jr .main_loop
.done
add sp, $6
ret
INCLUDE "home/audio.asm"
GBKiss:: ; 1add
di
xor a
ld [rIF], a
ld [rIE], a
ld a, $0
call GetSRAMBank
ld a, BANK(GBKiss)
call BankSwitch
ld sp, wStackTop
ld a, $e4
ld [rBGP], a
ld [rOBP0], a
ld a, $1b
ld [rOBP1], a
predef InitCartIRMode
predef Func_7b21d
jp @ ; better luck next time
Func_1b01:: ; 1b01
ld sp, wStackTop
ld a, $3
call GetSRAMBank
callba TitleScreen
ld a, $3
call GetSRAMBank
ld a, BANK(Func_4060)
call BankSwitch
jp Func_4060
GetTime:: ; 1b28 (0:1b28)
ld a, [wc39e]
or a
ret z
push bc
push de
push hl
call SuppressVBlankCallback
push bc
ld a, [hSRAMBank]
push af
ld a, [wRTCTicker]
cp $11
jr z, .end_of_cycle
di
predef RTCUpdatePredef
ei
ld a, [wRTCTicker]
cp $10
jp nz, .done
ld a, $11
ld [wRTCTicker], a
jp .done
.end_of_cycle
di
predef RTCUpdatePredef
ei
ld a, $1
ld [wRTCTicker], a
ld a, e
ld [wRTCDays], a
ld a, d
ld [wRTCDays + 1], a
ld a, [wMinutesSinceMidnight]
ld e, a
ld a, c
ld [wc92c], a
ld a, b
ld [wc92c + 1], a
ld a, l
ld [wMinutesSinceMidnight], a
ld a, h
ld [wMinutesSinceMidnight + 1], a
ld a, e
cp l
jr z, .done
ld a, [wc932 + 1]
cp b
jr c, .disable_vblank
jr nz, .done
ld a, [wc932]
cp c
jr c, .disable_vblank
jr nz, .done
ld a, [wc934 + 1]
cp h
jr c, .disable_vblank
jr nz, .done
ld a, [wc934]
cp l
jr c, .disable_vblank
jr nz, .done
.disable_vblank
di
ld a, [rIE]
push af
and $fe
ld [rIE], a
ei
ld hl, wc936
ld c, $26
ld b, $2
predef Func_7e497
ld a, $6e
predef WriteRTCPredef
ld a, $6e
predef WriteRTCPredef
di
pop af
and $1
ld c, a
ld a, [rIE]
or c
ld [rIE], a
ld hl, rIF
res 0, [hl]
ei
ld a, [wFarCallDestBank]
push af
callba Func_93c0c
pop af
ld [wFarCallDestBank], a
.done
pop af
call GetSRAMBank
pop bc
call ApplyVBlankCallbackEnableFlag
pop hl
pop de
pop bc
ret
SuppressVBlankCallback:: ; 1beb (0:1beb)
di
ld a, [wNextVBlankFlags]
ld c, a
res 7, a
ld [wNextVBlankFlags], a
ld a, [wc2e8]
ld b, a
xor a
ld [wc2e8], a
ei
ret
ApplyVBlankCallbackEnableFlag:: ; 1bff (0:1bff)
ld a, c
and $80
ld c, a
di
ld a, [wNextVBlankFlags]
or c
ld [wNextVBlankFlags], a
ld a, b
ld [wc2e8], a
ei
ret
Func_1c11:: ; 1c11
ld a, [hSRAMBank]
push af
call SuppressVBlankCallback
push bc
ld c, l
ld b, h
predef IR_Receive_Predef
pop bc
ld l, a
call ApplyVBlankCallbackEnableFlag
pop af
call GetSRAMBank
ld a, l
ret
Func_1c27:: ; 1c27
ld a, [hSRAMBank]
push af
call SuppressVBlankCallback
push bc
predef Func_7e556
ld l, a
ld h, b
pop bc
call ApplyVBlankCallbackEnableFlag
pop af
call GetSRAMBank
ret
Func_1c3b:: ; 1c3b
ld a, [hSRAMBank]
push af
call SuppressVBlankCallback
push bc
predef Func_7aa8e
jr c, .asm_1c53
xor a
ld [wc326], a
.asm_1c4a
pop bc
call ApplyVBlankCallbackEnableFlag
pop af
call GetSRAMBank
ret
.asm_1c53
ld a, $80
ld [wc326], a
jr .asm_1c4a
Func_1c5a:: ; 1c5a
ld a, [hSRAMBank]
push af
ld b, c
push bc
call SuppressVBlankCallback
pop af
push bc
ld c, a
predef Func_7ac8d
jr asm_1c8c
Func_1c69:: ; 1c69
ld a, [hSRAMBank]
push af
ld b, c
push bc
call SuppressVBlankCallback
pop af
push bc
ld c, a
predef Func_7aca4
jr asm_1c8c
Func_1c78:: ; 1c78
ld a, [hSRAMBank]
push af
call SuppressVBlankCallback
push bc
predef Func_7aa8e
jr asm_1c8c
Func_1c83:: ; 1c83
ld a, [hSRAMBank]
push af
call SuppressVBlankCallback
push bc
predef Func_7ac16
asm_1c8c
jr nc, .asm_1c99
pop bc
call ApplyVBlankCallbackEnableFlag
pop af
call GetSRAMBank
ld a, $ff
ret
.asm_1c99
pop bc
call ApplyVBlankCallbackEnableFlag
pop af
call GetSRAMBank
xor a
ret
EnableVBlank::
ld hl, rIE
set 0, [hl]
ret
DisableVBlank::
ld hl, rIE
res 0, [hl]
ret
EnableHBlank::
ld hl, rIE
set 1, [hl]
ret
DisableHBlank::
ld hl, rIE
res 1, [hl]
ret
EnableTimerInt::
ld hl, rIE
set 2, [hl]
ret
DisableTimerInt::
ld hl, rIE
res 2, [hl]
ret
EnableJoypadInt::
ld hl, rIE
set 3, [hl]
ret
DisableJoypadInt::
ld hl, rIE
res 3, [hl]
ret
SECTION "1d00", ROM0 [$1d00]
INCLUDE "home/crash.asm"
NewSaveFileInWRam:: ; 1db9
ld a, [hSRAMBank]
push af
ld a, BANK(sHeap)
call GetSRAMBank
ld bc, sSaveBlock1End - sSaveBlock1