Introduction
An NSF file holds music from the Nintendo Entertainment System and the Famicom. It does not store notes or audio: it stores the original 6502 machine code that played the tune, taken from a game, plus a small header saying where to load it and how to call it. A player puts the code into memory, sets up the sound hardware, calls the tune’s INIT routine to choose a song, and then calls its PLAY routine at the rate the header gives (usually once for every video frame). Each PLAY writes to the sound registers of the NES, and a player has to emulate those chips to hear the result.
Two extensions grew from it. NSFe is a chunked file that carries the same tune with track names, authors, lengths and play order, and NSF2 is an NSF whose header adds interrupts and a non-returning INIT, and which can have the NSFe chunks appended.
Structure
The file starts with the five bytes N E S M $1A. The rest of the 128-byte
header is little-endian: the version (1, or 2 for NSF2), the number of songs, the start
song (counted from 1), the load, init and play addresses, three 32-byte strings for the
name, artist and copyright, the play rates for NTSC and PAL consoles in microseconds, eight
bank numbers, a region byte, a byte of sound-chip flags, the NSF2 flags, and a 24-bit length
of the program data. The program data follows, up to the end of the file or, when a length
is given, up to that length; anything after that is NSFe metadata.
0Magic and version NESM and $1A, a version of 1 or 26 bytes6Songs and addresses songs, start song, load, init and play8 bytes14Text name, artist, copyright96 bytes110Rates and banks NTSC rate, eight initial banks, PAL rate12 bytes122Region, chips, NSF2 flags and data length6 bytes128Program data the 6502 code and the musicvariesendMetadata NSFe chunks, only when a data length is givenvaries
The header page gives every field. Bank switching says how large tunes are mapped into the 32 KB the program sees, and INIT and PLAY what a player must do before and after calling them. The sound chips page lists the extra chips a game cartridge can carry. NSF2 and NSFe have their own pages.
What the explorer shows
The explorer reads the whole file: every byte of the header, the data and the appended metadata is in the structure tree, with the flags and the chunks explained, and a header or chunk that breaks a rule of the format is listed as a problem on the field. It then runs the tune. A 2A03 processor (the NES’s 6502 without decimal mode) is emulated cycle by cycle, with the RAM, the bank registers, the NSF2 timer and interrupts and the APU registers around it; INIT is called with the song and the region, and PLAY at the rate of the region. The sound of the five APU channels (two pulse, triangle, noise and DPCM) and of every expansion chip the header flags (VRC6, VRC7, FDS, MMC5, Namco 163, Sunsoft 5B and the VT02+) is mixed, played, and drawn as a waveform; the NSFe mixe levels of the devices apply. The program is also disassembled as 2A03 code, with the names of the NES registers, the bank registers and the INIT and PLAY entry points. The song, the region and the length are set in the options bar.
The processor makes one bus access per cycle, in the order of the real chip (dummy reads, the double write of read-modify-write instructions), and the APU and chips are brought up to date before any access that can see them, so a register write reaches the APU on its own cycle. A DPCM sample fetch holds the processor for 3 or 4 cycles.
The VRC7 is NSFPlay’s NES_VRC7 over emu2413 v1.5.9 ported exactly; compared integer for integer with the compiled NSFPlay code on 150 random register scripts (60,878 steps identical). Its level is set by the player (×1.883) to match the other chips.
An NSFe file’s time chunk gives each song’s length and its fade chunk the
fade-out (linear); 30 seconds with no fade where the file gives none, and for any NSF. The Length
option is 0 by default (from the file); any other number plays that many seconds with no fade. The
Song option can be the playlist (plst chunk), which plays the songs in that order, each
with its own time and fade.
Not described
Known, not yet covered
- Viewer: the NSF2 NMI wrapper is the site’s own and saves A, X and Y, as the specification asks; it is not byte for byte the wrapper of any hardware player.
Unknown
- Viewer and reference: what a disabled “loudness” does to the sound, bits 6 and 7 of the VT02+ register
$4030(OP1LandOP2L). The V.R. Technology VT16 data sheet (revision A7, page 42) and VT18 data sheet (revision A9, page 44) define the bits only as “XOP1 / XOP2 loudness enable/disable”, 0 = enabled (the default), with no level or effect; the VT02 and VT03 data sheets do not list the bits, and MAME, FCEUX, puNES, NintendulatorNRS and Mesen2 do not model them. The player keeps the two flags but does not change the mix. Bit 5 (PCML, the 4,081 or 4,096 byte PCM length) is done, with one gap: the VT16 and VT18 data sheets give the length only for$4013=$FF(4,081 or 4,096); the player counts other lengths as$4013× 16 + 16 when the bit is set, which no document states.
Sources
- NSF on the NESdev wiki, the header, the loading rules, INIT and PLAY and the sound chips, checked against Kevin Horton’s original NES Sound Format specification.
- NSF2 on the NESdev wiki: the feature flags, the IRQ timer, the non-returning INIT and the appended metadata.
- NSFe on the NESdev wiki, checked against Disch’s NSFe specification, revision 2, for the original chunks.
- The processor is checked against the nestest ROM and its log (Kevin Horton), 8,991 instructions with the register and cycle count after each.
- The APU is the core of the VGM viewer, checked tick by tick against libvgm’s NES APU; the test files of nes-audio-tests (APU, expansion chips and NSF2 features) are the sample files.
- The FDS sound is compared read for read and step for step with the
nes_fds.cppcore of NSFPlay (Brad Smith’s fork), 7,384 steps and 1,804 reads, identical. The chips are compared with NSFPlay’s cores likewise, except the VRC7. - VT02+ Sound and VT02+ Registers on the NESdev wiki, and V.R. Technology’s VT02 data sheet (revision A5) and VT03 data sheet (revision A6), for the second APU,
$4030to$4035, the DMA and the 250 Hz and 200 Hz triangle clock. - V.R. Technology’s VT16 data sheet (revision A7, 2007, the
$4030row, its field text and Programming Guide note 11) and VT18 data sheet (revision A9, page 44), from vrt.com.tw (/datasheet.htm), for$4030bits 5 to 7 (PCML,OP1L,OP2L). The note names “$4015(D5)” where the register table hasPCMLin$4030bit 5 ($4015has no bit 5 on write); it is read as$4030bit 5. - The VT NSF rips linked from VT02+ Sound (Santa Claus, 100 In 1 (101 In 1) Arcade Action II) and the OneBus code of NintendulatorNRS (
OneBus.cpp, which uses$4030bits 0, 1, 3 and 4 only), read for the$4030writes.
Links
Software
Developers
- nes-audio-tests – test NSF files for the APU, the expansion chips and the NSF2 features
- MAME sound devices – source of sound chip emulations, among them the chips used by NES expansions
Reference
- NSF – the NESdev wiki page of the format
- NSF2 – the NESdev wiki page of the second version
- NSFe – the NESdev wiki page of the extended format
- NES Sound Format specification – the original document by Kevin Horton