2021-12-29 21:09:00 +00:00
|
|
|
const std = @import("std");
|
|
|
|
|
2022-10-21 08:12:25 +00:00
|
|
|
const pollBlankingDma = @import("bus/dma.zig").pollBlankingDma;
|
|
|
|
|
2022-10-21 08:11:50 +00:00
|
|
|
const Bus = @import("Bus.zig");
|
2022-10-21 08:11:44 +00:00
|
|
|
const Arm7tdmi = @import("cpu.zig").Arm7tdmi;
|
2022-10-21 08:11:50 +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-10-21 08:12:18 +00:00
|
|
|
const log = std.log.scoped(.Scheduler);
|
2021-12-29 21:09:00 +00:00
|
|
|
|
|
|
|
pub const Scheduler = struct {
|
2022-10-21 08:11:51 +00:00
|
|
|
const Self = @This();
|
|
|
|
|
2021-12-29 21:09:00 +00:00
|
|
|
tick: u64,
|
|
|
|
queue: PriorityQueue(Event, void, lessThan),
|
|
|
|
|
2022-10-21 08:11:51 +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-10-21 08:11:51 +00:00
|
|
|
return sched;
|
2021-12-29 21:09:00 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 08:11:51 +00:00
|
|
|
pub fn deinit(self: Self) void {
|
2022-10-21 08:11:47 +00:00
|
|
|
self.queue.deinit();
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:12:26 +00:00
|
|
|
pub inline fn now(self: *const Self) u64 {
|
|
|
|
return self.tick;
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:12:22 +00:00
|
|
|
pub fn handleEvent(self: *Self, cpu: *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;
|
2022-10-21 08:12:22 +00:00
|
|
|
const stat = &bus.ppu.dispstat;
|
|
|
|
const vcount = &bus.ppu.vcount;
|
|
|
|
const irq = &bus.io.irq;
|
2021-12-29 21:09:00 +00:00
|
|
|
|
|
|
|
if (should_handle) {
|
|
|
|
const event = self.queue.remove();
|
2022-10-21 08:12:18 +00:00
|
|
|
// log.debug("Handle {} @ tick = {}", .{ event.kind, self.tick });
|
2021-12-29 21:09:00 +00:00
|
|
|
|
|
|
|
switch (event.kind) {
|
|
|
|
.HeatDeath => {
|
2022-10-21 08:12:26 +00:00
|
|
|
log.err("A u64 overflowered. This *actually* should never happen.", .{});
|
|
|
|
unreachable;
|
2021-12-29 21:09:00 +00:00
|
|
|
},
|
2022-10-21 08:11:49 +00:00
|
|
|
.HBlank => {
|
2022-10-21 08:12:03 +00:00
|
|
|
// The End of a Hblank (During Draw or Vblank)
|
2022-10-21 08:12:22 +00:00
|
|
|
const old_scanline = vcount.scanline.read();
|
2022-10-21 08:12:03 +00:00
|
|
|
const scanline = (old_scanline + 1) % 228;
|
2022-10-21 08:11:49 +00:00
|
|
|
|
2022-10-21 08:12:22 +00:00
|
|
|
vcount.scanline.write(scanline);
|
|
|
|
stat.hblank.unset();
|
|
|
|
|
|
|
|
// Perform Vc == VcT check
|
|
|
|
const coincidence = scanline == stat.vcount_trigger.read();
|
|
|
|
stat.coincidence.write(coincidence);
|
|
|
|
|
|
|
|
if (coincidence and stat.vcount_irq.read()) {
|
|
|
|
irq.coincidence.set();
|
|
|
|
cpu.handleInterrupt();
|
|
|
|
}
|
2022-10-21 08:11:51 +00:00
|
|
|
|
2022-10-21 08:12:03 +00:00
|
|
|
if (scanline < 160) {
|
2022-10-21 08:11:51 +00:00
|
|
|
// Transitioning to another Draw
|
2022-10-21 08:11:51 +00:00
|
|
|
self.push(.Draw, self.tick + (240 * 4));
|
2022-10-21 08:11:49 +00:00
|
|
|
} else {
|
2022-10-21 08:11:51 +00:00
|
|
|
// Transitioning to a Vblank
|
2022-10-21 08:12:22 +00:00
|
|
|
if (scanline == 160) {
|
|
|
|
stat.vblank.set();
|
2022-10-21 08:12:03 +00:00
|
|
|
|
2022-10-21 08:12:22 +00:00
|
|
|
if (stat.vblank_irq.read()) {
|
|
|
|
irq.vblank.set();
|
|
|
|
cpu.handleInterrupt();
|
|
|
|
}
|
2022-10-21 08:12:25 +00:00
|
|
|
|
|
|
|
// See if Vblank DMA is present and not enabled
|
|
|
|
pollBlankingDma(bus, .VBlank);
|
2022-10-21 08:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (scanline == 227) stat.vblank.unset();
|
2022-10-21 08:12:03 +00:00
|
|
|
self.push(.VBlank, self.tick + (240 * 4));
|
2022-10-21 08:11:49 +00:00
|
|
|
}
|
|
|
|
},
|
2022-10-21 08:11:51 +00:00
|
|
|
.Draw => {
|
|
|
|
// The end of a Draw
|
2022-10-21 08:12:20 +00:00
|
|
|
bus.ppu.drawScanline();
|
2022-10-21 08:11:49 +00:00
|
|
|
|
2022-10-21 08:11:51 +00:00
|
|
|
// Transitioning to a Hblank
|
2022-10-21 08:12:22 +00:00
|
|
|
if (bus.ppu.dispstat.hblank_irq.read()) {
|
|
|
|
bus.io.irq.hblank.set();
|
|
|
|
cpu.handleInterrupt();
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:12:25 +00:00
|
|
|
// See if Hblank DMA is present and not enabled
|
|
|
|
pollBlankingDma(bus, .HBlank);
|
|
|
|
|
2022-10-21 08:12:20 +00:00
|
|
|
bus.ppu.dispstat.hblank.set();
|
2022-10-21 08:11:51 +00:00
|
|
|
self.push(.HBlank, self.tick + (68 * 4));
|
2022-10-21 08:11:49 +00:00
|
|
|
},
|
|
|
|
.VBlank => {
|
2022-10-21 08:11:51 +00:00
|
|
|
// The end of a Vblank
|
2022-10-21 08:12:22 +00:00
|
|
|
|
|
|
|
// Transitioning to a Hblank
|
|
|
|
if (bus.ppu.dispstat.hblank_irq.read()) {
|
|
|
|
bus.io.irq.hblank.set();
|
|
|
|
cpu.handleInterrupt();
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:12:25 +00:00
|
|
|
// See if Hblank DMA is present and not enabled
|
|
|
|
pollBlankingDma(bus, .HBlank);
|
|
|
|
|
2022-10-21 08:12:22 +00:00
|
|
|
bus.ppu.dispstat.hblank.set();
|
2022-10-21 08:12:03 +00:00
|
|
|
self.push(.HBlank, self.tick + (68 * 4));
|
2022-10-21 08:11:49 +00:00
|
|
|
},
|
2022-10-21 08:12:26 +00:00
|
|
|
.TimerOverflow => |id| {
|
|
|
|
// log.warn("TIM{} Overflowed", .{id});
|
|
|
|
|
|
|
|
switch (id) {
|
|
|
|
0 => bus.io.tim0.handleOverflow(cpu, &bus.io),
|
|
|
|
1 => bus.io.tim1.handleOverflow(cpu, &bus.io),
|
|
|
|
2 => bus.io.tim2.handleOverflow(cpu, &bus.io),
|
|
|
|
3 => bus.io.tim3.handleOverflow(cpu, &bus.io),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Removes the **first** scheduled event of type `needle`
|
|
|
|
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-10-21 08:11:52 +00:00
|
|
|
pub fn push(self: *Self, kind: EventKind, end: u64) void {
|
2022-10-21 08:11:51 +00:00
|
|
|
self.queue.add(.{ .kind = kind, .tick = end }) catch unreachable;
|
2022-10-21 08:11:49 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 08:11:52 +00:00
|
|
|
pub 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-10-21 08:11:44 +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-10-21 08:12:26 +00:00
|
|
|
pub const EventKind = union(enum) {
|
2021-12-29 21:09:00 +00:00
|
|
|
HeatDeath,
|
2022-10-21 08:11:49 +00:00
|
|
|
HBlank,
|
|
|
|
VBlank,
|
2022-10-21 08:11:51 +00:00
|
|
|
Draw,
|
2022-10-21 08:12:26 +00:00
|
|
|
TimerOverflow: u2,
|
2021-12-29 21:09:00 +00:00
|
|
|
};
|