Skip to Content

The Beeper: EAR, MIC and the Tape

Manic Miner plays a two-voice tune on a machine whose sound is a single bit. The same bit’s neighbour tells the tape recorder what to save, and the same port reads back what the tape plays. So how does one bit make music, and how do music and data share one wire without tripping over each other?

The beeper is the oldest sound on the Next, and still the one every Spectrum program knows. A 48K game’s music and effects, the click of a key in BASIC, the screech of a program loading, and every program ever saved to cassette all go through it. Your Next runs those programs unchanged, so it has to reproduce the beeper exactly: the levels, the timing, and the small details of what a program reads back, which some old programs depend on more than their authors would like to admit. I have a lot of respect for this little bit: it carried a whole decade of Spectrum sound on its own.

Read The Big Picture: Four Sources, One Mixer first: it introduces the four kinds of sound source, and this is the first of them. If square waves and sound from a single bit are new to you, the background chapter How a Computer Makes Sound explains them, and ZX Spectrum: How the Screen Is Drawn explains port $FE, which the beeper shares with the border, and contention. Where the beeper’s levels go after the mixer is the subject of The Mixer: One Sum, Three Outputs.

The road ahead

We start with the port: the two bits of port $FE that make sound, why there are two of them, and how loud each one is in the mixer. Then you play a note with a timing loop, and see why the CPU’s speed and contention change its pitch, the second one in a rather surprising way. The key idea comes next: how short pulses let a single bit play two voices at once, and what that trick costs. After that, the tape: saving through the MIC socket, then loading through the EAR socket, where an input stage cleans up the tape’s signal before your program reads it, and where the issue 2 keyboard still lurks. We end with the internal speaker, which can keep the beeper all to itself.

The CPU runs at 3.5 MHz with contention off unless a section says otherwise; times in T-states are CPU clock cycles at that speed. Levels are 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, from programs checked to do what the text says, at 48,000 samples a second; they show the sound after the audio jack’s capacitor, so they move around zero.

Two bits of sound

The beeper lives in port $FE, the ULA’s port. Any write to an even port address (address bit 0 is 0) writes it, and three of its fields matter:

76543210writeignoredEARbeeperMICtape outborder colourread1tapeinput1keyboard: one bit per keyof the selected rows, 0 = heldany even port address is port $FE; bit 6 also reads the EAR and MIC bits back
Port $FE as a program writes it and reads it, bit 7 on the left. Blue bits are the beeper’s and the tape’s; grey bits do other jobs or always read 1.
  • Bit 4, EAR, is the beeper: the bit your program switches on and off to make a sound.
  • Bit 3, MIC, is the tape output: the bit your program switches to save data to tape. You hear it too, more quietly.
  • Bits 2–0 are the border colour.

Bits 7–5 of a write are ignored. A reset clears the port: EAR and MIC start low, so a machine that has just been switched on is silent.

I like to call EAR and MIC the loud brother and the quiet one: they live in the same port, share the same speaker, and one of them always talks four times louder than the other.

Why two sound bits? Because the 48K had two outputs with two jobs, sharing one circuit. Its ULA has a single pin for the tape sockets and the speaker: bits 4 and 3 drive it through resistors of different values, and the signal from the EAR socket arrives on the same pin. That is why MIC is quieter than EAR, and why what a program writes to bits 4 and 3 shows up when it reads the port back (more on that in What port $FE bit 6 reads). The Next has no such pin: everything is logic inside its FPGA. It keeps the two bits, their loudness and the read-back rules anyway, so that the programs that relied on them still work. I find that a lovely piece of loyalty: the pin is gone, and its habits are kept on purpose.

Reading port $FE returns the keyboard in bits 4–0 (a 0 for each key held in the rows the address selects), bit 6, the tape input, and 1 in bits 7 and 5. With no key held and nothing on bit 6, a read gives $BF.

EAR and MIC in the mixer

The mixer adds each bit as a fixed level, the same on the left and the right side:

  • EAR adds 512 while port $FE bit 4 is 1, and 0 while it is 0.
  • MIC adds 128 while the MIC line is 1, and 0 while it is 0. The MIC line is bit 3 combined with the tape input (see Saving to tape); with no tape playing it is just bit 3.

