# Sound Sources: KH "Audio system" (tier 3; register behaviour stated as measured, though the notes give no captures for sound), CC65 `supervision.inc` comments (tier 3, a programmer's notes), MAME's `svis_snd.cpp` (GPL-2.0+, closed box: its output may be run and listened to, never read). The family's "sound: good enough" rule applies: sound is judged less strictly than pictures, but every number here carries its tag. ## Channels | Ch | Registers | What | Output | | --- | --- | --- | --- | | 1 | 2010-2013 | square | right only | | 2 | 2014-2017 | square | left only | | 3 | 2018-201C | 4-bit sample DMA | left and/or right (201B bits 3, 2) | | 4 | 2028-202A | noise (LFSR) | left and/or right (202A bits 3, 2) | ## Squares (2010-2017) - Frequency `F` = 11 bits (2011 bits 2-0 : 2010); **Fout = 125,000 / (F + 1)** Hz, i.e. the 4 MHz clock / 32 / (F + 1) (DOC KH; CC65 "frequency=125000/counter" agrees but for the +1). - Writing **either** frequency register resets the duty prescaler. - 2012 `?EDD VVVV`: volume 0-15, linear; duty 00 12.5%, 01 25%, 10 50%, 11 75%; E (bit 6) = play continuously, else play only for the length. - 2013 length: writing it starts the tone for `L + 1` ticks of the 16-bit prescaler (4 MHz / 65,536 = 61.04 Hz, 16.384 ms), with up to one tick of uncertainty because the prescaler is never reset. CC65 says "x/60 sec (0 means 256)"; KH's formula gives 1 tick for L = 0 (**C4**). If E is set the counter still runs but has no effect unless E is cleared before it expires. ## Sample DMA (2018-201C) - Start address 2018/19 (16 bits); length 201A x 16 bytes (0 = 4096 bytes = 8192 samples); control 201B `?BBB LRFF`: B = the ROM bank mapped at 8000-BFFF **for the sample fetches only** (overrides 2026's bank), L/R outputs, rate FF = one sample every 256, 512, 1024 or 2048 clocks (15,625 / 7,812.5 / 3,906.25 / 1,953.1 Hz). - Trigger: write 201C with bit 7 set. Each byte gives two samples, **high nibble first**; the first byte is fetched on the first prescaler overflow after the trigger; each fetch takes one cycle. - The registers are used up as it plays: a second trigger without rewriting them continues from the last address with a maximum-length (4096-byte) sample. - At the end it sets 2027 bit 1 (the sample-DMA IRQ), cleared by reading 2025. - CC65 calls 201B bits 2-3 "volume" and gives the rate as "0 15360, 11 15360/4" (**C5**: KH's bit names and rates are the measured ones; CC65's 15360 looks like 15,625 rounded through a wrong clock). ## Noise (2028-202A) - 2028 `FFFF VVVV`: volume 0-15 linear; F selects the LFSR clock as 4 MHz / divisor with divisors 8, 32, 64, 128, ... 131,072 for F = 0..D, and E, F repeating C, D (KH's table). - LFSR: 15 bits, maximal length 32,767; 7 bits (127) when 202A bit 0 is clear. KH says it is "tapped at bits 14 and 15", which cannot both exist in a 15-bit register numbered from 0 (**C9**: the exact taps and output bit are U15). - 202A `???N LREP`: N bit 4 noise enable, L/R outputs, E bit 1 continuous (else the 2029 length, as the squares), P bit 0 LFSR length. **Writing 202A resets the LFSR to all ones**; writing 2028/2029 does not. ## Mixing and output - Each side is a **4-bit DAC**. The hardware **adds** square, noise and sample DMA for that side and **clips at 0Fh** (KH "Audio Mixing"). - Pins: ASIC 14 audio left, 15 right (KH "ASIC Pinout"); the console has a speaker and an amplifier (mono speaker, stereo headphones: U16). - What a square or the noise outputs when "low" (0 or its volume subtracted?), and whether the sample DMA's 4-bit value is offset, are **U17**: they decide DC levels, not pitch. ## Model level Cycle-exact is cheap here: every source changes state on a divider of the 4 MHz clock, so the specification steps the sound per CPU cycle (or computes the next edge) and produces a 4-bit stereo stream at 4 MHz decimated by the host. The board's output contract is the two 4-bit DAC values over time; resampling is the host's.