fix: speed percentage in title is now accurate

We now properly account for full speed being 59.97Hz not, 59Hz or 60Hz
This commit is contained in:
2022-03-22 10:39:42 -03:00
parent eff25a0ab2
commit 5dd69500ca
3 changed files with 23 additions and 20 deletions

View File

@@ -9,12 +9,19 @@ const Timer = std.time.Timer;
const Thread = std.Thread;
const Atomic = std.atomic.Atomic;
// One frame operates at 59.7275005696Hz
const cycles_per_frame: u64 = 228 * (308 * 4);
const clock_rate: u64 = 1 << 24;
// 228 Lines which consist of 308 dots (which are 4 cycles long)
const cycles_per_frame: u64 = 228 * (308 * 4); //280896
const clock_rate: u64 = 1 << 24; // 16.78MHz
// TODO: Don't truncate this, be more accurate w/ timing
// 59.6046447754ns (truncated to just 59ns)
const clock_period: u64 = std.time.ns_per_s / clock_rate;
const frame_period = (clock_period * cycles_per_frame);
// 59.7275005696Hz
pub const frame_rate = @intToFloat(f64, std.time.ns_per_s) /
((@intToFloat(f64, std.time.ns_per_s) / @intToFloat(f64, clock_rate)) * @intToFloat(f64, cycles_per_frame));
const log = std.log.scoped(.Emulation);
const RunKind = enum {
@@ -27,9 +34,9 @@ const RunKind = enum {
pub fn run(kind: RunKind, quit: *Atomic(bool), fps: *FpsAverage, sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
switch (kind) {
.Unlimited => runUnSync(quit, sched, cpu, bus),
.Unlimited => runUnsync(quit, sched, cpu, bus),
.Limited => runSync(quit, sched, cpu, bus),
.UnlimitedFPS => runUnSyncFps(quit, fps, sched, cpu, bus),
.UnlimitedFPS => runUnsyncFps(quit, fps, sched, cpu, bus),
.LimitedFPS => runSyncFps(quit, fps, sched, cpu, bus),
.LimitedBusy => runBusyLoop(quit, sched, cpu, bus),
}
@@ -47,7 +54,7 @@ pub fn runFrame(sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
}
}
pub fn runUnSync(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
pub fn runUnsync(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
log.info("Unsynchronized EmuThread has begun", .{});
while (!quit.load(.Unordered)) runFrame(sched, cpu, bus);
}
@@ -69,13 +76,13 @@ pub fn runSync(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus
}
}
pub fn runUnSyncFps(quit: *Atomic(bool), fps: *FpsAverage, sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
pub fn runUnsyncFps(quit: *Atomic(bool), fps: *FpsAverage, sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
log.info("Unsynchronized EmuThread with FPS Tracking has begun", .{});
var fps_timer = Timer.start() catch unreachable;
while (!quit.load(.Unordered)) {
runFrame(sched, cpu, bus);
fps.add(emuFps(fps_timer.lap()));
fps.add(fps_timer.lap());
}
}
@@ -93,7 +100,7 @@ pub fn runSyncFps(quit: *Atomic(bool), fps: *FpsAverage, sched: *Scheduler, cpu:
sleep(&timer, &wake_time);
// Determine FPS
fps.add(emuFps(fps_timer.lap()));
fps.add(fps_timer.lap());
// Update to the new wake time
wake_time += frame_period;
@@ -149,8 +156,3 @@ fn sleep(timer: *Timer, wake_time: *u64) void {
fn spinLoop(timer: *Timer, wake_time: u64) void {
while (true) if (timer.read() > wake_time) break;
}
inline fn emuFps(left: u64) u64 {
@setRuntimeSafety(false);
return @floatToInt(u64, @intToFloat(f64, std.time.ns_per_s) / @intToFloat(f64, left));
}