A side by side comparison of Eugene Jarvis and Sam Dicker's 6808 assembly code for the Defender sound ROM (Williams, 1980), beside the JavaScript in GWAVE. GWAVE does not emulate the 6808. Each ROM routine is written as a generator that writes the same DAC values at the same CPU cycles.
VSNDRM1.SRC is loaded from GitHub into your browser at startup.
Loading VSNDRM1.SRC from GitHub…
GWAVE source: snapshot of GWAVE @ 20261030 7d41b2b
SOUND
The ROM makes sound only by storing bytes at SOUND, the PIA port that drives the DAC. GWAVE
keeps that one effect and replaces the CPU with arithmetic: it counts time in 6800 cycles and writes each
byte at the cycle the ROM would have.
The 6808's clock is the 3.579545 MHz crystal divided by 4 on the chip: 894,886 cycles a second.
CPU_CLOCK is that rate. FRAME_CYCLES is one 60 Hz game frame, used only to space
out GWAVE's game-sound sequences.
The 6808 runs from a 3.579545 MHz crystal, divided by 4 on the chip.
SOUND is port A of the PIA at $0400, wired to the MC1408 DAC. Every STAA SOUND,
STAB SOUND and COM SOUND in the ROM is a this.write() in GWAVE. Both
are marked red throughout this page.
Each routine is a generator. It yields the number of cycles until its next event and
resume() adds them to this.time. The cycle counts were worked out from the source;
no instruction is decoded at run time.
The 6800 runs instruction after instruction. Between two writes to SOUND, only the number of cycles matters for the sound.
byte() and word() keep the 8- and 16-bit wraparound of the registers.
timesLooped() and timesLoopedWord() turn a loaded count of 0 into 256 or 65,536
passes.
8-bit counters wrap: a DECA/BNE loop that starts at 0 runs 256 times, and a DEX loop from 0 runs 65,536 times.
SETUP · vectors
At power-up the ROM sets up the PIA, seeds the random generator, clears its flags and waits for an interrupt.
SETUP masks interrupts, puts the stack at $7F and programs the PIA: port A (the DAC) as outputs, port B (the command) as inputs, and an interrupt on a rising CB1 edge. The first byte at $F800 is the checksum the diagnostic compares against.
GWAVE has no PIA. Commands reach the board through send().
A still holds $3C from the control-register write, so STAA HI seeds the random generator with
$3C. That is why GWAVE starts with random = { high: 0x3c, low: 0 }. The five flags start at
zero in the constructor and in reset().
BRA * loops until an interrupt. idle() holds the DAC level and yields 1,024 cycles
at a time.
An interrupt abandons whatever the CPU was doing: the IRQ handler resets the stack and never returns.
startInterrupt() and nmi() do the same by replacing the running generator.
IRQ · ADDX · JMPTBL
A command from the game raises an interrupt. The handler decodes the 5-bit number and dispatches: 1–13 to GWAVE, 14–28 through a jump table, 29–31 to VARI. When the sound ends it falls through to the background sounds or idles.
19 cycles pass before the command is read: the interrupt entry (12), LDS (3) and
LDAA SOUND+2 (4). A command sent in that window only replaces the latch (interruptMask
"masked"). During CLI's 2 cycles a new command is held as pending and restarts the
handler. The game sends the number inverted and COMA turns it back, so GWAVE's
send() takes the plain number and keeps 5 bits.
A set organ flag means the command belongs to an organ sequence. Negative ($FF, set by ORGANT) means the number picks a tune. Positive (set by ORGANN) means it is one of the three numbers that give the oscillator mask and the note.
Any command other than 14 resets the spinner step, and any other than 18 clears the bonus flag. Repeating 14 or 18 therefore continues the sound instead of restarting it.
The speech check (LDAB TALK, CMPB #$7E) costs 10 cycles and never finds the
JMP opcode, because Defender has no speech ROM. Command 0 goes straight to IRQ3.
Commands 1–13 load a GWAVE vector and play it. GWAVE adds two options for its own front panel: a loop (the turbine) and a starting frequency offset. A ROM command always starts at offset 0.
Commands 14–28 index JMPTBL. The yield is the instruction sum: CMPA and BHI 6, SUBA 2, ASLA 2, LDX 3, BSR 8,
ADDX 25 or 31, LDX 0,X 6, JSR 0,X 8. X now holds the routine's own address, kept
in this.index.
Commands 29–31 load VARI vector 0–2 (SAW, FOSHIT, QUASAR) and play it.
After every sound: with a background flag set, clear the bonus flag and restart BG1 or BG2; otherwise
BEQ * idles. BG1 runs with whatever X holds, so its length is this.index.
ADDX adds A to X through memory. It takes 25 cycles, or 31 when the low byte carries and
INC XPLAY runs. indexingCycles() gives that count for a table address and offset.
GWAVE keeps the routine addresses only for a side effect: BG1 and THRUST never load X, so they use their own address as the sample count ($F913 = 63,763 for BG1, $F91C = 63,772 for THRUST).
GWLD · WVTRAN · TRANS · WVDECA · SVTAB · GWVTAB · GFRTAB
Each GWAVE sound is a 7-byte vector: a waveform, a pattern of periods to play it at, and settings for repeats, echoes, decay and sweep. GWLD copies the waveform into RAM, decays it, and sets up the pattern.
The vectors in command order. decodeGWaveVector() splits the nibbles: echoes and cycles, echo
decay and wave number, then pre-decay, sweep step, sweep count, pattern length and pattern offset.
GWLD multiplies the vector number by 7, finds the vector with ADDX and unpacks the first two bytes into RAM.
GWAVE copies the whole decoded patch into the voice at once, then yields the 72 cycles up to
STAA TEMPA plus ADDX.
The waveform's address is found by skipping waves one at a time, each length byte plus one.
DEC TEMPA is 6 cycles; each skipped wave costs 49 (BMI 4, LDAA 5, INCA 2, JSR 9, ADDX 25, BRA
4).
WVTRAN copies the waveform into GWTAB in RAM through TRANS: 43 cycles a byte in the TRANS1 loop, 57 around
it. transferWave() copies the samples and returns that count.
The pre-decay is applied once, right after the copy.
The remaining four bytes: sweep step, sweep count, pattern length and pattern offset, which give the
pattern's start and end addresses. FOFSET is cleared. GWAVE set these fields at the top of
loadGWave() and counts the 128 cycles here.
Each wave byte loses one sixteenth of the original ROM sample per unit of decay. The step comes from the ROM
copy, so every decay subtracts the same amount. X ends at the end of the wave, recorded in
this.index.
Seven waveforms. The first byte of each is its length, which GWAVE takes from samples.length.
The period patterns, one table with offsets into it. GWAVE keeps the table as bytes and names each pattern with its offset and length. The zeros marked "MATCH THE PROMS" are kept.
GWAVE · GPLAY · GOUT · GEND
The player steps through the period pattern. For each period it plays the wave in RAM a number of times, waiting the period between samples. Each echo replays the pattern with the wave decayed further.
GECNT counts the echoes. Each echo starts again at the first period, GWFRQ.
The next period is the pattern byte plus FOFSET. At FRQEND the echo is over. GCCNT is how many times the wave plays at this period.
The inner loop. Wait GPER × 6 cycles (DECA 2, BNE 4), output one wave byte, move to the next. The 13 is LDAA
GPER 3, LDAA ,X 5 and STAA SOUND 5; the 12 between samples is INX, CPX and BNE.
After a full wave, DECB counts the repeats. Before the next one, INX/DEX pairs and two NOPs add 36 cycles ("SYNC 36"), which with BRA and LDX make the 43-cycle gap.
At the end of the pattern the wave is decayed by GECDEC and the pattern plays again until the echoes run out.
A laser-ball bonus stops here after each pass; the next command 18 takes the next sweep step.
GEND50 · GEND60 · GEND61 · GW0–GW3
After the echoes, a sound with a sweep step shifts every period by the step and plays again, for as many steps as GDCNT allows. Periods that would overflow are cut from the pattern.
GDFINC is the step and GDCNT the number of steps. Each step adds GDFINC to FOFSET.
GEND61 finds the run of pattern bytes that stay valid after adding FOFSET. Rising: the sum must not carry. Falling (GDFINC negative): the sum must carry and must not be zero. The run becomes the new GWFRQ to FRQEND. If none is valid, the sound ends.
With a new range, the wave is copied and pre-decayed again if the echoes decay it, and
JMP GWAVE plays the sound again.
BG1 · BG2INC · BG2 · BGEND
Two sounds keep playing after a command finishes: BG1, a quiet filtered noise, and BG2, the turbine, a GWAVE sound that sweeps forever. Commands 15, 16 and 19 switch them.
BG1 sets its flag and runs filtered noise with FMAX 1, no distortion and no decay. X still holds BG1's own address, which becomes the sample count.
BG2INC stops BG1 and raises the turbine level BG2FLG from 1 to 29, then back to 1.
BG2 loads the TRBV vector and sets FOFSET to the complement of BG2FLG × 4, so a higher level gives shorter periods and a higher pitch. It enters the sweep code at GEND60, and BG2LP counts GDCNT up so the sweep never runs out.
BGEND clears both flags. The sound playing now ends at the next command, after which the board idles.
BON2
Command 18 plays the BONV sweep one step per command.
The first 18 sets B2FLG and plays BONV. While the flag stays set, each further 18 enters GEND50 to take one more sweep step, which plays once and stops at the B2FLG test.
VARILD · VARI · VVECT
VARI plays a square wave whose low and high halves have separate lengths. Both lengths change step by step, and a 16-bit timer decides how long each step lasts.
Nine-byte vectors: LOPER, HIPER, LODT, HIDT, HIEN, SWPDT (two bytes), LOMOD and VAMP.
VARILD multiplies the number by 9 and copies the vector to RAM through TRANS.
loadVari() decodes the patch and returns 435 cycles plus ADDX.
Output the amplitude VAMP, then load the two half-period counters.
Complement SOUND, count LOCNT; complement, count HICNT. X counts SWPDT down through both halves at 14 cycles a count (DEX 4, BEQ 4, DECA 2, BNE 4) and ends the step wherever it reaches zero, even in the middle of a half.
VSWEEP leaves the output on the level with bit 7 set: if SOUND is below $80 it is complemented, which costs 2 more cycles.
Both half-periods change by LODT and HIDT until HICNT equals HIEN.
LOMOD changes LOPER and the whole sweep repeats, until LOPER wraps to 0. With LOMOD 0 the sound ends after one sweep.
SP1
Command 14 plays the CABSHK VARI sound. Each repeat of the command raises a step that shortens the low half, so the pitch climbs while the game keeps sending 14.
Load CABSHK, VARI vector 3.
SP1FLG counts 1 to 31 and wraps. The interrupt handler clears it when any other command arrives.
LOPER comes from 32 minus the step: 14 for each count above 20 and 5 for each count from 20 down.
spinnerLowPeriod() is the same sum; the yield is the cost of the two loops.
SP1LP plays VARI again and again until the next command.
HI · LO
The noise routines share a 16-bit shift register in HI and LO. Each routine has its own copy of the shift code; GWAVE has one function.
The new top bit is bit 3 XOR bit 0 of LO. HI and LO shift right together, and the bit that falls out of LO
is the random bit (the carry). The function yields before and after the 6-cycle ROR LO, so HI
and LO change at their own cycles.
FNOISE copies the current output level into B (TAB) and taps its bit 3 instead of LO's. That is
the source argument: shiftRandom(cycles, level).
LITE · APPEAR · LITEN
Random clicks: each random 1 flips the output, then a delay. After a number of bits the delay changes, which moves the pitch of the crackle.
The two entries only set parameters, which GWAVE stores as patches. LITE: period 1, step +1, 3 bits per period. APPEAR: period $C0, step −2, 16 bits per period.
Store the period, put the output at $FF, set the bit count.
For each bit: shift the generator; on a 1, complement SOUND. Then wait LFREQ × 6 cycles.
LFREQ moves by DFREQ until it reaches 0. LITE's period grows from 1 to 255, so the crackle falls in pitch.
TURBO · NOISE
Random bits sent straight to the DAC at full or zero level, with an amplitude that decays.
TURBO, the laser: amplitude $FF, decay 1, 32 bits per step, period 1, period growing.
Store decay, period, amplitude and bit count.
Shift the generator. Output 0 for a 0 bit and NAMP for a 1 bit.
Wait NFRQ1 × 8 cycles (DEX 4, BNE 4), then the next bit.
After CYCNT bits the amplitude drops by DECAY; the sound ends when it reaches 0. With NFFLG set the period grows by one each time, so the noise falls in pitch.
THRUST · CANNON · FNOISE
Instead of jumping to each random level, the output slides toward it at a set rate. The slope limits how fast the output can change, which acts as a low-pass filter on the noise.
THRUST: slope 3, no decay, and X (THRUST's own address) as the sample count. CANNON, the explosion: slope $FF, 1,000 samples per stretch, decay and distortion on. BG1 is the same routine with slope 1.
Store the parameters and clear FLO, the fraction of the slope. Each stretch starts from the current output level.
The new random LO is the target level. The slope is FMAX, or FMAX AND HI with distortion, which makes the slope random too.
Rising: write the level, add the slope (FHI.FLO as a 16-bit step), and stop on overflow or once past the target. X counts the samples of the stretch.
Falling: the same with subtraction.
Snap to the target level and pick the next one.
When the stretch's sample count runs out: with decay on, FMAX.FLO loses one eighth (three shifts, negate, add). The sound ends when FMAX is 0 and FLO is 7; otherwise the next stretch starts.
RADIO · RADSND
A 16-step wavetable read by a phase accumulator whose speed rises each time the phase wraps.
The 16-byte waveform.
The frequency starts at 100. RADIO never sets the phase itself. GWAVE starts its low byte, B, at $FF
(RADIO_PHASE_LOW_AT_ENTRY); the last load into B before RADIO is the interrupt handler's
LDAB TALK. The high byte, TEMPA, keeps what the previous sound left, as
this.temp.a does.
Add the frequency to the 16-bit phase TEMPA:B. When the phase carries out, the frequency goes up by one;
when the frequency wraps to 0 the sound ends. BRA *+2 evens out the two paths.
The low 4 bits of TEMPA, the phase's high byte, pick the waveform byte to output.
HYPER
A square wave whose duty cycle steps through 128 phases.
Output 0 and start at phase 0.
A time counter runs from 0 to 127 with a delay of 18 counts of DECB/BNE between steps. When it equals the phase, the output flips.
At the end of each cycle the output flips again and the phase advances. The sound ends after phase 127.
SCREAM · STABLE
Four echoes, each a frequency and a timer. Each sample adds 128, 64, 32 and 16 for the echoes whose timers are in their upper half. The frequencies fall, and each echo starts the next one partway down.
Clear the table and start the first echo at frequency $40. ECHOS is 4 in the ROM; GWAVE lets its front panel choose 1 to 8.
For each echo: add its frequency to its timer; if the timer's bit 7 is set, add the echo's amplitude. Each echo has half the amplitude of the one before.
Output the sum. TEMPB counts 256 samples before the frequencies change.
Every non-zero frequency drops by 1. When one reaches $37, the next echo starts at $41. The scream ends when all frequencies are 0.
Return, with X at the end of the table.
ORGANT · ORGANN · ORGANL · ORGAN · ORGTAB · NOTTAB
Eight square waves an octave apart, added together. A mask chooses which octaves sound, and a delay built as code in RAM sets the pitch. Command 27 followed by a tune number plays a tune. Command 28 followed by three more commands, the two halves of the oscillator mask and a note number, plays a single note.
The assembler computes each duration from a note time, a tempo and a length. For example
TD/PHANC/2*1 is (49096 shifted right once) ÷ 3 ÷ 2 = 4091. GWAVE stores the results. The
Phantom's last note is 20,618 ($508A), as in the source; the released Red Label ROM has $508B.
The delay for each note of the scale.
ORGANT only sets the flag negative. The next command is the tune number.
Find the tune: each entry starts with its length, and a length of 0 ends the table. An invalid number returns through IRQ3.
For each 4-byte note: the oscillator mask, the delay and the duration go to ORGANL, which builds the delay and plays the note.
ORGANN sets the flag to 3 for the three commands that follow.
Each of the next two commands, minus 1, is half of the oscillator mask: the mask shifts left 4 bits and the
value is added. Then BRA * waits for the next command.
The third command picks the note from NOTTAB (the first note for numbers past the table). The note plays with duration $FFFF, again and again, until the next command.
ORGANL writes code into RAM at RDELAY: one NOP (2 cycles) for every 2 counts of delay, a
CMPA 0 (3 cycles) for an odd delay, then JMP ORGAN1. The pitch is set by that
code's run time, which organDelayCycles() computes; organLoadCycles() is the time
ORGANL takes to write it.
One sample per pass. TEMPB counts up, so its bit n is a square wave 2ⁿ⁺¹ samples long. The output is 16 ×
the number of set bits in TEMPB AND OSCIL; popCount() replaces the seven LSRB/ADCA pairs. A
pass is 72 cycles plus the delay. LDAB TEMPB is written as bytes ($F6 and an address), the
extended form, one cycle longer than the direct form the assembler would choose.
NMI
The diagnostic switch on the board pulls NMI. The ROM checks itself and plays a sound in a loop.
Sum the ROM from $FFFF down to $F801 and compare with the checksum byte at $F800: 16 cycles a byte (ADCB 5, DEX 4, CPX 3, BNE 4) for 2,047 bytes. A bad ROM stops at WAI; GWAVE's ROM is always good.
Play FOSHIT (VARI vector 1), check for speech, and start again. SEI keeps commands out until a reset.
board hardware
The ROM ends at the DAC. GWAVE also models what happens to the voltage: it averages the DAC level over each audio sample and applies the board's output filter.
integrate() returns the area under that staircase for one audio sample: each DAC value times
the cycles it was held. Scheduled commands are applied at their exact cycle along the way.
The DAC holds each value until the next write. Its output is a staircase that changes at exact CPU cycles.
DacRenderer divides by the cycles per sample (894,886 ÷ the sample rate) to get the average
level, then centres it on 128.
SOUND ($0400) → PIA port A → MC1408 DAC → TDA2002 amplifier → C14 (800 µF) → 8 Ω speaker
The runtime applies that high-pass as a one-pole filter after rendering.
C14 and the speaker form a high-pass filter at 1 / (2π × 8 Ω × 800 µF), about 25 Hz.
An AudioWorklet runs the board on the audio thread and posts snapshots to the page for the meters and displays.
The board's sound goes to the cabinet speaker.
With the CABINET switch on, GWAVE adds a 110 Hz high-pass and a 4.8 kHz low-pass after the board.
The cabinet's speaker and enclosure shape the sound further.