News: 11 March 2016 - Forum Rules
Current Moderators - DarkSol, KingMike, MathOnNapkins, Azkadellia, Danke

Author Topic: How do you make an SRAM hack?  (Read 7386 times)

PKstarship

  • Jr. Member
  • **
  • Posts: 44
    • View Profile
How do you make an SRAM hack?
« on: September 22, 2015, 08:37:10 am »
I've noticed that there is a lack of SRAM patches for most of the NES Mega Man games, so I wanted to make patches for them. How might I go about trying to do this?

KingMike

  • Forum Moderator
  • Hero Member
  • *****
  • Posts: 7015
  • *sigh* A changed avatar. Big deal.
    • View Profile
Re: How do you make an SRAM hack?
« Reply #1 on: September 22, 2015, 09:48:22 am »
Learn ASM. (that's the hard part)
Learn how Mega Man's passwords are represented in RAM. (usually not so hard once you know the above)
And then it should be easy.
"My watch says 30 chickens" Google, 2018

FAST6191

  • Hero Member
  • *****
  • Posts: 2920
    • View Profile
Re: How do you make an SRAM hack?
« Reply #2 on: September 22, 2015, 10:23:57 am »
You want to turn a game that had no SRAM type save (presumably/hopefully a password save though) into one with one? The alternative is some kind of SRAM launched cheat/hack which has been seen many times (usually for hacking the console itself but hey).

For the NES it is a bit more annoying than some later systems but anyway there are 3 main options

1) It was there in beta/development but disabled for the final release. You then enable it again.
2) It was there in another region. Same deal really except it might be easier to backport the translation instead.
3) You actually hack the game, this takes various forms.

The reason I said presumably/hopefully it has a password save system is because that makes things considerably easier. In those cases you will probably want to just figure out where the game stores the data behind the password when it displays it to you when you quit/end a level/whatever, put that into SRAM and (as it will likely be the same as the data used by the password entry screen) then fetch said data from SRAM and put it into the decode point for the password restore screen. If you are really good then you can also store things the password might lose; I usually use the example of Road Rash on the megadrive/genesis that will store you bike, money and progress but not your current weapon (they are easy enough to get in a race but there are some desirable ones that might not be in a given race or benefit you to have earlier in the race, whether this was an intended effect I will leave for another discussion).
If the password is not a binary storage mechanism ( http://tasvideos.org/PasswordGenerators.html ) and instead some kind of glorified level select ( http://uk.ign.com/cheats/games/worms-2-pc-3143 ) then it might be even easier.

The biggest trick for the NES will probably involve mappers (and on cart chips for the SNES or possibly memory bank controllers/mbcs for the GB/GBC) as such things often do on the NES and such things will vary -- if there is not one there then you are going to want the mapper setup to gain itself a SRAM bank (or sacrifice an existing bank to become one), and then you are going to have to do the bank handling/restore. Something like the GBA on the other hand has SRAM (or EEPROM or Flash) mapped directly in memory and beyond having to observe access protocols, wait periods and read lengths (all simple enough as far as such things go) it is nothing pressing at all and if the emulator or flash cart handling program supports the save type (you would be hard pressed to be troubled here) then you are good to go.

Redoing all the menus would make this a far longer hack so if you want to leave the password display and restore functionality mostly intact then it is not going to hurt anybody, give or take people maybe overwriting a save if they are not careful.

The other type of one is for when a game has no save at all and not so much as a password system. Not as common on the NES for games that would really benefit from it as it was on the older consoles (see also the jokes/comments with people leaving the console on over night/over dinner/over the school day...) but far from unheard of as well. Not a fun thing to do if you can help it but not impossible either, just long.

Two approaches here.
1) A savestate is the ultimate form of this but doing that in hardware, especially without extra hardware like a good flash cart or debugging board, is hard, even if you somehow have one of those save game chips that is comparable to the amount of memory a given system has.
You work backwards from this chopping out things you do not need.

