-- Generalised power experiment. P1 casts TBG_POWER (hex, or "none" for a baseline) from TBG_LOAD. -- Env: -- TBG_LIB, TBG_LOAD, TBG_OUT (dir) -- TBG_POWER hex power id or "none" -- TBG_SPAN frames to record after the cast (default 1500) -- TBG_P1HOLD buttons P1 holds after the cast ("down" default, "" for idle), '+'-joined -- TBG_P1PLAN extra P1 presses relative to cast: "rel:btn+btn[/hold],..." -- TBG_P2PLAN / TBG_P2HOLD same for port 2 (P2 is human only with TBG_POKE=DE=0) -- TBG_B1 / TBG_B2 220-byte files written into P1/P2 logic playfields ($0296 / $04F6) at cast time -- TBG_WATCH extra addresses "addr,addr,..." (hex, WRAM) logged every frame -- TBG_POKE "addr=val,..." written at cast time (hex) -- TBG_POKEREL "rel:addr=val+addr=val,..." pokes at frames relative to the cast (hex addr/val) -- TBG_HOLDPOKE "addr=val,..." written every frame after the cast (hex) -- TBG_CLEARP1TOP N: zero the top N playfield rows of P1 every frame (keeps P1 alive while it soft-drops) -- TBG_SHOTREL frames (relative) for screenshot + WRAM dump -- TBG_CDL 1 = dump PRG CDL to OUT/cdl.bin at the end local L = dofile(os.getenv("TBG_LIB")) local out = os.getenv("TBG_OUT") local W = emu.memType.snesWorkRam 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 "1500") local hold = os.getenv("TBG_P1HOLD") or "down" local rel = {} for f in (os.getenv("TBG_SHOTREL") or ""):gmatch("%d+") do rel[tonumber(f)] = true end local plan = L.inputPlan(os.getenv("TBG_P1PLAN")) local plan2 = L.inputPlan(os.getenv("TBG_P2PLAN")) -- P2 presses relative to cast (port 2; needs TBG_POKE=DE=0 for a human P2) local hold2 = os.getenv("TBG_P2HOLD") or "" local watch = {} for a in (os.getenv("TBG_WATCH") or ""):gmatch("%x+") do watch[#watch + 1] = tonumber(a, 16) end 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 pokerel = {} for item in (os.getenv("TBG_POKEREL") or ""):gmatch("[^,]+") do local f, rest = item:match("^(%d+):(.+)$") if f then pokerel[tonumber(f)] = rest end end local b1, b2 = readfile(os.getenv("TBG_B1")), readfile(os.getenv("TBG_B2")) local base = {0x20D, 0x229, 0x22A, 0x221, 0x222, 0x228, 0x46D, 0x489, 0x48A, 0x481, 0x482, 0x488, 0x16F, 0x16B, 0x16C, 0x16D, 0xF6, 0xF7, 0x19C} local header = "rel P1st P1pw P1d28 P1mt P1stk P1s26 P2st P2pw P2d28 P2mt P2stk P2s26 q16F q16B q16C q16D lvF6 lvF7 g19C" for _, a in ipairs(watch) do header = header .. string.format(" w%04X", a) end local frame, castAt, pressUntil = 0, nil, -1 local log = io.open(out .. "/log.txt", "w"); log:write(header .. "\n") local bf = io.open(out .. "/boards.bin", "wb") L.load(os.getenv("TBG_LOAD")) emu.addEventCallback(function() local t = {} if castAt then for b in hold:gmatch("[^+]+") do t[b] = true end local r = frame - castAt for _, e in ipairs(plan) do if r >= e.f and r < e.f + e.hold then for b in e.b:gmatch("[^+]+") do t[b] = true end end end end if frame < pressUntil then t = {up = true} end local t2 = {} if castAt then for b in hold2:gmatch("[^+]+") do t2[b] = true end local r = frame - castAt for _, e in ipairs(plan2) do if r >= e.f and r < e.f + e.hold then for b in e.b:gmatch("[^+]+") do t2[b] = true end end end end emu.setInput(t, 0) emu.setInput(t2, 1, 1) -- Mesen 2.1.1: port 2 needs the third argument (with (t, 1) the input goes to port 1) end, emu.eventType.inputPolled) emu.addEventCallback(function() frame = frame + 1 if not castAt and frame > 3 and 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(0x231, pid, W) emu.write(0x221, 1, W) pressUntil = frame + 4 end castAt = frame end if castAt then local r = frame - castAt for a, v in (os.getenv("TBG_HOLDPOKE") or ""):gmatch("(%x+)=(%x+)") do emu.write(tonumber(a, 16), tonumber(v, 16), W) end local ct = tonumber(os.getenv("TBG_CLEARP1TOP") or "0") for i = 0, ct * 10 - 1 do emu.write(0x296 + i, 0, W) end if pokerel[r] then for a, v in pokerel[r]:gmatch("(%x+)=(%x+)") do emu.write(tonumber(a, 16), tonumber(v, 16), W) end end local t = {} for _, a in ipairs(base) do t[#t + 1] = string.format("%02X", emu.read(a, W)) end for _, a in ipairs(watch) do t[#t + 1] = string.format("%02X", emu.read(a, W)) end log:write(r .. " " .. table.concat(t, " ") .. "\n") local b = {} for i = 0, 219 do b[#b + 1] = string.char(emu.read(0x296 + i, W)) end for i = 0, 219 do b[#b + 1] = string.char(emu.read(0x4F6 + i, W)) end bf:write(table.concat(b)) if rel[r] then L.shot(string.format("%s/r%04d.png", out, r)) end if r >= span then log:close(); bf:close() if os.getenv("TBG_CDL") == "1" then local d = emu.getCdlData(emu.memType.snesPrgRom) local c = {} for i = 0, #d do c[#c + 1] = string.char(d[i] or 0) end local h = io.open(out .. "/cdl.bin", "wb"); h:write(table.concat(c)); h:close() end emu.stop(0) end end if frame > span + 3000 then log:close(); bf:close(); emu.stop(1) end end, emu.eventType.endFrame)