Files

34 lines
1.7 KiB
Lua

-- Log every RNG call (81:81A2): $0122/$0123 on entry, A at the PLA (81:81C0), DBR, and the byte read by ADC $4000,X.
package.path = "/home/siege/Projects/tbg/re/lua/?.lua;" .. package.path
local L = require("mech_lib")
local last = tonumber(os.getenv("TBG_END") or "600")
local plan = L.plan(os.getenv("TBG_PLAN"))
local frame, held, holdLeft, running = 0, nil, 0, false
local h = io.open(os.getenv("TBG_OUT"), "w")
local cur
L.load(os.getenv("TBG_LOAD"), function()
running = true
emu.addMemoryCallback(L.guard(function()
local s = emu.getState()
cur = string.format("%05d %02X%02X dbr=%02X caller_sp=%04X", frame, emu.read(0x123, emu.memType.snesWorkRam), emu.read(0x122, emu.memType.snesWorkRam), s["cpu.dbr"], s["cpu.sp"])
end), emu.callbackType.exec, 0x8181A2, 0x8181A2)
emu.addMemoryCallback(L.guard(function()
local s = emu.getState()
local x = s["cpu.x"]
local ea = s["cpu.dbr"] * 0x10000 + 0x4000 + x
cur = cur .. string.format(" X'=%04X A1=%02X ea=%06X", x, s["cpu.a"] & 0xFF, ea)
end), emu.callbackType.exec, 0x8181BA, 0x8181BA)
emu.addMemoryCallback(L.guard(function()
h:write(cur, string.format(" new=%02X%02X\n", emu.read(0x123, emu.memType.snesWorkRam), emu.read(0x122, emu.memType.snesWorkRam)))
end), emu.callbackType.exec, 0x8181C0, 0x8181C0)
end)
emu.addEventCallback(L.guard(function()
if held and holdLeft > 0 then local t = {} for b in held:gmatch("[^+]+") do t[b] = true end emu.setInput(t, 0) end
end), emu.eventType.inputPolled)
emu.addEventCallback(L.guard(function()
if not running then return end
frame = frame + 1; holdLeft = holdLeft - 1
if plan[frame] then held, holdLeft = plan[frame][1], plan[frame][2] end
if frame >= last then h:close(); emu.stop(0) end
end), emu.eventType.endFrame)