2) You make a save. Kind of the reverse approach from above -- where you would trim a savestate for the above here you would figure out what the game/player needs and implement a full save encode and decode function. So you reckon level progress, score, maybe some upgrades....
For some games this might be trivial as they have no score/lives/whatever and you can just trigger a branch that ends up starting level 4 or whatever, for others it is going to be a nightmare as you figure out what is needed.

In both cases you are probably going to have to map the memory out and figure out what it what. For the password based stuff you might well never even have to know what the password is composed of as far as data, checksums and whatever else.

I do not have an example of this second kind of hack to hand. The vast majority will be unlocked functionality from dev/beta or another region, or it will be some kind of password system based hack. Likewise it is probably no coincidence that a game series as well hacked/disassembled/understood as home console megaman was picked.

I have a feeling I trailed off somewhere in there and forgot something important but I have to go do something so I will leave it there.

KingMike

  • Forum Moderator
  • Hero Member
  • *****
  • Posts: 7015
  • *sigh* A changed avatar. Big deal.
    • View Profile
Re: How do you make an SRAM hack?
« Reply #3 on: September 22, 2015, 12:12:31 pm »
The first Mega Man game had no save at all, the rest had a password that was a grid of dots that had to be filled in.
Unless you dug deep enough into a disassembly (I think someone did one for MM1 but I don't think so on the rest), you probably wouldn't be able to just delete the password screen entirely, so the best route you could go (again, with ASM hacking) would be to have the password data stored to SRAM when it is given to you (after game over or finishing a level), then at the time the player loads the password input screen, load the SRAM to make the password auto-entered (so the player just has to press Start to confirm, same effect as if they had entered it manually).
This "auto-entry" method is pretty much what Classic NES Metroid for the GBA (the US/EU standalone release as well as the unlockable in Zero Mission for all regions) did.
"My watch says 30 chickens" Google, 2018

henke37

  • Hero Member
  • *****
  • Posts: 643
    • View Profile
Re: How do you make an SRAM hack?
« Reply #4 on: September 23, 2015, 02:45:31 am »
It is generally rather easy to skip entire screens. Deleting them may be hard, but not skipping them. A carefully placed jump is all it takes for the screen to be bypassed.

Vanya

  • Hero Member
  • *****
  • Posts: 1789
    • View Profile
Re: How do you make an SRAM hack?
« Reply #5 on: September 23, 2015, 04:20:24 am »
Funny this should come up just now. I just tried out that Castlevania RPG hack that was posted a few days ago and thought to myself that a save feature for this hack would be particularly useful and fitting.
The FDS version of CV1 had a save feature, but I'm pretty confident that the NES version no longer has any of that code left in it.
So I guess I'd be left with having to build it from scratch.

RetroRain

  • Sr. Member
  • ****
  • Posts: 287
    • View Profile
Re: How do you make an SRAM hack?
« Reply #6 on: September 23, 2015, 06:23:57 am »
SRAM hacks for the NES are a piece of cake.  I did one for Megaman 1 (Upgrade Patch) and Megaman 2 (The Mystery of Dr. Wily), and I halfway did one for Super Mario Bros. (not released).

All you have to do is figure out what you want saved (completed levels, lives, score, etc.), and have those addresses written to one of the SaveRAM addresses ($6000 - $7FFFF), at the right point/s in the game when you want those things saved.  For instance, you can check for writes to those addresses after you beat a level.  And then, at the right point/s you want them loaded, simply read those values stored in $6000 - $7FFF and write them back to the addresses you took those values from.

I think it only took me 2 or 3 days to do the SRAM hack for Megaman 2.  A little bit longer for Megaman 1, since I had to program an option on the titlescreen to erase your saved data.  It all depends on how much time you have in the day.  They don't take that long to do.

If you've never done a SRAM hack before, I recommend starting out small, and try to save one thing.  Trying having the game save your completed levels or lives after beating a level, or something like that.

PKstarship

  • Jr. Member
  • **
  • Posts: 44
    • View Profile
Re: How do you make an SRAM hack?
« Reply #7 on: September 23, 2015, 08:57:07 am »
I think once I learn more ASM I'll start then. I'm guessing though that I load the said value(s) into accumulator, store it in the SRAM address(es), and when you continue, make it load it those values the accumulator again, and back into the correct slots?

Rockman, I really loved your SRAM hack of Mega Man 2. I really hate passwords and love seeing them patched out. You're actually one of the reasons I wanted to make SRAM hacks :)

Also, is the SRAM address you mentioned just for the Mega Man games, or for every game on the NES?
« Last Edit: September 23, 2015, 09:08:18 am by PKstarship »

Vanya

  • Hero Member
  • *****
  • Posts: 1789
    • View Profile
Re: How do you make an SRAM hack?
« Reply #8 on: September 23, 2015, 10:09:12 pm »
I'd like to know that, too.

Also, I have pretty much no idea where to begin when it comes to adding features to a title screen that had none at all.
Well, that's not entirely true.
For something like CV1, there is already code that runs on the title screen that is waiting for the start button to be pressed.
So I can start with that I suppose. I could mod that code to branch to some custom loading code based one some unused byte or bit.
And there is one sprite on the US title screen, too.
I could probably use that as a starting point for creating a pointer like the one in the Japanese CV1 for NES.

Also, does UNROM support SRAM or would it require a conversion to another mapper?

Disch

  • Hero Member
  • *****
  • Posts: 2814
  • NES Junkie
    • View Profile
Re: How do you make an SRAM hack?
« Reply #9 on: September 23, 2015, 11:31:27 pm »
I think once I learn more ASM I'll start then. I'm guessing though that I load the said value(s) into accumulator, store it in the SRAM address(es), and when you continue, make it load it those values the accumulator again, and back into the correct slots?

Yes.  SRAM is battery backed, so any values you write to it will persist between power cycles.  So anything you write to the $6000-7FFF range is effectively "saved".

Though commercial games almost always include a sort of checksum or CRC along with the save data to prevent against memory corruption.

But really... getting it to save is the easy part.  The hard part is creating an in-game interface to save/load the game.

Quote
Also, is the SRAM address you mentioned just for the Mega Man games, or for every game on the NES?

Technically it depends on the mapper.
Though 99% of mappers will put SRAM at $6000-7FFF ... so yes, it's always that address range.


Also, I have pretty much no idea where to begin when it comes to adding features to a title screen that had none at all.

It's not exactly easy.  You have to not only change what is drawn on the screen, but you'll probably also have to add a cursor sprite, as well as adding logic to look for and respond to button presses.

This kind of thing is at least an intermediate level asm hack, as it not only demands a reasonably firm grasp on fundamentals, but also requires you to dig into existing code, learn how it works, and add on to it.

Quote
Also, does UNROM support SRAM or would it require a conversion to another mapper?

I don't think UNROM supports SRAM.  But "mapper 2" is not strictly UNROM... but is "UNROM and compatible", and several existing mapper 2 games do have SRAM.  So you'd be fine and wouldn't have to change mappers.

For emulation purposes this would be fine -- but if you plan to put this on real hardware you'd probably be better off switching to another mapper.

PKstarship

  • Jr. Member
  • **
  • Posts: 44
    • View Profile
Re: How do you make an SRAM hack?
« Reply #10 on: September 24, 2015, 12:35:22 am »
As far as interfaces go for me adding SRAM to Mega Man, I want to change the "Pass Word" option on the starting screen to "Continue", and possibly change the other option to "New Game". I would just make the game load the save and jump to the stage select. I'm not sure what to do on the screen after you beat the bosses, it always shows the password. Maybe I'll get rid of the grid and make it say "Progress Saved" instead.

I also want to make it so that after you beat the game and load the save, it starts you back at the stage select, so you can play the Robot Master levels again, or go through Wily's/Cossack's/Dr. X's castle again.

RetroRain

  • Sr. Member
  • ****
  • Posts: 287
    • View Profile
Re: How do you make an SRAM hack?
« Reply #11 on: September 24, 2015, 01:13:06 am »
Rockman, I really loved your SRAM hack of Mega Man 2. I really hate passwords and love seeing them patched out. You're actually one of the reasons I wanted to make SRAM hacks :)

Thank you!  I appreciate that!  It means a lot to me. :)

