Files

14 KiB
Raw Permalink Blame History

TBG (J) — sound: driver upload, command protocol, songs, SFX, runtime design

[C] = confirmed in Mesen (port/RAM/DSP captures) or by byte-exact reproduction, [I] = inferred from code. All addresses are SNES CPU addresses unless prefixed "SPC $".

Tools (this pass)

file what
re/tools/snd_cpu.py small 65816 interpreter (LoROM, WRAM, PC hooks) used to run the game's own sound code
re/tools/snd_build_spc.py runs 88:8000 like the game (boot = cmd 00, 01, 19; then cmd $13 song) with the APU primitives hooked → SPC RAM image + an ordered APU op list (--ops); --compare against a Mesen SPC RAM dump; --list prints the 38 sound sets
re/tools/snd_play.cpp host for the SPC side: replays an op list against blargg's snes_spc 0.9.0 (third_party/snes_spc, gitignored clone of github.com/blarggs-audio-libraries/snes_spc, LGPL-2.1) with the exact CPU-side handshakes, renders WAV, writes .spc/RAM/DSP after each play
re/tools/snd_render_all.py renders every song id → re/runs/snd/songs/song_XX.{wav,spc} (20 s each)
re/lua/snd_log.lua Mesen: log every 88:8000 call (A=cmd, X, Y, caller) + optional APU port writes + SPC RAM/DSP/regs dumps
re/lua/snd_power.lua same logging around battle_power.lua (forced P1 casts)

Build the host: g++ -O2 -std=c++17 -Ithird_party/snes_spc re/tools/snd_play.cpp third_party/snes_spc/snes_spc/{SNES_SPC,SNES_SPC_misc,SNES_SPC_state,SPC_DSP,SPC_Filter}.cpp -o re/runs/snd/snd_play Example: python3 -I re/tools/snd_build_spc.py rom/tbg_jp.sfc --song 0x02 --play 600 --ops /tmp/o.txt && re/runs/snd/snd_play /tmp/o.txt /tmp/s02

1. Sound code and data

  • Dispatcher 88:8000 (BRA 88:801E): JSL 88:8000 with A = command, X/Y = parameters. Saves regs, sets D = $0000 (scratch DP $00–$7F) and DBR = $88, jumps through the 26-entry table at 88:803D. Work tables in WRAM 7E:2000–227F. [C]
  • Container 88:8A48 (file 0x40A48): 6 big-endian 32-bit offsets, linear LoROM offsets from 88:8A48 (88:835C converts them to bank:addr). [C]
    • table 0 (file 0x40A60): count (BE16) = 38 sound sets, each [size16 LE][nA][A ids][nB][B ids][nC][C ids] (88:83AB/88:83EC). C = song ids the set can play, B = SFX ids it carries, A = instrument ids [C for C/B by observed triggers, A [I]]. snd_build_spc.py --list prints them.
    • table 1 (0x40F52, 100 × 4 bytes), table 4 (0x752DA, 60 × (off32,len32)), table 5 (0x760BE, 60 × (off32,len32)): instrument / sequence / sample directories used by the set builders (88:85B8 … 88:8A38) [I — not needed: the builder is run, not reimplemented].
    • table 3 (file 0x738E0) = IPL block list: one block of $19F1 bytes → SPC $0800 (the driver), end block len 0, jump $0812. [C]
  • Upload primitives (all [C], reproduced byte-exact):
    • 88:84B9 boot: standard IPL protocol ($BBAA wait, $CC start, byte/counter pairs), blocks [len16][dest16][data].
    • 88:8553 = command $3E (88:8589) + 88:8570 enter-transfer: ports2/3 := 0; wait ports2/3 == $0265; 16-bit STZ $2140 (ports 0 and 1 := 0); ports2/3 := $0265.
    • 88:8301 set address: ports2/3 := addr; port0 := (read(port0) ^ $80) | $40; wait echo.
    • 88:831A write word: ports2/3 := word; port0 := (read(port0) ^ $80) & $80; wait echo. SPC stores the word at the address and advances by 2.
    • 88:827A / 88:82C3 stream: set address $1C, then words from the 24-bit pointer $64 ($68 bytes rounded up to even; 827A continues into the next bank at $8000); $1C += len.
    • 88:8A38 kick: port0 := read(port0) ^ $80 | $01 (no wait).
    • 88:8561 end-transfer: wait ports2/3 == $0968 (driver ready); $76 := 0.
    • 88:8589 driver command: wait read(port0) == $76; port0 := ((read(port0) ^ $80) & $80) | cmd; $76 := sent.
    • 88:85A5 SFX: port1 := ((read(port1) ^ $80) & $80) | id (no wait).
  • SPC RAM layout: driver $0800–$21F0 (pointer header $0800–$0811, code from $0812); a sound set is uploaded from $2200 (pointer tables at $2200–$23xx, data/samples from $2400; the battle sets reach ~$F5EE). After a set loads, the driver relocates its own operand addresses (e.g. ROM F0xx → 53xx), so a static RAM dump is not a playable state — the driver has to process the command itself. [C]

