-- Log sound dispatcher calls (88:8000) and CPU->APU port writes; dump SPC RAM/DSP at chosen frames. -- env: SND_OUT dir, SND_PLAN (battle_lib inputPlan), SND_DUMPS "f1,f2", SND_END, SND_LOAD savestate, SND_PORTLOG=1 local L = dofile(os.getenv("TBG_LIB")) local out = os.getenv("SND_OUT") local plan = L.inputPlan(os.getenv("SND_PLAN")) local dumps = {} for f in (os.getenv("SND_DUMPS") or ""):gmatch("%d+") do dumps[tonumber(f)] = true end local last = tonumber(os.getenv("SND_END") or "3000") local portlog = os.getenv("SND_PORTLOG") == "1" local log = io.open(out .. "/calls.txt", "w") local plog = portlog and io.open(out .. "/ports.txt", "w") or nil local frame = 0 if os.getenv("SND_LOAD") then L.load(os.getenv("SND_LOAD")) end emu.addMemoryCallback(function() local s = emu.getState() local ret = emu.read(((s["cpu.sp"] + 1) & 0xFFFF), emu.memType.snesMemory) | (emu.read(((s["cpu.sp"] + 2) & 0xFFFF), emu.memType.snesMemory) << 8) | (emu.read(((s["cpu.sp"] + 3) & 0xFFFF), emu.memType.snesMemory) << 16) log:write(string.format("%d CALL A=%04X X=%04X Y=%04X from=%06X\n", frame, s["cpu.a"], s["cpu.x"], s["cpu.y"], ret)) end, emu.callbackType.exec, 0x888000, 0x888000, emu.cpuType.snes) if plog then for _, bank in ipairs({0x00, 0x80, 0x81, 0x82, 0x83, 0x84, 0x86, 0x88}) do local base = bank << 16 emu.addMemoryCallback(function(addr, value) local s = emu.getState() plog:write(string.format("%d W %04X=%02X pc=%02X%04X\n", frame, addr & 0xFFFF, value, s["cpu.k"], s["cpu.pc"])) end, emu.callbackType.write, base + 0x2140, base + 0x2143, emu.cpuType.snes) end end local function dump(tag) local t = {} for i = 0, 0xFFFF do t[#t + 1] = string.char(emu.read(i, emu.memType.spcRam)) end local h = io.open(string.format("%s/spc_%s.ram", out, tag), "wb"); h:write(table.concat(t)); h:close() t = {} for i = 0, 0x7F do t[#t + 1] = string.char(emu.read(i, emu.memType.spcDspRegisters)) end h = io.open(string.format("%s/spc_%s.dsp", out, tag), "wb"); h:write(table.concat(t)); h:close() local s = emu.getState() h = io.open(string.format("%s/spc_%s.regs", out, tag), "w") for _, k in ipairs({"spc.pc", "spc.a", "spc.x", "spc.y", "spc.sp", "spc.ps", "spc.cpuRegs[0]", "spc.cpuRegs[1]", "spc.cpuRegs[2]", "spc.cpuRegs[3]", "spc.outputReg[0]", "spc.outputReg[1]", "spc.outputReg[2]", "spc.outputReg[3]", "spc.timer0.target", "spc.timer1.target", "spc.timer2.target", "spc.timer0.enabled", "spc.timer1.enabled", "spc.timer2.enabled", "spc.romEnabled", "spc.dspReg", "spc.ramReg[0]", "spc.ramReg[1]"}) do h:write(k .. "=" .. tostring(s[k]) .. "\n") end h:close() end local held = {} emu.addEventCallback(function() local t = {[0] = {}, [1] = {}} local any = {false, false} for _, e in ipairs(held) do if e.left > 0 then for b in e.b:gmatch("[^+]+") do t[e.port][b] = true end; any[e.port + 1] = true end end if any[1] then emu.setInput(t[0], 0) end if any[2] then emu.setInput(t[1], 1, 1) end -- port 2 needs the third argument end, emu.eventType.inputPolled) emu.addEventCallback(function() frame = frame + 1 for _, e in ipairs(held) do e.left = e.left - 1 end for _, e in ipairs(plan) do if e.f == frame then held[#held + 1] = {b = e.b, port = e.port, left = e.hold} end end if dumps[frame] then dump(tostring(frame)) end if frame >= last then log:close(); if plog then plog:close() end; emu.stop(0) end end, emu.eventType.endFrame)