From f9bb0460d8d21356a181be96b60527f8cff1ca22 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Wed, 25 May 2022 12:15:11 -0300 Subject: [PATCH] perf: don't check scheduler every iteration of runFrame loop ~20fps gain in Pokemon Emerald, nice --- src/emu.zig | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/emu.zig b/src/emu.zig index 193c0eb..acaf07d 100644 --- a/src/emu.zig +++ b/src/emu.zig @@ -51,13 +51,15 @@ 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(); + const next = sched.nextTimestamp(); + if (next > frame_end) break; - while (sched.tick >= sched.nextTimestamp()) { - sched.handleEvent(cpu); + while (sched.tick < next) { + if (cpu.bus.io.haltcnt == .Execute) cpu.step() else sched.tick += 1; + cpu.handleDMATransfers(); } + + sched.handleEvent(cpu); } }