#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Generate synthetic .spc files that exercise the S-DSP, for the referee and the rungs. Each file is a 64 KB ARAM image with BRR samples, a sample directory, and a small SPC700 program that writes DSP registers through $F2/$F3 at chosen moments, then spins. They contain no game data, so they (and this generator) may be committed; what they make the referee print is recorded in docs/dsp/validation.md. python -I scripts/dsp_make_spc.py OUTDIR # writes OUTDIR/.spc ARAM layout (every case): $0000-$01FF direct page and stack (untouched by the DSP) $0400-$0FFF program $1000-$10FF sample directory (DIR = $10), 64 entries $1100-$7FFF BRR samples $8000-$F7FF echo buffer region (ESA = $80, EDL <= 15) The SPC700 registers in the image: $F1 = $00 (IPL ROM off, timers off). """ from __future__ import annotations import random import struct import sys from pathlib import Path PROGRAM = 0x0400 DIR_PAGE = 0x10 SAMPLES = 0x1100 # DSP register numbers VOLL, VOLR, PITCHL, PITCHH, SRCN, ADSR1, ADSR2, GAIN = range(8) MVOLL, MVOLR, EVOLL, EVOLR, KON, KOFF, FLG, ENDX = (0x0C, 0x1C, 0x2C, 0x3C, 0x4C, 0x5C, 0x6C, 0x7C) EFB, PMON, NON, EON, DIR, ESA, EDL = (0x0D, 0x2D, 0x3D, 0x4D, 0x5D, 0x6D, 0x7D) FIR = 0x0F class Program: """A tiny SPC700 assembler: just the instructions the cases need.""" def __init__(self) -> None: self.code = bytearray() def write(self, register: int, value: int) -> None: self.code += bytes((0x8F, register & 0xFF, 0xF2)) # MOV $F2,#register (5 cycles) self.code += bytes((0x8F, value & 0xFF, 0xF3)) # MOV $F3,#value (5 cycles) def voice(self, voice: int, register: int, value: int) -> None: self.write(voice << 4 | register, value) def nops(self, count: int) -> None: self.code += bytes(count) # NOP, 2 cycles each: shifts the phase of what follows def odd(self) -> None: self.code += bytes((0xE4, 0x00)) # MOV A,$00 (3 cycles): with NOPs, reaches odd phases def wait(self, cycles: int) -> None: """Spin for about `cycles` SPC700 clocks (6 per inner iteration).""" outer, inner = divmod(max(cycles // 6, 1), 256) for count in [256] * outer + ([inner] if inner else []): self.code += bytes((0xCD, count & 0xFF)) # MOV X,#count self.code += bytes((0x1D, 0xD0, 0xFD)) # DEC X ; BNE -3 def wait_samples(self, samples: int) -> None: self.wait(samples * 32) def spin(self) -> None: self.code += bytes((0x2F, 0xFE)) # BRA $ def brr_block(rng: random.Random, shift: int, filt: int, end: bool, loop: bool) -> bytes: header = shift << 4 | filt << 2 | (2 if loop else 0) | (1 if end else 0) return bytes([header]) + bytes(rng.randrange(256) for _ in range(8)) def sine_block(phase: int, shift: int, filt: int, end: bool, loop: bool) -> bytes: """A block of a 16-sample-period triangle in 4-bit nibbles.""" header = shift << 4 | filt << 2 | (2 if loop else 0) | (1 if end else 0) shape = [0, 2, 4, 6, 7, 6, 4, 2, 0, -2, -4, -6, -8, -6, -4, -2] nibbles = [shape[(phase + i) % 16] & 0xF for i in range(16)] return bytes([header]) + bytes(nibbles[i] << 4 | nibbles[i + 1] for i in range(0, 16, 2)) class Image: def __init__(self, seed: int) -> None: self.ram = bytearray(0x10000) self.dsp = bytearray(128) self.rng = random.Random(seed) self.next_sample = SAMPLES self.entries = 0 self.ram[0xF1] = 0x00 # CONTROL: IPL ROM off, timers off self.ram[0xF2] = 0x00 # DSP registers at load: everything 0, echo writes off, not muted. self.dsp[FLG] = 0x20 self.dsp[DIR] = DIR_PAGE self.dsp[ESA] = 0x80 self.dsp[MVOLL] = self.dsp[MVOLR] = 0x7F def sample(self, blocks: list[bytes], loop_block: int | None = 0) -> int: """Store a BRR sample, add a directory entry, return its SRCN.""" start = self.next_sample data = b"".join(blocks) self.ram[start : start + len(data)] = data self.next_sample += len(data) assert self.next_sample < 0x8000 loop = start + 9 * (loop_block or 0) srcn = self.entries struct.pack_into(" bytes: self.ram[origin : origin + len(program.code)] = program.code header = bytearray(0x100) header[0:33] = b"SNES-SPC700 Sound File Data v0.30" header[33:35] = b"\x1a\x1a" header[35] = 27 # no ID666 tag header[36] = 30 struct.pack_into(" int: blocks = [] for shift in range(16): for filt in range(4): last = shift == 15 and filt == 3 blocks.append( brr_block(image.rng, shift, filt, end=last and end_loop, loop=last and end_loop) ) return image.sample(blocks, loop_block=0) def case_brr() -> bytes: """Every BRR range (incl. the invalid 13-15) under every filter, eight pitches.""" image = Image(1) srcn = every_range_and_filter(image) tri = image.sample( [sine_block(0, 11, 0, False, False)] + [sine_block(0, 11, 2, i == 7, i == 7) for i in range(8)], 0, ) p = Program() pitches = [0x1000, 0x0800, 0x0400, 0x2000, 0x3FFF, 0x0123, 0x1E00, 0x0001] for v in range(8): p.voice(v, SRCN, srcn if v < 6 else tri) p.voice(v, VOLL, 0x30 if v % 2 else 0x10) p.voice(v, VOLR, 0x10 if v % 2 else 0x30) p.voice(v, PITCHL, pitches[v] & 0xFF) p.voice(v, PITCHH, pitches[v] >> 8) p.voice(v, ADSR1, 0x00) p.voice(v, GAIN, 0x7F) # direct gain, full p.write(KON, 0xFF) p.spin() return image.spc(p, "brr ranges filters") def case_envelopes() -> bytes: """ADSR at assorted rates, sustain levels, release; GAIN modes 4-7 and direct.""" image = Image(2) tri = image.sample([sine_block(i, 12, 0, i == 3, i == 3) for i in range(4)], 0) p = Program() settings = [ (0x8F, 0xE0, 0x00), # ADSR: attack F (instant), decay 0, sustain 7, SR 0 (0x8A, 0x2F, 0x00), # attack A, sustain 1, SR $0F (0x80, 0x5B, 0x00), # attack 0 (slowest), sustain 2, SR $1B (0xF7, 0xB4, 0x00), # decay 7, sustain 5 (0x00, 0x00, 0x9C), # GAIN 4: linear decrease, rate $1C (after a direct preset below) (0x00, 0x00, 0xBE), # GAIN 5: exponential decrease (0x00, 0x00, 0xD8), # GAIN 6: linear increase (0x00, 0x00, 0xF8), # GAIN 7: bent-line increase ] for v, (adsr1, adsr2, gain) in enumerate(settings): p.voice(v, SRCN, tri) p.voice(v, VOLL, 0x20) p.voice(v, VOLR, 0x20) p.voice(v, PITCHH, 0x08 + v) p.voice(v, ADSR1, adsr1) p.voice(v, ADSR2, adsr2) p.voice( v, GAIN, 0x60 if v in (4, 5) else gain ) # decreasing modes start from a direct level p.write(KON, 0xFF) p.wait_samples(200) p.voice(4, GAIN, 0x9C) p.voice(5, GAIN, 0xBE) p.wait_samples(4000) p.write(KOFF, 0x0F) # release the ADSR voices p.wait_samples(2000) p.write(KOFF, 0x00) p.voice(0, ADSR1, 0x00) # switch voice 0 to GAIN mid-note p.voice(0, GAIN, 0xE4) p.write(KON, 0x01) p.wait_samples(3000) p.spin() return image.spc(p, "envelopes") def case_echo() -> bytes: """Echo with an overflowing FIR, negative feedback, EDL and ESA changed mid-run.""" image = Image(3) srcn = every_range_and_filter(image) p = Program() for v in range(4): p.voice(v, SRCN, srcn) p.voice(v, VOLL, 0x40) p.voice(v, VOLR, 0xC0) # negative volume p.voice(v, PITCHH, 0x04 << v) p.voice(v, GAIN, 0x7F) p.write(EDL, 4) p.write(ESA, 0x80) for i, coefficient in enumerate([0x7F] * 8): # sum of taps far over 1.0: exercises the clamps p.write(FIR + i * 0x10, coefficient) p.write(EFB, 0x90) p.write(EVOLL, 0x60) p.write(EVOLR, 0xA0) p.write(EON, 0x0F) p.write(FLG, 0x00) # echo writes on p.write(KON, 0x0F) p.wait_samples(6000) for i, coefficient in enumerate([0xFF, 0x08, 0x17, 0x24, 0x24, 0x17, 0x08, 0xFF]): p.write(FIR + i * 0x10, coefficient) p.write(EDL, 1) # takes effect when the offset next wraps to 0 p.wait_samples(6000) p.write(ESA, 0xA0) # moves the buffer at once (sampled every sample) p.wait_samples(3000) p.write(FLG, 0x20) # echo writes off: the buffer replays what it holds p.wait_samples(3000) p.spin() return image.spc(p, "echo fir") def case_noise_pmod() -> bytes: """Noise at several clock rates; pitch modulation down a chain of voices.""" image = Image(4) tri = image.sample([sine_block(i, 11, 1, i == 7, i == 7) for i in range(8)], 0) p = Program() for v in range(8): p.voice(v, SRCN, tri) p.voice(v, VOLL, 0x20) p.voice(v, VOLR, 0x20) p.voice(v, PITCHH, 0x02 + v) p.voice(v, PITCHL, 0x37 * v) p.voice(v, GAIN, 0x7F) p.write(NON, 0x81) # voice 0 noise (its output still drives voice 1's PMON) p.write(PMON, 0xFF) # bit 0 is ignored p.write(FLG, 0x20 | 0x1F) p.write(KON, 0xFF) for rate in (0x1E, 0x10, 0x08, 0x01, 0x00, 0x1A): p.wait_samples(2000) p.write(FLG, 0x20 | rate) p.wait_samples(2000) p.spin() return image.spc(p, "noise pmod") def case_kon_timing() -> bytes: """KON and KOFF at every phase a 2-cycle NOP can reach; ENDX writes; one-shot samples.""" image = Image(5) one_shot = image.sample([sine_block(i, 12, 0, i == 2, False) for i in range(3)], None) looped = image.sample([sine_block(i, 12, 0, i == 2, i == 2) for i in range(3)], 1) p = Program() for v in range(8): p.voice(v, SRCN, one_shot if v % 2 else looped) p.voice(v, VOLL, 0x18) p.voice(v, VOLR, 0x18) p.voice(v, PITCHH, 0x10) p.voice(v, ADSR1, 0x8F) p.voice(v, ADSR2, 0xE0) for step in range(32): p.nops(step) if step % 2: p.odd() p.write(KON, 1 << (step % 8) | 0x02) p.nops(step % 5) if step % 3 == 1: p.odd() p.write(KOFF, 1 << ((step + 3) % 8)) p.write(KOFF, 0x00) p.wait_samples(37) if step % 4 == 0: p.write(ENDX, 0x00) p.write(KON, 0xFF) p.write(KON, 0x00) # rewritten within the same sample pair p.wait_samples(500) p.spin() return image.spc(p, "kon koff timing") def case_flg() -> bytes: """FLG: mute, soft reset (bit 7), echo write disable, toggled while voices play.""" image = Image(6) srcn = every_range_and_filter(image) p = Program() for v in range(8): p.voice(v, SRCN, srcn) p.voice(v, VOLL, 0x20) p.voice(v, VOLR, 0x20) p.voice(v, PITCHH, 0x08) p.voice(v, GAIN, 0x7F) p.write(EDL, 2) p.write(EON, 0xFF) p.write(EFB, 0x40) p.write(EVOLL, 0x40) p.write(EVOLR, 0x40) p.write(FIR, 0x7F) p.write(FLG, 0x00) p.write(KON, 0xFF) for flg in (0x40, 0x00, 0x80, 0x00, 0x20, 0x00, 0xE0, 0x00): p.wait_samples(1500) p.write(FLG, flg) if flg == 0x00: p.write(KON, 0xFF) p.wait_samples(1500) p.spin() return image.spc(p, "flg") def case_endx_override() -> bytes: """ENDX, ENVX and OUTX written at every step phase, into and around the override windows.""" image = Image(7) looped = image.sample([sine_block(i, 12, 0, i == 1, i == 1) for i in range(2)], 0) p = Program() for v in range(8): p.voice(v, SRCN, looped) p.voice(v, VOLL, 0x20) p.voice(v, VOLR, 0x20) p.voice(v, PITCHH, 0x20) # short blocks: ENDX bits set often p.voice(v, GAIN, 0x7F) p.write(KON, 0xFF) p.wait_samples(16) for step in range(64): p.nops(step % 16) if step % 2: p.odd() p.write(ENDX, step) p.nops(step % 7) p.voice(step % 8, 0x08, 0x55) # ENVX p.nops(step % 5) p.voice((step + 3) % 8, 0x09, 0xAA) # OUTX p.wait_samples(3) p.spin() return image.spc(p, "endx envx outx writes") def case_edl_zero() -> bytes: """EDL 0's four-byte buffer, entered and left, with feedback and writes on.""" image = Image(8) srcn = every_range_and_filter(image) p = Program() for v in range(4): p.voice(v, SRCN, srcn) p.voice(v, VOLL, 0x30) p.voice(v, VOLR, 0x30) p.voice(v, PITCHH, 0x08 << v) p.voice(v, GAIN, 0x7F) p.write(EDL, 0) p.write(ESA, 0x90) p.write(FIR + 0x70, 0x7F) p.write(EFB, 0x60) p.write(EVOLL, 0x7F) p.write(EVOLR, 0x7F) p.write(EON, 0x0F) p.write(FLG, 0x00) p.write(KON, 0x0F) p.wait_samples(4000) p.write(EDL, 1) # taken at once: the offset is at 0 every sample p.wait_samples(4000) p.write(EDL, 0) # taken only when the offset next wraps p.wait_samples(4000) p.spin() return image.spc(p, "edl 0") def case_echo_wrap() -> bytes: """An echo buffer across $FFFF into the zero page, the program kept out of its way. The buffer covers $F900-$00FF. That holds the S-SMP's $F2/$F3 RAM shadows, which the program's stores also reach, so this case replays the log's M lines as well. """ image = Image(9) srcn = every_range_and_filter(image) p = Program() for v in range(4): p.voice(v, SRCN, srcn) p.voice(v, VOLL, 0x30) p.voice(v, VOLR, 0xD0) p.voice(v, PITCHH, 0x06 + v) p.voice(v, GAIN, 0x7F) p.write(EDL, 1) p.write(ESA, 0xF9) p.write(FIR, 0x40) p.write(FIR + 0x70, 0x40) p.write(EFB, 0x40) p.write(EVOLL, 0x60) p.write(EVOLR, 0x60) p.write(EON, 0x0F) p.write(FLG, 0x00) p.write(KON, 0x0F) p.wait_samples(12000) p.spin() return image.spc(p, "echo wrap") def case_kon_ring() -> bytes: """KON over a sounding voice into a first block with filters 1-3 (the ring survives).""" image = Image(10) tails = [ image.sample( [sine_block(i, 10, 2, False, False) for i in range(3)] + [sine_block(3, 10, 2, True, True)], 0, ), image.sample( [sine_block(i, 9, filt, i == 3, i == 3) for i, filt in enumerate((1, 2, 3, 1))], 0 ), image.sample( [sine_block(i, 11, filt, i == 3, i == 3) for i, filt in enumerate((3, 1, 2, 3))], 0 ), ] p = Program() for v in range(8): p.voice(v, SRCN, tails[0]) p.voice(v, VOLL, 0x20) p.voice(v, VOLR, 0x20) p.voice(v, PITCHH, 0x08 + v) p.voice(v, GAIN, 0x7F) p.write(KON, 0xFF) for round_number in range(48): p.wait_samples(211) voice = round_number % 8 p.voice(voice, SRCN, tails[1 + round_number % 2]) p.write(KON, 1 << voice) p.write(KON, 0x00) p.spin() return image.spc(p, "kon ring") def case_env_quirks() -> bytes: """The envelope's quirks: GAIN mid-attack and mid-decay, the bent line, low-rate edges.""" image = Image(11) tri = image.sample([sine_block(i, 12, 0, i == 3, i == 3) for i in range(4)], 0) p = Program() for v in range(8): p.voice(v, SRCN, tri) p.voice(v, VOLL, 0x10) p.voice(v, VOLR, 0x10) p.voice(v, PITCHH, 0x10) p.voice(v, ADSR1, 0x8F if v < 4 else 0x8A) # fast or moderate attack, decay 0 p.voice(v, ADSR2, 0x40 + v * 0x20 & 0xFF) p.voice(v, GAIN, 0x80 + 0x20 * (v % 4) + 0x12) p.write(KON, 0xFF) p.wait_samples(60) for v in range(8): # to GAIN mid-attack or mid-decay: mode changes keep running (U9, U11) p.voice(v, ADSR1, 0x7F) p.wait_samples(1500) for v in range(8): # the bent line after a level reached another way (U10) p.voice(v, GAIN, 0xE0 + v * 2 + 0x0C) p.wait_samples(1500) for v in range(8): # linear decrease to below zero, then the bent line (a negative hidden) p.voice(v, GAIN, 0x9F) p.wait_samples(400) for v in range(8): p.voice(v, GAIN, 0xFF) p.wait_samples(600) for v in range(8): # back to ADSR at a slow sustain, with rate 0 on odd voices p.voice(v, ADSR2, 0x1F * (v % 2) + 0xE0) p.voice(v, ADSR1, 0xFF) p.wait_samples(1500) p.write(KOFF, 0xFF) p.wait_samples(2000) p.write(KOFF, 0x00) p.spin() return image.spc(p, "envelope quirks") def level_block(nibble: int, end: bool) -> bytes: """A block of one nibble at shift 12: a flat, loud level.""" return bytes([0xC0 | (3 if end else 0)]) + bytes([(nibble & 0xF) * 0x11] * 8) def case_latch_sweep() -> bytes: """Registers latched once a sample, toggled at every one of the 32 step phases. A write lands every 35 clocks, so its phase moves on 3 each time and 32 writes visit every phase; a second pass with the two values swapped gives every phase both of them. A value read live instead of from its latch differs on the writes that fall between latch and use. The program is long, so it sits at $2000. """ image = Image(12) srcn = every_range_and_filter(image) p = Program() for v in range(8): p.voice(v, SRCN, srcn) p.voice(v, VOLL, 0x28) p.voice(v, VOLR, 0x28) p.voice(v, PITCHH, 0x06 + v) p.voice(v, ADSR1, 0x8F) p.voice(v, ADSR2, 0xFC) # a slow sustain: still sounding when the last sweep ends p.voice(v, GAIN, 0x40) p.write(EDL, 2) p.write(FIR + 0x70, 0x60) p.write(EFB, 0x30) p.write(EVOLL, 0x50) p.write(EVOLR, 0x50) p.write(EON, 0xFF) p.write(FLG, 0x00) p.write(KON, 0xFF) p.wait_samples(400) def sweep(register: int, values: tuple[int, int]) -> None: for index in range(64): p.write(register, values[index % 2]) p.nops(11) p.odd() for register, first, second in ( (PMON, 0xFE, 0), (NON, 0xFF, 0), (EON, 0, 0xFF), (FLG, 0x20, 0), ): sweep(register, (first, second)) sweep(register, (second, first)) p.wait_samples(50) for v in range(8): sweep(v << 4 | ADSR1, (0x0F, 0x8F)) p.wait_samples(3000) p.spin() return image.spc(p, "latch sweep", origin=0x2000) def case_edges() -> bytes: """Single behaviours set up one voice at a time: U8, U9, U10 at KON, U14.""" image = Image(13) level = image.sample([level_block(7, True)], 0) noisy = image.sample([brr_block(image.rng, 11, 0, i == 7, i == 7) for i in range(8)], 0) tri = image.sample([sine_block(i, 11, 0, i == 3, i == 3) for i in range(4)], 0) p = Program() for v in range(8): p.voice(v, VOLL, 0x18) p.voice(v, VOLR, 0x18) p.voice(v, SRCN, tri) p.voice(v, PITCHH, 0x10) # The interpolator's three-tap wrap: a flat -$8000 at phase 0 comes out positive. p.voice(6, SRCN, image.sample([level_block(-8, True)], 0)) p.voice(6, GAIN, 0x7F) p.voice(6, PITCHH, 0x00) # U8: voice 0's loud flat level modulates voice 1's top pitch past what a group holds. p.voice(0, SRCN, level) p.voice(0, GAIN, 0x7F) p.voice(1, SRCN, noisy) p.voice(1, GAIN, 0x7F) p.voice(1, PITCHL, 0xFF) p.voice(1, PITCHH, 0x3F) p.write(PMON, 0x02) # U10: the bent line, keyed on again from its top. p.voice(2, GAIN, 0xFF) # U9: decay, then GAIN falling through level 5, then ADSR again at a sustain rate of 0. p.voice(5, ADSR1, 0x8F) p.voice(5, ADSR2, 0x00) p.write(KON, 0x67) p.wait_samples(40) p.voice(5, GAIN, 0xBF) p.voice(5, ADSR1, 0x0F) p.wait_samples(120) p.voice(5, ADSR1, 0x8F) p.wait_samples(400) p.write(KON, 0x04) # the hidden value must restart from 0 p.wait_samples(300) # U14: KON with KOFF set and left set, on a slow attack: released at the next poll. p.voice(3, ADSR1, 0x80) p.voice(3, ADSR2, 0xE0) p.write(KOFF, 0x08) p.write(KON, 0x08) p.wait_samples(300) p.write(KOFF, 0x00) p.wait_samples(50) # U14 again, KOFF cleared in time: the voice lives. p.voice(4, ADSR1, 0x80) p.voice(4, ADSR2, 0xE0) p.write(KOFF, 0x10) p.write(KON, 0x10) p.write(KOFF, 0x00) p.wait_samples(1000) p.spin() return image.spc(p, "edges") def case_saturation() -> bytes: """Products that reach +$8000, which only a cast to 16 bits turns back (U17). The echo buffer starts full of -$8000, FIR7 and EFB are -128, and four loud voices hold the main sum at -$8000 against an MVOL of -128. """ image = Image(14) # -7, not -8: a flat -$8000 lands above the three-tap wrap and comes out positive. level = image.sample([level_block(-7, True)], 0) image.ram[0x8000:0x9000] = bytes((0x00, 0x80)) * 0x800 p = Program() for v in range(4): p.voice(v, SRCN, level) p.voice(v, VOLL, 0x7F) p.voice(v, VOLR, 0x7F) p.voice(v, PITCHH, 0x10) p.voice(v, GAIN, 0x7F) p.write(EDL, 1) p.write(FIR + 0x70, 0x80) p.write(EFB, 0x80) p.write(MVOLL, 0x80) p.write(MVOLR, 0x80) p.write(EVOLL, 0x7F) p.write(EVOLR, 0x7F) p.write(FLG, 0x00) p.write(KON, 0x0F) p.wait_samples(6000) p.write(KOFF, 0x0F) p.wait_samples(2000) p.spin() return image.spc(p, "saturation") CASES = { "brr": case_brr, "envelopes": case_envelopes, "echo": case_echo, "noise-pmod": case_noise_pmod, "kon-timing": case_kon_timing, "flg": case_flg, "endx-override": case_endx_override, "edl-zero": case_edl_zero, "echo-wrap": case_echo_wrap, "kon-ring": case_kon_ring, "env-quirks": case_env_quirks, "latch-sweep": case_latch_sweep, "edges": case_edges, "saturation": case_saturation, } def main() -> int: if len(sys.argv) != 2: print(__doc__) return 2 out = Path(sys.argv[1]) out.mkdir(parents=True, exist_ok=True) for name, build in CASES.items(): (out / f"{name}.spc").write_bytes(build()) print(f"wrote {out / name}.spc") return 0 if __name__ == "__main__": sys.exit(main())