chore: create different types of emuloops

This commit is contained in:
Rekai Nyangadzayi Musuka 2022-10-21 05:12:27 -03:00
parent 74cda6a1d0
commit 92e63f5dd6
2 changed files with 102 additions and 42 deletions

View File

@ -8,17 +8,32 @@ const Timer = std.time.Timer;
const Thread = std.Thread; const Thread = std.Thread;
const Atomic = std.atomic.Atomic; const Atomic = std.atomic.Atomic;
// One frame operates at 59.7275005696Hz
const cycles_per_frame: u64 = 228 * (308 * 4); const cycles_per_frame: u64 = 228 * (308 * 4);
const clock_rate: u64 = 1 << 24; const clock_rate: u64 = 1 << 24;
const clock_period: u64 = std.time.ns_per_s / clock_rate; const clock_period: u64 = std.time.ns_per_s / clock_rate;
const frame_period = (clock_period * cycles_per_frame); const frame_period = (clock_period * cycles_per_frame);
const sync_to_video: bool = true;
// One frame operates at 59.7275005696Hz
const log = std.log.scoped(.Emulation); const log = std.log.scoped(.Emulation);
const RunKind = enum {
Unlimited,
UnlimitedFPS,
Limited,
LimitedFPS,
LimitedBusy,
};
pub fn run(kind: RunKind, quit: *Atomic(bool), pause: *Atomic(bool), fps: *Atomic(u64), sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
switch (kind) {
.Unlimited => runUnSync(quit, pause, sched, cpu, bus),
.Limited => runSync(quit, pause, sched, cpu, bus),
.UnlimitedFPS => runUnSyncFps(quit, pause, fps, sched, cpu, bus),
.LimitedFPS => runSyncFps(quit, pause, fps, sched, cpu, bus),
.LimitedBusy => runBusyLoop(quit, pause, sched, cpu, bus),
}
}
pub fn runFrame(sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void { pub fn runFrame(sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
var cycles: u64 = 0; var cycles: u64 = 0;
while (cycles < cycles_per_frame) : (cycles += 1) { while (cycles < cycles_per_frame) : (cycles += 1) {
@ -31,63 +46,108 @@ pub fn runFrame(sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
} }
} }
pub fn runEmuThread(quit: *Atomic(bool), pause: *Atomic(bool), fps: *Atomic(u64), sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void { pub fn runUnSync(quit: *Atomic(bool), pause: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
var timer = Timer.start() catch unreachable; log.info("Unsynchronized EmuThread has begun", .{});
var fps_timer = Timer.start() catch unreachable; while (!quit.load(.Unordered)) if (!pause.load(.Unordered)) runFrame(sched, cpu, bus);
}
pub fn runSync(quit: *Atomic(bool), pause: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
log.info("Synchronized EmuThread has begun", .{});
var timer = Timer.start() catch unreachable;
var wake_time: u64 = frame_period; var wake_time: u64 = frame_period;
log.info("EmuThread has begun execution", .{}); while (!quit.load(.Unordered)) if (!pause.load(.Unordered)) {
runFrame(sched, cpu, bus);
while (!quit.load(.Unordered)) { // Put the Thread to Sleep + Backup Spin Loop
if (!pause.load(.Unordered)) { // This saves on resource usage when frame limiting
runFrame(sched, cpu, bus); sleep(&timer, &wake_time);
const timestamp = timer.read(); // Update to the new wake time
fps.store(emuFps(fps_timer.lap()), .Unordered); wake_time += frame_period;
};
}
// ns_late is non zero if we are late. pub fn runUnSyncFps(quit: *Atomic(bool), pause: *Atomic(bool), fps: *Atomic(u64), sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
var ns_late = timestamp -| wake_time; log.info("Unsynchronized EmuThread with FPS Tracking has begun", .{});
var fps_timer = Timer.start() catch unreachable;
// log.info("timestamp: {} | late: {}", .{ timestamp, ns_late }); while (!quit.load(.Unordered)) if (!pause.load(.Unordered)) {
runFrame(sched, cpu, bus);
fps.store(emuFps(fps_timer.lap()), .Unordered);
};
}
// If we're more than a frame late, skip the rest of this loop pub fn runSyncFps(quit: *Atomic(bool), pause: *Atomic(bool), fps: *Atomic(u64), sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
// Recalculate what our new wake time should be so that we can log.info("Synchronized EmuThread has begun", .{});
// get "back on track" var timer = Timer.start() catch unreachable;
if (ns_late > frame_period) { var fps_timer = Timer.start() catch unreachable;
wake_time = timestamp + frame_period; var wake_time: u64 = frame_period;
continue;
}
if (sync_to_video) { while (!quit.load(.Unordered)) if (!pause.load(.Unordered)) {
// Employ several sleep calls in periods of 10ms runFrame(sched, cpu, bus);
// By doing this the behaviour should average out to be
// more consistent
const sleep_for = frame_period - ns_late; // Put the Thread to Sleep + Backup Spin Loop
const loop_count = sleep_for / (std.time.ns_per_ms * 10); // How many groups of 10ms // This saves on resource usage when frame limiting
sleep(&timer, &wake_time);
var i: usize = 0; // Determine FPS
while (i < loop_count) : (i += 1) { fps.store(emuFps(fps_timer.lap()), .Unordered);
std.time.sleep(std.time.ns_per_ms * 10);
}
// Spin to make up the difference if there is a need // Update to the new wake time
// Make sure that we're using the old wake time and not the onne we recalcualted wake_time += frame_period;
spinLoop(&timer, wake_time); };
} }
// Update to the new wake time pub fn runBusyLoop(quit: *Atomic(bool), pause: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
wake_time += frame_period; log.info("Run EmuThread with spin-loop sync", .{});
} var timer = Timer.start() catch unreachable;
var wake_time: u64 = frame_period;
while (!quit.load(.Unordered)) if (!pause.load(.Unordered)) {
runFrame(sched, cpu, bus);
spinLoop(&timer, wake_time);
// Update to the new wake time
wake_time += frame_period;
};
}
fn sleep(timer: *Timer, wake_time: *u64) void {
const step = std.time.ns_per_ms * 10;
const timestamp = timer.read();
// ns_late is non zero if we are late.
const ns_late = timestamp -| wake_time.*;
// If we're more than a frame late, skip the rest of this loop
// Recalculate what our new wake time should be so that we can
// get "back on track"
if (ns_late > frame_period) {
wake_time.* = timestamp + frame_period;
return;
} }
const sleep_for = frame_period - ns_late;
// Employ several sleep calls in periods of 10ms
// By doing this the behaviour should average out to be
// more consistent
const loop_count = sleep_for / step; // How many groups of 10ms
var i: usize = 0;
while (i < loop_count) : (i += 1) std.time.sleep(step);
// Spin to make up the difference if there is a need
// Make sure that we're using the old wake time and not the onne we recalculated
spinLoop(timer, wake_time.*);
} }
fn spinLoop(timer: *Timer, wake_time: u64) void { fn spinLoop(timer: *Timer, wake_time: u64) void {
while (true) if (timer.read() > wake_time) break; while (true) if (timer.read() > wake_time) break;
} }
fn emuFps(left: u64) u64 { inline fn emuFps(left: u64) u64 {
@setRuntimeSafety(false); @setRuntimeSafety(false);
return @floatToInt(u64, @intToFloat(f64, std.time.ns_per_s) / @intToFloat(f64, left)); return @floatToInt(u64, @intToFloat(f64, std.time.ns_per_s) / @intToFloat(f64, left));
} }

View File

@ -88,7 +88,7 @@ pub fn main() anyerror!void {
var emu_fps = Atomic(u64).init(0); var emu_fps = Atomic(u64).init(0);
// Create Emulator Thread // Create Emulator Thread
const emu_thread = try Thread.spawn(.{}, emu.runEmuThread, .{ &quit, &pause, &emu_fps, &scheduler, &cpu, &bus }); const emu_thread = try Thread.spawn(.{}, emu.run, .{ .LimitedFPS, &quit, &pause, &emu_fps, &scheduler, &cpu, &bus });
defer emu_thread.join(); defer emu_thread.join();
// Initialize SDL // Initialize SDL