The two bits together give four levels, 0, 128, 512 and 640. Drawn on the mixer’s whole range, they turn out to be a small part of it:

the mixer’s range, one side06402,0484,0966,1448,191enlarged: 0 to 700neither0MIC128EAR512EAR + MIC6400128256384512640
EAR and MIC in the mixer, drawn to scale. Top: the four levels the two bits can make, against the sum’s whole range of one side. Bottom: the same four levels, enlarged.

EAR is four times as loud as MIC, the ratio the 48K’s two resistors gave them. Small on that scale, yes, but please don’t mistake the beeper for a quiet source: its 512 is twice what one AY channel at full volume adds (255), and about a quarter of the DACs’ largest swing on one side. Nor does it rest in the middle, as the DACs do. Its silence is 0, so the first time a program sets bit 4, the sum jumps by 512, and the speaker clicks, just as it did on the 48K (the mixer chapter’s Silence is 1,536 explains why the other sources do not click).

A note from a loop

The beeper holds whatever level it was given last. That’s all it does. To play a note, your program switches bit 4 on and off at the note’s pitch, and it has to do the switching itself, at exactly the right moments. The usual way is a loop that counts the time between two writes in T-states. Here is the loop behind the overview’s beeper capture:

di ; interrupts would stretch it ld c,$fe ld a,$10 ; bit 4 set: EAR on Loop: out (c),a ; 12 T-states: write EAR xor $10 ; 7 next write's EAR: opposite ld b,133 ; 7 Dly: djnz Dly ; 13 × 132 + 8: wait jp Loop ; 10

How it works:

  1. out (c),a writes the EAR bit; the speaker’s cone moves.
  2. xor $10 flips bit 4 of A, so the next write is the opposite level.
  3. djnz counts B down from 133. Each round that jumps back costs 13 T-states and the last one 8, so the wait is 13 × 132 + 8 = 1,724 T-states.
  4. One pass of the loop is 12 + 7 + 7 + 1,724 + 10 = 1,760 T-states: half a period of the note, since it takes two passes to go up and come back down.
  5. The loop closes with jp Loop rather than jr Loop. A jr would be a byte shorter (2 bytes against 3) but slower: it takes 12 T-states where jp takes 10. In a timing loop the speed is what counts, and every T-state is part of the note: with jr, a pass would be 1,762 T-states and the note 993 Hz. In a beeper loop, I always count T-states before bytes.

At 3.5 MHz, 1,760 T-states take 1,760 ÷ 3,500,000 = 503 µs. A whole period is twice that, so the note is 3,500,000 ÷ (2 × 1,760) = 994 Hz:

−2560+256one pass: 1,760 T-states, 503 µsone period, two passes: 1,006 µs, so 994 Hz0 ms1 ms2 ms3 ms4 ms
The loop’s note, captured from the machine: a square wave whose halves are each one pass of the loop, 1,760 T-states. The wave is centred on zero, as the capacitor after the jack centres it.

The loop’s length is the only thing that sets the pitch, and it changes in steps of 13 T-states, one round of djnz. B = 132 gives 1,747 T-states and 1,002 Hz, B = 134 gives 1,773 and 987 Hz. So your program cannot hit every pitch exactly, and a beeper tune is slightly out of tune wherever its notes fall between those steps. A loop with a finer step (another instruction of 4 T-states in the wait, for example) gets closer. I find it rather charming that the tuning of a beeper tune is a matter of instruction timings.

The CPU’s speed and contention

Because the pitch is a count of T-states, it is only as steady as the CPU’s clock. If you ask me, this is the beeper’s real weakness, more than its single bit. Two things change how long the same loop takes:

  • The CPU’s speed. The Next’s CPU runs at 3.5, 7, 14 or 28 MHz, set by the CPU Speed register, NextReg $07. A T-state at 7 MHz lasts half as long, so the same loop plays twice as high: 1,989 Hz, an octave up. A 48K beeper tune played with the CPU at a higher speed is too high and too fast, like a record on the wrong speed.
  • Contention. At 3.5 MHz, the Next stops the CPU for a few T-states when it reads memory from $4000 to $7FFF while the screen is being drawn, as the 48K’s ULA did (see Contention). A loop that runs in that memory gets longer while the beam is on a line of the picture, and its note drops while it is. The Peripheral 3 Setting register, NextReg $08, bit 6 switches contention off, and at 7 MHz and above there is none.

