Skip to Content

One AY Chip: Tones, Noise and Envelopes

Sixteen registers. Write three numbers, and the chip plays a chord forever, with no further help from the CPU. The whole soundtrack of the ZX Spectrum 128K was made from three counters, a random-number generator and a staircase. So how can so little make music?

The AY chip is the sound of the 128K era and of most Spectrum music since: game soundtracks, demo music, the trackers musicians still write in today. It is a generator (the overview’s word): once your program has set it up, it plays by itself, and the CPU is free to run the game. That is the whole point. A beeper tune keeps the CPU busy for every single edge of the wave; an AY tune costs a few register writes a frame, and the game hardly notices that the band is playing. I think no chip of its era gave so much music for so little CPU. The Next has three of these chips. Here we take one of them apart; the next chapter, TurboSound, puts the three together.

Read The Big Picture: Four Sources, One Mixer first: it introduces the four kinds of source and the difference between a generator and a level writer. If square waves and DACs are new to you, the background chapter How a Computer Makes Sound explains them. And where the chip’s output goes next is the subject of The Mixer: One Sum, Three Outputs.

The road ahead

We start with the parts of one chip and the sixteen registers a program reaches them through, by way of the 128K’s two ports. Then we follow the sound from its sources: the clock that drives everything, a counter that turns a period into a square wave, and a shift register that makes noise. The mixer register decides what each channel plays, with one case that surprises almost everyone, and the volume table turns a volume into a level on a curve rather than a straight line. Then comes the envelope, a volume that moves by itself in sixteen shapes; if you read only one section, make it that one. After a look at the two chips the Next can pretend to be, the AY-3-8910 and the YM2149, we make some music: a chord, the classic effects, and a bass line played by the envelope itself.

The chip is in YM2149 mode (the state after power-on) unless a section says otherwise. Its counters step 218,750 times a second. Levels are in the units of the Next’s mixer: one channel at full volume adds 255. The waveforms are captured from the emulated Next at 48,000 samples a second, from programs checked to do what the text says; the AY chips’ levels count up from 0, as they do on the hardware.

The parts of one chip

The chip was designed by General Instrument as the AY-3-8910, a programmable sound generator, or PSG; Yamaha made a compatible chip, the YM2149. The 128K used the first. The Next builds both into its FPGA, and a register chooses which one each of its three chips behaves as (you meet it in AY or YM, further on).

Inside, one chip has a handful of parts, all driven by one clock:

clock218,750ticks/stone AR0, R1tone BR2, R3tone CR4, R5noiseR6envelopeR11, R12, R13mixerR7toneand/ornoise,perchannelamplitude R8:fixed orenvelopevolumetableamplitude R9:fixed orenvelopevolumetableamplitude R10:fixed orenvelopevolumetableABCoutputs,0–255 eachthe envelope’s level,shared by A, B and C
One AY chip. Three tone generators and one noise generator make on/off signals; the mixer register picks tone, noise or both for each channel; each channel’s volume is fixed or follows the envelope; a table turns the volume into a level; three outputs, A, B and C, go to the mixer.
  • Three tone generators, one per channel, each a counter that makes a square wave at a pitch your program sets.
  • One noise generator, a shift register that makes random bits, shared by the three channels.
  • The mixer register, which decides for each channel whether it gets its tone, the noise, both, or neither.
  • An amplitude per channel: a fixed volume, or the envelope, a volume that rises and falls by itself; one envelope generator is shared by the three channels.
  • A volume table, the chip’s DAC, which turns each channel’s 4- or 5-bit volume into a level.
  • Three outputs, channels A, B and C, each 0 to 255 units, which go to the mixer.

Here is what I find most remarkable: every one of these parts is a small counter or a register. None of them can make a curve. Everything the chip plays is built from on/off signals and steps of volume, and that is exactly why the AY sounds the way it does.

Sixteen registers behind two ports

The chip has 16 registers, R0 to R15, each 8 bits wide, though many use fewer:

