Compute checksum for extended SRAM

Validate SRAM on file screen load and delete if checksum incorrect
This commit is contained in:
cassidoxa
2022-07-25 22:19:06 -04:00
parent 0e6e5d5b45
commit 10079a539b
3 changed files with 56 additions and 18 deletions

View File

@@ -188,7 +188,7 @@ JSL.l AltBufferTable : NOP #8 ; Delete screen
;--------------------------------------------------------------------------------
org $0CCCCC ;<- 64CCC - Bank0C.asm : 1628 (JSL Intro_ValidateSram) / Bank02.asm : 75 (REP #$30)
; Explanation: In JP 1.0 the code for Intro_ValidateSram was inline in Bank 0C
JML.l Validate_SRAM ;(Return via RTL. Original code JML'd to Intro_LoadSpriteStats which returns with RTL, but we want to skip that)
JML.l ValidateSRAM ;(Return via RTL. Original code JML'd to Intro_LoadSpriteStats which returns with RTL, but we want to skip that)
org $0CCD57 ;<- 64D57 - Bank0C.asm :
RTL ;Just in case anybody ever removes the previous hook
;--------------------------------------------------------------------------------
@@ -299,12 +299,17 @@ BRA +
;================================================================================
; Extended SRAM Save file
;--------------------------------------------------------------------------------
org $0ccf08 ; <- Bank0C.asm : 2036 (LDA.w #$0007 : STA $7EC00D : STA $7EC013)
org $0CCF08 ; <- Bank0C.asm : 2036 (LDA.w #$0007 : STA $7EC00D : STA $7EC013)
JSL CopyExtendedSaveFileToWRAM
;--------------------------------------------------------------------------------
org $008998 ; <- Bank00.asm : 1296 (LDX.w #$0000)
JSL CopyExtendedWRAMSaveFileToSRAM
;--------------------------------------------------------------------------------
org $00899C ; <- bank_00.asm : #_00899C (CLC)
JSL WriteSaveChecksum
LDA.w #$01F3 : TCS : SEP #$30 : PLB : RTL ; Get the stack and data bank correct
padbyte $FF : pad $0089C2 ; Fill adjacent free rom forward. See bank_00.asm: #_0089C2
;--------------------------------------------------------------------------------
org $0CD7AB ; <- Bank0C.asm : 3342 (STA $700400, X)
JSL.l ClearExtendedSaveFile
;--------------------------------------------------------------------------------
@@ -2807,4 +2812,4 @@ if !FEATURE_NEW_TEXT
JSL RenderCharSetColorExtended_init
org $0EF285
JSL RenderCharSetColorExtended_close : NOP
endif
endif

View File

@@ -1,12 +1,40 @@
;--------------------------------------------------------------------------------
Validate_SRAM:
REP #$30 ; vanilla behavior from $0CCD45, includes prize pack reset after save and quit
LDX #$00FE : -
STZ $0D00, X
STZ $0E00, X
STZ $0F00, X
DEX #2
BPL -
WriteSaveChecksum:
LDX.w #$0000 : TXA : - ; Checksum first $04FE bytes
CLC : ADC.l SaveDataWRAM, X
INX #2
CPX #$04FE : BNE -
LDX.w #$0000 : - ; Checksum extended save data
CLC : ADC.l ExtendedFileNameWRAM, X
INX #2
CPX #$0FFE : BNE -
STA.b $00
LDA.w #$5A5A
SEC : SBC.b $00
STA.l $7004FE
RTL
;--------------------------------------------------------------------------------
ValidateSRAM:
REP #$30
LDX.w #$0000 : TXA : - ; Checksum first $04FE bytes
CLC : ADC.l CartridgeSRAM,X
INX #2
CPX.w #$04FE : BNE -
LDX.w #$0000 : - ; Checksum extended save data
CLC : ADC.l ExtendedFileNameSRAM, X
INX #2
CPX #$0FFE : BNE -
STA.b $00
LDA.w #$5A5A
SEC : SBC.b $00
CMP.l InverseChecksumSRAM : BEQ +
TDC : STA.l FileValiditySRAM : + ; Delete save by way of zeroing validity marker
LDX.w #$00FE : - ; includes prize pack reset after save and quit
STZ $0D00, X
STZ $0E00, X
STZ $0F00, X
DEX #2
BPL -
SEP #$30
RTL
;--------------------------------------------------------------------------------
@@ -23,6 +51,11 @@ ClearExtendedSaveFile:
STA $700D00, X
STA $700E00, X
STA $700F00, X
STA $701000, X
STA $701100, X
STA $701200, X
STA $701300, X
STA $701400, X
RTL
;--------------------------------------------------------------------------------
ClearExtendedWRAMSaveFile:
@@ -125,5 +158,4 @@ CopyExtendedWRAMSaveFileToSRAM:
REP #$30
PLB
PLA
LDX.w #$0000 : TXA ; what we wrote over
RTL

View File

@@ -10,6 +10,7 @@
;--------------------------------------------------------------------------------
pushpc
org 0 ; This module writes no bytes. Asar gives bank cross errors without this.
SaveDataWRAM = $7EF000
;================================================================================
; Room Data ($7EF000 - $7EF27F
@@ -366,7 +367,7 @@ GTCollectedKeys: skip 1 ; / Ganon's Tower
skip 2 ; Reserved for previous table
FileMarker: skip 1 ; $FF = Active save file | $00 = Inactive save file
skip 13 ; Unused
InverseChecksum: skip 2 ; Vanilla Inverse Checksum. Don't write unless computing checksum.
InverseChecksumWRAM: skip 2 ; Vanilla Inverse Checksum. Don't write unless computing checksum.
;================================================================================
; Expanded SRAM ($7F6000 - $7F6FFF)
@@ -379,8 +380,7 @@ ExtendedFileNameWRAM: skip 24 ; File name, 12 word-length characters.
RoomPotData: skip 592 ; Table for expanded pot shuffle. One word per room.
SpritePotData: skip 592 ; Table for expanded pot shuffle. One word per room.
PurchaseCounts: skip 96 ; Keeps track of shop purchases
PrivateBlock: skip 512 ; Reserved for 3rd party developers
DummyValue: skip 1 ; $01 if you're a real dummy
PrivateBlock: skip 513 ; Reserved for 3rd party developers
;================================================================================
; Direct SRAM Assignments ($700000 - $7080000)
@@ -403,7 +403,8 @@ ProgressIndicatorSRAM: skip 1 ;
skip 19 ;
FileNameVanillaSRAM: skip 8 ; First four characters of file name
FileValiditySRAM: skip 2 ;
skip 285 ;
skip 283 ;
InverseChecksumSRAM: skip 2 ;
ExtendedFileNameSRAM: skip 24 ; We read and write the file name directly from and to SRAM (24 bytes)
skip $1AE4 ;
RomVersionSRAM: skip 4 ; ALTTPR ROM version. Low byte is the version, high byte writes
@@ -515,7 +516,7 @@ endmacro
%assertSRAM(FollowerDropped, $7EF3D3)
%assertSRAM(FileNameVanillaWRAM, $7EF3D9)
%assertSRAM(FileValidity, $7EF3E1)
%assertSRAM(InverseChecksum, $7EF4FE)
%assertSRAM(InverseChecksumWRAM, $7EF4FE)
;================================================================================
; Randomizer Assertions
@@ -640,7 +641,6 @@ endmacro
%assertSRAM(SpritePotData, $7F6268)
%assertSRAM(PurchaseCounts, $7F64B8)
%assertSRAM(PrivateBlock, $7F6518)
%assertSRAM(DummyValue, $7F6718)
;================================================================================
; Direct SRAM Assertions
@@ -654,6 +654,7 @@ endmacro
%assertSRAM(ProgressIndicatorSRAM, $7003C5)
%assertSRAM(FileNameVanillaSRAM, $7003D9)
%assertSRAM(FileValiditySRAM, $7003E1)
%assertSRAM(InverseChecksumSRAM, $7004FE)
%assertSRAM(ExtendedFileNameSRAM, $700500)
%assertSRAM(RomNameSRAM, $702000)
%assertSRAM(RomVersionSRAM, $701FFC)