# The behaviour the core implements Each rule is written here, with its source and evidence tag, before the code that implements it; the code cites the rule number. Source names are those of [start-here.md](start-here.md). Tags (charter methodology): `DOC` (a document, named), `TEST ROM` (Acid800, hardware-stated expectations), `OBSERVED` (the closed box: Nick Mikstas's VHDL @ 88a8e00 under GHDL, driven through its pins only; or MAME 0.285 run as a binary), `FITTED`, `HYPOTHESIS`. A document overrules the closed box (restart.md rule 5); a difference is recorded in [validation.md](validation.md), "Closed-box differences". **Modelling level: cycle.** One `tick()` is one period of the O2 pin. All four channels, the clocks, the polynomial counters, the interrupt logic, the pot and keyboard scans are stepped every period. Half-cycle effects (U3) are not modelled. ## The cycle convention - **B0.1** A board performs the bus access of cycle T, then calls `tick()` for cycle T. A read returns the state left by tick T-1. [handoff-brief.md, the boards' briefs] `DOC` - **B0.2** A write is applied at the end of its own cycle's tick, after the chip's logic for that cycle has run, so the logic first sees it in tick T+1. This is how the Altirra manual's deadlines read (an AUDF write at R-1 reaches a reload at R; at R it does not) [AHRM 5.3 "Reload timing"], and agrees with ijor's statement that the decoder's strobe comes in the cycle after the CPU's write [RE p1]. `DOC` ## Rung 1: registers - **B1.1** Sixteen addresses each way, A3-A0; the board decodes chip selects and mirrors. [DS 32] `DOC` - **B1.2** Reading an address with no read register ($B, $C) returns $FF. [AHRM 5.1; Acid800 `pokey_default.s` reads $0C = $FF] `DOC`, `TEST ROM` - **B1.3** Reads have no side effects. [AHRM 5.6 (SERIN), 5.8 (KBCODE)] `DOC` - **B1.4** IRQST: a bit reads 0 while its interrupt is pending. An IRQEN bit of 1 enables; writing 0 clears the pending bit (the only acknowledge); a disabled source is locked at 1 and loses its events. [DS 23; AHRM 5.7] `DOC` - **B1.5** IRQST bit 3 is not latched: it reads 0 whenever the serial output shifter is idle, whatever IRQEN says. [DS 23; AHRM 5.6, 5.7] `DOC`. The /IRQ pin for bit 3 is asserted only while IRQEN bit 3 is 1. [C6: the documents read two ways; closed box: pin inactive with the enable clear] `OBSERVED` - **B1.6** SKSTAT (0 = true): bit 7 frame error, bit 6 keyboard overrun, bit 5 serial overrun (latches, set back to 1 by any SKRES write), bit 4 the SID pin, bit 3 Shift latch, bit 2 key held, bit 1 serial input busy, bit 0 reads 1. [DS 22; C3; AHRM 5.6, 5.8] `DOC` - **B1.7** Initialisation mode is SKCTL bits 1-0 = 00. It holds in reset the 15 kHz and 64 kHz clocks, the three polynomial counters, the serial input and output state machines and the SEROUT-full flag, and the keyboard scan. It does not touch IRQEN, IRQST, KBCODE, SERIN, AUDF/AUDC/AUDCTL, the timer counters, the channel outputs, the pot scan (except that the slow scan's clock stops), or the bit on SOD. [AHRM 5.2] `DOC` - **B1.8** Power-on: the chip has no defined state [DS 20; AHRM 5.2]. The default is every register and counter 0 (so in initialisation mode), no pot scan running; `power_on(seed=...)` randomises what is undefined. [decision 8] `HYPOTHESIS` (a choice, not a fact) ## Rung 2: clocks, timers, polynomial counters, RANDOM, STIMER, joins Cycle numbers: a write in cycle T is applied at the end of tick T (B0.2); "tick T+k" is the k-th tick after it; a read in cycle T+k sees the state after tick T+k-1. - **B2.1** The 15 kHz clock is a 7-bit XNOR counter, x^7 + x^6 + 1, shifting left (new bit = XNOR of bits 6 and 5); when it holds %1001001 a 1 is forced in and a pulse emitted. Reset state %0000000. The 64 kHz clock is a 5-bit XOR counter, x^5 + x^3 + 1 (new bit = bits 4 XOR 2), forced 1 and pulse at %00010, reset state %11111. Both are held at their reset states in initialisation mode. [AHRM 5.2 "Clock reset timing"] `DOC`. A tick emits the pulse when the counter holds the special state at its start. The tap positions are the only ones giving periods 114 and 28 (checked by enumeration). `DOC` - **B2.1b** The two clock counters see initialisation mode one tick late: a tick's clocks behave as init mode stood at the start of the previous tick. So after the SKCTL write at T that leaves init mode the counters still shift their reset bit in during tick T+1, are at their reset states at the start of tick T+2, and the first pulses come in ticks T+80 and T+21 (78 and 19 ticks after the reset state, as AHRM 5.2 says), then every 114 and 28; a period-0 timer on that clock latches its interrupt in tick T+83 / T+24 and IRQST first reads it in cycle T+84 / T+25. `TEST ROM`: Acid800 `pokey_inittiming.s`, all of it (the 15 kHz and 64 kHz NOP-sled counts, even and odd, and the four IRQST reads at T+83/T+84 for 15 kHz and T+80/T+81 for 64 kHz with AUDF1 = 2 after a STIMER at T+4), passes in the AcidSAP harness on both the 2-cycle and the 3-cycle machine only with this one-tick lag on both clocks (scratch run 2026-11-14: no lag fails both machines, lags 1/2 fail the 3-cycle machine, 2/2 fail both). The manual's sentence "the IRQ is asserted in IRQST 83 and 24 cycles after the write to SKCTL" read with B0.2 gives one cycle earlier (contradiction C10 in undocumented-behavior.md); the test's executable expectations, stated by the same author to pass on real machines, are followed. (2026-11-19: the 64 kHz read at T+81 does not time a release, since the test writes SKCTL = $01 before it; the 64 kHz +25 rests on the sled counts and the T+80 read: C10, correction.) The die: the SKCTL latch loads, and init changes, in the O2-low phase of tick T+1 [DIE: ijor PokeyReSchem-13, sheets 2, 3, 7], which fits a lag of one tick. As drawn, init then enters the 15 kHz register on a phase-1 gate and the 64 kHz register on a phase-2 gate [DIE: sheet 4], which would start the 64 kHz register one tick later still (+26, the closed box's value); the test needs both registers to start together, and the core follows the test (U1, "The die"). The polynomial counters are not delayed (B2.8: RANDOM's $FF/$7F/$3F/$1F after the write [AHRM 5.5], Acid800 `pokey_noise.s` $95/$08, both passing). That entering init mode is delayed the same way is a `HYPOTHESIS` (one delayed copy of the init signal for both edges; no test enters init mode and times the clocks). - **B2.1a** The clocks do not reset at once: in init mode (as they see it, B2.1b) each counter keeps shifting with its reset value's bit fed in (0 for the 15 kHz counter, 1 for the 64 kHz one), so a full reset takes 7 and 5 ticks, and a shorter stay in init mode leaves them part way. The manual says only that init mode must be "enabled long enough for both clocks to reset fully" [AHRM 5.2] `DOC`; the mechanism (the same as the polynomial counters' in B2.8) is a `HYPOTHESIS` that the closed box supports: with holds of 1 to 5 cycles its first pulse after release moves, with longer holds it does not (scratch experiment e17, 2026-11-14) `OBSERVED`. - **B2.2** Each channel has an 8-bit down counter. On a tick in which its clock input pulses: a counter at 0 goes "below zero" (wraps to 255) and schedules its **fire** three ticks later; otherwise it decrements. On the fire tick the channel sends its pulse (to the output circuit, the timer interrupt of channels 1, 2 and 4, and the serial port) and reloads from AUDF (not if it is the low half of a joined pair). Period at 1.79 MHz: N + 4; at the slow clocks (N + 1) x 28 or x 114. [AHRM 5.3 "Timer period"] `DOC` - **B2.3** A timer interrupt is latched in the fire tick, so it first reads in IRQST one cycle later: with STIMER at T and AUDF1 = N at 1.79 MHz, reload in tick T+3, first fire in tick T+7+N, IRQST at T+8+N, then every N+4. [AHRM 5.3 "Reload timing", "STIMER preemption", Figure 8; Acid800 `pokey_timertiming.s`] `DOC`, `TEST ROM` - **B2.4** STIMER (write at T) reloads all four counters from AUDF in tick T+3, sends no pulse, sets no interrupt, and cancels every fire scheduled for tick T+3 or later; so a STIMER at T+3+N (T+4 for N = 0) still blocks the first fire. It does not touch the 15/64 kHz clocks or the polynomial counters. [AHRM 5.3 "Resetting the timers", "STIMER preemption", "Reload timing"] `DOC` - **B2.5** Clock inputs: channel 1 takes O2 itself if AUDCTL bit 6, channel 3 if bit 5, else the base clock; channel 2 takes channel 1's fire if bit 4, channel 4 channel 3's fire if bit 3, else the base clock; the base clock is the 15 kHz pulse if AUDCTL bit 0, else the 64 kHz pulse. [HM III AUDCTL; AHRM 5.3, 5.4] `DOC` - **B2.6** Joined pairs: the low counter never reloads on its own fire (it keeps counting through 256); the high counter is clocked by the low's fire in the same tick, and when the high fires both reload. With AUDF1 = AUDF2 = 0 and STIMER at T: timer 1 at T+8, timer 2 at T+11, both reloaded in tick T+10, then every N + 7 (N = AUDF1 + 256 AUDF2); the low fires AUDF2 + 1 times per period. [AHRM 5.3 "Linked timers", "Linked timer fire timing", Figure 8] `DOC`. The manual's text says the second reload is "at T+9"; its Figure 8 marks T+9 as the last AUDF write for the next period and its IRQ cycles need a reload in tick T+10 (C9). - **B2.7** The polynomial counters shift once per tick outside init mode: right-shifting XNOR registers whose end bit is the generator's output. 4-bit: 1 + x^3 + x^4, 5-bit: 1 + x^3 + x^5 [AHRM 5.5] `DOC`; the feedback taps (4-bit: bits 0 and 1; 5-bit: bits 0 and 2; counted from the output end) are the only ones that reproduce the manual's patterns 000011101100101 and 0000011100100010101111011010011 from a zeroed register (checked by enumeration) `DOC`. The long counter is one 17-stage register, input at stage 16; in 17-bit mode the feedback is XNOR(stage 0, stage 5), in 9-bit mode XNOR(stage 8, stage 13); RANDOM reads stages 16-9 inverted. [AHRM 5.5: polynomials 1 + x^4 + x^9, 1 + x^12 + x^17, XNOR, "shifts right", "reads bits inverted"] `DOC`; the tap and read positions are the only ones giving Acid800 `pokey_noise.s`'s $95 (9-bit) and $08 (17-bit) 114 cycles after init is left, and the manual's $1F four cycles after `TEST ROM`. Where the extra eight stages sit in 9-bit mode (here: below the 9-bit loop, still shifting) is not stated anywhere (U2) `HYPOTHESIS`; the closed box can answer it through RANDOM (rung 2 records what it shows). - **B2.8** In initialisation mode the polynomial registers keep shifting with 0 fed in: RANDOM reads $FF from nine cycles after the SKCTL write and the whole 17-stage register is clear nine cycles later. [AHRM 5.5 "Initialization behavior"] `DOC`. (The 4- and 5-bit registers are assumed to do the same `HYPOTHESIS`.) On leaving, all three run from zero: RANDOM reads $FF, $7F, $3F, $1F at T+1..T+4. [AHRM 5.5] `DOC` - **B2.9** A change of AUDCTL bit 7 reaches the long counter's feedback in tick T+1, like every other write (B0.2); nothing is cleared, the lower eight stages hold the bits that left the 9-bit loop. No document gives this `HYPOTHESIS` (U2). The closed box agrees on the stages (its 17-bit recurrence holds across a 9-to-17 switch) but not on the timing: it changes recurrence at tick T+2, and on a 17-to-9 switch produces one bit, at tick T+2, that fits neither recurrence. Recorded as closed-box difference D3 (validation.md); the rung 2 streams keep AUDCTL bit 7 fixed per stream. ## Rung 3: the output stage - **B3.1** A fire clocks its channel's output circuit in the tick after the fire tick. `OBSERVED`: on the closed box an AUDC write reaches the pin one cycle sooner than a fire does relative to B0.2's convention; this is the per-cycle placement that makes both paths line up (no document gives either latency: U4). - **B3.2** With AUDC bit 7 = 0 the 5-bit generator masks clock pulses: a pulse passes only when the channel's 5-bit bit is 1. [AHRM 5.3 "Distortion"] `DOC`; 1 = pass, and the passes of channel 1 at the machine clock with AUDF1 = N are the manual's 5-bit pattern read every N + 4 bits, which is what a real chip recorded (the 62-bit level pattern of the gated square at AUDF1 = 0) [Plogue] `TEST ROM` (hardware recording; `tests/test_output.py`). *When* the bit is taken no document says. The core takes it from the closed box `OBSERVED`: each channel holds a gate flip-flop; on each of its own fires (a joined pair's low half included, though it does not reload) the fire passes if the gate is open, and the gate is then loaded for the next fire: open (1) if AUDC bit 7 is 1, else the channel's 5-bit bit at that moment (channel 1: the generator's output end; channel k one stage later). An AUDC write made in the fire's own cycle already counts for this load. While AUDC bit 7 is 1 the gate is held open every tick, so a channel ungated for even one cycle passes its next fire. STIMER loads the four gates in tick T+2, with its flip-flop set (B3.7). Measured through the pins: the steady state (gate taken N + 4 cycles before the fire, not at it: directed AUDF1 = 0..5, channel 3 = 0..3, 0 differences), the first fire after a STIMER on channels 1, 3 and 4 at both clock rates (89 cases), and the switch rules on random streams. In init mode the gate is open ("the 5-bit generator is a constant 1" [AHRM 5.5]) `DOC`; the closed box keeps it shut (D6). A STIMER other than the first after leaving init mode does not load the box's gates (D7); the core loads them on every STIMER `HYPOTHESIS` (a STIMER is a reload, and the first one does). - **B3.3** A clocked channel in a noise distortion copies its noise bit (4-bit generator if AUDC bit 6, else 9/17-bit) in register polarity: the audio sees the bits inverted from RANDOM, and 0 from both in init mode. [AHRM 5.3, 5.5] `DOC` - **B3.4** Channels 1-4 receive each generator bit one tick apart, channel 1 first [AHRM 5.5] `DOC`. Channel 1 samples stage 8 of the long register (the bit that has just left RANDOM, so the CPU sees every bit first [AHRM 5.5] `DOC`), stage 5 of the 4-bit register (its output end); the stage is `OBSERVED` (closed box: 0 differences on the 4-bit, 9-bit and 17-bit directed cases and on channel 4 joined; every other stage differs). The 5-bit gate (B3.2) takes the 5-bit loop's output end for channel 1, one stage further per channel `OBSERVED` (closed box, every other stage differs). - **B3.5** Pure tone: each clock toggles the flip-flop. [AHRM 5.3] `DOC` - **B3.6** High-pass: channel 1's output is its flip-flop XOR a second flip-flop that copies it when channel 3 fires (AUDCTL bit 2); channel 2 with channel 4 (bit 1); disabled, the second flip-flop is held at 1, so channels 1 and 2 are inverted relative to 3 and 4. [AHRM 5.3 "High-pass filter"] `DOC`. The copy is made in channel 3's fire tick, of channel 1's flip-flop as it stood before that tick's clock `OBSERVED` (closed box, the manual's $3B/$3C example: 0 differences over 600 cycles; the other three placements differ). The manual's 1.5-cycle delay and its half-cycle pulse (U3) are not modelled. - **B3.7** STIMER sets the four flip-flops to 1 [AHRM 5.3] `DOC` (C2 settled for the core as the manual says: channels 1 and 2 off, 3 and 4 on with the high-pass off). The set holds in ticks T+2 and T+3, after each tick's clocks, so a clock in either tick (from a fire in tick T+1 or T+2) is lost `OBSERVED` (closed box: pure-tone directed cases; a fire at T+2 left the box's output unchanged in all 8 cases, a fire at T+1 or earlier left nothing visible either). - **B3.8** Volume-only (AUDC bit 4) forces the output to 1 after the high-pass; the code is the volume when the output is 1, else 0. [AHRM 5.3] `DOC` - **B3.10** An AUDC write reaches the output stage with the convention of B0.2 (no extra delay) `OBSERVED` (closed box, directed cases); the cycles from an AUDC write to the analogue pin on hardware are unknown (U4). ## Rung 4: interrupts as software sees them Notation: F is a source's latch tick (a timer's fire tick, B2.3), so IRQST first reads the bit in cycle F+1; W is the cycle of an IRQEN write. - **B4.1** An IRQEN write is applied at the end of its own tick (B0.2), enabling and disabling alike, with no further delay. A latched source is latched in tick F only if its IRQEN bit is 1 during that tick: an enable at W <= F-1 latches it, at W >= F it is lost. A disable at W <= F-1 blocks it; a disable at W >= F clears the latched bit at the end of tick W, so the bit and the line are seen by reads in cycles F+1..W only (none when W = F). [AHRM 5.7 "Interrupt enable/status", "Enable/disable timing"] `DOC`. **Which cycle the manual's deadlines count from.** AHRM 5.7: an enabling write "must occur at least four cycles before the interrupt source activates"; its example is "if a timer is configured such that the IRQ handler would trigger on cycle 16 of a scan line, the latest that the write to IRQEN can occur is cycle 12"; a disabling write "at least two cycles in advance ... for an IRQ on cycle 16, the write must occur on cycle 14 or earlier", leaving "a one-cycle window where an IRQ can still occur after its source has been shut off". The manual's own example names the cycle the **IRQ handler triggers**, i.e. the first cycle of the 6502's acknowledge, A. On a machine whose delay from IRQST to that cycle is two cycles (AHRM 5.7 "Interrupt timing": "2-3 unhalted cycles"; Acid800 `pokey_irqtiming.s`: timer 1 IRQST at STIMER+16, acknowledge from +18 on a 130XE, +19 on an 800XL), A = F+3, and the one rule above gives both of the manual's numbers: - enable: W <= F-1 = A-4, "four cycles before"; - disable: at W = A-2 = F+1 the line is up only after tick F; the 6502 polls in the second-to-last cycle of an instruction, and the STA's second-to-last cycle is F, which sees the line as it was after tick F-1: not up. The next poll is after the line has dropped: blocked. At W = A-1 = F+2 the STA's poll in cycle F+1 sees the line, so the IRQ is taken right after the write: the manual's "one-cycle window" and its warning that an IRQ "can happen immediately after a write to IRQEN". The window is the CPU's polling, not a delay inside POKEY. The other reading, A = the IRQST cycle (F+1), would need an enable to take effect two ticks later than B0.2 and a disable one tick later (so that a disable at W = F still lets the line up for a cycle); but `pokey_irqtiming.s`'s first half measures the IRQEN path itself (IRQEN bit 3 enabled while the serial output is idle, B1.5): the IRQ is taken after the first instruction that follows the write (expected X = $01, or $02 on a 3-cycle machine; $01 with a one-cycle offset), which is the B0.2 timing with no extra delay `TEST ROM`. One IRQEN register with one timing fits both halves. No test sweeps an IRQEN write one cycle at a time across a timer fire, so the deadline itself is a reading of the manual's example, supported but not measured: `DOC` + `TEST ROM` for the parts, the composition a `HYPOTHESIS` that a cycle sweep of the IRQEN write against a timer 1 fire on a real machine, reading IRQST, would settle. On a 3-cycle machine the same chip gives "five cycles" for the enable and still two for the disable; the manual's example matches the 2-cycle relation. - **B4.2** /IRQ is asserted while any latched bit is pending or IRQEN bit 3 is 1 with the serial output idle (B1.4, B1.5); its level is settled once per tick, after the tick's write, so `chip.irq` and `on_irq` show no same-tick pulse when a disable lands in the latch tick. [AHRM 5.7] `DOC`; the once-per-tick settling is the cycle-level model's choice (B0.1). - **B4.3** The 2-3 cycle delay from IRQST to the 6502's acknowledge is the CPU's and the board's, not the chip's (handoff-brief.md, request 5). [AHRM 5.7 "Interrupt timing"; Acid800 `pokey_irqtiming.s` comments] `DOC`, `TEST ROM` ## Rung 5: the pot scan Notation: W is the cycle of a POTGO write; a read in cycle R sees the state after tick R-1 (B0.1). "Below threshold" is `pot_level(n)` False. - **B5.1** One 8-bit scan counter serves all eight pots. A POTGO write starts (or restarts) a scan: the counter is cleared in tick W+1 and the scan runs from then. [AHRM 5.9 "Polling mechanism"; "Reading live counters": "the counter being cleared during the cycle after the write to POTGO"] `DOC` - **B5.2** While a scan runs the counter steps by one: in fast scan (SKCTL bit 2) in every tick after the clear; in slow scan in the tick after each 15 kHz pulse. [AHRM 5.9 "Polling mechanism", "Fast pot scan mode"] `DOC`. The slow-scan placement: the manual's first increment "81+114N cycles after the write to SKCTL that exits initialization mode", read as the cycle whose read shows the transitional value (B5.5), with the 15 kHz pulse in tick T+80 (B2.1b), puts the step one tick after the pulse [AHRM 5.9 "Scan timing"] `DOC`; that it is a separate tick rather than another one-cycle offset in the manual's init-relative counting (C10) is a `HYPOTHESIS`, which a hardware read sequence of a live POT register after a timed SKCTL write would settle. In init mode the 15 kHz clock stops, so a slow scan is suspended; a fast scan is not. [AHRM 5.9 "Suspended scans"] `DOC` - **B5.3** Every tick of a running scan, after the counter's step, each pot is sampled: below threshold, its POT register takes the counter and its ALLPOT bit is 1; at or above threshold, its register holds and its bit is 0. An input that falls back below threshold resumes updating. ALLPOT is sampled every cycle, not at the 15 kHz rate, so a register can latch on any cycle. [AHRM 5.9 "Polling status"] `DOC`. So `pot_level(n)` is asked for all eight pins every tick while a scan runs, and not otherwise. - **B5.4** The scan ends in the tick that begins with the counter at 228: that tick still steps (fast scan) and samples, then ends the scan and forces ALLPOT to $00; the registers keep their values. In fast scan the registers still updating therefore take 229 ($E5) and ALLPOT reads $00 from W+231; in slow scan they hold 228 ($E4), the read after the transitional $E0 shows $E4, and ALLPOT reads $00 one cycle later. [AHRM 5.9 "Scan timing"; "Fast pot scan mode": "stops one value higher ... (229 vs. 228)"] `DOC` - **B5.5** A read of a POT register in a cycle whose tick will change it (a counter step or the clear, with that pot below threshold) returns the bitwise AND of its value before and after that tick. So a fast scan reads $00, $00, $00, $02, $00, $04, $04, $06, $00, $08, ... from W+1; a slow scan shows one such value per step, and $E0 at the step to 228; ALLPOT and latched registers read stably. [AHRM 5.9 "Reading live counters", the 17-value list] `DOC`. Whether the pot is below threshold in that tick is asked of `pot_level(n)` during the read: the value the tick will sample (B5.3). - **B5.6** The dump transistors are on (`dump_active`) while slow scan is selected (SKCTL bit 2 = 0) and no scan runs; selecting fast scan turns them off at once. [AHRM 5.9 "Fast pot scan mode", "Discharge time"] `DOC`. The cycle: `dump_active` is settled at the end of each tick, from SKCTL and the scan as that tick leaves them (B0.2) `HYPOTHESIS` (no document gives it; the closed box cannot measure it, see validation.md). - **B5.7** Initialisation mode does not touch the pot scan apart from the stopped 15 kHz clock [AHRM 5.2, 5.9 "Reset"] `DOC`. Power-on: no scan, the counter and registers 0 (B1.8, decision 8); the "abnormal pot scan of up to 256 counts" the manual allows at power-up is not modelled (`power_on(seed)` randomises the registers only) `HYPOTHESIS`. - **B5.8** The optional RC helper (`pokey_python.rc`, decision 4), board side and never imported by the chip: each pin's capacitor charges while the dump transistors are off and drains while they are on; `level(n)` is True once the charge reaches the board's per-pin target (cycles to threshold, from the controller's position). Charge is kept in "cycles of charging"; it drains exponentially with a time constant of 23 cycles (the manual's 4.6 V to 0.2 V in 40 us at 1.79 MHz: ln 23 = 3.1 time constants in 72 cycles), `FITTED` to [AHRM 5.9 "Discharge time"] treating charge as proportional to voltage (`HYPOTHESIS`: the real curve is an RC charge); it is capped at twice the target (`HYPOTHESIS`, [AHRM 5.9 "Overcharge"]: about 4.8 V against a 2.1 V threshold). ## Rung 6: the keyboard scan - **B6.1** A 6-bit scan counter drives K0-K5 (`scan_code`) and steps by one (binary, upwards, $3F to $00) in each tick with a 15 kHz pulse while the scan is enabled (SKCTL bit 1); one key is checked per step, so a full scan is 64 pulses. [AHRM 5.8 "Scan algorithm": "a 6-bit counter", "One key out of 64 total is checked per cycle at 15KHz", "the next key ($02)" after $01] `DOC`. The step happens in the pulse tick, and the key checked in that tick is the new code: `OBSERVED` (closed box: K pins change in the row after each 15 kHz pulse tick, and a key held over two codes raises its interrupt in the tick the second code appears). - **B6.2** With the scan disabled (SKCTL bit 1 = 0, which init mode includes) the state machine is forced to state 0 and the counter held in reset; SKSTAT bit 2 returns to 1; KBCODE, pending interrupts and the Shift latch are unaffected; Break is not detected. [AHRM 5.8 "Keyboard scan enable"; 5.2] `DOC`. The reset value is $3F, so the first step after enabling shows $00 `OBSERVED` (closed box: K pins $3F while held, then $00, $01, ...). - **B6.3** Each step asks the board `key_response(code)`: bit 0 is /KR1 (a key of the matrix is down at this code), bit 1 is /KR2 (the modifier line). [handoff-brief.md, "Pins in"; AHRM 5.8 "Control/Shift/Break scan"] `DOC` - **B6.4** The four-state machine, with "match" = the counter equals the compare register, or always when debounce (SKCTL bit 0) is off [AHRM 5.8 "Scan algorithm", "Keyboard debounce"] `DOC`: state 0: key down: compare := counter, go to 1. state 1: match and not down: go to 0; match and down: keyboard interrupt (IRQST bit 6, B4.1), KBCODE := counter with bit 6 = the Shift latch and bit 7 = the Control latch, SKSTAT bit 2 := 0 (held), go to 3; no match and down: go to 0. state 3: match and not down: go to 2. state 2: match and not down: SKSTAT bit 2 := 1, go to 0. State 2 with match and the key down again returns to 3 [DS 11, the flow chart's debounce loop] `HYPOTHESIS` (the manual lists no transition for it; staying in 2 would wait for a release that has happened). - **B6.5** Keyboard overrun: a new key registered while IRQST bit 6 is still pending clears SKSTAT bit 6 (set back by SKRES); KBCODE takes the new code. [AHRM 5.8 "Keyboard overruns"] `DOC`. A key registered while IRQEN bit 6 is 0 sets no interrupt and no overrun (B1.4: a disabled source is locked) `DOC`. - **B6.6** Control, Shift and Break are read on /KR2 at codes $00, $10 and $30, every scan, whatever debounce says: Control and Shift into latches (Shift is SKSTAT bit 3, live; both feed KBCODE bits 7 and 6 only when a key is registered); Break raises IRQST bit 7 on a down transition only (no repeat while held; lost if disabled at that moment). They do not touch SKSTAT bit 2 or the overrun. [AHRM 5.8 "Control/Shift/Break scan", "Key press detection", "Phantom key formation" ($00-07 Control, $10-17 Shift, $30-37 Break)] `DOC`. SKSTAT bit 3 reads 0 while Shift is latched down (B1.6). - **B6.7** A board's matrix is its own: the 5200 keypad answers at the four codes that differ in bits 0 and 5, so with debounce off a held key is registered at code c+1 and again 32 steps later at c+33, with SKSTAT bit 2 going back to 1 in between. [AHRM 5.8 "Keyboard debounce": "the 5200 keyboard is the opposite: it requires debounce to be disabled"; a5200-python `docs/handoff-brief.md`, request 4: K0 and K5 unconnected] `DOC` (a consequence of B6.4, tested). ## Rung 7: the serial port's audio-affecting parts (decision 2 (b)) The input and output shift registers are not built (decision 2): the output shifter is idle for ever (IRQST bit 3 reads 0, B1.5), SOD holds its bit, and no start bit is ever seen on SID. What is built is what reaches the timers and the audio, and the clock pins. - **B7.1** Serial output data: `sod` is 1 at power-on and holds; init mode does not change it [AHRM 5.6 "Serial port reset": "the bit currently being output ... is not reset"] `DOC`; 1 at power-on `HYPOTHESIS` (mark, the bus's idle level). SKCTL bit 7 (force break) makes `sod` read 0 while set [DS 22; AHRM 5.6] `DOC`. - **B7.2** Two-tone mode (SKCTL bit 3): the resync source is (timer 1's fire AND the serial data bit) OR timer 2's fire, where the data bit is `sod`, forced to 0 by SKCTL bit 7 [AHRM 5.6 "Two-tone mode", footnote 26 (the corrected block diagram), "Two-tone force break mode"] `DOC`. A resync reloads counters 1 and 2 from AUDF1 and AUDF2, sends no pulse, and cancels their fires due in that tick or later, as STIMER does (B2.4) [AHRM 5.6 "Two-tone resync": "As with STIMER, this does not send pulses"; "If timer 1 is set to 86 cycles, then it will be preempted"] `DOC`. It lands two ticks after the triggering fire tick (the timer's own reload): timer 1 at 1.79 MHz with AUDF1 = 0 then fires at IRQST cycles T+8, T+14, T+20 after a STIMER at T [AHRM 5.6 "Two-tone resync timing"] `DOC`. Both timers firing in one tick give one resync [same] `DOC`. When two-tone is switched off pending resyncs are dropped `HYPOTHESIS`. The two-tone output flip-flop (SOD's square wave) reaches no audio and is not modelled; `sod` stays the data bit. - **B7.3** Asynchronous receive (SKCTL bit 4): while waiting for a start bit timers 3 and 4 are held in reset: reloaded from AUDF3 and AUDF4 every tick with no fire and no pulse, their pending fires cancelled; they run from the tick after the bit is cleared (B0.2) [AHRM 5.6 "Asynchronous receive mode", footnote 25] `DOC`. Since no start bit is ever detected (decision 2), the hold lasts as long as the bit is set: the limitation is recorded in adoption.md. The tick of the release is a `HYPOTHESIS` (Acid800 `pokey_asyncrecv.s` checks it to a scan line). - **B7.4** Clock pins (Table 10, AHRM 5.6 "Clock selection"): the output clock `oclk` is a divide-by-two flip-flop toggled by each fire of channel 4 (SKCTL bits 6-4 = 010, 011, 100, 101) or channel 2 (110, 111); with the external output clock (000, 001) it follows `bclk_in`. The bidirectional clock `bclk_out` is a flip-flop toggled by channel 4's fires in modes 010 and 110 and `None` (an input) otherwise. Bits 6-4 = 000 reset both flip-flops (to 0, `HYPOTHESIS`). [AHRM 5.6 Table 10, footnote 24 ("the output clock toggles level each time the timer expires"), "Serial port reset"] `DOC`. A fire toggles them in its fire tick `HYPOTHESIS`. None of them reaches the audio. ## The output curve (used only by `mix()`; no judge compares volts) - **B3.9** `mix()` passes each channel's code through a 4-bit DAC whose two high bits weigh 1.05 times binary (steps 3-4, 7-8, 11-12 about 1.2 times wider), sums the four currents, and compresses above one channel's range: linear to 15, then 15 + 19.63 (1 - e^-(s-15)/31.17), so that two channels at 15 give 1.5 times one and all four about 2 times one; normalised to 1.0 at four channels at 15. [AHRM 5.3 "Volume control", in words only; decision 5] `FITTED` to those words, no table. Variant `LINEAR` gives the plain sum / 60.