R0tone A, low 8 bitsR1tone A, top 4 bitsR2tone B, low 8 bitsR3tone B, top 4 bitsR4tone C, low 8 bitsR5tone C, top 4 bitsR6noise periodR7mixerR8amplitude AR9amplitude BR10amplitude CR11envelope period, lowR12envelope period, highR13envelope shapeR14I/O port AR15I/O port B7070R7, bits 7 to 0: port B and port A direction, noise off for C, B, A,tone off for C, B, A (active low)
The sixteen registers, with the bits each one uses. Orange bits make sound; grey outlined bits are the I/O ports and their directions; faint bits do nothing.

Your program reaches them through two ports, the 128K’s own:

  1. It writes the register’s number to port $FFFD, the register select port. The value must have bits 7–5 clear: it is a register number, 0 to 31. Only 0 to 15 exist; a write to a register numbered 16 to 31 is ignored.
  2. It writes the value to port $BFFD, the data port. The selected register stays selected, so the program can write it again without selecting it again.
  3. It reads the selected register back from port $FFFD. (On the +3’s timing, port $BFFD reads the same.)

A value written to port $FFFD with bit 7 set is not a register number; the Next uses those values to choose between its three chips (the next chapter’s Three chips, two ports). Values with bits 7–5 anything else but 000 or the chip-select pattern select nothing.

After a reset, every register is 0 except R7, which is $FF: all tones and noise switched off. With every volume at 0, the chip sits in polite silence until your program sets it up. My advice is never to rely on that, though: set every register your tune uses, R7 included, before the first note.

R14 and R15 are not sound registers at all: they are the chip’s two 8-bit I/O ports, A and B. On the 128K, port A drove the serial connector. On the Next, nothing is connected to them. R7 bits 6 and 7 make each one an output (1) or an input (0); an input reads $FF, as if every pin were pulled up, and an output reads back the value last written. Two empty rooms in the chip, if you like, with the lights still working.

The clock that drives everything

The chip runs from the Next’s 1.75 MHz PSG clock, the 28 MHz master clock divided by 16. Inside, the chip divides it by 8 again, so its counters step 218,750 times a second. Let’s call one of those steps a tick of the chip. Every pitch, every noise and every envelope in the rest of this chapter is counted in ticks. I suggest you keep that one number, 218,750, in your head: everything else follows from it.

A counter that makes a square wave

Each channel’s tone period is a 12-bit number: the low 8 bits in R0 (channel A), R2 (B) or R4 (C), the top 4 bits in R1, R3 or R5. The tone generator is a counter that counts ticks:

  1. At every tick, the counter goes up by one.
  2. When it has counted to the period (it has counted period ticks since it last started), it starts again from 0, and the channel’s square wave flips: high becomes low, or low becomes high.
  3. So each half of the square wave lasts period ticks, and a whole period lasts twice that.

Here is a period of 3, tick by tick:

tickscountersquarewave00112230415260718290101112120131142150161172180one period:2 × 3 = 6 ticksthe wave flips each timethe counter starts again from 0
A tone generator with period 3, worked out tick by tick. The counter goes 0, 1, 2 and starts again, and the square wave flips each time it does: 3 ticks per half, 6 per period.

The pitch is therefore

frequency = 218,750 ÷ (2 × period)

Let’s tune up. The A of 440 Hz needs a period of 218,750 ÷ 880 = 248.6, so 249, which plays 218,750 ÷ 498 = 439.3 Hz: as close as a whole number allows, and too little out of tune to hear. Captured from the machine:

channel Aperiod 249439.3 Hz02550 ms1 ms2 ms3 ms4 ms5 ms6 ms
Channel A with period 249 at full volume, captured: a square wave of 439.3 Hz, each half 249 ticks of the chip (1.14 ms), swinging from 0 to 255 units.

The 12 bits give periods from 1 to 4,095, and so pitches from 218,750 ÷ 8,190 = 26.7 Hz, a low growl, up to 109,375 Hz, far above hearing. A period of 0 plays as 1. The steps between pitches are fine at the bottom of the range and coarse at the top: 249 and 250 are 1.8 Hz apart, but 10 and 11 are almost 1,000 Hz apart, so very high notes come out of tune. My rule of thumb: write your tune where the periods are large, and the counter will keep it in tune for you.

