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

Author Topic: NES Text Editing Help  (Read 2738 times)

maso128

  • Jr. Member
  • **
  • Posts: 6
    • View Profile
NES Text Editing Help
« on: June 06, 2017, 07:34:39 pm »
I'm translating an NES game with tons of extra space. Is there any way to use a hex editor to jump to another offset, write out some text, then jump back to a previous offset?

Disch

  • Hero Member
  • *****
  • Posts: 2760
  • NES Junkie
    • View Profile
Re: NES Text Editing Help
« Reply #1 on: June 06, 2017, 09:27:19 pm »
Unfortunately it's not that easy on the NES.  Not only do you need free space, but it has to be in the right bank.

On the NES, the number of addresses that are available is severely limited.  Therefore, only some of the game is "visible" at any one time.  Games get around this by "swapping" different parts of the game ("banks") out at different times.  For example, you might be able to see both banks 3 and F at once ... or banks 5 and F .... but not 3, 5 and F.  At least, not all at the same time.

So what you have to figure out is what banks are "visible" when the game is loading text.  If you have free space in that bank... then using it is just as simple as changing a pointer.  But if the free space is in a separate bank, that's a whole different can of worms.

FCandChill

  • Hero Member
  • *****
  • Posts: 530
    • View Profile
Re: NES Text Editing Help
« Reply #2 on: June 06, 2017, 09:31:34 pm »
Isn't this most because of how the pointer is strucured? If you have a one or two byte pointer, it's implied the pointer can only be used for that bank only.
« Last Edit: June 07, 2017, 12:56:35 am by MathOnNapkins »

maso128

  • Jr. Member
  • **
  • Posts: 6
    • View Profile
Re: NES Text Editing Help
« Reply #3 on: June 06, 2017, 10:55:32 pm »
Is there any sort of indication of which bank is which?

Psyklax

  • Hero Member
  • *****
  • Posts: 1086
    • View Profile
    • Psyklax Translations
Re: NES Text Editing Help
« Reply #4 on: June 07, 2017, 04:23:00 am »
The problem lies with how the NES was made. It supports a PRG ROM of 16 or 32kb and a CHR ROM of 8kb. That's it. In the first couple of years, programmers had to make do with this, but eventually companies included MMC chips (mappers) to switch banks in and out of these areas. The NES itself couldn't change, so you're still in a 16-bit address space (up to FFFF).

So using this empty space requires the game telling the mapper to switch banks (I confess I don't know how that works - yet) and then working within that bank. Of course the problem here is that all the code that was in the previous bank (or whatever else is there - music, level data etc) isn't in this bank. So when Disch says it's a "whole different can of worms", he's right. :)

Pity it's not like the Game Boy Color: it uses a 24-bit address space and loads the entire ROM in one chunk. So when I translated a game with a 1MB ROM size, I could use any empty space at the end of banks for my text. I finished half the game before I even had to overwrite any Japanese text. I could even overwrite unnecessary graphics with text. It was heaven. :D

Disch

  • Hero Member
  • *****
  • Posts: 2760
  • NES Junkie
    • View Profile
Re: NES Text Editing Help
« Reply #5 on: June 07, 2017, 08:36:41 am »
Is there any sort of indication of which bank is which?

It depends on the mapper being used (which depends on the game).

But 99% of the time, banks are either 16K ($4000 bytes) or 8K ($2000 bytes) in size.  So basically if you were to take the ROM (minus header) and break it up into several $4000 or $2000 byte chunks, those would be the banks.

Example:  Assuming you have 16K banks, the ROM would look like this:
0x00000 - 0x0000F = header
0x00010 - 0x0400F = bank 0
0x04010 - 0x0800F = bank 1
0x08010 - 0x0C00F = bank 2
... etc

Isn't this most because of how the pointer is strucured? If you have a one or two byte pointer, it's implied the pointer can only be used for that bank only.

As Psyklax mentioned, not really.

It has to do with the NES only having a 16-bit address bus (unlike the SNES and apparently GBC which have a 24-bit bus).  On the SNES you can have a 3 byte pointer because the assembly allows for 3-byte addresses.  But the NES assembly only allows for 2-byte addresses.  Period.

However, an NES game could have 3 byte pointers.  But for that to work, the game would not be able to use the pointer directly.  It would have to examine the address, manually swap in the desired bank, then modify the pointer to point to the right data before actually using it.  Most games do not do this.

maso128

  • Jr. Member
  • **
  • Posts: 6
    • View Profile
Re: NES Text Editing Help
« Reply #6 on: June 07, 2017, 04:54:10 pm »
The NES game I'm hacking is Mother, which uses an MMC3 mapper with 16KiB banks.

From what I can gather, all the enemy names and battle messages are stored in one bank with extra space. All the main dialogue is stored in two consecutive banks with no extra space. Finally, all the PSI ability names are stored in one bank with no extra space.

KingMike

  • Forum Moderator
  • Hero Member
  • *****
  • Posts: 6934
  • *sigh* A changed avatar. Big deal.
    • View Profile
Re: NES Text Editing Help
« Reply #7 on: June 08, 2017, 01:27:50 am »
(unlike the SNES and apparently GBC which have a 24-bit bus).
GBC uses a variation of Z80 which is 16-bit bus so they also had to do ROM banking. The only reason they'd include a bank byte is in the programmers were expecting their text to be big enough to warrant coding support for a bank byte into the text routine.
"My watch says 30 chickens" Google, 2018

Disch

  • Hero Member
  • *****
  • Posts: 2760
  • NES Junkie
    • View Profile
