# Sound: what the board adds around gb-apu-python Tags as in [README.md](README.md). The four Game Boy channels are [gb-apu-python](../../gb-apu-python)'s `APU(model=AGB)`, pinned by revision (owner's decision). This page uses only its public contract (README.md, docs/contract.md outside "The AGB row", and docstrings; restart.md rule 4): what the board must supply and what it takes back. ## The channels through the contract - **Clock**: the APU runs on 4,194,304 Hz T-cycles; the GBA's clock is 16,777,216 Hz, exactly 4x (GBATEK §System Clock; contract.md "The clock"). The board calls `apu.run(cycles // 4)` as it catches up, carrying the remainder (contract: "A host with another master clock converts to these T-cycles and calls run; sequencer edges and register accesses go between calls, at the T they belong to"). - **Frame sequencer**: the APU has no timer; the host calls `frame_tick()` at 512 Hz and `secondary_tick()` half a period later (contract.md "The frame sequencer"). GBATEK gives the channels' units (length 1/256 s, envelope 1/64 s, sweep 1/128 s steps; §GBA Sound Channel 1-4; `DOC`), which fix 512 Hz, but not what clocks the sequencer on the GBA nor its phase. HYPOTHESIS: a divider of the system clock, one step every 32,768 cycles, running from power-on and not reset by SOUNDCNT_X; `frame_clock_high` is kept in step with that divider (U-SND-1). - **Registers**: GBA `04000060`-`04000081` map onto NR10-NR51 as GBATEK's I/O map pairs them (`060` NR10; `062` NR11, `063` NR12; `064` NR13, `065` NR14; `068` NR21, `069` NR22; `06C` NR23, `06D` NR24; `070` NR30; `072` NR31, `073` NR32; `074` NR33, `075` NR34; `078` NR41, `079` NR42; `07C` NR43, `07D` NR44; `080` NR50, `081` NR51; `084` NR52; wave RAM `090`-`09F`; §GBA I/O Map; `DOC`). Halfword and word accesses split into the byte registers. The unused bytes between them (`066`, `06A`, `06E`, `076`, `07A`, `07E`, `086`, `08A`-`08F`) read as GBATEK's open-bus rule for I/O says ([memory-map.md](memory-map.md)); the per-register readable masks are the mGBA suite's "I/O read tests" (src/io-read.c; `TEST ROM`). - **Outputs taken back**: `digital()` (each channel's 0-15 DAC input), `dacs()` (which DACs are on), `nr50`, `nr51` (contract.md "Outputs"); the board's mixer replaces the core's sample mixer. The wave channel's 75 % forced volume and banked wave RAM are the AGB row's (README.md); `volumes()`, `wave_digit()`, `wave_quarters()` give the parts apart. - **NR50's Vin bits** are unused on the GBA (GBATEK §GBA Comparison of CGB and GBA Sound; `DOC`). ## SOUNDCNT_X (NR52) on the GBA Bit 7 is the master enable for the PSG **and** the FIFOs: "While Bit 7 is cleared, both PSG and FIFO sounds are disabled, and all PSG registers at 4000060h..4000081h are reset to zero (and must be re-initialized after re-enabling sound). However, registers 4000082h and 4000088h are kept read/write-able (of which, 4000082h has no function when sound is off, whilst 4000088h does work even when sound is off)" (GBATEK §GBA Sound Control Registers; `DOC`). The PSG part is the APU's power-off; the board adds: FIFO output silenced while off. Whether the FIFOs are also emptied and whether FIFO DMA requests still happen while off: U-SND-2. ## SOUNDCNT_H `04000082` (GBA only) (GBATEK §GBA Sound Control Registers; gbadoc audio/directsound.md; `DOC`) | Bits | Field | | --- | --- | | 0-1 | PSG volume: 25 %, 50 %, 100 %, prohibited | | 2, 3 | FIFO A, B volume: 50 %, 100 % | | 8, 9 | FIFO A to right, left | | 10 | FIFO A timer: 0 or 1 | | 11 | FIFO A reset (GBATEK "W?", gbadoc "RW") | | 12-15 | the same for FIFO B | What value 3 in bits 0-1 does: U-SND-3. What a read of bits 11/15 returns: the mGBA suite's I/O read table (`TEST ROM`). ## The FIFOs (GBATEK §GBA Sound Channel A and B - DMA Sound; §GBA DMA Transfers "Sound DMA"; `DOC`) - FIFO_A `040000A0`, FIFO_B `040000A4`: write-only, 32 bits = four signed 8-bit samples, byte 0 played first. Capacity 8 x 32 bits (32 bytes). gbadoc says "only 8 bytes total" for the samples (registers.md); GBATEK's 32-byte figure is the one the DMA request rule needs (four words left), so the board uses 32 bytes and records the disagreement (U-SND-4). - How 8- and 16-bit writes to the FIFO registers behave (does a byte write push one byte, or a whole word with the other bytes' old contents?): not documented (U-SND-5). - Each overflow of the selected timer moves one sample from the FIFO to the channel's output ("If Timer overflows then Move 8bit data from FIFO to sound circuit"). - "If FIFO contains only 4 x 32bits (16 bytes) then Request more data per DMA, Receive 4 x 32bit (16 bytes) per DMA." gbadoc instead says the DMA transfers "a single 32 bit quantity" on FIFO-empty. U-SND-6; alyosha-tas `fifo_dma/*` judge it. - Sound DMA (DMA1 or DMA2, start timing 3): "the DMA Repeat bit must be set, and the destination address must be FIFO_A (040000A0h) or FIFO_B (040000A4h). Upon DMA request from sound controller, 4 units of 32bits (16 bytes) are transferred (both Word Count register and DMA Transfer Type bit are ignored). The destination address will not be incremented in FIFO mode." - Which DMA serves which FIFO: the channel whose destination is that FIFO's address (HYPOTHESIS from GBATEK's wording; gbadoc: "I believe that FIFO A always sends its empty requests to DMA1 and that FIFO B sends its empty requests only to DMA2, though I don't have any verification of this"). U-SND-7. - FIFO reset (SOUNDCNT_H bit 11/15): empties the FIFO; gbadoc adds that it "put[s] the playing cursor back to FIFO's sample 0" (`DOC`). - An empty FIFO at a timer overflow: what sample plays (the last, zero, or stale data) is not documented (U-SND-8). alyosha-tas `unsafe/README.md`: "fifo_dma_disable_jam and fifo_dma_disable_jam_2 also crash on console due to FIFO DMA being triggered but then disabled before it starts" (`TEST ROM` prose: a request whose DMA is disabled before it runs locks the machine). - The cycle at which the timer overflow pops the sample, raises the request and the DMA starts: alyosha-tas `fifo_dma/fifo*`, `fifo_dma_disable_*` (text verdicts; detectors pass 3 of 9). ## The mixer and SOUNDBIAS (GBATEK §GBA Sound Control Registers, "Max Output Levels", "Resampling", "PWM"; `DOC`) - "Each of the two FIFOs can span the FULL output range (+/-200h). Each of the four PSGs can span one QUARTER of the output range (+/-80h). The current output levels of all six channels are added together by hardware ... The BIAS value is added to that signed value ... values that exceed the unsigned 10bit output range of 0..3FFh are clipped." - So a FIFO sample s (-128..127) contributes `s * 4` at 100 % and `s * 2` at 50 % (HYPOTHESIS for the exact scale: ±200h is 4 x 128); a PSG channel's 0-15 DAC value through NR50's 0-7 master volume and SOUNDCNT_H's 25/50/100 % gives at most ±80h. The exact PSG arithmetic (the DAC's centre, whether NR50 is `(vol + 1) / 8`, rounding) is not in GBATEK (U-SND-9). - SOUNDBIAS `04000088`: bits 1-9 bias level (default `100h` in those bits, register `0200`), bits 14-15 resolution: 9 bit at 32,768 Hz (default), 8 bit at 65,536, 7 bit at 131,072, 6 bit at 262,144. "The final sound output is resampled to a rate of 32.768kHz, at 9bit depth (the above 10bit value, divided by two)." Output is PWM at the system clock: N low bits then 512 - N high bits per 32 kHz sample. GBATEK labels the register "BIOS" in the I/O map (the BIOS sets it). - "The PSG channels 1-4 are internally generated at 262.144kHz" (`DOC`). - Model for the board (HYPOTHESIS, U-SND-10): at each output sample period (512, 256, 128 or 64 cycles by SOUNDBIAS) the board samples each FIFO's current output and the PSG outputs, sums, adds the bias, clips to 0-3FFh, keeps the top 9/8/7/6 bits, and emits that value as the board's sample. The host converts to its own rate. The analogue filtering after the PWM pin is outside the board. - The GBA has one speaker (mono) and stereo on the headphone socket (GBATEK §GBA Sound Controller); the board outputs the stereo pair. ## Judges There is no hardware judge for the AGB row's channels themselves (gb-apu-python README). For the board's part: alyosha-tas `fifo_dma/*` (9 ROMs, text verdicts, hardware-timed per the pin note), the AGS aging cartridges' sound tests (rung 6), Cult-of-GBA's and Nintendo's BIOS sound driver calls in krom's `BIOS/Sound/*` (timings; run only), and level and envelope comparisons against NanoBoyAdvance (detector) as the handoff brief lists for rung 5. NBA 1.8.3 passes 3 of the 9 fifo_dma ROMs (OBSERVED, [validation.md](validation.md)), so its sound timing is not a reference for those. ## As built (rung 5, 2026-11-23) `gba_python/sound.py`; tests in `tests/test_sound.py`; judges in [validation.md](validation.md) "Rung 5". - **Sound off** (SOUNDCNT_X bit 7): FIFO writes load nothing, and the 1-to-0 edge empties both FIFOs (alyosha-tas `fifo_dma/fifo.asm` tests 2 and 3, whose comments report what a console did; `TEST ROM`; U-SND-2). The playing samples go to 0 (HYPOTHESIS). - **The mixer** (`Sound.mix`): for each side, every PSG channel whose DAC is on and whose NR51 bit is set adds `psg_level` = `(2d - 15) * (v + 1) * 16 // 15`, shifted right by 2, 1, 0 for SOUNDCNT_H's 25/50/100 % (U-SND-9: ±80h at d = 15 or 0, v = 7; `HYPOTHESIS` beyond GBATEK's range); each FIFO sent to that side adds its signed sample x 4 (100 %) or x 2 (50 %): ±200h (`DOC` for the range, `HYPOTHESIS` for the scale). The bias (SOUNDBIAS bits 1-9) is added and the sum clipped to 0..3FFh (`DOC`). With sound off the output is the bias alone (`DOC`: SOUNDBIAS "does work even when sound is off"; with the FIFOs emptied and the PSG reset the branch is an equivalent mutant). - **The output**: one sample every 512, 256, 128 or 64 cycles (SOUNDBIAS bits 14-15), on a grid from power-on, holding each channel's value at that cycle (U-SND-10, `HYPOTHESIS`); the 10-bit sum is cut to 9, 8, 7 or 6 bits (`DOC`). `Sound.start_capture()` turns sampling on and `take_output()` returns `(left, right, period)`; capture changes nothing the CPU sees (a unit test runs a ROM both ways and compares registers, time and picture). `python -m gba_python ROM --wav FILE` writes 16-bit stereo at 32,768 Hz (the host's averaging of finer rates; the analogue stage after the PWM pin is not modelled). - Unchanged from rungs 0-4: the register map onto gb-apu-python's AGB row, the sequencer every 32,768 cycles (U-SND-1), FIFO requests at 16 bytes or fewer (U-SND-6), the DMA chosen by its destination (U-SND-7), an empty FIFO holding its last sample (U-SND-8). Not reached: the cycle timing of FIFO DMA against the CPU (six of alyosha-tas's nine `fifo_dma` ROMs fail; a sweep of the request-to-DMA delay from 0 to 8 cycles passed no more of them and was not kept), and any level check against a recording or a detector: NanoBoyAdvance's driver has no audio output and mGBA's documented scripting interface (0.10.2) has none either.