The 128K’s AY ran from a clock of about 1.77 MHz instead of 1.75 MHz. The same period plays about 1.3 % lower on the Next, a quarter of a semitone: a 128K tune keeps its tune, just a little lower. I doubt many ears will ever notice.

Noise from a shift register

The noise generator makes random bits with a 17-bit shift register:

161514131211109876543210every step: each bit moves one place right →noiseexclusive orthe new bit 16
The noise generator’s 17-bit shift register. At every step the bits move one place towards bit 0; the new top bit is bit 0 exclusive-or bit 2; bit 0 is the noise signal.
  1. At every noise step, all 17 bits move one place towards bit 0, and the bit that falls out of bit 0 is lost.
  2. The new bit 16 is bit 0 exclusive-or bit 2, taken before the move. (If the register is all zeros, as it is after a reset, a 1 is fed in instead, so that it gets going.)
  3. Bit 0 is the noise signal: on or off, like a tone.

The bits it produces look random, though they are worked out exactly: the pattern repeats only after 114,681 steps. Each step changes the output with a chance of about one half. Randomness on a budget, and nobody can tell. That is why I call the noise generator the coin-tosser: at every step it flips a coin, heads or tails, and the coin happens to be loaded with arithmetic rather than luck.

The noise period

The noise generator’s counter runs at half the tone generators’ rate, 109,375 steps a second, and the noise period in R6 bits 4–0 (1 to 31; 0 plays as 1) divides it once more: the register steps 109,375 ÷ period times a second. A small period gives a high, hissing noise; a large one a slower, rougher one, closer to a rumble:

noiseperiod 1109,375steps/s0255noiseperiod 313,528steps/s02550 ms2 ms4 ms6 ms8 ms10 ms
Noise on channel A, captured, 10 ms each. With period 1 the register steps 109,375 times a second and the level changes faster than the samples can follow; with period 31 it steps 3,528 times a second, and the runs of on and off are long enough to see.

Even at period 31, the pattern takes 114,681 ÷ 3,528 = 32.5 seconds to repeat, so no ear ever hears it come round. Keep in mind, though, that there is only one noise generator per chip: its period is the same for every channel that uses it. The coin-tosser works for all three channels, and at one speed.

Tone, noise, both or neither

R7, the mixer register, decides what each channel plays. Bits 0–2 switch the tones of A, B and C, bits 3–5 their noise, and both are active low: a 0 switches the source on. Each channel’s signal is

(tone, or tone switched off) and (noise, or noise switched off)

which gives four cases:

  • Tone only (tone bit 0, noise bit 1): the square wave.
  • Noise only (tone bit 1, noise bit 0): the noise.
  • Tone and noise (both 0): on only where the tone and the noise are on, so a noise that comes and goes at the tone’s pitch: a pitched hiss, used for drums and engines.
  • Neither (both 1): the signal is always on, so the channel plays its volume as a steady level.

Captured on channel A, with the same tone (period 109) and noise (period 8) each time:

tone onlyR7 = $3E0255noise onlyR7 = $370255tone andnoiseR7 = $360255neitherR7 = $3F02550 ms2 ms4 ms6 ms8 ms10 ms
Channel A with R7 set four ways, captured, 10 ms each. Tone and noise together is the noise only while the tone is high; with neither, the channel holds its volume as a steady level.

Look at the last row. Uhm, a channel with everything switched off is not silent? Exactly, and I think it is the most counter-intuitive rule of the chip. It has two consequences that catch programmers out:

  • A channel is silenced by its volume, not by R7. Switching off a channel’s tone and noise does not make it silent: it makes it a steady level, which you hear as a click when it appears and disappears. The silent way is volume 0. Let me warn you: this catches almost everyone out once.
  • The steady level is a DAC. A program that switches off a channel’s tone and noise and then rewrites its volume many times a second plays a waveform through the volume table: that is how 128K programs played sampled drums and speech on the AY, before DAC sound cards. I love this one: the generator, turned into a level writer by sheer stubbornness.

