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
|
; xors sb800
; this has the effect of invalidating the save data checksum
; which the game interprets as being having no save data
InvalidateSaveData:
push hl
ldh a, [hBankSRAM]
push af
ld a, BANK("SRAM2")
call BankswitchSRAM
ld a, $08
xor $ff
ld [sBackupGeneralSaveData + 0], a
ld a, $00
xor $ff
ld [sBackupGeneralSaveData + 1], a
pop af
call BankswitchSRAM
call DisableSRAM
call EnableSRAM
bank1call DiscardSavedDuelData
call DisableSRAM
pop hl
ret
; saves all data to SRAM, including
; General save data and Album/Deck data
; and backs up in SRAM2
SaveAndBackupData:
push de
ld de, sGeneralSaveData
call SaveGeneralSaveDataFromDE
ld de, sAlbumProgress
call UpdateAlbumProgress
call WriteBackupGeneralSaveData
call WriteBackupCardAndDeckSaveData
pop de
ret
_SaveGeneralSaveData:
push de
call GetReceivedLegendaryCards
ld de, sGeneralSaveData
call SaveGeneralSaveDataFromDE
ld de, sAlbumProgress
call UpdateAlbumProgress
pop de
ret
; de = pointer to general game data in SRAM
SaveGeneralSaveDataFromDE:
push hl
push bc
call EnableSRAM
push de
farcall TryGiveMedalPCPacks
ld [wMedalCount], a
farcall OverworldMap_GetOWMapID
ld [wCurOverworldMap], a
pop de
push de
call CopyGeneralSaveDataToSRAM
pop de
call DisableSRAM
pop bc
pop hl
ret
; writes in de total num of cards collected
; and in (de + 1) total num of cards to collect
; also updates wTotalNumCardsCollected and wTotalNumCardsToCollect
UpdateAlbumProgress:
push hl
push de
push de
call GetCardAlbumProgress
call EnableSRAM
pop hl
ld a, d
ld [wTotalNumCardsCollected], a
ld [hli], a
ld a, e
ld [wTotalNumCardsToCollect], a
ld [hl], a
call DisableSRAM
pop de
pop hl
ret
; save values that are listed in WRAMToSRAMMapper
; from WRAM to SRAM, and calculate its checksum
CopyGeneralSaveDataToSRAM:
push hl
push bc
push de
push de
ld hl, sGeneralSaveDataHeaderEnd - sGeneralSaveData
add hl, de
ld e, l
ld d, h
xor a
ld [wGeneralSaveDataByteCount + 0], a
ld [wGeneralSaveDataByteCount + 1], a
ld [wGeneralSaveDataCheckSum + 0], a
ld [wGeneralSaveDataCheckSum + 1], a
ld hl, WRAMToSRAMMapper
.loop_map
ld a, [hli]
ld [wTempPointer + 0], a
ld c, a
ld a, [hli]
ld [wTempPointer + 1], a
or c
jr z, .done_copy
ld a, [hli]
ld c, a ; number of bytes LO
ld a, [hli]
ld b, a ; number of bytes HI
ld a, [wGeneralSaveDataByteCount + 0]
add c
ld [wGeneralSaveDataByteCount + 0], a
ld a, [wGeneralSaveDataByteCount + 1]
adc b
ld [wGeneralSaveDataByteCount + 1], a
call .CopyBytesToSRAM
inc hl
inc hl
jr .loop_map
.done_copy
pop hl
ld a, $08
ld [hli], a
ld a, $00
ld [hli], a
ld a, [wGeneralSaveDataByteCount + 0]
ld [hli], a
ld a, [wGeneralSaveDataByteCount + 1]
ld [hli], a
ld a, [wGeneralSaveDataCheckSum + 0]
ld [hli], a
ld a, [wGeneralSaveDataCheckSum + 1]
ld [hli], a
pop de
pop bc
pop hl
ret
.CopyBytesToSRAM
push hl
ld a, [wTempPointer + 0]
ld l, a
ld a, [wTempPointer + 1]
ld h, a
.loop_bytes
push bc
ld a, [hli]
ld [de], a
inc de
ld c, a
ld a, [wGeneralSaveDataCheckSum + 0]
add c
ld [wGeneralSaveDataCheckSum + 0], a
ld a, [wGeneralSaveDataCheckSum + 1]
adc 0
ld [wGeneralSaveDataCheckSum + 1], a
pop bc
dec bc
ld a, c
or b
jr nz, .loop_bytes
ld a, l
ld [wTempPointer + 0], a
ld a, h
ld [wTempPointer + 1], a
pop hl
ret
; returns carry if no error
; is found in sBackupGeneralSaveData
ValidateBackupGeneralSaveData:
push de
ldh a, [hBankSRAM]
push af
ld a, BANK(sBackupGeneralSaveData)
call BankswitchSRAM
ld de, sBackupGeneralSaveData
call ValidateGeneralSaveDataFromDE
ld de, sAlbumProgress
call LoadAlbumProgressFromSRAM
pop af
call BankswitchSRAM
call DisableSRAM
pop de
ld a, [wNumSRAMValidationErrors]
cp 1
ret
; returns carry if no error
; is found in sGeneralSaveData
_ValidateGeneralSaveData:
push de
call EnableSRAM
ld de, sGeneralSaveData
call ValidateGeneralSaveDataFromDE
ld de, sAlbumProgress
call LoadAlbumProgressFromSRAM
call DisableSRAM
pop de
ld a, [wNumSRAMValidationErrors]
cp 1
ret
; validates the general game data saved in SRAM
; de = pointer to general game data in SRAM
ValidateGeneralSaveDataFromDE:
push hl
push bc
push de
xor a
ld [wNumSRAMValidationErrors], a
push de
push de
inc de
inc de
ld a, [de]
inc de
ld [wGeneralSaveDataByteCount + 0], a
ld a, [de]
inc de
ld [wGeneralSaveDataByteCount + 1], a
ld a, [de]
inc de
ld [wGeneralSaveDataCheckSum + 0], a
ld a, [de]
inc de
ld [wGeneralSaveDataCheckSum + 1], a
pop de
ld hl, sGeneralSaveDataHeaderEnd - sGeneralSaveData
add hl, de
ld e, l
ld d, h
ld hl, WRAMToSRAMMapper
.loop
ld a, [hli]
ld c, a
ld a, [hli]
or c
jr z, .exit_loop
ld a, [hli]
ld c, a ; number of bytes LO
ld a, [hli]
ld b, a ; number of bytes HI
ld a, [wGeneralSaveDataByteCount + 0]
sub c
ld [wGeneralSaveDataByteCount + 0], a
ld a, [wGeneralSaveDataByteCount + 1]
sbc b
ld [wGeneralSaveDataByteCount + 1], a
; loop all the bytes of this struct
.loop_bytes
push hl
push bc
ld a, [de]
push af
ld c, a
ld a, [wGeneralSaveDataCheckSum + 0]
sub c
ld [wGeneralSaveDataCheckSum + 0], a
ld a, [wGeneralSaveDataCheckSum + 1]
sbc 0
ld [wGeneralSaveDataCheckSum + 1], a
pop af
; check if it's within the specified values
cp [hl] ; min value
jr c, .error
inc hl
cp [hl] ; max value
jr z, .next_byte
jr c, .next_byte
.error
ld a, [wNumSRAMValidationErrors]
inc a
ld [wNumSRAMValidationErrors], a
.next_byte
inc de
pop bc
pop hl
dec bc
ld a, c
or b
jr nz, .loop_bytes
; next mapped struct
inc hl
inc hl
jr .loop
.exit_loop
pop hl
ld a, [hli]
sub $8
ld c, a
ld a, [hl]
sub 0
or c
ld hl, wGeneralSaveDataByteCount
or [hl]
inc hl
or [hl]
ld hl, wGeneralSaveDataCheckSum
or [hl]
inc hl
or [hl]
jr z, .no_header_error
ld hl, wNumSRAMValidationErrors
inc [hl]
.no_header_error
pop de
; copy play time minutes and hours
ld hl, (sPlayTimeCounter + 2) - sGeneralSaveData
add hl, de
ld a, [hli]
ld [wPlayTimeHourMinutes + 0], a
ld a, [hli]
ld [wPlayTimeHourMinutes + 1], a
ld a, [hli]
ld [wPlayTimeHourMinutes + 2], a
; copy medal count and current overworld map
ld hl, sGeneralSaveDataHeaderEnd - sGeneralSaveData
add hl, de
ld a, [hli]
ld [wMedalCount], a
ld a, [hl]
ld [wCurOverworldMap], a
pop bc
pop hl
ret
LoadAlbumProgressFromSRAM:
push de
ld a, [de]
ld [wTotalNumCardsCollected], a
inc de
ld a, [de]
ld [wTotalNumCardsToCollect], a
pop de
ret
; first copies data from backup SRAM to main SRAM
; then loads it to WRAM from main SRAM
LoadBackupSaveData:
push hl
push de
call EnableSRAM
bank1call DiscardSavedDuelData
call DisableSRAM
call LoadBackupGeneralSaveData
call LoadBackupCardAndDeckSaveData
ld de, sGeneralSaveData
call LoadGeneralSaveDataFromDE
pop de
pop hl
ret
_LoadGeneralSaveData:
push de
ld de, sGeneralSaveData
call LoadGeneralSaveDataFromDE
pop de
ret
; de = pointer to save data
LoadGeneralSaveDataFromDE:
push hl
push bc
call EnableSRAM
call .LoadData
call DisableSRAM
pop bc
pop hl
ret
.LoadData
push hl
push bc
push de
ld a, e
add sGeneralSaveDataHeaderEnd - sGeneralSaveData
ld [wTempPointer + 0], a
ld a, d
adc 0
ld [wTempPointer + 1], a
ld hl, WRAMToSRAMMapper
.asm_11459
ld a, [hli]
ld e, a
ld d, [hl]
or d
jr z, .done_copy
inc hl
ld a, [hli]
ld c, a
ld a, [hli]
ld b, a
; copy bc bytes from wTempPointer to de
push hl
ld a, [wTempPointer + 0]
ld l, a
ld a, [wTempPointer + 1]
ld h, a
.loop_copy
ld a, [hli]
ld [de], a
inc de
dec bc
ld a, c
or b
jr nz, .loop_copy
ld a, l
ld [wTempPointer + 0], a
ld a, h
ld [wTempPointer + 1], a
pop hl
inc hl
inc hl
jr .asm_11459
.done_copy
call EnableSRAM
ld a, [sAnimationsDisabled]
ld [wAnimationsDisabled], a
ld a, [sTextSpeed]
ld [wTextSpeed], a
call DisableSRAM
pop de
pop bc
pop hl
ret
wram_sram_map: MACRO
dw \1 ; WRAM address
dw \2 ; number of bytes
db \3 ; min allowed value
db \4 ; max allowed value
ENDM
; maps WRAM addresses to SRAM addresses in order
; to save and subsequently retrieve them on game load
; also works as a test in order check whether
; the saved values is SRAM are legal, within the given value range
WRAMToSRAMMapper:
wram_sram_map wMedalCount, 1, $00, $ff ; sMedalCount
wram_sram_map wCurOverworldMap, 1, $00, $ff ; sCurOverworldMap
wram_sram_map wPlayTimeCounter + 0, 1, $00, $ff ; sPlayTimeCounter
wram_sram_map wPlayTimeCounter + 1, 1, $00, $ff
wram_sram_map wPlayTimeCounter + 2, 1, $00, $ff
wram_sram_map wPlayTimeCounter + 3, 2, $00, $ff
wram_sram_map wOverworldMapSelection, 1, $00, $ff ; sOverworldMapSelection
wram_sram_map wTempMap, 1, $00, $ff ; sTempMap
wram_sram_map wTempPlayerXCoord, 1, $00, $ff ; sTempPlayerXCoord
wram_sram_map wTempPlayerYCoord, 1, $00, $ff ; sTempPlayerYCoord
wram_sram_map wTempPlayerDirection, 1, $00, $ff ; sTempPlayerDirection
wram_sram_map wActiveGameEvent, 1, $00, $ff ; sActiveGameEvent
wram_sram_map wDuelResult, 1, $00, $ff ; sDuelResult
wram_sram_map wNPCDuelist, 1, $00, $ff ; sNPCDuelist
wram_sram_map wChallengeHallNPC, 1, $00, $ff ; sChallengeHallNPC
wram_sram_map wd698, 4, $00, $ff ; sb818
wram_sram_map wOWMapEvents, NUM_MAP_EVENTS, $00, $ff ; sOWMapEvents
wram_sram_map .EmptySRAMSlot, 1, $00, $ff ; sb827
wram_sram_map wSelectedPauseMenuItem, 1, $00, $ff ; sSelectedPauseMenuItem
wram_sram_map wSelectedPCMenuItem, 1, $00, $ff ; sSelectedPCMenuItem
wram_sram_map wConfigCursorYPos, 1, $00, $ff ; sConfigCursorYPos
wram_sram_map wSelectedGiftCenterMenuItem, 1, $00, $ff ; sSelectedGiftCenterMenuItem
wram_sram_map wPCPackSelection, 1, 0, 14 ; sPCPackSelection
wram_sram_map wPCPacks, NUM_PC_PACKS, $00, $ff ; sPCPacks
wram_sram_map wDefaultSong, 1, $00, $ff ; sDefaultSong
wram_sram_map wDebugPauseAllowed, 1, $00, $ff ; sDebugPauseAllowed
wram_sram_map wRonaldIsInMap, 1, $00, $ff ; sRonaldIsInMap
wram_sram_map wMastersBeatenList, 10, $00, $ff ; sMastersBeatenList
wram_sram_map wNPCDuelistDirection, 1, $00, $ff ; sNPCDuelistDirection
wram_sram_map wMultichoiceTextboxResult_ChooseDeckToDuelAgainst, 1, $00, $ff ; sMultichoiceTextboxResult_ChooseDeckToDuelAgainst
wram_sram_map wd10e, 1, $00, $ff ; sb84b
wram_sram_map .EmptySRAMSlot, 15, $00, $ff ; sb84c
wram_sram_map .EmptySRAMSlot, 16, $00, $ff ; sb85b
wram_sram_map .EmptySRAMSlot, 16, $00, $ff ; sb86b
wram_sram_map wEventVars, 64, $00, $ff ; sEventVars
dw NULL
; fills an empty SRAM slot with zero
.EmptySRAMSlot:
db $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00
; save the game
; if c is 0, save the player at their current position
; otherwise, save the player in Mason's lab
_SaveGame:
ld a, c
or a
jr nz, .force_mason_lab
farcall BackupPlayerPosition
jr .save
.force_mason_lab
ld a, $2
ld [wTempPlayerXCoord], a
ld a, $4
ld [wTempPlayerYCoord], a
ld a, SOUTH
ld [wTempPlayerDirection], a
ld a, MASON_LABORATORY
ld [wTempMap], a
ld a, OWMAP_MASON_LABORATORY
ld [wOverworldMapSelection], a
.save
call SaveAndBackupData
ret
_AddCardToCollectionAndUpdateAlbumProgress:
ld [wCardToAddToCollection], a
push hl
push bc
push de
ldh a, [hBankSRAM]
push af
ld a, BANK(sAlbumProgress)
call BankswitchSRAM
ld a, [wCardToAddToCollection]
call AddCardToCollection
ld de, sAlbumProgress
call UpdateAlbumProgress
pop af
call BankswitchSRAM
call DisableSRAM ; unnecessary
; unintentional? runs the same write operation
; on the same address but on the current SRAM bank
ld a, [wCardToAddToCollection]
call AddCardToCollection
ld de, $b8fe
call UpdateAlbumProgress
pop de
pop bc
pop hl
ret
WriteBackupCardAndDeckSaveData:
ld bc, sCardAndDeckSaveDataEnd - sCardAndDeckSaveData
ld hl, sCardCollection
jr WriteDataToBackup
WriteBackupGeneralSaveData:
ld bc, sGeneralSaveDataEnd - sGeneralSaveData
ld hl, sGeneralSaveData
; fallthrough
; bc = number of bytes to copy to backup
; hl = pointer in SRAM of data to backup
WriteDataToBackup:
ldh a, [hBankSRAM]
push af
.loop
xor a ; SRAM0
call BankswitchSRAM
ld a, [hl]
push af
ld a, BANK("SRAM2")
call BankswitchSRAM
pop af
ld [hli], a
dec bc
ld a, b
or c
jr nz, .loop
pop af
call BankswitchSRAM
call DisableSRAM
ret
LoadBackupCardAndDeckSaveData:
ld bc, sCardAndDeckSaveDataEnd - sCardAndDeckSaveData
ld hl, sCardCollection
jr LoadDataFromBackup
LoadBackupGeneralSaveData:
ld bc, sGeneralSaveDataEnd - sGeneralSaveData
ld hl, sGeneralSaveData
; fallthrough
; bc = number of bytes to load from backup
; hl = pointer in SRAM of backup data
LoadDataFromBackup:
ldh a, [hBankSRAM]
push af
.loop
ld a, BANK("SRAM2")
call BankswitchSRAM
ld a, [hl]
push af
xor a
call BankswitchSRAM
pop af
ld [hli], a
dec bc
ld a, b
or c
jr nz, .loop
pop af
call BankswitchSRAM
call DisableSRAM
ret
|