Transfers per scene [C, Mesen boot→battle capture re/runs/snd/boot]

when calls (A,Y) upload
power-on 80:8053..8064 00, 01, 19/Y=0 IPL driver $0800–$21F0, driver init
BPS logo 80:8098 13/Y=$24 set $0D → $2200–$55A0, then plays song $24
title 83:ECF5 13/$20 set $10
char select 84:EC64 13/$11 set $03 ($2200–$C4F4)
Battle World 84:8119 13/$17 (same set $03 — no upload)
battle 80:B7A6 13/stage song the stage's set ($2200–~$F5EE)
power cast 80:B7A6 13/power song power set (if not $2B/$09/$0C/$2A), stage set reloaded after

Validation [C]:

  • Interpreter image vs Mesen SPC RAM: song $24 (19 637 uploaded bytes) and the battle set for song $02 (54 204 bytes) and the Copy power set (12 436 bytes): 0 differing bytes.
  • Host (snes_spc) vs Mesen after 600 frames of song $24: all uploaded + relocated bytes identical; DSP voice registers (VOL, PITCH, SRCN, ADSR, GAIN, MVOL, EVOL, …) identical at the same moment; only read-only ENVX/OUTX/ENDX and 3 driver variables ($15, $63, $206) differ (timing).
  • All 57 song ids render (re/runs/snd/songs/); SFX over music changes the output only after each SFX.

2. Command protocol (88:803D table) [C unless noted]

cmd entry effect
$00 8073 clear sound WRAM, parse container
$01 8082 IPL-upload the driver, wait ready
$02 8091 preload the set containing song Y (no play) [I] (used at 80:B725)
$06 80EC port3 := Y, driver cmd $36 — danger/pinch music: Y=$20 on, Y=0 off (81:A869 at every spawn: on if either board has a block in its top 4 playfield rows; re-sent after a stage song starts 80:B6D5)
$08 8105 query: returns Y = current set | $8000 if song Y is in it — no APU I/O (the "not loaded" path leaves the stack unbalanced; the game never takes it)
$13 8146 play song Y: if the current set's C list lacks Y, find the set that has it (88:8207) and upload it; then song < $30: driver cmd Y; song ≥ $30: port3 := Y−$30, driver cmd $30
$14 8173 SFX Y (low byte only): 88:85A5 port1 toggle|id — plays over music, no upload
$15 8180 port3 := Y, driver cmd $3B — fade/stop: $FF (80:827D, scene changes), $FE (84:F274 results, 83:B68F, 83:B756), $FD (power cast/end 80:B60B/80:B63B, 83:8058, 84:F2FE)
$16 8195 port3 := Y, driver cmd $3A — Y=4 at power end when the power had no own music (80:B5FF), Y=1 at round end if a status power was running (81:A752) [I: "restore volume/tempo"]
$17 / $18 81AA / 81BF port3 := Y, driver cmd $39 / $38 (not seen)
$19 81D4 sound mode Y (boot Y=0; from options 7F:2111 at 80:8F1A) [I: stereo/mono]
$1A — sent with Y=0 at 80:814B / 80:81F9 (end of the main loop / ending) [I]
$0F / $10–$12 8125 / 8134 driver cmd $37 / re-init + upload (not seen)

