Files
2026-10-21 07:34:29 -05:00

45 lines
2.0 KiB
Lua

-- Which PCs write a WRAM range during a frame window? Env: TBG_LIB, TBG_OUT, TBG_PLAN, TBG_LO/TBG_HI (24-bit),
-- TBG_F0/TBG_F1 (frame window). Optional: TBG_LOAD savestate, TBG_POKE "addr=val" (frame 2), TBG_CAST frame to press Up.
local L = dofile(os.getenv("TBG_LIB"))
local out = os.getenv("TBG_OUT")
local plan = L.inputPlan(os.getenv("TBG_PLAN"))
local lo, hi = tonumber(os.getenv("TBG_LO")), tonumber(os.getenv("TBG_HI"))
local f0, f1 = tonumber(os.getenv("TBG_F0")), tonumber(os.getenv("TBG_F1"))
local frame = 0
local pcs = {}
local loadp = os.getenv("TBG_LOAD")
local loaded = false
for a, v in (os.getenv("TBG_POKE") or ""):gmatch("(%x+)=(%x+)") do end
emu.addMemoryCallback(function(a, v)
if frame < f0 or frame > f1 then return end
local pc = L.pc()
local e = pcs[pc]
if not e then e = {n = 0, lo = a, hi = a, f = frame}; pcs[pc] = e end
e.n = e.n + 1
if a < e.lo then e.lo = a end
if a > e.hi then e.hi = a end
end, emu.callbackType.write, lo, hi)
local castf = tonumber(os.getenv("TBG_CAST") or "-1")
emu.addEventCallback(function()
if castf >= 0 then emu.setInput(frame >= castf and frame < castf + 3 and {up = true} or {}, 0); return end
local t = {}
for _, e in ipairs(plan) do if frame >= e.f and frame < e.f + e.hold and e.port == 0 then for b in e.b:gmatch("[^+]+") do t[b] = true end end end
emu.setInput(t, 0)
end, emu.eventType.inputPolled)
emu.addEventCallback(function()
if loadp and not loaded then L.load(loadp); loaded = true end
frame = frame + 1
if frame == 2 then
for a, v in (os.getenv("TBG_POKE") or ""):gmatch("(%x+)=(%x+)") do emu.write(tonumber(a, 16), tonumber(v, 16), emu.memType.snesWorkRam) end
end
if frame > f1 then
local h = io.open(out .. "/wtrace.txt", "w")
local keys = {}
for k in pairs(pcs) do keys[#keys + 1] = k end
table.sort(keys)
for _, k in ipairs(keys) do local e = pcs[k]
h:write(string.format("pc=%06X n=%6d range=%06X-%06X first=f%05d\n", k, e.n, e.lo, e.hi, e.f)) end
h:close(); emu.stop(0)
end
end, emu.eventType.endFrame)