Re: NES Text Editing Help
« Reply #8 on: June 08, 2017, 01:39:39 am »
GBC uses a variation of Z80 which is 16-bit bus so they also had to do ROM banking. The only reason they'd include a bank byte is in the programmers were expecting their text to be big enough to warrant coding support for a bank byte into the text routine.

But if the bank byte isn't on the bus, then it has the same restrictions.

How is GBC bankswapping different from NES bankswapping then?


The NES game I'm hacking is Mother, which uses an MMC3 mapper with 16KiB banks.

From what I can gather, all the enemy names and battle messages are stored in one bank with extra space. All the main dialogue is stored in two consecutive banks with no extra space. Finally, all the PSI ability names are stored in one bank with no extra space.

MMC3 technically has 8K banks, but the game might treat them as 16K.

If dialogue is spread across banks already, then it's likely that you'd be able to move it elsewhere to get more room.... But adding a new chunk of dialogue in a completely different area is probably not going to be easy.

The short version is "it's really hard to do what you want to do, but not impossible."  This will likely take some asm work to figure out how the game reads the text, and how it determines which banks to swap in when doing so, and finding a way to change all that.  That's about as detailed as I can get right now.

Sorry for being so vague.  I'm kind of burnt out right now and this is a difficult question to answer.

KingMike

  • Forum Moderator
  • Hero Member
  • *****
  • Posts: 6934
  • *sigh* A changed avatar. Big deal.
    • View Profile
Re: NES Text Editing Help
« Reply #9 on: June 08, 2017, 01:54:52 am »
But if the bank byte isn't on the bus, then it has the same restrictions.

How is GBC bankswapping different from NES bankswapping then?

Not much really. There's only a handful of mappers on that, I think (MBC1/2/3/5/6/7, official multicart mapper MMMO1 I think it's called, a couple Hudson mappers I think, and maybe Tamagotchi. But the most common are MBC1, 2 and 5, it seems. MBC3 seems to have been used by few popular games like Pokemon and the others are maybe 1 or 2 games, aside from the MM thing used in several late-era compilations)

Probably the more notable thing is that SRAM locking seems to have been a standard feature on those mappers.

Though I forgot GBC used RAM banking for both VRAM and CPU-RAM. (as both were increased on GBC but to retain backwards compatibility with DMG games).
"My watch says 30 chickens" Google, 2018

Disch

  • Hero Member
  • *****
  • Posts: 2760
  • NES Junkie
    • View Profile
Re: NES Text Editing Help
« Reply #10 on: June 08, 2017, 09:58:39 am »
Then I'm completely confused by Psyklax.  Did he mean GBA?

Psyklax

  • Hero Member
  • *****
  • Posts: 1086
    • View Profile
    • Psyklax Translations
Re: NES Text Editing Help
« Reply #11 on: June 08, 2017, 01:11:59 pm »
Okay, I need to jump back in here. :D

I confess I don't know a damn thing about the GBC's internals, unlike the NES which I'm currently learning plenty about. But I DO know that when I translated the first Detective Conan game, I could use 3-byte pointers to address anywhere in the 1MB ROM file. Now, was that programming trickery on the part of the developers? I don't know. But it works. :)

Let me give an example. The GBC had everything in banks, and the first byte told you which bank. Take the first line of dialogue from the Conan game: it's located at $2140C in the ROM. Each bank is $4000 in size, so 00 denotes 0000-3FFF, 01 denotes 4000-7FFF and so on. So 2140C is in bank 08 (starting at $20000). Next, you say the exact address, little-endian of course, so the second byte is 0C. But the third byte is always between 40 and 7F, so you add or subtract 40 to get the correct number. We need 14, so we add 40 to get 54. Sure enough, if you look at $C060, there's a pointer table with the first entry being 08 0C 54. Simple. :)

Using this, you can access anywhere in the entire ROM, and given that it's a 8Mbit file for an 8-bit machine, there's a LOT of empty space to use for my translation. Now, maybe it doesn't have a 24-bit bus, I don't know the insides. And maybe other GBC games don't do that, I don't know. But it works on the Conan games. :)

Gideon Zhi

  • IRC Staff
  • Hero Member
  • *****
  • Posts: 3520
    • View Profile
    • Aeon Genesis
Re: NES Text Editing Help
« Reply #12 on: June 08, 2017, 01:49:47 pm »
Using this, you can access anywhere in the entire ROM, and given that it's a 8Mbit file for an 8-bit machine, there's a LOT of empty space to use for my translation. Now, maybe it doesn't have a 24-bit bus, I don't know the insides. And maybe other GBC games don't do that, I don't know. But it works on the Conan games. :)

The GBC and the NES are very different architectures. I've worked with several GB/C games where this worked, but it generally does not apply to the NES.

Pennywise

  • Hero Member
  • *****
  • Posts: 2260
  • I'm curious
    • View Profile
    • Yojimbo's Translations
Re: NES Text Editing Help
« Reply #13 on: June 08, 2017, 02:30:11 pm »
That was the programmer's coding a 24 byte pointer setup. I've encountered that before in an NES game and it makes translating a game very easy.

Psyklax

  • Hero Member
  • *****
  • Posts: 1086
    • View Profile
    • Psyklax Translations
Re: NES Text Editing Help
« Reply #14 on: June 08, 2017, 06:20:24 pm »
That was the programmer's coding a 24 byte pointer setup. I've encountered that before in an NES game and it makes translating a game very easy.

It certainly made my translation a lot easier. :) I had no idea it was a programmer trick rather than how the system worked. I'm starting to realise that there's probably no set way for the ROM data to do things, it's however the programmer decides to do it.