3. Songs

Song ids are the C-list entries. The sound test (80:91D1) lists 19: 21 02 1C 18 0F 15 00 13 17 1A 23 22 20 25 1B 27 1F 04 2B.

song use trigger [C unless noted]
$24 BPS logo jingle 80:8098 boot
$20 title 83:ECF5
$11 character select 84:EC64, 83:832D, 84:8070
$17 Battle World map 84:8119, 83:8189, 83:8234
$25 / $27 Dragon / Queen intro 84:813F / 84:832B
stage per opponent id (80:B6EB): Halloween $21, Mirurun $02, Shaman $1C, Aladdin $18, Princess $0F, Bit $15, Ninja $00, Wolfman $13, Dragon $1B, Queen $1F 80:B6A5 → 80:B781
stage variant 80:B777: $21 $03 $1C $18 $10 $16 $01 $14 $1B $1F (restored after powers / new round via 80:B731) 80:B731
power music 80:B67A[power id]: $2B = keep the stage music (Mirurun, Pentris, Fly, Seesaw, Cut, Moses, Stardust, Yose, Press, Sol, Mirror, Heavy, HyperFly); $09 (Dainashi, Noroi, Doton, Shield, Doubles); $0C (Reverse, Mahi, Kaiten, Nu, Remocon, Hanten, Nakayoshi, Roulette); $2A (Mayakashi, Select, Dark); own sets: Copy/Fax $0E, Medusa $37, Bomb $0D, Vampire $2C, Clear $39, Mambo $0B, Gang $34, Suritote $05, Inori $36, Fever $33, Change $08 cast: 80:B61A (fade $FD unless $2B) + 80:B644 (play) ~100 frames later
$23 match won (1P vs COM if P1 won; always in 2P) 84:F330 / 84:F35C
$1A match lost (1P, COM won) — continue screen 84:F346
$22 game over (continue declined) 84:F2E2
$04 ending 83:B77A

$09, $0C, $2A, $1D, $2E, $30 are in every battle set (shared). The "own set" power songs ($05 $08 $0D $0E $36 $37 $39 …) are silent music tracks: in-game only the power's SFX voices sound (Mesen ENVX during Copy: 1–2 voices) [C]; they exist to load that power's SFX samples. $2B is a silent placeholder. Unplaced ids: $06 $0A $12 $19 $1D $1E $26 $28 $29 $2D $2E $2F $30 $31 $35 (menus/jingles; renders in re/runs/snd/songs/).

4. Sound effects (cmd $14) and MatchEvent mapping

Sound-test SFX list (80:91E4, ids between the $2B separators): 00 01 03 15 14 04 05 07 08 16 10 0A 11 0E 0F 12 13.