R7 bits 6 and 7 are not sound: they set the direction of the I/O ports, as Sixteen registers behind two ports explains.

Sixteen volumes on a curve

Each channel’s amplitude register, R8 (A), R9 (B) or R10 (C), sets its loudness:

  • Bits 3–0: a fixed volume, 0 to 15. 0 is silent.
  • Bit 4: when it is 1, the channel ignores bits 3–0 and follows the envelope (the next section).

The volume is not the level. It goes through the chip’s volume table, which turns it into the level that reaches the mixer. The table is logarithmic: each step up multiplies the level by roughly the same factor. Why? Because your ear hears loudness as a ratio: doubling a level sounds like the same step up whether it goes from 32 to 64 or from 128 to 255. On a straight scale, the top volumes would sound almost alike, and the bottom ones would jump.

The YM2149 has a table of 32 entries, and a fixed volume v uses entry 2 × v + 1, every other entry. The AY-3-8910 has a table of 16, one per volume. They are not the same:

064128192255AY: 40YM: 27AY-3-8910 modeYM2149 mode0123456789101112131415fixed volume (R8–R10 bits 3–0)
The level each fixed volume gives, in mixer units, in YM2149 mode (red) and AY-3-8910 mode (orange). Both reach 255 at volume 15, but AY mode is louder at every volume below it.

At volume 15, both give 255. Below it, the AY table is louder: volume 8 gives 40 in AY mode and 27 in YM mode, volume 1 gives 3 against 2. So a 128K tune that uses middle volumes for its accompaniment sounds more balanced in AY mode, the chip it was written for. If an old tune sounds thin to you, AY mode is the first thing I would try.

The envelope: a volume that moves

An envelope is a volume that moves by itself. A channel whose amplitude register has bit 4 set takes its volume from the chip’s one envelope generator, which steps up or down at a rate your program sets, in one of sixteen shapes.

I picture the envelope generator as the ghost at the volume knob: nobody touches the knob, and yet it turns, up or down, at exactly the pace you asked for. It is my favourite part of the chip.

The envelope period is a 16-bit number, R11 (the low byte) and R12 (the high byte). Like a tone counter, the envelope’s counter counts ticks, and every period ticks (0 plays as 1) the envelope takes one step. A ramp from silence to full volume, or back, is 32 steps, so it lasts

32 × period ÷ 218,750 seconds

from 146 µs at period 1 to 9.6 seconds at 65,535. A drum hit needs a few milliseconds; a slowly fading note a second or more. Plenty of room either way, and I find that range generous for a 16-bit counter.

The sixteen envelope shapes

R13 bits 3–0 choose the shape, and each bit has a name. I always read them in this order, because it is the order in which they matter:

  • ATT (attack, bit 2): 1, the envelope starts at the bottom and climbs; 0, it starts at the top and falls.
  • CONT (continue, bit 3): 0, after one ramp it drops to 0 and stays there, whatever the other two bits say.
  • HOLD (bit 0), with CONT set: after one ramp it stops and holds its level.
  • ALT (alternate, bit 1), with CONT set: the next ramp goes the other way. With HOLD, the envelope holds the level at the opposite end of where the ramp finished.

Let’s work through two shapes:

  • Shape 13, 1101 (CONT, ATT, HOLD): it starts at the bottom and climbs (ATT); at the top it stops (HOLD), and stays at full volume. The sound of a note that swells and then sustains.
  • Shape 14, 1110 (CONT, ATT, ALT): it climbs; at the top, CONT keeps it going and ALT turns it round, so it falls; at the bottom it turns again. A triangle, forever.

Here are all sixteen, captured from the machine with the same period:

00000= 910001= 920010= 930011= 940100= 1550101= 1560110= 1570111= 158100091001101010111011121100131101141110151111bits: CONT ATT ALT HOLD (R13 bits 3–0); 10 ms each, a ramp of 2.9 ms
The sixteen envelope shapes, captured from the machine, 10 ms each with a period of 20 ticks (a ramp of 2.9 ms). Shapes 0 to 3 play as shape 9, and shapes 4 to 7 as shape 15, so there are eight different ones.

