The Machine Speaks Back
Unit 8 taught the machine to hear you. This one teaches it to answer. The SID is the C64's three-voice synthesiser — set a voice's pitch, its envelope, and the volume, open the gate, and the chip holds the note on its own while your code does nothing.
Back in Unit 8 the machine learned to hear you — reading the joystick through a register. This unit is the other half: the machine answering back. And on the C64, answering means the SID.
The SID (Sound Interface Device) is a proper synthesiser on a chip — three independent voices, each with its own pitch, waveform, and volume envelope. It's why C64 music still sounds like nothing else. You don't bang out the waveform yourself the way a lesser machine would; you set the chip's registers — the knobs on the synth — and it generates the sound on its own, holding the note while your code moves on.
A voice needs four things set: its pitch (a frequency), its envelope (how the note swells and fades — attack, decay, sustain, release), the master volume, and a waveform with the gate opened to start it. This unit plays one steady note on voice 1.
What you'll hear by the end
A steady, bright tone, held for as long as the machine runs. It's a sawtooth wave — the buzzy, reedy voice that carries a thousand C64 basslines. The waveform strip above is the real captured output.
Set the knobs, open the gate
; Meet the Machine - Unit 15: The Machine Speaks Back
; Assemble with: acme -f cbm -o sid.prg sid.asm
;
; The SID is the C64's sound chip - a three-voice synthesiser. You set a voice's
; pitch, its envelope, and the volume, then pick a waveform and open the "gate".
; The chip holds the note on its own while the CPU does nothing.
*= $0801
!byte $0c,$08,$0a,$00,$9e,$32,$30,$36,$31,$00,$00,$00 ; 10 SYS 2061
*= $080d
; a background colour, so the screen shows it's running
lda #$00
sta $d020 ; border black
lda #$06
sta $d021 ; background blue
; clear the start-up text away to a blank screen
ldx #0
clear lda #$20 ; space
sta $0400,x
sta $0500,x
sta $0600,x
sta $0700,x
inx
bne clear
; ----------------------------------------------- YOUR CODE START
; --- set up SID voice 1 ---
lda #$0f
sta $d418 ; master volume to maximum (0-15)
lda #$00
sta $d400 ; frequency, low byte
lda #$11
sta $d401 ; frequency, high byte -> the pitch
lda #$00
sta $d405 ; attack 0, decay 0
lda #$f0
sta $d406 ; sustain 15, release 0 -> the note holds at full
lda #$21
sta $d404 ; sawtooth waveform + gate ON -> the note starts
; ------------------------------------------------- YOUR CODE END
loop jmp loop ; the CPU idles; the SID plays on by itself
The sound is one block of stores to the SID's registers, which live from $D400:
lda #$0f
sta $d418 ; master volume to maximum (0-15)
lda #$00
sta $d400 ; frequency, low byte
lda #$11
sta $d401 ; frequency, high byte -> the pitch
lda #$00
sta $d405 ; attack 0, decay 0
lda #$f0
sta $d406 ; sustain 15, release 0 -> the note holds at full
lda #$21
sta $d404 ; sawtooth waveform + gate ON -> the note starts
Every line is a sta to a SID register — you're turning knobs on a synthesiser. $D418 is the master volume. $D400/$D401 set the pitch as a 16-bit frequency, low byte then high (the two-byte numbers from Unit 14). $D405/$D406 set the envelope: with sustain at maximum and release at zero, the note holds at full level for as long as it's gated.
The last store does two things at once. $D404 is voice 1's control register; $21 selects the sawtooth waveform (bit 5) and opens the gate (bit 0). Opening the gate is what starts the note — and because nothing ever closes it, the note rings on while the CPU sits in loop. You set the chip going and walked away; the SID does the rest.
Assemble and run
acme -f cbm -o sid.prg sid.asm
A steady sawtooth tone; a blank blue screen.
Try this: change the pitch
$D400/$D401 are the frequency. Raise the high byte — lda #$22 instead of #$11 — and the note jumps roughly an octave; lower it and the note drops. The number is the pitch; the SID turns it into a tone.
Try this: a different voice
Change $D404 from $21 to $11 (triangle + gate) for a softer, flute-like note, or $41 (pulse + gate) for a hollow square — though pulse also wants a width set in $D402/$D403. Same chip, same four steps; a different waveform bit gives a different timbre.
If it doesn't work
- Silence. Volume at
$D418is zero, or the gate never opened — the control write to$D404must have bit 0 set ($21, not$20). - A click, then nothing. The envelope released. With release in the low nibble of
$D406set above zero, the note fades after the gate; keep sustain high and release at zero for a held tone. - A tone that never stops even when you mean it to. That's the gate staying open — clearing bit 0 of
$D404(write$20) closes the gate and starts the release. Sound is something you turn off as well as on.
What you've learnt
The SID is the C64's three-voice synthesiser. You set a voice's pitch ($D400/$D401), its envelope ($D405/$D406), the master volume ($D418), then write a waveform plus the gate to $D404 to start the note. The chip holds it on its own while the CPU is free. You turn the knobs; the synth plays.
What's next
That's the last of what the machine can do — you've now driven its screen, its colour, its input, and its sound. One unit remains, and it isn't about a new instruction. Next — The Machine Trusts You — what it means to program a machine that does exactly what you wrote, even when you're wrong.