# The host contract `gb_apu_python.APU` is driven entirely by its host: it has no clock, no timer and no bus of its own. Everything here is the whole interface; anything else on the object is internal. ## Construction and models ```python APU(sample_rate: int = 44100, model: Model = DMG) ``` `sample_rate` only matters to `take_samples()`. A `Model` is a frozen row of one revision's differences; `MODELS` maps names to rows. | Row | `cgb` | Revisions | Judged by | |---|---|---|---| | `DMG` | False | DMG-CPU (the revisions gameboy-python runs) | blargg `dmg_sound` 12/12 | | `CGB` | True | CPU CGB E (gameboy-python's CGB) | blargg `cgb_sound` 12/12, SameSuite APU 63/63 | | `AGB` | True | Game Boy Advance, in GBA mode (gba-python) | no hardware judge: see "The AGB row" | `cgb` selects the CGB-family rules: powering off clears the length counters and keeps them read-only; wave RAM is reachable at any time while CH3 plays and a trigger corrupts nothing; NRx2 writes while a channel plays take the CGB "zombie" path; the CGB sweep and noise counters (SameBoy's model, which SameSuite measures). A new row adds a `Model` field per difference, defaulting to the current behaviour so the other rows stay as they are, and the rules behind it. ### The AGB row Rewritten clean-room on 2026-11-25 from GBATEK alone (`src/gb_apu_python/agb.py`; docs/provenance.md). GBATEK's channel 3 sections, as the code cites them: - **Two banks** of 32 digits. NR30 bit 6 selects the bank that plays; the CPU's reads and writes of $FF30-$FF3F reach the other one, whether or not the channel plays (DOC: "The currently selected Bank Number (Bit 6) will be played back, while reading/writing to/from wave RAM will address the other (not selected) bank"). - **NR30 bit 5** (dimension): 0 plays the selected bank over and over; 1 plays the selected bank, then the other, then the selected one again, 32 digits each (DOC: "output will start by replaying the currently selected bank"). - **A shift register per bank**, no pointer: each period of the channel's timer shifts the playing bank one digit; the digit that leaves is played and goes back in at the far end (DOC: "the entire 128 bits are shifted, and the 4 least significant bits are output"). The CPU sees a bank in its current turn: a byte address holds whatever digits have moved under it. A restart (NR34 bit 7) goes back to the selected bank and does not turn either bank back. - **NR32 bit 7** forces 75 % (three quarters) over bits 6-5, mute included: `wave_quarters()` is 3 and `digital()[2]` is `digit * 3 >> 2`. - **Read masks** (`READ_MASKS_AGB`): NR30 and NR32 read their bits 5-7 back (mask $1F); NR31 and the rest as the CGB. - `capture_state().wave_ram` is 32 bytes on this row: bank 0 then bank 1, each as the CPU would read it now. Where GBATEK is silent the code takes the simplest reading and says HYPOTHESIS: the bank not playing does not move (in 64-digit mode the CPU can be reaching the bank that plays, and sees it move); NR30 is read at each digit; a restart does not move the register, so the first digit out is the one at the bank's end (digit 0 of an untouched bank, unlike the CGB's digit 1); NR31 reads as on the CGB; both banks power on as zeros; 75 % rounds down. Nothing here has a hardware judge. ## The clock - `tick(t)`: advance `t` T-cycles of the APU's 4,194,304 Hz clock. The Game Boy calls it once per M-cycle with `t = 4` (2 in CGB double speed). `t` is even: the channels count 2 MHz ticks, and an odd T would be lost. - `run(t)`: catch up any number of T-cycles in those 4-T steps (the remainder last). An odd T is held and joins the next call, so any `t` is safe. 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. ## The frame sequencer - `frame_tick()`: one sequencer step (512 Hz). The Game Boy calls it at each falling edge of its system counter's bit 12 (13 in double speed), and when a DIV write makes that bit fall. - `secondary_tick()`: the rising edge half a period later (envelope reloads). - `frame_clock_high`: the clock's level, which the host sets before it writes NR52. Powering on while it is high spends the next step (SameSuite `div_write_trigger`). A host without DIV keeps it in step with its own clock, or leaves it False. ## Registers `read(address)` and `write(address, value)` take the Game Boy addresses: NR10-NR52 ($FF10-$FF26, named `NR10` ... `NR52`) and wave RAM (`WAVE_RAM` = $FF30-$FF3F). Unused addresses read $FF. A host maps its own register addresses onto these. `pcm(pair)` is the CGB's PCM12/PCM34 ($FF76/$FF77). ## Outputs - `digital()`: the four channels' digital outputs (0-15), the DACs' inputs. - `dacs()`: which DACs are on (an off DAC contributes nothing at all). - `nr50`, `nr51`: master volume and panning as written. - `channel_on`: the four channel-on flags (NR52's low bits). - `volumes()`, `wave_digit()`, `wave_quarters()`: the envelope volumes and the wave channel's digit and volume apart ("The AGB row"). - `take_samples()`: with `capture = True`, interleaved signed 16-bit stereo at `sample_rate` since the last call. The mix is this core's labelled fit (DACs, panning, volume, a high-pass); a host with its own mixer (the GBA's FIFOs and SOUNDBIAS) takes `digital()`, `dacs()`, `nr50` and `nr51` instead. ## State `capture_state()` returns an `APUState`: a frozen snapshot (model, T, power, registers, wave RAM, sequencer step, channel enables, lengths, envelope volumes, digital outputs) for comparison and reporting, with `as_dict()`. Restoring is not offered, as with the board's `capture_board_state`. ## Hand-off `skip_boot()` puts the APU in the state the Game Boy boot ROM leaves (the board's skip-boot start).