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;
|
2022-10-21 08:12:28 +00:00
|
|
|
const FpsAverage = @import("util.zig").FpsAverage;
|
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:29 +00:00
|
|
|
// 228 Lines which consist of 308 dots (which are 4 cycles long)
|
|
|
|
const cycles_per_frame: u64 = 228 * (308 * 4); //280896
|
2022-10-21 08:12:29 +00:00
|
|
|
const clock_rate: u64 = 1 << 24; // 16.78MHz
|
2022-10-21 08:12:29 +00:00
|
|
|
|
|
|
|
// TODO: Don't truncate this, be more accurate w/ timing
|
|
|
|
// 59.6046447754ns (truncated to just 59ns)
|
2022-10-21 08:12:26 +00:00
|
|
|
const clock_period: u64 = std.time.ns_per_s / clock_rate;
|
|
|
|
const frame_period = (clock_period * cycles_per_frame);
|
|
|
|
|
2022-10-21 08:12:29 +00:00
|
|
|
// 59.7275005696Hz
|
2022-10-21 08:12:29 +00:00
|
|
|
pub const frame_rate = @intToFloat(f64, std.time.ns_per_s) /
|
2022-10-21 08:12:29 +00:00
|
|
|
((@intToFloat(f64, std.time.ns_per_s) / @intToFloat(f64, clock_rate)) * @intToFloat(f64, cycles_per_frame));
|
|
|
|
|
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:12:27 +00:00
|
|
|
const RunKind = enum {
|
|
|
|
Unlimited,
|
|
|
|
UnlimitedFPS,
|
|
|
|
Limited,
|
|
|
|
LimitedFPS,
|
|
|
|
LimitedBusy,
|
|
|
|
};
|
|
|
|
|
2022-10-21 08:12:35 +00:00
|
|
|
pub fn run(kind: RunKind, quit: *Atomic(bool), fps: *FpsAverage, sched: *Scheduler, cpu: *Arm7tdmi) void {
|
2022-10-21 08:12:27 +00:00
|
|
|
switch (kind) {
|
2022-10-21 08:12:35 +00:00
|
|
|
.Unlimited => runUnsync(quit, sched, cpu),
|
|
|
|
.Limited => runSync(quit, sched, cpu),
|
|
|
|
.UnlimitedFPS => runUnsyncFps(quit, fps, sched, cpu),
|
|
|
|
.LimitedFPS => runSyncFps(quit, fps, sched, cpu),
|
|
|
|
.LimitedBusy => runBusyLoop(quit, sched, cpu),
|
2022-10-21 08:12:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:12:35 +00:00
|
|
|
pub fn runFrame(sched: *Scheduler, cpu: *Arm7tdmi) void {
|
2022-10-21 08:12:30 +00:00
|
|
|
const frame_end = sched.tick + cycles_per_frame;
|
|
|
|
|
|
|
|
while (sched.tick < frame_end) {
|
2022-10-21 08:12:35 +00:00
|
|
|
if (cpu.bus.io.haltcnt == .Halt) sched.tick += 1;
|
|
|
|
if (cpu.bus.io.haltcnt == .Execute) cpu.step();
|
|
|
|
cpu.bus.handleDMATransfers();
|
2021-12-29 21:09:00 +00:00
|
|
|
|
2022-10-21 08:11:49 +00:00
|
|
|
while (sched.tick >= sched.nextTimestamp()) {
|
2022-10-21 08:12:35 +00:00
|
|
|
sched.handleEvent(cpu);
|
2021-12-29 21:09:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-21 08:12:03 +00:00
|
|
|
|
2022-10-21 08:12:35 +00:00
|
|
|
pub fn runUnsync(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi) void {
|
2022-10-21 08:12:37 +00:00
|
|
|
log.info("Start unsynchronized emu thread", .{});
|
2022-10-21 08:12:35 +00:00
|
|
|
while (!quit.load(.Unordered)) runFrame(sched, cpu);
|
2022-10-21 08:12:27 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 08:12:35 +00:00
|
|
|
pub fn runSync(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi) void {
|
2022-10-21 08:12:37 +00:00
|
|
|
log.info("Start synchronized emu thread", .{});
|
2022-10-21 08:12:26 +00:00
|
|
|
var timer = Timer.start() catch unreachable;
|
2022-10-21 08:12:27 +00:00
|
|
|
var wake_time: u64 = frame_period;
|
|
|
|
|
2022-10-21 08:12:28 +00:00
|
|
|
while (!quit.load(.Unordered)) {
|
2022-10-21 08:12:35 +00:00
|
|
|
runFrame(sched, cpu);
|
2022-10-21 08:12:27 +00:00
|
|
|
|
|
|
|
// Put the Thread to Sleep + Backup Spin Loop
|
|
|
|
// This saves on resource usage when frame limiting
|
|
|
|
sleep(&timer, &wake_time);
|
|
|
|
|
|
|
|
// Update to the new wake time
|
|
|
|
wake_time += frame_period;
|
2022-10-21 08:12:28 +00:00
|
|
|
}
|
2022-10-21 08:12:27 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 08:12:35 +00:00
|
|
|
pub fn runUnsyncFps(quit: *Atomic(bool), fps: *FpsAverage, sched: *Scheduler, cpu: *Arm7tdmi) void {
|
2022-10-21 08:12:37 +00:00
|
|
|
log.info("Start unsynchronized emu thread w/ fps tracking", .{});
|
2022-10-21 08:12:27 +00:00
|
|
|
var fps_timer = Timer.start() catch unreachable;
|
|
|
|
|
2022-10-21 08:12:28 +00:00
|
|
|
while (!quit.load(.Unordered)) {
|
2022-10-21 08:12:35 +00:00
|
|
|
runFrame(sched, cpu);
|
2022-10-21 08:12:29 +00:00
|
|
|
fps.add(fps_timer.lap());
|
2022-10-21 08:12:28 +00:00
|
|
|
}
|
2022-10-21 08:12:27 +00:00
|
|
|
}
|
2022-10-21 08:12:26 +00:00
|
|
|
|
2022-10-21 08:12:35 +00:00
|
|
|
pub fn runSyncFps(quit: *Atomic(bool), fps: *FpsAverage, sched: *Scheduler, cpu: *Arm7tdmi) void {
|
2022-10-21 08:12:37 +00:00
|
|
|
log.info("Start synchronized emu thread w/ fps tracking", .{});
|
2022-10-21 08:12:27 +00:00
|
|
|
var timer = Timer.start() catch unreachable;
|
|
|
|
var fps_timer = Timer.start() catch unreachable;
|
|
|
|
var wake_time: u64 = frame_period;
|
2022-10-21 08:12:26 +00:00
|
|
|
|
2022-10-21 08:12:28 +00:00
|
|
|
while (!quit.load(.Unordered)) {
|
2022-10-21 08:12:35 +00:00
|
|
|
runFrame(sched, cpu);
|
2022-10-21 08:12:26 +00:00
|
|
|
|
2022-10-21 08:12:27 +00:00
|
|
|
// Put the Thread to Sleep + Backup Spin Loop
|
|
|
|
// This saves on resource usage when frame limiting
|
|
|
|
sleep(&timer, &wake_time);
|
2022-10-21 08:12:27 +00:00
|
|
|
|
2022-10-21 08:12:27 +00:00
|
|
|
// Determine FPS
|
2022-10-21 08:12:29 +00:00
|
|
|
fps.add(fps_timer.lap());
|
2022-10-21 08:12:27 +00:00
|
|
|
|
2022-10-21 08:12:27 +00:00
|
|
|
// Update to the new wake time
|
|
|
|
wake_time += frame_period;
|
2022-10-21 08:12:28 +00:00
|
|
|
}
|
2022-10-21 08:12:27 +00:00
|
|
|
}
|
2022-10-21 08:12:26 +00:00
|
|
|
|
2022-10-21 08:12:35 +00:00
|
|
|
pub fn runBusyLoop(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi) void {
|
2022-10-21 08:12:37 +00:00
|
|
|
log.info("Start synchronized emu thread using busy loop", .{});
|
2022-10-21 08:12:27 +00:00
|
|
|
var timer = Timer.start() catch unreachable;
|
|
|
|
var wake_time: u64 = frame_period;
|
2022-10-21 08:12:27 +00:00
|
|
|
|
2022-10-21 08:12:28 +00:00
|
|
|
while (!quit.load(.Unordered)) {
|
2022-10-21 08:12:35 +00:00
|
|
|
runFrame(sched, cpu);
|
2022-10-21 08:12:27 +00:00
|
|
|
spinLoop(&timer, wake_time);
|
2022-10-21 08:12:27 +00:00
|
|
|
|
2022-10-21 08:12:27 +00:00
|
|
|
// Update to the new wake time
|
|
|
|
wake_time += frame_period;
|
2022-10-21 08:12:28 +00:00
|
|
|
}
|
2022-10-21 08:12:27 +00:00
|
|
|
}
|
2022-10-21 08:12:27 +00:00
|
|
|
|
2022-10-21 08:12:27 +00:00
|
|
|
fn sleep(timer: *Timer, wake_time: *u64) void {
|
2022-10-21 08:12:28 +00:00
|
|
|
// const step = std.time.ns_per_ms * 10; // 10ms
|
2022-10-21 08:12:27 +00:00
|
|
|
const timestamp = timer.read();
|
2022-10-21 08:12:27 +00:00
|
|
|
|
2022-10-21 08:12:27 +00:00
|
|
|
// ns_late is non zero if we are late.
|
|
|
|
const ns_late = timestamp -| wake_time.*;
|
2022-10-21 08:12:27 +00:00
|
|
|
|
2022-10-21 08:12:27 +00:00
|
|
|
// 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;
|
2022-10-21 08:12:27 +00:00
|
|
|
}
|
2022-10-21 08:12:27 +00:00
|
|
|
|
|
|
|
const sleep_for = frame_period - ns_late;
|
|
|
|
|
2022-10-21 08:12:28 +00:00
|
|
|
// // 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);
|
2022-10-21 08:12:27 +00:00
|
|
|
|
2022-10-21 08:12:28 +00:00
|
|
|
std.time.sleep(sleep_for);
|
2022-10-21 08:12:27 +00:00
|
|
|
|
|
|
|
// 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.*);
|
2022-10-21 08:12:27 +00:00
|
|
|
}
|
2022-10-21 08:12:26 +00:00
|
|
|
|
2022-10-21 08:12:27 +00:00
|
|
|
fn spinLoop(timer: *Timer, wake_time: u64) void {
|
2022-10-21 08:12:27 +00:00
|
|
|
while (true) if (timer.read() > wake_time) break;
|
2022-10-21 08:12:03 +00:00
|
|
|
}
|