-- margin_probe.lua: census_probe.lua plus a read-only per-frame copy of the P1 probe's current -- record (0207A900, 24 bytes, through the 22000000 mirror) at the same sample point, so the margin of every -- frame of the unarmed power-on trajectory is available after the ring freezes. Paper analysis 2026-10-16. -- Slowdown census probe. -- Every frame, right after the emulator's frame loop asserts IRQ12 (the VBlank handler has not run yet), -- sample the eight scheduler task slots at 0200D794 (stride 64, state byte at offset 0) and the match -- context, through the tested 22000000 cache-through mirror. Slot 3 is the game task: state 3 (active) -- or 4 (forced runnable) at VBlank means the game pass did not finish inside the frame. -- Episode mode replays research_config.lua inputs exactly as roundtrip_probe.lua does (step 1..frames). -- Power-on mode holds every control released. Probe mode (P1 board probe image, build09) checks the -- ROM patch words, optionally arms the probe's private header for continuous capture, and drains the -- 24-byte record ring (0207AC00, 700 records) as it fills. local cfg = dofile('research_config.lua') local M = 0x20000000 local function u8(a) return memory.readbyte(a + M) end local function u16(a) return memory.readword(a + M) end local function u32(a) return memory.readdword(a + M) % 4294967296 end local status = assert(io.open('results/status.txt', 'w')) status:write('started=1\n'); status:flush() local controls = {'Up','Down','Left','Right','Weak Punch','Medium Punch','Strong Punch', 'Weak Kick','Medium Kick','Strong Kick','Start','Coin'} local fixed = joypad.get() for name, value in pairs(fixed) do if type(value) == 'boolean' then fixed[name] = false end end if cfg.probe then local patches = dofile('patch.lua') for _, p in ipairs(patches) do assert(memory.readdword(p[1]) % 4294967296 == p[2], string.format('patch readback %08X', p[1])) end status:write('patch_words=', #patches, '\n'); status:flush() if cfg.arm then -- Header as the boot page leaves it (e2e_probe.py probe_init), then armed for a continuous ring: -- CAPST 2 skips the auto-capture freeze, so records keep wrapping at 700. Written before any -- frame runs: the imported state starts with an empty cache, so the CPU sees these bytes. for off = 0, 0x1FC, 4 do memory.writedword(0x2207A800 + off, 0) end memory.writedword(0x2207A85C, -1) -- FIRSTOVR memory.writeword(0x2207A904, 0xFFFF) -- CUR.PASSEND memory.writeword(0x2207A910, 0xFFFF) -- CUR.IRQFIRST memory.writeword(0x2207A916, 0xFFFF) -- CUR.PASSSTART memory.writedword(0x2207A80C, 1) -- ARMED memory.writedword(0x2207A860, 2) -- CAPST memory.writedword(0x2207A800, 0x50524231) -- MAGIC "PRB1" last assert(u32(0x0207A800) == 0x50524231 and u32(0x0207A80C) == 1 and u32(0x0207A860) == 2, 'header init readback') end end local out = assert(io.open('results/census.tsv', 'w')) out:write('step\tframe\ts0\ts1\ts2\ts3\ts4\ts5\ts6\ts7\tlive\tgno\tphase\tcno\tp1c\tp2c\tround\tstage\twit\ttimer\tslow\tfade\n') local overruns, live_frames = 0, 0 local function sample(step) local f = u32(0x02007F00) local s = {} for i = 0, 7 do s[i + 1] = u8(0x0200D794 + 64 * i) end local live = u8(0x02011389) if live ~= 0 then live_frames = live_frames + 1 end if s[4] == 3 or s[4] == 4 then overruns = overruns + 1 end out:write(step, '\t', f, '\t', table.concat(s, '\t'), '\t', live, '\t', u16(0x02015436), '\t', u16(0x02015438), '\t', u16(0x020154A6), '\t', u8(0x02011387), '\t', u8(0x02011388), '\t', u8(0x0201138A), '\t', u16(0x02026BF6), '\t', u16(0x020155E8), '\t', u8(0x02028679), '\t', u8(0x02068C66), '\t', u8(0x0201138D), '\n') end local rec, seen = nil, 0 if cfg.probe then rec = assert(io.open('results/records.tsv', 'w')) rec:write('step\tk\tframe\tperiod\tpassend\tvblend\tsnd\tsprwait\tirqticks\tirqn\tflags\tirqfirst\tdmau\twit\tpassstart\n') end local function drain(step) local widx = u32(0x0207A804) if widx < seen then seen = 0 end -- the probe re-armed (auto capture reset WIDX) while seen < widx do local base = 0x0207AC00 + 24 * (seen % 700) local b = memory.readbyterange(base + M, 24) local function w(o) return b[o + 1] * 256 + b[o + 2] end rec:write(step, '\t', seen, '\t', w(0), '\t', w(2), '\t', w(4), '\t', w(6), '\t', w(8), '\t', w(10), '\t', w(12), '\t', b[15], '\t', b[16], '\t', w(16), '\t', w(18), '\t', w(20), '\t', w(22), '\n') seen = seen + 1 end end local function advance(step) local input = {} for name, value in pairs(fixed) do input[name] = value end if not cfg.poweron then for index, control in ipairs(controls) do local bit = 2 ^ (index - 1) input['P1 ' .. control] = math.floor(cfg.p1[step] / bit) % 2 == 1 input['P2 ' .. control] = math.floor(cfg.p2[step] / bit) % 2 == 1 end end joypad.set(input) emu.frameadvance() local actual = joypad.get() for name, value in pairs(input) do assert(actual[name] == value, 'input mismatch: ' .. name) end end local curout = nil if cfg.probe then curout = assert(io.open('results/cur.tsv', 'w')) curout:write('step\tframe\tperiod\tpassend\tvblend\tsnd\tflags\tirqn\tirqfirst\tdmau\tpassstart\tframes_hdr\tovr3\tlive_hdr\n') end local function curread(step) local b = memory.readbyterange(0x0207A900 + M, 24) local function w(o) return b[o + 1] * 256 + b[o + 2] end curout:write(step, '\t', u32(0x02007F00), '\t', w(2), '\t', w(4), '\t', w(6), '\t', w(8), '\t', b[16], '\t', b[15], '\t', w(16), '\t', w(18), '\t', w(22), '\t', u32(0x0207A810), '\t', u32(0x0207A818), '\t', u32(0x0207A814), '\n') end emu.speedmode('turbo') if not cfg.poweron then sample(0) end for step = 1, cfg.frames do advance(step) sample(step) if cfg.probe then drain(step) end if cfg.probe then curread(step) end if step % 600 == 0 then out:flush(); if rec then rec:flush() end end end out:close() if curout then curout:close() end if rec then rec:write('#end\tseen=', seen, '\twidx=', u32(0x0207A804), '\tfrozen=', u32(0x0207A808), '\tcapst=', u32(0x0207A860), '\tframes_hdr=', u32(0x0207A810), '\tovr3=', u32(0x0207A818), '\tovro=', u32(0x0207A81C), '\tnstale=', u32(0x0207A940), '\n') rec:close() end status:write('frames=', cfg.frames, '\noverruns=', overruns, '\nlive=', live_frames, '\nrecords=', seen, '\nok=1\n') status:close() os.exit(0)