2022-10-21 08:11:47 +00:00
|
|
|
const std = @import("std");
|
|
|
|
|
2022-10-21 08:11:49 +00:00
|
|
|
const EventKind = @import("scheduler.zig").EventKind;
|
2022-10-21 08:11:50 +00:00
|
|
|
const Scheduler = @import("scheduler.zig").Scheduler;
|
|
|
|
|
|
|
|
const Allocator = std.mem.Allocator;
|
2022-10-21 08:11:47 +00:00
|
|
|
|
|
|
|
pub const Ppu = struct {
|
|
|
|
vram: Vram,
|
2022-10-21 08:11:48 +00:00
|
|
|
palette: Palette,
|
2022-10-21 08:11:49 +00:00
|
|
|
sched: *Scheduler,
|
2022-10-21 08:11:47 +00:00
|
|
|
|
2022-10-21 08:11:49 +00:00
|
|
|
pub fn init(alloc: Allocator, sched: *Scheduler) !@This() {
|
2022-10-21 08:11:49 +00:00
|
|
|
// Queue first Hblank
|
|
|
|
sched.push(.{ .kind = .HBlank, .tick = sched.tick + 240 * 4 });
|
|
|
|
|
2022-10-21 08:11:47 +00:00
|
|
|
return @This(){
|
|
|
|
.vram = try Vram.init(alloc),
|
2022-10-21 08:11:48 +00:00
|
|
|
.palette = try Palette.init(alloc),
|
2022-10-21 08:11:49 +00:00
|
|
|
.sched = sched,
|
2022-10-21 08:11:47 +00:00
|
|
|
};
|
|
|
|
}
|
2022-10-21 08:11:47 +00:00
|
|
|
|
2022-10-21 08:11:49 +00:00
|
|
|
pub fn deinit(self: @This()) void {
|
2022-10-21 08:11:47 +00:00
|
|
|
self.vram.deinit();
|
2022-10-21 08:11:50 +00:00
|
|
|
self.palette.deinit();
|
2022-10-21 08:11:47 +00:00
|
|
|
}
|
2022-10-21 08:11:47 +00:00
|
|
|
};
|
|
|
|
|
2022-10-21 08:11:48 +00:00
|
|
|
const Palette = struct {
|
|
|
|
buf: []u8,
|
|
|
|
alloc: Allocator,
|
|
|
|
|
|
|
|
fn init(alloc: Allocator) !@This() {
|
|
|
|
return @This(){
|
|
|
|
.buf = try alloc.alloc(u8, 0x400),
|
|
|
|
.alloc = alloc,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:11:49 +00:00
|
|
|
fn deinit(self: @This()) void {
|
2022-10-21 08:11:48 +00:00
|
|
|
self.alloc.free(self.buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub inline fn get32(self: *const @This(), idx: usize) u32 {
|
|
|
|
return (@as(u32, self.get16(idx + 2)) << 16) | @as(u32, self.get16(idx));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub inline fn set32(self: *@This(), idx: usize, word: u32) void {
|
|
|
|
self.set16(idx + 2, @truncate(u16, word >> 16));
|
|
|
|
self.set16(idx, @truncate(u16, word));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub inline fn get16(self: *const @This(), idx: usize) u16 {
|
|
|
|
return (@as(u16, self.buf[idx + 1]) << 8) | @as(u16, self.buf[idx]);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub inline fn set16(self: *@This(), idx: usize, halfword: u16) void {
|
|
|
|
self.buf[idx + 1] = @truncate(u8, halfword >> 8);
|
|
|
|
self.buf[idx] = @truncate(u8, halfword);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub inline fn get8(self: *const @This(), idx: usize) u8 {
|
|
|
|
return self.buf[idx];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-10-21 08:11:47 +00:00
|
|
|
const Vram = struct {
|
|
|
|
buf: []u8,
|
2022-10-21 08:11:47 +00:00
|
|
|
alloc: Allocator,
|
2022-10-21 08:11:47 +00:00
|
|
|
|
|
|
|
fn init(alloc: Allocator) !@This() {
|
|
|
|
return @This(){
|
|
|
|
.buf = try alloc.alloc(u8, 0x18000),
|
2022-10-21 08:11:47 +00:00
|
|
|
.alloc = alloc,
|
2022-10-21 08:11:47 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:11:49 +00:00
|
|
|
fn deinit(self: @This()) void {
|
2022-10-21 08:11:47 +00:00
|
|
|
self.alloc.free(self.buf);
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:11:47 +00:00
|
|
|
pub inline fn get32(self: *const @This(), idx: usize) u32 {
|
2022-10-21 08:11:48 +00:00
|
|
|
return (@as(u32, self.get16(idx + 2)) << 16) | @as(u32, self.get16(idx));
|
2022-10-21 08:11:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub inline fn set32(self: *@This(), idx: usize, word: u32) void {
|
2022-10-21 08:11:48 +00:00
|
|
|
self.set16(idx + 2, @truncate(u16, word >> 16));
|
|
|
|
self.set16(idx, @truncate(u16, word));
|
2022-10-21 08:11:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub inline fn get16(self: *const @This(), idx: usize) u16 {
|
|
|
|
return (@as(u16, self.buf[idx + 1]) << 8) | @as(u16, self.buf[idx]);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub inline fn set16(self: *@This(), idx: usize, halfword: u16) void {
|
|
|
|
self.buf[idx + 1] = @truncate(u8, halfword >> 8);
|
|
|
|
self.buf[idx] = @truncate(u8, halfword);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub inline fn get8(self: *const @This(), idx: usize) u8 {
|
|
|
|
return self.buf[idx];
|
|
|
|
}
|
|
|
|
};
|