Goal: trigger a sound from a real gameplay event using Lesson 13’s manual-DSP technique, then get an honest picture of what it takes to add real music — which this tutorial deliberately doesn’t implement from scratch.
Lesson 13’s tone started once, at boot, and never stopped. A sound effect needs to fire on demand — say, when Lesson 12’s check_collision reports a hit. The trick: re-triggering a voice that’s already playing requires a key-off, then key-on pair, not just key-on again — writing KON while a voice is already active doesn’t restart its envelope, it’s ignored.
; Call after check_collision returns with carry set.
play_hit_sfx:
ldx #$015C ; KOFF: key off voice 0
jsr write_dsp
ldx #$014C ; KON: key on voice 0 (restarts the envelope cleanly)
jsr write_dsp
rtsWire it into the collision check from Lesson 12:
jsr check_collision
bcc @no_hit
jsr play_hit_sfx
@no_hit:Because both check_collision and write_dsp/spc_upload_byte are plain subroutines, calling one from the other’s success path is nothing new — it’s the same JSR/RTS discipline from every earlier lesson, just with a sound chip on the other end instead of a PPU register.
The DSP has 8 voices; Lesson 13 only used voice 0. A second sound effect — say, a short blip on a button press — can run on voice 1 entirely independently, using the same write_dsp calls with voice 1’s register offsets (add $10 to each of voice 0’s register numbers: VOLL becomes $10, PITCHL becomes $12, and so on). Wire this to the A button from Lesson 11’s exercises, and you have two independently-triggerable sounds sharing the same sample data, distinguished only by which voice plays them.
Everything in Lessons 13-14 is deliberately minimal — enough to understand the communication protocol and make a sound. Real background music needs a sequencer running on the SPC700 itself: something that reads a compact song format out of ARAM and continuously updates DSP registers in time, entirely independent of the 65816 (which should be free to run gameplay code, not spend every frame bit-banging DSP writes). Writing that sequencer from scratch is a serious project on its own — arguably comparable in scope to everything covered in Lessons 1-13 combined — which is why essentially every SNES homebrew game, however small, builds on an existing audio driver rather than writing one. Three actively-used, ca65-compatible options, roughly in order of how actively maintained and documented they are as of this writing:
.it (Impulse Tracker) module files you can author in the free tool OpenMPT.All three solve the same underlying problem this lesson introduced by hand — getting a compact song representation into ARAM and having the SPC700 play it back continuously — with a real sequencer instead of a fixed one-shot tone. Picking one and working through its own integration guide is the natural next step after this tutorial, and is intentionally left as that: a next step, not part of the core curriculum here.
One catch worth flagging: the +$10-per-voice rule only applies to the per-voice registers (VOLL/VOLR/PITCHL/PITCHH/SRCN/ADSR1/ADSR2, each living at voice_number*$10 + offset). KON ($4C) and KOFF ($5C) are different — they’re shared, one-bit-per-voice registers, always at the same address, where voice N is bit N. Voice 1 is bit 1, not address $1C (that’s actually MVOLR, a completely different register). One-time voice 1 setup, alongside voice 0’s in Lesson 13:
ldx #$107F ; VOLL (voice 1) = max
jsr write_dsp
ldx #$117F ; VOLR (voice 1) = max
jsr write_dsp
ldx #$1200 ; PITCHL (voice 1) = 0
jsr write_dsp
ldx #$1320 ; PITCHH (voice 1) = $20 -> pitch $2000, an octave up
jsr write_dsp
ldx #$1400 ; SRCN (voice 1) = 0, same uploaded sample
jsr write_dsp
ldx #$15C3 ; ADSR1 (voice 1), same envelope as voice 0
jsr write_dsp
ldx #$162F ; ADSR2 (voice 1)
jsr write_dsp
Then the trigger, using bit 1 of KON/KOFF instead of voice 0’s bit 0:
play_blip_sfx:
ldx #$025C ; KOFF: key off voice 1 only (bit 1)
jsr write_dsp
ldx #$024C ; KON: key on voice 1 only (bit 1)
jsr write_dsp
rts
; --- inside nmi_handler ---
lda $4218
and #%10000000 ; A button
beq @no_blip
jsr play_blip_sfx
@no_blip:
Because voice 0’s collision sound only ever touches bit 0 of KON/KOFF and voice 1’s blip only ever touches bit 1, triggering both close together doesn’t interfere — you should hear them overlap cleanly.
Neither a plain .incbin nor a hand-written linker segment: Terrific Audio Driver compiles your MML songs and sound effects with its own tad-compiler tool, and the resulting audio data is loaded through a LoadAudioData callback you provide — the driver’s ca65 API generates that callback for you rather than expecting you to wire up the binary data yourself. So the integration point is a callback function, not a segment or an included file, which is a genuinely different shape than every VRAM/CGRAM/OAM loading pattern used elsewhere in this tutorial.
ADSR1/ADSR2 values ($C3/$2F) for voice 0. The SPC700 reference documents the ADSR envelope bit layout — without necessarily working out exact timings, identify which of the two registers controls “attack” (how quickly the sound reaches full volume when key-on fires) versus “release.”
ADSR1 is bit 7 = ADSR-enable, bits 6-4 = Decay Rate, bits 3-0 = Attack Rate — attack lives entirely in ADSR1’s low nibble. ADSR2 is bits 7-5 = Sustain Level, bits 4-0 = Sustain Rate, and it’s that Sustain Rate field that governs how fast the envelope falls toward silence after key-off — the closest thing the DSP has to a “release” control, even though the register reference calls it “sustain rate” rather than “release” by name. So: attack is in ADSR1 (low 4 bits), release-like behavior is in ADSR2 (low 5 bits).
Home | Previous: Lesson 13 — The SPC700 & audio basics Next: Lesson 15 — Code organization & optimization