Here is the same loop three ways:

3.5 MHz994 Hz7 MHz1,989 Hzthe picture’s lines: a pass takesabout 2,007 T-states, 872 Hzborder:994 Hzborder:994 Hz3.5 MHz,contended994 / 872 Hz0 ms3 ms6 ms9 ms12 ms15 ms18 ms
The same loop captured three times, 18 ms each. At 7 MHz every pass takes half as long and the note is an octave higher. Placed in contended memory at 3.5 MHz with contention on, the loop plays 994 Hz in the border and is stretched to about 872 Hz while the picture’s lines are drawn (shaded).

Contention does not lower the note; it breaks it in two. It happens only while the beam is on one of the picture’s 192 lines, not in the border above and below, so the loop runs at two speeds in every frame:

  • In the border, about 8 ms of each 20 ms frame, a pass takes its 1,760 T-states, and the note is 994 Hz.
  • On the picture’s lines, about 12 ms of each frame, the CPU is held up again and again, and in this capture a pass takes about 2,007 T-states: 872 Hz.

So the pitch drops and comes back 50 times a second, and what the ear hears is a rough, warbling tone, not a lower note. Counting every period of a frame and dividing by 20 ms would give about 918 Hz, but that is only arithmetic: no pass of the loop ever plays it. I think this is the most surprising thing in the chapter, and the one worth remembering.

How much a pass is stretched depends on the loop, not only on contention. Each of its memory accesses is held up by up to 6 T-states, depending on where it falls in the ULA’s repeating pattern of 8 T-states, and a pass does not fall on the pattern the same way each time. Another loop, or the same loop at another address, is stretched by a different amount; the 2,007 T-states are this loop’s. What every such loop shares is the shape: full speed in the border, slower on the picture’s lines.

This is why beeper music on the Spectrum usually runs from $8000 and up, and with interrupts off: an interrupt would take the CPU away from the loop every 20 ms and leave a gap in the note. My advice for your own beeper code is the same: keep it out of contended memory, and keep the interrupts off while it plays.

Two voices from one bit

A square wave is one voice. Can you write two square waves at once to one bit? Not simply: when one voice wants the bit high and the other wants it low, there is no level in between to give. The trick that beeper music uses instead is to make each voice a train of short pulses, one at the start of each of its periods, and to keep the bit low the rest of the time. Pulses this short rarely need the bit at the same moment, so the bit can carry both trains, and a train of pulses at a voice’s pitch sounds at that pitch just as a square wave does. Look how little this asks of the hardware: nothing at all. It is all in the program, and I count it among the finest tricks of the 8-bit era.

This program plays two voices. Every pass of its loop gives each voice one fixed slot of time, and in its slot a voice either switches EAR on or leaves it off. Each voice keeps an 8-bit counter, its phase, and adds its step to it on every pass; when the addition carries out of 8 bits, the voice has finished a period and takes its slot with EAR on:

ld hl,0 ; H, L: the two voices' phases ld de,$0405 ; D, E: their steps, 4 and 5 Loop: ld a,h ; 4 add a,d ; 4 voice 1's phase + its step ld h,a ; 4 sbc a,a ; 4 $FF if that carried, else 0 and $10 ; 7 ... so A is EAR on or off out ($fe),a ; 11 voice 1's slot begins ld a,l ; 4 add a,e ; 4 the same for voice 2 ld l,a ; 4 sbc a,a ; 4 and $10 ; 7 out ($fe),a ; 11 voice 2's slot begins ld a,0 ; 7 nop ; 4 four nops: voice 2's slot nop ; 4 is as long as voice 1's nop ; 4 nop ; 4 out ($fe),a ; 11 both slots over: EAR off jp Loop ; 10

