-- Log every call of the decompressor 80:B7DA (A=src bank | dst bank<<8, X=src, Y=dst) and dump its output -- (dst bank, Y0..Yend) when it returns at 80:B868. Env: TBG_LIB, TBG_OUT, TBG_PLAN, TBG_END. local L = dofile(os.getenv("TBG_LIB")) local out = os.getenv("TBG_OUT") local plan = L.inputPlan(os.getenv("TBG_PLAN")) local last = tonumber(os.getenv("TBG_END") or "4300") local frame, n = 0, 0 local cur local log = io.open(out .. "/calls.txt", "w") local function onEntry() local s = emu.getState() cur = {a = s["cpu.a"], x = s["cpu.x"], y = s["cpu.y"], f = frame, caller = 0} -- return address on the stack (JSL pushes 3 bytes; S points below them) local sp = s["cpu.sp"] local r = emu.read(sp + 1, emu.memType.snesMemory) | (emu.read(sp + 2, emu.memType.snesMemory) << 8) | (emu.read(sp + 3, emu.memType.snesMemory) << 16) cur.caller = r - 3 end local function onExit() if not cur then return end local s = emu.getState() local srcbank, dstbank = cur.a & 0xFF, (cur.a >> 8) & 0xFF local y0, y1 = cur.y, s["cpu.y"] n = n + 1 log:write(string.format("%d f%05d caller=%06X src=%02X%04X dst=%02X%04X end=%04X\n", n, cur.f, cur.caller, srcbank, cur.x, dstbank, y0, y1)) local t = {} local base = (dstbank == 0x7F and 0x10000 or 0) + y0 for i = 0, (y1 - y0) do t[#t + 1] = string.char(emu.read(base + i, emu.memType.snesWorkRam)) end local h = io.open(string.format("%s/call%03d.bin", out, n), "wb"); h:write(table.concat(t)); h:close() cur = nil end for _, b in ipairs({0x00, 0x80}) do emu.addMemoryCallback(onEntry, emu.callbackType.exec, (b << 16) | 0xB7DA, (b << 16) | 0xB7DA) emu.addMemoryCallback(onExit, emu.callbackType.exec, (b << 16) | 0xB868, (b << 16) | 0xB868) end emu.addEventCallback(function() 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() frame = frame + 1 if frame >= last then log:close(); emu.stop(0) end end, emu.eventType.endFrame)