Introduction
An SPC file holds music from the Super Nintendo (the Super Famicom). It does not store notes or audio. The console's sound unit is a small computer of its own, with an SPC700 processor, 64 KB of RAM and a digital signal processor, the S-DSP, which mixes eight voices of compressed samples. The game loads a sound program and its samples into that RAM and the program plays the song. An SPC file is a snapshot of that unit while a song plays: the processor's registers, the 64 KB of RAM (program, samples, song data and echo buffer) and the 128 registers of the DSP. A player loads the snapshot into an emulated sound unit and lets the program carry on, so the song sounds as it did on the console.
The file also has a tag, called ID666, with the song, game, artist, who dumped it, and how long to play it. A newer, optional chunk at the end, xid6, adds longer names, track numbers, intro and loop lengths and more.
Structure
The file is 66,048 bytes ($10200) and has a fixed layout. It begins with the text
SNES-SPC700 Sound File Data v0.30 and the bytes 26, 26, then a flag for the tag
and a minor version, then the SPC700 registers as they were at the moment of the snapshot. The
ID666 tag follows. The RAM is at offset $100, the DSP registers at $10100, and the last 64
bytes are RAM that sits under the boot ROM. An xid6 chunk may follow the fixed part.
0Header signature, 26 26, tag flag, version, PC, A, X, Y, PSW, SP46 bytes$2EID666 tag song, game, dumper, comments, date, length, fade, artist, emulator210 bytes$100RAM the 64 KB of the sound unit65,536 bytes$10100DSP registers 128 bytes, eight voices of 16 and the global registers128 bytes$10180Unused64 bytes$101C0Boot ROM area the RAM under the ROM at $FFC064 bytes$10200xid6 optional extended tag itemsvaries
The header page gives every field, and the ID666 tag page the two ways the tag is written. RAM and the SPC700 says what the memory and the processor's registers are, with the timers and the ports to the console, and DSP registers lists the registers of the sound chip. The xid6 page has the extended items.
What the explorer shows
The explorer reads the whole file: every byte of the header, tag, RAM (in five parts), the DSP registers (voice by voice), the unused areas and the xid6 chunk is in the structure tree, with each field explained. A tag in either form is told apart and read, and a chunk or a field that breaks a rule is listed as a problem. The Song tab shows the tag and the extended items, the SPC700 tab the registers and the ports, and the DSP tab the eight voices (volume, pitch, sample, envelope and flags) and the global registers.
It then plays the song. The SPC700 is emulated instruction by instruction, with its three timers, the ports and the boot ROM mapping, and the S-DSP is emulated with BRR decoding, Gaussian interpolation, the envelopes, noise, pitch modulation and the echo with its FIR filter. The result is stereo at 32,000 Hz, the rate of the real unit, and is played and drawn as a waveform. The length and the fade come from the tag or the xid6 items and can be changed in the options bar, which also has a switch for each of the eight voices, for the echo and for surround (a voice or master volume with opposite signs on the left and right plays one side out of phase; with the switch off, every such pair is turned in phase).
The Code tab lists the 64 KB of RAM as SPC700 instructions, from address $0000, with the entry point (the PC of the file) marked. Each instruction shows its bytes, the assembler text, the cycles, the flags it changes and a short meaning, and the registers of the processor's I/O area ($F0 to $FF: CONTROL, DSPADDR, DSPDATA, the four ports and the timers) are named. The decoder is our own and is checked against the instruction chart of the SNESdev wiki (all 256 opcodes: text, flags, bytes, cycles) and against the 254,000 SingleStepTests cases for length and cycle count. As the direct page is page 0 or page 1 according to the P flag, the register names apply to page 0.
Divergence from the specification
- The tag has two forms and no flag. The specification describes a text and a binary form of the ID666 tag and says nothing on how to tell them apart. The explorer reads the date, the length and the fade as text when they are made of the characters the text form allows, and as numbers otherwise, as the Game_Music_Emu player does for the length.
- The binary date. The specification says the binary date is a 32-bit number, yyyymmdd. Files from ZSNES, such as the sample
02-theme.spc(bytes16 05 D0 07), keep day, month and a 16-bit year. The explorer reads that form when it gives a plausible date and the specified form does not. - The amplification item. The SNESdev wiki calls xid6 item $36 a one-byte mixing level; the original description makes it a 32-bit amplification in which 65,536 is normal. The explorer follows the original (xid6).
- The text-form emulator code. Some files write the emulator in the text form as an ASCII digit, so a '0' is read as 0.
- Halting programs. A song program that executes STOP or SLEEP stops the processor, as it does on the console; the explorer then plays silence for the rest of the length. The multiply instruction sets its flags from the whole 16-bit product, as in Game_Music_Emu; the real processor sets them from the high byte. Neither case is a difference in sound.
The timer-target write glitch
The three timers of the SPC700 count with an 8-bit divider. On the real chip, writing a timer's
target (the registers $FA to $FC) just after the timer counted can make the divider miss a bit.
blargg measured this and built it into snes_spc 0.9.0 (SNES_SPC.cpp, the code under
SPC_MORE_ACCURACY, with the table glitch_probs). The rule the explorer follows
is the one in that source:
- The new target differs from the old one, the divider equals the new target (low 8 bits), and the timer's next count is exactly one prescaler period (128 clocks for timers 0 and 1, 16 for timer 2) away.
- The new target is not 1, 2, 4 or 8 (the test
((target-1)|~15)&targetis zero for those). The target 256, written as 0, is not excluded. - Then the divider loses the lowest set bit of the new target. For the targets 3, 5 and 9 this happens only with the chance
glitch_probs[timer][old target], a table of 768 bytes that the explorer holds inglitch.jsand checks against the C source byte for byte; for every other target it always happens.
The C code draws the chance from the C library's rand(). The explorer uses a fixed linear
congruential generator (state 1, multiplier 1103515245, increment 12345, 31 bits, started again when a file
is loaded), so a file always plays the same way. The option Timer glitch in the options bar
turns the behaviour off, which is how snes_spc and Game_Music_Emu play by default; the Playback tab
gives the title of the waveform accordingly.
Where a song loops
A file does not say where its song repeats; the xid6 items give lengths only when the dumper entered them. The Playback tab therefore runs the song's own program and finds the repeat from what the program plays. The method:
- The song runs with the S-DSP logging every key-on: the time in samples, the voice, the sample number (SRCN) and the pitch.
- For each period p, the smallest first, the tail of the log is tested for being p-periodic: the same voice, sample and pitch at the same offsets, within 32 ms, for at least two periods, and the loop is at least one second long.
- Without the pitch the test is made again on voice and sample alone, for songs whose pitch changes (vibrato or slides) but whose notes repeat.
- The result is accepted only when two searches, made at least 10 seconds and one period apart, agree; the loop start is then the earliest key-on from which the pattern holds, and the intro is what precedes it.
- The run is capped at 600 seconds. A song that has not repeated by then (a long cue, or a program that never repeats) is reported as having no loop found, with the number of key-ons seen.
The tab shows the intro and loop lengths, the key-on that starts the loop and whether pitch was used. This is derived from the program, as players do when a file has no length; it is not a value stored in the file, and the xid6 items, when present, are still the ones used for the default length of the options bar.
RSN archives
An RSN file, as in the archives of snesmusic.org, is a RAR archive of SPC files. The explorer opens a file with the extension .rsn through the RAR reader (which tells RAR 1.5 to 4.x from RAR 5.0 by the signature) and lists the members; each member named .spc opens as an SPC file in the same way as a file of its own.
The reserved bytes of the tag (the last 45 or 46 bytes before the RAM) are not used by any specification; the explorer shows them as bytes.
Sources
- SPC file format v0.30 (the original text), for the header, both forms of the ID666 tag and the fixed areas.
- SPC and RSN file format on the SNESdev wiki: the v0.31 notes and the xid6 chunk with its item ids.
- SPC700 reference on the SNESdev wiki: the instruction set.
- The sound unit follows blargg's snes_spc as found in Game_Music_Emu (LGPL 2.1): Spc_Dsp, Spc_Cpu, Snes_Spc and Spc_Emu, for the timers, the registers, the DSP and the reading of the tag.
- The processor is checked against the SingleStepTests SPC700 suite (MIT), 248,536 cases with register, memory and cycle count after each; the differences are the ones listed above.
- The timer-target glitch is taken from snes_spc 0.9.0 (
SNES_SPC.cpp, SHA-2566932207f56c3a2e1c901a159a25ec195d169e28ea71335d5e055d32d0f089c4f),SPC_MORE_ACCURACYandglitch_probs. - RSN: SPC and RSN file format (an RSN is a RAR archive of SPC files) and the RAR reader of the explorer.
- The DSP and the timers are checked sample for sample against a C build of the same snes_spc source on real files, and against random register settings.
Links
Software
- Zophar's Domain SPC collection – SPC files of Super Nintendo games to download
- SNES Music – an archive of SPC and RSN files
Developers
- Game_Music_Emu – a library that plays SPC among other game music formats, with blargg's snes_spc
- SingleStepTests SPC700 – JSON test cases for each SPC700 instruction
Reference
- SPC file format – the original description, version 0.30
- SPC and RSN file format – the SNESdev wiki page with xid6
- SPC700 reference – the processor's instructions