Pacman Audio

The distinctive audio sound in pacman is a major part of the game and in translating the Z80 ROM code into C, I got a better understanding of how it works.  This post captures my findings so far.

Hardware

The hardware is very simple.  It seems to have been simply an 8-bit DAC that plays samples at 96KHz.  A 32-byte memory mapped set of registers is used by the game ROM to play sounds.  It is organised as 3 channels each with a unique waveform, volume and frequency.  Channel 1 has a 20-bit frequency and frequency counter while channels 2 and 3 both have 16-bit frequencies.

Each of the registers appears as 8-bits wide to the Z80 CPU but is actually only 4-bits wide.  (I haven't found the sound chip / ROM on the schematic but it may well have been only 4-bits). The highest 4-bits of each register are therefore ignored.  This means setting a 16-bit frequency is done by setting values across 4 registers.  Volume and waveform values are also limited to 4-bits only.

The memory mapped registers contain frequency counters which are used to generate the sounds.  On each cycle, the frequency is added to the frequency counter.  The highest 5 bits of this counter are used to select a sample from a waveform.  This sample is multiplied by the volume (0 to 15) to generate an 8-bit sample.

A 256-byte ROM contains waveforms for the various sounds. The ROM file 82s126.1m contains 256 bytes which are organised as 8 blocks of 32 bytes.  Each of the 32 bytes contains a value from 0x0 to 0xf, i.e. the high nybble is not used.   It was probably organised as 256 x 4 bit.

Software

The software uses the 3 channels in two different ways.  There are "songs" and "effects".  A number of tables in the CPU ROM are used to drive these.  There are 6 sets of software registers in the RAM, each 16-bytes long, which control aspects such as duration, repeat count, initial volume, initial frequency, frequency change and volume change.  The 60Hz interrupt handler advances internal state machines to play the various songs and effects. For example, if an effect has a frequency delta of +1, the 1 is added to the frequency every interrupt.

Audio waveforms in Excel

If we import the values into excel and draw line graphs we can see the following 8 waveforms.  Only waveforms 0 through 6 seem to be used in the ROM.


wave 0

wave 1 

wave 2


 
wave 3

wave 4



wave 5

wave 6


wave 7




Sound effects

The sound effects are distributed across the 3 channels so some can be played in parallel. A bitmask selects which sound is played. The highest number bit is played first. Sounds are played until completion but can also have a repeat value. For example, the bonus life sound plays 10 dings. Once a sound is finished, the bit mask is re-evaluated. If a bit is still set, the sound plays again. So the background siren for example is set to continuously play. The "waka waka" pill eating sound is continous while pacman is eating but the pitch doesn't change as pacman gets faster since the sound has to play to completion before it starts again.

Bit mask channel 1 channel 2 channel 3
1 bonus life
repeats 10 times
wave6
siren
freq delta +4,
wave6
eat odd number pill
freq delta -3
wave2
2 coin insert
wave2
faster siren
freq delta +5
eat even numbered pill
freq delta +3
wave2
4 faster siren
freq delta +6
eat fruit
freq delta -1
wave6
8 faster siren
freq delta +7
eat ghost
freq delta +2
wave5
16 faster siren
freq delta +8
pacman dying
freq delta -1
wave1
32 ghosts edible
freq delta -6
wave4
pacman dead
freq delta +1
wave0
64 ghost eyes return home
freq delta +4
wave0
128 steady tone not used
maybe for testing?

Songs

As well as the sound effects, there are pre-programmed sequences of "songs" that can be played on the first two channels. There is a song table for channel 3 but it doesn't contain any data so does not seem to be used

Bit mask channel 1 channel 2
1 bass start of frame treble start of frame
2 bass for scenes treble for scenes

Additional ROM?


The ROM file 82s126.3m is also 256 bytes long and also contains bytes that have zero as the first nybble.  I've read that this is audio as well except that 1) it only has 128 bytes of data, the higher 128 bytes are all zero, 2) no waveform higher than 6 is referenced in the ROM code and 3) the waveforms do not look like anything useful for audio.  So I suspect this isn't actually an audio ROM at all.  I'm not sure what it contains as I haven't found any part of the game that doesn't work without it.

Comments

Popular posts from this blog

Translating pacman into C

Movement patterns