Some documents you'll want to get, if you're going to dive into NES ASM hacking:

- 6502.txt
- NEStech.txt
- mappers-0.80.txt

And definitely get FCEUX 2.2.2, if you don't have it already.  It will be of tremendous value to you when doing ASM hacking for the NES.

Check out some of my posts on board2 (http://acmlm.kafuka.org).  I have discussed about ASM hacking on more than one occasion.

The simplest ASM hack in my opinion is the infinite lives ASM hack.  And it can be done to just about any game.

Just find the write to the lives address (most likely a STA, just set a breakpoint to find out), after your player dies, and change that STA to a LDA.  Or, you can NOP out the code that decrements the lives address.  There is more than one way to do it.  For starting out though, I would just find the STA and change it to an LDA.

Disch

  • Hero Member
  • *****
  • Posts: 2814
  • NES Junkie
    • View Profile
Re: How do you make an SRAM hack?
« Reply #12 on: September 24, 2015, 01:59:48 am »
- 6502.txt
- NEStech.txt
- mappers-0.80.txt

Holy 1998.   :P

6502.txt still holds up, but those other docs are questionable at best.

Really the best source for NES technical information is the wiki:

http://wiki.nesdev.com/w/index.php/NES_reference_guide  <- main page
http://wiki.nesdev.com/w/index.php/Mapper   <- mappers (by mapper number)



PS:  looks like I may have been wrong about mapper 2 allowing for SRAM.  Looks like you will have to switch mappers.


EDIT:

Sorry if I'm stepping on your toes here, Rockman.  Just trying to get the best info to PKstarship  ^^.  I mean no disrespect.



EDIT 2:

<plug>
If you want a downloadable reference for mappers, there's always my doc collection:
http://www.romhacking.net/documents/362/

Which is infinitely better than the ancient firebug doc -- but is slightly outdated and has a few inaccuracies.
</plug>

The wiki really is the place to go, though.
« Last Edit: September 24, 2015, 02:07:59 am by Disch »

PKstarship

  • Jr. Member
  • **
  • Posts: 44
    • View Profile
Re: How do you make an SRAM hack?
« Reply #13 on: September 24, 2015, 09:11:52 am »
Thank you for all of your help :) Sadly though Rockman, I can't get FCEUX, I don't have a PC. I have some DOS programs to help me though. I figured a SRAM hack would be somewhat simple to make.

