-- Record every executed ROM PC with its M/X/E flags ("BBAAAA ps e"), from boot with scripted input -- (or from TBG_LOAD). Env: TBG_OUT, TBG_END, TBG_PLAN, TBG_HOLD, TBG_LOAD, TBG_SHOTS, TBG_SAVE_AT local out, last = os.getenv("TBG_OUT"), tonumber(os.getenv("TBG_END") or "300") local plan, shots = {}, {} for f, b in (os.getenv("TBG_PLAN") or ""):gmatch("(%d+):([%w+]+)") do plan[tonumber(f)] = b end for f in (os.getenv("TBG_SHOTS") or ""):gmatch("%d+") do shots[tonumber(f)] = true end local hold = tonumber(os.getenv("TBG_HOLD") or "4") local saveAt = tonumber(os.getenv("TBG_SAVE_AT") or "-1") local loadFrom = os.getenv("TBG_LOAD") local frame, held, holdLeft = 0, nil, 0 local wantLoad, wantSave = loadFrom ~= nil, false local seen, seenPc = {}, {} emu.addMemoryCallback(function(addr) if wantLoad or wantSave then if addr ~= 0x0081B6 then return end if wantLoad then local h = io.open(loadFrom, "rb"); emu.loadSavestate(h:read("a")); h:close(); wantLoad = false else local h = io.open(out .. "/state.mss", "wb"); h:write(emu.createSavestate()); h:close(); wantSave = false end return end if seenPc[addr] then return end seenPc[addr] = true local s = emu.getState() local key = addr * 512 + (s["cpu.ps"] & 0x30) * 2 + (s["cpu.emulationMode"] and 1 or 0) if not seen[key] then seen[key] = frame end end, emu.callbackType.exec, 0x008000, 0xFFFFFF) emu.addEventCallback(function() if held and holdLeft > 0 then local t = {} for b in held:gmatch("[^+]+") do t[b] = true end emu.setInput(t, 0) end end, emu.eventType.inputPolled) emu.addEventCallback(function() if wantLoad then return end frame = frame + 1 if frame % 50 == 0 then print("frame", frame) end holdLeft = holdLeft - 1 if plan[frame] then held, holdLeft = plan[frame], hold end if frame == saveAt then wantSave = true end if shots[frame] then local h = io.open(string.format("%s/f%05d.png", out, frame), "wb"); h:write(emu.takeScreenshot()); h:close() end if frame >= last then local h = io.open(out .. "/cov.txt", "w") for k, f in pairs(seen) do h:write(string.format("%06X %02X %d %d\n", k // 512, (k % 512) // 2, k % 2, f)) end h:close(); emu.stop(0) end end, emu.eventType.endFrame)