-- Record unique CPU PCs that read/write watched ranges. Env: -- TBG_LIB, TBG_LOAD, TBG_WATCH="7e2280-7e25ef,..." (CPU addrs), TBG_MODE=write|read, TBG_END, TBG_OUT (file), TBG_PLAN local L = dofile(os.getenv("TBG_LIB")) local out = os.getenv("TBG_OUT") local mode = os.getenv("TBG_MODE") or "write" local last = tonumber(os.getenv("TBG_END") or "600") local plan = L.inputPlan(os.getenv("TBG_PLAN")) local hits, frame = {}, 0 local cbType = mode == "read" and emu.callbackType.read or emu.callbackType.write if os.getenv("TBG_LOAD") then L.load(os.getenv("TBG_LOAD")) end for s, e in (os.getenv("TBG_WATCH") or ""):gmatch("(%x+)-(%x+)") do emu.addMemoryCallback(function(addr, value) local key = string.format("%06X", L.pc()) local h = hits[key] if not h then h = {n = 0, lo = addr, hi = addr, vals = {}, first = frame}; hits[key] = h end h.n = h.n + 1 if addr < h.lo then h.lo = addr end if addr > h.hi then h.hi = addr end h.vals[value] = true end, cbType, tonumber(s, 16), tonumber(e, 16), emu.cpuType.snes) end emu.addEventCallback(function() local pressed = {[0] = {}, [1] = {}} for _, e in ipairs(plan) do if frame >= e.f and frame < e.f + e.hold then for b in e.b:gmatch("[^+]+") do pressed[e.port][b] = true end end end emu.setInput(pressed[0], 0); emu.setInput(pressed[1], 1) end, emu.eventType.inputPolled) emu.addEventCallback(function() frame = frame + 1 if frame >= last then local keys = {} for k in pairs(hits) do keys[#keys + 1] = k end table.sort(keys) local h = io.open(out, "w") for _, k in ipairs(keys) do local v = {} for x in pairs(hits[k].vals) do v[#v + 1] = x end table.sort(v) local s = {} for i = 1, math.min(#v, 16) do s[i] = string.format("%02X", v[i]) end if #v > 16 then s[#s + 1] = "..." end h:write(string.format("%s n=%d first=%d range=%06X-%06X vals=%s\n", k, hits[k].n, hits[k].first, hits[k].lo, hits[k].hi, table.concat(s, ","))) end h:close() emu.stop(0) end end, emu.eventType.endFrame)