2021-12-29 21:09:00 +00:00
|
|
|
const std = @import("std");
|
|
|
|
|
2022-01-07 23:49:58 +00:00
|
|
|
const Bus = @import("Bus.zig");
|
2022-01-02 03:08:36 +00:00
|
|
|
const Arm7tdmi = @import("cpu.zig").Arm7tdmi;
|
2022-01-08 00:00:42 +00:00
|
|
|
|
2021-12-29 21:09:00 +00:00
|
|
|
const Order = std.math.Order;
|
|
|
|
const PriorityQueue = std.PriorityQueue;
|
|
|
|
const Allocator = std.mem.Allocator;
|
2022-02-11 05:33:33 +00:00
|
|
|
const log = std.log.scoped(.Scheduler);
|
2021-12-29 21:09:00 +00:00
|
|
|
|
|
|
|
pub const Scheduler = struct {
|
2022-01-10 03:34:33 +00:00
|
|
|
const Self = @This();
|
|
|
|
|
2021-12-29 21:09:00 +00:00
|
|
|
tick: u64,
|
|
|
|
queue: PriorityQueue(Event, void, lessThan),
|
|
|
|
|
2022-01-10 03:34:33 +00:00
|
|
|
pub fn init(alloc: Allocator) Self {
|
|
|
|
var sched = Self{ .tick = 0, .queue = PriorityQueue(Event, void, lessThan).init(alloc, {}) };
|
|
|
|
sched.push(.HeatDeath, std.math.maxInt(u64));
|
2021-12-29 21:09:00 +00:00
|
|
|
|
2022-01-10 03:34:33 +00:00
|
|
|
return sched;
|
2021-12-29 21:09:00 +00:00
|
|
|
}
|
|
|
|
|
2022-01-10 03:34:33 +00:00
|
|
|
pub fn deinit(self: Self) void {
|
2022-01-04 02:08:55 +00:00
|
|
|
self.queue.deinit();
|
|
|
|
}
|
|
|
|
|
2022-03-13 08:28:51 +00:00
|
|
|
pub inline fn now(self: *const Self) u64 {
|
|
|
|
return self.tick;
|
|
|
|
}
|
|
|
|
|
2022-04-14 02:21:25 +00:00
|
|
|
pub fn handleEvent(self: *Self, cpu: *Arm7tdmi) void {
|
2022-03-15 07:34:13 +00:00
|
|
|
if (self.queue.removeOrNull()) |event| {
|
2022-04-09 22:15:12 +00:00
|
|
|
const late = self.tick - event.tick;
|
|
|
|
|
2021-12-29 21:09:00 +00:00
|
|
|
switch (event.kind) {
|
|
|
|
.HeatDeath => {
|
2022-04-21 13:15:52 +00:00
|
|
|
log.err("u64 overflow. This *actually* should never happen.", .{});
|
2022-03-13 08:28:51 +00:00
|
|
|
unreachable;
|
2021-12-29 21:09:00 +00:00
|
|
|
},
|
2022-01-09 14:22:50 +00:00
|
|
|
.Draw => {
|
2022-03-15 07:34:13 +00:00
|
|
|
// The end of a VDraw
|
2022-04-14 02:21:25 +00:00
|
|
|
cpu.bus.ppu.drawScanline();
|
|
|
|
cpu.bus.ppu.handleHDrawEnd(cpu, late);
|
2022-01-05 22:34:42 +00:00
|
|
|
},
|
2022-03-13 08:28:51 +00:00
|
|
|
.TimerOverflow => |id| {
|
|
|
|
switch (id) {
|
2022-05-05 23:09:00 +00:00
|
|
|
0 => cpu.bus.tim[0].handleOverflow(cpu, late),
|
|
|
|
1 => cpu.bus.tim[1].handleOverflow(cpu, late),
|
|
|
|
2 => cpu.bus.tim[2].handleOverflow(cpu, late),
|
|
|
|
3 => cpu.bus.tim[3].handleOverflow(cpu, late),
|
2022-03-13 08:28:51 +00:00
|
|
|
}
|
|
|
|
},
|
2022-04-20 12:39:12 +00:00
|
|
|
.ApuChannel => |id| {
|
|
|
|
switch (id) {
|
|
|
|
0 => cpu.bus.apu.ch1.channelTimerOverflow(late),
|
2022-04-21 00:33:46 +00:00
|
|
|
1 => cpu.bus.apu.ch2.channelTimerOverflow(late),
|
2022-04-21 03:21:55 +00:00
|
|
|
2 => cpu.bus.apu.ch3.channelTimerOverflow(late),
|
2022-04-21 05:40:02 +00:00
|
|
|
3 => cpu.bus.apu.ch4.channelTimerOverflow(late),
|
2022-04-20 12:39:12 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
.FrameSequencer => cpu.bus.apu.tickFrameSequencer(late),
|
2022-04-14 08:58:32 +00:00
|
|
|
.SampleAudio => cpu.bus.apu.sampleAudio(late),
|
2022-04-14 02:21:25 +00:00
|
|
|
.HBlank => cpu.bus.ppu.handleHBlankEnd(cpu, late), // The end of a HBlank
|
|
|
|
.VBlank => cpu.bus.ppu.handleHDrawEnd(cpu, late), // The end of a VBlank
|
2022-03-13 08:28:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-08 05:13:58 +00:00
|
|
|
/// Removes the **first** scheduled event of type `needle`
|
2022-03-13 08:28:51 +00:00
|
|
|
pub fn removeScheduledEvent(self: *Self, needle: EventKind) void {
|
|
|
|
var it = self.queue.iterator();
|
|
|
|
|
|
|
|
var i: usize = 0;
|
|
|
|
while (it.next()) |event| : (i += 1) {
|
|
|
|
if (std.meta.eql(event.kind, needle)) {
|
|
|
|
|
|
|
|
// This invalidates the iterator
|
|
|
|
_ = self.queue.removeIndex(i);
|
|
|
|
|
|
|
|
// Since removing something from the PQ invalidates the iterator,
|
|
|
|
// this implementation can safely only remove the first instance of
|
|
|
|
// a Scheduled Event. Exit Early
|
|
|
|
break;
|
2021-12-29 21:09:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 05:24:14 +00:00
|
|
|
pub fn push(self: *Self, kind: EventKind, end: u64) void {
|
2022-01-10 03:34:33 +00:00
|
|
|
self.queue.add(.{ .kind = kind, .tick = end }) catch unreachable;
|
2022-01-06 01:18:33 +00:00
|
|
|
}
|
|
|
|
|
2022-01-10 05:24:14 +00:00
|
|
|
pub fn nextTimestamp(self: *Self) u64 {
|
2022-03-15 07:34:13 +00:00
|
|
|
if (self.queue.peek()) |e| return e.tick;
|
|
|
|
|
|
|
|
unreachable; // There's always the HeatDeath event scheduled
|
2021-12-29 21:09:00 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const Event = struct {
|
|
|
|
kind: EventKind,
|
|
|
|
tick: u64,
|
|
|
|
};
|
|
|
|
|
2022-01-02 03:08:36 +00:00
|
|
|
fn lessThan(_: void, a: Event, b: Event) Order {
|
2021-12-29 21:09:00 +00:00
|
|
|
return std.math.order(a.tick, b.tick);
|
|
|
|
}
|
|
|
|
|
2022-03-13 08:28:51 +00:00
|
|
|
pub const EventKind = union(enum) {
|
2021-12-29 21:09:00 +00:00
|
|
|
HeatDeath,
|
2022-01-05 22:34:42 +00:00
|
|
|
HBlank,
|
|
|
|
VBlank,
|
2022-01-09 14:22:50 +00:00
|
|
|
Draw,
|
2022-03-13 08:28:51 +00:00
|
|
|
TimerOverflow: u2,
|
2022-04-14 08:58:32 +00:00
|
|
|
SampleAudio,
|
2022-04-20 12:39:12 +00:00
|
|
|
FrameSequencer,
|
|
|
|
ApuChannel: u2,
|
2021-12-29 21:09:00 +00:00
|
|
|
};
|