# Adopting pokey-python on a board What a board calls and what it gets, exactly, at the commit named in [status.md](status.md) ("The commit boards should pin"). Written for a5200-python and a7800-python (their requests are answered one by one in [handoff-brief.md](handoff-brief.md)) and for the Rust transcription `pokey-rust`, which must reproduce `validation/conformance.txt` (`scripts/conformance.py`). Every pin of the interface is built; the one part deliberately not built is the serial port's two shift registers (decision 2), stated where it shows (SID, SOD, IRQST bit 3). ## Construction from pokey_python import POKEY, C012294, LINEAR chip = POKEY(C012294) # or POKEY(LINEAR): the same engine, a linear mix() chip = POKEY(C012294, record=True) # also queue levels() per tick: take_outputs() `POKEY()` and `chip.power_on()` give every register and counter 0, so the chip starts in initialisation mode (SKCTL = 0) with no pot scan running (B1.8, a choice: the chip has no defined power-on state). `chip.power_on(seed=n)` randomises what the data sheet leaves undefined, for testing that software initialises the chip. There is no `reset()`: the chip has no reset pin. The object holds fewer than 80 attributes (a test guards it; PyPy turns larger instances into dictionaries). ## Time: one `tick()` per O2 period - `chip.tick()` advances one period of the O2 pin; `chip.run(n)` advances n. `chip.clock` counts ticks. - Call it for **every** machine cycle, the cycles in which ANTIC, MARIA or a stretched access halts the CPU included: POKEY's clock does not stop. - The core never assumes a rate (1.79 MHz, 1.25 or 1.5 MHz on arcade boards, uneven on a 7800 cartridge) and derives its 15 kHz and 64 kHz clocks from O2 alone. Never derive them from a video counter: their phase is set by the program's last SKCTL write that left initialisation mode (B2.1). ## The bus: the access first, then the tick of the same cycle value = chip.read(register) # register = A3-A0 (0-15); the board decodes selects and mirrors chip.write(register, value) chip.tick() - A read returns the state the previous tick left (B0.1); reads have no side effects. Addresses with no read register ($B, $C) return $FF. - A write is applied at the end of its own cycle's tick (B0.2). With this order every cycle number of [timing.md](timing.md) reads directly: a write before tick T and a read before tick T+k is "write at T+0, read at T+k". - One access per cycle. A board whose CPU writes in a cycle performs the write, then ticks; it does not tick first. ## Pins in | Pin | API | Consulted | Default | State | | --- | --- | --- | --- | --- | | P0-P7 | `chip.pot_level = f(n) -> bool` (above threshold) | every tick of a running scan, all eight pins (B5.3); also during a POTn read in a cycle whose tick will change that register (B5.5) | never crosses (pots read 228, or 229 in fast scan) | rung 5. A board without its own capacitor model may use `pokey_python.rc.PotRC` (B5.8): `rc = PotRC(chip, targets)`, `chip.pot_level = rc.level`, `chip.on_dump = rc.dumped`, targets in cycles of charging | | /KR1, /KR2 | `chip.key_response = f(code) -> int`, bit 0 = KR1 asserted, bit 1 = KR2 asserted, for the 6-bit scan code | once per scan step, with the new code, in the tick of each 15 kHz pulse while SKCTL bit 1 is set (B6.1, B6.3) | 0 | rung 6. /KR2 is read as Control at $00, Shift at $10, Break at $30 (B6.6); a 5200 board returns its upper button on bit 1 for every code and its keypad on bit 0 ignoring bits 0 and 5 of the code (B6.7) | | SID | `chip.sid` (0 or 1) | read by SKSTAT bit 4 | 1 (mark) | SKSTAT bit 4 only. The input shift register is not built (decision 2): a start bit on SID is never seen, so with SKCTL bit 4 (asynchronous receive) set, timers 3 and 4 stay held (B7.3) | | BCLK when an input | `chip.bclk_in` (0 or 1) | shown on `oclk` in clock modes 000 and 001 | 1 | rung 7 (B7.4) | The board's matrix is the board's: the 5200's function ignores bits 0 and 5 of the code so one key answers at four codes; the core assumes nothing about the matrix. ## Pins out | Pin | API | State | | --- | --- | --- | | /IRQ | `chip.irq` (True while asserted), valid after every tick, settled once per tick (no same-tick pulses); optional `chip.on_irq(level)` called on each change | timers 1, 2, 4 and the idle serial output (bit 3) now; IRQEN writes take effect at the end of their tick (B4.1). The 2-3 cycle delay to the 6502's response is the CPU's and the board's: a board feeds the CPU the level after the previous tick (the 2-cycle machine), or one tick older (the 3-cycle one), as `tests/acidsap.py` does | | audio, digital | `chip.levels() -> (c1, c2, c3, c4)`: each channel's volume (0-15) if its output bit is 1, else 0, valid after every tick | rung 3: the comparison of record | | audio, analogue | `chip.mix() -> float` 0.0-1.0: the four codes through `variant.dac` and the fitted saturating curve (B3.9, FITTED to the Altirra manual's words; `LINEAR` gives the plain sum / 60) | rung 3. The amplifier, filters, the mix with GTIA's or the TIA's sound and resampling are the board's | | dump transistors | `chip.dump_active` (True while the chip drains the pot pins: slow scan selected and no scan running), settled after every tick; optional `chip.on_dump(level)` on each change | rung 5 (B5.6) | | K0-K5 | `chip.scan_code` ($3F while the scan is disabled, then $00, $01, ...), valid after every tick | rung 6 (B6.1, B6.2) | | SOD, OCLK, BCLK | `chip.sod` (the held data bit, 1 at power-on; 0 under force break), `chip.oclk` (the output clock's divide-by-two of channel 4 or 2 per SKCTL bits 6-4, or `bclk_in` with the external clock), `chip.bclk_out` (channel 4's divide-by-two in modes 010 and 110, else None) | rung 7 (B7.1, B7.4). The output shift register is not built (decision 2): SOD never shifts a byte and IRQST bit 3 stays 0; two-tone resync (B7.2) and the asynchronous-receive hold (B7.3) act on the timers and so on the audio | With `record=True`, `chip.take_outputs()` returns one `levels()` tuple per tick since the last call. ## State `chip.snapshot() -> dict` of plain values (lists copied) and `chip.restore(state)`: everything except the wiring (`pot_level`, `key_response`, `on_irq`, `on_dump`), the variant and the record flag. ## What a board must not do - Skip ticks while the CPU is halted, or batch them out of order with bus accesses. - Silence or skip a channel because it is inaudible (ultrasonic tones and aliased noise are used by real software: timing.md, "Ultrasonic settings"). - Work around a corner of the chip in the board. Raise it here with a repro. ## Known differences from the closed box The core follows documents and hardware numbers; where it differs from the schematic-derived VHDL measured through its pins, the difference is listed (D1-D8 in [validation.md](validation.md)). None is a known hardware error of the core; D6 and D7 are choices that rest on the Altirra manual and on symmetry respectively.