From 78e7c0bc3f9baab5bf58411db37aa7769d8cb7a3 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Fri, 21 Oct 2022 05:12:45 -0300 Subject: [PATCH] perf: don't check scheduler every iteration of runFrame loop ~20fps gain in Pokemon Emerald, nice --- src/emu.zig | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/emu.zig b/src/emu.zig index 193c0eb..002874c 100644 --- a/src/emu.zig +++ b/src/emu.zig @@ -50,14 +50,17 @@ pub fn run(kind: RunKind, quit: *Atomic(bool), fps: *FpsTracker, sched: *Schedul pub fn runFrame(sched: *Scheduler, cpu: *Arm7tdmi) void { const frame_end = sched.tick + cycles_per_frame; - while (sched.tick < frame_end) { - if (cpu.bus.io.haltcnt == .Halt) sched.tick += 1; - if (cpu.bus.io.haltcnt == .Execute) cpu.step(); - cpu.handleDMATransfers(); + while (true) { + const next = sched.nextTimestamp(); + const run_until = std.math.min(next, frame_end); - while (sched.tick >= sched.nextTimestamp()) { - sched.handleEvent(cpu); + while (sched.tick < run_until) { + if (cpu.bus.io.haltcnt == .Execute) cpu.step() else sched.tick += 1; + cpu.handleDMATransfers(); } + + if (sched.tick >= frame_end) break; + sched.handleEvent(cpu); } }