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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
|
_RunPaletteCommand: ; 71ddf (1c:5ddf)
call GetPredefRegisters
ld a, b
cp $ff
jr nz, .next
ld a, [wDefaultPaletteCommand] ; use default command if command ID is $ff
.next
cp UPDATE_PARTY_MENU_BLK_PACKET
jp z, UpdatePartyMenuBlkPacket
ld l, a
ld h, 0
add hl, hl
ld de, SetPalFunctions
add hl, de
ld a, [hli]
ld h, [hl]
ld l, a
ld de, SendSGBPackets
push de
jp [hl]
SetPal_Black: ; 71ed3 (1c:5ed3)
ld hl, PalPacket_Black
ld de, BlkPacket_Battle
ret
; uses PalPacket_Empty to build a packet based on mon IDs and health color
SetPal_Battle: ; 71eda (1c:5eda)
ld hl, PalPacket_Empty
ld de, wPalPacket
ld bc, $10
call CopyData
;ld a, [wPlayerBattleStatus3]
ld hl, wBattleMonSpecies
ld a, [hl]
and a
jr z, .asm_71ef9
ld hl, wPartyMon1
ld a, [wPlayerMonNumber]
ld bc, wPartyMon2 - wPartyMon1
call AddNTimes
.asm_71ef9
call DeterminePaletteID
ld b, a
;ld a, [wEnemyBattleStatus3]
ld hl, wEnemyMonSpecies2
call DeterminePaletteID
ld c, a
ld hl, wPalPacket + 1
ld a, [wPlayerHPBarColor]
add PAL_GREENBAR
ld [hli], a
inc hl
ld a, [wEnemyHPBarColor]
add PAL_GREENBAR
ld [hli], a
inc hl
ld a, b
ld [hli], a
inc hl
ld a, c
ld [hl], a
ld hl, wPalPacket
ld de, BlkPacket_Battle
ld a, SET_PAL_BATTLE
ld [wDefaultPaletteCommand], a
ret
SetPal_TownMap: ; 71f26 (1c:5f26)
ld hl, PalPacket_TownMap
ld de, BlkPacket_WholeScreen
ret
; uses PalPacket_Empty to build a packet based the mon ID
SetPal_StatusScreen: ; 71f2d (1c:5f2d)
ld hl, PalPacket_Empty
ld de, wPalPacket
ld bc, $10
call CopyData
ld a, [wcf91]
cp VICTREEBEL + 1
jr c, .pokemon
ld a, $1 ; not pokemon
.pokemon
call DeterminePaletteIDOutOfBattle
push af
ld hl, wPalPacket + 1
ld a, [wStatusScreenHPBarColor]
add PAL_GREENBAR
ld [hli], a
inc hl
pop af
ld [hl], a
ld hl, wPalPacket
ld de, BlkPacket_StatusScreen
ret
SetPal_PartyMenu: ; 71f59 (1c:5f59)
ld hl, PalPacket_PartyMenu
ld de, wPartyMenuBlkPacket
ret
SetPal_Pokedex: ; 71f60 (1c:5f60)
ld hl, PalPacket_Pokedex
ld de, wPalPacket
ld bc, $10
call CopyData
ld a, [wcf91]
call DeterminePaletteIDOutOfBattle
ld hl, wPalPacket + 3
ld [hl], a
ld hl, wPalPacket
ld de, BlkPacket_Pokedex
ret
SetPal_Slots: ; 71f7d (1c:5f7d)
ld hl, PalPacket_Slots
ld de, BlkPacket_Slots
ret
SetPal_Titlescreen: ; 71f84 (1c:5f84)
ld hl, PalPacket_Titlescreen
ld de, BlkPacket_Titlescreen
ret
; used mostly for menus and the Oak intro
SetPal_Generic: ; 71f8b (1c:5f8b)
ld hl, PalPacket_Generic
ld de, BlkPacket_WholeScreen
ret
SetPal_NidorinoIntro: ; 71f92 (1c:5f92)
ld hl, PalPacket_NidorinoIntro
ld de, BlkPacket_NidorinoIntro
ret
SetPal_GameFreakIntro: ; 71f99 (1c:5f99)
ld hl, PalPacket_GameFreakIntro
ld de, BlkPacket_GameFreakIntro
ld a, SET_PAL_GENERIC
ld [wDefaultPaletteCommand], a
ret
; uses PalPacket_Empty to build a packet based on the current map
SetPal_Overworld: ; 71fa5 (1c:5fa5)
ld hl, PalPacket_Empty
ld de, wPalPacket
ld bc, $10
call CopyData
ld a, [wCurMapTileset]
cp CEMETERY
jr z, .PokemonTowerOrAgatha
cp CAVERN
jr z, .caveOrBruno
ld a, [wCurMap]
cp REDS_HOUSE_1F
jr c, .townOrRoute
cp UNKNOWN_DUNGEON_2
jr c, .normalDungeonOrBuilding
cp NAME_RATERS_HOUSE
jr c, .caveOrBruno
cp LORELEIS_ROOM
jr z, .Lorelei
cp BRUNOS_ROOM
jr z, .caveOrBruno
cp TRADE_CENTER
jr z, .trade_center_colosseum
cp COLOSSEUM
jr z, .trade_center_colosseum
.normalDungeonOrBuilding
ld a, [wLastMap] ; town or route that current dungeon or building is located
.townOrRoute
cp SAFFRON_CITY + 1
jr c, .town
ld a, PAL_ROUTE - 1
.town
inc a ; a town's palette ID is its map ID + 1
ld hl, wPalPacket + 1
ld [hld], a
ld de, BlkPacket_WholeScreen
ld a, SET_PAL_OVERWORLD
ld [wDefaultPaletteCommand], a
ret
.PokemonTowerOrAgatha
ld a, PAL_GREYMON - 1
jr .town
.caveOrBruno
ld a, PAL_CAVE - 1
jr .town
.Lorelei
xor a
jr .town
.trade_center_colosseum
ld a, PAL_GREYMON - 1
jr .town
; used when a Pokemon is the only thing on the screen
; such as evolution, trading and the Hall of Fame
SetPal_PokemonWholeScreen: ; 72001 (1c:6001)
push bc
ld hl, PalPacket_Empty
ld de, wPalPacket
ld bc, $10
call CopyData
pop bc
ld a, c
and a
ld a, PAL_BLACK
jr nz, .next
ld a, [wWholeScreenPaletteMonSpecies]
call DeterminePaletteIDOutOfBattle
.next
ld [wPalPacket + 1], a
ld hl, wPalPacket
ld de, BlkPacket_WholeScreen
ret
SetPal_TrainerCard: ; 72025 (1c:6025)
ld hl, BlkPacket_TrainerCard
ld de, wTrainerCardBlkPacket
ld bc, $40
call CopyData
ld de, BadgeBlkDataLengths
ld hl, wTrainerCardBlkPacket + 2
ld a, [wObtainedBadges]
ld c, 8
.badgeLoop
srl a
push af
jr c, .haveBadge
; The player doens't have the badge, so zero the badge's blk data.
push bc
ld a, [de]
ld c, a
xor a
.zeroBadgeDataLoop
ld [hli], a
dec c
jr nz, .zeroBadgeDataLoop
pop bc
jr .nextBadge
.haveBadge
; The player does have the badge, so skip past the badge's blk data.
ld a, [de]
.skipBadgeDataLoop
inc hl
dec a
jr nz, .skipBadgeDataLoop
.nextBadge
pop af
inc de
dec c
jr nz, .badgeLoop
ld hl, PalPacket_TrainerCard
ld de, wTrainerCardBlkPacket
ret
SendUnknownPalPacket_7205d:: ; 7205d (1c:605d)
ld hl, UnknownPalPacket_72811
ld de, BlkPacket_WholeScreen
ret
SendUnknownPalPacket_72064:: ; 72064 (1c:6064)
ld hl, UnknownPalPacket_72821
ld de, UnknownPacket_72751
ret
SetPalFunctions: ; 7206b (1c:606b)
dw SetPal_Black
dw SetPal_Battle
dw SetPal_TownMap
dw SetPal_StatusScreen
dw SetPal_Pokedex
dw SetPal_Slots
dw SetPal_Titlescreen
dw SetPal_NidorinoIntro
dw SetPal_Generic
dw SetPal_Overworld
dw SetPal_PartyMenu
dw SetPal_PokemonWholeScreen
dw SetPal_GameFreakIntro
dw SetPal_TrainerCard
dw SendUnknownPalPacket_7205d
dw SendUnknownPalPacket_72064
; The length of the blk data of each badge on the Trainer Card.
; The Rainbow Badge has 3 entries because of its many colors.
BadgeBlkDataLengths: ; 7208b (1c:608b)
db 6 ; Boulder Badge
db 6 ; Cascade Badge
db 6 ; Thunder Badge
db 6 * 3 ; Rainbow Badge
db 6 ; Soul Badge
db 6 ; Marsh Badge
db 6 ; Volcano Badge
db 6 ; Earth Badge
DeterminePaletteID: ; 72093 (1c:6093)
ld a, [hl]
DeterminePaletteIDOutOfBattle: ; 72094 (1c:6094)
ld [wd11e], a
and a ; is the mon index 0?
jr z, .skipDexNumConversion
push bc
predef IndexToPokedex
pop bc
ld a, [wd11e]
.skipDexNumConversion
ld e, a
ld d, 0
ld hl, MonsterPalettes ; not just for Pokemon, Trainers use it too
add hl, de
ld a, [hl]
ret
YellowIntroPaletteAction:: ; 720ad (1c:60ad)
ld a, e
and a
jr nz, .asm_720bd
ld hl, PalPacket_Generic
ld a, [hGBC]
and a
jp z, SendSGBPacket
jp InitGBCPalettes
.asm_720bd
ld hl, UnknownPalPacket_72811
ld a, [hGBC]
and a
jp z, SendSGBPacket
call InitGBCPalettes
ld hl, PalPacket_Generic
inc hl
ld a, [hli]
call GetGBCBasePalAddress
ld a, e
ld [wGBCBasePalPointers + 2], a
ld a, d
ld [wGBCBasePalPointers + 2 + 1], a
xor a ; CONVERT_BGP
call DMGPalToGBCPal
ld a, 1
call TransferCurBGPData
ret
LoadOverworldPikachuFrontpicPalettes:: ; 720e3 (1c:60e3)
ld hl, PalPacket_Empty
ld de, wPalPacket
ld bc, $10
call CopyData
call GetPal_Pikachu
ld hl, wPartyMenuBlkPacket
ld [hl], a
ld hl, wPartyMenuBlkPacket + 2
ld a, $26
ld [hl], a
ld hl, wPalPacket
ld a, [hGBC]
and a
jr nz, .cgb_1
call SendSGBPacket
jr .okay_1
.cgb_1
call InitGBCPalettes
.okay_1
ld hl, BlkPacket_WholeScreen
ld de, wPalPacket
ld bc, $10
call CopyData
ld hl, wPartyMenuBlkPacket + 2
ld a, $5
ld [hli], a
ld a, $7
ld [hli], a
ld a, $6
ld [hli], a
ld a, $b
ld [hli], a
ld a, $a
ld [hl], a
ld hl, wPalPacket
ld a, [hGBC]
and a
jr nz, .cgb_2
call SendSGBPacket
jr .okay_2
.cgb_2
call InitGBCPalettes
.okay_2
ret
GetPal_Pikachu:: ; 7213b (1c:613b)
; similar to SetPal_Overworld
ld a, [wCurMapTileset]
cp CEMETERY
jr z, .PokemonTowerOrAgatha
cp CAVERN
jr z, .caveOrBruno
ld a, [wCurMap]
cp REDS_HOUSE_1F
jr c, .townOrRoute
cp UNKNOWN_DUNGEON_2
jr c, .normalDungeonOrBuilding
cp NAME_RATERS_HOUSE
jr c, .caveOrBruno
cp LORELEIS_ROOM
jr z, .Lorelei
cp BRUNOS_ROOM
jr z, .caveOrBruno
cp TRADE_CENTER
jr z, .battleOrTradeCenter
cp COLOSSEUM
jr z, .battleOrTradeCenter
.normalDungeonOrBuilding
ld a, [wLastMap] ; town or route that current dungeon or building is located
.townOrRoute
cp SAFFRON_CITY + 1
jr c, .town
ld a, PAL_ROUTE - 1
.town
inc a ; a town's pallete ID is its map ID + 1
ret
.PokemonTowerOrAgatha
ld a, PAL_GREYMON - 1
jr .town
.caveOrBruno
ld a, PAL_CAVE - 1
jr .town
.Lorelei
xor a ; PAL_PALLET - 1
jr .town
.battleOrTradeCenter
ld a, PAL_GREYMON - 1
jr .town
InitPartyMenuBlkPacket: ; 7217f (1c:617f)
ld hl, BlkPacket_PartyMenu
ld de, wPartyMenuBlkPacket
ld bc, $30
jp CopyData
UpdatePartyMenuBlkPacket: ; 7218b (1c:618b)
; Update the blk packet with the palette of the HP bar that is
; specified in [wWhichPartyMenuHPBar].
ld hl, wPartyMenuHPBarColors
ld a, [wWhichPartyMenuHPBar]
ld e, a
ld d, 0
add hl, de
ld e, l
ld d, h
ld a, [de]
and a
ld e, (1 << 2) | 1 ; green
jr z, .next
dec a
ld e, (2 << 2) | 2 ; yellow
jr z, .next
ld e, (3 << 2) | 3 ; red
.next
push de
ld hl, wPartyMenuBlkPacket + 8 + 1
ld bc, 6
ld a, [wWhichPartyMenuHPBar]
call AddNTimes
pop de
ld [hl], e
ret
SendSGBPacket: ; 721b4 (1c:61b4)
ld a, 1
ld [hDisableJoypadPolling], a ; don't poll joypad while sending packet
call _SendSGBPacket
xor a
ld [hDisableJoypadPolling], a
ret
_SendSGBPacket: ; 71feb (1c:5feb)
;check number of packets
ld a, [hl]
and a, $07
ret z
; store number of packets in B
ld b, a
.loop2
; save B for later use
push bc
; send RESET signal (P14=LOW, P15=LOW)
xor a
ld [rJOYP], a
; set P14=HIGH, P15=HIGH
ld a, $30
ld [rJOYP], a
;load length of packets (16 bytes)
ld b, $10
.nextByte
;set bit counter (8 bits per byte)
ld e, $08
; get next byte in the packet
ld a, [hli]
ld d, a
.nextBit0
bit 0, d
; if 0th bit is not zero set P14=HIGH, P15=LOW (send bit 1)
ld a, $10
jr nz, .next0
; else (if 0th bit is zero) set P14=LOW, P15=HIGH (send bit 0)
ld a, $20
.next0
ld [rJOYP], a
; must set P14=HIGH, P15=HIGH between each "pulse"
ld a, $30
ld [rJOYP], a
; rotation will put next bit in 0th position (so we can always use command
; "bit 0, d" to fetch the bit that has to be sent)
rr d
; decrease bit counter so we know when we have sent all 8 bits of current byte
dec e
jr nz, .nextBit0
dec b
jr nz, .nextByte
; send bit 1 as a "stop bit" (end of parameter data)
ld a, $20
ld [rJOYP], a
; set P14=HIGH, P15=HIGH
ld a, $30
ld [rJOYP], a
call Wait7000
; wait for about 70000 cycles
; call Wait7000
; restore (previously pushed) number of packets
pop bc
dec b
; return if there are no more packets
ret z
; else send 16 more bytes
jr .loop2
LoadSGB: ; 721f8 (1c:61f8)
xor a
ld [wOnSGB], a
call CheckSGB
jr c, .onSGB
ld a, [hGBC]
and a
jr z, .onDMG
ld a, $1
ld [wOnSGB], a
.onDMG
ret
.onSGB
ld a, $1
ld [wOnSGB], a
di
call PrepareSuperNintendoVRAMTransfer
ei
ld a, 1
ld [wCopyingSGBTileData], a
ld de, ChrTrnPacket
ld hl, SGBBorderGraphics
call CopyGfxToSuperNintendoVRAM
xor a
ld [wCopyingSGBTileData], a
ld de, PctTrnPacket
ld hl, BorderPalettes
call CopyGfxToSuperNintendoVRAM
xor a
ld [wCopyingSGBTileData], a
ld de, PalTrnPacket
ld hl, SuperPalettes
call CopyGfxToSuperNintendoVRAM
call ClearVram
ld hl, MaskEnCancelPacket
jp SendSGBPacket
PrepareSuperNintendoVRAMTransfer: ; 72247 (1c:6247)
ld hl, .packetPointers
ld c, 9
.loop
push bc
ld a, [hli]
push hl
ld h, [hl]
ld l, a
call SendSGBPacket
pop hl
inc hl
pop bc
dec c
jr nz, .loop
ret
.packetPointers ; 7225b (1c:625b)
; Only the first packet is needed.
dw MaskEnFreezePacket
dw DataSnd_728a1
dw DataSnd_728b1
dw DataSnd_728c1
dw DataSnd_728d1
dw DataSnd_728e1
dw DataSnd_728f1
dw DataSnd_72901
dw DataSnd_72911
CheckSGB: ; 7226d (1c:626d)
ld hl, MltReq2Packet
call SendSGBPacket
call Wait7000
ld a, [rJOYP]
and $3
cp $3
jr nz, .isSGB
ld a, $20
ld [rJOYP], a
ld a, [rJOYP]
ld a, [rJOYP]
call Wait7000
call Wait7000
ld a, $30
ld [rJOYP], a
call Wait7000
call Wait7000
ld a, $10
ld [rJOYP], a
ld a, [rJOYP]
ld a, [rJOYP]
ld a, [rJOYP]
ld a, [rJOYP]
ld a, [rJOYP]
ld a, [rJOYP]
call Wait7000
call Wait7000
ld a, $30
ld [rJOYP], a
ld a, [rJOYP]
ld a, [rJOYP]
ld a, [rJOYP]
call Wait7000
call Wait7000
ld a, [rJOYP]
and $3
cp $3
jr nz, .isSGB
call SendMltReq1Packet
and a
ret
.isSGB
call SendMltReq1Packet
scf
ret
SendMltReq1Packet: ; 722ce (1c:62ce)
ld hl, MltReq1Packet
call SendSGBPacket
jp Wait7000
CopyGfxToSuperNintendoVRAM: ; 722d7 (1c:62d7)
di
push de
call DisableLCD
ld a, $e4
ld [rBGP], a
call _UpdateGBCPal_BGP_CheckDMG
ld de, vChars1
ld a, [wCopyingSGBTileData]
and a
jr z, .notCopyingTileData
call CopySGBBorderTiles
jr .next
.notCopyingTileData
ld bc, $1000
call CopyData
.next
ld hl, vBGMap0
ld de, $c
ld a, $80
ld c, $d
.loop
ld b, $14
.innerLoop
ld [hli], a
inc a
dec b
jr nz, .innerLoop
add hl, de
dec c
jr nz, .loop
ld a, $e3
ld [rLCDC], a
pop hl
call SendSGBPacket
xor a
ld [rBGP], a
call _UpdateGBCPal_BGP_CheckDMG
ei
ret
Wait7000: ; 7231c (1c:631c)
; Each loop takes 9 cycles so this routine actually waits 63000 cycles.
ld de, 7000
.loop
nop
nop
nop
dec de
ld a, d
or e
jr nz, .loop
ret
SendSGBPackets: ; 72328 (1c:6328)
ld a, [hGBC]
and a
jr z, .notGBC
push de
call InitGBCPalettes
pop hl
call InitGBCPalettes
ld a, [rLCDC]
and rLCDC_ENABLE_MASK
ret z
call Delay3
ret
.notGBC
push de
call SendSGBPacket
pop hl
jp SendSGBPacket
InitGBCPalettes: ; 72346 (1c:6346)
ld a, [hl]
and $f8
cp $20
jp z, TranslatePalPacketToBGMapAttributes
inc hl
index = 0
REPT NUM_ACTIVE_PALS
IF index > 0
pop hl
ENDC
ld a, [hli]
inc hl
IF index < (NUM_ACTIVE_PALS + -1)
push hl
ENDC
call GetGBCBasePalAddress
ld a, e
ld [wGBCBasePalPointers + index * 2], a
ld a, d
ld [wGBCBasePalPointers + index * 2 + 1], a
xor a ; CONVERT_BGP
call DMGPalToGBCPal
ld a, index
call TransferCurBGPData
ld a, CONVERT_OBP0
call DMGPalToGBCPal
ld a, index
call TransferCurOBPData
ld a, CONVERT_OBP1
call DMGPalToGBCPal
ld a, index + 4
call TransferCurOBPData
index = index + 1
ENDR
ret
GetGBCBasePalAddress:: ; 723fe (1c:63fe)
; Input: a = palette ID
; Output: de = palette address
push hl
ld l, a
xor a
ld h, a
add hl, hl
add hl, hl
add hl, hl
ld de, GBCBasePalettes
add hl, de
ld a, l
ld e, a
ld a, h
ld d, a
pop hl
ret
DMGPalToGBCPal:: ; 7240f (1c:640f)
; Populate wGBCPal with colors from a base palette, selected using one of the
; DMG palette registers.
; Input:
; a = which DMG palette register
; de = address of GBC base palette
and a
jr nz, .notBGP
ld a, [rBGP]
ld [wLastBGP], a
jr .convert
.notBGP
dec a
jr nz, .notOBP0
ld a, [rOBP0]
ld [wLastOBP0], a
jr .convert
.notOBP0
ld a, [rOBP1]
ld [wLastOBP1], a
.convert
color_index = 0
REPT NUM_COLORS
ld b, a
and %11
call .GetColorAddress
ld a, [hli]
ld [wGBCPal + color_index * 2], a
ld a, [hl]
ld [wGBCPal + color_index * 2 + 1], a
IF color_index < (NUM_COLORS + -1)
ld a, b
rrca
rrca
ENDC
color_index = color_index + 1
ENDR
ret
.GetColorAddress: ; 7246a (1c:646a)
add a
ld l, a
xor a
ld h, a
add hl, de
ret
TransferCurBGPData:: ; 72470 (1c:6470)
push de
add a
add a
add a
or $80 ; auto-increment
ld [rBGPI], a
ld de, rBGPD
ld hl, wGBCPal
ld b, %10 ; mask for non-V-blank/non-H-blank STAT mode
ld a, [rLCDC]
and rLCDC_ENABLE_MASK
jr nz, .lcdEnabled
rept NUM_COLORS
call TransferPalColorLCDDisabled
endr
jr .done
.lcdEnabled
rept NUM_COLORS
call TransferPalColorLCDEnabled
endr
.done
pop de
ret
BufferBGPPal:: ; 724a2 (1c:64a2)
; Copy wGBCPal to palette a in wBGPPalsBuffer.
push de
add a
add a
add a
ld l, a
xor a
ld h, a
ld de, wBGPPalsBuffer
add hl, de
ld de, wGBCPal
ld c, PAL_SIZE
.loop
ld a, [de]
ld [hli], a
inc de
dec c
jr nz, .loop
pop de
ret
TransferBGPPals:: ; 724ba (1c:64ba)
; Transfer the buffered BG palettes.
ld a, [rLCDC]
and rLCDC_ENABLE_MASK
jr z, .lcdDisabled
di
.waitLoop
ld a, [rLY]
cp 144
jr c, .waitLoop
.lcdDisabled
call .DoTransfer
ei
ret
.DoTransfer: ; 724cc (1c:64cc)
xor a
or $80 ; auto-increment
ld [rBGPI], a
ld de, rBGPD
ld hl, wBGPPalsBuffer
ld c, 4 * PAL_SIZE
.loop
ld a, [hli]
ld [de], a
dec c
jr nz, .loop
ret
TransferCurOBPData: ; 724df (1c:64df)
push de
add a
add a
add a
or $80 ; auto-increment
ld [rOBPI], a
ld de, rOBPD
ld hl, wGBCPal
ld b, %10 ; mask for non-V-blank/non-H-blank STAT mode
ld a, [rLCDC]
and rLCDC_ENABLE_MASK
jr nz, .lcdEnabled
rept NUM_COLORS
call TransferPalColorLCDDisabled
endr
jr .done
.lcdEnabled
rept NUM_COLORS
call TransferPalColorLCDEnabled
endr
.done
pop de
ret
TransferPalColorLCDEnabled: ; 72511 (1c:6511)
; Transfer a palette color while the LCD is enabled.
; In case we're already in H-blank or V-blank, wait for it to end. This is a
; precaution so that the transfer doesn't extend past the blanking period.
ld a, [rSTAT]
and b
jr z, TransferPalColorLCDEnabled
; Wait for H-blank or V-blank to begin.
.notInBlankingPeriod
ld a, [rSTAT]
and b
jr nz, .notInBlankingPeriod
; fall through
TransferPalColorLCDDisabled: ; 7251b (1c:651b)
; Transfer a palette color while the LCD is disabled.
ld a, [hli]
ld [de], a
ld a, [hli]
ld [de], a
ret
_UpdateGBCPal_BGP_CheckDMG:: ; 72520 (1c:6520)
ld a, [hGBC]
and a
ret z
; fall through
_UpdateGBCPal_BGP:: ; 72524 (1c:6524)
index = 0
REPT NUM_ACTIVE_PALS
ld a, [wGBCBasePalPointers + index * 2]
ld e, a
ld a, [wGBCBasePalPointers + index * 2 + 1]
ld d, a
xor a ; CONVERT_BGP
call DMGPalToGBCPal
ld a, index
call BufferBGPPal
index = index + 1
ENDR
call TransferBGPPals
ret
_UpdateGBCPal_OBP:: ; 7256c (1c:656c)
index = 0
REPT NUM_ACTIVE_PALS
ld a, [wGBCBasePalPointers + index * 2]
ld e, a
ld a, [wGBCBasePalPointers + index * 2 + 1]
ld d, a
ld a, c
call DMGPalToGBCPal
ld a, c
dec a
rlca
rlca
IF index > 0
IF index == 1
inc a
ELSE
add index
ENDC
ENDC
call TransferCurOBPData
index = index + 1
ENDR
ret
TranslatePalPacketToBGMapAttributes:: ; 725be (1c:65be)
; translate the SGB pal packets into something usable for the GBC
push hl
pop de
ld hl, PalPacketPointers
ld a, [hli]
ld c, a
.loop
ld a, e
.innerLoop
cp [hl]
jr z, .checkHighByte
inc hl
inc hl
dec c
jr nz, .innerLoop
ret
.checkHighByte
; the low byte of pointer matched, so check the high byte
inc hl
ld a, d
cp [hl]
jr z, .foundMatchingPointer
inc hl
dec c
jr nz, .loop
ret
.foundMatchingPointer
callba LoadBGMapAttributes
ret
PalPacketPointers:: ; 725e2 (1c:65e2)
db (palPacketPointersEnd - palPacketPointers) / 2
palPacketPointers
dw BlkPacket_WholeScreen
dw BlkPacket_Battle
dw BlkPacket_StatusScreen
dw BlkPacket_Pokedex
dw BlkPacket_Slots
dw BlkPacket_Titlescreen
dw BlkPacket_NidorinoIntro
dw wPartyMenuBlkPacket
dw wTrainerCardBlkPacket
dw BlkPacket_GameFreakIntro
dw wPalPacket
dw UnknownPacket_72751
palPacketPointersEnd
CopySGBBorderTiles: ; 725fb (1c:65fb)
; SGB tile data is stored in a 4BPP planar format.
; Each tile is 32 bytes. The first 16 bytes contain bit planes 1 and 2, while
; the second 16 bytes contain bit planes 3 and 4.
; This function converts 2BPP planar data into this format by mapping
; 2BPP colors 0-3 to 4BPP colors 0-3. 4BPP colors 4-15 are not used.
ld b, 128
.tileLoop
; Copy bit planes 1 and 2 of the tile data.
ld c, 16
.copyLoop
ld a, [hli]
ld [de], a
inc de
dec c
jr nz, .copyLoop
; Zero bit planes 3 and 4.
ld c, 16
xor a
.zeroLoop
ld [de], a
inc de
dec c
jr nz, .zeroLoop
dec b
jr nz, .tileLoop
ret
INCLUDE "data/sgb_packets.asm"
INCLUDE "data/mon_palettes.asm"
INCLUDE "data/super_palettes.asm"
INCLUDE "data/sgb_border.asm"
|