emuforge

Sound

The sound side is nearly a console of its own. It has a processor (the SPC700), 64 KB of memory, three timers and a chip that makes the sound (the S-DSP). The main processor sees four bytes of it, and can do nothing else. A game sends a program and samples through them, and then tells that program what to play. The first program, which receives them, is in the console. The one here is our own.

FileWhat is in it
crates/snes-spc700the processor
crates/snes-dspthe S-DSP
crates/snesemu-core/src/sound.rs, sound/timer.rsthe memory, the ports, the timers, and the time of it all
crates/snesemu-core/src/boot.rsour boot program

The processor's cycles rest on Kris Bleakley's "SPC700 Commands", its instructions on fullsnes and the SNESdev Wiki's page. The ports, timers and registers rest on the SNESdev Wiki's "S-SMP" and "Timing", fullsnes and anomie's timing document. The S-DSP rests on anomie's document about it (with jwdonal's updates), fullsnes and the SNESdev Wiki's page about envelopes (credits).

Time

The sound side has its own crystal. The processor and the S-DSP have 1,024,000 cycles a second. Each 32 of them make one sample, so there are 32,000 samples a second. On NTSC, a cycle is 118,125 / 5,632 master clocks, which is about 20.97. On PAL, it is 2,128,137 / 102,400, or about 20.78. This exact ratio is a choice: consoles that people measured are up to about 0.5% faster, and no two are the same. A fixed one gives the same result on every machine.

It runs only when something could tell. The main processor brings it up to its own time when it reads or writes a port, and the frame's end does it too. What is left of a cycle is kept for the next time. It never runs past that time, as the sound processor can stop after any cycle, also in the middle of an instruction. So how often it catches up changes nothing. A test compares this with catching up for each master clock, with all timers, noise and echo.

It is as if this happened in each cycle:

  1. The S-DSP does one of its 32 steps. It uses the memory first (fullsnes, "SNES APU Low Level Timings").
  2. The processor does its cycle: one read, one write, or one cycle inside.

The processor really does whole instructions while there is time for the longest one, with the same code and the same use of the bus in the same cycles. The S-DSP does its steps later, many at once, but before anything could tell. And waits are passed (how). A test with random programs compares all of this with going cycle by cycle.

A port that the main processor writes is there for the cycles that come after that clock. Each of the four bytes in each direction keeps its value until it is written again. If both sides use a byte at the same time, a console can give a mix of two values. Here it is always one of them.

What the sound processor sees

AddressWhat is there
$0000-$00EFmemory. This is the direct page, unless the P flag moves that to $0100-$01FF
$00F0-$00FFregisters
$0100-$01FFmemory, which always has the stack
$0200-$FFBFmemory
$FFC0-$FFFFthe boot program, while bit 7 of $F1 is set, or memory. Writes always go to the memory
RegisterReadWrite
$F0 TEST0bit 1: memory can be written. Bits 3 and 0: the timers run while bit 3 is 1 and bit 0 is 0
$F1 CONTROL0bits 0 to 2 turn on the timers. Bit 4 clears what it reads at $F4 and $F5, and bit 5 what it reads at $F6 and $F7. Bit 7 shows the boot program
$F2the register of the S-DSP that is chosenchooses it
$F3that registerthat register. From $80 on, writes do nothing, and reads give the registers from $00 on again
$F4-$F7what the main processor wrote to $2140-$2143what the main processor reads there
$F8, $F9memorymemory
$FA-$FC0how far the three timers count. 0 is 256
$FD-$FFthe timers' outputs, with 4 bits. Reading one clears itnothing

Writes to registers go to the memory behind them too, if TEST allows writing. The S-DSP sees that memory, and can always write. When the console is turned on, CONTROL shows the boot program, the timers are off, TEST is $0A, and the ports are 0. Memory has 32 bytes of 0 and 32 bytes of $FF in turns, which people saw on one kind of chip. Others have other patterns.

TEST's other bits are not there. They add waits, and change how memory is read.

The processor

It has the registers A, X, Y and SP with 8 bits, PC with 16, and flags. A and Y together are a 16-bit register. There are no interrupts. SLEEP and STOP stop it for good, until a reset.

It does one cycle in each call, and then everything about the instruction is in its fields. An instruction has up to three parts: it reads its opcode, it finds an address, and it does the rest (lib.rs, decode.rs). Which of the cycles use the bus is as in Bleakley's table. He says that the cycles without an access also read memory, and gives their addresses. Here, and in the test data, they use no address.

Some reads are made only because the chip makes them. Their value is not used, but they can have an effect. For example, most instructions that store a byte first read its address. If one stores something at $FD, that read clears the output of timer 0.

  • What A, X, Y and SP are when the console is turned on is a guess (0). So is that a reset keeps them, and that it takes no time.
  • DIV when the result is too large gives wrong values on the chip. The documents do not say which. What is here rests on 1,000 test cases, of which 234 do not fit. Of 120 ways to divide by shifting and subtracting, it is the only one that agrees with all of them (crates/snes-spc700/tests/division.rs), but these cases are another emulator's. H after DIV is from these cases too.
  • TSET1 and TCLR1 read their byte twice. Which of the two values is used is a guess. They are only different where a read changes what is there.

The timers

TimerOne count inTargetOutput
0 and 1128 cycles (8,000 a second)$FA, $FB$FD, $FE
216 cycles (64,000 a second)$FC$FF

Each timer has three stages (sound/timer.rs):

  1. A divider, which always runs, since the console was turned on. A reset does not set it back. It has the same start as the S-DSP's steps: timer 2's comes round in steps 0 and 16, and that of the others in step 0 of every fourth sample. A test reads each timer one cycle later each time, and looks for the step in which it first counted.
  2. A count, which goes up by 1 each time that the divider comes round, while the timer is on and TEST lets it run. When it reaches the target, it starts at 0.
  3. The output goes up by 1 when that happens. It has 4 bits, so after 15 comes 0. A program that does not read it in time loses counts.

For example, a program writes 80 to $FA and then 1 to CONTROL. Timer 0 was off, so its count and output are cleared. Its output goes up each 80 × 128 = 10,240 cycles, which is 100 times a second. How long the first one takes depends on where the divider is. It is 1 to 128 cycles, and then 79 × 128.

Nothing counts in each cycle. A timer remembers when it was looked at last. At the next read or change, what happened since then is worked out in one go. A test compares this with counting each cycle. Guesses are that the dividers start at 0 when the console is turned on, that the count is only compared for being the same as the target (so with a target that is lower than the count it first goes on through 255), and that the count and the output are cleared when a timer is turned on, and not while it is off.

The boot program

Nothing of a game is in the sound memory when the console is turned on. The sound processor starts with a program of 64 bytes at $FFC0. It takes a program from the main processor through the ports, and starts it. The console maker's program is not used here, in any form, and neither is a disassembly of it. Ours is written from the protocol, which the SNESdev Wiki has in words, and from what games do. boot.rs has it, and says what each part rests on.

The protocol, with "port 0" for $F4 and $2140, and so on:

  1. The program sets the stack pointer to $EF, and clears $0000 to $00EF.
  2. Ports 0 and 1 show $AA and $BB. The main processor answers with $CC in port 0.
  3. A command: ports 2 and 3 have an address. The program writes the value of port 0 back. If port 1 was 0, it starts the program at that address. If not, a block begins at that address.
  4. A block begins when port 0 has 0. Port 1 has each byte, and port 0 has its number, which starts at 0. The program stores the byte and then writes the number back. After 255 comes 0 again, 256 bytes further on.
  5. If port 0 has a number that is ahead of the one that it waits for, that is the next command. So after a block, the main processor adds at least 2 to the last number. If that is 0, it takes another number, as each block begins with 0.

What "ahead" means around 255 and 0 is not defined in these pages. Here, the difference as a signed byte has to be above 0. This is a guess. With it, blocks of every length from 1 to 600 bytes work in a test, with another block after each.

The times in our program are its own. $AA and $BB are both there after 2,406 cycles. On NTSC, that is at clock 50,464, at the end of line 36. No document that was used says how long the console takes. A byte that is already there when the last one is done takes 25 cycles. The SNESdev Wiki says that a byte takes about 520 master clocks, which is about the same.

Where games go back into it

Many games upload again later. They tell their own program to show the boot program and jump to it. They depend on where they enter it, so these places are part of what it has to do. They were found by watching games. 3,093 retail files ran for 2,400 frames each, with simple button presses, and what was kept was the address at which the sound processor entered $FFC0 to $FFFF, with its registers. 388 of them went back:

AddressFilesWhat was seenWhat our program has there
$FFC0375the start, as the documents sayit loads $EF into X
$FFC29, of 6 gamesX is always $EFso the two bytes before this load X. From here on, it sets the stack pointer to X, and clears the direct page
$FFC910, of 6 games, such as ESPN Baseball Tonight and Young Merlinthe stack pointer is $FB. ESPN Baseball Tonight uploads one ret instruction and starts it, to get back to where it wasso the stack is set up in the 9 bytes before this. That the direct page is cleared there too is a guess. From here on, it clears the P flag, shows $AA and $BB, and waits for $CC

Six files use both $FFC2 and $FFC0. P was clear each time. boot.rs has tests for all three places. They check that the stack and the direct page stay as they are from $FFC9 on, and that the ret gets back. How it was made has the whole story.

Where our program can be different from the console's: its bytes, if a program reads them. Its times. What it leaves in the registers for the program that it starts. And what it does when the main processor does not follow the protocol.

An upload, step by step

Two bytes, 2F FE, go to $0200, and then this program starts. It is one instruction, which jumps to itself. Each step on the left can only come after the one above it on the right, and the other way around. Both sides ask again and again until the other one's byte has changed. Each read or write of the main processor brings the sound side up to its clock first.

Main processorSound processor, in our boot program
1reads $2140 and $2141 again and againsets up its stack and clears the direct page. Then it writes $AA to $F4 and $BB to $F5
2sees $AA and $BBreads $F4 again and again, until it is $CC
3writes $00 to $2142 and $02 to $2143 (the address), and 1 to $2141 (a block). At last it writes $CC to $2140
4reads $2140 until it is $CCsees $CC. It copies $F6 and $F7 to $00 and $01 in its memory, reads the 1 at $F5, and only then writes $CC to $F4
5sees $CC. It writes $2F to $2141, and then 0 to $2140reads $F4 until it is 0. This is the number that it waits for
6reads $2140 until it is 0reads $2F at $F5, stores it at $0200, and writes 0 to $F4. Now it waits for number 1
7sees 0. It writes $FE to $2141, and then 1 to $2140sees 1. It stores $FE at $0201, and writes 1 to $F4. Now it waits for number 2
8sees 1, and that was all. It writes $00 and $02 to $2142 and $2143, 0 to $2141 (start), and 3 to $2140, which is 2 more than the last numberreads $F4 again and again. It is still 1, and then 3. 3 is ahead of 2, so this is a command
9reads $2140 until it is 3copies the address, reads the 0 at $F5, writes 3 to $F4, and jumps to $0200
10sees 3, and goes on. From now on, the ports mean what the game's own program saysruns the program

The order of the writes matters. The main processor writes port 0 last, as that is what tells the other side that all is there. The sound processor reads everything before it writes port 0 back, as the main processor can change its bytes after that. A game that sends 16 bits at once writes ports 0 and 1 in one instruction. The lower byte, which is port 0, is then first, and the other one comes a cycle later. This is 6 master clocks, and the sound processor needs several of its own cycles, of about 21 clocks, from reading port 0 to reading port 1. So it gets the right byte.

The S-DSP

It has eight voices. Each one plays a compressed sample (BRR) or noise, at any pitch, with an envelope. An echo can be added to the voices. All samples, and the echo's buffer, are in the sound memory. So the echo can overwrite a program or samples where a game sets it up in the wrong place.

It has 128 registers. Voice n has $n0 to $n9:

Register
$n0, $n1volume on the left and on the right, with a sign
$n2, $n3pitch, 14 bits. $1000 plays 32,000 samples a second
$n4the number of its sample. DIR ($5D) says where the table of their start and loop addresses is
$n5, $n6, $n7the envelope: attack, decay, sustain and release (ADSR), or gain
$n8, $n9the envelope's level now (ENVX), and the voice's output now (OUTX)
Register
$0C, $1C, $2C, $3Cthe main volume and the echo's volume, for the left and the right
$4C, $5Ckey on and key off (KON and KOFF), one bit for each voice
$6CFLG: bit 7 silences all voices, bit 6 mutes the output, bit 5 keeps the echo from writing to memory, and bits 4-0 are the rate of the noise
$7CENDX: which voices reached the end of their sample. Any write clears it
$0Dhow much of the echo goes back into it
$2D, $3D, $4Dwhich voices have their pitch changed by the voice before, play noise, and go into the echo
$5D, $6D, $7Dwhere the table of samples (DIR) and the echo's buffer (ESA) are, in steps of 256 bytes, and how long the buffer is (EDL, in steps of 2,048 bytes. 0 is 4 bytes)
$0F, $1F ... $7Fthe 8 coefficients of the echo's filter, for the oldest sample first

A sample is in blocks of 9 bytes: a header and 16 steps of 4 bits. The header says how far to shift them and how the two samples before are used. It also says if the block is the last one, and if the voice then goes on at the loop address. A voice decodes four samples at a time, when its position needs them. When it gets to a block that is the last one and has no loop, the voice is silent at once, also if it plays noise.

What is between two samples is made from the four samples around it, with a table of 512 numbers (interpolation).

Key on: the voice that is keyed on is silent for a few samples while it gets its first ones. If the sample in which it is keyed on is number 0, the first that can be heard is number 6. KON and KOFF are only looked at in every second sample. A voice whose bit is in both is keyed on and then turned off again.

The envelope has 11 bits. A counter that is the same for all voices and the noise says in which samples each of 31 rates has its step. Key off takes 8 from the level in each sample.

The echo reads an old sample from its buffer, filters the last eight of them for each side, and adds that to the output. It also puts it back into the buffer, with the voices that were chosen for the echo.

The 32 steps

The S-DSP does not read its registers all at once. Each register is used in its own step, so a write only reaches what comes after it. A voice needs nine steps:

Step of a voiceWhat it does
S1loads the number of the sample
S2reads the start or loop address if it needs one. Loads the lower byte of the pitch, and $n5
S3loads the upper byte of the pitch, and changes it by the voice before. Reads the header and the next byte of the sample. Makes the voice's output. Then come FLG's bit 7, the end of the sample, key off and key on, and the envelope, with $n6 or $n7
S4adds its left side to the sum. Reads the second byte, and decodes four samples if they are needed. Moves on by the pitch
S5adds its right side. Prepares ENDX
S6prepares OUTX
S7ENDX can be read. Prepares ENVX
S8OUTX can be read
S9ENVX can be read

Each voice is three steps after the one before. S2 of voice 1 is in step 0, and that of voice 2 in step 3. So in most steps, the chip works on two or three voices. S1 of voices 2 to 7 is four steps before their S2. For voice 2, that is step 31 of the sample before. Voice 0 is apart from the others. S1, S2 and the three parts of S3 are in steps 17, 21, 22, 25 and 30. Its S4 is in step 31, and S5 to S9 are in steps 0 to 4. S1 of voice 1 is in step 20.

StepWhat else happens
22reads the left sample of the echo. Its address is made from the ESA that was loaded before. Filter 0
23reads the right sample of the echo. Filters 1 and 2
24filters 3, 4 and 5
25filters 6 and 7. Filter 7's result is clamped, and the sums before it wrap
26the left sample of the output, and what goes into the echo's buffer
27the right sample. Both go out here. Loads $2D
28loads $3D, $4D, DIR, and FLG's bit 5 for the left side
29writes the left sample of the echo if it may. The counter goes on. Loads EDL at the start of the buffer, ESA, and FLG's bit 5 for the right side. In every second sample, it clears the bits of KON that were used
30writes the right sample of the echo if it may. Goes on in its buffer, and makes the next noise. In every second sample, it first loads KON and KOFF, which voice 0's S3 in this step then uses

For example, the volume of voice 3 on the left is used in step 8, and the one on the right in step 9. If a program changes both in two instructions in a row, one sample can have one new and one old volume.

A write from the sound processor comes after the S-DSP's step in the same cycle. So a write in the cycle of step 8 comes after the old left volume was used, and reaches the right volume if that is the one it changes.

What is not exact

The table for the interpolation is an approximation (gauss.rs). The chip has 512 numbers, and no description was used that gives them all. In their place is a cubic B-spline. It also spans four samples, is also shaped like a bell, and always adds up to 1 before it is rounded. It has no number that was fitted to the chip, and it was not compared with the chip's table. The difference can be heard in principle, in how much of the high tones is left. All voices but noise have slightly other values because of it, and so do OUTX, pitch modulation and what the echo writes to memory. fullsnes says that the chip's table overflows for three samples of -$4000. The arithmetic here can overflow in this way too, but not at the same places. To replace the table, only the function that makes it has to change.

These are guesses, or places where documents differ (lib.rs says more about each):

  • The registers are 0 when the console is turned on. Each chip has other values. FLG works like $E0 until it is written, but reads as 0. A reset keeps the registers, but for ENDX, ENVX and OUTX.
  • The order in step 30. KON and KOFF are loaded first. That voice 0 still uses the noise from before this step is a guess.
  • ENDX is set when the last four samples of a block are decoded. In two other places, documents say that this is at the start of the block.
  • The filter: a product that does not fit in 16 bits wraps.
  • When the mute bit counts is in no document. Here it is in steps 26 and 27, for each side.
  • Bent increase, one of the gain modes, compares the level before it is cut to 11 bits. fullsnes cuts it first.
  • The interpolation keeps the width that fullsnes has for each number. anomie's document can give another lowest bit.
  • Key on clears the level that bent increase compares.
  • Where it starts: it starts with step 0, and KON and KOFF are first loaded in the second sample, 62 and 126 cycles after a count of the first two timers' dividers. That is the more frequent of the two ways in which the chip starts.

The S-DSP can do many samples at once, and says which memory it could use until then. Tests compare both with single steps. The core uses both, so the S-DSP is mostly behind the processor.

What was tested

  • The processor agrees with SingleStepTests' SPC700 tests in 256,000 cases, 1,000 for each instruction, cycle by cycle. These cases come from another emulator, and are no measurement of a chip. 249,000 agree as they are. In the other 7,000, the wait cycle comes at another place. They are the 7 instructions that load from [dp]+Y. Here they are as in Bleakley's table, and the test moves that cycle. How to run them says more. Other tests compare the cycles with Bleakley's table, worked out by hand.
  • The S-DSP is tested through its registers: in which step each write counts, how long a key on takes, the filter, the echo and the noise. What is expected is worked out from the documents. No sound was compared with a console.
  • The boot program is tested with blocks of every length from 1 to 600, with a second one after each, and with any registers at its start.
  • The whole sound side is tested with a program that was uploaded by the protocol and answers.

From commit 019fda0af379. Edit this page