-
Notifications
You must be signed in to change notification settings - Fork 8
/
home.asm
4923 lines (4581 loc) · 97.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
SECTION "rst 00", ROM0 [$00]
rst $38
RefreshMapColorsScrolling:
push af
ld a,$2f
jp CallToBank
SECTION "rst8", ROM0 [$8]
rst $38
_RefreshMapColors:
push af
ld a,BANK(RefreshMapColors)
jp CallToBank
; HAX: rst10 is used for the vblank hook
SECTION "rst 10", ROM0 [$10]
ld b, BANK(GbcVBlankHook)
ld hl, GbcVBlankHook
jp Bankswitch
; HAX: rst18 can be used for "Bankswitch"
SECTION "rst18", ROM0[$18]
jp Bankswitch
IsOakable EQU $20
SECTION "rst 20", ROM0 [IsOakable]
; zero if pokedex is completed and gym rematches are done
ld a, [wRoamingFlag]
and OAKABLE
cp OAKABLE
ret nz ; fall through rst $28
IsAllGymRematchesDone EQU $28
SECTION "rst 28", ROM0 [IsAllGymRematchesDone]
ld a, [wRematchFlag]
cp $ff
ret
OverwriteRoster EQU $30
SECTION "rst 30", ROM0 [OverwriteRoster]
jpba _OverwriteRoster
SECTION "rst 38", ROM0 [$38]
di
stop
jr @
; Hardware interrupts
SECTION "vblank", ROM0 [$40]
push hl
ld hl, VBlank
jp InterruptWrapper
SECTION "hblank", ROM0 [$48]
push hl
ld hl, _GbcPrepareVBlank
jp InterruptWrapper
SECTION "timer", ROM0 [$50]
push hl
ld hl, Timer
jp InterruptWrapper
SECTION "serial", ROM0 [$58]
push hl
ld hl, Serial
jp InterruptWrapper
SECTION "joypad", ROM0 [$60]
reti
SECTION "Home", ROM0
DisableLCD::
xor a
ld [rIF], a
ld a, [rIE]
ld b, a
res 0, a
ld [rIE], a
.wait
ld a, [rLY]
cp LY_VBLANK
jr nz, .wait
ld a, [rLCDC]
and $ff ^ rLCDC_ENABLE_MASK
ld [rLCDC], a
ld a, b
ld [rIE], a
ret
EnableLCD::
ld a, [rLCDC]
set rLCDC_ENABLE, a
ld [rLCDC], a
ret
ClearSprites::
xor a
ld hl, wOAMBuffer
ld b, 40 * 4
.loop
ld [hli], a
dec b
jr nz, .loop
ret
HideSprites::
ld a, 160
ld hl, wOAMBuffer
ld de, 4
ld b, 40
.loop
ld [hl], a
add hl, de
dec b
jr nz, .loop
ret
INCLUDE "home/copy.asm"
HandleRematch:
callba _HandleRematch
jpba CheckRanOccured
HandleRematchAfter:
callba _HandleRematchAfter
LoadHLMoves:
; forced to Gen 6 by default
ld hl, Gen6Moves
ret
SECTION "Initialization",HOME[$c0]
IsGBC:
ld hl,Start
push hl ; hijack ret
ld a,BANK(InitGbcMode)
ld [$2000],a
jp InitGbcMode
LoadBank1:
xor a
ld [$2000],a
ret
Initialize:
cp $11
jr z,IsGBC
.IsNotGBC
ld a,$30
ld [$2000],a
jr loop3
_ColorOverworldSprites: ; $00dc
push af
ld a,BANK(ColorOverworldSprites)
jr CallToBank
_LoadTilesetPatternsAndPalettes: ; $00e1
push af
ld a, BANK(LoadTilesetPalette)
loop3
ld [$2000],a
call $00f5
pop af
call LoadTilesetTilePatternData
ret
nop
nop
CallToBank: ; $00f1
ld [$2000],a
pop af
push af
call $6000
ld a,[$ff00+$b8]
ld [$2000],a
pop af
ret
SECTION "Entry", ROM0[$100]
jp Initialize
SECTION "Header", ROM0 [$104]
; The header is generated by rgbfix.
; The space here is allocated to prevent code from being overwritten.
ds $150 - $104
SECTION "Main", ROM0
Start::
cp GBC
Start_2:
jr z, .gbc
xor a
jr .ok
.gbc
ld a, 0
.ok
ld [wGBC], a
jp Init
INCLUDE "home/joypad.asm"
INCLUDE "data/map_header_pointers.asm"
INCLUDE "home/overworld.asm"
CheckForUserInterruption:: ; 12f8 (0:12f8)
; Return carry if Up+Select+B, Start or A are pressed in c frames.
; Used only in the intro and title screen.
call DelayFrame
push bc
call JoypadLowSensitivity
pop bc
ld a, [hJoyHeld]
cp D_UP + SELECT + B_BUTTON
jr z, .input
ld a, [hJoy5]
and START | A_BUTTON
jr nz, .input
dec c
jr nz, CheckForUserInterruption
and a
ret
.input
scf
ret
; function to load position data for destination warp when switching maps
; INPUT:
; a = ID of destination warp within destination map
LoadDestinationWarpPosition:: ; 1313 (0:1313)
ld b,a
ld a,[H_LOADEDROMBANK]
push af
ld a,[wPredefParentBank]
ld [H_LOADEDROMBANK],a
ld [$2000],a
ld a,b
add a
add a
ld c,a
ld b,0
add hl,bc
ld bc,4
ld de,wCurrentTileBlockMapViewPointer
call CopyData
pop af
ld [H_LOADEDROMBANK],a
ld [$2000],a
ret
DrawHPBar:: ; 1336 (0:1336)
; Draw an HP bar d tiles long, and fill it to e pixels.
; If c is nonzero, show at least a sliver regardless.
; The right end of the bar changes with [wListMenuID].
push hl
push de
push bc
; Left
ld a, $71 ; "HP:"
ld [hli], a
ld a, $62
ld [hli], a
push hl
; Middle
ld a, $63 ; empty
.draw
ld [hli],a
dec d
jr nz, .draw
; Right
ld a,[wListMenuID]
dec a
ld a, $6d ; status screen and battle
jr z, .ok
dec a ; pokemon menu
.ok
ld [hl],a
pop hl
ld a, e
and a
jr nz, .fill
; If c iz nonzero, draw a pixel anyway.
ld a, c
and a
jr z, .done
ld e, 1
.fill
ld a, e
sub 8
jr c, .partial
ld e, a
ld a, $6b ; full
ld [hli], a
ld a, e
and a
jr z, .done
jr .fill
.partial
; Fill remaining pixels at the end if necessary.
ld a, $63 ; empty
add e
ld [hl], a
.done
pop bc
pop de
pop hl
ret
; loads pokemon data from one of multiple sources to wcf98
; loads base stats to W_MONHDEXNUM
; INPUT:
; [wWhichPokemon] = index of pokemon within party/box
; [wcc49] = source
; 00: player's party
; 01: enemy's party
; 02: current box
; 03: daycare
; OUTPUT:
; [wcf91] = pokemon ID
; wcf98 = base address of pokemon data
; W_MONHDEXNUM = base address of base stats
LoadMonData:: ; 1372 (0:1372)
ld hl, LoadMonData_
ld b, BANK(LoadMonData_)
jp Bankswitch
Func_137a:: ; 137a (0:137a)
; Write c to [wd0dc + b]. Unused.
ld hl, wd0dc
ld e, b
ld d, 0
add hl, de
ld a, c
ld [hl], a
ret
LoadFlippedFrontSpriteByMonIndex:: ; 1384 (0:1384)
ld a, 1
ld [W_SPRITEFLIPPED], a
LoadFrontSpriteByMonIndex:: ; 1389 (0:1389)
push hl
ld a, [wd11e]
push af
ld a, [wcf91]
ld [wd11e], a
predef IndexToPokedex
ld hl, wd11e
ld a, [hl]
pop bc
ld [hl], b
and a
pop hl
jr z, .invalidDexNumber ; dex #0 invalid
cp NUM_POKEMON + 1
jr c, .validDexNumber ; dex >#151 invalid
.invalidDexNumber
ld a, RHYDON ; $1
ld [wcf91], a
ret
.validDexNumber
push hl
ld de, vFrontPic
call LoadMonFrontSprite
pop hl
ld a, [H_LOADEDROMBANK]
push af
ld a, Bank(asm_3f0d0)
ld [H_LOADEDROMBANK], a
ld [$2000], a
xor a
ld [$ffe1], a
call asm_3f0d0
xor a
ld [W_SPRITEFLIPPED], a
pop af
ld [H_LOADEDROMBANK], a
ld [$2000], a
ret
; PlayCry
GetCryData:: ; 13d9 (0:13d9)
; Load cry data for monster a.
dec a
ld c, a
ld b, 0
ld hl, CryData
add hl, bc
add hl, bc
add hl, bc
ld a, Bank(CryData)
call BankswitchHome
ld a, [hli]
ld b, a ; cry id
ld a, [hli]
ld [wc0f1], a
ld a, [hl]
ld [wc0f2], a
call BankswitchBack
; Cry headers have 3 channels,
; and start from index $14,
; so add 3 times the cry id.
ld a, b
ld c, $14
rlca ; * 2
add b
add c
ret
DisplayPartyMenu:: ; 13fc (0:13fc)
ld a,[hTilesetType]
push af
xor a
ld [hTilesetType],a
call GBPalWhiteOutWithDelay3
call ClearSprites
call PartyMenuInit
call DrawPartyMenu
jp HandlePartyMenuInput
GoBackToPartyMenu:: ; 1411 (0:1411)
ld a,[hTilesetType]
push af
xor a
ld [hTilesetType],a
call PartyMenuInit
call RedrawPartyMenu
jp HandlePartyMenuInput
PartyMenuInit:: ; 1420 (0:1420)
ld a, 1 ; hardcoded bank
call BankswitchHome
call LoadHpBarAndStatusTilePatterns
ld hl, wd730
set 6, [hl] ; turn off letter printing delay
xor a
ld [wcc49], a
ld [wcc37], a
ld hl, wTopMenuItemY
inc a
ld [hli], a ; top menu item Y
xor a
ld [hli], a ; top menu item X
ld a, [wcc2b]
push af
ld [hli], a ; current menu item ID
inc hl
ld a, [wPartyCount]
and a ; are there more than 0 pokemon in the party?
jr z, .storeMaxMenuItemID
dec a
; if party is not empty, the max menu item ID is ([wPartyCount] - 1)
; otherwise, it is 0
.storeMaxMenuItemID
ld [hli], a ; max menu item ID
ld a, [wd11f]
and a
ld a, A_BUTTON + B_BUTTON
jr z, .next
xor a
ld [wd11f], a
inc a
.next
ld [hli], a ; menu watched keys
pop af
ld [hl], a ; old menu item ID
ret
HandlePartyMenuInput:: ; 145a (0:145a)
ld a,1
ld [wMenuWrappingEnabled],a
ld a,$40
ld [wd09b],a
call HandleMenuInputPokemonSelection
call PlaceUnfilledArrowMenuCursor
ld b,a
xor a
ld [wd09b],a
ld a,[wCurrentMenuItem]
ld [wcc2b],a
ld hl,wd730
res 6,[hl] ; turn on letter printing delay
ld a,[wMenuItemToSwap]
and a
jp nz,.swappingPokemon
pop af
ld [hTilesetType],a
bit 1,b
jr nz,.noPokemonChosen
ld a,[wPartyCount]
and a
jr z,.noPokemonChosen
ld a,[wCurrentMenuItem]
ld [wWhichPokemon],a
ld hl,wPartySpecies
ld b,0
ld c,a
add hl,bc
ld a,[hl]
ld [wcf91],a
ld [wBattleMonSpecies2],a
call BankswitchBack
and a
ret
.noPokemonChosen
call BankswitchBack
scf
ret
.swappingPokemon
bit 1,b ; was the B button pressed?
jr z,.handleSwap ; if not, handle swapping the pokemon
.cancelSwap ; if the B button was pressed
callba ErasePartyMenuCursors
xor a
ld [wMenuItemToSwap],a
ld [wd07d],a
call RedrawPartyMenu
jr HandlePartyMenuInput
.handleSwap
ld a,[wCurrentMenuItem]
ld [wWhichPokemon],a
callba SwitchPartyMon
jr HandlePartyMenuInput
DrawPartyMenu:: ; 14d4 (0:14d4)
ld hl, DrawPartyMenu_
jr DrawPartyMenuCommon
RedrawPartyMenu:: ; 14d9 (0:14d9)
ld hl, RedrawPartyMenu_
DrawPartyMenuCommon:: ; 14dc (0:14dc)
ld b, BANK(RedrawPartyMenu_)
jp Bankswitch
; prints a pokemon's status condition
; INPUT:
; de = address of status condition
; hl = destination address
PrintStatusCondition:: ; 14e1 (0:14e1)
push de
dec de
dec de ; de = address of current HP
ld a,[de]
ld b,a
dec de
ld a,[de]
or b ; is the pokemon's HP zero?
pop de
jr nz,PrintStatusConditionNotFainted
; if the pokemon's HP is 0, print "FNT"
ld a,"F"
ld [hli],a
ld a,"N"
ld [hli],a
ld [hl],"T"
and a
ret
PrintStatusConditionNotFainted ; 14f6
ld a,[H_LOADEDROMBANK]
push af
ld a,BANK(PrintStatusAilment)
ld [H_LOADEDROMBANK],a
ld [$2000],a
call PrintStatusAilment ; print status condition
pop bc
ld a,b
ld [H_LOADEDROMBANK],a
ld [$2000],a
ret
; function to print pokemon level, leaving off the ":L" if the level is at least 100
; INPUT:
; hl = destination address
; [wcfb9] = level
PrintLevel:: ; 150b (0:150b)
ld a,$6e ; ":L" tile ID
ld [hli],a
ld c,2 ; number of digits
ld a,[wcfb9] ; level
cp a,100
jr c,PrintLevelCommon
; if level at least 100, write over the ":L" tile
dec hl
inc c ; increment number of digits to 3
jr PrintLevelCommon
; prints the level without leaving off ":L" regardless of level
; INPUT:
; hl = destination address
; [wcfb9] = level
PrintLevelFull:: ; 151b (0:151b)
ld a,$6e ; ":L" tile ID
ld [hli],a
ld c,3 ; number of digits
ld a,[wcfb9] ; level
PrintLevelCommon:: ; 1523 (0:1523)
ld [wd11e],a
ld de,wd11e
ld b,$41 ; no leading zeroes, left-aligned, one byte
jp PrintNumber
Func_152e:: ; 152e (0:152e)
; Unused.
ld hl,wd0dc
ld c,a
ld b,0
add hl,bc
ld a,[hl]
ret
; copies the base stat data of a pokemon to W_MONHDEXNUM (W_MONHEADER)
; INPUT:
; [wd0b5] = pokemon ID
GetMonHeader:: ; 1537 (0:1537)
ld a,[H_LOADEDROMBANK]
push af
ld a,BANK(BaseStats)
ld [H_LOADEDROMBANK],a
ld [$2000],a
push bc
push de
push hl
ld a,[wd11e]
push af
ld a,[wd0b5]
ld [wd11e],a
ld de,GhostPic
ld b,$66 ; size of Ghost sprite
cp a,MON_GHOST ; Ghost
jr z,.specialID
predef IndexToPokedex ; convert pokemon ID in [wd11e] to pokedex number
ld a,[wd11e]
dec a
ld bc,28
ld hl,BaseStats
call AddNTimes
ld de,W_MONHEADER
ld bc,28
call CopyData
jr .done
.specialID
ld hl,W_MONHSPRITEDIM
ld [hl],b ; write sprite dimensions
inc hl
ld [hl],e ; write front sprite pointer
inc hl
ld [hl],d
jr .done
.done
ld a,[wd0b5]
ld [W_MONHDEXNUM],a
pop af
ld [wd11e],a
pop hl
pop de
pop bc
pop af
ld [H_LOADEDROMBANK],a
ld [$2000],a
ret
; copy party pokemon's name to wcd6d
GetPartyMonName2:: ; 15b4 (0:15b4)
ld a,[wWhichPokemon] ; index within party
ld hl,wPartyMonNicks
; this is called more often
GetPartyMonName:: ; 15ba (0:15ba)
push hl
push bc
call SkipFixedLengthTextEntries ; add 11 to hl, a times
ld de,wcd6d
push de
ld bc,11
call CopyData
pop de
pop bc
pop hl
ret
; function to print a BCD (Binary-coded decimal) number
; de = address of BCD number
; hl = destination address
; c = flags and length
; bit 7: if set, do not print leading zeroes
; if unset, print leading zeroes
; bit 6: if set, left-align the string (do not pad empty digits with spaces)
; if unset, right-align the string
; bit 5: if set, print currency symbol at the beginning of the string
; if unset, do not print the currency symbol
; bits 0-4: length of BCD number in bytes
; Note that bits 5 and 7 are modified during execution. The above reflects
; their meaning at the beginning of the functions's execution.
PrintBCDNumber:: ; 15cd (0:15cd)
ld b,c ; save flags in b
res 7,c
res 6,c
res 5,c ; c now holds the length
bit 5,b
jr z,.loop
bit 7,b
jr nz,.loop
ld [hl],"¥"
inc hl
.loop
ld a,[de]
swap a
call PrintBCDDigit ; print upper digit
ld a,[de]
call PrintBCDDigit ; print lower digit
inc de
dec c
jr nz,.loop
bit 7,b ; were any non-zero digits printed?
jr z,.done ; if so, we are done
.numberEqualsZero ; if every digit of the BCD number is zero
bit 6,b ; left or right alignment?
jr nz,.skipRightAlignmentAdjustment
dec hl ; if the string is right-aligned, it needs to be moved back one space
.skipRightAlignmentAdjustment
bit 5,b
jr z,.skipCurrencySymbol
ld [hl],"¥"
inc hl
.skipCurrencySymbol
ld [hl],"0"
call PrintLetterDelay
inc hl
.done
ret
PrintBCDDigit:: ; 1604 (0:1604)
and $f
and a
jr z,.zeroDigit
.nonzeroDigit
bit 7,b ; have any non-space characters been printed?
jr z,.outputDigit
; if bit 7 is set, then no numbers have been printed yet
bit 5,b ; print the currency symbol?
jr z,.skipCurrencySymbol
ld [hl],"¥"
inc hl
res 5,b
.skipCurrencySymbol
res 7,b ; unset 7 to indicate that a nonzero digit has been reached
.outputDigit
add a,"0"
ld [hli],a
jp PrintLetterDelay
.zeroDigit
bit 7,b ; either printing leading zeroes or already reached a nonzero digit?
jr z,.outputDigit ; if so, print a zero digit
bit 6,b ; left or right alignment?
ret nz
inc hl ; if right-aligned, "print" a space by advancing the pointer
ret
; uncompresses the front or back sprite of the specified mon
; assumes the corresponding mon header is already loaded
; hl contains offset to sprite pointer ($b for front or $d for back)
UncompressMonSprite:: ; 1627 (0:1627)
ld bc,W_MONHEADER
add hl,bc
ld a,[hli]
ld [W_SPRITEINPUTPTR],a ; fetch sprite input pointer
ld a,[hl]
ld [W_SPRITEINPUTPTR+1],a
; define (by index number) the bank that a pokemon's image is in
; index = Mew, bank 1
; index = Kabutops fossil, bank $B
; index < $1F, bank 9
; $1F ≤ index < $4A, bank $A
; $4A ≤ index < $74, bank $B
; $74 ≤ index < $99, bank $C
; $99 ≤ index, bank $D
;ld a,[wcf91] ; XXX name for this ram location
ld a,[wcf91]
ld b, a
ld a, b
cp MON_GHOST
jr nz, .next0
ld a, BANK(GhostPic)
jr .GotBank
.next0
ld a, [W_MONSPRITEBANK]
.GotBank
jp UncompressSpriteData
; de: destination location
LoadMonFrontSprite:: ; 1665 (0:1665)
push de
ld hl, W_MONHFRONTSPRITE - W_MONHEADER
call UncompressMonSprite
ld hl, W_MONHSPRITEDIM
ld a, [hli]
LoadUncompressedBackSprite:
ld c, a
pop de
; fall through
; postprocesses uncompressed sprite chunks to a 2bpp sprite and loads it into video ram
; calculates alignment parameters to place both sprite chunks in the center of the 7*7 tile sprite buffers
; de: destination location
; a,c: sprite dimensions (in tiles of 8x8 each)
LoadUncompressedSpriteData:: ; 1672 (0:1672)
push de
and $f
ld [H_SPRITEWIDTH], a ; each byte contains 8 pixels (in 1bpp), so tiles=bytes for width
ld b, a
ld a, $7
sub b ; 7-w
inc a ; 8-w
srl a ; (8-w)/2 ; horizontal center (in tiles, rounded up)
ld b, a
add a
add a
add a
sub b ; 7*((8-w)/2) ; skip for horizontal center (in tiles)
ld [H_SPRITEOFFSET], a
ld a, c
swap a
and $f
ld b, a
add a
add a
add a ; 8*tiles is height in bytes
ld [H_SPRITEHEIGHT], a ; $ff8c
ld a, $7
sub b ; 7-h ; skip for vertical center (in tiles, relative to current column)
ld b, a
ld a, [H_SPRITEOFFSET]
add b ; 7*((8-w)/2) + 7-h ; combined overall offset (in tiles)
add a
add a
add a ; 8*(7*((8-w)/2) + 7-h) ; combined overall offset (in bytes)
ld [H_SPRITEOFFSET], a
xor a
ld [$4000], a
ld hl, S_SPRITEBUFFER0
call ZeroSpriteBuffer ; zero buffer 0
ld de, S_SPRITEBUFFER1
ld hl, S_SPRITEBUFFER0
call AlignSpriteDataCentered ; copy and align buffer 1 to 0 (containing the MSB of the 2bpp sprite)
ld hl, S_SPRITEBUFFER1
call ZeroSpriteBuffer ; zero buffer 1
ld de, S_SPRITEBUFFER2
ld hl, S_SPRITEBUFFER1
call AlignSpriteDataCentered ; copy and align buffer 2 to 1 (containing the LSB of the 2bpp sprite)
pop de
jp InterlaceMergeSpriteBuffers
; copies and aligns the sprite data properly inside the sprite buffer
; sprite buffers are 7*7 tiles in size, the loaded sprite is centered within this area
AlignSpriteDataCentered:: ; 16c2 (0:16c2)
ld a, [H_SPRITEOFFSET]
ld b, $0
ld c, a
add hl, bc
ld a, [H_SPRITEWIDTH] ; $ff8b
.columnLoop
push af
push hl
ld a, [H_SPRITEHEIGHT] ; $ff8c
ld c, a
.columnInnerLoop
ld a, [de]
inc de
ld [hli], a
dec c
jr nz, .columnInnerLoop
pop hl
ld bc, 7*8 ; 7 tiles
add hl, bc ; advance one full column
pop af
dec a
jr nz, .columnLoop
ret
; fills the sprite buffer (pointed to in hl) with zeros
ZeroSpriteBuffer:: ; 16df (0:16df)
ld bc, SPRITEBUFFERSIZE
.nextByteLoop
xor a
ld [hli], a
dec bc
ld a, b
or c
jr nz, .nextByteLoop
ret
; combines the (7*7 tiles, 1bpp) sprite chunks in buffer 0 and 1 into a 2bpp sprite located in buffer 1 through 2
; in the resulting sprite, the rows of the two source sprites are interlaced
; de: output address
InterlaceMergeSpriteBuffers:: ; 16ea (0:16ea)
xor a
ld [$4000], a
push de
ld hl, S_SPRITEBUFFER2 + (SPRITEBUFFERSIZE - 1) ; destination: end of buffer 2
ld de, S_SPRITEBUFFER1 + (SPRITEBUFFERSIZE - 1) ; source 2: end of buffer 1
ld bc, S_SPRITEBUFFER0 + (SPRITEBUFFERSIZE - 1) ; source 1: end of buffer 0
ld a, SPRITEBUFFERSIZE/2 ; $c4
ld [H_SPRITEINTERLACECOUNTER], a ; $ff8b
.interlaceLoop
ld a, [de]
dec de
ld [hld], a ; write byte of source 2
ld a, [bc]
dec bc
ld [hld], a ; write byte of source 1
ld a, [de]
dec de
ld [hld], a ; write byte of source 2
ld a, [bc]
dec bc
ld [hld], a ; write byte of source 1
ld a, [H_SPRITEINTERLACECOUNTER] ; $ff8b
dec a
ld [H_SPRITEINTERLACECOUNTER], a ; $ff8b
jr nz, .interlaceLoop
ld a, [W_SPRITEFLIPPED]
and a
jr z, .notFlipped
ld bc, 2*SPRITEBUFFERSIZE
ld hl, S_SPRITEBUFFER1
.swapLoop
swap [hl] ; if flipped swap nybbles in all bytes
inc hl
dec bc
ld a, b
or c
jr nz, .swapLoop
.notFlipped
pop hl
ld de, S_SPRITEBUFFER1
ld c, (2*SPRITEBUFFERSIZE)/16 ; $31, number of 16 byte chunks to be copied
ld a, [H_LOADEDROMBANK]
ld b, a
jp CopyVideoData
INCLUDE "data/collision.asm"
INCLUDE "home/copy2.asm"
INCLUDE "home/text.asm"
INCLUDE "home/init.asm"
INCLUDE "home/vblank.asm"
INCLUDE "home/fade.asm"
INCLUDE "home/serial.asm"
INCLUDE "home/timer.asm"
INCLUDE "home/audio.asm"
UpdateSprites:: ; 2429 (0:2429)
ld a, [wUpdateSpritesEnabled]
dec a
ret nz
ld a, [H_LOADEDROMBANK]
push af
ld a, Bank(_UpdateSprites)
ld [H_LOADEDROMBANK], a
ld [$2000], a
call _UpdateSprites
pop af
ld [H_LOADEDROMBANK], a
ld [$2000], a
ret
TextScriptEndingChar:: ; 24d6 (0:24d6)
db "@"
TextScriptEnd:: ; 24d7 (0:24d7)
ld hl,TextScriptEndingChar
ret
ExclamationText:: ; 24db (0:24db)
TX_FAR _ExclamationText
db "@"
GroundRoseText:: ; 24e0 (0:24e0)
TX_FAR _GroundRoseText