One more thing, is it more difficult to make an SRAM hack for an SNES game? I'm a bit more familiar with SNES ASM, and I want to make a save hack for Mega Man X to help my friend with his hack.

Disch

  • Hero Member
  • *****
  • Posts: 2814
  • NES Junkie
    • View Profile
Re: How do you make an SRAM hack?
« Reply #14 on: September 24, 2015, 03:44:55 pm »
I can't get FCEUX, I don't have a PC.

Linux?  MacOS?

Working without FCEUX is a SERIOUS handicap.  FCEUX is huge.

Can you run it through Wine or a VM or something?

Quote
One more thing, is it more difficult to make an SRAM hack for an SNES game?

It's comparable.

It might be a little easier since SNES doesn't have to deal with bankswapping, so it's easier to add new code without having to worry about finding free space.

PKstarship

  • Jr. Member
  • **
  • Posts: 44
    • View Profile
Re: How do you make an SRAM hack?
« Reply #15 on: September 24, 2015, 04:53:43 pm »
Well, um... I do ASM and hacking stuff on an Android tablet. I have made ASM code before though, I made a routine for Super Metroid that made Samus have the Hyper Beam when she is at full health. Apperently FCEUX has been ported to Android, but without the debugging featues, sadly.

dougeff

  • Sr. Member
  • ****
  • Posts: 358
    • View Profile
