-
Notifications
You must be signed in to change notification settings - Fork 806
/
save.asm
1132 lines (1030 loc) · 20.9 KB
/
save.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
SaveMenu:
call LoadStandardMenuHeader
farcall DisplaySaveInfoOnSave
call SpeechTextbox
call UpdateSprites
farcall SaveMenu_CopyTilemapAtOnce
ld hl, WouldYouLikeToSaveTheGameText
call SaveTheGame_yesorno
jr nz, .refused
call AskOverwriteSaveFile
jr c, .refused
call PauseGameLogic
call _SavingDontTurnOffThePower
call ResumeGameLogic
call ExitMenu
and a
ret
.refused
call ExitMenu
call GSReloadPalettes
farcall SaveMenu_CopyTilemapAtOnce
scf
ret
SaveAfterLinkTrade:
call PauseGameLogic
farcall StageRTCTimeForSave
farcall BackupMysteryGift
call SavePokemonData
call SaveChecksum
call SaveBackupPokemonData
call SaveBackupChecksum
farcall BackupPartyMonMail
farcall SaveRTC
call ResumeGameLogic
ret
ChangeBoxSaveGame:
push de
ld hl, ChangeBoxSaveText
call MenuTextbox
call YesNoBox
call ExitMenu
jr c, .refused
call AskOverwriteSaveFile
jr c, .refused
call PauseGameLogic
call SavingDontTurnOffThePower
call SaveBox
pop de
ld a, e
ld [wCurBox], a
call LoadBox
call SavedTheGame
call ResumeGameLogic
and a
ret
.refused
pop de
ret
Link_SaveGame:
call AskOverwriteSaveFile
jr c, .refused
call PauseGameLogic
call _SavingDontTurnOffThePower
call ResumeGameLogic
and a
.refused
ret
MoveMonWOMail_SaveGame:
call PauseGameLogic
push de
call SaveBox
pop de
ld a, e
ld [wCurBox], a
call LoadBox
call ResumeGameLogic
ret
MoveMonWOMail_InsertMon_SaveGame:
call PauseGameLogic
push de
call SaveBox
pop de
ld a, e
ld [wCurBox], a
ld a, TRUE
ld [wSaveFileExists], a
farcall StageRTCTimeForSave
farcall BackupMysteryGift
call ValidateSave
call SaveOptions
call SavePlayerData
call SavePokemonData
call SaveChecksum
call ValidateBackupSave
call SaveBackupOptions
call SaveBackupPlayerData
call SaveBackupPokemonData
call SaveBackupChecksum
farcall BackupPartyMonMail
farcall BackupGSBallFlag
farcall SaveRTC
call LoadBox
call ResumeGameLogic
ld de, SFX_SAVE
call PlaySFX
ld c, 24
call DelayFrames
ret
StartMoveMonWOMail_SaveGame:
ld hl, MoveMonWOMailSaveText
call MenuTextbox
call YesNoBox
call ExitMenu
jr c, .refused
call AskOverwriteSaveFile
jr c, .refused
call PauseGameLogic
call _SavingDontTurnOffThePower
call ResumeGameLogic
and a
ret
.refused
scf
ret
PauseGameLogic:
ld a, TRUE
ld [wGameLogicPaused], a
ret
ResumeGameLogic:
xor a ; FALSE
ld [wGameLogicPaused], a
ret
AddHallOfFameEntry:
ld a, BANK(sHallOfFame)
call OpenSRAM
ld hl, sHallOfFame + HOF_LENGTH * (NUM_HOF_TEAMS - 1) - 1
ld de, sHallOfFame + HOF_LENGTH * NUM_HOF_TEAMS - 1
ld bc, HOF_LENGTH * (NUM_HOF_TEAMS - 1)
.loop
ld a, [hld]
ld [de], a
dec de
dec bc
ld a, c
or b
jr nz, .loop
ld hl, wHallOfFamePokemonList
ld de, sHallOfFame
ld bc, wHallOfFamePokemonListEnd - wHallOfFamePokemonList + 1
call CopyBytes
call CloseSRAM
; This vc_hook causes the Virtual Console to set [sGSBallFlag] and [sGSBallFlagBackup]
; to GS_BALL_AVAILABLE, which enables you to get the GS Ball, take it to Kurt, and
; encounter Celebi. It assumes that sGSBallFlag and sGSBallFlagBackup are at their
; original addresses.
vc_hook Enable_GS_Ball_mobile_event
vc_assert BANK(sGSBallFlag) == $1 && sGSBallFlag == $be3c, \
"sGSBallFlag is no longer located at 01:be3c."
vc_assert BANK(sGSBallFlagBackup) == $1 && sGSBallFlagBackup == $be44, \
"sGSBallFlagBackup is no longer located at 01:be44."
vc_assert GS_BALL_AVAILABLE == $b, \
"GS_BALL_AVAILABLE is no longer equal to $b."
ret
SaveGameData:
call _SaveGameData
ret
AskOverwriteSaveFile:
ld a, [wSaveFileExists]
and a
jr z, .erase
call CompareLoadedAndSavedPlayerID
jr z, .yoursavefile
ld hl, AnotherSaveFileText
call SaveTheGame_yesorno
jr nz, .refused
jr .erase
.yoursavefile
ld hl, AlreadyASaveFileText
call SaveTheGame_yesorno
jr nz, .refused
jr .ok
.erase
call ErasePreviousSave
.ok
and a
ret
.refused
scf
ret
SaveTheGame_yesorno:
ld b, BANK(WouldYouLikeToSaveTheGameText)
call MapTextbox
call LoadMenuTextbox
lb bc, 0, 7
call PlaceYesNoBox
ld a, [wMenuCursorY]
dec a
call CloseWindow
push af
call GSReloadPalettes
pop af
and a
ret
CompareLoadedAndSavedPlayerID:
ld a, BANK(sPlayerData)
call OpenSRAM
ld hl, sPlayerData + (wPlayerID - wPlayerData)
ld a, [hli]
ld c, [hl]
ld b, a
call CloseSRAM
ld a, [wPlayerID]
cp b
ret nz
ld a, [wPlayerID + 1]
cp c
ret
_SavingDontTurnOffThePower:
call SavingDontTurnOffThePower
SavedTheGame:
call _SaveGameData
; wait 32 frames
ld c, 32
call DelayFrames
; copy the original text speed setting to the stack
ld a, [wOptions]
push af
; set text speed to medium
ld a, TEXT_DELAY_MED
ld [wOptions], a
; <PLAYER> saved the game!
ld hl, SavedTheGameText
call PrintText
; restore the original text speed setting
pop af
ld [wOptions], a
ld de, SFX_SAVE
call WaitPlaySFX
call WaitSFX
; wait 30 frames
ld c, 30
call DelayFrames
ret
_SaveGameData:
ld a, TRUE
ld [wSaveFileExists], a
farcall StageRTCTimeForSave
farcall BackupMysteryGift
call ValidateSave
call SaveOptions
call SavePlayerData
call SavePokemonData
call SaveBox
call SaveChecksum
call ValidateBackupSave
call SaveBackupOptions
call SaveBackupPlayerData
call SaveBackupPokemonData
call SaveBackupChecksum
call UpdateStackTop
farcall BackupPartyMonMail
farcall BackupGSBallFlag
farcall SaveRTC
ld a, BANK(sBattleTowerChallengeState)
call OpenSRAM
ld a, [sBattleTowerChallengeState]
cp BATTLETOWER_RECEIVED_REWARD
jr nz, .ok
xor a
ld [sBattleTowerChallengeState], a
.ok
call CloseSRAM
ret
UpdateStackTop:
; sStackTop appears to be unused.
; It could have been used to debug stack overflow during saving.
call FindStackTop
ld a, BANK(sStackTop)
call OpenSRAM
ld a, [sStackTop + 0]
ld e, a
ld a, [sStackTop + 1]
ld d, a
or e
jr z, .update
ld a, e
sub l
ld a, d
sbc h
jr c, .done
.update
ld a, l
ld [sStackTop + 0], a
ld a, h
ld [sStackTop + 1], a
.done
call CloseSRAM
ret
FindStackTop:
; Find the furthest point that sp has traversed to.
; This is distinct from the current value of sp.
ld hl, wStackBottom
.loop
ld a, [hl]
or a
ret nz
inc hl
jr .loop
SavingDontTurnOffThePower:
; Prevent joypad interrupts
xor a
ldh [hJoypadReleased], a
ldh [hJoypadPressed], a
ldh [hJoypadSum], a
ldh [hJoypadDown], a
; Save the text speed setting to the stack
ld a, [wOptions]
push af
; Set the text speed to medium
ld a, TEXT_DELAY_MED
ld [wOptions], a
; SAVING... DON'T TURN OFF THE POWER.
ld hl, SavingDontTurnOffThePowerText
call PrintText
; Restore the text speed setting
pop af
ld [wOptions], a
; Wait for 16 frames
ld c, 16
call DelayFrames
ret
ErasePreviousSave:
call EraseBoxes
call EraseHallOfFame
call EraseLinkBattleStats
call EraseMysteryGift
call SaveData
call EraseBattleTowerStatus
ld a, BANK(sStackTop)
call OpenSRAM
xor a
ld [sStackTop + 0], a
ld [sStackTop + 1], a
call CloseSRAM
ld a, $1
ld [wSavedAtLeastOnce], a
ret
EraseLinkBattleStats:
ld a, BANK(sLinkBattleStats)
call OpenSRAM
ld hl, sLinkBattleStats
ld bc, sLinkBattleStatsEnd - sLinkBattleStats
xor a
call ByteFill
jp CloseSRAM
EraseMysteryGift:
ld a, BANK(sBackupMysteryGiftItem)
call OpenSRAM
ld hl, sBackupMysteryGiftItem
ld bc, sBackupMysteryGiftItemEnd - sBackupMysteryGiftItem
xor a
call ByteFill
jp CloseSRAM
EraseHallOfFame:
ld a, BANK(sHallOfFame)
call OpenSRAM
ld hl, sHallOfFame
ld bc, sHallOfFameEnd - sHallOfFame
xor a
call ByteFill
jp CloseSRAM
InitDefaultEZChatMsgs: ; unreferenced
ld a, BANK(sEZChatMessages) ; MBC30 bank used by JP Crystal; inaccessible by MBC3
call OpenSRAM
ld hl, .Data
ld de, sEZChatMessages
ld bc, EASY_CHAT_MESSAGE_LENGTH * 4
call CopyBytes
jp CloseSRAM
.Data:
; introduction
db $0d, EZCHAT_GREETINGS, $00, EZCHAT_EXCLAMATIONS, $00, EZCHAT_POKEMON
db $22, EZCHAT_GREETINGS, $01, EZCHAT_EXCLAMATIONS, $00, EZCHAT_POKEMON
; begin battle
db $03, EZCHAT_BATTLE, $05, EZCHAT_CONDITIONS, $03, EZCHAT_EXCLAMATIONS
db $0e, EZCHAT_CONVERSATION, $03, EZCHAT_GREETINGS, $00, EZCHAT_POKEMON
; win battle
db $39, EZCHAT_FEELINGS, $07, EZCHAT_BATTLE, $00, EZCHAT_EXCLAMATIONS
db $04, EZCHAT_FEELINGS, $01, EZCHAT_EXCLAMATIONS, $00, EZCHAT_POKEMON
; lose battle
db $0f, EZCHAT_EXCLAMATIONS, $14, EZCHAT_FEELINGS, $05, EZCHAT_EXCLAMATIONS
db $11, EZCHAT_TIME, $0c, EZCHAT_CONVERSATION, $06, EZCHAT_BATTLE
EraseBattleTowerStatus:
ld a, BANK(sBattleTowerChallengeState)
call OpenSRAM
xor a
ld [sBattleTowerChallengeState], a
jp CloseSRAM
SaveData:
call _SaveData
ret
Function14d6c: ; unreferenced
ld a, BANK(s4_a60b) ; MBC30 bank used by JP Crystal; inaccessible by MBC3
call OpenSRAM
ld a, [s4_a60b] ; address of MBC30 bank
ld b, $0
and a
jr z, .ok
ld b, $2
.ok
ld a, b
ld [s4_a60b], a ; address of MBC30 bank
call CloseSRAM
ret
Function14d83: ; unreferenced
ld a, BANK(s4_a60c) ; aka BANK(s4_a60d) ; MBC30 bank used by JP Crystal; inaccessible by MBC3
call OpenSRAM
xor a
ld [s4_a60c], a ; address of MBC30 bank
ld [s4_a60d], a ; address of MBC30 bank
call CloseSRAM
ret
DisableMobileStadium: ; unreferenced
ld a, BANK(sMobileStadiumFlag)
call OpenSRAM
xor a
ld [sMobileStadiumFlag], a
call CloseSRAM
ret
HallOfFame_InitSaveIfNeeded:
ld a, [wSavedAtLeastOnce]
and a
ret nz
call ErasePreviousSave
ret
ValidateSave:
ld a, BANK(sCheckValue1) ; aka BANK(sCheckValue2)
call OpenSRAM
ld a, SAVE_CHECK_VALUE_1
ld [sCheckValue1], a
ld a, SAVE_CHECK_VALUE_2
ld [sCheckValue2], a
jp CloseSRAM
SaveOptions:
ld a, BANK(sOptions)
call OpenSRAM
ld hl, wOptions
ld de, sOptions
ld bc, wOptionsEnd - wOptions
call CopyBytes
ld a, [wOptions]
and ~(1 << NO_TEXT_SCROLL)
ld [sOptions], a
jp CloseSRAM
SavePlayerData:
ld a, BANK(sPlayerData)
call OpenSRAM
ld hl, wPlayerData
ld de, sPlayerData
ld bc, wPlayerDataEnd - wPlayerData
call CopyBytes
ld hl, wCurMapData
ld de, sCurMapData
ld bc, wCurMapDataEnd - wCurMapData
call CopyBytes
jp CloseSRAM
SavePokemonData:
ld a, BANK(sPokemonData)
call OpenSRAM
ld hl, wPokemonData
ld de, sPokemonData
ld bc, wPokemonDataEnd - wPokemonData
call CopyBytes
call CloseSRAM
ret
SaveBox:
call GetBoxAddress
call SaveBoxAddress
ret
SaveChecksum:
ld hl, sGameData
ld bc, sGameDataEnd - sGameData
ld a, BANK(sGameData)
call OpenSRAM
call Checksum
ld a, e
ld [sChecksum + 0], a
ld a, d
ld [sChecksum + 1], a
call CloseSRAM
ret
ValidateBackupSave:
ld a, BANK(sBackupCheckValue1) ; aka BANK(sBackupCheckValue2)
call OpenSRAM
ld a, SAVE_CHECK_VALUE_1
ld [sBackupCheckValue1], a
ld a, SAVE_CHECK_VALUE_2
ld [sBackupCheckValue2], a
call CloseSRAM
ret
SaveBackupOptions:
ld a, BANK(sBackupOptions)
call OpenSRAM
ld hl, wOptions
ld de, sBackupOptions
ld bc, wOptionsEnd - wOptions
call CopyBytes
call CloseSRAM
ret
SaveBackupPlayerData:
ld a, BANK(sBackupPlayerData)
call OpenSRAM
ld hl, wPlayerData
ld de, sBackupPlayerData
ld bc, wPlayerDataEnd - wPlayerData
call CopyBytes
ld hl, wCurMapData
ld de, sBackupCurMapData
ld bc, wCurMapDataEnd - wCurMapData
call CopyBytes
call CloseSRAM
ret
SaveBackupPokemonData:
ld a, BANK(sBackupPokemonData)
call OpenSRAM
ld hl, wPokemonData
ld de, sBackupPokemonData
ld bc, wPokemonDataEnd - wPokemonData
call CopyBytes
call CloseSRAM
ret
SaveBackupChecksum:
ld hl, sBackupGameData
ld bc, sBackupGameDataEnd - sBackupGameData
ld a, BANK(sBackupGameData)
call OpenSRAM
call Checksum
ld a, e
ld [sBackupChecksum + 0], a
ld a, d
ld [sBackupChecksum + 1], a
call CloseSRAM
ret
TryLoadSaveFile:
call VerifyChecksum
jr nz, .backup
call LoadPlayerData
call LoadPokemonData
call LoadBox
farcall RestorePartyMonMail
farcall RestoreGSBallFlag
farcall RestoreMysteryGift
call ValidateBackupSave
call SaveBackupOptions
call SaveBackupPlayerData
call SaveBackupPokemonData
call SaveBackupChecksum
and a
ret
.backup
call VerifyBackupChecksum
jr nz, .corrupt
call LoadBackupPlayerData
call LoadBackupPokemonData
call LoadBox
farcall RestorePartyMonMail
farcall RestoreGSBallFlag
farcall RestoreMysteryGift
call ValidateSave
call SaveOptions
call SavePlayerData
call SavePokemonData
call SaveChecksum
and a
ret
.corrupt
ld a, [wOptions]
push af
set NO_TEXT_SCROLL, a
ld [wOptions], a
ld hl, SaveFileCorruptedText
call PrintText
pop af
ld [wOptions], a
scf
ret
TryLoadSaveData:
xor a ; FALSE
ld [wSaveFileExists], a
call CheckPrimarySaveFile
ld a, [wSaveFileExists]
and a
jr z, .backup
ld a, BANK(sPlayerData)
call OpenSRAM
ld hl, sPlayerData + wStartDay - wPlayerData
ld de, wStartDay
ld bc, 8
call CopyBytes
ld hl, sPlayerData + wStatusFlags - wPlayerData
ld de, wStatusFlags
ld a, [hl]
ld [de], a
call CloseSRAM
ret
.backup
call CheckBackupSaveFile
ld a, [wSaveFileExists]
and a
jr z, .corrupt
ld a, BANK(sBackupPlayerData)
call OpenSRAM
ld hl, sBackupPlayerData + wStartDay - wPlayerData
ld de, wStartDay
ld bc, 8
call CopyBytes
ld hl, sBackupPlayerData + wStatusFlags - wPlayerData
ld de, wStatusFlags
ld a, [hl]
ld [de], a
call CloseSRAM
ret
.corrupt
ld hl, DefaultOptions
ld de, wOptions
ld bc, wOptionsEnd - wOptions
call CopyBytes
call ClearClock
ret
INCLUDE "data/default_options.asm"
CheckPrimarySaveFile:
ld a, BANK(sCheckValue1) ; aka BANK(sCheckValue2)
call OpenSRAM
ld a, [sCheckValue1]
cp SAVE_CHECK_VALUE_1
jr nz, .nope
ld a, [sCheckValue2]
cp SAVE_CHECK_VALUE_2
jr nz, .nope
ld hl, sOptions
ld de, wOptions
ld bc, wOptionsEnd - wOptions
call CopyBytes
call CloseSRAM
ld a, TRUE
ld [wSaveFileExists], a
.nope
call CloseSRAM
ret
CheckBackupSaveFile:
ld a, BANK(sBackupCheckValue1) ; aka BANK(sBackupCheckValue2)
call OpenSRAM
ld a, [sBackupCheckValue1]
cp SAVE_CHECK_VALUE_1
jr nz, .nope
ld a, [sBackupCheckValue2]
cp SAVE_CHECK_VALUE_2
jr nz, .nope
ld hl, sBackupOptions
ld de, wOptions
ld bc, wOptionsEnd - wOptions
call CopyBytes
ld a, $2
ld [wSaveFileExists], a
.nope
call CloseSRAM
ret
LoadPlayerData:
ld a, BANK(sPlayerData)
call OpenSRAM
ld hl, sPlayerData
ld de, wPlayerData
ld bc, wPlayerDataEnd - wPlayerData
call CopyBytes
ld hl, sCurMapData
ld de, wCurMapData
ld bc, wCurMapDataEnd - wCurMapData
call CopyBytes
call CloseSRAM
ld a, BANK(sBattleTowerChallengeState)
call OpenSRAM
ld a, [sBattleTowerChallengeState]
cp BATTLETOWER_RECEIVED_REWARD
jr nz, .not_4
ld a, BATTLETOWER_WON_CHALLENGE
ld [sBattleTowerChallengeState], a
.not_4
call CloseSRAM
ret
LoadPokemonData:
ld a, BANK(sPokemonData)
call OpenSRAM
ld hl, sPokemonData
ld de, wPokemonData
ld bc, wPokemonDataEnd - wPokemonData
call CopyBytes
call CloseSRAM
ret
LoadBox:
call GetBoxAddress
call LoadBoxAddress
ret
VerifyChecksum:
ld hl, sGameData
ld bc, sGameDataEnd - sGameData
ld a, BANK(sGameData)
call OpenSRAM
call Checksum
ld a, [sChecksum + 0]
cp e
jr nz, .fail
ld a, [sChecksum + 1]
cp d
.fail
push af
call CloseSRAM
pop af
ret
LoadBackupPlayerData:
ld a, BANK(sBackupPlayerData)
call OpenSRAM
ld hl, sBackupPlayerData
ld de, wPlayerData
ld bc, wPlayerDataEnd - wPlayerData
call CopyBytes
ld hl, sBackupCurMapData
ld de, wCurMapData
ld bc, wCurMapDataEnd - wCurMapData
call CopyBytes
call CloseSRAM
ret
LoadBackupPokemonData:
ld a, BANK(sBackupPokemonData)
call OpenSRAM
ld hl, sBackupPokemonData
ld de, wPokemonData
ld bc, wPokemonDataEnd - wPokemonData
call CopyBytes
call CloseSRAM
ret
VerifyBackupChecksum:
ld hl, sBackupGameData
ld bc, sBackupGameDataEnd - sBackupGameData
ld a, BANK(sBackupGameData)
call OpenSRAM
call Checksum
ld a, [sBackupChecksum + 0]
cp e
jr nz, .fail
ld a, [sBackupChecksum + 1]
cp d
.fail
push af
call CloseSRAM
pop af
ret
_SaveData:
; This is called within two scenarios:
; a) ErasePreviousSave (the process of erasing the save from a previous game file)
; b) unused mobile functionality
; It is not part of a regular save.
ld a, BANK(sCrystalData)
call OpenSRAM
ld hl, wCrystalData
ld de, sCrystalData
ld bc, wCrystalDataEnd - wCrystalData
call CopyBytes
; This block originally had some mobile functionality, but since we're still in
; BANK(sCrystalData), it instead overwrites the sixteen wEventFlags starting at 1:s4_a60e with
; garbage from wd479. This isn't an issue, since ErasePreviousSave is followed by a regular
; save that unwrites the garbage.
ld hl, wd479
ld a, [hli]
ld [s4_a60e + 0], a
ld a, [hli]
ld [s4_a60e + 1], a
jp CloseSRAM
_LoadData:
ld a, BANK(sCrystalData)
call OpenSRAM
ld hl, sCrystalData
ld de, wCrystalData
ld bc, wCrystalDataEnd - wCrystalData
call CopyBytes
; This block originally had some mobile functionality to mirror _SaveData above, but instead it
; (harmlessly) writes the aforementioned wEventFlags to the unused wd479.
ld hl, wd479
ld a, [s4_a60e + 0]
ld [hli], a
ld a, [s4_a60e + 1]
ld [hli], a
jp CloseSRAM
GetBoxAddress:
ld a, [wCurBox]
cp NUM_BOXES
jr c, .ok
xor a
ld [wCurBox], a
.ok
ld e, a
ld d, 0
ld hl, BoxAddresses
rept 5
add hl, de
endr
ld a, [hli]
push af
ld a, [hli]
ld e, a
ld a, [hli]
ld d, a
ld a, [hli]
ld h, [hl]
ld l, a
pop af
ret
SaveBoxAddress:
; Save box via wBoxPartialData.
; We do this in three steps because the size of wBoxPartialData is less than
; the size of sBox.
push hl
; Load the first part of the active box.
push af
push de
ld a, BANK(sBox)
call OpenSRAM
ld hl, sBox
ld de, wBoxPartialData
ld bc, (wBoxPartialDataEnd - wBoxPartialData)
call CopyBytes
call CloseSRAM
pop de
pop af
; Save it to the target box.
push af
push de
call OpenSRAM
ld hl, wBoxPartialData
ld bc, (wBoxPartialDataEnd - wBoxPartialData)
call CopyBytes
call CloseSRAM
; Load the second part of the active box.
ld a, BANK(sBox)
call OpenSRAM
ld hl, sBox + (wBoxPartialDataEnd - wBoxPartialData)
ld de, wBoxPartialData
ld bc, (wBoxPartialDataEnd - wBoxPartialData)
call CopyBytes
call CloseSRAM
pop de
pop af
ld hl, (wBoxPartialDataEnd - wBoxPartialData)
add hl, de
ld e, l
ld d, h
; Save it to the next part of the target box.
push af
push de
call OpenSRAM
ld hl, wBoxPartialData
ld bc, (wBoxPartialDataEnd - wBoxPartialData)
call CopyBytes
call CloseSRAM
; Load the third and final part of the active box.
ld a, BANK(sBox)
call OpenSRAM
ld hl, sBox + (wBoxPartialDataEnd - wBoxPartialData) * 2
ld de, wBoxPartialData
ld bc, sBoxEnd - (sBox + (wBoxPartialDataEnd - wBoxPartialData) * 2) ; $8e
call CopyBytes
call CloseSRAM
pop de
pop af
ld hl, (wBoxPartialDataEnd - wBoxPartialData)
add hl, de
ld e, l
ld d, h
; Save it to the final part of the target box.
call OpenSRAM
ld hl, wBoxPartialData
ld bc, sBoxEnd - (sBox + (wBoxPartialDataEnd - wBoxPartialData) * 2) ; $8e
call CopyBytes
call CloseSRAM
pop hl
ret
LoadBoxAddress:
; Load box via wBoxPartialData.
; We do this in three steps because the size of wBoxPartialData is less than
; the size of sBox.
push hl
ld l, e
ld h, d
; Load part 1
push af
push hl
call OpenSRAM
ld de, wBoxPartialData
ld bc, (wBoxPartialDataEnd - wBoxPartialData)
call CopyBytes
call CloseSRAM
ld a, BANK(sBox)
call OpenSRAM
ld hl, wBoxPartialData
ld de, sBox
ld bc, (wBoxPartialDataEnd - wBoxPartialData)
call CopyBytes
call CloseSRAM
pop hl
pop af
ld de, (wBoxPartialDataEnd - wBoxPartialData)