How it works:

  1. Every instruction always takes the same time, whatever the phases hold: sbc a,a turns the carry into a mask instead of a jump. One pass is always 112 T-states, so the loop runs 3,500,000 ÷ 112 = 31,250 times a second.
  2. Voice 1’s slot runs from its out to voice 2’s out, 34 T-states (23 for the five instructions in between and 11 for the out itself). Voice 2’s slot runs from its out to the last one, also 34. The remaining 44 T-states of the pass, from the last out round to the first, EAR is off.
  3. Voice 1’s phase gains 4 each pass, so it carries once every 256 ÷ 4 = 64 passes: its pitch is 31,250 × 4 ÷ 256 = 488.3 Hz. Voice 2’s gains 5, and carries every 51 or 52 passes: on average 31,250 × 5 ÷ 256 = 610.4 Hz. The two notes are a major third apart, a ratio of exactly 5 to 4.
  4. The slots of a pass follow each other and never overlap, so the bit is exactly the two pulse trains added together.

Here is the bit over 8 ms, worked out from the program, which is the same arithmetic the CPU does:

voice 1488 Hzvoice 2610 Hzthe bitEAR: both0 ms1 ms2 ms3 ms4 ms5 ms6 ms7 ms8 msone pass,enlargedvoice 1’s sloton if its phase carriedvoice 2’s sloton if its phase carriedalways off0 T34 T68 T112 Tone pass: 112 T-states, 32 µs; 31,250 passes a second
The two voices and the bit they share, over 8 ms. Each voice switches EAR on for one 34 T-state slot (9.7 µs) once per period; the bit carries both trains of pulses, which never meet.

The pulses are far too short to hear one by one; what your ear hears is how often they come. A frequency spectrum of the captured sound, which shows how strong each pitch in it is, finds both notes:

488 Hzvoice 1610 Hzvoice 2977 Hz1,221 Hzstrongnone100 Hz300 Hz500 Hz700 Hz900 Hz1,100 Hz1,300 Hz
The spectrum of the two-voice program’s sound, captured from the machine: how strong each frequency from 100 to 1,300 Hz is in it. The two notes stand out at 488 and 610 Hz, each with its overtone at twice its pitch.

Each note comes with overtones, pitches at whole multiples of it (977 and 1,221 Hz are the second ones). A train of pulses this short has overtones almost as strong as the note itself, many of them, up far beyond this figure; they give the beeper’s chords their buzzing, nasal colour, which I would not trade for a cleaner one. What lies between the notes is close to nothing. Voice 2’s small unevenness, a period of 51 passes here and 52 there, spreads its peak a little but does not move it.

What two voices cost

Well, nothing comes for free, and a chord from one bit is no exception. Here is the bill, as I see it:

  • Loudness. A voice is on for 34 of every 7,168 T-states (voice 1’s period of 64 passes), less than half a percent of the time. Short pulses are quiet: the louder a voice should be, the longer its slot, so a program sets the volume of a voice by the width of its pulses. Two voices need two slots that never meet, so each can have at most half the pass.
  • Pitch. A voice’s pitch is the loop’s rate times its step divided by 256. Finer pitches need a 16-bit phase, which costs more T-states per pass and lowers the rate.
  • The CPU. While the tune plays, the CPU does nothing else: every T-state of the loop is counted. This is why beeper music on the 48K played on title screens and between levels, and why the 128K’s AY chip, which plays by itself, was such a step forward.

Saving to tape

A program saves to tape by switching port $FE bit 3, MIC, in a pattern of pulses that encodes its data. The MIC socket of the Next carries that pattern to a tape recorder, as the 48K’s did. The quiet brother has a day job after all.

The MIC line is not bit 3 alone, though. It is bit 3 combined with the tape input (see Loading from tape) and with the EAR bit that the Raspberry Pi’s audio link can produce (the chapter on audio from the Raspberry Pi) by an exclusive or: the line is 1 when an odd number of the three are 1. The same line feeds both the MIC socket and the mixer’s MIC input:

EAR socketnoise rejectionunder ~1 µs droppedrelaxation1 held ~1.17 ms → 0tape inputPi’s EAR bitexclusive ororport $FE bit 4 (EAR)port $FE bit 3 (MIC)issue 2: NextReg $08 bit 0andport $FE bit 6exclusive orthe MIC lineMIC socketmixer: MIC, 128
The beeper’s lines. Port $FE bit 3 is combined with the tape input and the Pi’s EAR bit into the MIC line, which goes to the MIC socket and into the mixer. The EAR socket’s signal passes the input stage and becomes the tape input, which port $FE bit 6 reads together with the EAR and MIC bits.

