# Sound Hardware Galaga has two sound sources on the CPU board, both reached through the Z80 memory map (`galaga_map` in `src/mame/namco/galaga.cpp`): 1. A **Namco 3-voice wavetable generator** (WSG, the Pac-Man PROM design), written directly at `0x6800`-`0x681f`. 2. A **Namco 54xx noise generator** — a Fujitsu MB8844 4-bit MCU running a 1 KB mask program — commanded through the **06xx bus controller** at `0x7000`-`0x70ff` / `0x7100`, with three 4-bit outputs feeding an analog band-pass/mixer network. No AY, no SN76489, no sample ROM. MAME sources: `sound/namco.cpp/.h`, `namco/namco54.cpp/.h`, `namco/namco06.cpp/.h`, `namco/galaga.cpp`, `namco/galaga_a.cpp`, `cpu/mb88xx/mb88xx.cpp/.h`, and for the filter math `sound/disc_flt.hxx`, `disc_mth.hxx`, `discrete.h`, `disc_cls.h`, `emu/disound.h`. ## Clocks (all from `galaga()` in galaga.cpp unless noted) | Signal | Value | Derivation | |---|---|---| | Master crystal | 18.432 MHz | `MASTER_CLOCK` | | Z80 ×3 | 3.072 MHz | master/6 | | WSG sample clock | **96,000 Hz** | master/6/32 (`NAMCO_WSG` clock) | | 06xx base clock | 48,000 Hz | master/6/64 | | 54xx MCU clock | 1.536 MHz | master/6/2 | | 54xx instruction rate | 256,000 Hz | MB88xx core: 6 clocks per cycle (`execute_clocks_to_cycles`, mb88xx.h) | ## Namco WSG (3 voices) MAME device `namco_wsg_device` = `namco_audio_device<3, false>` (namco.h): three voices, unpacked wave PROM. Galaga wires no sound-enable latch — galaga.cpp never calls `sound_enable_w` and the device starts enabled (`device_start`). ### Register window `0x6800`-`0x681f` (`pacman_sound_w`, namco.cpp) Each write stores the **low nibble only** into a 32-entry register file; values persist until rewritten. Offsets relative to `0x6800`. | Offset | Voice | Function | Bits | |---|---|---|---| | `0x05` / `0x0a` / `0x0f` | 0 / 1 / 2 | waveform select | D0-D2 (0-7) | | `0x10` | 0 | frequency bits 0-3 (voice 0 only) | D0-D3 | | `0x11` `0x12` `0x13` `0x14` | 0 | frequency bits 4-7, 8-11, 12-15, 16-19 | D0-D3 each | | `0x15` | 0 | volume | D0-D3 (0-15) | | `0x16` `0x17` `0x18` `0x19` | 1 | frequency bits 4-7, 8-11, 12-15, 16-19 | D0-D3 each | | `0x1a` | 1 | volume | D0-D3 | | `0x1b` `0x1c` `0x1d` `0x1e` | 2 | frequency bits 4-7, 8-11, 12-15, 16-19 | D0-D3 each | | `0x1f` | 2 | volume | D0-D3 | | `0x00`-`0x04`, `0x06`-`0x09`, `0x0b`-`0x0e` | — | stored, decoded to nothing | — | Voice 0 has a 20-bit frequency; voices 1-2 have 16 bits (bits 0-3 fixed at 0). MAME's decoder notes the top nibble (`0x14`/`0x19`/`0x1e`) is zero in practice; whether Galaga's code ever sets it is `[unverified]`. ### Wave PROM `prom-1.1d` Region `"namco"` of `ROM_START(galaga)` (galaga.cpp): 256 bytes at offset `0x000`, CRC `7a2815b4`, SHA1 `085ada18c498fdb18ecedef0ea8fe9217edb7b46`. `prom-2.5c` (256 bytes at `0x100`, CRC `77245b66`, SHA1 `0c4d0bee858b97632411c440bea6948a74759746`) shares the region but is a timing PROM the WSG never reads (`waveform_r` masks the address to 8 bits). Layout (`waveform_r`, non-packed instance): byte `waveform × 32 + step` holds one sample in **bits 0-3**, bits 4-7 ignored; value = nibble − 8, signed −8..+7. Eight waveforms of 32 steps. ### Synthesis algorithm (`namco_update_one`, `device_clock_changed`, namco.cpp) - Per voice, per 96 kHz tick: `acc = (acc + freq) mod 2^20`; `step = acc >> 15`; `s = (PROM[wave*32 + step] & 0x0f) − 8`; `out = s × volume`. - Tone frequency = `freq × 96000 / 2^20` = `freq × 0.0915527` Hz. - MAME's form: it doubles the clock until ≥ 192 kHz (once, so the stream runs at 192 kHz) with 15 + 1 = 16 fractional bits, indexing by `(counter >> 16) & 0x1f`. Same pitch; the extra bit is 2× oversampling, resampled by the generic core. - Mixing (`sound_stream_update`; `add_int` in disound.h divides by the given max): each voice adds `s × volume / 384` (`MIX_RES = 128 × 3`) to one float stream. Per voice −0.3125..+0.2734; three voices −0.9375..+0.8203 full scale. - Route into the mono speaker: `0.90 × 10/16 = 0.5625` (galaga.cpp). - A voice with volume 0 is skipped **and its accumulator is not advanced** in MAME; hardware presumably keeps counting `[unverified]`. - namco.cpp's header describes the real output stage as an LS273 latch, a 4.7k/2.2k/1k/470 Ω sample ladder and a 4066 switching 10k/22k/47k/100k Ω for volume; MAME linearises this as `s × volume`. Exact curve `[unverified]`. ## Namco 54xx noise generator ### The part (namco54.cpp/.h, mb88xx.cpp/.h) MB8844: 10-bit program space, 6-bit data space (64 nibbles), 6 clocks per cycle. Program `54xx.bin`, 0x400 bytes, CRC `ee7357e0`, SHA1 `01bdf984a49e8d0cc8761b2cc162fd6434d5afbe` (device region `"mcu"`). Reset comes from LS259 "misclatch" bit 3 (write `0x6823`, D0): 0 holds the 54xx, 51xx and both sub-Z80s in reset, 1 releases them (`galaga()`; `namco_54xx_device::reset`). Port wiring (`device_add_mconfig`, pin list in the namco54.cpp header): K0-K3 (pins 24-27) read command bits 4-7; R0 (pins 12,13,15,16) reads command bits 0-3; R10 (pin 23) is /IRQ from the 06xx chip select. Outputs, each 4 bits: **channel 0** = O0-O3 (pins 4-7), **channel 1** = O4-O7 (pins 8-11), **channel 2** = R4-R7 (pins 17-20). R8/R9 carry raw RNG bits but are unconnected on the board. Each output port write pushes its nibble into a discrete-sim input node (`O_w`: low nibble → node 0, high nibble → node 1; `R1_w` → node 2). ### Command delivery through the 06xx (namco06.cpp) - Control `0x7100`: bits 0-3 chip selects (bit 0 = 51xx, **bit 3 = 54xx**; bits 1-2 unused on Galaga), bit 4 = 1 read / 0 write, bits 5-7 = n, divider 2^n. Data `0x7000`-`0x70ff` (offset ignored). - A non-zero divider starts a timer at 48000 / 2^n Hz. On each falling edge the 06xx asserts NMI to the main Z80 and the selected chip-select lines; on the rising edge it clears both. Control `0x10` (divider 0) stops everything. - The Z80's NMI handler writes the next byte to `0x7000`; the 06xx forwards it to each selected chip — the 54xx latches it (`write`). The chip-select edge raises the MB8844 IRQ (`chip_select`); the MCU's interrupt routine reads K/R0. - Galaga traffic recorded in namco06.cpp's header: control `0xA8` (divider 5 → 1500 bytes/s, write, chip 3), **12 data bytes**, then `0x10`. Bursts `0xA1` (write 4) and `0x71` (read 3) target the 51xx. - Whether the MCU sees the previous or the fresh byte within one half-period depends on NMI-vs-IRQ latency; MAME orders it by scheduler with a 6 kHz maximum quantum (galaga.cpp). Hardware ordering `[unverified]`. ### Command set (restated from the namco54.cpp header) | Byte | Meaning | |---|---| | `0x0n`, `0x8n`-`0xFn` | no operation | | `0x1n` / `0x2n` / `0x5n` | play sound type A / B / C | | `0x3n` + 4 bytes / `0x4n` + 4 bytes | set type-A / type-B parameters | | `0x6n` + 5 bytes | set type-C parameters | | `0x7n` | type-C volume = n | What the parameter bytes encode is not documented in MAME; it lives in the 54xx.bin program. The 12-byte boot burst fits `0x6n`+5, `0x3n`+4 and one 1-byte command, or `0x3n`+4, `0x4n`+4 and two 1-byte commands `[unverified]`. Which play commands Galaga issues for enemy versus player-ship explosions is `[unverified]`; namco54.cpp only states the part produces the explosions. ## Filter network on the 54xx outputs (`galaga_discrete`, galaga_a.cpp) MAME's discrete simulator, at the machine output rate (48 kHz default; discrete.cpp uses the machine sample rate when the device has no clock). The Galaga block gives values without reference designators (the Bosconian block above it names R22-R46/C26-C31 for the same topology). **DAC per channel** (`discrete_dac_r1_ladder`; `dst_dac_r1` in disc_mth.hxx): bit 0 → 47 kΩ, bit 1 → 22 kΩ, bit 2 → 10 kΩ, bit 3 → 4.7 kΩ, no bias/ground resistor, logic high assumed 4 V (marked "unmeasured"). Source resistance 2,635 Ω; output = 4 V × 2635 × Σ(bit_i / R_i): weights 0.224 / 0.479 / 1.054 / 2.243 V, full scale exactly 4.0 V. **Band-pass per channel** (`DISC_OP_AMP_FILTER_IS_BAND_PASS_1M`: multiple- feedback op-amp band-pass; field order r1, r2, r3, r4, rF, c1, c2 in discrete.h). `VREF = 5 V × 2.2k/(3.3k+2.2k) = 2.0 V` on the non-inverting input and through r3 to the summing node; rails 0/5 V, output clipped to 0..3.5 V. | Channel | fed by | r1 (series + DAC 2,635 Ω) | r3 to VREF | rF | c1 = c2 | f_c | Q | gain (input-referred) | |---|---|---|---|---|---|---|---|---| | CHANL1 | 54xx ch 2 (R4-R7) | 100 kΩ + 2,635 | 22 kΩ | 220 kΩ | 1 nF | 2,521 Hz | 1.74 | −1.07 | | CHANL2 | 54xx ch 1 (O4-O7) | 47 kΩ + 2,635 | 10 kΩ | 150 kΩ | 10 nF | 450 Hz | 2.12 | −1.51 | | CHANL3 | 54xx ch 0 (O0-O3) | 150 kΩ + 2,635 | 22 kΩ | 470 kΩ | 10 nF | 167 Hz | 2.47 | −1.54 | From `dst_op_amp_filt` reset (disc_flt.hxx): `R = r1 ∥ r3`, `f_c = 1/(2π √(R·rF·c1·c2))`, `Q = √(rF/R · c1·c2)/(c1+c2)`, summing-node gain `−rF/R × c2/(c1+c2)`, input current `(V_in − VREF)/r1`, hence input-referred gain `−rF/r1 × ½`. Implemented as a biquad band-pass around VREF. **Mixer** (`DISC_MIXER_IS_OP_AMP`; `dst_mixer` in disc_mth.hxx): inverting summer about VREF, inputs 33 kΩ (CHANL1), 33 kΩ (CHANL2), 10 kΩ (CHANL3), feedback 3.3 kΩ → gains 0.10 / 0.10 / 0.33; no feedback cap; 0.1 µF output coupling cap (`cAmp`, MAME assumes a 100 kΩ load → 15.9 Hz high-pass). Scale 40,800 counts/V, the output node (disc_cls.h) divides by 32,768, route 0.90 (galaga.cpp): 1 V at the mixer ≈ 1.12 of full scale, so loud explosions rely on the core clamp. The 4 V DAC level, the load and the op-amp part are MAME assumptions; tolerances `[unverified]`. ## What a faithful browser implementation needs - **WSG**: three 20-bit phase accumulators, the 32-nibble register file, the 256-byte PROM, a 96 kHz (or 192 kHz oversampled) render loop and a resampler to the AudioContext rate. Deterministic and cheap; the DAC/volume ladder curve is the only optional analog detail. - **06xx**: control/data registers, the divided-clock NMI generator, per-byte forwarding to the 54xx latch with a chip-select edge. Needed regardless of the 54xx choice because the same NMIs drive 51xx input reads. - **54xx, option A — run the MB8844 program**: an MB88xx interpreter (10-bit PC, 64-nibble RAM, K/R/O ports, IRQ, 6-clock cycles at 1.536 MHz) executing `54xx.bin`. Bit-exact with MAME by construction; costs 256k MCU cycles per second and requires the MCU dump alongside the game ROMs. - **54xx, option B — hand-written noise HLE**: reproduce the three 4-bit output streams from the command bytes (LFSR, envelope and noise clock per type) without the MCU. Cheaper and needs no MCU dump, but MAME documents none of the algorithm: it must be re-derived by disassembling `54xx.bin` or by recording the MCU outputs and matching them, and every parameter byte Galaga sends must be understood. Verifiable only by A/B waveform comparison. - **Filter**: three biquad band-passes with the f_c/Q/gain above, the 0.10/0.10/0.33 summer and the 15.9 Hz high-pass at the render rate; then sum with the WSG at 0.5625 : 0.90. Nothing here was checked against a schematic. ## What MAME's 54xx does for Galaga (measured 2026-09-11) Traced through MAME 0.285's instruction-level 54xx (the MB8844 running `54xx.bin`) by logging its port writes from the debugger, through the power-on bang and a player death (`oracle/` notes in `docs/mame-oracle.md`): - The game sends the 54xx exactly two things: at boot, `30 40 00 02 df 40 30 30 03 df 10 20` (set type-A parameters, set type-B parameters, play A, play A, play B, play B), and at each player death `10 10 20 20`. Nothing else in 150 s of attract mode, a 2,400-frame game with kills, or a game to three deaths. Enemy explosions are WSG sounds; the 54xx is the power-on bang and the player's death. - Only output channel 1 (O4-O7, the `OUTO` at 0x09f with carry set) ever moves. Channels 0 (O0-O3) and 2 (R4-R7) stay at 0. - The sound is a random bit times an amplitude: 13 for about 120 updates, then 15, 11, 8, 6, 4, 3, 2, 1 for about 784 updates each, then 0: 6,377 updates in 2.66 s, one update every 106.8 MCU cycles on average (417 µs). The bit sequence has linear complexity 232 and differs between two triggers (about half the bits agree), so no small generator reproduces it and none needs to: it is white. - A and B were never observed apart because the game never sends them apart. **The analog path, and where the model departs from MAME's source.** By `namco54.cpp` and `mb88xx.cpp` the channel-1 nibble drives only the 450 Hz band-pass (CHANL2, mixer gain 0.10). Simulating exactly that chain (MAME's `dst_op_amp_filt` coefficients, rails, mixer, coupling capacitor) on the MCU's recorded output reproduces neither MAME's level nor its spectrum: the recording peaks at 0.80 of full scale where that chain cannot exceed 0.23, and two thirds of its energy lies at 100-300 Hz with no linear relation to the noise bits (waveform correlation 0.41 for the 450 Hz band, 0.0 for a 167 Hz band, in both recordings). The cause was not found `[unverified]`. The browser model therefore follows the recording, which is what the ear gets: the noise nibble through two unit-peak band-passes, 167 Hz Q 2.47 at 0.379 full scale per volt and 450 Hz Q 2.12 at 0.126, chosen to match the recording's nine-band spectrum (L1 distance 0.19) and RMS. Against MAME's recording of the power-on bang: envelope correlation 0.965 over 50 ms windows, RMS within 8 %, peak within 1 %. **The WSG**, rendered from the identical register-write stream at 96 kHz and averaged to 48 kHz, correlates 0.969 sample for sample with MAME's mix over the attract demo's six seconds of music, at equal RMS; the residual is MAME's 2x oversampling and resampler. `cargo run --release --bin galaga-audio -- attract-1800 out.wav` renders a run; MAME's side is `-wavwrite` on the same run. Neither recording is committed: both are the game's copyrighted audio.