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 {
|
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-01-10 03:34:33 +00:00
|
|
|
pub fn handleEvent(self: *Self, _: *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();
|
2022-01-10 03:34:33 +00:00
|
|
|
std.log.info("[Scheduler] Handle {} at {} ticks", .{ event.kind, self.tick });
|
2021-12-29 21:09:00 +00:00
|
|
|
|
|
|
|
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-09 14:22:50 +00:00
|
|
|
// The End of a Hblank
|
2022-01-06 01:18:33 +00:00
|
|
|
const scanline = bus.io.vcount.scanline.read();
|
2022-01-09 14:22:50 +00:00
|
|
|
const new_scanline = scanline + 1;
|
2022-01-06 01:18:33 +00:00
|
|
|
|
2022-01-09 14:22:50 +00:00
|
|
|
// TODO: Should this be done @ end of Draw instead of end of Hblank?
|
|
|
|
bus.ppu.drawScanline(&bus.io);
|
2022-01-06 01:18:33 +00:00
|
|
|
|
2022-01-09 14:22:50 +00:00
|
|
|
bus.io.vcount.scanline.write(new_scanline);
|
|
|
|
bus.io.dispstat.hblank.unset();
|
|
|
|
|
|
|
|
if (new_scanline < 160) {
|
|
|
|
// Transitioning to another Draw
|
2022-01-10 03:34:33 +00:00
|
|
|
self.push(.Draw, self.tick + (240 * 4));
|
2022-01-06 01:18:33 +00:00
|
|
|
} else {
|
2022-01-09 14:22:50 +00:00
|
|
|
// Transitioning to a Vblank
|
|
|
|
bus.io.dispstat.vblank.set();
|
2022-01-10 03:34:33 +00:00
|
|
|
self.push(.VBlank, self.tick + (308 * 4));
|
2022-01-06 01:18:33 +00:00
|
|
|
}
|
|
|
|
},
|
2022-01-09 14:22:50 +00:00
|
|
|
.Draw => {
|
|
|
|
// The end of a Draw
|
2022-01-06 01:18:33 +00:00
|
|
|
|
2022-01-09 14:22:50 +00:00
|
|
|
// Transitioning to a Hblank
|
|
|
|
bus.io.dispstat.hblank.set();
|
2022-01-10 03:34:33 +00:00
|
|
|
self.push(.HBlank, self.tick + (68 * 4));
|
2022-01-05 22:34:42 +00:00
|
|
|
},
|
|
|
|
.VBlank => {
|
2022-01-09 14:22:50 +00:00
|
|
|
// The end of a Vblank
|
2022-01-06 01:18:33 +00:00
|
|
|
|
|
|
|
const scanline = bus.io.vcount.scanline.read();
|
2022-01-09 14:22:50 +00:00
|
|
|
const new_scanline = scanline + 1;
|
|
|
|
bus.io.vcount.scanline.write(new_scanline);
|
2022-01-06 01:18:33 +00:00
|
|
|
|
2022-01-09 14:22:50 +00:00
|
|
|
if (new_scanline < 228) {
|
|
|
|
// Transition to another Vblank
|
2022-01-10 03:34:33 +00:00
|
|
|
self.push(.VBlank, self.tick + (308 * 4));
|
2022-01-06 01:18:33 +00:00
|
|
|
} else {
|
2022-01-09 14:22:50 +00:00
|
|
|
// Transition to another Draw
|
2022-01-06 01:18:33 +00:00
|
|
|
bus.io.vcount.scanline.write(0); // Reset Scanline
|
2022-01-09 14:22:50 +00:00
|
|
|
|
|
|
|
bus.io.dispstat.vblank.unset();
|
2022-01-10 03:34:33 +00:00
|
|
|
self.push(.Draw, self.tick + (240 * 4));
|
2022-01-06 01:18:33 +00:00
|
|
|
}
|
2022-01-05 22:34:42 +00:00
|
|
|
},
|
2021-12-29 21:09:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 03:34:33 +00:00
|
|
|
pub inline fn push(self: *Self, kind: EventKind, end: u64) void {
|
|
|
|
self.queue.add(.{ .kind = kind, .tick = end }) catch unreachable;
|
2022-01-06 01:18:33 +00:00
|
|
|
}
|
|
|
|
|
2022-01-10 03:34:33 +00:00
|
|
|
pub inline fn nextTimestamp(self: *Self) u64 {
|
2021-12-29 21:09:00 +00:00
|
|
|
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-09 14:22:50 +00:00
|
|
|
Draw,
|
2021-12-29 21:09:00 +00:00
|
|
|
};
|