Starting a song: INIT
A song number from 0 to the number of songs minus 1 goes into the accumulator. A player shows songs from 1, so the first song in the header’s start song field is loaded as that number minus 1. A player then does these things, in this order:
- Writes
$00to all of the RAM at$0000to$07FFand$6000to$7FFF. - Writes
$00to the sound registers$4000to$4013, then$00and$0Fto$4015(all channels but the DPCM switched on, as Battletoads, Castlevania and Gremlins 2 need). - Writes
$40to$4017: the frame counter in four-step mode, with its interrupt disabled. - For a bank-switched tune, writes the eight
initial banks to
$5FF8to$5FFF. - Sets A to the song, X to the region (0 NTSC, 1 PAL; 2 is Dendy in the players that have it) and, for compatibility with NSF2, Y to 0.
- Calls the INIT address as a subroutine.
INIT must end in RTS before any music is heard, except for an NSF2 tune that uses a
non-returning INIT. A tune that is only for one
region may ignore X; others use it to pick pitches and tempo for the console.
Playing: PLAY
After INIT returns, the player calls the PLAY address as a subroutine at every period of the
header: the NTSC rate when X was 0 and the PAL rate when it was 1. A PLAY routine normally ends in
RTS, and does a small amount of work: it reads the music data and writes the sound
registers. If PLAY takes longer than the period, the call that would have come in that time is
skipped and the next one starts at the following period. A PLAY that does not return (some
players use one to stream sampled sound, like SuperNSF, MUSE and Deflemask) is allowed, but it
stops a player that uses the same processor for its user interface.
A tune may be driven by something other than PLAY. The pseudo-IRQ technique counts PLAY calls and does its work only on some of them. NSF2 adds real interrupts.
Regions
| X | Console | Processor clock | PLAY period |
|---|---|---|---|
| 0 | NTSC (Famicom, NES) | 1,789,773 Hz | NTSC rate in the header (+6E) |
| 1 | PAL | 1,662,607 Hz | PAL rate in the header (+78) |
| 2 | Dendy | 1,773,448 Hz | Dendy rate of the NSFe RATE chunk, otherwise the PAL rate |
A tune that supports both NTSC and PAL says so in the region byte. Which one to play when the user has not chosen is the one the tune is made for. For a tune for both, bit 0 of the region byte can be read as a preference for PAL (set) or NTSC (clear), which most players do not do; the explorer does. An NSFe regn chunk lists the regions and the preferred one.
In the explorer
The Tune tab lists the steps above for the song and region chosen in the options bar, with the value of A, X and Y. The player runs them as written, calls PLAY at the period of the region, and skips a call if the last one has not returned.