With CONT clear, ALT and HOLD do nothing, so shapes 0–3 are all “fall once, then silence”, the same as shape 9, and shapes 4–7 are all “climb once, then silence”, the same as shape 15. Did you notice that the ramps curve instead of running straight? Every step goes through the logarithmic volume table, so the level grows by a factor at each step, which your ear hears as an even climb. Sixteen shapes, eight sounds:

  • Fall once and stay silent (0–3, 9), or climb once and fall silent (4–7, 15): a plucked or struck note.
  • Fall, again and again (8), or climb, again and again (12): a saw tooth.
  • Fall and climb (10), or climb and fall (14), forever: a triangle.
  • Fall once and jump to full volume (11), or climb once and stay there (13).

Writing R13 restarts the envelope, even with the value it already holds: it goes back to the start of its shape at once. That is how your program strikes a new note with the same shape: one write per note. Let me warn you about the other side of that: a player that rewrites R13 every frame “just to be sure” restarts the envelope every frame.

32 steps or 16

In YM2149 mode, the envelope’s 32 steps go through the 32-entry table, each one to its own level. In AY-3-8910 mode the table has 16 entries, so the envelope uses only its top four bits: each level lasts two steps. The ramp takes exactly as long, but it climbs in 16 coarser steps:

YM214932 steps0255AY-3-891016 steps02550 ms2.5 ms5 ms7.5 ms10 ms12.5 ms
Shape 13 (climb and hold) with a period of 60 ticks, captured in both modes. In YM2149 mode it climbs in 32 steps of 274 µs; in AY-3-8910 mode in 16 steps of twice that. Both reach full volume at the same moment, 8.8 ms after the write.

The YM’s ramp shows fewer than 32 distinct levels at its bottom, because its table’s lowest entries come in equal pairs (1, 1, 2, 2, 3, 3): there are 29 different levels in all. To my ear, that is the YM’s real advantage: the ghost at the volume knob turns it more smoothly.

AY or YM

The Peripheral 2 Setting register, NextReg $06, bits 1–0, sets what all three of the Next’s chips are:

  • 00 (the power-on value): YM2149.
  • 01: AY-3-8910.
  • 10: the register list calls it “ZXN-8950”, but the chips look only at bit 0, so it behaves exactly as YM2149. There is no third kind of chip, however exotic the name sounds.
  • 11: holds all three chips in reset: silent, registers cleared, writes ignored, until the mode is changed again.

What your program can see change between the two modes:

  • The volume table (Sixteen volumes on a curve): AY mode is louder below volume 15.
  • The envelope’s resolution (32 steps or 16): 16 levels in AY mode, 32 in YM mode, over the same time.
  • The bits a register reads back. In YM mode, every register reads back all 8 bits as written. In AY mode, the bits a register does not use read as 0: R1, R3, R5 and R13 keep their low 4 bits, R6, R8, R9 and R10 their low 5 bits; the others read whole.
  • Registers 16 to 31, which do not exist: in YM mode they read $FF; in AY mode they read register number − 16, as if the top bit of the number were ignored. Writes to them are dropped in both modes.

So how does a program find out which chip it is on? It writes a value with the top bits set to R1, say, and reads it back: whole in YM mode, its low 4 bits in AY mode. I like tests like this one: they ask the hardware, rather than trusting a setting someone may have changed.

One more read is the Next’s own: port $BFF5 (an address that the data port $BFFD also answers) reads the selected chip’s identity in bits 7–6 and the selected register’s number in bits 4–0, for software that must know which chip and register it last selected (the next chapter’s Three chips, two ports).

Playing a chord

Three channels, three notes. C, E and G above middle C are periods 209, 166 and 140 (523.3, 658.9 and 781.3 Hz). Set once, they play until something changes them:

A: C5period 2090255B: E5period 1660255C: G5period 1400255A + B + Cthe threeadded07650 ms1 ms2 ms3 ms4 ms5 ms6 ms
A C major chord, captured: channel A plays C (523.3 Hz), B plays E (658.9 Hz), C plays G (781.3 Hz), each alone at full volume, and the three added together, the sum a chip in mono gives both sides (the TurboSound chapter’s Where the channels go).

