How to find PRG and CHR ROM in .nes file format

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
How to find PRG and CHR ROM in .nes file format
by on (#191807)
Hello there, first time posting here and first time working with the nes, so sorry in advance if this has been asked before as I wasnt able to find a post.

I am currently writing an NES emulator in verilog to be synthesized for an FPGA. And I am running into some issues understanding where the PRG and CHR ROM would be located if I was loading a ROM block with the data from a .nes file. (I'm only talking about NROM due to it's simplicity at the moment)

As I understand it, the NROM cartridge (aka mapper 0) contains 16 bytes of header data followed by 16384 bytes of PRG ROM (assuming there isn't a trainer.) In the actual NES this ROM would be mapped to $C000 to $FFFF and consequently mirrored to $8000 to $BFFF. Knowing this I think I should be able to read the .nes file and locate the end of the PRG ROM and consequently find the $FFFC/D reset vector for the 6502.

The ROM I used to test so far was the nestest.nes file from the nesdev wiki

So by some simple arithmetic I found what I think is the Reset Vector at byte $400C/D inside the .nes file. Following the vector to address location $C004,
Image
Image

I go to byte $0014 in the .nes file. finding the instruction #78 which is the opcode for SEI, so it appears that my assumption is correct for this file. Additionally I did the same with donkey kong as well and it would seem that the address at the reset vector leads me to $07AE which has again the instruction #78.

Image

Image

Now my question is this, have I made any mistaken assumptions regarding the organization of the format? if not, is the CHR rom just the next 2k bytes from the $400F in the file?

Additionally, are all NROM-mapped roms organized the same way?

Thanks in advance to anybody who can lend an answer.
Re: How to find PRG and CHR ROM in .nes file format
by on (#191808)
The format is described here:
https://wiki.nesdev.com/w/index.php/INES

One byte in the header will tell you how many 16k blocks of PRG there are. Another byte will tell you how many 8k blocks of CHR there are.

Some NROM games have 32k instead of 16k of PRG. I think there might be one or two with no CHR in the file as well (using RAM instead of ROM). Again, though, this is all specified by the header, so you can figure it out from that.
Re: How to find PRG and CHR ROM in .nes file format
by on (#191809)
searzocom wrote:
Hello there, first time posting here and first time working with the nes, so sorry in advance if this has been asked before as I wasnt able to find a post.

I am currently writing an NES emulator in verilog to be synthesized for an FPGA. And I am running into some issues understanding where the PRG and CHR ROM would be located if I was loading a ROM block with the data from a .nes file. (I'm only talking about NROM due to it's simplicity at the moment)

As I understand it, the NROM cartridge (aka mapper 0) contains 16 bytes of header data followed by 16384 bytes of PRG ROM (assuming there isn't a trainer.) In the actual NES this ROM would be mapped to $C000 to $FFFF and consequently mirrored to $8000 to $BFFF. Knowing this I think I should be able to read the .nes file and locate the end of the PRG ROM and consequently find the $FFFC/D reset vector for the 6502.

The ROM I used to test so far was the nestest.nes file from the nesdev wiki

So by some simple arithmetic I found what I think is the Reset Vector at byte $400C/D inside the .nes file. Following the vector to address location $C004,
Image
Image

I go to byte $0014 in the .nes file. finding the instruction #78 which is the opcode for SEI, so it appears that my assumption is correct for this file. Additionally I did the same with donkey kong as well and it would seem that the address at the reset vector leads me to $07AE which has again the instruction #78.

Image

Image

Now my question is this, have I made any mistaken assumptions regarding the organization of the format? if not, is the CHR rom just the next 2k bytes from the $400F in the file?

Additionally, are all NROM-mapped roms organized the same way?

Thanks in advance to anybody who can lend an answer.


The fifth and sixth bytes in the .nes header, immediately after the FourCC ("NES\x1a"), indicate the amount of PRG and CHR data in units of 16KB and 8KB respectively. .nes dumps of cartridges without PRG ROM bankswitching can have either 16KB (fifth byte of header = 01) or 32KB (fifth byte of header = 02) of PRG ROM.
Re: How to find PRG and CHR ROM in .nes file format
by on (#191810)
oh shit yeah, I see so the header does contain that info.

My next question is if i wanted to understand how bank-switching works for other types of roms, what's a good starting point to read?
Re: How to find PRG and CHR ROM in .nes file format
by on (#191811)
Every mapper does it differently. Maybe just start here and read about the mapper that corresponds to a file you're interested in:
http://wiki.nesdev.com/w/index.php/Mapper

The common mappers generally have low numbers (e.g. the original version of iNES only went from 0-15, wasn't expecting there to be hundreds of mappers like there is). You can cover like 95% of ROMs with only a handful of them.

In general, PRG banks come in 8, 16, or 32k blocks. CHR banks come in 1, 2, 4, or 8k blocks. The iNES file format doesn't distinguish this though, it just expects the total size to be a multiple of 16k/8k but this has nothing to do with how the mapper banks it.

If something is using 8k banking, bank 0 is the first 8k of the PRG, bank 1 is the second 8k, etc.