I have enabled SRAM in my header:
ROMEndLoc: dc.l EndOfRom-1 ; ROM End dc.l $FF0000 ; RAM Start dc.l $FFFFFF ; RAM End dc.l $5241F020 ; Backup RAM ID ; Enable SRAM dc.l $00200000 ; Backup RAM start address ; SRAM Start dc.l $00203FFF ; Backup RAM end address ; SRAM End dc.b " " ; Modem support dc.b " " ; Notes dc.b "JUE " ; Country EndOfHeader:
I know that values have to be read from an odd address in SRAM.
In my game, when you finish a level, a byte get's added to a RAM address called "logos". And that's what I want to save/load.
When moving one to logos, I do this at the end of the level:
SavelogostoSRAM: movem.l d0/a1,-(sp) ; Move d0 and a1 to stack move.b #1,($A130F1).l ; enable SRAM (required) lea ($00200001).l,a1 ; base of SRAM moveq #0,d0 ; Clear d0 move.b (logos).w,d0 ; move current logo RAM address to d0 move.b d0,0(a1) ; save logos to SRAM move.b #0,($A130F1).l ; disable SRAM (required) movem.l (sp)+,d0/a1 ; Move d0 and a1 from stack rts
I see no problem with this.
To load it, I do this when you press start at the title screen:
movem.l d0/a1,-(sp) ; Move d0 and a1 to stack move.b #1,($A130F1).l ; enable SRAM (required) lea ($00200001).l,a1 ; base of SRAM moveq #0,d0 ; Clear d0 move.b 0(a1),d0 ; load logo off SRAM into d0 move.b d0,(logos).w ; Otherwise, copy logos from SRAM to RAM move.b #0,($A130F1).l ; disable SRAM (required) movem.l (sp)+,d0/a1 ; Move d0 and a1 from stack rts
Seems good, right?
Well, here's the thing, it works perfectly, until you close the emulator down (Regen). When you reload, it will either do one of three things:
- Load what was saved there like it's meant to [rare]
- Loads the previous data from before (if it's written to the same SRAM twice, it will load what was first written there [most common after second level complete])
- Loads a complete random number [rare]
Gens emulator does the exact same thing, and Kega seems to ignore the SRAM altogether (even though SRAM isn't disabled on Kega)
Where can I be going wrong? Any ideas?
EDIT: Here's a funny story. I delete the s2built.srm file so there is no more save data, and as soon as I load the hack again, it immediately returns, and when I press start of the title screen, it loads the previous data from before. How does that work out?
EDIT2: It's the emulator shifting it. When I close the emulator down, the SRM file is created. I open the file in a hex editor, and it's all FF's except for that one write I did, but it's at location 0x000002 (not 0x000001). When I reopen the game, as suspected, it doesn't load it (because it's now loading FF which is at 0x000001).
I closed and rep-open the emulator and game. I then closed and re-open the same SRM file in the hex editor (which hasn't been touched by the game yet), and now the one write I did earlier which was at 0x000002 is now at 0x000003! Did the same thing again, now it's at 0x000001 (this is when the game loaded the data correctly). Did it again the data is back at 0x000003, etc, etc.
So the emulator is shifting it all the time. But then again, Regen, Kega and Gens all do this, maybe I've gone wrong at the header...