local onFrameEndListenerName = "Genesis Lemmings TAS lag frames tracker"; local previousFrameCounter = -1 local previousF54DCounter = 0 local previousF9E2Value = 0 local lagFramesCount = 0 local stoptracking = false local lagFrameTimes = {} local function OnAfterFrameUpdate() local currentCounterValue = memory.readbyte(0xF54D) local currentF9E2Value = memory.readbyte(0xF9E2) local currentFrameCounter = emu.framecount() if (currentCounterValue ~= 0) then if ((not stoptracking) and (currentFrameCounter == previousFrameCounter + 1)) then if ((previousF54DCounter == currentCounterValue) and (currentF9E2Value ~= 0)) then lagFramesCount = lagFramesCount + 1 -- see comment below on why we ignore the first one if (lagFramesCount > 1) then console.writeline("lag at frame #" .. currentFrameCounter) table.insert(lagFrameTimes, currentFrameCounter) end end if ((previousF9E2Value ~= 0) and (currentF9E2Value == 0)) then console.writeline() console.writeline("***** end of level observed on frame " .. currentFrameCounter .. " *****"); console.writeline(#lagFrameTimes .. " lag frames (frame counter values listed below):") for idx,frameCounterValue in ipairs(lagFrameTimes) do console.writeline(" " .. frameCounterValue); end console.writeline("*****"); console.writeline(); end elseif ((not stoptracking) and (currentFrameCounter == previousFrameCounter - 1)) then if ((previousF54DCounter == currentCounterValue) and (previousF9E2Value ~= 0)) then lagFramesCount = lagFramesCount - 1 if (lagFramesCount > 0) then table.remove(lagFrameTimes) end end else stoptracking = true end -- effectiveLagFramesCount: when the level starts we observe the following -- per-frame changes in 0xF54D: 0->1, 1->1, then the regular cadence of 1,2,3,1,2,3 -- Therefore, the straightforward calculation of lagFramesCount always reports -- one extra frame from that phenomenon. This appears unavoidable, so we compensate -- for that initial 1->1 in the output below by not counting it. local effectiveLagFramesCount = lagFramesCount if (lagFramesCount > 0) then effectiveLagFramesCount = lagFramesCount - 1 end local offsetFromBottomOfWindow = 5 if (stoptracking) then gui.text(0, offsetFromBottomOfWindow, "lag frames: [aborted tracking, most recent value] " .. effectiveLagFramesCount, nil, nil, "bottomleft") else gui.text(0, offsetFromBottomOfWindow, "lag frames: " .. effectiveLagFramesCount, nil, nil, "bottomleft") end previousF54DCounter = currentCounterValue previousF9E2Value = currentF9E2Value else stoptracking = false lagFramesCount = 0 previousF54DCounter = 0 previousF9E2Value = 0 lagFrameTimes = {} end previousFrameCounter = currentFrameCounter end console.clear() event.unregisterbyname(onFrameEndListenerName) event.onframestart(OnAfterFrameUpdate, onFrameEndListenerName)