What NSF2 adds
NSF2 is an NSF whose version byte is 2. It keeps the header and is backward compatible where its new features are not used, and adds:
- feature flags in byte
+7C, which version 1 players ignore; - a 24-bit length of the program data at
+7D, so that NSFe metadata can follow the data; - a programmable timer interrupt and access to the APU’s own interrupts;
- a non-returning INIT, interrupted by PLAY;
- UTF-8 for all strings.
The feature flags (+7C)
| Bit | Meaning when set |
|---|---|
| 0–3 | Reserved, must be 0. |
| 4 | The tune may use IRQs. |
| 5 | The tune uses the non-returning INIT. |
| 6 | The PLAY routine is not used. |
| 7 | The metadata has a mandatory chunk that has to be understood to play the file. |
In a file of version 1 these bits are ignored. The vector overlay below is implied by bit 4 or bit 5 only, not by the version.
The data length (+7D)
A non-zero 24-bit length says that the program data ends there, $80 bytes into the
file plus that length, and that metadata may follow. Non-mandatory metadata also works in a
version 1 file, but an older player reads it as part of the program. If the metadata is only
optional information like track names, and no other NSF2 feature is needed, leaving the version at 1
keeps the file valid for such players, as playback is almost always deterministic and the extra bytes
are not run.
IRQ support (bit 4)
The tune may use interrupts: a timer of its own, and the APU frame counter and DMC interrupts.
This implies a vector overlay at $FFFA to $FFFF,
on top of the program’s own data. The NMI vector ($FFFA) and the reset vector
($FFFC) belong to the player; the IRQ vector ($FFFE) is RAM, so the
tune has to write its address there before it enables interrupts. For convenience the player
first fills that RAM with the two bytes the data holds there.
The first time INIT is called the interrupt-disable flag is set (SEI) and the timer is
inactive. The tune enables interrupts itself (CLI); the player never changes the flag.
The timer
| Address | Access | Use |
|---|---|---|
$401B | read, write | The low 8 bits of the reload value. |
$401C | read, write | The high 8 bits of the reload value. |
$401D | write | Bit 0 set starts the timer; clear stops it. |
$401D | read | Acknowledges the interrupt. Bit 7 is the interrupt flag, before it is cleared; bit 0 says whether the timer is active. |
While active, the counter goes down by one on every processor cycle. When it goes below 0 it sets
its interrupt flag and reloads, so a reload value of N repeats every N + 1 cycles; 0 repeats every
cycle. The interrupt line is held until the flag is acknowledged by reading $401D.
While inactive, the counter is reloaded on every cycle.
Non-returning INIT (bit 5)
INIT may run forever and PLAY interrupts it, as an NMI would:
- INIT is called as for NSF but with Y =
$80. This call must return. - NMI is enabled.
- INIT is called a second time, with the same A and X and Y =
$81. This call does not need to return. - PLAY is called by an NMI wrapper that interrupts INIT.
The wrapper belongs to the player: it saves and restores A, X and Y and calls PLAY as a
subroutine, so PLAY ends in RTS, not RTI. As PLAY is called inside an NMI
the interrupt-disable flag is set while it runs. The wrapper should also stop the NMI from happening
again while PLAY still runs. If the second INIT does return, the player falls into its own endless
loop and INIT is not called a third time. Another value of Y than $80 or
$81 means a player that does not have the feature, which a tune can use to fall back to
another way.
Suppressed PLAY (bit 6)
PLAY is never called. It is for use with the non-returning INIT, so that INIT is not interrupted. The bit does not by itself imply an NMI-driven PLAY.
Metadata
NSFe metadata may be appended after the data, at $80 plus the data length. It is the
NSFe chunk format with these differences:
- There is no
NSFEmarker: the first byte begins the first chunk. - The chunks that are already part of the NSF format must not be used: INFO, DATA, BANK and NSF2.
- RATE and regn may be used, to add the Dendy region; the header supplies a fallback.
- The metadata should end with an NEND chunk.
When bit 7 of the flags is set, the metadata has a mandatory chunk (one whose id starts with a capital letter) that a player must understand, or it must refuse the file. An example is a VRC7 chunk that replaces the VRC7 with the YM2413. The bit lets the format grow without a change to the header.
Notes for writers
- With the non-returning INIT, use a standard rate in the header, since a hardware player may not be able to change its NMI timing.
- With IRQs or the non-returning INIT, do not switch the bank of
$F000, so a player can put the overlay straight on the data. - Writing the IRQ vector at
$FFFEin the data as well as to the RAM works with both NSF1 players that have some IRQ support and NSF2. - For the header strings use only ASCII; the NSFe auth chunk can carry the UTF-8 versions.
In the explorer
The flags are decoded in the tree and in the Tune tab, the metadata is a list of chunks with their fields, and a metadata chunk that must not be there or that is mandatory and unknown is a problem on it. The player has the vector overlay with the IRQ vector in RAM, the timer, the frame counter and DMC interrupts, the two calls of a non-returning INIT with the NMI wrapper, and the suppressed PLAY.