"""CPU clocks per display frame from the stale-compare statistics, re-derived with a corrected self-test. Paper analysis, 2026-10-16. Method of our board review of 15 October 2026 (its sections 2.1 and 7.1), recomputed: PER(k) = F + j(k) - j(k-1) - p(k-1) (j: IRQ12 acceptance delay, p: wrapper+native prefix to the FRC write) stale at reset k <=> compare #4 of reset k-1 lands between the mask (06000576) and the FRC write (06000596) => F = mean(PER) + P4 + a'/2 - c + s * P(B) c is the centre of the PER(k) band that precedes a stale-marked record k+1 (IRQ10 in the high band), a' the mask-to-write distance, s the extra prefix when the FRT handler runs inside the prefix, P(B) its frequency. The REVIEW's self-test (s7.1) used the emulator truth 52,433.25 ticks for build24, whose FRT clock in fact ran 52,436.9 ticks per frame (double-counted carry, fixed in build26). Here the estimator is tested on both emulator builds with the right truths and P4, then applied to the board (P4 = 52,431 for the OCRA+1 period of manual Figure 11.7; 52,428 shown for the 13,107 alternative). Writes frame_board_paper_20261016/numbers/frame_period.json and prints a table. """ import csv, json, statistics as st, collections from pathlib import Path AF = Path(__file__).resolve().parents[2] OUT = AF / 'frame_board_paper_20261016/numbers' NOMINAL = 25_000_000 / 59.59949 # 419,467.3 clocks: driver/MAME reference ratio def q(s, f): s = sorted(s) return s[int(round(f * (len(s) - 1)))] def load_emu(run): rows = [r for r in csv.DictReader(open(AF / f'slowdown_census_20261015/{run}/episodes/attract/records.tsv'), delimiter='\t') if r['k'].isdigit() and r['passstart'].isdigit()] return {int(r['k']): dict(per=int(r['period']), irqn=int(r['irqn']), irqfirst=int(r['irqfirst']), vend=int(r['vblend'])) for r in rows}, (115, 145, 180, 210) def load_board(): rec = {} for r in csv.DictReader(open(AF / 'board_probe_20261011/board_return_20261013_131322/analysis/reviewed_records.csv')): if r['status'] in ('unresolved', 'not_filmed'): continue rec[int(r['idx'])] = dict(per=int(r['period']), irqn=int(r['irqn']), irqfirst=int(r['irqfirst']), vend=int(r['vblend'])) return rec, (140, 175, 210, 240) def estimate(rec, bands, P4): lo0, hi0, lo1, hi1 = bands def band(v): if v['irqn'] < 1 or v['irqfirst'] == 65535: return None g = v['irqfirst'] - v['vend'] return 0 if lo0 <= g <= hi0 else 1 if lo1 <= g <= hi1 else None X = [v['per'] for v in rec.values()] B = [rec[k]['per'] for k in rec if (k + 1) in rec and band(rec[k + 1]) == 1 and k != 60] L, U = q(B, .05), q(B, .95) c = (L + U) / 2 m = st.mean(X) cat = lambda p: 'low' if p < L - 25 else 'high' if p > U + 5 else 'main' pairs = collections.Counter((cat(rec[k]['per']), cat(rec[k + 1]['per'])) for k in rec if (k + 1) in rec) main = [x for x in X if cat(x) == 'main'] lows = [x for x in X if cat(x) == 'low'] shift = st.median(main) - st.median(lows) out = dict(n=len(X), mean=round(m, 2), se=round(st.pstdev(X) / len(X) ** .5, 2), band=[L, U], centre=c, n_band=len(B), shift=shift, p_high=sum(1 for x in X if cat(x) == 'high') / len(X), p_onesided=(pairs[('main', 'low')] + pairs[('low', 'low')]) / len(X), p_alllow=len(lows) / len(X), high_then_low=pairs[('high', 'low')], high=sum(v for (a, b), v in pairs.items() if a == 'high')) est = {} for lab, pB in (('above_band', out['p_high']), ('one_sided_low', out['p_onesided']), ('all_low', out['p_alllow'])): for ap in (6, 10): est[f'{lab}_a{ap}'] = P4 + m + shift * pB - c + ap / 2 out['F'] = {k: round(v, 1) for k, v in est.items()} return out def main(): OUT.mkdir(parents=True, exist_ok=True) res = {} cases = [('emulator build24 (FRT frame 52,436.9; P4 52,428)', load_emu('attract_probe_b24'), 52428, 52436.9), ('emulator build26 (FRT frame 52,433.24; P4 52,431)', load_emu('attract_probe_b26'), 52431, 52433.24)] bias = collections.defaultdict(list) for name, (rec, bands), P4, truth in cases: r = estimate(rec, bands, P4) r['truth'] = truth r['error'] = {k: round(v - truth, 1) for k, v in r['F'].items()} for k, v in r['error'].items(): bias[k].append(v) res[name] = r rec, bands = load_board() for P4 in (52431, 52428): r = estimate(rec, bands, P4) corr = {} for k, v in r['F'].items(): b = st.mean(bias[k]) corr[k] = dict(raw=v, mean_emulator_bias=round(b, 1), corrected=round(v - b, 1), clocks=round(8 * (v - b)), ppm_vs_nominal=round(1e6 * (8 * (v - b) / NOMINAL - 1))) r['corrected'] = corr res[f'board P1 (P4 {P4})'] = r (OUT / 'frame_period.json').write_text(json.dumps(res, indent=1)) for name, r in res.items(): print('==', name, f"n={r['n']} mean={r['mean']} band={r['band']} c={r['centre']} shift={r['shift']} " f"P(high)={r['p_high']:.3f} P(1-sided low)={r['p_onesided']:.3f} P(all low)={r['p_alllow']:.3f}") for k, v in r['F'].items(): extra = f" err {r['error'][k]:+.1f}" if 'error' in r else \ f" bias-corrected {r['corrected'][k]['corrected']} = {r['corrected'][k]['clocks']} clk ({r['corrected'][k]['ppm_vs_nominal']:+d} ppm)" print(f' {k:20s} F={v:9.1f}{extra}') if __name__ == '__main__': main()