feat(ppu): improve timings + implement BG mode 3 bitmap

This commit is contained in:
Rekai Nyangadzayi Musuka 2022-10-21 05:11:51 -03:00
parent c33068d831
commit 8348d7c4bc
3 changed files with 49 additions and 34 deletions

View File

@ -2,7 +2,7 @@ const Bus = @import("Bus.zig");
const Scheduler = @import("scheduler.zig").Scheduler; const Scheduler = @import("scheduler.zig").Scheduler;
const Arm7tdmi = @import("cpu.zig").Arm7tdmi; const Arm7tdmi = @import("cpu.zig").Arm7tdmi;
const cycles_per_frame: u64 = 100; // TODO: How many cycles actually? const cycles_per_frame: u64 = 160 * (308 * 4);
pub fn runFrame(sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void { pub fn runFrame(sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
var cycles: u64 = 0; var cycles: u64 = 0;

View File

@ -1,6 +1,7 @@
const std = @import("std"); const std = @import("std");
const EventKind = @import("scheduler.zig").EventKind; const EventKind = @import("scheduler.zig").EventKind;
const Io = @import("bus/io.zig").Io;
const Scheduler = @import("scheduler.zig").Scheduler; const Scheduler = @import("scheduler.zig").Scheduler;
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
@ -18,18 +19,13 @@ pub const Ppu = struct {
pub fn init(alloc: Allocator, sched: *Scheduler) !@This() { pub fn init(alloc: Allocator, sched: *Scheduler) !@This() {
// Queue first Hblank // Queue first Hblank
sched.push(.{ .kind = .HBlank, .tick = sched.tick + 240 * 4 }); sched.push(.{ .kind = .Draw, .tick = sched.tick + 240 * 4 });
// Initialize the Frame Buffer to white
const white_buf: [buf_len]u8 = [_]u8{ 0xFF, 0x7F } ** (buf_len / 2);
const frame_buf = try alloc.alloc(u8, buf_len);
std.mem.copy(u8, frame_buf, &white_buf);
return @This(){ return @This(){
.vram = try Vram.init(alloc), .vram = try Vram.init(alloc),
.palette = try Palette.init(alloc), .palette = try Palette.init(alloc),
.sched = sched, .sched = sched,
.frame_buf = frame_buf, .frame_buf = try alloc.alloc(u8, buf_len),
.alloc = alloc, .alloc = alloc,
}; };
} }
@ -39,6 +35,22 @@ pub const Ppu = struct {
self.vram.deinit(); self.vram.deinit();
self.palette.deinit(); self.palette.deinit();
} }
pub fn drawScanline(self: *@This(), io: *const Io) void {
const bg_mode = io.dispcnt.bg_mode.read();
const scanline = io.vcount.scanline.read();
switch (bg_mode) {
0x3 => {
// Mode 3
const start = buf_pitch * @as(usize, scanline);
const end = start + buf_pitch;
std.mem.copy(u8, self.frame_buf[start..end], self.vram.buf[start..end]);
},
else => std.debug.panic("[PPU] TODO: Implement BG Mode {}", .{bg_mode}),
}
}
}; };
const Palette = struct { const Palette = struct {

View File

@ -37,45 +37,48 @@ pub const Scheduler = struct {
std.debug.panic("[Scheduler] Somehow, a u64 overflowed", .{}); std.debug.panic("[Scheduler] Somehow, a u64 overflowed", .{});
}, },
.HBlank => { .HBlank => {
std.log.debug("[Scheduler] tick {}: Hblank", .{self.tick}); // The End of a Hblank
// We've reached the end of a scanline
const scanline = bus.io.vcount.scanline.read(); const scanline = bus.io.vcount.scanline.read();
bus.io.vcount.scanline.write(scanline + 1); const new_scanline = scanline + 1;
bus.io.dispstat.hblank.set(); // TODO: Should this be done @ end of Draw instead of end of Hblank?
bus.ppu.drawScanline(&bus.io);
if (scanline < 160) { bus.io.vcount.scanline.write(new_scanline);
self.push(.{ .kind = .Visible, .tick = self.tick + (68 * 4) }); bus.io.dispstat.hblank.unset();
if (new_scanline < 160) {
// Transitioning to another Draw
self.push(.{ .kind = .Draw, .tick = self.tick + (240 * 4) });
} else { } else {
self.push(.{ .kind = .VBlank, .tick = self.tick + (68 * 4) }); // Transitioning to a Vblank
bus.io.dispstat.vblank.set();
self.push(.{ .kind = .VBlank, .tick = self.tick + (308 * 4) });
} }
}, },
.Visible => { .Draw => {
std.log.debug("[Scheduler] tick {}: Visible", .{self.tick}); // The end of a Draw
// Beginning of a Scanline // Transitioning to a Hblank
bus.io.dispstat.hblank.unset(); bus.io.dispstat.hblank.set();
bus.io.dispstat.vblank.unset(); self.push(.{ .kind = .HBlank, .tick = self.tick + (68 * 4) });
self.push(.{ .kind = .HBlank, .tick = self.tick + (240 * 4) });
}, },
.VBlank => { .VBlank => {
std.log.debug("[Scheduler] tick {}: VBlank", .{self.tick}); // The end of a Vblank
// Beginning of a Scanline, not visible though
bus.io.dispstat.hblank.unset();
bus.io.dispstat.vblank.set();
const scanline = bus.io.vcount.scanline.read(); const scanline = bus.io.vcount.scanline.read();
bus.io.vcount.scanline.write(scanline + 1); const new_scanline = scanline + 1;
bus.io.vcount.scanline.write(new_scanline);
if (scanline < 227) { if (new_scanline < 228) {
// Another Vblank Scanline // Transition to another Vblank
self.push(.{ .kind = .VBlank, .tick = self.tick + 68 * (308 * 4) }); self.push(.{ .kind = .VBlank, .tick = self.tick + (308 * 4) });
} else { } else {
// Transition to another Draw
bus.io.vcount.scanline.write(0); // Reset Scanline bus.io.vcount.scanline.write(0); // Reset Scanline
self.push(.{ .kind = .Visible, .tick = self.tick + 68 * (308 * 4) });
bus.io.dispstat.vblank.unset();
self.push(.{ .kind = .Draw, .tick = self.tick + (240 * 4) });
} }
}, },
} }
@ -106,5 +109,5 @@ pub const EventKind = enum {
HeatDeath, HeatDeath,
HBlank, HBlank,
VBlank, VBlank,
Visible, Draw,
}; };