# Start here: the S-DSP as a module author needs it The S-DSP is the sound generator of the SNES's audio unit. It shares 64 KiB of ARAM with the S-SMP (the SPC700 CPU, built in `spc700-python`) and is programmed only through the S-SMP's `$F2` (address) and `$F3` (data) ports. The board composes the two; this document describes the DSP alone. Every statement cites a permitted source: **anomie** = Anomie's S-DSP Doc r1212 with jwdonal's updates (`reference/dsp/docs/anomie-s-dsp-r1212-jwdonal.txt`, cited by section); **fullsnes** = Martin Korth's fullsnes, "SNES APU ..." chapters (facts only); **wiki** = the SNESdev wiki pages "S-DSP registers", "DSP envelopes", "BRR samples" (facts only); **spc_dsp6** = the name of a test in blargg's hardware-verified test ROM (the name says what was checked on a console; the ROM's source is not read); **ares** = ares `sfc/dsp` (ISC), used only for its separate Gaussian formula. **OBSERVED** = a reading chosen because it agrees with the snes_spc 0.9.0 referee run as a closed box ([validation.md](validation.md)); **HYPOTHESIS** = nothing settles it yet. No statement here rests on reading an implementation's source. Quirks are U*n* and open items C*n* in [undocumented-behavior.md](undocumented-behavior.md). ## Clocks | Clock | Rate | Source | | --- | --- | --- | | Input oscillator | 24.576 MHz nominal; "indirectly observed to vary" 24.5928-24.6456 MHz | anomie (introduction) | | DSP internal | 3.072 MHz: 96 internal clocks per stereo sample | anomie (introduction) | | S-SMP clock (one **step**) | 1.024 MHz | anomie (introduction) | | Output sample | 32 kHz: one per 32 S-SMP clocks (768 oscillator clocks) | anomie "SOUND GENERATION" | All S-SMP RAM accesses go through the S-DSP, which makes two memory accesses between each S-SMP access (anomie, introduction); fullsnes "Low Level Timings" charts them: per S-SMP cycle two DSP RAM slots and one CPU slot. The module is clocked once per S-SMP clock and runs one of the 32 steps each time ([timing.md](timing.md)). ## The register map ($00-$7F) Registers are 7-bit addressed; `$80-$FF` are read-only mirrors of `$00-$7F` (anomie "S-DSP REGISTERS"; fullsnes "DSP Registers"; spc_dsp6 `Misc/dsp addr above $7F`). Unlisted registers (`$xA`, `$xB`, `$1D`, `$xE`) are general-purpose bytes with no effect (fullsnes "xAh - NA"). Per voice x (`$x0-$x9`), with the step that reads each (anomie's per-register lists): | Reg | Name | Bits | Meaning (anomie section "$xN") | Read at | | --- | --- | --- | --- | --- | | `$x0`/`$x1` | VOLL/VOLR | signed | `(int16)((S * V) >> 7)` | S4 / S5 | | `$x2`/`$x3` | PITCHL/PITCHH | `--pppppp pppppppp` | 14-bit pitch, `$1000` = 32 kHz; the top bits of `$x3` read back as written | S2 / S3a | | `$x4` | SRCN | `nnnnnnnn` | directory entry `DIR*$100 + SRCN*4` | S1 | | `$x5` | ADSR1 | `edddaaaa` | ADSR enable, decay rate `d*2+16`, attack rate `a*2+1` | S2 | | `$x6` | ADSR2 | `lllrrrrr` | sustain level, sustain rate | S3c | | `$x7` | GAIN | `0ggggggg` / `1mmggggg` | direct gain / gain mode and rate | S3c | | `$x8` | ENVX | `0eeeeeee` | DSP-written: E >> 4 | written at S9 | | `$x9` | OUTX | `oooooooo` | DSP-written: the high byte of the voice's output after the envelope, before VOL | written at S8 | Global registers (anomie and fullsnes, same names; steps from anomie): | Reg | Name | Meaning | Read at step | | --- | --- | --- | --- | | `$0C`/`$1C` | MVOLL/MVOLR | main volume, signed | 26 / 27 | | `$2C`/`$3C` | EVOLL/EVOLR | echo volume, signed | 26 / 27 | | `$4C` | KON | key on; "takes effect 'on write'" through an internal KON | 30, every other sample | | `$5C` | KOFF | key off | 30, every other sample | | `$6C` | FLG | `rmennnnn`: soft reset, mute, echo-write disable, noise rate | r at each voice's S3c; e at 28 and 29; n at 30 | | `$7C` | ENDX | voice-end flags; any write clears all bits | written at each voice's S7 | | `$0D` | EFB | echo feedback, signed | 26 | | `$2D` | PMON | pitch modulation, voices 1-7 | 27 | | `$3D` | NON | noise enable | 28 | | `$4D` | EON | echo enable | 28 | | `$5D` | DIR | sample directory page | 28 | | `$6D` | ESA | echo buffer page | 29 (used at the next 22) | | `$7D` | EDL | echo delay `----dddd` | 29, applied only when the echo offset is 0 | | `$xF` | FFC0-FFC7 | FIR coefficients, signed; FFC0 multiplies the oldest sample | 22: FFC0; 23: FFC1-2; 24: FFC3-5; 25: FFC6-7 | The spc_dsp6 `Timing/...` tests name these same registers and steps (list in [validation.md](validation.md)); they were run on consoles by their author. ## The voice pipeline (anomie "SOUND GENERATION", S1-S9) - **S1** load SRCN (with DIR loaded at step 28 it forms the directory entry). - **S2** load the sample pointer if needed (the start address on KON, the loop address after an end block); load PITCHL and ADSR1. - **S3a** load PITCHH, apply pitch modulation. **S3b** load the BRR header (every sample) and the first of the two data bytes. **S3c** the noise or interpolated sample, the envelope applied (the value that modulates the next voice), FLG.7, the header's end check, KOFF and KON, ADSR2 or GAIN, the envelope update. - **S4** VOLL; if a new group is needed, read the second data byte and decode four samples, advance the pointer or flag the loop and set ENDX at S7; advance the position by the pitch. - **S5** VOLR; the new ENDX bit prepared. **S6** OUTX prepared. **S7** ENDX visible; ENVX prepared. **S8** OUTX visible. **S9** ENVX visible. ## BRR (anomie "BRR DECODING"; fullsnes "BRR Samples") Nine-byte blocks: a header `ssssffle`, then 16 signed 4-bit samples, high nibble first. - Shift 0-12: `RD = (D << shift) >> 1`. Shifts 13-15 give `0` or `-$800` by the nibble's sign (anomie; fullsnes: "as if shift=12 and nibble=(nibble SAR 3)"). - Filters, with `S(x-1)`, `S(x-2)` the voice's previous two decoded samples (15-bit, carried across groups and blocks), exactly as anomie and fullsnes write them: - 0: `RD` - 1: `RD + S1 + ((-S1) >> 4)` - 2: `RD + 2*S1 + ((-3*S1) >> 5) - S2 + (S2 >> 4)` - 3: `RD + 2*S1 + ((-13*S1) >> 6) - S2 + ((3*S2) >> 4)` - The result is computed wide, clamped to 16 bits, then clipped to 15 bits (anomie); fullsnes lists the consequences ("+4000h..+7FFFh become -4000h..-1", below -8000h clamps then clips to 0) (U3, U4). - Header bits: `e` (bit 0): after this block continue at the loop address and set ENDX. `e` with `l` clear: "as soon as a header is loaded with 'e' set and 'l' clear, the voice goes into the Release state and the envelope goes to 0 immediately" (anomie), so that block is never heard (U5; spc_dsp6 `Order/brr end uses curr header`). - Decoding never stops: KOFF and FLG.7 only set the envelope; a released voice keeps decoding, looping and setting ENDX; only KON restarts it (anomie; spc_dsp6 `Misc/brr not always decoding`) (U7). - Each voice has a 12-sample ring of three 4-sample groups, two active and one reserve; when the position passes `$4000` the ring turns and a group is decoded (anomie). KON resets the ring index but not its contents, so a first block with filter 1-3 reads the ring's physical end (anomie) (U6). BRR addresses wrap at 16 bits (spc_dsp6 `Misc/brr addr wrap-around`). ## Pitch and interpolation (anomie "PITCH ADJUSTMENTS"; fullsnes "BRR Pitch") - The position is 15 bits: bits 14-12 pick the sample, bits 11-4 the Gaussian phase. At S4: if the position is `>= $4000`, a group is decoded and `$4000` subtracted; then the pitch is added and the position clamped to `$7FFF` (anomie; spc_dsp6 `Misc/interp pos clamped at $7FFF`, `Order/pitch added before interp`, `Order/pitch after brr`) (U8). - Pitch modulation (voices 1-7, PMON): `P + (((OUT[x-1] >> 5) * P) >> 10)`, OUT in its 16-bit form (anomie "$2d PMON"; fullsnes "Pitch Counter" states the same as a factor `(OUTX SAR 4) + $400`). Whether it applies to a noise voice is C5. - Gaussian interpolation over the four samples from the oldest, phase `i`: each product `gauss * sample SAR 10` (15-bit samples); "the 1st addition can't overflow, the 2nd ... bugs, the 3rd ... is saturated"; the result `SAR 1` is 15 bits (fullsnes "4-Point Gaussian Interpolation"). anomie states the products with `>> 11` (C1). - The table: 512 entries 0-1305, printed in anomie and in fullsnes (anomie: "from libopenspc"); fullsnes: the four taps of a phase "sum up to 7FFh..801h". The module generates it with ares's formula (ISC, notice kept), which reproduces all 512 printed values; SHA-256 of the entries as little-endian int16: `0acd5077942f6cc6676829bc1954a05aeaad51be716f62d9ef4e8e7c94685af2` (`scripts/dsp_gauss_formula.py`). Where the values came from first is not documented (U1). ## The counter and rates (anomie "COUNTERS"; fullsnes "ADSR/Gain (and Noise) Rates") One counter, 0 at reset, counting down from `$77FF` once per sample (anomie; updated at step 29). An action at rate R (1-31) fires when `(Counter + offset[R]) % rate[R] == 0`; rate 0 never fires. anomie and fullsnes give the rates (samples per event: 2048, 1536, 1280, 1024, 768, ..., 3, 2, 1; "all values are 1,3,5 SHL n", fullsnes) and anomie the offsets (0, 1040, 536 repeating; 0 for rates 30 and 31) (spc_dsp6 `Misc/counter rate synchronizations`, `Misc/noise rates`, `Envelope/envelope rates`). anomie also prints a mask/xor form attributed to Mednafen (a GPL program): it is not used (C7). ## Envelopes (anomie "$x5-$x7"; fullsnes "ADSR/Gain Envelope"; wiki "DSP envelopes") The envelope E is 11 bits. Modes and their candidate E' and rate: | Mode | Condition | Rate | E' | | --- | --- | --- | --- | | Release | after KOFF, FLG.7 or an end-without-loop block | 31 | `E - 8` | | Attack | ADSR1.7 = 1 | `a*2+1` (`$F`: 31) | `E + 32` (`$F`: `E + 1024`) | | Decay | ADSR1.7 = 1 | `d*2+16` | `E - ((E-1) >> 8) - 1` | | Sustain | ADSR1.7 = 1 | ADSR2 bits 4-0 | as Decay | | Direct gain | ADSR1.7 = 0, GAIN.7 = 0 | any | `GAIN * 16` | | Linear decrease | GAIN `100rrrrr` | r | `E - 32` | | Exponential decrease | `101rrrrr` | r | `E - ((E-1) >> 8) - 1` | | Linear increase | `110rrrrr` | r | `E + 32` | | Bent increase | `111rrrrr` | r | `E + 32` if the saved previous value `< $600`, else `E + 8` | Then anomie's four steps, every sample once the KON sequence allows: (1) if the counter fires at the rate, E = E' clamped to 0-`$7FF`; (2) Decay becomes Sustain when the upper 3 bits match the sustain level, which is ADSR2's in ADSR mode and **GAIN's top 3 bits** otherwise (U9); (3) Attack becomes Decay when E' > `$7FF`, "negative values also trigger this"; (4) the value is saved for bent increase. "These updates happen even when ADSR mode is not selected" (U11; spc_dsp6 `Envelope/attack->decay during gain`, `Envelope/decay->sustain during gain`). For step (4) anomie says "pre-clamp" and fullsnes "clipped to 11 bits ... a negative value ... clipped version being greater than 0x600": the module follows fullsnes (OBSERVED, C15; spc_dsp6 `Envelope/hidden env after gain`). ADSR1 is read at S2 and ADSR2/GAIN at S3c, so a write between them splits (spc_dsp6 `Timing/Voice/V2 adsr0.*`, `V3 adsr1.*`, `V3 gain.*`). The voice output is `(S * E) >> 11` (anomie). ## KON and KOFF (anomie "$4c KON / $5c KOFF"; fullsnes "KON/KOFF Notes") - Polled "only at 16000 Hz": internal KON bits are cleared at step 29 and KOFF and the internal KON loaded at step 30, every other sample; "the internal KON bits are not cleared until 63 cycles after they are loaded" (anomie) (U13; spc_dsp6 `KON/kon every other sample`, `Timing/Misc/29 kon cleared`, `30 kon`, `30 koff`). - At each voice's S3c: FLG.7 or the KOFF bit sends it to Release (FLG.7 also sets E = 0); then a KON bit performs the KON actions; KON "takes effect 'on write', even though a non-zero value can be read back much later" (anomie). KON while KOFF is set gives no sound: "turned off again 2 samples later" (U14; spc_dsp6 `KON/kon then koff`). - The KON sequence, anomie's samples #0-#6: #0 envelope 0, Attack, interpolation and ring index reset, the final pre-KON decode; #1 the start address read at S2, no decoding, no header checks; #2-#4 one group decoded each at S4; #5 envelope updating begins, output still 0; #6 the first data sample, and the first position update at S4. KON clears the voice's ENDX bit at S7 of sample #0 (anomie S3c, S4) (spc_dsp6: the 18 `KON/...` tests). ## ENDX, ENVX, OUTX ENDX bit x is set when voice x finishes an `e` block ("at the START of decoding the BRR block, not at the end", anomie and fullsnes: the next block's start); any write clears all bits (anomie; spc_dsp6 `Misc/endx write clears immediately`). ENDX (S5/S7), OUTX (S6/S8) and ENVX (S7/S9) are prepared two steps before they become visible, and "a write by the SMP to this register up to 2 cycles earlier will overwrite the DSP's updated value" (anomie) (U15; spc_dsp6 `Timing/Voice/V7 endx *`, `V8 outx`, `V9 envx`). The waiting ENVX and OUTX values sit in one latch each, shared by all voices (OBSERVED, C16). ## Noise (anomie "$3d NON"; fullsnes "NON") One 15-bit generator, `$4000` at reset; updated at step 30 when the counter fires at FLG's rate: `N = (N >> 1) | (((N << 14) ^ (N << 13)) & $4000)` (anomie; fullsnes states the same as "Level.Bit0 XOR Level.Bit1"). A NON voice outputs N (15-bit signed: initially -`$4000`, fullsnes) instead of the interpolated sample, before the envelope; its BRR decoding, pitch, ENDX and end-without-loop release continue (anomie, fullsnes) (U21; spc_dsp6 `Misc/noise rates`, `Order/noise rate flg.1F`, `Order/voice 0 noise`). ## Mixing and output (anomie "SOUND GENERATION", "$x0", "$0c", "$2c"; fullsnes "Volume Registers") - Each voice's output times VOL (`(int16)((S*V) >> 7)`) is added to the main sum and, with its EON bit, to the echo sum, "clamping to 16 bits after each addition" (spc_dsp6 `Misc/voice sums clamped each time`). - Per channel: `clamp16((int16)((main * MVOL) >> 7) + (int16)((FIR * EVOL) >> 7))`; FLG.6 mutes. anomie outputs left at step 26 and right at 27 (C3). fullsnes's final `XOR FFFFh` is the analogue post-amplifier's phase inversion: outside the digital output. - 16-bit signed stereo at 32 kHz. The DAC, its filter and the analogue path are the board's. ## Echo (anomie "$6d ESA", "$7d EDL", "$xf FFCx"; fullsnes "Echo Registers"; wiki errata) - The buffer is `EDL * 2048` bytes (4 bytes for EDL 0) at `ESA * $100`, wrapping at 16 bits ("clobbering zeropage", wiki) (spc_dsp6 `Echo/wrap_around`, `Echo/zero_length`, `Echo/edl 0 quirk`, `Echo/edl lengths`). - Step 22: the pointer from the ESA loaded at the previous 29 and the echo offset; left word read (23: right), each `>> 1` into the FIR history. FIR: `(int16)` of the sum of `S(x-7)*FFC0 >> 6` ... `S(x-1)*FFC6 >> 6`, then `clamp16(+ S(x)*FFC7 >> 6)`, then `& ~1` (anomie, fullsnes; spc_dsp6 `Random/echo fir`, `Echo/echo calc`). The referee differs at one point of this (D1). - Echo out: `clamp16(echo_sum + (int16)((FIR * EFB) >> 7)) & ~1`, written at step 29 (left) and 30 (right) unless FLG.5, which is loaded at 28 for the left write and 29 for the right. - Step 29: if the offset is 0, the length is loaded from EDL; step 30: the offset advances by 4 and returns to 0 at the length; so a new EDL can take up to 0.24 s (anomie; fullsnes; wiki) (U18; spc_dsp6 `Echo/edl_changes`, `Order/edl before inc offset`). ESA is loaded at 29, "32 cycles before the value is used" (U20; spc_dsp6 `Timing/Echo/29 esa`, `Order/esa applied after echo write`). With writes disabled the buffer keeps being read: "a static sample buffer up to 0.96 seconds long" (anomie). - Echo writes are ARAM writes the S-SMP sees (wiki) (U19). ## FLG ($6C) - **r**: "acts as if KOFF=$ff and forces all envelopes to 0", checked at each voice's S3c (anomie) (spc_dsp6 `KON/kon then flg.80`, `Order/flg.80 after env used`, `Order/flg.80 clears next envx`). - **m**: mute; "samples will still be decoded, echos processed" (anomie). The analogue mute pulse is outside the digital output (C10). - **e**: echo write disable (anomie, fullsnes). - **nnnnn**: noise rate, step 30. ## Power-on and reset anomie "S-DSP REGISTERS": "On power on, most registers are uninitialized ... seems to differ between chips. On reset, most registers retain their previous values. ... FLG will always act as if set to 0xE0 after power on or reset, even if the value read back indicates otherwise. VxENVX and VxOUTX are of course 0 ... ENDX will be 0". fullsnes "DSP Registers" says ENDX becomes `FFh` "a few cycles after reset" and gives power-up garbage examples (C11, C17). The counter is 0 and noise `$4000` at reset (anomie). Where step 0 and the every-other-sample poll fall after reset is not documented (U22, C12). The IPL ROM does not set FLG (wiki "S-DSP registers"). ## What the module does not own ARAM itself, the `$F2` latch and `$F3` decode, `$F0`/`$F1`, the timers and the IPL ROM (the S-SMP side, `snes_python/smp.py`); the oscillator rate (a machine-table entry); resampling, the DAC's analogue filter and cartridge audio; the `.spc` file format (`snes_python/spc_file.py`).