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

95 lines
4.1 KiB
Lua

-- Cast a power from a savestate and record what graphics the cut-in loads.
-- Env: TBG_LIB, TBG_LOAD (savestate), TBG_OUT, TBG_POKE "addr=val,..." (hex, applied on frame 2),
-- TBG_CAST frame to press Up (default 5), TBG_SHOTS "f1,f2" dump frames, TBG_END, TBG_OAMLOG=1 (OAM every frame).
local L = dofile(os.getenv("TBG_LIB"))
local out = os.getenv("TBG_OUT")
local last = tonumber(os.getenv("TBG_END") or "300")
local castf = tonumber(os.getenv("TBG_CAST") or "5")
local shots = {}
for f in (os.getenv("TBG_SHOTS") or ""):gmatch("%d+") do shots[tonumber(f)] = true end
local frame, loaded = 0, false
local log = io.open(out .. "/log.txt", "w")
local oamlog = os.getenv("TBG_OAMLOG") and io.open(out .. "/oam.bin", "wb") -- 0x220 bytes per frame
local M = emu.memType.snesMemory
local vaddr = 0
local cur
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(0x420B, function(a, v)
for ch = 0, 7 do
if (v >> ch) & 1 == 1 then
local b = 0x4300 + ch * 16
local bb = emu.read(b + 1, M)
local src = emu.read(b + 2, M) | (emu.read(b + 3, M) << 8) | (emu.read(b + 4, M) << 16)
local n = emu.read(b + 5, M) | (emu.read(b + 6, M) << 8)
if bb == 0x18 or bb == 0x19 then
log:write(string.format("f%04d DMA pc=%06X src=%06X n=%04X vaddr=%04X\n", frame, L.pc(), src, n, vaddr))
end
end
end
end)
for _, b 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 = (emu.read(sp + 1, M) | (emu.read(sp + 2, M) << 8) | (emu.read(sp + 3, M) << 16)) - 3
end, emu.callbackType.exec, (b << 16) | 0xB7DA, (b << 16) | 0xB7DA)
emu.addMemoryCallback(function()
if not cur then return end
log:write(string.format("f%04d 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, (b << 16) | 0xB868, (b << 16) | 0xB868)
end
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
-- TBG_PRESSES="frame:btn,..." replaces the cast press (e.g. cursor moves on the character select).
local presses = {}
for f, b in (os.getenv("TBG_PRESSES") or ""):gmatch("(%d+):(%a+)") do presses[tonumber(f)] = b end
local usePresses = os.getenv("TBG_PRESSES") ~= nil
emu.addEventCallback(function()
if usePresses then
local t = {}
for d = 0, 3 do if presses[frame - d] then t[presses[frame - d]] = true end end
emu.setInput(t, 0)
return
end
emu.setInput(frame >= castf and frame < castf + 3 and {up = true} or {}, 0)
end, emu.eventType.inputPolled)
emu.addEventCallback(function()
if not loaded then L.load(os.getenv("TBG_LOAD")); 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 oamlog then
local t = {}
for i = 0, 0x21F do t[#t + 1] = string.char(emu.read(i, emu.memType.snesSpriteRam)) end
oamlog:write(table.concat(t))
end
if shots[frame] then
local p = string.format("%s/f%04d", out, frame)
dumpmem(p .. ".vram", emu.memType.snesVideoRam, 0x10000)
dumpmem(p .. ".cgram", emu.memType.snesCgRam, 0x200)
dumpmem(p .. ".oam", emu.memType.snesSpriteRam, 0x220)
L.shot(p .. ".png")
local st = emu.getState()
local h = io.open(p .. ".ppu.txt", "w")
for k, v in pairs(st) do if k:find("^ppu%.") and not k:find("Buffer") then h:write(k, "=", tostring(v), "\n") end end
h:close()
end
if frame >= last then log:close(); if oamlog then oamlog:close() end; emu.stop(0) end
end, emu.eventType.endFrame)