The Big Picture: Four Sources, One Mixer
A 48K Spectrum had one bit of sound and a speaker the size of a coin. Play a modern game on a ZX Spectrum Next, and you hear chip music in nine voices, in stereo, with sampled drums and speech on top. And the same machine still loads a tape by listening to it. So where does each of those sounds come from, and how do they all end up in one pair of headphones?
I think sound is the most underestimated worker in a program. Music sets the mood and the pace of a game. An effect tells you that you jumped, fired or were hit before your eyes have found it on the screen. Speech and sampled sounds make a game feel larger than its 128K. On a Spectrum, sound had one more job, too: programs were saved to tape and loaded from it as sound. And a machine with several sound sources that run by themselves does all this without tying up the CPU, which is busy enough drawing the screen, thank you very much.
This is the map of the Next’s sound, the first of its sound chapters. The chapters that follow take each
source apart; here you see how they fit together, and I would rather you had the map before the
close-ups. If digital sound is new to you, read
How a Computer Makes Sound first: it explains
samples, the DAC, the filter that makes the result smooth, sound from a single bit and mixing, and I will
not explain them again. ZX Spectrum: How the Screen Is Drawn
explains port $FE, which the beeper shares with the border, and
The ZX Spectrum Next Core and the FPGA explains why
the Next’s hardware lives in an FPGA.
The road ahead
We start where the Spectrum started, with one bit, and see what the Next kept and what it added beside it: four kinds of source. Then the whole cast walks on stage in one figure, and you hear each source on its own, one by one. After that comes the idea I would ask you to keep if you keep only one: some sources make a sound by themselves, and others need the CPU at every step, and that difference decides what a sound costs your program. From there we follow the sound downstream, into the one mixer that adds everything on a left and a right side, and out through the jack, the speaker, HDMI and the Pi. Two clocks explain why the CPU’s speed changes the beeper’s pitch but not the AY’s, and a handful of registers turn each part on and off. We end with a walk past the chapters that follow, so you know where each source is taken apart.
The numbers in the sound chapters are for the Next’s 28 MHz master clock, with the CPU at 3.5 MHz unless a chapter says otherwise. Levels are given in the units of the Next’s mixer, whose sum runs from 0 to 8,191 per side. The waveforms are captured from the emulated Next at 48,000 samples a second, from programs checked to do what the text says, and keep the hardware’s proportions.
From one bit to four kinds of source
The ZX Spectrum 48K makes sound with one bit. Bit 4 of port $FE, called EAR, drives the speaker:
the program sets it, clears it, sets it again, and the speaker’s cone follows. Bit 3, MIC, drives
the tape output and reaches the same speaker more weakly. Every sound a 48K program makes, from a key
click to two-voice music, is the program switching those bits on and off at the right moments. The
speaker is driven in the program’s own time: while it plays, your program can do little else.
The Spectrum 128K added a sound chip, the AY-3-8910, with three voices that play by themselves once they are set up. The Next keeps both and adds more beside them:
That makes four kinds of source: the beeper, with its two bits EAR and MIC (MIC also carries the tape input), the AY chips, the DACs and the Pi. Everything that follows, here and in the chapters after this one, is about these four, the mixer that adds them, and the outputs the sum leaves through.
What I like best is what did not change. The Spectrum’s sound stays exactly as it was. A 48K program that
toggles port $FE bit 4 hears the same square wave; a 128K tune that writes the AY through port $FFFD
and port $BFFD plays on the first AY chip exactly as it did on the 128K. Everything new is switched off
or out of the way until a program asks for it, as The switches, further down, shows. The newcomers wait
in the wings until they are called.
The cast
Here is the whole system in one figure. The sources are on the left, each in the colour it keeps throughout the sound chapters, with the ports and registers your program uses to drive it:
Before we meet the sources one by one, let’s hear them. Each capture below is a program playing a tone of about 1,000 Hz through one source alone:
The level scale is the point of this figure: it shows how loud each source can be next to the others. A DAC or the Pi playing at full scale is about twice as loud as the beeper, and one AY channel is half as loud as the beeper. I find that worth remembering when you balance a mix: the old one-bit beeper is no whisper.
Have you noticed that the four tones are all close to 1,000 Hz, but not equal? Each source gets its pitch from its own clock, divided by whole numbers the program chooses, and only some pitches come out of whole numbers:
- The beeper: 3,500,000 ÷ (2 × 1,760) = 994 Hz. The CPU’s 3.5 MHz clock, divided by the length of the program’s loop in T-states, twice per cycle; the loop’s length can change only in steps of 13 T-states.
- The AY channel: 218,750 ÷ (2 × 109) = 1,003 Hz. The chip’s counters, divided by the channel’s period; the next period, 110, gives 994 Hz.
- The DAC: 3,500,000 ÷ (218 × 16) = 1,003 Hz. The CPU’s clock again, divided by the 218 T-states between two writes and the 16 writes of one cycle.
- The Pi: exactly 1,000 Hz. The Pi sends samples it has already worked out for that pitch, so the Next only plays them back.
The clocks behind the sound, further down, explains where these clocks come from.
The beeper, EAR and MIC
The beeper is the 48K’s one bit, unchanged: port $FE bit 4. While the bit is 1, it adds 512 units
to both sides of the mixer; while it is 0, nothing. Your program makes a note by switching it at the
note’s pitch: the capture above is a loop that flips the bit every 1,760 T-states, which at 3.5 MHz is a
square wave of 3,500,000 ÷ (2 × 1,760) = 994 Hz.
Port $FE bit 3, MIC, adds 128 units while it is 1: a quarter of EAR, the ratio the 48K’s resistors
gave the two bits. On the Next, the MIC line is more than the port bit. Before it reaches the mixer, it
is combined with the tape input, the signal arriving at the EAR socket from a tape recorder, and
with an EAR bit the Pi’s audio can produce. So you hear a tape load at MIC’s level, as on the original
machine. The same line goes out of the MIC socket, so a program saves to tape by switching bit 3.
The beeper costs the CPU all its time while it plays, and its pitch depends on how fast the CPU runs. I still have a soft spot for it: it is the most demanding source on the machine and the simplest. The Beeper: EAR, MIC and the Tape explains the bits, the tape input and how one bit can play two voices.
Three AY chips
The Next has three copies of the 128K’s sound chip. Each has three channels, A, B and C, and each channel plays a square wave at a pitch set by a 12-bit period, mixed with noise if you want, at one of 16 fixed volumes or following an envelope, a shape that rises or falls by itself. A chip can behave as the original General Instrument AY-3-8910 or as Yamaha’s compatible YM2149, which has a finer envelope.
The 128K’s own ports reach them: port $FFFD selects a register, port $BFFD writes it. A
special value written to port $FFFD (one with bit 7 set, which no register number has) selects
which chip the following writes go to, and that is all your program needs to play nine channels. This
is TurboSound Next, and if you ask me, it is the neatest way anyone could have tripled a sound chip
without breaking a single old program. One full channel adds up to 255 units to the mixer, and with every
channel of all three chips at full volume a side gets at most 2,295.
The capture above is chip 0’s channel B, with period 109: a square wave of 1,003 Hz, written once and left alone. One AY Chip: Tones, Noise and Envelopes takes a single chip apart, and TurboSound: Three AY Chips Behind Two Ports adds the other two, the stereo modes and panning.
Four 8-bit DACs
A DAC channel holds an 8-bit number the CPU writes, and adds it to the mixer until the next write. The Next has four, A and B on the left side and C and D on the right. Each side adds its two channels and multiplies them by 4: up to 2,040 units.
The Spectrum and Pentagon world had half a dozen sound cards built on such DACs (Soundrive, Covox,
Specdrum and others), each on its own ports. The Next answers on all of their ports, so their software
plays unchanged. I find that generous: six sound cards’ worth of software, and you need none of the
cards. It also adds three NextRegs, $2C to $2E (the DAC B Mirror (left), DAC A+D Mirror
(mono) and DAC C Mirror (right) registers), through which the Copper, or a program with no free
port, can write them. The capture above is a sine wave, 16 steps per cycle, written to port $DF,
Specdrum’s port, which writes channels A and D at once and so reaches both sides. The 8-Bit DACs: Four Channels, Six Sound Cards
explains them in full.
The Raspberry Pi’s audio
Inside the Next sits a Raspberry Pi Zero, connected by, among other things, an I2S link: a connection of a few wires that carries a stream of digital samples, left and right, in both directions. The Pi can play anything a small Linux computer can (an MP3, a speech synthesiser, another computer’s sound chip in software) and send the result to the Next, which adds it to the mixer at 10 bits per side: up to 1,023 units.
The link also works the other way, sending the Next’s own sound to the Pi. And the Pi’s stream can be
turned into the EAR bit of the tape input, so that the Next loads a program the Pi plays. I like to
think of the Pi as the guest musician: it brings its own instrument, plays whatever you ask, and needs
only a seat in the mixer. The PI I2S Audio Control register, NextReg $A2, sets it all up. The
capture above is a 1,000 Hz sine, streamed in. Audio from the Raspberry Pi: Samples on Four Wires explains the link.
Generators and level writers
The captures in The cast look alike, but they are made in two very different ways. Here is the idea I promised, and it decides what a sound costs your program:
- A generator makes a waveform by itself from a few settings. The AY chips are generators: the program writes a period, a volume and which channels are on, and the chip plays that tone until it is told otherwise, for a second or for an hour, without any help from the CPU.
- A level writer holds whatever level it was given last. The beeper and the DACs are level writers: they make a waveform only if the CPU (or the DMA, or the Copper) writes a new level at every step of it, on time.
I always picture the two as the music box and the puppet. Wind the music box (the generator) up once, and it plays its tune while you walk away. The puppet (the level writer) moves only while someone pulls its strings, and stops dead the moment the hand lets go.
Here are the same two captures, with the CPU’s writes marked under them:
The DAC’s sine costs 16 writes per cycle, 16,055 a second, each one on time. If a write comes late, the step comes late, and the wave is distorted: the puppet stumbles. Your program can afford that for a drum hit or a word of speech, or it can hand the job to the DMA, which copies a sample to a DAC port at a fixed rate on its own (the DAC chapter shows how). The AY’s tone costs nothing while it plays. That is why games of the 128K era played their music on the AY: the program runs the game and touches the chip only when the tune moves to the next note, typically once every frame.
In return, a level writer can play anything: any recorded sound, any waveform the program can compute. A generator plays only what its counters can make: square waves, noise and envelope shapes. The Next has both kinds, so you choose per sound. Here is my advice: AY music, with DAC drums over it. The music box carries the tune, the puppet dances only for the moments that need it, and the two get along splendidly.
One mixer, two sides
Every source ends in one place: the mixer, which adds them. It has two sides, and every source gives each side its own number. For the beeper the two numbers are always the same; the AY chips, the DACs and the Pi can make them differ, which is what places a sound left or right (the background chapter’s section Two speakers explains stereo).
I call the mixer the accountant. It does one thing, adding, and it never subtracts, never rounds and never asks where a number came from. You will see in a moment why that habit matters.
On each side, the mixer adds five numbers, many million times a second. The beeper gives two of them, one for each of its bits, and every other source one:
- EAR: 512 while port
$FEbit 4 is 1, otherwise 0. - MIC: 128 while the MIC line is 1, otherwise 0.
- The AY chips: 0 to 2,295, the three chips’ channels on that side added together.
- The DACs: 0 to 2,040, that side’s two channels (each 0–255) added and multiplied by 4.
- The Pi: 0 to 1,023, the side’s 10-bit sample.
The sum is a 13-bit number, 0 to 8,191. Here is how much of that range each source can take:
Two things in the figure deserve a closer look.
The sum always fits. Every source at its loudest adds up to 5,998, well below 8,191, so the sum itself never overflows: the accountant’s ledger has room for every column. The audio jack is tighter: it takes only the sum’s lowest 12 bits, up to 4,095. That is plenty for loud music with the beeper on top, but not for every source at full scale at once. The mixer chapter’s Headroom shows how much room real mixes need, and what the jack does past 4,095.
Silence is not zero. Every input is a number from 0 upwards; nothing is ever subtracted. A DAC at
rest holds $80, the middle of its range, so that a sample can swing both ways from it, and the two on
each side add 1,024 units even when no program has touched them. The Pi’s rest level is the middle of its
range too, 512. So a silent Next sits at 1,536, and every sound moves up and down around whatever constant
level its sources add. Sounds alarming? I promise it is not. The background chapter’s section on
mixing
explains why that does no harm: a capacitor after the converters removes any constant level before the
sound reaches a speaker. The Mixer: One Sum, Three Outputs
takes all of this apart.
Where the sound goes
The two sums leave the machine by four ways at once:
- The audio jack gets each side’s lowest 12 bits through a one-bit converter: a single pin of the FPGA switched on and off at 28 MHz, more often “on” the larger the number, and a resistor and a capacitor on the board that smooth the pulses into a voltage. It is the converter the background chapter calls delta-sigma.
- The internal speaker gets one mono signal, the left and right sums added together, through one
more converter of the same kind. The Peripheral 3 Setting register, NextReg
$08, bit 4 switches it on (and it is on after power-on). With bit 6 of the Peripheral 2 Setting register, NextReg$06, also set, the speaker plays only the beeper, and EAR and MIC leave the mix. So your program can click keys and beep alerts on the speaker without disturbing the music in the headphones. - HDMI audio sends the sums as numbers, 48,000 samples a second, each a 16-bit word, beside the
picture on the HDMI cable. Bit 2 of the Peripheral 4 Setting register, NextReg
$09, silences it. - The Raspberry Pi receives the sums over the I2S link, when the link is set up to send.
One line goes out separately: the MIC line, port $FE bit 3 combined with the tape input and the
Pi’s EAR bit, drives the MIC socket, the tape recorder’s input. That is how a program saves to tape.
Here is a detail I find worth a second look: the jack and the speaker have no sample rate. Their converters follow the sum as it changes, 28 million times a second. Only HDMI samples it, because HDMI carries sound as numbers. The mixer chapter explains each output.
The clocks behind the sound
Every part of the Next runs from one master clock of 28 MHz. Two branches of it time the sound, and they behave differently when the CPU’s speed changes:
The AY chips’ clock is fixed. The master clock divided by 16 gives 1.75 MHz, close to the 1.77 MHz of the 128K’s AY, and each chip divides that by 8 again: its counters step 218,750 times a second. A tone’s pitch is that rate divided by twice the channel’s period. So period 109 gives 218,750 ÷ 218 = 1,003 Hz, and the A of 440 Hz needs a period of 249 (439.3 Hz, as close as a whole number allows). Neither the CPU’s speed nor anything else a program does changes these clocks, so AY music plays at the same pitch at 3.5 MHz and at 28 MHz. I find that reassuring: whatever your program does to the CPU, the band stays in tune.
The beeper and the DACs follow the CPU. They change only when an instruction writes them, so their
timing is the program’s. A loop that flips port $FE bit 4 every 1,760 T-states makes 994 Hz at
3.5 MHz and twice that, 1,989 Hz, at 7 MHz. The Next’s CPU can run at 3.5, 7, 14 or 28 MHz (the CPU
Speed register, NextReg $07), so a beeper tune written for the 48K plays an octave higher at 7 MHz,
and a sample player plays its samples faster. Chipmunks, anyone? My rule of thumb: a level writer’s
pitch belongs to whoever owns the clock that times its writes.
Contention, which stops the CPU at certain moments when it touches the screen memory at 3.5 MHz (see ZX Spectrum: How the Screen Is Drawn), also stretches a loop that runs in that memory, and its note comes out lower. A DMA transfer timed by its own prescaler, or the Copper, does not depend on the CPU’s speed (the DAC chapter returns to this).
A third rate, 48,000 samples a second, belongs to HDMI only: it is how the finished sum is sent over the cable, not how any sound is made. The background chapter’s section Fast enough, or it sounds wrong explains why that rate is enough.
The switches
Five registers decide which parts of the sound system are on and how they behave. An old 48K or 128K program never writes them; a Next program, or NextZXOS, sets them up. Here are the audio bits, coloured by the source each one belongs to:
- The Peripheral 2 Setting register, NextReg
$06: bit 6 sends the beeper only to the internal speaker (as Where the sound goes explains). Bits 1–0 are the AY chips’ mode:00YM2149,01AY-3-8910, and11holds all three chips silent in reset. The register list also names a mode10, but the chips use only bit 0, so10behaves as YM2149. - The Peripheral 3 Setting register, NextReg
$08: bit 5 chooses how each chip’s three channels are spread over the two sides (ABC or ACB), bit 4 switches the internal speaker on, bit 3 the DACs, bit 1 TurboSound (with it off, only the selected chip plays, as on a 128K), and bit 0 the issue 2 keyboard, which changes what port$FEreads back from the MIC bit. - The Peripheral 4 Setting register, NextReg
$09: bits 7–5 put AY chips 2, 1 and 0 in mono, bit 2 silences HDMI audio. - NextReg
$84, one byte of the four Internal Port Decoding Enables registers, NextRegs$82–$85: bit 0 enables the AY’s ports, bits 1–7 the seven families of DAC ports, from Soundrive to Specdrum. Some of those ports collide with other devices (a joystick, the mouse); these bits decide who answers. - The PI I2S Audio Control register, NextReg
$A2: bits 7–6 switch the Pi’s audio on (left, right or both), bit 4 sets which way the data travels on its two pins, bits 3–2 mute each side, and bit 0 sends the Pi’s stream to the tape input.
After power-on, the AY chips are in YM mode with TurboSound off, the internal speaker is on, the DACs are
off and held at $80, every port family is enabled, HDMI audio plays and the Pi’s audio is off. In other
words, a machine that sounds exactly like a 128K until something asks for more. Sneaky, and very polite,
and I think it is exactly the right default.
The chapters that follow
Each source gets a chapter of its own. The Beeper: EAR, MIC and the Tape
covers port $FE, the tape input, the speaker-only beeper, and music from one bit.
One AY Chip: Tones, Noise and Envelopes takes the tone counters, the noise,
the envelope and the volume tables apart, and sets the AY against the YM. Then
TurboSound: Three AY Chips Behind Two Ports
puts three chips behind two ports, with panning, ABC, ACB and mono. The 8-Bit DACs: Four Channels, Six Sound Cards covers
the six sound cards on four channels, the port families and feeding samples on time, and
Audio from the Raspberry Pi: Samples on Four Wires the I2S link in both directions and the tape input from the Pi.
You can read those source chapters in any order; I would start with the one whose sound you like best. The Mixer: One Sum, Three Outputs comes after them, since it adds up their levels: the sum in full, the jack, the speaker and HDMI. And the last chapter, From the Machine to Your Speakers: Sound in an Emulator, is mostly about Klive itself: how an emulator turns all this into 48,000 samples a second.
What to take home
Four sources and one accountant, then. The Next keeps the 48K’s beeper, port $FE bit 4 (EAR) and
bit 3 (MIC), and the 128K’s AY chip, and adds two more AY chips (TurboSound Next), four 8-bit
DACs and the Raspberry Pi’s audio over an I2S link: four kinds of source. The MIC line also carries
the tape input, so you hear a loading tape, and it drives the MIC socket for saving.
The sources come in two kinds. Generators, the AY chips, play by themselves once set up, like a music box; level writers, the beeper and the DACs, need a write for every step, like a puppet on its strings. That difference decides what a sound costs the CPU, and it is why I keep recommending AY music with DAC drums on top.
One mixer adds every source on a left and a right side: EAR 512, MIC 128, the AY chips up to 2,295, the DACs up to 2,040 and the Pi up to 1,023 units. The sum, at most 5,998, fits its 13-bit range of 8,191; the jack takes its lowest 12 bits, up to 4,095. And silence is 1,536, not zero. The sums leave through the audio jack and the internal speaker, both one-bit converters at 28 MHz, through HDMI at 48,000 samples a second, and to the Pi.
The AY chips run from a fixed 1.75 MHz, while the beeper’s and the DACs’ timing is the program’s,
so it changes with the CPU’s speed. NextRegs $06, $08, $09, $84 and $A2 switch the parts on
and off, and after power-on the Next sounds just like a 128K. Now go and meet the cast one by one.