Reading a rom with the iNES header

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
Reading a rom with the iNES header
by on (#44641)
As I understand from here, I read the 16 byte header. The header is followed by a 512 KB trainer if present. From the header the program knows how many 16 KB PRG-ROM chunks there are, this value which I call prg_rom_pages in the header is used to determine how much data needs to be read from the file to load. This value is 16K*prg_rom_pages. I have a similar value chr_rom_pages in the header for determining how many bytes are to be read into CHR-ROM.

The problem is some values don't appear right in loading. For example in Smash T.V., the first value in PRG ROM is 0x77 which doesn't make sense as I know of no instruction that uses that op code. Yes, I know not to attempt to load the trainer if there isn't a trainer present. Is there something I'm missing?

by on (#44643)
First, welcome!

A trainer is 512 bytes, not KB. A trainer won't be present unless someone adds it, I'm pretty sure. So any commercial, untampered with game won't have one.

The first byte in PRG ROM might not be code, but data. Depending on the mapper, one of the PRG banks will be put into $C000-$FFFF on power-up. In this bank, $FFFA/$FFFB contain the address of the NMI routine, $FFFC/$FFFD will contain the address of the reset routine, and the last two bytes contain the address of the IRQ routine. Usually these point to routines inside of this bank. So on powerup, you have to set the PC to the address of the reset routine, which will be specified in $FFFC/$FFFD. It's not just the first byte of PRG ROM.

by on (#44644)
Celius wrote:
First, welcome!

A trainer is 512 bytes, not KB. A trainer won't be present unless someone adds it, I'm pretty sure. So any commercial, untampered with game won't have one.

The first byte in PRG ROM might not be code, but data. Depending on the mapper, one of the PRG banks will be put into $C000-$FFFF on power-up. In this bank, $FFFA/$FFFB contain the address of the NMI routine, $FFFC/$FFFD will contain the address of the reset routine, and the last two bytes contain the address of the IRQ routine. Usually these point to routines inside of this bank. So on powerup, you have to set the PC to the address of the reset routine, which will be specified in $FFFC/$FFFD. It's not just the first byte of PRG ROM.

Yeah, I meant byte with the trainer. Freudian slip considering most of what I was talking about was in KB.

And thanks for the rest of the info, that should help.

by on (#44645)
You're welcome! I'm glad when I can be of help. You should read Disch's mapper documentation; it will tell how the PRG banks are all laid out and stuff and which bank will be where on power up. It's usually the last that's fixed, but I'm not sure if this applies to every mapper.

by on (#44665)
Might there be a link to Disch's mapper documentation? That could be very useful.

by on (#44666)
I'm not sure if I'm allowed to link to the site, but you can find it on rom hacking dot net. It's referred to as the NES Mapper List. It's by Disch.

by on (#44684)
is there some beef with nesdev and rhdn or something? why couldn't u post a link

by on (#44685)
I remember someone saying linking to rom hacking sites is not allowed. I'm not sure if this really is true, or why, I'm just playing it safe. Maybe because it's a site about hacking commercial games, and that's illegal/bad or something. I'm pretty stupid when it comes to things like this, so I'm not exactly sure.

by on (#44686)
frantik wrote:
is there some beef with nesdev and rhdn or something? why couldn't u post a link

We can't link to illegal stuff here, and Celius probably wasn't sure if the contents of that site fall in that category... I think I've links to that page in here before, so it's probably OK. OTOH, I've seen stuff of questionable legality here, such as disassemblies of commercial games, so I'm really not sure what's OK and what's not.

by on (#44689)
hmmm... Not sure if I agree with not being able to link to RHDN, if that is the case. As far as I know, there aren't ROMs on that site, but patch files. There is some good knowledge to find in the docs over there, too.

by on (#44695)
I'll take one for the team and test the waters.

Disch's NES Mapper List can be found here: http://romhacking.net/docs/362/

by on (#44732)
Apparently we can do it! Thanks for the team shot, MetalSlime ; ) hehe