-- Log every general-purpose DMA (write to $420B): channel regs, VRAM/CGRAM target, PC, frame. -- Env: TBG_OUT dir, TBG_PLAN input plan (battle_lib syntax), TBG_END last frame, TBG_DUMPS="f1,f2" full dumps. 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 "4200") local dumps = {} for f in (os.getenv("TBG_DUMPS") or ""):gmatch("%d+") do dumps[tonumber(f)] = true end local log = io.open(out .. "/dma.txt", "w") local frame = 0 local vaddr, vmain, caddr = 0, 0, 0 local M = emu.memType.snesMemory local function rd(a) return emu.read(a, M) end -- I/O is written through bank mirrors ($80:4xxx etc.), so hook every bank that maps it. local function onWrite(reg, fn) for _, base in ipairs({0x00, 0x80}) do for bank = base, base + 0x3F do local a = (bank << 16) | reg emu.addMemoryCallback(fn, emu.callbackType.write, a, a) end end end onWrite(0x2116, function(a, v) vaddr = (vaddr & 0xFF00) | v end) onWrite(0x2117, function(a, v) vaddr = (vaddr & 0x00FF) | (v << 8) end) onWrite(0x2115, function(a, v) vmain = v end) onWrite(0x2121, function(a, v) caddr = v end) onWrite(0x420B, function(a, v) local pc = L.pc() for ch = 0, 7 do if (v >> ch) & 1 == 1 then local b = 0x4300 + ch * 16 local dmap, bbad = rd(b), rd(b + 1) local src = rd(b + 2) | (rd(b + 3) << 8) | (rd(b + 4) << 16) local n = rd(b + 5) | (rd(b + 6) << 8) if n == 0 then n = 0x10000 end log:write(string.format("f%05d pc=%06X ch%d dmap=%02X bb=21%02X src=%06X n=%04X vaddr=%04X vmain=%02X cg=%02X\n", frame, pc, ch, dmap, bbad, src, n, vaddr, vmain, caddr)) end end end) -- Decompressor calls (80:B7DA ... 80:B868) in the same log, so ordering against DMAs is exact. local cur for _, bk in ipairs({0x00, 0x80}) do emu.addMemoryCallback(function() local s = emu.getState() cur = {a = s["cpu.a"], x = s["cpu.x"], y = s["cpu.y"]} local sp = s["cpu.sp"] cur.caller = (rd(sp + 1) | (rd(sp + 2) << 8) | (rd(sp + 3) << 16)) - 3 end, emu.callbackType.exec, (bk << 16) | 0xB7DA, (bk << 16) | 0xB7DA) emu.addMemoryCallback(function() if not cur then return end log:write(string.format("f%05d DECOMP caller=%06X src=%02X%04X dst=%02X%04X end=%04X\n", frame, cur.caller, cur.a & 0xFF, cur.x, (cur.a >> 8) & 0xFF, cur.y, emu.getState()["cpu.y"])) cur = nil end, emu.callbackType.exec, (bk << 16) | 0xB868, (bk << 16) | 0xB868) end local held = {} 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) local function dumpmem(path, mt, size) local t = {} for i = 0, size - 1 do t[#t + 1] = string.char(emu.read(i, mt)) end local h = io.open(path, "wb"); h:write(table.concat(t)); h:close() end emu.addEventCallback(function() frame = frame + 1 if dumps[frame] then local p = string.format("%s/f%05d", out, frame) dumpmem(p .. ".vram", emu.memType.snesVideoRam, 0x10000) dumpmem(p .. ".cgram", emu.memType.snesCgRam, 0x200) dumpmem(p .. ".oam", emu.memType.snesSpriteRam, 0x220) L.dumpWram(p .. ".wram") L.shot(p .. ".png") local st = emu.getState() local h = io.open(p .. ".ppu.txt", "w") local keys = {} for k, _ in pairs(st) do if k:find("^ppu") then keys[#keys + 1] = k end end table.sort(keys) for _, k in ipairs(keys) do h:write(k, "=", tostring(st[k]), "\n") end h:close() end if frame >= last then log:close(); emu.stop(0) end end, emu.eventType.endFrame)