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

Author Topic: Pac-Man NES hi-score saving hack attempt...  (Read 1866 times)

hamguy

  • Newbie
  • *
  • Posts: 4
    • View Profile
Pac-Man NES hi-score saving hack attempt...
« on: October 28, 2015, 04:20:31 am »
Hello.
The only thing I hate more than passwords in NES games is when an NES game has a "hi-score," yet doesn't save it. It makes me wonder why they put a score in it in the first place. Anyway, I've decided to stop waiting for others to hopefully do it, and do it myself. I won't stop until every last (good) NES game has SRAM saving of some kind.

I was going to start with Donkey Kong, but thank the heavens, someone already made a score saving patch for that. So I decided to do Pac-Man first. Using FCEUX, I believe I've found where the hi-score is in the hex editor. And using virtuanes, I made it have SRAM (I think).

But now I have absolutely no clue what to do next. I think I know what I need to do; find where the SRAM is and put the hi-score there, but I'm totally lost. And I don't even know WHAT to look for as far as guides that would tell me what to do next. I don't really know what else to say, except that if I can learn to do this, passwords in NES games will be a thing of the past.

Thank you for your time.

goldenband

  • Sr. Member
  • ****
  • Posts: 296
    • View Profile
Re: Pac-Man NES hi-score saving hack attempt...
« Reply #1 on: October 28, 2015, 12:41:12 pm »
I don't actually know the answer to this question, but I'm going to think through it so that -- following Cunningham's Law -- we'll both get the right answer out of it. :D

OK, so here are the steps I would expect to follow:

1) Find out where (at what memory locations) the high score value is stored. I've never used them for the NES, but there should be debug tools that allow you to search RAM for a particular value, or to notify you when a value is incremented. The tricky thing is that the score can be stored in several different formats (e.g. binary-coded decimal), and with different byte orders.

2) Next, you need to write a short piece of assembly code that will read those memory locations, and write them to the SRAM. I think that's just a matter of the assembly instructions LDA and STA (not sure if A is the right register to use), and it should be easy enough to find out what the opcodes for those are so they can be written in pure hex. I'd write them here, but I've programmed very little assembly code, and don't fully understand the different address modes like zero-page and absolute; some of it's noted here. The other thing you need to know is the memory location of the SRAM, and that's available from various docs AFAIK.

3) You'll also want to write a short piece of code that does the reverse of the above when the game first boots up: reads the SRAM, and writes the high score to RAM. That, in itself, should be easy if you can do #2.

4) Now, this is the intimidating part: you need to find unused or reclaimable space in the ROM to put in the code you've written, and a small amount of space to put in JSR (jump to subroutine) instructions to jump to that code and execute it. Just inserting the instructions will screw up the ROM completely, so I think what most people do is to find existing commands that can be NOPed and replace them with JSRs of the same byte length. One JSR needs to be executed when the game first boots up, and only then; the other needs to happen at some point in the GAME OVER sequence.

As for the subroutines themselves, it's usually much easier to find unused space at the end of a ROM page to put in instructions, so my naive impulse is to put the code somewhere with a lot of $00 or $FF, JSR there, RTS back, and hope for the best. With a small ROM that may be OK, but in a big one there's apt to be some sort of mapper or bank-switching disaster.

OK, now that I've given you what I assume is a terrible and naive answer, I look forward to reading a better one! :D

dougeff

  • Sr. Member
  • ****
  • Posts: 359
    • View Profile
Re: Pac-Man NES hi-score saving hack attempt...
« Reply #2 on: October 28, 2015, 05:30:22 pm »
I think you might need to edit the header to tell the emulator that you have SRAM and/or a mapper that has SRAM capabilities. Though, I think some emulators just assume all roms use SRAM.
nesdoug.com -- blog/tutorial on programming for the NES

goldenband

  • Sr. Member
  • ****
  • Posts: 296
    • View Profile
Re: Pac-Man NES hi-score saving hack attempt...
« Reply #3 on: October 28, 2015, 05:32:38 pm »
I think you might need to edit the header to tell the emulator that you have SRAM and/or a mapper that has SRAM capabilities. Though, I think some emulators just assume all roms use SRAM.

Absolutely. Here's the iNES header format:

http://wiki.nesdev.com/w/index.php/INES

hamguy

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Pac-Man NES hi-score saving hack attempt...
« Reply #4 on: October 28, 2015, 05:45:21 pm »
Thanks for your response, Goldenband!

2) Next, you need to write a short piece of assembly code that will read those memory locations, and write them to the SRAM. I think that's just a matter of the assembly instructions LDA and STA (not sure if A is the right register to use), and it should be easy enough to find out what the opcodes for those are so they can be written in pure hex. I'd write them here, but I've programmed very little assembly code, and don't fully understand the different address modes like zero-page and absolute; some of it's noted here. The other thing you need to know is the memory location of the SRAM, and that's available from various docs AFAIK.

3) You'll also want to write a short piece of code that does the reverse of the above when the game first boots up: reads the SRAM, and writes the high score to RAM. That, in itself, should be easy if you can do #2.

I have NO clue what any of that means. Like, none at all.....
However, I'm not going to give up! I'm determined to do this!
Thank you very much for telling me about it though, because I will learn what it all means. The link to 6502.org will be a huge help, I think.

I don't have a lot of free time to work on it at the moment but I will soon, and I won't stop until I've succeeded.
Thanks again!

KingMike

  • Forum Moderator
  • Hero Member
  • *****
  • Posts: 6882
  • *sigh* A changed avatar. Big deal.
    • View Profile
Re: Pac-Man NES hi-score saving hack attempt...
« Reply #5 on: October 28, 2015, 10:47:07 pm »
Also with these very small SRAM hacks (high score, and like that Ninja Gaiden hack that sounds like it just saves the level number), I wonder if they have any sort of check (such as a checksum) that the SRAM data is initialized and valid? (for only one or a few bytes, I'd recommend adding some dummy non-zero data around it to checksum so it shouldn't give a false positive)
I would not recommend assuming the emulator will initialize the SRAM to certain values (like 0).
"My watch says 30 chickens" Google, 2018

SuperStarFox

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: Pac-Man NES hi-score saving hack attempt...
« Reply #6 on: April 12, 2019, 04:29:40 am »
I've Always Wanted Something Like This... I Want To See Pac-Man On NES Have a Save High Score Feature Rather Than Seeing The High Score Delete All The Time!

EnigMog

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: Pac-Man NES hi-score saving hack attempt...
« Reply #7 on: April 24, 2019, 03:38:17 pm »
Curious whatever happened with this. I'm surprised by the lack of high score having hacks in NES games. Very happy with the work of this guy: https://nes.goondocks.se/patches2.php