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:
parent
eff25a0ab2
commit
5dd69500ca
30
src/emu.zig
30
src/emu.zig
|
@ -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));
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@ const window_scale = 3;
|
|||
const gba_width = @import("ppu.zig").width;
|
||||
const gba_height = @import("ppu.zig").height;
|
||||
const framebuf_pitch = @import("ppu.zig").framebuf_pitch;
|
||||
const expected_rate = @import("emu.zig").frame_rate;
|
||||
|
||||
pub const enable_logging: bool = false;
|
||||
const is_binary: bool = false;
|
||||
|
@ -76,10 +77,10 @@ pub fn main() anyerror!void {
|
|||
|
||||
// Init Atomics
|
||||
var quit = Atomic(bool).init(false);
|
||||
var emu_fps = FpsAverage.init();
|
||||
var emu_rate = FpsAverage.init();
|
||||
|
||||
// Create Emulator Thread
|
||||
const emu_thread = try Thread.spawn(.{}, emu.run, .{ .UnlimitedFPS, &quit, &emu_fps, &scheduler, &cpu, &bus });
|
||||
const emu_thread = try Thread.spawn(.{}, emu.run, .{ .LimitedFPS, &quit, &emu_rate, &scheduler, &cpu, &bus });
|
||||
defer emu_thread.join();
|
||||
|
||||
// Initialize SDL
|
||||
|
@ -161,8 +162,8 @@ pub fn main() anyerror!void {
|
|||
_ = SDL.SDL_RenderCopy(renderer, texture, null, null);
|
||||
SDL.SDL_RenderPresent(renderer);
|
||||
|
||||
const avg = emu_fps.calc();
|
||||
const dyn_title = std.fmt.bufPrint(&dyn_title_buf, "{s} [Emu: {d:0>3}fps, {d:0>3}%] ", .{ title, avg, (avg * 100 / 59) }) catch unreachable;
|
||||
const actual = emu_rate.calc();
|
||||
const dyn_title = std.fmt.bufPrint(&dyn_title_buf, "{s} [Emu: {d:0>3.2}fps, {d:0>3.2}%] ", .{ title, actual, actual * 100 / expected_rate }) catch unreachable;
|
||||
SDL.SDL_SetWindowTitle(window, dyn_title.ptr);
|
||||
}
|
||||
|
||||
|
|
|
@ -33,8 +33,8 @@ pub const FpsAverage = struct {
|
|||
self.sample_count += 1;
|
||||
}
|
||||
|
||||
pub fn calc(self: *const Self) u64 {
|
||||
return self.total / self.sample_count;
|
||||
pub fn calc(self: *const Self) f64 {
|
||||
return @intToFloat(f64, std.time.ns_per_s) / (@intToFloat(f64, self.total) / @intToFloat(f64, self.sample_count));
|
||||
}
|
||||
|
||||
fn reset(self: *Self, sample: u64) void {
|
||||
|
|
Loading…
Reference in New Issue