INES

From NESdev Wiki
Revision as of 19:27, 13 January 2019 by Rainwarrior (talk | contribs) (→‎Nametable Mirroring: alternative wording about theoretical use to address bregalad's point in talk (though it could probably be improved yet))
Jump to navigationJump to search

The .NES file format (file name suffix .nes) is the de facto standard for distribution of NES binary programs, with use even in licensed emulators such as commercialized PocketNES and Wii Virtual Console. It is often called the iNES format, as it was created by Marat Fayzullin for an emulator called iNES. A "rebooted" version of this file format, NES 2.0, was created to fix many shortcomings and support in emulators is being added.

iNES emulator

iNES was an early NES emulator developed by Marat Fayzullin. Its most lasting contribution to the NES scene was its popularization of the iNES ROM file format and mapper numbering system.

Name of file format

This file format is commonly referred to as the iNES file format/iNES header format. The file extension is .nes, so it is sometimes referred to as the .nes file format, and files in it as .nes files. Now that the NES 2.0 file format exists, which uses the same .nes extension, a .nes file/the .nes file format could mean the iNES file format or NES 2.0 format, so the full format names should be used where the differences in the formats are relevant, like specifications or format support.

iNES file format

An iNES file consists of the following sections, in order:

  1. Header (16 bytes)
  2. Trainer, if present (0 or 512 bytes)
  3. PRG ROM data (16384 * x bytes)
  4. CHR ROM data, if present (8192 * y bytes)
  5. PlayChoice INST-ROM, if present (0 or 8192 bytes)
  6. PlayChoice PROM, if present (16 bytes Data, 16 bytes CounterOut) (this is often missing, see PC10 ROM-Images for details)

Some ROM-Images additionally contain a 128-byte (or sometimes 127-byte) title at the end of the file.

The format of the header is as follows:

  • 0-3: Constant $4E $45 $53 $1A ("NES" followed by MS-DOS end-of-file)
  • 4: Size of PRG ROM in 16 KB units
  • 5: Size of CHR ROM in 8 KB units (Value 0 means the board uses CHR RAM)
  • 6: Flags 6 - Mapper, mirroring, battery, trainer
  • 7: Flags 7 - Mapper, VS/Playchoice, NES 2.0
  • 8: Flags 8 - PRG-RAM size (rarely used extension)
  • 9: Flags 9 - TV system (rarely used extension)
  • 10: Flags 10 - TV system, PRG-RAM presence (unofficial, rarely used extension)
  • 11-15: Unused padding (should be filled with zero, but some rippers put their name across bytes 7-15)

Flags 6

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
|||||+--- 1: 512-byte trainer at $7000-$71FF (stored before PRG data)
||||+---- 1: Ignore mirroring control or above mirroring bit; instead provide four-screen VRAM
++++----- Lower nybble of mapper number

In the iNES format, cartridge boards are divided into classes called "mappers" based on similar board hardware and behavior, and each mapper has an 8-bit number (or 12-bit in NES 2.0). The low 4-bits of this mapper are given here in bits 4-7 of this field.

The presence persistent saved memory is given by bit 1. This usually takes the form of battery-backed PRG-RAM at $6000, but there are some mapper-specific exceptions:

  • UNROM 512 and GTROM use flash memory to store their game state by rewriting the PRG-ROM area.

Nametable Mirroring

See: Nametable Mirroring

For mappers with hard-wired mirroring, connecting CIRAM A10 to PPU A10 or A11 for a vertical or horizontal arrangement is specified by bit 0.

Some mappers have a 4-screen variation of the board, which is specified with bit 3:

A few mappers override normal usage of the nametable mirroring bits:

  • UNROM 512 uses %....1..0 to indicate a 1-screen board, and %....1..1 to indicate a 4-screen board.
  • Mapper 218 (Magic Floor) has 4 unusual CIRAM configurations corresponding to each of the possible values.

Ambiguity:

  • Many mappers (MMC1, MMC3, AxROM...) have mapper controlled nametable mirroring. These will ignore bit 0.
  • Several mappers have some form of 4-screen as their only option. ROMs might be found with bit 3 set to redundantly indicate this:
  • Mappers that share 4-screen nametable RAM with CHR-RAM may interact with the NES 2.0 CHR-RAM in byte 11.
  • Mapper 70 had a 1-screen variant that was sometimes specified with bit 3 set. This was relocated to Mapper 152.
  • Theoretically bit 3 could be used for most mappers that had hard-wired mirroring to transparently provide 4KB of VRAM for 4-screen instead. However, emulator support for this is largely untested for mappers without prior 4-screen variations.

Trainer

The trainer is extra code that was used by a Famicom copier to patch its PRG-RAM before starting the game. These patches were needed because these copiers could not support different mappers (like the PowerPak or Everdrive N8).

Games that are available with a trainer are usually also available as a good dump without one. The trainer is only needed to support these copier hacks.

One of these copiers was made by Front Fareast Industrial; hence the nickname "FFE" for the mapper hacks.

Flags 7

76543210
||||||||
|||||||+- VS Unisystem
||||||+-- PlayChoice-10 (8KB of Hint Screen data stored after CHR data)
||||++--- If equal to 2, flags 8-15 are in NES 2.0 format
++++----- Upper nybble of mapper number

The PlayChoice-10 bit is not part of the official specification, and most emulators simply ignore the extra 8KB of data. PlayChoice games are designed to look good with the 2C03 RGB PPU, which handles color emphasis differently from a standard NES PPU.

Vs. games have a coin slot and different palettes. The detection of which palette a particular game uses is left unspecified.

NES 2.0 is a more recent extension to the format that allows more flexibility in ROM and RAM size, among other things.

Flags 8

76543210
||||||||
++++++++- PRG RAM size

Size of PRG RAM in 8 KB units (Value 0 infers 8 KB for compatibility; see PRG RAM circuit)

This was a later extension to the iNES format and not widely used. NES 2.0 is recommended for specifying PRG RAM size instead.

Flags 9

76543210
||||||||
|||||||+- TV system (0: NTSC; 1: PAL)
+++++++-- Reserved, set to zero

Though in the official specification, very few emulators honor this bit as virtually no ROM images in circulation make use of it.

Flags 10

76543210
  ||  ||
  ||  ++- TV system (0: NTSC; 2: PAL; 1/3: dual compatible)
  |+----- PRG RAM ($6000-$7FFF) (0: present; 1: not present)
  +------ 0: Board has no bus conflicts; 1: Board has bus conflicts

This byte is not part of the official specification, and relatively few emulators honor it.

The PRG RAM Size value (stored in byte 8) was recently added to the official specification; as such, virtually no ROM images in circulation make use of it.

Older versions of the iNES emulator ignored bytes 7-15, and several ROM management tools wrote messages in there. Commonly, these will be filled with "DiskDude!", which results in 64 being added to the mapper number.

A general rule of thumb: if the last 4 bytes are not all zero, and the header is not marked for NES 2.0 format, an emulator should either mask off the upper 4 bits of the mapper number or simply refuse to load the ROM.

Variant comparison

Over the years, the header of the .NES file format has changed as new features became needed. There are three discernable generations:

Archaic iNES
Created by Marat and used in very old versions of iNES and in NESticle. ROM image conversion and auditing tools tended to store signature strings at offsets 7-15.
iNES 0.7
Created by Marat when the scene discovered the diversity of NES cartridge hardware. Mapper high nibble is supported in emulators since roughly 2000.
iNES
Later revisions added byte 8 (PRG RAM size) and byte 9 (TV system), though few other emulators supported these fields.
NES 2.0
Created by kevtris for the FPGA Kevtendo and maintained by the NESdev community to clarify ambiguous cases that previous headers did not clarify. Became widely adopted starting in the 2010s.
Thing Archaic iNES iNES NES 2.0
Byte 7 Unused Mapper high nibble, Vs. Mapper high nibble, NES 2.0 signature, PlayChoice, Vs.
Byte 8 Unused Total PRG RAM size (linear) Mapper highest nibble, mapper variant
Byte 9 Unused TV system Upper bits of ROM size
Byte 10 Unused Unused PRG RAM size (logarithmic; battery and non-battery)
Byte 11 Unused Unused VRAM size (logarithmic; battery and non-battery)
Byte 12 Unused Unused TV system
Byte 13 Unused Unused Vs. PPU variant
Byte 14 Unused Unused Miscellaneous ROMs
Byte 15 Unused Unused Default expansion device
Mappers supported 0-15 0-255 0-4095

Recommended detection procedure:

  1. If byte 7 AND $0C = $08, and the size taking into account byte 9 does not exceed the actual size of the ROM image, then NES 2.0.
  2. If byte 7 AND $0C = $00, and bytes 12-15 are all 0, then iNES.
  3. Otherwise, archaic iNES.

See also

References