From 264cd46187c8686117aaee4f94fd990c1ce6934d Mon Sep 17 00:00:00 2001 From: Apexo Date: Sun, 29 Mar 2026 14:32:07 +0200 Subject: handle jumps in time --- index.html | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/index.html b/index.html index b6cf570..34bfe8e 100644 --- a/index.html +++ b/index.html @@ -546,15 +546,23 @@ function updateEeprom(eepromData) { prevEeprom.set(eepromData); } -// ---- RAF polling loop ---- function poll() { requestAnimationFrame(poll); if (!sim) return; // catch up simulator - const dt = Date.now() - t0; - const cycles = CPU_FREQ / 1000 * dt; - sim.step(cycles - sim.getState().cycles); + const tt = 1000 * sim.getState().cycles / CPU_FREQ; + let dt = Date.now() - t0 - tt; + + if (dt < 0 || dt > 100) { + // handle backwards/forward time jumps to avoid freezing the sim or blocking the UI by simulating too much + const dt2 = Math.max(0, Math.min(100, dt)); + console.log("timp jump", dt, dt2); + t0 += dt - dt2; + dt = dt2; + } + + sim.step(Math.round(dt * CPU_FREQ / 1000)); // Read state from emulator const st = sim.getState(); -- cgit v1.2.3