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:03 +00:00
|
|
|
const Atomic = std.atomic.Atomic;
|
2022-10-21 08:11:51 +00:00
|
|
|
const cycles_per_frame: u64 = 160 * (308 * 4);
|
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:08 +00:00
|
|
|
pub fn runEmuThread(quit: *Atomic(bool), pause: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
|
2022-10-21 08:12:03 +00:00
|
|
|
while (!quit.load(.Unordered)) {
|
2022-10-21 08:12:08 +00:00
|
|
|
if (!pause.load(.Unordered)) runFrame(sched, cpu, bus);
|
2022-10-21 08:12:03 +00:00
|
|
|
}
|
|
|
|
}
|