-- Battle animation capture. From TBG_LOAD, P1 casts TBG_POWER (hex id, or "none"), then every frame records -- OAM, CGRAM and the BG1/BG2 tilemaps, logs VRAM DMAs, PPU register writes (window/colour math/scroll/ -- layers/brightness/mosaic, HDMA enables with channel setup) and the PCs that write the OAM shadow 7E:29F1. -- Env: TBG_LIB TBG_LOAD TBG_OUT TBG_POWER TBG_SPAN (frames after the cast, default 900) -- TBG_SHOTEVERY (screenshot every N frames, default 4; 0 = none) TBG_VRAMAT "rel,rel" full VRAM dumps -- TBG_HOLDPOKE "addr=val,..." written every frame after the cast (default 222=0,482=0: no garbage) -- TBG_POKE "addr=val,..." at cast time; TBG_B1/TBG_B2 220-byte playfields written at cast time -- TBG_P2HOLD buttons P2 holds after the cast ("" default); TBG_NOCAST=1 just records (for round-end etc.) -- TBG_CASTER=2: P2 casts instead (needs TBG_POKE=DE=0 so port 2 is a human). boards.bin: per frame the -- P1 + P2 logic playfields ($0296 / $04F6, 220 bytes each) local L = dofile(os.getenv("TBG_LIB")) local out = os.getenv("TBG_OUT") local W = emu.memType.snesWorkRam local M = emu.memType.snesMemory local pstr = os.getenv("TBG_POWER") or "none" local pid = pstr ~= "none" and tonumber(pstr, 16) or nil local span = tonumber(os.getenv("TBG_SPAN") or "900") local every = tonumber(os.getenv("TBG_SHOTEVERY") or "4") local vramat = {} for f in (os.getenv("TBG_VRAMAT") or ""):gmatch("%d+") do vramat[tonumber(f)] = true end local holdpoke = os.getenv("TBG_HOLDPOKE") or "222=0,482=0" local hold2 = os.getenv("TBG_P2HOLD") or "" local function readfile(p) if not p or p == "" then return nil end local h = io.open(p, "rb"); local d = h:read("a"); h:close(); return d end local b1, b2 = readfile(os.getenv("TBG_B1")), readfile(os.getenv("TBG_B2")) local frame, castAt, pressUntil = 0, nil, -1 local rel = -1 local caster2 = os.getenv("TBG_CASTER") == "2" local bf = io.open(out .. "/boards.bin", "wb") local fb = io.open(out .. "/frames.bin", "wb") -- per frame: OAM 0x220, CGRAM 0x200, BG1 map 0x800, BG2 map 0x800 local ev = io.open(out .. "/events.txt", "w") local st = io.open(out .. "/state.txt", "w") -- per frame game state local writers = {} -- frame -> set of PCs writing the OAM shadow local hb = io.open(out .. "/hdma.bin", "wb") -- per frame: 8 channels x (enabled, reg, mode, 224 lines x 4 bytes) local wb = io.open(out .. "/wram3.bin", "wb") -- per frame: 7E:3000-3FFF (effect state) local hdmaen = 0 local vaddr = 0 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(0x420C, function(a, v) hdmaen = v end) for _, r in ipairs({0x420B, 0x420C}) do onWrite(r, 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 r == 0x420C then hdmaen = v end if r == 0x420C then ev:write(string.format("%d HDMA ch%d ctl=%02X reg=21%02X table=%06X ind=%02X pc=%06X\n", rel, ch, emu.read(b, M), bb, src, emu.read(b + 7, M), L.pc())) elseif bb == 0x18 or bb == 0x19 then ev:write(string.format("%d VDMA vaddr=%04X src=%06X n=%04X pc=%06X\n", rel, vaddr, src, n, L.pc())) elseif bb == 0x22 and not (src == 0x7E2C11 and n == 0x200) then ev:write(string.format("%d CDMA src=%06X n=%04X pc=%06X\n", rel, src, n, L.pc())) end end end end) end -- PPU register writes worth knowing for effects (values logged when they change). local regs = {0x2100, 0x2101, 0x2105, 0x2106, 0x2107, 0x2108, 0x210D, 0x210E, 0x210F, 0x2110, 0x2111, 0x2112, 0x2123, 0x2124, 0x2125, 0x2126, 0x2127, 0x2128, 0x2129, 0x212A, 0x212B, 0x212C, 0x212D, 0x212E, 0x212F, 0x2130, 0x2131, 0x2132, 0x2133} local lastv = {} for _, r in ipairs(regs) do onWrite(r, function(a, v) local k = a & 0xFFFF if r == 0x210D or r == 0x210E or r == 0x210F or r == 0x2110 or r == 0x2111 or r == 0x2112 or r == 0x2132 or lastv[k] ~= v then lastv[k] = v ev:write(string.format("%d REG %04X=%02X pc=%06X\n", rel, k, v, L.pc())) end end) end -- OAM shadow writers (bank 7E and the low-RAM mirrors). local function oamw(a, v) local pc = L.pc() local s = writers[frame] if not s then s = {}; writers[frame] = s end s[pc] = true end emu.addMemoryCallback(oamw, emu.callbackType.write, 0x7E29F1, 0x7E2C10) for _, base in ipairs({0x00, 0x80}) do for bank = base, base + 0x3F do emu.addMemoryCallback(oamw, emu.callbackType.write, (bank << 16) | 0x29F1, (bank << 16) | 0x2C10) end end -- Walk each enabled HDMA channel's table like the hardware does and record the value(s) written for each of -- the 224 visible lines (up to 4 bytes per line; the register(s) depend on the transfer mode). local nbytes = {[0] = 1, 2, 2, 4, 4, 4, 2, 4} local function rdb(addr) return emu.read(addr & 0xFFFFFF, M) end local function hdma_lines() local rec = {} for ch = 0, 7 do local b = 0x4300 + ch * 16 local ctl, reg = emu.read(b, M), emu.read(b + 1, M) local en = (hdmaen >> ch) & 1 local lines = {} if en == 1 then local mode, indirect = ctl & 7, (ctl & 0x40) ~= 0 local nb = nbytes[mode] local tab = emu.read(b + 2, M) | (emu.read(b + 3, M) << 8) | (emu.read(b + 4, M) << 16) local ibank = emu.read(b + 7, M) local cur = {0, 0, 0, 0} local line, guard = 0, 0 while line < 224 and guard < 300 do guard = guard + 1 local cnt = rdb(tab); tab = tab + 1 if cnt == 0 then break end local rep, n = (cnt & 0x80) ~= 0, cnt & 0x7F if cnt == 0x80 then rep, n = false, 128 end local src if indirect then src = (ibank << 16) | rdb(tab) | (rdb(tab + 1) << 8); tab = tab + 2 else src = tab end for k = 0, n - 1 do if k == 0 or rep then for j = 1, nb do cur[j] = rdb(src + j - 1) end src = src + nb end if line < 224 then lines[line] = {cur[1], cur[2], cur[3], cur[4]} end line = line + 1 end if not indirect then tab = src end end end local t = {string.char(en, reg, ctl)} for l = 0, 223 do local v = lines[l] or {0, 0, 0, 0} t[#t + 1] = string.char(v[1], v[2], v[3], v[4]) end rec[#rec + 1] = table.concat(t) end return table.concat(rec) 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 local function rd(mt, a, n) local t = {}; for i = 0, n - 1 do t[#t + 1] = string.char(emu.read(a + i, mt)) end; return table.concat(t) end L.load(os.getenv("TBG_LOAD")) emu.addEventCallback(function() local t = {} if frame < pressUntil and not caster2 then t = {up = true} end emu.setInput(t, 0) local t2 = {} if castAt then for b in hold2:gmatch("[^+]+") do t2[b] = true end end if frame < pressUntil and caster2 then t2 = {up = true} end emu.setInput(t2, 1, 1) -- Mesen 2.1.1: port 2 needs the third argument end, emu.eventType.inputPolled) emu.addEventCallback(function() local w = writers[frame] or {} -- writes made during the frame that just ended writers[frame] = nil frame = frame + 1 if not castAt and frame > 3 and (os.getenv("TBG_NOCAST") == "1" or (emu.read(0x20D, W) == 5 and emu.read(0x46D, W) ~= 0x0B and emu.read(0x46D, W) ~= 0x0C)) then if b1 then for i = 1, 220 do emu.write(0x295 + i, b1:byte(i), W) end end if b2 then for i = 1, 220 do emu.write(0x4F5 + i, b2:byte(i), W) end end for a, v in (os.getenv("TBG_POKE") or ""):gmatch("(%x+)=(%x+)") do emu.write(tonumber(a, 16), tonumber(v, 16), W) end if pid then emu.write(caster2 and 0x491 or 0x231, pid, W) emu.write(caster2 and 0x481 or 0x221, 1, W) pressUntil = frame + 4 end castAt = frame dumpmem(out .. "/cast.vram", emu.memType.snesVideoRam, 0x10000) end if castAt then rel = frame - castAt for a, v in holdpoke:gmatch("(%x+)=(%x+)") do emu.write(tonumber(a, 16), tonumber(v, 16), W) end fb:write(rd(emu.memType.snesSpriteRam, 0, 0x220)) bf:write(rd(W, 0x296, 220)); bf:write(rd(W, 0x4F6, 220)) fb:write(rd(emu.memType.snesCgRam, 0, 0x200)) fb:write(rd(emu.memType.snesVideoRam, 0xF000, 0x800)) fb:write(rd(emu.memType.snesVideoRam, 0xF800, 0x800)) hb:write(hdma_lines()) wb:write(rd(W, 0x3000, 0x1000)) local s = {} for _, a in ipairs({0x20D, 0x229, 0x46D, 0x489, 0x221, 0x481, 0x8A, 0x8B, 0x8C, 0x224, 0x484}) do s[#s + 1] = string.format("%02X", emu.read(a, W)) end local pcs = {} for pc in pairs(w) do pcs[#pcs + 1] = string.format("%06X", pc) end table.sort(pcs) st:write(rel .. " " .. table.concat(s, " ") .. " | " .. table.concat(pcs, ",") .. "\n") if every > 0 and rel % every == 0 then L.shot(string.format("%s/s%04d.png", out, rel)) end if vramat[rel] then dumpmem(string.format("%s/v%04d.vram", out, rel), emu.memType.snesVideoRam, 0x10000) end if rel >= span then fb:close(); bf:close(); ev:close(); st:close(); hb:close(); wb:close(); emu.stop(0) end end if frame > span + 3000 then fb:close(); ev:close(); st:close(); emu.stop(1) end end, emu.eventType.endFrame)