From d54c8df7b34070cf718f53c8d2121e0945824424 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Fri, 21 Oct 2022 05:11:49 -0300 Subject: [PATCH] feat(sched): add HBlank and VBlank events to the scheduler --- src/bus.zig | 5 +++-- src/cpu.zig | 6 +++--- src/emu.zig | 8 ++++---- src/main.zig | 6 +++--- src/ppu.zig | 5 ++++- src/scheduler.zig | 8 ++++++++ 6 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/bus.zig b/src/bus.zig index 8f13e50..83f1bea 100644 --- a/src/bus.zig +++ b/src/bus.zig @@ -1,5 +1,6 @@ const std = @import("std"); +const Scheduler = @import("scheduler.zig").Scheduler; const Io = @import("bus/io.zig").Io; const Bios = @import("bus/bios.zig").Bios; const GamePak = @import("bus/pak.zig").GamePak; @@ -13,11 +14,11 @@ pub const Bus = struct { ppu: Ppu, io: Io, - pub fn init(alloc: Allocator, path: []const u8) !@This() { + pub fn init(alloc: Allocator, sched: *Scheduler, path: []const u8) !@This() { return @This(){ .pak = try GamePak.init(alloc, path), .bios = try Bios.init(alloc, "./bin/gba_bios.bin"), // TODO: don't hardcode this + bundle open-sorce Boot ROM - .ppu = try Ppu.init(alloc), + .ppu = try Ppu.init(alloc, sched), .io = Io.init(), }; } diff --git a/src/cpu.zig b/src/cpu.zig index 02b7718..2828a06 100644 --- a/src/cpu.zig +++ b/src/cpu.zig @@ -18,14 +18,14 @@ const arm_lut: [0x1000]InstrFn = populate(); pub const Arm7tdmi = struct { r: [16]u32, - sch: *Scheduler, + sched: *Scheduler, bus: *Bus, cpsr: CPSR, - pub fn init(scheduler: *Scheduler, bus: *Bus) @This() { + pub fn init(sched: *Scheduler, bus: *Bus) @This() { return .{ .r = [_]u32{0x00} ** 16, - .sch = scheduler, + .sched = sched, .bus = bus, .cpsr = .{ .raw = 0x0000_00DF }, }; diff --git a/src/emu.zig b/src/emu.zig index 5fe97b4..72f6569 100644 --- a/src/emu.zig +++ b/src/emu.zig @@ -4,14 +4,14 @@ const Bus = @import("bus.zig").Bus; const cycles_per_frame: u64 = 100; // TODO: How many cycles actually? -pub fn runFrame(sch: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void { +pub fn runFrame(sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void { var cycles: u64 = 0; while (cycles < cycles_per_frame) : (cycles += 1) { - sch.tick += 1; + sched.tick += 1; _ = cpu.step(); - while (sch.tick >= sch.nextTimestamp()) { - sch.handleEvent(cpu, bus); + while (sched.tick >= sched.nextTimestamp()) { + sched.handleEvent(cpu, bus); } } } diff --git a/src/main.zig b/src/main.zig index 6dadc7c..3f4892c 100644 --- a/src/main.zig +++ b/src/main.zig @@ -23,12 +23,12 @@ pub fn main() anyerror!void { return; } - var bus = try Bus.init(alloc, zba_args[0]); - defer bus.deinit(); - var scheduler = Scheduler.init(alloc); defer scheduler.deinit(); + var bus = try Bus.init(alloc, &scheduler, zba_args[0]); + defer bus.deinit(); + var cpu = Arm7tdmi.init(&scheduler, &bus); cpu.skipBios(); diff --git a/src/ppu.zig b/src/ppu.zig index 70738c7..fcd85c1 100644 --- a/src/ppu.zig +++ b/src/ppu.zig @@ -1,15 +1,18 @@ const std = @import("std"); const Allocator = std.mem.Allocator; +const Scheduler = @import("scheduler.zig").Scheduler; pub const Ppu = struct { vram: Vram, palette: Palette, + sched: *Scheduler, - pub fn init(alloc: Allocator) !@This() { + pub fn init(alloc: Allocator, sched: *Scheduler) !@This() { return @This(){ .vram = try Vram.init(alloc), .palette = try Palette.init(alloc), + .sched = sched, }; } diff --git a/src/scheduler.zig b/src/scheduler.zig index 5b6d202..fc4d38c 100644 --- a/src/scheduler.zig +++ b/src/scheduler.zig @@ -35,6 +35,12 @@ pub const Scheduler = struct { .HeatDeath => { std.debug.panic("[Scheduler] Somehow, a u64 overflowed", .{}); }, + .HBlank => { + std.debug.panic("[Scheduler] tick {}: Hblank", .{self.tick}); + }, + .VBlank => { + std.debug.panic("[Scheduler] tick {}: VBlank", .{self.tick}); + }, } } } @@ -57,4 +63,6 @@ fn lessThan(_: void, a: Event, b: Event) Order { pub const EventKind = enum { HeatDeath, + HBlank, + VBlank, };