A music player does not touch the chip while a note sounds. Once a frame, 50 times a second, it moves on in the tune: when a note ends, it writes the next note’s period and volume, or restarts the envelope with R13. A few register writes per frame is the whole cost of the music to the CPU. Not a bad deal for a band, if you ask me.

Effects, one register at a time

The once-a-frame player is also how the classic effects are made, by changing a register a little every frame. Here are the four I would call the backbone of chip music:

  • Vibrato: the period moves up and down by one or two around the note, a wobble in pitch.
  • Tremolo: the volume moves up and down, a wobble in loudness.
  • Arpeggio: one channel cycles through the notes of a chord, a new one every frame or two, so a single voice sounds like a chord. Three channels can then play six or nine “notes”, the chip-music sound.
  • Slides: the period changes by a fixed amount every frame, for pitch bends and laser sounds.

Bass from the envelope

The envelope does not have to be slow. With a short period, a repeating shape is itself a waveform. Shape 8, a falling saw tooth, repeats every 32 steps, so with a period of 14 it plays 218,750 ÷ (32 × 14) = 488.3 Hz. Shape 10, a triangle, takes 64 steps for one cycle, so a period of 7 gives the same pitch:

shape 8period 14saw tooth0255shape 10period 7triangle02550 ms1 ms2 ms3 ms4 ms5 ms
The envelope as a waveform, captured: shape 8 with a period of 14 ticks, a saw tooth, and shape 10 with a period of 7, a triangle, both at 488.3 Hz. Neither has a tone switched on: each is a steady channel whose volume follows the envelope.

These waves sound very different from the square tones, and much richer, which made the “buzzer” bass a favourite of AY musicians. The price is that there is only one envelope per chip: a channel that plays the bass takes the envelope away from the other two: there is only one ghost, and it plays one shape at a time. Musicians often switch on the channel’s tone as well, at a pitch related to the envelope’s: the mixer register then lets the envelope’s wave through only while the tone is high, which gives yet another colour. I think it is one of the cleverest tricks ever played on this chip.

What to take home

Here is the chip in a few paragraphs, the way I would want to remember it. One AY chip has three tone generators, one noise generator, a mixer register, a fixed volume or the envelope for each channel, and a logarithmic volume table. Out come three outputs, A, B and C, each 0 to 255 units. Your program reaches its 16 registers through port $FFFD, which selects one (bits 7–5 clear), and port $BFFD, which writes it; reads come from port $FFFD. After a reset, every register is 0 except R7, which is $FF.

Everything counts in ticks, 218,750 a second (1.75 MHz ÷ 8). A tone’s 12-bit period plays 218,750 ÷ (2 × period), so 249 is A at 439.3 Hz, and a period of 0 plays as 1. Noise comes from a 17-bit shift register, the coin-tosser, fed with bit 0 exclusive-or bit 2, stepping 109,375 ÷ period times a second, with the period in R6, 1 to 31. R7 switches each channel’s tone and noise, active low, and a channel with neither plays its volume as a steady level: silence is volume 0, not R7.

R8 to R10 hold a fixed volume, 0 to 15 (YM entry 2v + 1, or the AY’s own table, louder below 15), or set bit 4 to follow the envelope. The envelope, the ghost at the volume knob, takes a step every period ticks (R11 and R12, 16 bits), 32 steps a ramp, and R13 picks one of 16 shapes from CONT, ATT, ALT and HOLD, eight of them different; every write to R13 restarts it. NextReg $06 bits 1–0 make the chips YM2149 (00 or 10), AY-3-8910 (01, with a 16-level envelope and masked reads), or hold them all in reset (11).

A few writes per frame play three voices; vibrato, tremolo and arpeggios rewrite registers frame by frame; and a short envelope period turns the envelope into a saw-tooth or triangle bass. Three counters, a random-number generator and a staircase, and the rest, as they say, is music.

Last updated on