Files

42 lines
1.8 KiB
Lua

-- Log frame + A on entry to listed functions. Env: TBG_ADDRS="808554,83ECE5,..." plus drive.lua env (TBG_PLAN/TBG_END/TBG_OUT/TBG_LOAD).
local out = os.getenv("TBG_OUT")
local log = io.open(out .. "/trace.txt", "w")
local frame = 0
local counts = {}
for a in (os.getenv("TBG_ADDRS") or ""):gmatch("%x+") do
local addr = tonumber(a, 16)
emu.addMemoryCallback(function()
counts[addr] = (counts[addr] or 0) + 1
if counts[addr] <= (tonumber(os.getenv("TBG_MAXHITS") or "3")) then
local s = emu.getState()
log:write(string.format("f%05d %06X A=%04X X=%04X Y=%04X\n", frame, addr, s["cpu.a"], s["cpu.x"], s["cpu.y"]))
log:flush()
end
end, emu.callbackType.exec, addr)
end
local plan = {}
for f, b in (os.getenv("TBG_PLAN") or ""):gmatch("(%d+):([%w+]+)") do plan[tonumber(f)] = b end
local shots = {}
for f in (os.getenv("TBG_SHOTS") or ""):gmatch("%d+") do shots[tonumber(f)] = true end
local last = tonumber(os.getenv("TBG_END") or "600")
local held, holdLeft = nil, 0
local loadFrom, loaded = os.getenv("TBG_LOAD"), false
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 loadFrom and not loaded then local h = io.open(loadFrom, "rb"); emu.loadSavestate(h:read("a")); h:close(); loaded = true end
frame = frame + 1
holdLeft = holdLeft - 1
if plan[frame] then held, holdLeft = plan[frame], 4 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
for a, c in pairs(counts) do log:write(string.format("count %06X %d\n", a, c)) end
log:close(); emu.stop(0)
end
end, emu.eventType.endFrame)