Save data corruption on flashcarts?

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems.

Moderator: Moderators

Post Reply
User avatar
Vectrex2809
Posts: 97
Joined: Mon Jul 14, 2014 6:05 am
Location: Tokyo, Japan

Save data corruption on flashcarts?

Post by Vectrex2809 »

Currently, I'm about to finish one of my NES projects and it seems like save game data gets sometimes corrupted when using it on a flashcart according to my gametesters (See pictures on how it looks like and how it's supposed to look like). The save game data that's causing problems is the player name and the arrow positions which are supposed to be kept if the game is turned off. To quote one of my gametesters - "I am seeing another possible issue though. This has happened twice to me now. Not sure if it is the powerpak or not, but I will run the game for hours upon hours at a time and have no problems. I will turn off the game for a day or so, and then when I turn it back on the screen where you make your name, choose the music, and that good stuff trips out. The letter options for the name are shapes, symbols, and what looks like heiroglyphics. The arrows where you choose the music, see high score and stuff, are above the options, and can go all the way to the left and right side of the screen.
you can continue off that screen, but the name you chose are the same shapes and symbols.

I can remove the file from my powerpak and put the one you gave me back on, and it works fine again until you turn it off for awhile, and then it does it again
".
Also, when I play my game on NEStopia for mac, the baterry backed WRAM works flawlessly, however, it doesn't on FCEUX for Windows (Using Parallels Desktop), at least when you close the emulator. The game data just resets...

Image How it's supposed to look like

Image

Image

Image How it looks like on a PowerPak


