The 8-Bit DACs: Four Channels, Six Sound Cards
An AY chip can whistle, buzz and hiss, and with a little help it can even play a decent bass. But ask it to say “Game over” in a human voice, or to play a real snare drum, and it shrugs. It only knows square waves, noise and envelopes. A DAC knows nothing at all, and that is exactly its strength: it plays whatever number you give it. Give it the right numbers fast enough, and out comes a voice, a drum kit or a whole song from a file. The Next has four of them, and it answers on the ports of six different Spectrum and Pentagon sound cards, so the software written for those cards plays without a single change. How does one machine pretend to be six cards at once?
Sampled sound is what lifts a game above the bleeps, and I would argue it is the cheapest way to make a Spectrum game sound expensive. A drum hit on a DAC over AY music makes a chip tune sound like a band. A spoken word at the start of a level, a sampled explosion or a scream of guitar is something no tone generator can fake. And for a whole family of music players, the ones that play tracker modules with recorded instruments, a DAC is the only way in.
Read The Big Picture: Four Sources, One Mixer first: it introduces the DACs and the difference between a generator, which plays on its own, and a level writer, which needs the CPU at every step. A DAC is the purest level writer there is. If samples and DACs are new to you, the background chapter How a Computer Makes Sound explains them, and I will build on it rather than repeat it.
The road ahead
We begin with what one DAC channel does, which takes about a minute, because it does almost nothing: it holds a number. Then the four channels meet the two sides of the mixer, and the switch that turns them on. Next comes the fun part, the port map: which port of which sound card reaches which channel, the bits that turn each card on and off, and the ports two cards would both like to own. After a quick look at the NextReg mirrors, we arrive at the question that decides whether a sample sounds right or wrong, and the one I would ask you to remember if you remember only one: who times the writes? A CPU loop, the CTC, the DMA and the Copper each do it differently, and only some of them keep their pitch when the CPU changes speed. We end with a drum hit and the DACs’ share of the mixer.
The numbers are for the Next’s 28 MHz master clock, with the CPU at 3.5 MHz unless a capture says
otherwise. Levels are a DAC channel’s own 8-bit value, $00 to $FF, or the units of the Next’s
mixer, where a side’s two channels count four times. The waveforms are captured from an emulated Next at
48,000 samples a second, from programs checked to do what the text says; each shows one channel’s level.
A number that holds
A DAC channel is an 8-bit register with a converter behind it. Your program writes a value, $00 to
$FF, and the channel turns it into a level. Then it does nothing at all until the next write. No counter, no
clock, no envelope: the level simply holds. Write a new number and the level jumps to it; write the same
number again and nothing changes. I call a DAC channel the parrot: it says exactly what you told it, and
keeps saying it until you teach it something new. It never has an idea of its own, and I mean that as praise.
So a DAC makes a waveform only if the program writes one step of it after another, on time. Here is a channel written once every millisecond, with eight values that go up, down and round again:
$DF once every 3,500 T-states, a millisecond at 3.5 MHz. Each value holds until the next write, so the level is a staircase. The ticks under the plot mark the writes.See the dashed line in the middle? That is $80, the rest level. A DAC’s values run from 0 upwards, and
a sound has to swing both ways, so silence is the middle of the range: a sample that goes up and down by the
same amount swings about $80. Every channel starts there after a reset. A sample that ends at $80 leaves the
channel silent; one that ends anywhere else leaves a small constant level, which you will not hear (a capacitor
after the converters removes any constant level, as the mixer chapter explains), but which takes some of the
room the other sources could use. My advice: treat $80 as home, and always bring the parrot back there.
Four channels, two sides
The Next has four channels, A, B, C and D. A and B belong to the left side, C and D to the right:
Each side adds its two channels into a 9-bit number, 0 to 510, and the mixer multiplies it by 4, so the DACs can give a side up to 2,040 units. Why two channels a side, when one would do? Because that is what it takes to copy every card: Soundrive drives four channels, two a side, Covox one a side, and the mono cards a single channel that you hear on both. Four channels cover them all, as you will see in a moment.
The DACs have a switch of their own: bit 3 of the Peripheral 3 Setting register, NextReg $08. It is 0
after power-on, so the DACs start off. While it is 0, every channel is held at $80, and every write to
them, through a port or a NextReg, is ignored. Set the bit, and the channels start at $80 and follow your
writes. Clear it again, and they go back to $80 at once: a quick way to silence all four. I like having a
single bit that shuts all four parrots up at once; a program that has lost track of its samples will thank you
for it.
Six sound cards on four channels
Back in the day, a Spectrum or Pentagon owner who wanted sampled sound plugged in a sound card built around one or more DACs, and every card picked its own ports. Software followed the cards, so a program written for one card writes to that card’s ports and nowhere else. The Next answers on all of them, a master of disguise wearing six costumes at once. Here is the whole map: each card, the bit that enables it, and the port that writes each channel:
$84 is 1; the NextRegs need no enable. A cell with the same port in two columns is a mono port: one write reaches both channels.A few things in the map deserve a closer look.
A port is its low byte. The DACs look only at the low 8 bits of the port address, so $00DF, $12DF and
$7FDF all reach Specdrum’s channels. That keeps out ($df),a working whatever is in A, which is exactly what
an out (n),a instruction puts in the high byte.
Mono ports write two channels. Pentagon’s $FB and Specdrum’s $DF write A and D, GS Covox’s $B3 writes
B and C. One channel on each side, both set to the same value: the sample plays in the middle, at the same
level as a stereo card playing it on both channels.
The enables. Each card’s ports answer only while its bit is set in NextReg $84, one byte of the four
Internal Port Decoding Enables registers, NextRegs $82–$85. Bits 1 to 7 of NextReg $84 belong to the
DAC cards, in the order of the map (bit 0 is the AY chips’ ports). After power-on every bit is 1, so every card
answers. A program that needs one of those ports for something else clears the bit, and the DAC lets go of it.
The ports two cards share. Some cards picked the same ports, and the Next sorts them out like this:
$0Fand$4Fbelong to both Soundrive mode 1 and Covox, and$5Fto both Soundrive mode 1 and Profi Covox. They always reach the same channel, so two cards enabled at once simply agree.$FBis the one real clash. Soundrive mode 2 uses it for channel D alone; Pentagon uses it for A and D at once. Soundrive mode 2 wins: Pentagon’s$FBanswers only while bit 2 is clear, and then a write to$FBreaches both A and D.$F1and$F9, two of Soundrive mode 2’s ports, end in the same two address bits as$FD, and the 128K’s paging ports$7FFD,$DFFDand$1FFDlook at only a few address bits. Without care, a sample written to$7FF1would also page your memory around. Now that would make for an exciting crash. So while Soundrive mode 2 is on, a write to$F1or$F9reaches the DAC only.$1Fis Soundrive mode 1’s channel A for writes and the Kempston joystick for reads, and$DFis Specdrum’s port for writes and, while Specdrum’s bit is set and the mouse’s ports are disabled, a second address of the joystick for reads. A write and a read never meet, so the DAC and the joystick share the ports in peace.
I find this list the most satisfying part of the DACs. Six cards that never planned to share a machine, and the Next keeps every one of them happy with a handful of careful rules.
Let’s hear the map at work. A Covox card has one channel per side: $0F writes B, on the left, and $4F writes
C, on the right. This loop writes a 16-step sine to the left and a 32-step one, an octave lower, to the right:
$0F plays on the left only, port $4F on the right only: 1,078 Hz and 539 Hz, from a step every 203 T-states.The NextReg mirrors
The ports are not the only way in. Three NextRegs write the DACs too: the DAC B Mirror (left) register,
NextReg $2C, writes channel B, the DAC A+D Mirror (mono) register, NextReg $2D, writes A and D, and
the DAC C Mirror (right) register, NextReg $2E, writes C. They are there for code that has no free port,
and for the one device that can write NextRegs but no port at all: the Copper, as you will see shortly.
Their writes need no NextReg $84 bit, but they do need the DACs to be on.
Reading the same three NextRegs gives you something else entirely: the samples arriving from the Raspberry Pi, which belong to the Pi chapter. I admit I find that a little odd at first sight, one number writing here and reading there, but it saves registers, and the two uses never get in each other’s way.
Who times the writes?
Here comes the part that decides whether a sample sounds right. A DAC plays a sample at the rate your writes
arrive, and only at that rate. Write the steps of a recording made at 16,000 samples a second 16,000 times a
second, and it sounds as it was recorded. Write them faster and the voice turns into a chipmunk; write them at
an uneven pace and the sound gets a rough, gritty edge. So the real question about a DAC is not how to write it
(any out will do) but who decides when. The Next gives you four answers:
Let me walk you through them with the same 16-step sine, played each way.
A CPU loop. The simplest way of all: a loop that writes a step, waits, and goes round again. This one writes a step every 218 T-states:
.org $8000
Start: di
nextreg $07,0 ; 3.5 MHz
nextreg $08,$58 ; the DACs on (bit 3)
ld hl,Table
Loop: ld a,(hl) ; 7
out ($df),a ; 11: Specdrum, channels A and D
ld a,l ; 4
inc a ; 4
and $0f ; 7: the next of 16 steps
ld l,a ; 4
ld b,13 ; 7
Dly: djnz Dly ; 13 * 12 + 8
jp Loop ; 10: 218 T-states a step
.org $9000
Table: .defb 128,177,218,245,255,245,218,177
.defb 128,79,38,11,1,11,38,79At 3.5 MHz that is 3,500,000 ÷ 218 = 16,055 steps a second, and 16,055 ÷ 16 = 1,003 Hz. The catch is that
the loop’s rate is the CPU’s rate. Switch the CPU to 7 MHz with the CPU Speed register, NextReg $07, and
the same loop plays twice as fast, an octave higher. Run it from contended memory at 3.5 MHz and it slows down
while the screen is drawn, as the beeper chapter showed. And while the loop plays, your program does nothing
else: the CPU is the sound card.
The CTC. The Next’s CTC has four channels that count the 28 MHz master clock, divided first by a prescaler of 16 or 256 and then by a time constant of 1 to 256. At every zero count it can interrupt the CPU, and the interrupt routine writes the next step. I think of the CTC as the metronome: it ticks at its own pace, and nobody at the piano can hurry it.
.org $8000
Start: di
nextreg $07,0
nextreg $08,$58
ld a,$be
ld i,a
im 2
nextreg $22,$04 ; ULA and line interrupts off
nextreg $c0,$a1 ; hardware IM 2, vectors from $A0
nextreg $c9,$ff ; clear the CTC status
ld bc,$183b
ld a,$85 ; control word: interrupt, timer,
; prescaler 16, time constant next
out (c),a
ld a,109
out (c),a
ei
Park: jr Park
Isr: push af
push hl
ld hl,(Ptr)
ld a,(hl)
out ($df),a
ld a,l
inc a
and $0f
ld l,a
ld (Ptr),hl
pop hl
pop af
ei
reti
Ptr: .defw Table
.org $9000
Table: .defb 128,177,218,245,255,245,218,177
.defb 128,79,38,11,1,11,38,79Channel 0’s interrupt comes through vector $A6, so the word at $BEA6 holds the address of Isr. Now the
rate is 28,000,000 ÷ 16 ÷ 109 = 16,055 interrupts a second, the same 1,003 Hz, and it stays 1,003 Hz at
every CPU speed, because the metronome never looks at the CPU’s clock. The CPU still writes every sample, but
only for the interrupt routine, about 150 T-states with the interrupt itself; between samples it is free to run
your game.
The DMA. The Next’s DMA can copy bytes from memory to a port by itself, and its prescaler paces them: one byte every 32 × P master clocks, so about 875,000 ÷ P bytes a second. Set it up once, point it at the sample and the DAC’s port, and it plays the whole thing while the CPU does something else. The DMA is the courier here: you hand it the parcel and the address once, and it delivers every byte on schedule without asking you again.
.org $8000
Start: di
nextreg $07,0
nextreg $08,$58
ld hl,DmaProg
ld b,16 ; the 16 bytes from DmaProg
Send: ld a,(hl)
out ($6b),a ; the zxnDMA's port
inc hl
djnz Send
Park: jr Park
DmaProg:
.defb $83 ; disable
.defb $7d,$00,$c0,16,0 ; WR0: A to B, $C000, 16 bytes
.defb $14 ; WR1: A is memory, incrementing
.defb $68,$21,55 ; WR2: B is I/O, fixed; P = 55
.defb $cd,$df,$00 ; WR4: burst mode, B = $00DF
.defb $a2 ; WR5: auto restart
.defb $cf,$87 ; load, enable
.org $c000
.defb 128,177,218,245,255,245,218,177
.defb 128,79,38,11,1,11,38,79Two settings matter here. Burst mode makes the DMA give the bus back to the CPU while it waits for the next byte; in continuous mode it would keep the bus, and with it the CPU, for the whole block. And auto restart starts the block again at its end, so these 16 bytes play for ever. With P = 55, the prescaler alone would give 875,000 ÷ 55 = 15,909 bytes a second, 994 Hz. You get a little less, 968 Hz at 3.5 MHz, because each byte also costs a few CPU clocks to take the bus back from the CPU. But the rate barely moves with the CPU’s speed: 964 Hz at 7 MHz.
The Copper. The Copper can write NextRegs, and the DAC A+D Mirror (mono) register, NextReg $2D,
writes channels A and D. So a Copper list that waits for each line and then moves the next sample into $2D
plays a sample with no CPU at all, at one sample a line: 15,625 a second at 48K timing, 15,351 at 128K timing.
Its limits are just as clear. The samples have to sit in the list itself, which holds 1,024 instructions, and
to play every frame the list starts again at each frame (the Copper’s mode 11), so its samples repeat with
the frame. Fine for a short looping sound; a long recording needs the CPU to keep rewriting the list.
Here are the three captured, each at two CPU speeds:
So which one should you use? Here is my advice. For a sample that plays alongside a game, use the DMA in burst mode: one setup, no CPU, and a rate that does not care how fast the CPU runs. For effects that have to be computed sample by sample, mixing two sounds, say, use the CTC and do the work in the interrupt routine. Keep the CPU loop for the moments when nothing else runs anyway, and for software that brings its own. And the Copper is a lovely trick for a short sound in a program whose Copper has a few instructions to spare. If you ask me, the courier is the one to reach for first; the others are for when it is busy.
A drum hit
Time for something that no generator can play. These are 2,048 bytes of noise that die away, a rough stand-in for a snare drum, played once by the DMA at prescaler 55:
$DF. Without auto restart, the DMA stops at the last byte, and the channel holds that byte.The block lasts 2,048 × 32 × 55 ÷ 28,000,000 = 128.7 ms by the prescaler alone, and a little longer with each
byte’s few extra clocks, about 131 ms. Have a look at the end. Without auto restart the DMA stops after the
last byte, and the channel does what every DAC channel does: it holds that byte. The parrot repeats the
last word it heard. A sample should therefore end at $80, or near it; one that ends anywhere else leaves its
last level behind. That is also why a drum over AY music costs so little: start the DMA, go back to the game,
and the drum takes care of itself. I find few things on the Next as rewarding for so little code.
The DACs in the mixer
Each side’s two channels add up to 0–510, and the mixer counts that four times, so the DACs take 0 to 2,040 units of the mixer’s 0–8,191 per side:
$80, they add 1,024 units, the middle of their range.At rest the DACs are not silent in the mixer’s eyes: two channels at $80 add 1,024 units a side, and that
is most of the 1,536 a silent Next sits at (the Pi adds the other 512). A full-scale sample on both channels of a
side swings from there down to 0 and up to 2,040, four times the beeper’s 512; through a mono port, one channel
a side, it swings about 1,020, twice the beeper. So the parrots fill a good part of the room even
while they say nothing, and I always count that in first. The mixer
chapter’s Headroom shows what happens when
the DACs, the AY chips and the beeper all shout at once.
What to take home
Four parrots and six disguises, then. A DAC channel is a number that holds. You write $00 to $FF, the level
jumps there and stays until the next write, and $80 is silence. The Next has four channels, A and B on the
left, C and D on the right; each side adds its two, 0 to 510, and the mixer counts them four times, up to
2,040 units. Bit 3 of NextReg $08 switches them on; it is off after power-on, and while it is off every
channel holds $80.
The Next answers on the ports of six cards: Soundrive in its two modes, Profi Covox, Covox, Pentagon and ATM, GS
Covox and Specdrum, each enabled by one bit, 1 to 7, of NextReg $84. Only a port’s low byte counts. Mono ports
write one channel on each side; $FB is Soundrive mode 2’s channel D when that mode is on and Pentagon’s A and D
when it is off; and NextRegs $2C, $2D and $2E write B, A and D, and C from code or the Copper.
What decides how a sample sounds is who times the writes. A CPU loop plays at the CPU’s speed and takes all of the CPU. The CTC interrupts at 28 MHz ÷ 16 or 256 ÷ its time constant, the DMA moves a byte every 32 × prescaler master clocks, a little under 875,000 ÷ P a second, and the Copper can write one sample a line. Those three keep their rate at any CPU speed, and the DMA in burst mode leaves the CPU almost all its time. Point the courier at a drum hit, let it go, and get back to your game.