local onFrameEndListenerName = "Genesis Lemmings TAS lag frames tracker"; local previousFrameCounter = -1 local previousF54DCounter = 0 local lagFramesCount = 0 local stoptracking = false local function OnAfterFrameUpdate() local counterAddr = 0xF54D local currentCounterValue = memory.readbyte(counterAddr) local currentFrameCounter = emu.framecount() if (currentCounterValue ~= 0) then if ((not stoptracking) and (currentFrameCounter == previousFrameCounter + 1)) then if (previousF54DCounter == currentCounterValue) then lagFramesCount = lagFramesCount + 1 end elseif ((not stoptracking) and (currentFrameCounter == previousFrameCounter - 1)) then if (previousF54DCounter == currentCounterValue) then lagFramesCount = lagFramesCount - 1 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: [stopped tracking, most recent value] " .. effectiveLagFramesCount, nil, nil, "bottomleft") else gui.text(0, offsetFromBottomOfWindow, "lag frames: " .. effectiveLagFramesCount, nil, nil, "bottomleft") end previousF54DCounter = currentCounterValue else stoptracking = false lagFramesCount = 0 previousF54DCounter = 0 end previousFrameCounter = currentFrameCounter end event.unregisterbyname(onFrameEndListenerName) event.onframestart(OnAfterFrameUpdate, onFrameEndListenerName)