Here's some of my code. It's from an outdated version of my game but it is essentially the same in later versions (Don't have my src with me, sorry)

Code: Select all

  LDA wramcheck
  BNE NoReset
  LDX #$00

Reset_VRAM:		;Resets VRAM if needed

  LDA #$00			;Puts zeros on all VRAM vatiables in the $6000 range
  STA $6000,x
  INX
  CPX #$00
  BNE Reset_VRAM

  LDA #$7F
  STA playername
  STA playername+1
  STA playername+2
  STA playername+3
  STA playername+4
  STA playername+5
  STA playername+6
  STA playername+7

  LDA #$8F
  STA music
  LDA #$D5
  STA scores
  LDA #$11
  STA ok

  INC wramcheck

NoReset:
This is the WRAM reset routine. the wramcheck variable is only supposed to go back to zero when you do a specific task.

Don't know if you need more code. I'm sorry but I don't have my SRC with me since I'm on a vacation
User avatar
tokumaru
Posts: 12106
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Save data corruption on flashcarts?

Post by tokumaru »

Only one byte working as a flag to indicate the validity of WRAM? You shoul implement a chacksum or something stronger.

Also, are you only using $6000-$60FF?
User avatar
koitsu
Posts: 4203
Joined: Sun Sep 19, 2004 9:28 pm
Location: A world gone mad

Re: Save data corruption on flashcarts?

Post by koitsu »

Yeah, a proper CRC check should be used. What I've seen done in commercial games (SNES/SFC titles, however) is that they use a portion of battery-backed SRAM for the actual data, but write it to multiple locations along with a CRC stored with the data. E.g. $6000-60FD would be your data, with the CRC of all data in $60FE-60FF, then $6100-61FD would be a copy of the same data in $6000-60FD and CRC in $61FE-61FF, etc...

This allowed some redundancy in case some of the areas of SRAM had issues, e.g. if CRC for $6000-60FD didn't match, try the next "section", yadda yadda. How much redundancy you want is up to you, but I imagine 3 copies would be enough.
Sik
Posts: 1589
Joined: Thu Aug 12, 2010 3:43 am

Re: Save data corruption on flashcarts?

Post by Sik »

If I recall correctly that was mentioned in the SNES manual.
User avatar
Vectrex2809
Posts: 97
Joined: Mon Jul 14, 2014 6:05 am
Location: Tokyo, Japan

Re: Save data corruption on flashcarts?

Post by Vectrex2809 »

There's not too much save game data in the game indeed. To tell you the truth, I was thinking about using WRAM for the name entry screen and the high scores only. I'll try to implement a checksum, but I was also thinking about dropping the save feature since there's not too much data to be saved...
User avatar
Vectrex2809
Posts: 97
Joined: Mon Jul 14, 2014 6:05 am
Location: Tokyo, Japan

Re: Save data corruption on flashcarts?

Post by Vectrex2809 »

Also, just wondering. Is there some relation with such data corruption and the infamous "Hold reset while turning off unit" message with the Legend of Zelda and other battery-backed games?
User avatar
thefox
Posts: 3139
Joined: Mon Jan 03, 2005 10:36 am
Location: Tampere, Finland
Contact:

Re: Save data corruption on flashcarts?

Post by thefox »

Some older PowerPak mappers may have problems with WRAM corruption, although I'm not sure if that applies here.
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi
Sik
Posts: 1589
Joined: Thu Aug 12, 2010 3:43 am

Re: Save data corruption on flashcarts?

Post by Sik »

Depends entirely on the mapper I believe. Some mappers pay attention to whether the CPU is writing or not (those are safe) but some (most?) just check if the CPU accesses a given address, regardless of whether it's read or write (those are not safe).
User avatar
thefox
Posts: 3139
Joined: Mon Jan 03, 2005 10:36 am
Location: Tampere, Finland
Contact:

Re: Save data corruption on flashcarts?

Post by thefox »

Sik wrote:Depends entirely on the mapper I believe. Some mappers pay attention to whether the CPU is writing or not (those are safe) but some (most?) just check if the CPU accesses a given address, regardless of whether it's read or write (those are not safe).
The bug I was referring to was PowerPak-specific, specifically this: viewtopic.php?p=89777#p89777
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi
Sik
Posts: 1589
Joined: Thu Aug 12, 2010 3:43 am

Re: Save data corruption on flashcarts?

Post by Sik »

I was talking in general, the reason many NES games tell you to hold down the reset button while turning off is because they have risk of the mapper allowing writes to save RAM during the unstable period while it shuts off, potentially corrupting the data in it.
User avatar
koitsu
Posts: 4203
Joined: Sun Sep 19, 2004 9:28 pm
Location: A world gone mad

Re: Save data corruption on flashcarts?

Post by koitsu »

"Hold reset on NES" subject has already been discussed before (2nd thread relates to SNES but see tokumaru's post for relevancy; best guess is same thing as what's stated in previous thread):

viewtopic.php?f=9&t=10949
viewtopic.php?f=5&t=11962

Stay focused! :-)
User avatar
thefox
Posts: 3139
Joined: Mon Jan 03, 2005 10:36 am
Location: Tampere, Finland
Contact:

Re: Save data corruption on flashcarts?

Post by thefox »

Sik wrote:I was talking in general, the reason many NES games tell you to hold down the reset button while turning off is because they have risk of the mapper allowing writes to save RAM during the unstable period while it shuts off, potentially corrupting the data in it.
This doesn't apply to PowerPak though, because PowerPak has no battery backing for the SRAM (it's saved to the CF card while the system power is on), so the save data never has a chance of getting corrupted due to powering the system off.
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi
Pokun
Posts: 1923
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: Save data corruption on flashcarts?

Post by Pokun »

That's good to know. On the contrary the Everdrive uses battery backed SRAM which contains the save data until another game is loaded in which case it's copied to the SD card (If you play the same game again the next time you turn on it's never copied though). So I guess holding reset is needed on the Everdrive.

What mappers doesn't require holding reset? I remember reading in a Swedish Nintendo magazine that Nintendo worked on a way to protect the save data, but I never heard of a game that used that.
tepples
Posts: 22345
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Save data corruption on flashcarts?

Post by tepples »

The "Why Game Paks Never Forget" article in Nintendo Power states that MMC5 contains improved protection circuitry that doesn't require holding Reset.

And perhaps MMC6, which has independent write protection for both banks and whose SRAM size is too small to use as work RAM, might be less sensitive to bus noise during power loss.
Pokun
Posts: 1923
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: Save data corruption on flashcarts?

Post by Pokun »

I see. Figures it was MMC5.
Post Reply