Skip to content

Commit

Permalink
fix: limit fps to 60 (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
PumpedSardines authored Nov 25, 2024
1 parent b405728 commit 6ad643e
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/cpu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,21 @@ function flushUart() {
}
}

const fps = 60;
const timePerFrame = 1000 / fps;
let fps = 60;
let currentFrequency = 0;
let start = performance.now();
let cycles = 50_000;
const desiredCycles = 500_000;
const desiredCycles = 30_000_000;
let intermediateFps = 0;

function cpuLoop() {
if (!store.get(hasLoadedAtom)) {
requestAnimationFrame(cpuLoop);
return;
}
const startOfLoop = performance.now();
const timePerFrame = 1000 / fps;
intermediateFps++;

updateButton();
updateSwitches();
Expand All @@ -126,14 +128,16 @@ function cpuLoop() {
if (performance.now() - start > 1000) {
store.set(clockFrequencyAtom, currentFrequency);
currentFrequency = 0;
start = start + 1000;
fps = intermediateFps;
intermediateFps = 0;
start = performance.now();
}

const endOfLoop = performance.now();
const timeTook = endOfLoop - startOfLoop;

if (timeTook < timePerFrame) {
cycles = Math.min(desiredCycles, Math.floor(cycles * 1.1));
cycles = Math.min(desiredCycles / fps, Math.floor(cycles * 1.1));
} else {
cycles = Math.max(1, Math.floor(cycles * 0.9));
}
Expand Down

0 comments on commit 6ad643e

Please sign in to comment.