-
Notifications
You must be signed in to change notification settings - Fork 0
/
home.asm
executable file
·6262 lines (5873 loc) · 82.1 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
SECTION "Boot", ROM0 [$0100]
nop
jp Start
SECTION "Header", ROM0 [$0104]
ds $4c
SECTION "Start", ROM0 [$0150]
INCLUDE "home/init.asm"
PushGFXRegisters:
ld a, [wc46c]
or a
jr nz, .go
jr .go
.go
ld a, [wSCX]
ld [rSCX], a
ld a, [wSCY]
ld [rSCY], a
ld a, [wWX]
ld [rWX], a
ld a, [wWY]
ld [rWY], a
ld a, [wBGP]
ld [rBGP], a
ld a, [wOBP0]
ld [rOBP0], a
ld a, [wOBP1]
ld [rOBP1], a
ld a, [wLCDC]
ld [rLCDC], a
ld a, [wLYC]
ld [rLYC], a
ld b, $0
ld hl, wc464
ld de, wc460
ld a, [de]
add b
ld [hli], a
inc de
ld a, [de]
ld [hli], a
inc de
ld a, [de]
ld [hli], a
inc de
ld a, [de]
ld [hl], a
ld a, [wc46c]
ld [wc469], a
ld a, $0
ld [wcac1], a
ld a, [wc46d]
cp $2
jr c, .skip
ld a, $2
ld [wc46d], a
ld a, [wc957]
xor $1
ld [wc957], a
ret
.skip
ret
SoftResetCheck: ; 2d0 (0:02d0)
ld a, [hJoyLast]
and A_BUTTON | B_BUTTON | START | SELECT
cp A_BUTTON | B_BUTTON | START | SELECT
ret nz
ld a, [wGameRoutine]
cp $0
ret z
xor a
ld [wGameRoutine], a
ld a, $2a
ld [wSubroutine], a
ret
INCLUDE "home/vblank.asm"
INCLUDE "home/lcd.asm"
ClearMemory:
.asm_0431
xor a
ld [hli], a
dec bc
ld a, b
or c
jr nz, .asm_0431
ret
InitSoundData: ; 439 (0:0439)
ld hl, wChannel1
ld bc, $200
call ClearMemory
ld hl, hMusicID
ld bc, $8
call ClearMemory
ld a, $ff
ld [rNR51], a
ld [wcf95], a
ld a, $8f
ld [rNR52], a
xor a
ld [rNR12], a
ld [rNR22], a
ld [rNR32], a
ld [rNR42], a
ld a, $77
ld [rNR50], a
ret
UpdateSound: ; 464 (0:0464)
ld a, [wMusicBank]
add MUSIC_BANK_00
ld [MBC3RomBank], a
call UpdateSound_20
ld a, [wROMBank]
ld [MBC3RomBank], a
ret
INCLUDE "home/farcall.asm"
INCLUDE "home/joypad.asm"
LoadPushOAM: ; 79a (0:079a)
ld c, hPushOAM % $100
ld b, PushOAMEnd - PushOAM
ld hl, PushOAM
.load
ld a, [hli]
ld [$ff00+c], a
inc c
dec b
jr nz, .load
ret
PushOAM:
ld a, wOAMBuffer / $100
ld [rDMA], a
; Wait until DMA is finished doing its thing (1 cycle per byte)
ld a, (wOAMBufferEnd - wOAMBuffer) / 4
.wait_dma
dec a
jr nz, .wait_dma
ret
PushOAMEnd:
Func_07b2:
ld a, [hBGMapYPixel]
sub $10
srl a
srl a
srl a
ld de, $0000
ld e, a
ld hl, VBGMap
ld b, BG_MAP_WIDTH
.asm_07c5
add hl, de
dec b
jr nz, .asm_07c5
ld a, [hBGMapXPixel]
sub $8
srl a
srl a
srl a
ld de, $0000
ld e, a
add hl, de
ld a, h
ld [hBGMapAddr], a
ld a, l
ld [hBGMapAddr + 1], a
ret
DisableLCD: ; 7df (0:07df)
ld a, [rIE]
ld [hIE_Backup], a
res 0, a
.wait_ly
ld a, [rLY]
cp $91
jr nz, .wait_ly
ld a, [wLCDC]
and $7f
ld [rLCDC], a
ld a, [hIE_Backup]
ld [rIE], a
ret
EnableLCD:
ld a, [rLCDC]
or $80
ld [rLCDC], a
ret
ClearMemory2:
.loop
ld a, $0
ld [hli], a
dec bc
ld a, c
or b
jr nz, .loop
ret
ClearVBGMap: ; 807 (0:0807)
ld hl, VWindow - 1
ld bc, VWindow - VBGMap
.asm_080d
ld a, $0
ld [hld], a
dec bc
ld a, b
or c
jr nz, .asm_080d
ret
Func_0816:
.asm_0816
ld a, [hli]
di
call WaitStat
ld [de], a
ei
inc de
dec bc
ld a, b
or c
jr nz, .asm_0816
ret
INCLUDE "home/oam_animations.asm"
ClearVTiles: ; 968 (0:0968)
ld hl, VTilesOB
ld bc, VBGMap - VTilesOB
jp ClearMemory3
ClearOAMBuffer: ; 971 (0:0971)
ld b, wOAMBufferEnd - wOAMBuffer
xor a
ld hl, wOAMBuffer
.asm_0977
ld [hli], a
dec b
jr nz, .asm_0977
ret
ClearWRAM: ; 97c (0:097c)
ld bc, wStackBottom - wOAMAnimations
ld hl, wOAMAnimations
jp ClearMemory3
ClearObjectAnimationBuffers:
ld a, $1
ld [wSpriteUpdatesEnabled], a
ld bc, wOAMAnimationsEnd - wOAMAnimations
ld hl, wOAMAnimations
call ClearMemory3
ld bc, wPlayerAndDenjuuObjectStructsEnd - wPlayerObjectStruct
ld hl, wPlayerObjectStruct
jp ClearMemory3
DeleteOAMAnimationStruct:
ld b, $20
xor a
.loop
ld [de], a
inc de
dec b
jr nz, .loop
ld a, $1
ld [wSpriteUpdatesEnabled], a
ret
WaitStat: ; 9aa (0:09aa)
push af
.asm_09ab
ld a, [rSTAT]
and $2
jr nz, .asm_09ab
pop af
ret
WaitStatAndLoad: ; 9b3 (0:09b3)
di
push af
.asm_09b5
ld a, [rSTAT]
and $2
jr nz, .asm_09b5
pop af
ld [hli], a
ei
ret
ClearBGMapAndAttrs:
ld bc, $400
ld hl, VBGMap
.asm_09c5
xor a
call WaitStatAndLoad
dec bc
ld a, b
or c
jr nz, .asm_09c5
ld bc, $400
ld hl, VBGMap
ld a, $1
ld [rVBK], a
.asm_09d8
xor a
call WaitStatAndLoad
dec bc
ld a, b
or c
jr nz, .asm_09d8
xor a
ld [rVBK], a
ret
ClearBGWindowAndAttrs:
ld bc, $400
ld hl, VWindow
.asm_09eb
xor a
call WaitStatAndLoad
dec bc
ld a, b
or c
jr nz, .asm_09eb
ld bc, $400
ld hl, VWindow
ld a, $1
ld [rVBK], a
.asm_09fe
xor a
call WaitStatAndLoad
dec bc
ld a, b
or c
jr nz, .asm_09fe
xor a
ld [rVBK], a
ret
Func_0a0b:
xor a
ld [wSCX], a
ld [wSCY], a
ld [wc3f7], a
ld [wWX], a
ld [wWY], a
ld [wLYC], a
ld [wc3cb], a
ld [wc3cc], a
ld a, $c3
ld [wLCDC], a
ret
INCLUDE "home/layout.asm"
DecompressGFXByIndex: ; c36 (0:0c36)
ld a, BANK(CompressedGFXBanksAndDests)
rst Bankswitch
push bc
pop de
ld hl, CompressedGFXBanksAndDests
sla e
rl d
sla e
rl d
add hl, de
ld a, [hli]
ld [wFontSourceBank], a
ld a, [hli]
ld [wFontSourceAddr], a
ld a, [hl]
ld [wFontSourceAddr + 1], a
ld a, [wFontSourceBank]
rst Bankswitch
ld hl, CompressedGFXAddresses
sla c
rl b
add hl, bc
ld a, [hli]
ld h, [hl]
ld l, a
push hl
pop de
ld a, [wFontSourceAddr + 1]
ld h, a
ld a, [wFontSourceAddr]
ld l, a
ld a, [de]
inc de
jp @ + 3 ; basically a huge nop
cp $0
jp z, .single_blocks
ld a, h
ld [wFontDestAddr], a
ld a, l
ld [wFontDestAddr + 1], a
ld a, [de]
ld c, a
inc de
ld a, [de]
ld b, a
inc de
.loop
ld a, b
or c
jp z, .exit
ld a, [de]
ld [wFontBlockFlags + 1], a
inc de
ld a, [de]
ld [wFontBlockFlags], a
inc de
ld a, $10 + 1
ld [wFontNumBlocks], a
.loop2
ld a, b
or c
jp z, .exit
ld a, [wFontNumBlocks]
dec a
jp z, .loop
ld [wFontNumBlocks], a
push de
ld a, [wFontBlockFlags]
ld d, a
ld a, [wFontBlockFlags + 1]
ld e, a
srl d
ld a, d
ld [wFontBlockFlags], a
rr e
ld a, e
ld [wFontBlockFlags + 1], a
jp c, .carry
pop de
ld a, [wFontDestAddr]
ld h, a
ld a, [wFontDestAddr + 1]
ld l, a
di
call WaitStat
ld a, [de]
call WaitStat
ld [hli], a
ei
ld a, h
ld [wFontDestAddr], a
ld a, l
ld [wFontDestAddr + 1], a
dec bc
inc de
jp .loop2
.carry
pop de
push de
ld a, [de]
ld l, a
inc de
ld a, [de]
and $7
ld h, a
ld a, [de]
srl a
srl a
srl a
and $1f
add $3
ld [wc402], a
ld a, h
cpl
ld d, a
ld a, l
cpl
ld e, a
ld a, [wFontDestAddr]
ld h, a
ld a, [wFontDestAddr + 1]
ld l, a
add hl, de
push hl
pop de
ld a, [wFontDestAddr]
ld h, a
ld a, [wFontDestAddr + 1]
ld l, a
.loop3
di
call WaitStat
ld a, [de]
call WaitStat
ld [hli], a
ei
dec bc
inc de
ld a, [wc402]
dec a
ld [wc402], a
jp nz, .loop3
ld a, h
ld [wFontDestAddr], a
ld a, l
ld [wFontDestAddr + 1], a
pop de
inc de
inc de
jp .loop2
.single_blocks
ld a, [de]
ld c, a
inc de
ld a, [de]
ld b, a
inc de
.loop4
ld a, b
or c
jp z, .exit
di
call WaitStat
ld a, [de]
call WaitStat
ld [hli], a
ei
inc de
dec bc
jp .loop4
.exit
ret
INCLUDE "home/random.asm"
INCLUDE "home/objects.asm"
GetWordFromTable:
ld b, $0
ld c, a
sla c
rl b
add hl, bc
ld a, [hli]
ld h, [hl]
ld l, a
ret
IncrementSubroutine:
ld a, [wSubroutine]
inc a
ld [wSubroutine], a
ret
IncrementSubroutine2:
ld a, [wSubroutine2]
inc a
ld [wSubroutine2], a
ret
WrapAroundBGMapOrWindowHorizontal: ; eb3 (0:0eb3)
push af
push bc
push de
call WrapAroundBGMapOrWindowVertical
ld d, h
ld e, l
ld bc, (-VBGMap) & $ffff
or a
jr z, .asm_0ec4
ld bc, (-VWindow) & $ffff
.asm_0ec4
add hl, bc
REPT 5
srl l
jr c, .not_at_start_of_row
ENDR
jr .is_at_start_of_row
.not_at_start_of_row
ld h, d
ld l, e
pop de
pop bc
pop af
ret
.is_at_start_of_row
ld h, d
ld l, e
ld bc, -BG_MAP_WIDTH
add hl, bc
call WrapAroundBGMapOrWindowVertical
pop de
pop bc
pop af
ret
Func_0eee:
push af
push bc
push de
call WrapAroundBGMapOrWindowVertical
ld d, h
ld e, l
ld bc, $6800
or a
jr z, .asm_0eff
ld bc, $6400
.asm_0eff
add hl, bc
srl l
jr nc, .asm_0f16
srl l
jr nc, .asm_0f16
srl l
jr nc, .asm_0f16
srl l
jr nc, .asm_0f16
srl l
jr nc, .asm_0f16
jr .asm_0f1c
.asm_0f16
ld h, d
ld l, e
pop de
pop bc
pop af
ret
.asm_0f1c
ld h, d
ld l, e
ld bc, $20
add hl, bc
call WrapAroundBGMapOrWindowVertical
pop de
pop bc
pop af
ret
WrapAroundBGMapOrWindowVertical: ; f29 (0:0f29)
push af
or a
jr nz, .window
ld a, h
and $3
ld h, VBGMap / $100
or h
ld h, a
pop af
ret
.window
ld a, h
and $f
ld h, VWindow / $100
or h
ld h, a
pop af
ret
INCLUDE "home/sram.asm"
INCLUDE "home/palettes.asm"
Get2DigitBCD: ; 12fb (0:12fb)
; bcd convert 2-digit number to [wNumCGBPalettesToFade]
push de
push bc
push hl
push af
xor a
ld [wFontSourceBank], a
ld [wNumCGBPalettesToFade], a
ld de, 0
pop af
ld b, 100
.loop_100
ld c, a
sub b
jr c, .okay_100
inc d
jr .loop_100
.okay_100
ld a, c
ld b, 10
.loop_10
ld c, a
sub b
jr c, .okay_10
push af
ld a, e
add $10
ld e, a
pop af
jr .loop_10
.okay_10
ld a, c
ld b, $1
.loop_1
ld c, a
sub b
jr c, .okay_1
inc e
jr .loop_1
.okay_1
ld a, d
ld [wFontSourceBank], a
ld a, e
ld [wNumCGBPalettesToFade], a
pop hl
pop bc
pop de
ret
Func_1338: ; 1338 (0:1338)
xor a
ld [wFontSourceBank], a
ld [wNumCGBPalettesToFade], a
ld a, b
or a
jr nz, .asm_1346
ld a, c
or a
ret z
.asm_1346
ld a, h
ld [wCGBFade_PalLimit], a
ld a, l
ld [wc441], a
ld d, b
ld a, h
sub d
ret c
jr nz, .asm_1358
ld d, c
ld a, l
sub d
ret c
.asm_1358
ld a, [wc441]
sub c
ld l, a
ld a, [wCGBFade_PalLimit]
sbc b
ld h, a
ld a, [wFontSourceBank]
add $1
ld [wFontSourceBank], a
ld a, $0
adc $0
ld d, a
ld a, [wNumCGBPalettesToFade]
add d
ld [wNumCGBPalettesToFade], a
jr .asm_1346
PrintDenjuuStat:
push hl
push de
push bc
push bc
push hl
call Get2DigitBCD
pop hl
xor a
ld [wFontSourceAddr], a
pop bc
ld a, c
cp $1
jp nz, .ThreeDigits
ld a, $1
ld [wFontSourceAddr], a
jp .TwoDigits
.ThreeDigits: ; 1394 (0:1394)
ld a, [wFontSourceBank]
and $f
or a
jr nz, .asm_139d
xor a
.asm_139d
add $f0
di
call WaitStat
ld [hl], a
ei
ld a, $1
ld [wFontSourceAddr], a
.TwoDigits: ; 13aa (0:13aa)
inc hl
ld a, [wNumCGBPalettesToFade]
and $f0
or a
jr nz, .asm_13bb
ld b, a
ld a, [wFontSourceAddr]
or a
jr z, .asm_13c5
ld a, b
.asm_13bb
swap a
add $f0
di
call WaitStat
ld [hl], a
ei
.asm_13c5
inc hl
ld a, [wNumCGBPalettesToFade]
and $f
add $f0
di
call WaitStat
ld [hl], a
ei
pop bc
pop de
pop hl
ret
Func_13d7:
push hl
push de
push bc
push hl
call Get2DigitBCD
pop hl
xor a
ld [wFontSourceAddr], a
ld a, [wNumCGBPalettesToFade]
and $f0
or a
jr nz, .asm_13ec
ld a, b
.asm_13ec
add $e0
di
call WaitStat
ld [hl], a
ei
inc a
push hl
ld de, $20
add hl, de
di
call WaitStat
ld [hl], a
ei
pop hl
ld a, $1
ld [wFontSourceAddr], a
inc hl
ld a, [wNumCGBPalettesToFade]
and $f
or a
jr nz, .asm_1417
ld b, a
ld a, [wFontSourceAddr]
or a
jr z, .asm_142c
ld a, b
.asm_1417
swap a
add $e0
di
call WaitStat
ld [hl], a
ei
inc a
ld de, $20
add hl, de
di
call WaitStat
ld [hl], a
ei
.asm_142c
pop bc
pop de
pop hl
ret
Print2DigitBCD_2:
push hl
push de
push bc
push bc
push hl
call Get2DigitBCD
pop hl
xor a
ld [wFontSourceAddr], a
pop bc
ld a, c
cp $1
jp nz, .asm_144c
ld a, $1
ld [wFontSourceAddr], a
jp .asm_146d
.asm_144c
ld a, [wFontSourceBank]
and $f
or a
jr nz, .asm_145c
ld b, a
ld a, [wFontSourceAddr]
or a
jr z, .asm_1468
ld a, b
.asm_145c
add $f0
call WaitStatAndLoad
ld a, $1
ld [wFontSourceAddr], a
jr .asm_146d
.asm_1468
ld a, $ff
call WaitStatAndLoad
.asm_146d
ld a, [wNumCGBPalettesToFade]
and $f0
or a
jr nz, .asm_147d
ld b, a
ld a, [wFontSourceAddr]
or a
jr z, .asm_1486
ld a, b
.asm_147d
swap a
add $f0
call WaitStatAndLoad
jr .asm_148b
.asm_1486
ld a, $ff
call WaitStatAndLoad
.asm_148b
ld a, [wNumCGBPalettesToFade]
and $f
add $f0
call WaitStatAndLoad
pop bc
pop de
pop hl
ret
Func_1499:
push hl
push de
push bc
push bc
push hl
call Get2DigitBCD
pop hl
pop bc
ld a, [wNumCGBPalettesToFade]
and $f
add $f0
call WaitStatAndLoad
pop bc
pop de
pop hl
ret
Func_14b1:
push hl
push de
push bc
xor a
ld [wFontSourceAddr], a
ld a, b
ld [wc442], a
ld a, c
ld [wc443], a
ld bc, $3e8
call Func_1338
ld a, [wFontSourceBank]
or a
jr nz, .asm_14d4
ld b, a
ld a, [wFontSourceAddr]
or a
jr z, .asm_14eb
ld a, b