Files

50 lines
2.2 KiB
Lua

-- drive.lua variant whose savestate save/load works: Mesen only allows (create|load)Savestate inside an
-- exec callback, so do it from an exec hook on the NMI stub 00:81B6.
-- Env: TBG_OUT, TBG_PLAN="frame:btn+btn,...", TBG_HOLD (frames each press is held, default 4),
-- TBG_SHOTS="f1,f2", TBG_WRAM="f1,f2" (dump WRAM), TBG_END, TBG_SAVE_AT, TBG_LOAD, TBG_PORT (default 0)
local out = os.getenv("TBG_OUT")
local plan, shots, wram = {}, {}, {}
for f, b in (os.getenv("TBG_PLAN") or ""):gmatch("(%d+):([%w+]+)") do plan[tonumber(f)] = b end
for f in (os.getenv("TBG_SHOTS") or ""):gmatch("%d+") do shots[tonumber(f)] = true end
for f in (os.getenv("TBG_WRAM") or ""):gmatch("%d+") do wram[tonumber(f)] = true end
local last = tonumber(os.getenv("TBG_END") or "600")
local saveAt = tonumber(os.getenv("TBG_SAVE_AT") or "-1")
local hold = tonumber(os.getenv("TBG_HOLD") or "4")
local port = tonumber(os.getenv("TBG_PORT") or "0")
local loadFrom = os.getenv("TBG_LOAD")
local frame, held, holdLeft = 0, nil, 0
local wantLoad, wantSave = loadFrom ~= nil, false
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(out .. "/state.mss", "wb"); h:write(emu.createSavestate()); h:close(); wantSave = false
end
end, emu.callbackType.exec, 0x0081B6)
emu.addEventCallback(function()
if held and holdLeft > 0 then
local t = {}
for b in held:gmatch("[^+]+") do t[b] = true end
emu.setInput(t, port)
end
end, emu.eventType.inputPolled)
emu.addEventCallback(function()
if wantLoad then return end
frame = frame + 1
holdLeft = holdLeft - 1
if plan[frame] then held, holdLeft = plan[frame], hold end
if frame == saveAt then wantSave = true end
if shots[frame] then
local h = io.open(string.format("%s/f%05d.png", out, frame), "wb"); h:write(emu.takeScreenshot()); h:close()
end
if wram[frame] then
local w = {}
for i = 0, 0x1FFFF do w[#w + 1] = string.char(emu.read(i, emu.memType.snesWorkRam)) end
local h = io.open(string.format("%s/f%05d.wram", out, frame), "wb"); h:write(table.concat(w)); h:close()
end
if frame >= last then emu.stop(0) end
end, emu.eventType.endFrame)