From fc7f2a2959e66fffcc6276e024c13189ed74f5e9 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Fri, 21 Oct 2022 05:12:46 -0300 Subject: [PATCH] fix: resolve issue when handling event sooner than expected --- src/emu.zig | 5 +---- src/scheduler.zig | 8 +++++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/emu.zig b/src/emu.zig index 002874c..492268b 100644 --- a/src/emu.zig +++ b/src/emu.zig @@ -51,10 +51,7 @@ pub fn runFrame(sched: *Scheduler, cpu: *Arm7tdmi) void { const frame_end = sched.tick + cycles_per_frame; while (true) { - const next = sched.nextTimestamp(); - const run_until = std.math.min(next, frame_end); - - while (sched.tick < run_until) { + while (sched.tick < std.math.min(frame_end, sched.nextTimestamp())) { if (cpu.bus.io.haltcnt == .Execute) cpu.step() else sched.tick += 1; cpu.handleDMATransfers(); } diff --git a/src/scheduler.zig b/src/scheduler.zig index 5f22521..43c4b9e 100644 --- a/src/scheduler.zig +++ b/src/scheduler.zig @@ -90,10 +90,12 @@ pub const Scheduler = struct { self.queue.add(.{ .kind = kind, .tick = self.now() + end }) catch unreachable; } - pub fn nextTimestamp(self: *Self) u64 { - if (self.queue.peek()) |e| return e.tick; + pub inline fn nextTimestamp(self: *const Self) u64 { + @setRuntimeSafety(false); - unreachable; // There's always the HeatDeath event scheduled + // Typically you'd use PriorityQueue.peek here, but there's always at least a HeatDeath + // event in the PQ so we can just do this instead. Should be faster in ReleaseSafe + return self.queue.items[0].tick; } };