Files

63 lines
2.7 KiB
Lua

-- Log every CPU-AI planner call (81:D9BA): WRAM $0000-$07FF at entry, workspace $0100-$01FF at exit
-- (81:DA7F, after PLD) and after 81:E1B6 (81:DA8D). Also min/max ROM address read in 81:E400-F2FF.
-- Env: TBG_LIB, TBG_LOAD (savestate), TBG_OUT (dir), TBG_FRAMES, TBG_P1RAND (seed; random P1 input),
-- TBG_POKES "addr=val,..." applied at frame 2 and every TBG_POKE_EVERY frames (hex WRAM addrs)
local L = dofile(os.getenv("TBG_LIB"))
local out = os.getenv("TBG_OUT")
local frames = tonumber(os.getenv("TBG_FRAMES") or "3000")
local seed = tonumber(os.getenv("TBG_P1RAND") or "0")
local pokes = os.getenv("TBG_POKES") or ""
local every = tonumber(os.getenv("TBG_POKE_EVERY") or "0")
local log = io.open(out .. "/calls.bin", "wb")
local meta = io.open(out .. "/calls.txt", "w")
local n, frame, inCall, loaded = 0, 0, false, false
local rmin, rmax = 0xFFFFFF, 0
local function w(a) return emu.read(a, emu.memType.snesWorkRam) end
local function blob(a, len) local t = {} for i = 0, len - 1 do t[#t + 1] = string.char(w(a + i)) end return table.concat(t) end
L.load(os.getenv("TBG_LOAD"))
emu.addMemoryCallback(function()
inCall = true
log:write("ENTR", blob(0, 0x800))
end, emu.callbackType.exec, 0x81D9BA, 0x81D9BA, emu.cpuType.snes)
emu.addMemoryCallback(function()
log:write("EXIT", blob(0x100, 0x100))
end, emu.callbackType.exec, 0x81DA7F, 0x81DA7F, emu.cpuType.snes)
emu.addMemoryCallback(function()
inCall = false
log:write("E1B6", blob(0x100, 0x100))
n = n + 1
meta:write(string.format("%d frame=%d p=%d type=%d res col=%d rot=%d\n", n, frame, w(0x12A), w(0x1F4), w(0x1DD), w(0x1DC)))
end, emu.callbackType.exec, 0x81DA8D, 0x81DA8D, emu.cpuType.snes)
emu.addMemoryCallback(function(addr)
if inCall then if addr < rmin then rmin = addr end if addr > rmax then rmax = addr end end
end, emu.callbackType.read, 0x81E400, 0x81F2FF, emu.cpuType.snes)
local rs = seed
local function rnd() rs = (rs * 1103515245 + 12345) % 2147483648 return rs end
local held = {}
emu.addEventCallback(function()
if seed ~= 0 then emu.setInput(held, 0) end
end, emu.eventType.inputPolled)
emu.addEventCallback(function()
frame = frame + 1
if pokes ~= "" and (frame == 2 or (every > 0 and frame % every == 0)) then
for a, v in pokes:gmatch("(%x+)=(%x+)") do emu.write(tonumber(a, 16), tonumber(v, 16), emu.memType.snesWorkRam) end
end
if seed ~= 0 and frame % 6 == 0 then
local r = rnd() % 10
held = {}
if r == 0 then held.left = true elseif r == 1 then held.right = true elseif r == 2 then held.a = true
elseif r == 3 then held.b = true elseif r <= 6 then held.down = true end
end
if frame >= frames then
meta:write(string.format("calls=%d romread=%06X-%06X\n", n, rmin, rmax))
log:close(); meta:close(); emu.stop(0)
end
end, emu.eventType.endFrame)