Files

37 lines
1.5 KiB
Lua

-- Load savestate, run N frames: log writers of watched WRAM addrs and collect executed PCs.
-- Env: TBG_LOAD, TBG_OUT, TBG_END, TBG_WATCH="12B-12E,1F7,..." (hex, CPU addresses in bank 00)
local out, last = os.getenv("TBG_OUT"), tonumber(os.getenv("TBG_END") or "300")
local frame, loaded = 0, false
local pcs, writers = {}, {}
emu.addMemoryCallback(function()
if not loaded then local h = io.open(os.getenv("TBG_LOAD"), "rb"); emu.loadSavestate(h:read("a")); h:close(); loaded = true end
end, emu.callbackType.exec, 0x0081B6)
emu.addMemoryCallback(function(addr)
pcs[addr] = true
end, emu.callbackType.exec, 0x808000, 0x9FFFFF)
for r in (os.getenv("TBG_WATCH") or ""):gmatch("[%x%-]+") do
local a, b = r:match("(%x+)%-(%x+)")
a = tonumber(a or r, 16); b = tonumber(b or r, 16)
emu.addMemoryCallback(function(addr, value)
local s = emu.getState()
local k = string.format("%04X <- PC %02X:%04X", addr, s["cpu.k"], s["cpu.pc"])
writers[k] = (writers[k] or 0) + 1
end, emu.callbackType.write, a, b)
end
emu.addEventCallback(function()
if not loaded then return end
frame = frame + 1
if frame >= last then
local h = io.open(out .. "/pcs.txt", "w")
local t = {}
for a in pairs(pcs) do t[#t + 1] = a end
table.sort(t)
for _, a in ipairs(t) do h:write(string.format("%06X\n", a)) end
h:close()
h = io.open(out .. "/writers.txt", "w")
for k, c in pairs(writers) do h:write(k .. " x" .. c .. "\n") end
h:close()
emu.stop(0)
end
end, emu.eventType.endFrame)