Re: How do you make an SRAM hack?
« Reply #16 on: September 24, 2015, 04:55:09 pm »
: (

September 24, 2015, 05:05:37 pm - (Auto Merged - Double Posts are not allowed before 7 days.)
According to the internet, you can install Wine on Android devices, which should allow you to run windows apps on it. I have no idea if this would allow you to run FCEUX, or how well it would run ANY windows exe files.

September 24, 2015, 05:14:33 pm - (Auto Merged - Double Posts are not allowed before 7 days.)
Well, I couldn't find it, except a video of someone running Solitaire (windows exe) on an Android device. maybe its still a work in progress.
« Last Edit: September 24, 2015, 05:14:33 pm by dougeff »
nesdoug.com -- blog/tutorial on programming for the NES

RetroRain

  • Sr. Member
  • ****
  • Posts: 287
    • View Profile
Re: How do you make an SRAM hack?
« Reply #17 on: September 25, 2015, 12:16:43 am »
Sorry if I'm stepping on your toes here, Rockman.  Just trying to get the best info to PKstarship  ^^.  I mean no disrespect.

No offense taken.  I know you're just trying to be helpful. :)

Believe it or not, I still use those docs though.  I like them. :P

However, when it comes to doing MMC5 conversions, I use that article on nesdev.wiki.  I find it to be more accurate and informative than the info in mappers-0.80.txt.

ShadowOne333

  • Hero Member
  • *****
  • Posts: 1506
    • View Profile
Re: How do you make an SRAM hack?
« Reply #18 on: September 25, 2015, 12:37:15 am »
No offense taken.  I know you're just trying to be helpful. :)

Believe it or not, I still use those docs though.  I like them. :P

However, when it comes to doing MMC5 conversions, I use that article on nesdev.wiki.  I find it to be more accurate and informative than the info in mappers-0.80.txt.
Now that you mention the convertion patches...
I was wondering if you will ever go back to your Megaman Origins hack to add MM3 and have the full trilogy in a nice NES ROM.

RetroRain

  • Sr. Member
  • ****
  • Posts: 287
    • View Profile
Re: How do you make an SRAM hack?
« Reply #19 on: September 25, 2015, 01:42:26 am »
Now that you mention the convertion patches...
I was wondering if you will ever go back to your Megaman Origins hack to add MM3 and have the full trilogy in a nice NES ROM.

Megaman Origins is a "finished" hack, as far as I'm concerned.  If I were to ever make a hack that had Megaman 1, 2, and 3, I'd have to do a few things:

- Re-convert Megaman 1 to CHR-ROM (since it only exists as CHR-ROM in my Upgrade Patch), and simply make it a CHR-ROM conversion, with no other alterations.  It would obviously use MMC5.

- Megaman 3 would then need to be converted to MMC5.

After those two things are done, I would then have Megaman 1, 2, and 3, all having the same mapper and using CHR-ROM.

Then I'd put it all together, and just like I did with Megaman Origins, I'd make a special bootup screen, and then the hack would be called The Wily Wars.

So yeah, if I ever decide to make 3 games in 1, it will be a NES version of The Wily Wars.

That's if I decide that I want to do it.  I haven't entirely ruled it out, but I pulled my Megaman Origins hack for a reason, since the patch essentially contained the ROM file data for both games, which it did not even occur to me until someone told me about it.

So for legal reasons, I decided not to do anymore multi-cart hacks.