146 lines
7.2 KiB
Lua
146 lines
7.2 KiB
Lua
-- Screen timeline capture for the non-battle screens.
|
|
-- Per frame: PPU state line (state.txt), OAM (oam.bin, 544 B/frame), CGRAM (cgram.bin, 512 B/frame).
|
|
-- Events: DMA / HDMA-enable / decompressor calls / first PPU-register write PC per frame (events.txt).
|
|
-- Snapshots: VRAM every TBG_VEVERY frames (default 20) + TBG_DUMPS, screenshots every TBG_SEVERY (default 10).
|
|
-- Env: TBG_LIB, TBG_OUT, TBG_PLAN (battle_lib syntax), TBG_END, TBG_DUMPS="f1,f2",
|
|
-- TBG_POKES="f1-f2:addr=val+addr=val,..." (WRAM bytes written every frame in [f1,f2]),
|
|
-- TBG_LOAD savestate (loaded at the first NMI), TBG_SAVES="f1,f2" (state at those frames).
|
|
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 "3000")
|
|
local vevery = tonumber(os.getenv("TBG_VEVERY") or "20")
|
|
local severy = tonumber(os.getenv("TBG_SEVERY") or "10")
|
|
local dumps, saves = {}, {}
|
|
for f in (os.getenv("TBG_DUMPS") or ""):gmatch("%d+") do dumps[tonumber(f)] = true end
|
|
for f in (os.getenv("TBG_SAVES") or ""):gmatch("%d+") do saves[tonumber(f)] = true end
|
|
local pokes = {}
|
|
for f1, f2, body in (os.getenv("TBG_POKES") or ""):gmatch("(%d+)-(%d+):([^,]+)") do
|
|
local set = {}
|
|
for a, v in body:gmatch("(%x+)=(%x+)") do set[#set + 1] = {tonumber(a, 16), tonumber(v, 16)} end
|
|
pokes[#pokes + 1] = {tonumber(f1), tonumber(f2), set}
|
|
end
|
|
local M = emu.memType.snesMemory
|
|
local function rd(a) return emu.read(a, M) end
|
|
local ev = io.open(out .. "/events.txt", "w")
|
|
local st = io.open(out .. "/state.txt", "w")
|
|
local oamf = io.open(out .. "/oam.bin", "wb")
|
|
local cgf = io.open(out .. "/cgram.bin", "wb")
|
|
local frame = 0
|
|
local loadFrom = os.getenv("TBG_LOAD")
|
|
local wantLoad, wantSave = loadFrom ~= nil, nil
|
|
|
|
emu.addMemoryCallback(function()
|
|
if wantLoad then local h = io.open(loadFrom, "rb"); emu.loadSavestate(h:read("a")); h:close(); wantLoad = false
|
|
elseif wantSave then local h = io.open(wantSave, "wb"); h:write(emu.createSavestate()); h:close(); wantSave = nil end
|
|
end, emu.callbackType.exec, 0x0081B6)
|
|
|
|
local function onWrite(lo, hi, fn)
|
|
for _, base in ipairs({0x00, 0x80}) do
|
|
for bank = base, base + 0x3F do
|
|
emu.addMemoryCallback(fn, emu.callbackType.write, (bank << 16) | lo, (bank << 16) | hi)
|
|
end
|
|
end
|
|
end
|
|
local vaddr, vmain, caddr = 0, 0, 0
|
|
local seen = {}
|
|
onWrite(0x2100, 0x2133, function(a, v)
|
|
local r = a & 0xFFFF
|
|
if r == 0x2116 then vaddr = (vaddr & 0xFF00) | v elseif r == 0x2117 then vaddr = (vaddr & 0xFF) | (v << 8)
|
|
elseif r == 0x2115 then vmain = v elseif r == 0x2121 then caddr = v end
|
|
if not seen[r] then seen[r] = true; ev:write(string.format("f%05d W%04X=%02X pc=%06X\n", frame, r, v, L.pc())) end
|
|
end)
|
|
onWrite(0x420B, 0x420C, function(a, v)
|
|
local pc, hdma = L.pc(), (a & 0xFFFF) == 0x420C
|
|
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 hdma then
|
|
local key = string.format("%d:%06X:%02X:%02X", ch, src, dmap, bbad)
|
|
if not seen[key] then
|
|
seen[key] = true
|
|
local t = {}
|
|
for i = 0, 63 do t[#t + 1] = string.format("%02X", rd(src + i)) end
|
|
ev:write(string.format("f%05d HDMA pc=%06X ch%d dmap=%02X bb=21%02X tbl=%06X ind=%02X: %s\n", frame, pc, ch,
|
|
dmap, bbad, src, rd(b + 7), table.concat(t, " ")))
|
|
end
|
|
else
|
|
if n == 0 then n = 0x10000 end
|
|
ev:write(string.format("f%05d DMA 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
|
|
end)
|
|
local cur
|
|
for _, bk in ipairs({0x00, 0x80}) do
|
|
emu.addMemoryCallback(function()
|
|
local s = emu.getState()
|
|
local sp = s["cpu.sp"]
|
|
cur = {a = s["cpu.a"], x = s["cpu.x"], y = s["cpu.y"], 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
|
|
ev: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
|
|
|
|
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
|
|
local function blob(mt, size)
|
|
local t = {}
|
|
for i = 0, size - 1 do t[#t + 1] = string.char(emu.read(i, mt)) end
|
|
return table.concat(t)
|
|
end
|
|
|
|
local keys = {"bgMode", "mainScreenLayers", "subScreenLayers", "screenBrightness", "forcedBlank", "colorMathEnabled",
|
|
"colorMathAddSubscreen", "colorMathSubtractMode", "colorMathHalveResult", "fixedColor", "oamMode", "oamBaseAddress",
|
|
"oamAddressOffset", "mosaicEnabled", "mosaicSize", "mode1Bg3Priority",
|
|
"layers[0].hscroll", "layers[0].vscroll", "layers[0].chrAddress", "layers[0].tilemapAddress", "layers[0].doubleWidth", "layers[0].doubleHeight",
|
|
"layers[1].hscroll", "layers[1].vscroll", "layers[1].chrAddress", "layers[1].tilemapAddress", "layers[1].doubleWidth", "layers[1].doubleHeight",
|
|
"layers[2].hscroll", "layers[2].vscroll", "layers[2].chrAddress", "layers[2].tilemapAddress", "layers[2].doubleWidth", "layers[2].doubleHeight",
|
|
"mode7.matrix[0]", "mode7.matrix[1]", "mode7.matrix[2]", "mode7.matrix[3]", "mode7.centerX", "mode7.centerY",
|
|
"mode7.hscroll", "mode7.vscroll", "mode7.largeMap", "mode7.fillWithTile0"}
|
|
local header = false
|
|
|
|
emu.addEventCallback(function()
|
|
if wantLoad then return end
|
|
frame = frame + 1
|
|
seen = {}
|
|
for _, p in ipairs(pokes) do
|
|
if frame >= p[1] and frame <= p[2] then for _, s in ipairs(p[3]) do emu.write(s[1], s[2], emu.memType.snesWorkRam) end end
|
|
end
|
|
local s = emu.getState()
|
|
if not header then st:write("frame " .. table.concat(keys, " ") .. " nmi80 scene\n"); header = true end
|
|
local row = {string.format("%d", frame)}
|
|
for _, k in ipairs(keys) do row[#row + 1] = tostring(s["ppu." .. k]) end
|
|
local w = emu.memType.snesWorkRam
|
|
row[#row + 1] = string.format("%02X%02X%02X", emu.read(0x82, w), emu.read(0x81, w), emu.read(0x80, w))
|
|
row[#row + 1] = string.format("%02X", emu.read(0xDE, w))
|
|
st:write(table.concat(row, " ") .. "\n")
|
|
oamf:write(blob(emu.memType.snesSpriteRam, 0x220))
|
|
cgf:write(blob(emu.memType.snesCgRam, 0x200))
|
|
local p = string.format("%s/f%05d", out, frame)
|
|
if frame % vevery == 0 or dumps[frame] then dumpmem(p .. ".vram", emu.memType.snesVideoRam, 0x10000) end
|
|
if dumps[frame] then L.dumpWram(p .. ".wram") end
|
|
if frame % severy == 0 or dumps[frame] then L.shot(p .. ".png") end
|
|
if saves[frame] then wantSave = p .. ".mss" end
|
|
if frame >= last then ev:close(); st:close(); oamf:close(); cgf:close(); emu.stop(0) end
|
|
end, emu.eventType.endFrame)
|