-
Notifications
You must be signed in to change notification settings - Fork 5
/
home.asm
executable file
·1866 lines (1583 loc) · 25.9 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 "constants.asm"
SECTION "NULL", ROM0
NULL::
INCLUDE "home/rst.asm"
INCLUDE "home/interrupts.asm"
SECTION "Header", ROM0
Start::
nop
jp _Start
SECTION "Home", ROM0
INCLUDE "home/init.asm"
INCLUDE "home/vblank.asm"
INCLUDE "home/delay.asm"
INCLUDE "home/rtc.asm"
INCLUDE "home/fade.asm"
INCLUDE "home/lcd.asm"
INCLUDE "home/time.asm"
INCLUDE "home/serial.asm"
INCLUDE "home/joypad.asm"
INCLUDE "home/decompress.asm"
INCLUDE "home/palettes.asm"
INCLUDE "home/copy.asm"
INCLUDE "home/text.asm"
INCLUDE "home/video.asm"
INCLUDE "home/map_objects.asm"
INCLUDE "home/sine.asm"
INCLUDE "home/movement.asm"
INCLUDE "home/tilemap.asm"
INCLUDE "home/menu.asm"
INCLUDE "home/handshake.asm"
INCLUDE "home/game_time.asm"
INCLUDE "home/map.asm"
INCLUDE "home/farcall.asm"
INCLUDE "home/predef.asm"
INCLUDE "home/window.asm"
INCLUDE "home/flag.asm"
INCLUDE "home/restore_music.asm"
xor_a:: ; 2ec6
xor a
ret
; 2ec8
xor_a_dec_a:: ; 2ec8
xor a
dec a
ret
; 2ecb
DisableSpriteUpdates:: ; 0x2ed3
; disables overworld sprite updating?
xor a
ld [hMapAnims], a
ld a, [VramState]
res 0, a
ld [VramState], a
xor a
ld [wSpriteUpdatesEnabled], a
ret
; 0x2ee4
EnableSpriteUpdates:: ; 2ee4
ld a, $1
ld [wSpriteUpdatesEnabled], a
ld a, [VramState]
set 0, a
ld [VramState], a
ld a, $1
ld [hMapAnims], a
ret
; 2ef6
INCLUDE "home/string.asm"
IsInJohto:: ; 2f17
; Return z if the player is in Johto, and nz in Kanto or Shamouti Island.
call GetCurrentLandmark
cp KANTO_LANDMARK
jr nc, .kanto_or_orange
.johto
xor a ; JOHTO_REGION
and a
ret
.kanto_or_orange
ld a, KANTO_REGION
and a
ret
; 2f3e
INCLUDE "home/item.asm"
INCLUDE "home/random.asm"
INCLUDE "home/sram.asm"
; Register aliases
_hl_:: ; 2fec
jp hl
; 2fed
_de_:: ; 2fed
push de
ret
; 2fef
INCLUDE "home/double_speed.asm"
ClearSprites:: ; 300b
; Erase OAM data
ld hl, Sprites
ld b, SpritesEnd - Sprites
xor a
.loop
ld [hli], a
dec b
jr nz, .loop
ret
; 3016
HideSprites:: ; 3016
; Set all OAM y-positions to 160 to hide them offscreen
ld hl, Sprites
ld de, 4 ; length of an OAM struct
ld b, (SpritesEnd - Sprites) / 4 ; number of OAM structs
ld a, 160 ; y
.loop
ld [hl], a
add hl, de
dec b
jr nz, .loop
ret
; 3026
INCLUDE "home/copy2.asm"
_Jumptable:
push de
ld e, a
ld d, 0
add hl, de
add hl, de
ld a, [hli]
ld h, [hl]
ld l, a
pop de
jp hl
LoadTileMapToTempTileMap:: ; 309d
; Load TileMap into TempTileMap
ld a, [rSVBK]
push af
ld a, BANK(TempTileMap)
ld [rSVBK], a
hlcoord 0, 0
decoord 0, 0, TempTileMap
ld bc, TileMapEnd - TileMap
call CopyBytes
pop af
ld [rSVBK], a
ret
; 30b4
Call_LoadTempTileMapToTileMap:: ; 30b4
xor a
ld [hBGMapMode], a
call LoadTempTileMapToTileMap
ld a, 1
ld [hBGMapMode], a
ret
; 30bf
LoadTempTileMapToTileMap:: ; 30bf
; Load TempTileMap into TileMap
ld a, [rSVBK]
push af
ld a, BANK(TempTileMap)
ld [rSVBK], a
hlcoord 0, 0, TempTileMap
decoord 0, 0
ld bc, TileMapEnd - TileMap
call CopyBytes
pop af
ld [rSVBK], a
ret
; 30d6
CopyName1:: ; 30d6
; Copies the name from de to StringBuffer2
ld hl, StringBuffer2
CopyName2:: ; 30d9
; Copies the name from de to hl
.loop
ld a, [de]
inc de
ld [hli], a
cp "@"
jr nz, .loop
ret
; 30e1
IsInArray:: ; 30e1
; Find value a for every de bytes in array hl.
; Return index in b and carry if found.
ld b, 0
ld c, a
.loop
ld a, [hl]
cp -1
jr z, .NotInArray
cp c
jr z, .InArray
inc b
add hl, de
jr .loop
.NotInArray:
and a
ret
.InArray:
scf
ret
; 30f4
SkipNames:: ; 0x30f4
; Skip a names.
ld bc, NAME_LENGTH
and a
ret z
.loop
add hl, bc
dec a
jr nz, .loop
ret
; 0x30fe
INCLUDE "home/math.asm"
PrintLetterDelay:: ; 313d
; Wait before printing the next letter.
; The text speed setting in Options1 is actually a frame count:
; fast: 1 frame
; mid: 3 frames
; slow: 5 frames
; TextBoxFlags[!0] and A or B override text speed with a one-frame delay.
; Options1[4] and TextBoxFlags[!1] disable the delay.
ld a, [Options1]
bit NO_TEXT_SCROLL, a
ret nz
; non-scrolling text?
ld a, [TextBoxFlags]
bit 1, a
ret z
push hl
push de
push bc
ld hl, hOAMUpdate
ld a, [hl]
push af
; orginally turned oam update off...
; ld a, 1
ld [hl], a
; force fast scroll?
ld a, [TextBoxFlags]
bit 0, a
jr z, .fast
; text speed
ld a, [Options1]
and %111
jr .updatedelay
.fast
ld a, 1
.updatedelay
ld [TextDelayFrames], a
.checkjoypad
call GetJoypad
; input override
ld a, [wDisableTextAcceleration]
and a
jr nz, .wait
; Wait one frame if holding A or B.
ld a, [hJoyDown]
bit 0, a ; A_BUTTON
jr z, .checkb
jr .delay
.checkb
bit 1, a ; B_BUTTON
jr z, .wait
.delay
call DelayFrame
jr .end
.wait
ld a, [TextDelayFrames]
and a
jr nz, .checkjoypad
.end
pop af
ld [hOAMUpdate], a
pop bc
pop de
pop hl
ret
; 318c
CopyDataUntil:: ; 318c
; Copy [hl .. bc) to de.
; In other words, the source data is
; from hl up to but not including bc,
; and the destination is de.
ld a, [hli]
ld [de], a
inc de
ld a, h
cp b
jr nz, CopyDataUntil
ld a, l
cp c
jr nz, CopyDataUntil
ret
; 0x3198
PrintNum:: ; 3198
homecall _PrintNum
ret
; 31a4
FarPrintText:: ; 31b0
ld [hBuffer], a
homecall PrintText, [hBuffer]
ret
; 31be
CallPointerAt:: ; 31be
ld a, [hROMBank]
push af
ld a, [hli]
rst Bankswitch
ld a, [hli]
ld h, [hl]
ld l, a
call _hl_
pop hl
ld a, h
rst Bankswitch
ret
; 31cd
QueueScript:: ; 31cd
; Push pointer hl in the current bank to wQueuedScriptBank.
ld a, [hROMBank]
FarQueueScript:: ; 31cf
; Push pointer a:hl to wQueuedScriptBank.
ld [wQueuedScriptBank], a
ld a, l
ld [wQueuedScriptAddr], a
ld a, h
ld [wQueuedScriptAddr + 1], a
ret
; 31db
StringCmp:: ; 31db
; Compare c bytes at de and hl.
; Return z if they all match, c if hl>de.
.loop
ld a, [de]
cp [hl]
ret nz
inc de
inc hl
dec c
jr nz, .loop
ret
; 0x31e4
CompareLong:: ; 31e4
; Compare bc bytes at de and hl.
; Return carry if they all match.
ld a, [de]
cp [hl]
jr nz, .Diff
inc de
inc hl
dec bc
ld a, b
or c
jr nz, CompareLong
scf
ret
.Diff:
and a
ret
; 31f3
ClearBGPalettes:: ; 31f3
call ClearPalettes
WaitBGMap:: ; 31f6
; Tell VBlank to update BG Map
ld a, 1 ; BG Map 0 tiles
ld [hBGMapMode], a
; Wait for it to do its magic
ld c, 4
jp DelayFrames
; 3200
WaitBGMap2:: ; 0x3200
ld a, 2
ld [hBGMapMode], a
ld c, 4
call DelayFrames
ld a, 1
ld [hBGMapMode], a
ld c, 4
jp DelayFrames
; 0x3218
ApplyTilemap:: ; 321c
ld a, [wSpriteUpdatesEnabled]
cp 0
jr z, .dmg
ld a, 1
ld [hBGMapMode], a
jr LoadEDTile
.dmg
; WaitBGMap
ld a, 1
ld [hBGMapMode], a
ld c, 4
jp DelayFrames
; 3238
LoadEDTile:: ; 323d
ld a, [hBGMapMode]
push af
xor a
ld [hBGMapMode], a
ld a, [hMapAnims]
push af
xor a
ld [hMapAnims], a
.wait
ld a, [rLY]
cp $7f
jr c, .wait
di
ld a, BANK(VTiles3)
ld [rVBK], a
hlcoord 0, 0, AttrMap
call .StackPointerMagic
xor a ; ld a, BANK(VTiles0)
ld [rVBK], a
hlcoord 0, 0
call .StackPointerMagic
.wait2
ld a, [rLY]
cp $7f
jr c, .wait2
ei
pop af
ld [hMapAnims], a
pop af
ld [hBGMapMode], a
ret
; 327b
.StackPointerMagic: ; 327b
; Copy all tiles to VBGMap
ld [hSPBuffer], sp
ld sp, hl
ld a, [hBGMapAddress + 1]
ld h, a
ld l, 0
ld a, SCREEN_HEIGHT
ld [hTilesPerCycle], a
lb bc, (1 << 1), (rSTAT % $100) ; b: not in v/hblank
.loop
rept SCREEN_WIDTH / 2
pop de
; if in v/hblank, wait until not in v/hblank
.loop\@
ld a, [$ff00+c]
and b
jr nz, .loop\@
; load BGMap0
ld [hl], e
inc l
ld [hl], d
inc l
endr
ld de, $20 - SCREEN_WIDTH
add hl, de
ld a, [hTilesPerCycle]
dec a
ld [hTilesPerCycle], a
jr nz, .loop
ld a, [hSPBuffer]
ld l, a
ld a, [hSPBuffer + 1]
ld h, a
ld sp, hl
ret
; 32f9
SetPalettes:: ; 32f9
; Inits the Palettes
; depending on the system the monochromes palettes or color palettes
push de
ld a, %11100100
call DmgToCgbBGPals
lb de, %11100100, %11100100
call DmgToCgbObjPals
pop de
ret
; 3317
ClearPalettes:: ; 3317
; Make all palettes white
ld a, [rSVBK]
push af
ld a, BANK(BGPals)
ld [rSVBK], a
; Fill BGPals and OBPals with $ffff (white)
ld hl, BGPals
if !DEF(MONOCHROME)
ld bc, 16 palettes
ld a, $ff
call ByteFill
else
ld b, (16 palettes) / 2
.mono_loop
ld a, PAL_MONOCHROME_WHITE % $100
ld [hli], a
ld a, PAL_MONOCHROME_WHITE / $100
ld [hli], a
dec b
jr nz, .mono_loop
endc
pop af
ld [rSVBK], a
; Request palette update
ld a, 1
ld [hCGBPalUpdate], a
ret
; 333e
GetMemCGBLayout:: ; 333e
ld b, CGB_RAM
GetCGBLayout:: ; 3340
predef_jump Predef_LoadCGBLayout
; 334e
SetHPPal:: ; 334e
; Set palette for hp bar pixel length e at hl.
call GetHPPal
ld [hl], d
ret
; 3353
GetHPPal:: ; 3353
; Get palette for hp bar pixel length e in d.
ld d, HP_GREEN
ld a, e
cp (50 * 48 / 100)
ret nc
inc d ; HP_YELLOW
cp (21 * 48 / 100)
ret nc
inc d ; HP_RED
ret
; 335f
CountSetBits:: ; 0x335f
; Count the number of set bits in b bytes starting from hl.
; Return in a, c and [wd265].
ld c, 0
.next
ld a, [hli]
ld e, a
ld d, 8
.count
srl e
ld a, 0 ; not xor a; preserve carry flag?
adc c
ld c, a
dec d
jr nz, .count
dec b
jr nz, .next
ld a, c
ld [wd265], a
ret
; 0x3376
GetWeekday:: ; 3376
ld a, [CurDay]
.mod
sub 7
jr nc, .mod
add 7
ret
; 3380
INCLUDE "home/pokedex_flags.asm"
NamesPointers:: ; 33ab
dba PokemonNames
dba MoveNames
dba ApricornNames
dba ItemNames
dbw 0, PartyMonOT
dbw 0, OTPartyMonOT
dba TrainerClassNames
; 33c0
GetName:: ; 33c3
; Return name CurSpecies from name list wNamedObjectTypeBuffer in StringBuffer1.
ld a, [hROMBank]
push af
push hl
push bc
push de
ld a, [wNamedObjectTypeBuffer]
cp PKMN_NAME
jr nz, .NotPokeName
ld a, [CurSpecies]
ld [wNamedObjectIndexBuffer], a
call GetPokemonName
ld hl, PKMN_NAME_LENGTH
add hl, de
ld e, l
ld d, h
jr .done
.NotPokeName:
ld a, [wNamedObjectTypeBuffer]
dec a
ld e, a
ld d, 0
ld hl, NamesPointers
add hl, de
add hl, de
add hl, de
ld a, [hli]
rst Bankswitch
ld a, [hli]
ld h, [hl]
ld l, a
ld a, [CurSpecies]
dec a
call GetNthString
ld de, StringBuffer1
ld bc, ITEM_NAME_LENGTH
call CopyBytes
.done
pop de
pop bc
pop hl
pop af
rst Bankswitch
ret
; 3411
GetNthString:: ; 3411
; Return the address of the
; ath string starting from hl.
and a
ret z
push bc
ld b, a
.readChar
ld a, [hli]
cp "@"
jr nz, .readChar
dec b
jr nz, .readChar
pop bc
ret
; 3420
GetBasePokemonName:: ; 3420
; Discards gender (Nidoran).
push hl
call GetPokemonName
ld hl, StringBuffer1
.loop
ld a, [hl]
cp "@"
jr z, .quit
cp "♂"
jr z, .end
cp "♀"
jr z, .end
inc hl
jr .loop
.end
ld [hl], "@"
.quit
pop hl
ret
; 343b
GetPokemonName:: ; 343b
; Get Pokemon name wNamedObjectIndexBuffer.
ld a, [hROMBank]
push af
push hl
ld a, BANK(PokemonNames)
rst Bankswitch
; Each name is ten characters
ld a, [wNamedObjectIndexBuffer]
dec a
ld d, 0
ld e, a
ld h, 0
ld l, a
add hl, hl ; hl = hl * 4
add hl, hl ; hl = hl * 4
add hl, de ; hl = (hl*4) + de
add hl, hl ; hl = (5*hl) + (5*hl)
ld de, PokemonNames
add hl, de
; Terminator
ld de, StringBuffer1
push de
ld bc, PKMN_NAME_LENGTH - 1
call CopyBytes
ld hl, StringBuffer1 + PKMN_NAME_LENGTH - 1
ld [hl], "@"
pop de
pop hl
pop af
rst Bankswitch
ret
; 3468
GetItemName:: ; 3468
; Get item name wNamedObjectIndexBuffer.
push hl
push bc
ld a, [wNamedObjectIndexBuffer]
ld [CurSpecies], a
ld a, ITEM_NAME
ld [wNamedObjectTypeBuffer], a
call GetName
ld de, StringBuffer1
pop bc
pop hl
ret
; 3487
GetApricornName::
; Get apricorn name wNamedObjectIndexBuffer.
push hl
push bc
ld a, [wNamedObjectIndexBuffer]
ld [CurSpecies], a
ld a, APRICORN_NAME
ld [wNamedObjectTypeBuffer], a
call GetName
ld de, StringBuffer1
pop bc
pop hl
ret
GetTMHMName:: ; 3487
; Get TM/HM name by item id wNamedObjectIndexBuffer.
push hl
push de
push bc
ld a, [wNamedObjectIndexBuffer]
push af
; TM/HM prefix
cp HM01
push af
jr c, .TM
ld hl, .HMText
ld bc, .HMTextEnd - .HMText
jr .asm_34a1
.TM:
ld hl, .TMText
ld bc, .TMTextEnd - .TMText
.asm_34a1
ld de, StringBuffer1
call CopyBytes
; TM/HM number
ld a, [wNamedObjectIndexBuffer]
ld c, a
; HM numbers start from 51, not 1
pop af
ld a, c
jr c, .asm_34b9
sub NUM_TMS
.asm_34b9
inc a
; Divide and mod by 10 to get the top and bottom digits respectively
ld b, "0"
.mod10
sub 10
jr c, .asm_34c2
inc b
jr .mod10
.asm_34c2
add 10
push af
ld a, b
ld [de], a
inc de
pop af
ld b, "0"
add b
ld [de], a
; End the string
inc de
ld a, "@"
ld [de], a
pop af
ld [wNamedObjectIndexBuffer], a
pop bc
pop de
pop hl
ld de, StringBuffer1
ret
.TMText:
db "TM"
.TMTextEnd:
db "@"
.HMText:
db "HM"
.HMTextEnd:
db "@"
; 34df
IsHM:: ; 34df
cp HM01
jr c, .NotHM
scf
ret
.NotHM:
and a
ret
; 34e7
IsHMMove:: ; 34e7
ld hl, .HMMoves
ld de, 1
jp IsInArray
.HMMoves:
db CUT
db FLY
db SURF
db STRENGTH
db WATERFALL
db DIVE
db -1
; 34f8
ItemIsMail:: ; b9e76
ld a, d
cp FLOWER_MAIL
ccf
ret
; b9e80
GetMoveName:: ; 34f8
push hl
ld a, MOVE_NAME
ld [wNamedObjectTypeBuffer], a
ld a, [wNamedObjectIndexBuffer] ; move id
ld [CurSpecies], a
call GetName
ld de, StringBuffer1
pop hl
ret
; 350c
ScrollingMenu:: ; 350c
call CopyMenuData2
ld a, [hROMBank]
push af
ld a, BANK(_ScrollingMenu)
rst Bankswitch
call _InitScrollingMenu
call .UpdatePalettes
call _ScrollingMenu
pop af
rst Bankswitch
ld a, [wMenuJoypad]
ret
; 3524
.UpdatePalettes: ; 3524
ld hl, VramState
bit 0, [hl]
jp nz, UpdateTimePals
jp SetPalettes
; 352f
InitScrollingMenu:: ; 352f
ld a, [wMenuBorderTopCoord]
dec a
ld b, a
ld a, [wMenuBorderBottomCoord]
sub b
ld d, a
ld a, [wMenuBorderLeftCoord]