Why an exclusive or? Because it keeps each signal whole, and I think it is exactly the right choice here. With no tape playing, the MIC line is bit 3, exactly what your program writes. While a tape plays and the program holds bit 3 still, the line follows the tape: if bit 3 is 1, upside down, which sounds the same and carries the same edges. What follows from that:

  • A loading tape is heard, at MIC’s level of 128: the screech of a loading program comes out of the speaker, as it did on the 48K.
  • The MIC socket echoes the tape input: a recorder plugged into it while another plays into EAR records what the Next hears.
  • A program saving data switches bit 3 while no tape plays, and the socket carries its pulses unchanged.

Loading from tape

A tape recorder plays its signal into the EAR socket. What the Next reads from it is not the raw voltage but a clean level, 0 or 1, produced by an input stage in two steps, noise rejection and relaxation, both counted on the master clock.

A tape’s signal is analogue and noisy: near an edge it may cross the threshold several times in a row. The first step, noise rejection, passes a new level on only when it has held steady for two ticks of the 1.75 MHz clock: any change starts the count again. Depending on where the ticks fall, that takes between 0.6 and 1.14 µs, so a pulse shorter than 0.6 µs never reaches the CPU and one longer than 1.14 µs always does. A real edge arrives about a microsecond late, which no loader notices.

I think of this step as the bouncer: a level gets in only if it stands still in the queue long enough, and a fidgety glitch is turned away at the door.

Relaxation: a level left at 1

The second step deals with a tape that stops on a 1, and I admire how politely it does it. Some recordings, and some tape circuits, leave the level at 1 after the last pulse instead of returning to 0, and a loader waiting for the next edge would see the bit stuck. The original machines did not have that problem: the 48K’s input relaxed back to 0 after at most about 800 µs, and the ZX81 expects it to within about 1,300 µs. The Next’s relaxation step copies them: a 1 that lasts 64 ticks of a 54,688 Hz clock (512 master clocks each), between 1.15 and 1.17 ms depending on where the ticks fall, turns into a 0, until the input goes low and high again.

Here are the two steps, worked out from the rules on made-up input signals:

noise rejection, over 6 µsglitch, 0.5 µs: droppedthe socketafterrejectionpasses 1.0 µs late0 µs1 µs2 µs3 µs4 µs5 µs6 µsrelaxation, over 5 msa tone: passes unchangedleft at 1beforeafterrelaxation1.17 ms, then 00 ms1 ms2 ms3 ms4 ms5 ms
The input stage, worked out from its rules. Top, over 6 µs: a half-microsecond glitch is rejected, and a real edge passes about a microsecond late. Bottom, over 5 ms: a tone passes untouched, and a level left at 1 relaxes to 0 after about 1.17 ms.

The relaxation cuts only highs longer than about 1.15 ms, so a tone with half-periods shorter than that, above about 430 Hz, passes untouched. A Spectrum tape is far quicker: its pilot tone’s pulses are 2,168 T-states long, 619 µs, and its data pulses shorter still, so the bouncer and the relaxation step never bother a real recording. The result of both steps is the tape input, the level that port $FE bit 6 reads and that the MIC line carries.

What port $FE bit 6 reads

Bit 6 is not the tape input alone. I call it the gossip: ask it about the tape, and it tells you that, and also what you yourself said on EAR a moment ago. It is 1 when any of these is true at the moment of the read:

  1. The tape input, combined by exclusive or with the Pi’s EAR bit, is 1.
  2. Port $FE bit 4, EAR, was last written as 1.
  3. Port $FE bit 3, MIC, was last written as 1, and the issue 2 keyboard is on: the Peripheral 3 Setting register, NextReg $08, bit 0 (0 after power-on).

So a loader must keep EAR at 0 while it reads the tape, or bit 6 reads 1 whatever the tape plays; the 48K ROM’s loader does, and keeps MIC at 1. If you write a loader of your own, my advice is to clear EAR before the first read of the tape, not somewhere after it. And bit 6 follows the bits at once: the read straight after a write that clears bit 4 already returns 0. On a 48K, the shared pin you met in Two bits of sound is an analogue line that takes a moment to fall, so a read that comes very soon after the write can still find a 1; the Next’s bit 6 is logic, and has no such delay. The gossip passes the news on at once.

The issue 2 keyboard

