2022-10-21 08:12:03 +00:00
|
|
|
const std = @import("std");
|
|
|
|
|
2022-10-21 08:11:50 +00:00
|
|
|
const Bus = @import("Bus.zig");
|
2021-12-29 21:09:00 +00:00
|
|
|
const Scheduler = @import("scheduler.zig").Scheduler;
|
2022-10-21 08:11:44 +00:00
|
|
|
const Arm7tdmi = @import("cpu.zig").Arm7tdmi;
|
2021-12-29 21:09:00 +00:00
|
|
|
|
2022-10-21 08:12:26 +00:00
|
|
|
const Timer = std.time.Timer;
|
|
|
|
const Thread = std.Thread;
|
2022-10-21 08:12:03 +00:00
|
|
|
const Atomic = std.atomic.Atomic;
|
2022-10-21 08:12:26 +00:00
|
|
|
|
2022-10-21 08:12:27 +00:00
|
|
|
const cycles_per_frame: u64 = 228 * (308 * 4);
|
2022-10-21 08:12:26 +00:00
|
|
|
const clock_rate: u64 = 1 << 24;
|
|
|
|
const clock_period: u64 = std.time.ns_per_s / clock_rate;
|
|
|
|
const frame_period = (clock_period * cycles_per_frame);
|
|
|
|
|
|
|
|
const sync_to_video: bool = true;
|
|
|
|
|
2022-10-21 08:12:27 +00:00
|
|
|
// One frame operates at 59.7275005696Hz
|
|
|
|
|
2022-10-21 08:12:26 +00:00
|
|
|
const log = std.log.scoped(.Emulation);
|
2021-12-29 21:09:00 +00:00
|
|
|
|
2022-10-21 08:11:49 +00:00
|
|
|
pub fn runFrame(sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
|
2022-10-21 08:11:46 +00:00
|
|
|
var cycles: u64 = 0;
|
|
|
|
while (cycles < cycles_per_frame) : (cycles += 1) {
|
2022-10-21 08:11:49 +00:00
|
|
|
sched.tick += 1;
|
2022-10-21 08:11:46 +00:00
|
|
|
_ = cpu.step();
|
2021-12-29 21:09:00 +00:00
|
|
|
|
2022-10-21 08:11:49 +00:00
|
|
|
while (sched.tick >= sched.nextTimestamp()) {
|
|
|
|
sched.handleEvent(cpu, bus);
|
2021-12-29 21:09:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-21 08:12:03 +00:00
|
|
|
|
2022-10-21 08:12:26 +00:00
|
|
|
pub fn runEmuThread(quit: *Atomic(bool), pause: *Atomic(bool), fps: *Atomic(u64), sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
|
|
|
|
var timer = Timer.start() catch unreachable;
|
2022-10-21 08:12:27 +00:00
|
|
|
var fps_timer = Timer.start() catch unreachable;
|
|
|
|
|
|
|
|
var wake_time: u64 = frame_period;
|
2022-10-21 08:12:26 +00:00
|
|
|
|
|
|
|
log.info("EmuThread has begun execution", .{});
|
|
|
|
|
2022-10-21 08:12:03 +00:00
|
|
|
while (!quit.load(.Unordered)) {
|
2022-10-21 08:12:26 +00:00
|
|
|
if (!pause.load(.Unordered)) {
|
|
|
|
runFrame(sched, cpu, bus);
|
|
|
|
|
2022-10-21 08:12:27 +00:00
|
|
|
const timestamp = timer.read();
|
|
|
|
fps.store(emuFps(fps_timer.lap()), .Unordered);
|
|
|
|
|
|
|
|
// ns_late is non zero if we are late.
|
|
|
|
var ns_late = timestamp -| wake_time;
|
|
|
|
|
|
|
|
// Calculate the new Thread wake time
|
|
|
|
wake_time += frame_period;
|
2022-10-21 08:12:26 +00:00
|
|
|
|
2022-10-21 08:12:27 +00:00
|
|
|
// If we're more than a frame late, skip the rest of this loop
|
|
|
|
if (ns_late > frame_period) continue;
|
2022-10-21 08:12:26 +00:00
|
|
|
|
2022-10-21 08:12:27 +00:00
|
|
|
if (sync_to_video) std.time.sleep(frame_period - ns_late);
|
|
|
|
|
|
|
|
// Backup Busy Loop Routine
|
|
|
|
// if (sync_to_video) spinLoop(&timer, wake_time);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-21 08:12:26 +00:00
|
|
|
|
2022-10-21 08:12:27 +00:00
|
|
|
fn spinLoop(timer: *Timer, wake_time: u64) void {
|
|
|
|
while (true) {
|
|
|
|
const timestamp = timer.read();
|
2022-10-21 08:12:26 +00:00
|
|
|
|
2022-10-21 08:12:27 +00:00
|
|
|
if (timestamp >= wake_time) {
|
|
|
|
break;
|
2022-10-21 08:12:26 +00:00
|
|
|
}
|
2022-10-21 08:12:03 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-21 08:12:26 +00:00
|
|
|
|
|
|
|
fn emuFps(left: u64) u64 {
|
|
|
|
@setRuntimeSafety(false);
|
|
|
|
return @floatToInt(u64, @intToFloat(f64, std.time.ns_per_s) / @intToFloat(f64, left));
|
|
|
|
}
|