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;
|
|
|
|
|
|
|
|
pub const Scheduler = struct {
|
|
|
|
tick: u64,
|
|
|
|
queue: PriorityQueue(Event, void, lessThan),
|
|
|
|
|
2022-01-02 19:58:57 +00:00
|
|
|
pub fn init(alloc: Allocator) @This() {
|
2021-12-29 21:09:00 +00:00
|
|
|
var scheduler = Scheduler{ .tick = 0, .queue = PriorityQueue(Event, void, lessThan).init(alloc, {}) };
|
|
|
|
|
|
|
|
scheduler.queue.add(.{
|
|
|
|
.kind = EventKind.HeatDeath,
|
|
|
|
.tick = std.math.maxInt(u64),
|
|
|
|
}) catch unreachable;
|
|
|
|
|
|
|
|
return scheduler;
|
|
|
|
}
|
|
|
|
|
2022-01-07 23:16:02 +00:00
|
|
|
pub fn deinit(self: @This()) void {
|
2022-01-04 02:08:55 +00:00
|
|
|
self.queue.deinit();
|
|
|
|
}
|
|
|
|
|
2022-01-06 01:18:33 +00:00
|
|
|
pub fn handleEvent(self: *@This(), _: *Arm7tdmi, bus: *Bus) void {
|
2021-12-29 21:09:00 +00:00
|
|
|
const should_handle = if (self.queue.peek()) |e| self.tick >= e.tick else false;
|
|
|
|
|
|
|
|
if (should_handle) {
|
|
|
|
const event = self.queue.remove();
|
|
|
|
|
|
|
|
switch (event.kind) {
|
|
|
|
.HeatDeath => {
|
2022-01-02 20:40:49 +00:00
|
|
|
std.debug.panic("[Scheduler] Somehow, a u64 overflowed", .{});
|
2021-12-29 21:09:00 +00:00
|
|
|
},
|
2022-01-05 22:34:42 +00:00
|
|
|
.HBlank => {
|
2022-01-06 01:18:33 +00:00
|
|
|
std.log.debug("[Scheduler] tick {}: Hblank", .{self.tick});
|
|
|
|
|
|
|
|
// We've reached the end of a scanline
|
|
|
|
const scanline = bus.io.vcount.scanline.read();
|
|
|
|
bus.io.vcount.scanline.write(scanline + 1);
|
|
|
|
|
|
|
|
bus.io.dispstat.hblank.set();
|
|
|
|
|
|
|
|
if (scanline < 160) {
|
|
|
|
self.push(.{ .kind = .Visible, .tick = self.tick + (68 * 4) });
|
|
|
|
} else {
|
|
|
|
self.push(.{ .kind = .VBlank, .tick = self.tick + (68 * 4) });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
.Visible => {
|
|
|
|
std.log.debug("[Scheduler] tick {}: Visible", .{self.tick});
|
|
|
|
|
|
|
|
// Beginning of a Scanline
|
|
|
|
bus.io.dispstat.hblank.unset();
|
|
|
|
bus.io.dispstat.vblank.unset();
|
|
|
|
|
|
|
|
self.push(.{ .kind = .HBlank, .tick = self.tick + (240 * 4) });
|
2022-01-05 22:34:42 +00:00
|
|
|
},
|
|
|
|
.VBlank => {
|
2022-01-06 01:18:33 +00:00
|
|
|
std.log.debug("[Scheduler] tick {}: VBlank", .{self.tick});
|
|
|
|
|
|
|
|
// Beginning of a Scanline, not visible though
|
|
|
|
bus.io.dispstat.hblank.unset();
|
|
|
|
bus.io.dispstat.vblank.set();
|
|
|
|
|
|
|
|
const scanline = bus.io.vcount.scanline.read();
|
|
|
|
bus.io.vcount.scanline.write(scanline + 1);
|
|
|
|
|
|
|
|
if (scanline < 227) {
|
|
|
|
// Another Vblank Scanline
|
|
|
|
self.push(.{ .kind = .VBlank, .tick = self.tick + 68 * (308 * 4) });
|
|
|
|
} else {
|
|
|
|
bus.io.vcount.scanline.write(0); // Reset Scanline
|
|
|
|
self.push(.{ .kind = .Visible, .tick = self.tick + 68 * (308 * 4) });
|
|
|
|
}
|
2022-01-05 22:34:42 +00:00
|
|
|
},
|
2021-12-29 21:09:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-06 01:18:33 +00:00
|
|
|
pub inline fn push(self: *@This(), event: Event) void {
|
|
|
|
self.queue.add(event) catch unreachable;
|
|
|
|
}
|
|
|
|
|
2021-12-29 21:09:00 +00:00
|
|
|
pub inline fn nextTimestamp(self: *@This()) u64 {
|
|
|
|
if (self.queue.peek()) |e| {
|
|
|
|
return e.tick;
|
|
|
|
} else unreachable;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const EventKind = enum {
|
|
|
|
HeatDeath,
|
2022-01-05 22:34:42 +00:00
|
|
|
HBlank,
|
|
|
|
VBlank,
|
2022-01-06 01:18:33 +00:00
|
|
|
Visible,
|
2021-12-29 21:09:00 +00:00
|
|
|
};
|