MatchEvent (src/core/match.hpp) original sound source [C]
Spawn (value=1, crystal piece) SFX $11 — the original plays it when the crystal piece is generated into the queue (3 previews earlier), 81:D3CD
Rotate (successful) SFX $01 (A 81:CD00, B 81:CD3A)
Shift none (no SFX in 7000 frames of random play)
Lock (no lines) SFX $03 (81:AEA9 P1 / 81:B604 P2; also index 0 of the clear table)
Clear (lines n) SFX by 81:B029 (P1) / 81:B784 (P2): 1 → $15, 2 → $14, 3 → $04, 4 → $05; ≥4 lines also 81:AD24 SFX $05 + big-clear effect 81:92D9
Garbage (rows) SFX $07 once per inserted row (81:A9C7, 10 frames apart)
Cast SFX $0E (81:ACC0 / 81:B41B), skipped for Mirror (1D) (81:ACB7); music: fade $FD unless power song is $2B, then power song (table above)
(cast refused: Up with nothing affordable) SFX $0F (81:AC6B / 81:B3C6) — no MatchEvent yet
Reflect not observed [I: Mirror is silent at cast; reflection sound unknown]
PowerEnd power song was $2B: cmd $16 Y=4 (80:B5FF); else cmd $15 Y=$FD (80:B60B) then the stage (variant) song via 80:B731
power effects power-specific SFX from the effect code, e.g. Pentris $07,$15,$15,$05 (81:C387, 81:C2B1, 81:C2EA); Clear $3B,$2E (81:89A5, 81:8848); Change $34,$3A (86:BB41, 86:BB81); Copy $1C,$1D (86:BE70, 86:BF3D, 86:C075); Vampire $21 (81:FB47); Nu $39 (82:8F0D); Mirurun $04 (81:BB98)
KO / RoundEnd SFX $0A (81:FC04, round-win mark); cmd $06 Y=0 (danger off, 81:A770); next round SFX $10 at READY/GO (81:CA72)
MatchEnd cmd $15 Y=$FE (84:F274), then song $23 (win) / $1A (1P loss)
danger (any board's top 4 rows occupied, checked at spawn) cmd $06 Y=$20 on / Y=0 off (81:A869)
menus cursor SFX $00 (83:869B …, 84:8D84 …), confirm $08 (83:EFA3, 83:8F9E, 84:91D6, 84:FC20), character chosen $16 (83:9003, 83:90F4)

libgme alone cannot work: the driver only plays after processing port commands (it relocates itself after each set load, and every song start / SFX / fade is a live port write), and gme has no port API. Use blargg's snes_spc 0.9.0 (LGPL-2.1, same SPC core as gme), which has SNES_SPC::read_port/write_port.

  1. Vendor snes_spc/: SNES_SPC.{cpp,h}, SNES_SPC_misc.cpp, SNES_SPC_state.cpp, SPC_CPU.h, SPC_DSP.{cpp,h}, SPC_Filter.{cpp,h}, blargg_common.h, blargg_config.h, blargg_endian.h, blargg_source.h (build it as its own small library target; LGPL: keep it separately linkable).
  2. Op generator — produce the same op list as snd_build_spc.py from the ROM. Two options:
    • (preferred, exact) port snd_cpu.py (~600 lines) to C++ and run the ROM's bank-$88 code with the same 10 hooks (84B9, 8301, 831A, 827A, 82C3, 8570, 8561, 8589, 85A5, 8A38) + $2140–$2143 write capture;
    • or transliterate 88:8146/8207/8334 and the set builders 85B8…8A38 (~1 KB of 65816), like rom_ai.cpp. Validate either against snd_build_spc.py --ops output for every song (dev tool, like ai_validate).
  3. Host — re/tools/snd_play.cpp is the reference: build the post-IPL SPC state (driver block at $0800, PC=$0812, SP=$EF, $F1=0, DSP FLG=$E0), load_spc it (no IPL ROM image needed), then run the ops with the handshakes shown there (time advances 24 SPC clocks per port access; end_frame every ~200k clocks). At runtime: boot ops once; for each music change run the cmd $13 op list synchronously (it's ~50 KB of words — milliseconds in emulation; original took ~25 frames), then keep playing with play() from the SDL audio stream callback; SFX = cmd1 (port1 toggle|id) and fades = cmd0 with port3, applied on the audio thread at the current SPC time. Output 32 kHz stereo s16 → SDL3 audio stream (it resamples).
  4. Event wiring = the MatchEvent table above; songs per scene = section 3.

Open questions

  • Meaning of driver cmds $37/$38/$39 and dispatcher cmds $1A, $16 Y=1/4 precisely, $19 (stereo/mono?).
  • Which scene uses songs $06 $0A $12 $19 $1D $1E $26 $28 $29 $2D $2E $2F $30 $31 $35 (round win/lose jingles, "READY", continue countdown, options…): play the renders, or log calls through the remaining scenes (options, ending, boss intros, continue).
  • Mirror's reflection sound; SFX ids $12/$13 (sound test) and $0A's exact moment.
  • Instrument/sample table formats (tables 1/4/5) — only needed if the C++ side reimplements the builder instead of running it.