The 48K was built in several board revisions, called issues. On the early issue 2 boards, writing a 1 to the MIC bit was enough to make bit 6 read 1; from issue 3 on, only the EAR bit did. Software written on an issue 2 machine sometimes compared the whole byte read from port $FE against a fixed value, with bit 6 assumed to be 1, instead of testing the key bits alone. On a later machine bit 6 read 0, the comparison failed, and the program saw keys that were not pressed, or ignored the ones that were. Haunted keyboards, and all because of one bit nobody meant to test.

The Next behaves as issue 3 after power-on, as most Spectrums did. The Peripheral 3 Setting register, NextReg $08, bit 0 gives such a program the issue 2 machine it was written for: with it set, the MIC bit reads back in bit 6 too. I find it rather touching that the Next keeps a switch for one board revision of the 48K.

Let me warn you, though: the setting has a price. The 48K ROM’s loader holds MIC at 1 the whole time it listens, so with the issue 2 keyboard on, bit 6 reads 1 throughout, the loader never sees an edge, and loading from tape fails. A program that needs the setting should switch it on after it has loaded, not before.

The internal speaker

The Next has a small speaker on its board, which plays the left and right sums added together. The Peripheral 3 Setting register, NextReg $08, bit 4 switches it on (1, the value after power-on) or off.

The Peripheral 2 Setting register, NextReg $06, bit 6 sends the beeper only to the internal speaker. With it set, and the speaker on, EAR and MIC leave the mix that reaches the audio jack and HDMI, and the speaker plays them alone:

after power-onthe beeper is part of the mixother sourcesEAR and MICmixeraudio jackHDMIinternalspeakerNextReg $06 bit 6 setthe beeper on the speaker aloneother sourcesEAR and MICmixeraudio jackHDMIinternalspeaker
Where the beeper goes. Left, as it is after power-on: EAR and MIC are added into the mix with every other source, and the jack, HDMI and the speaker all play it. Right, with NextReg $06 bit 6 set: EAR and MIC go to the speaker alone, and the jack and HDMI get the mix without them.

What is this good for? Keeping two kinds of sound apart: key clicks, alerts and the tape’s loading noise come from the machine itself, while the music plays in your headphones or on the television undisturbed. I like this switch a lot: the beeper gets a speaker of its own again, as on the 48K. The beeper is moved, not copied: the headphones no longer hear it. On the speaker, EAR and MIC keep their loudness and their 4-to-1 ratio; the mixer chapter’s The internal speaker shows the bits of the speaker’s word.

What to take home

Two bits, then, and a surprising amount of machine around them. The beeper is two bits of port $FE, which any even port address reaches: bit 4, EAR, the beeper itself, and bit 3, MIC, the tape output, beside the border colour in bits 2–0. A reset clears them. In the mixer, EAR adds 512 and MIC 128 to both sides, 4 to 1 as on the 48K, the loud brother and the quiet one, and because their silence is 0, the first switch-on clicks.

A note is a timing loop: two passes make one period, so 1,760 T-states a pass at 3.5 MHz is 994 Hz, and pitches come in steps of the loop’s smallest change. The CPU’s speed (NextReg $07) scales the pitch. Contention (3.5 MHz only, switched by NextReg $08 bit 6) slows the loop only while the picture’s lines are drawn, so the note does not drop to one lower pitch: it plays at two speeds, dropping and coming back 50 times a second.

Two voices share the bit as trains of short pulses in slots that never meet. A voice’s pitch is how often its pulses come, its loudness their width, and the CPU does nothing else while they play.

On the tape side, the MIC line is bit 3 exclusive-or the tape input and the Pi’s EAR bit; it feeds the MIC socket and the mixer, so a loading tape is heard and echoed. The EAR socket passes an input stage whose bouncer rejects pulses shorter than about a microsecond, and whose relaxation step turns a 1 held for about 1.15–1.17 ms into 0. Bit 6, the gossip, reads that tape input (exclusive-or the Pi’s EAR bit), or the EAR bit, or, with the issue 2 keyboard (NextReg $08 bit 0), the MIC bit, and follows them at once.

Finally, NextReg $08 bit 4 switches the internal speaker on, and NextReg $06 bit 6 moves the beeper to the speaker alone. Not bad for two bits, is it?

Last updated on