2022-01-28 20:33:38 +00:00
|
|
|
const std = @import("std");
|
2022-05-23 15:05:57 +00:00
|
|
|
const SDL = @import("sdl2");
|
2022-01-28 20:33:38 +00:00
|
|
|
|
2022-01-07 23:49:58 +00:00
|
|
|
const Bus = @import("Bus.zig");
|
2021-12-29 21:09:00 +00:00
|
|
|
const Scheduler = @import("scheduler.zig").Scheduler;
|
2022-01-02 03:08:36 +00:00
|
|
|
const Arm7tdmi = @import("cpu.zig").Arm7tdmi;
|
2022-05-23 15:38:44 +00:00
|
|
|
const FpsTracker = @import("util.zig").FpsTracker;
|
2022-07-22 23:19:31 +00:00
|
|
|
const FilePaths = @import("util.zig").FilePaths;
|
2021-12-29 21:09:00 +00:00
|
|
|
|
2022-03-14 08:16:02 +00:00
|
|
|
const Timer = std.time.Timer;
|
|
|
|
const Thread = std.Thread;
|
2022-01-28 20:33:38 +00:00
|
|
|
const Atomic = std.atomic.Atomic;
|
2022-07-22 23:19:31 +00:00
|
|
|
const Allocator = std.mem.Allocator;
|
2022-03-14 08:16:02 +00:00
|
|
|
|
2022-08-08 09:03:23 +00:00
|
|
|
// TODO: Move these to a TOML File
|
2022-09-18 12:20:01 +00:00
|
|
|
const sync_audio = false; // Enable Audio Sync
|
2022-08-08 09:03:23 +00:00
|
|
|
const sync_video: RunKind = .LimitedFPS; // Configure Video Sync
|
2022-09-18 09:30:39 +00:00
|
|
|
pub const win_scale = 4; // 1x, 2x, 3x, etc. Window Scaling
|
2022-08-08 09:03:23 +00:00
|
|
|
pub const cpu_logging = false; // Enable detailed CPU logging
|
2022-09-13 02:01:41 +00:00
|
|
|
pub const allow_unhandled_io = true; // Only relevant in Debug Builds
|
2022-09-17 23:27:17 +00:00
|
|
|
pub const force_rtc = false;
|
2022-05-23 15:05:57 +00:00
|
|
|
|
2022-03-22 13:39:42 +00:00
|
|
|
// 228 Lines which consist of 308 dots (which are 4 cycles long)
|
|
|
|
const cycles_per_frame: u64 = 228 * (308 * 4); //280896
|
2022-03-22 17:41:18 +00:00
|
|
|
const clock_rate: u64 = 1 << 24; // 16.78MHz
|
2022-03-22 13:39:42 +00:00
|
|
|
|
|
|
|
// TODO: Don't truncate this, be more accurate w/ timing
|
|
|
|
// 59.6046447754ns (truncated to just 59ns)
|
2022-03-14 08:16:02 +00:00
|
|
|
const clock_period: u64 = std.time.ns_per_s / clock_rate;
|
|
|
|
const frame_period = (clock_period * cycles_per_frame);
|
|
|
|
|
2022-03-22 13:39:42 +00:00
|
|
|
// 59.7275005696Hz
|
2022-03-22 17:41:18 +00:00
|
|
|
pub const frame_rate = @intToFloat(f64, std.time.ns_per_s) /
|
2022-03-22 13:39:42 +00:00
|
|
|
((@intToFloat(f64, std.time.ns_per_s) / @intToFloat(f64, clock_rate)) * @intToFloat(f64, cycles_per_frame));
|
|
|
|
|
2022-03-14 08:16:02 +00:00
|
|
|
const log = std.log.scoped(.Emulation);
|
2021-12-29 21:09:00 +00:00
|
|
|
|
2022-03-15 05:29:18 +00:00
|
|
|
const RunKind = enum {
|
|
|
|
Unlimited,
|
|
|
|
UnlimitedFPS,
|
|
|
|
Limited,
|
|
|
|
LimitedFPS,
|
|
|
|
LimitedBusy,
|
|
|
|
};
|
|
|
|
|
2022-07-21 14:05:49 +00:00
|
|
|
pub fn run(quit: *Atomic(bool), fps: *FpsTracker, sched: *Scheduler, cpu: *Arm7tdmi) void {
|
|
|
|
if (sync_audio) log.info("Audio sync enabled", .{});
|
2022-05-23 15:05:57 +00:00
|
|
|
|
2022-07-21 14:05:49 +00:00
|
|
|
switch (sync_video) {
|
2022-05-17 11:55:23 +00:00
|
|
|
.Unlimited => runUnsynchronized(quit, sched, cpu, null),
|
|
|
|
.Limited => runSynchronized(quit, sched, cpu, null),
|
|
|
|
.UnlimitedFPS => runUnsynchronized(quit, sched, cpu, fps),
|
|
|
|
.LimitedFPS => runSynchronized(quit, sched, cpu, fps),
|
2022-04-14 02:21:25 +00:00
|
|
|
.LimitedBusy => runBusyLoop(quit, sched, cpu),
|
2022-03-15 05:29:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-14 02:21:25 +00:00
|
|
|
pub fn runFrame(sched: *Scheduler, cpu: *Arm7tdmi) void {
|
2022-03-29 12:06:26 +00:00
|
|
|
const frame_end = sched.tick + cycles_per_frame;
|
|
|
|
|
2022-07-21 14:25:49 +00:00
|
|
|
while (sched.tick < frame_end) {
|
|
|
|
if (!cpu.stepDmaTransfer()) {
|
|
|
|
if (cpu.isHalted()) {
|
|
|
|
// Fast-forward to next Event
|
|
|
|
sched.tick = sched.queue.peek().?.tick;
|
|
|
|
} else {
|
|
|
|
cpu.step();
|
|
|
|
}
|
2021-12-29 21:09:00 +00:00
|
|
|
}
|
2022-05-25 15:15:11 +00:00
|
|
|
|
2022-07-21 14:25:49 +00:00
|
|
|
if (sched.tick >= sched.nextTimestamp()) sched.handleEvent(cpu);
|
2021-12-29 21:09:00 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-28 20:33:38 +00:00
|
|
|
|
2022-09-08 23:38:42 +00:00
|
|
|
fn syncToAudio(stream: *SDL.SDL_AudioStream, is_buffer_full: *bool) void {
|
|
|
|
const sample_size = 2 * @sizeOf(u16);
|
|
|
|
const max_buf_size: c_int = 0x400;
|
2022-05-23 15:05:57 +00:00
|
|
|
|
2022-09-08 23:38:42 +00:00
|
|
|
// Determine whether the APU is busy right at this moment
|
|
|
|
var still_full: bool = SDL.SDL_AudioStreamAvailable(stream) > sample_size * if (is_buffer_full.*) max_buf_size >> 1 else max_buf_size;
|
|
|
|
defer is_buffer_full.* = still_full; // Update APU Busy status right before exiting scope
|
|
|
|
|
|
|
|
// If Busy is false, there's no need to sync here
|
|
|
|
if (!still_full) return;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
still_full = SDL.SDL_AudioStreamAvailable(stream) > sample_size * max_buf_size >> 1;
|
|
|
|
if (!sync_audio or !still_full) break;
|
|
|
|
}
|
2022-05-23 15:05:57 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 15:38:44 +00:00
|
|
|
pub fn runUnsynchronized(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, fps: ?*FpsTracker) void {
|
2022-05-23 15:05:57 +00:00
|
|
|
log.info("Emulation thread w/out video sync", .{});
|
|
|
|
|
2022-05-17 11:55:23 +00:00
|
|
|
if (fps) |tracker| {
|
2022-05-23 15:05:57 +00:00
|
|
|
log.info("FPS Tracking Enabled", .{});
|
2022-05-17 11:55:23 +00:00
|
|
|
|
|
|
|
while (!quit.load(.SeqCst)) {
|
|
|
|
runFrame(sched, cpu);
|
2022-09-08 23:38:42 +00:00
|
|
|
syncToAudio(cpu.bus.apu.stream, &cpu.bus.apu.is_buffer_full);
|
2022-05-23 15:05:57 +00:00
|
|
|
|
2022-05-28 00:50:16 +00:00
|
|
|
tracker.tick();
|
2022-05-17 11:55:23 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-05-23 15:05:57 +00:00
|
|
|
while (!quit.load(.SeqCst)) {
|
|
|
|
runFrame(sched, cpu);
|
2022-09-08 23:38:42 +00:00
|
|
|
syncToAudio(cpu.bus.apu.stream, &cpu.bus.apu.is_buffer_full);
|
2022-05-23 15:05:57 +00:00
|
|
|
}
|
2022-05-17 11:55:23 +00:00
|
|
|
}
|
2022-03-15 05:29:18 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 15:38:44 +00:00
|
|
|
pub fn runSynchronized(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, fps: ?*FpsTracker) void {
|
2022-05-23 15:05:57 +00:00
|
|
|
log.info("Emulation thread w/ video sync", .{});
|
2022-09-08 23:38:42 +00:00
|
|
|
var timer = Timer.start() catch std.debug.panic("Failed to initialize std.timer.Timer", .{});
|
2022-03-15 05:29:18 +00:00
|
|
|
var wake_time: u64 = frame_period;
|
|
|
|
|
2022-05-17 11:55:23 +00:00
|
|
|
if (fps) |tracker| {
|
2022-05-23 15:05:57 +00:00
|
|
|
log.info("FPS Tracking Enabled", .{});
|
2022-03-14 11:54:48 +00:00
|
|
|
|
2022-05-17 11:55:23 +00:00
|
|
|
while (!quit.load(.SeqCst)) {
|
2022-05-23 15:05:57 +00:00
|
|
|
runFrame(sched, cpu);
|
2022-09-08 23:38:42 +00:00
|
|
|
const new_wake_time = blockOnVideo(&timer, wake_time);
|
2022-05-23 15:05:57 +00:00
|
|
|
|
|
|
|
// Spin to make up the difference of OS scheduler innacuracies
|
|
|
|
// If we happen to also be syncing to audio, we choose to spin on
|
|
|
|
// the amount of time needed for audio to catch up rather than
|
|
|
|
// our expected wake-up time
|
2022-09-08 23:38:42 +00:00
|
|
|
syncToAudio(cpu.bus.apu.stream, &cpu.bus.apu.is_buffer_full);
|
|
|
|
if (!sync_audio) spinLoop(&timer, wake_time);
|
2022-05-23 15:05:57 +00:00
|
|
|
wake_time = new_wake_time;
|
|
|
|
|
2022-05-28 00:50:16 +00:00
|
|
|
tracker.tick();
|
2022-05-17 11:55:23 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-05-23 15:05:57 +00:00
|
|
|
while (!quit.load(.SeqCst)) {
|
|
|
|
runFrame(sched, cpu);
|
2022-09-08 23:38:42 +00:00
|
|
|
const new_wake_time = blockOnVideo(&timer, wake_time);
|
2022-05-23 15:05:57 +00:00
|
|
|
|
2022-09-08 23:38:42 +00:00
|
|
|
// see above comment
|
|
|
|
syncToAudio(cpu.bus.apu.stream, &cpu.bus.apu.is_buffer_full);
|
|
|
|
if (!sync_audio) spinLoop(&timer, wake_time);
|
2022-05-23 15:05:57 +00:00
|
|
|
wake_time = new_wake_time;
|
|
|
|
}
|
2022-03-17 00:25:32 +00:00
|
|
|
}
|
2022-03-15 05:29:18 +00:00
|
|
|
}
|
2022-03-14 08:16:02 +00:00
|
|
|
|
2022-09-08 23:38:42 +00:00
|
|
|
inline fn blockOnVideo(timer: *Timer, wake_time: u64) u64 {
|
2022-05-23 15:05:57 +00:00
|
|
|
// Use the OS scheduler to put the emulation thread to sleep
|
|
|
|
const maybe_recalc_wake_time = sleep(timer, wake_time);
|
2022-03-14 11:54:48 +00:00
|
|
|
|
2022-05-23 15:05:57 +00:00
|
|
|
// If sleep() determined we need to adjust our wake up time, do so
|
|
|
|
// otherwise predict our next wake up time according to the frame period
|
|
|
|
return if (maybe_recalc_wake_time) |recalc| recalc else wake_time + frame_period;
|
2022-03-15 05:29:18 +00:00
|
|
|
}
|
2022-03-14 08:16:02 +00:00
|
|
|
|
2022-04-14 02:21:25 +00:00
|
|
|
pub fn runBusyLoop(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi) void {
|
2022-05-23 15:05:57 +00:00
|
|
|
log.info("Emulation thread with video sync using busy loop", .{});
|
2022-03-15 05:29:18 +00:00
|
|
|
var timer = Timer.start() catch unreachable;
|
|
|
|
var wake_time: u64 = frame_period;
|
2022-03-14 23:38:29 +00:00
|
|
|
|
2022-05-17 11:55:23 +00:00
|
|
|
while (!quit.load(.SeqCst)) {
|
2022-04-14 02:21:25 +00:00
|
|
|
runFrame(sched, cpu);
|
2022-03-15 05:29:18 +00:00
|
|
|
spinLoop(&timer, wake_time);
|
2022-03-14 23:38:29 +00:00
|
|
|
|
2022-09-08 23:38:42 +00:00
|
|
|
syncToAudio(cpu.bus.apu.stream, &cpu.bus.apu.is_buffer_full);
|
|
|
|
|
2022-03-15 05:29:18 +00:00
|
|
|
// Update to the new wake time
|
|
|
|
wake_time += frame_period;
|
2022-03-17 00:25:32 +00:00
|
|
|
}
|
2022-03-15 05:29:18 +00:00
|
|
|
}
|
2022-03-14 23:38:29 +00:00
|
|
|
|
2022-05-23 15:05:57 +00:00
|
|
|
fn sleep(timer: *Timer, wake_time: u64) ?u64 {
|
2022-03-17 00:25:32 +00:00
|
|
|
// const step = std.time.ns_per_ms * 10; // 10ms
|
2022-03-15 05:29:18 +00:00
|
|
|
const timestamp = timer.read();
|
2022-03-14 23:38:29 +00:00
|
|
|
|
2022-03-15 05:29:18 +00:00
|
|
|
// ns_late is non zero if we are late.
|
2022-05-23 15:05:57 +00:00
|
|
|
const ns_late = timestamp -| wake_time;
|
2022-03-14 23:38:29 +00:00
|
|
|
|
2022-03-15 05:29:18 +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"
|
2022-05-23 15:05:57 +00:00
|
|
|
if (ns_late > frame_period) return timestamp + frame_period;
|
2022-03-15 05:29:18 +00:00
|
|
|
const sleep_for = frame_period - ns_late;
|
|
|
|
|
2022-03-17 00:25:32 +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-03-15 05:29:18 +00:00
|
|
|
|
2022-03-17 00:25:32 +00:00
|
|
|
std.time.sleep(sleep_for);
|
2022-03-15 05:29:18 +00:00
|
|
|
|
2022-05-23 15:05:57 +00:00
|
|
|
return null;
|
2022-03-14 11:54:48 +00:00
|
|
|
}
|
2022-03-14 08:16:02 +00:00
|
|
|
|
2022-03-14 11:54:48 +00:00
|
|
|
fn spinLoop(timer: *Timer, wake_time: u64) void {
|
2022-03-14 23:38:29 +00:00
|
|
|
while (true) if (timer.read() > wake_time) break;
|
2022-01-28 20:33:38 +00:00
|
|
|
}
|