iNES file format
Files with the suffix .nes
are ROM files of game cartridges your emulator can play.
These ROMS are stored in the iNES file format.
iNES files store the contents of the cartridge ROM, and information on what additional hardware is present inside the cartridge.
The iNES spec has been extended with many features over the last two decades, but only a few essential parts need to be implemented to run almost every ROM.
In particular, you may ignore everything about the iNES 2.0 extension for this assignment.
If you do want to make your emulator run games that require information from the iNES 2.0 header to work you are free to do so, of course!
iNES files start with a sixteen byte header, followed by the ROM contents. The program rom (PRG-ROM) follows after the header. The character rom (CHR-ROM) follows after the PRG ROM. The format of an iNES file is described in detail below. For more information, see the useful links page.
Header
The iNES header is exactly 16 bytes long.
The first four bytes are always NES
followed by the number 26
1
The fifth byte (index 4) records how large the program ROM is in 16KiB (16384 byte) units.
The sixth byte (index 5) records how large the character ROM is in 8KiB (8192 byte) units.
If the sixth byte is zero, the cartridge uses character RAM instead of character ROM.
The amount of character RAM is specified in a messy way that you do not need to implement, you can simply assume it's 8KiB large and most games will work fine.
26
is the ASCII character that implied the end of a file on old computers.
The seventh and eight byte (index 6 and 7) specify more hardware features such as the mirroring mode and mapper. Parts that are labeled as 'not important' are rarely used, and there is no need to implement them.
Seventh byte Adapted from nesdev wiki:
76543210
||||||||
|||||||+- Mirroring: 0: horizontal (vertical arrangement) (CIRAM A10 = PPU A11)
||||||| 1: vertical (horizontal arrangement) (CIRAM A10 = PPU A10)
||||||+-- 1: Cartridge contains battery-backed PRG RAM ($6000-7FFF) or other persistent memory
|||||+--- Trainer, not important
||||+---- 1: Ignore mirroring control or above mirroring bit; instead provide four-screen VRAM
|||| Note: very few games use this
++++----- Lower four bits of mapper number
According to NesCartDB, only four official NES games ever used four-screen mirroring.
Eight byte Adapted from nesdev wiki:
76543210
||||||||
|||||||+- VS Unisystem, not important
||||||+-- PlayChoice-10, not important
||||++--- Not important
++++----- Upper four bits of mapper number
All other bytes of the header are not important.