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:51 +00:00
|
|
|
const Io = @import("bus/io.zig").Io;
|
2022-10-21 08:11:50 +00:00
|
|
|
const Scheduler = @import("scheduler.zig").Scheduler;
|
|
|
|
|
|
|
|
const Allocator = std.mem.Allocator;
|
2022-10-21 08:11:54 +00:00
|
|
|
pub const width = 240;
|
|
|
|
pub const height = 160;
|
2022-10-21 08:12:15 +00:00
|
|
|
pub const framebuf_pitch = width * @sizeOf(u16);
|
2022-10-21 08:11:47 +00:00
|
|
|
|
|
|
|
pub const Ppu = struct {
|
2022-10-21 08:11:51 +00:00
|
|
|
const Self = @This();
|
|
|
|
|
2022-10-21 08:11:47 +00:00
|
|
|
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:12:15 +00:00
|
|
|
framebuf: []u8,
|
2022-10-21 08:11:51 +00:00
|
|
|
alloc: Allocator,
|
2022-10-21 08:11:47 +00:00
|
|
|
|
2022-10-21 08:11:51 +00:00
|
|
|
pub fn init(alloc: Allocator, sched: *Scheduler) !Self {
|
2022-10-21 08:11:49 +00:00
|
|
|
// Queue first Hblank
|
2022-10-21 08:11:51 +00:00
|
|
|
sched.push(.Draw, sched.tick + (240 * 4));
|
2022-10-21 08:11:51 +00:00
|
|
|
|
2022-10-21 08:12:15 +00:00
|
|
|
const framebuf = try alloc.alloc(u8, framebuf_pitch * height);
|
|
|
|
std.mem.set(u8, framebuf, 0);
|
|
|
|
|
2022-10-21 08:11:51 +00:00
|
|
|
return Self{
|
2022-10-21 08:11:47 +00:00
|
|
|
.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:12:15 +00:00
|
|
|
.framebuf = framebuf,
|
2022-10-21 08:11:51 +00:00
|
|
|
.alloc = alloc,
|
2022-10-21 08:11:47 +00:00
|
|
|
};
|
|
|
|
}
|
2022-10-21 08:11:47 +00:00
|
|
|
|
2022-10-21 08:11:51 +00:00
|
|
|
pub fn deinit(self: Self) void {
|
2022-10-21 08:12:15 +00:00
|
|
|
self.alloc.free(self.framebuf);
|
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:51 +00:00
|
|
|
|
2022-10-21 08:11:51 +00:00
|
|
|
pub fn drawScanline(self: *Self, io: *const Io) void {
|
2022-10-21 08:11:51 +00:00
|
|
|
const bg_mode = io.dispcnt.bg_mode.read();
|
|
|
|
const scanline = io.vcount.scanline.read();
|
|
|
|
|
|
|
|
switch (bg_mode) {
|
|
|
|
0x3 => {
|
2022-10-21 08:12:15 +00:00
|
|
|
const start = framebuf_pitch * @as(usize, scanline);
|
|
|
|
const end = start + framebuf_pitch;
|
2022-10-21 08:11:51 +00:00
|
|
|
|
2022-10-21 08:12:15 +00:00
|
|
|
std.mem.copy(u8, self.framebuf[start..end], self.vram.buf[start..end]);
|
2022-10-21 08:11:51 +00:00
|
|
|
},
|
2022-10-21 08:11:52 +00:00
|
|
|
0x4 => {
|
2022-10-21 08:12:09 +00:00
|
|
|
const select = io.dispcnt.frame_select.read();
|
|
|
|
const vram_start = width * @as(usize, scanline);
|
|
|
|
const buf_start = vram_start * @sizeOf(u16);
|
2022-10-21 08:11:52 +00:00
|
|
|
|
2022-10-21 08:12:09 +00:00
|
|
|
const start = vram_start + if (select) 0xA000 else @as(usize, 0);
|
|
|
|
const end = start + width; // Each Entry is only a byte long
|
2022-10-21 08:11:52 +00:00
|
|
|
|
2022-10-21 08:12:09 +00:00
|
|
|
// Render Current Scanline
|
2022-10-21 08:11:52 +00:00
|
|
|
for (self.vram.buf[start..end]) |byte, i| {
|
2022-10-21 08:12:09 +00:00
|
|
|
const id = byte * 2;
|
|
|
|
const j = i * @sizeOf(u16);
|
2022-10-21 08:11:52 +00:00
|
|
|
|
2022-10-21 08:12:15 +00:00
|
|
|
self.framebuf[buf_start + j + 1] = self.palette.buf[id + 1];
|
|
|
|
self.framebuf[buf_start + j] = self.palette.buf[id];
|
2022-10-21 08:11:52 +00:00
|
|
|
}
|
|
|
|
},
|
2022-10-21 08:12:08 +00:00
|
|
|
else => {}, // std.debug.panic("[PPU] TODO: Implement BG Mode {}", .{bg_mode}),
|
2022-10-21 08:11:51 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-21 08:11:47 +00:00
|
|
|
};
|
|
|
|
|
2022-10-21 08:11:48 +00:00
|
|
|
const Palette = struct {
|
2022-10-21 08:11:51 +00:00
|
|
|
const Self = @This();
|
|
|
|
|
2022-10-21 08:11:48 +00:00
|
|
|
buf: []u8,
|
|
|
|
alloc: Allocator,
|
|
|
|
|
2022-10-21 08:11:51 +00:00
|
|
|
fn init(alloc: Allocator) !Self {
|
2022-10-21 08:12:15 +00:00
|
|
|
const buf = try alloc.alloc(u8, 0x400);
|
|
|
|
std.mem.set(u8, buf, 0);
|
|
|
|
|
2022-10-21 08:11:51 +00:00
|
|
|
return Self{
|
2022-10-21 08:12:15 +00:00
|
|
|
.buf = buf,
|
2022-10-21 08:11:48 +00:00
|
|
|
.alloc = alloc,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:11:51 +00:00
|
|
|
fn deinit(self: Self) void {
|
2022-10-21 08:11:48 +00:00
|
|
|
self.alloc.free(self.buf);
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:11:52 +00:00
|
|
|
pub fn get32(self: *const Self, 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:52 +00:00
|
|
|
pub fn set32(self: *Self, 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:52 +00:00
|
|
|
pub fn get16(self: *const Self, idx: usize) u16 {
|
2022-10-21 08:11:48 +00:00
|
|
|
return (@as(u16, self.buf[idx + 1]) << 8) | @as(u16, self.buf[idx]);
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:11:52 +00:00
|
|
|
pub fn set16(self: *Self, idx: usize, halfword: u16) void {
|
2022-10-21 08:11:48 +00:00
|
|
|
self.buf[idx + 1] = @truncate(u8, halfword >> 8);
|
|
|
|
self.buf[idx] = @truncate(u8, halfword);
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:11:52 +00:00
|
|
|
pub fn get8(self: *const Self, idx: usize) u8 {
|
2022-10-21 08:11:48 +00:00
|
|
|
return self.buf[idx];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-10-21 08:11:47 +00:00
|
|
|
const Vram = struct {
|
2022-10-21 08:11:51 +00:00
|
|
|
const Self = @This();
|
|
|
|
|
2022-10-21 08:11:47 +00:00
|
|
|
buf: []u8,
|
2022-10-21 08:11:47 +00:00
|
|
|
alloc: Allocator,
|
2022-10-21 08:11:47 +00:00
|
|
|
|
2022-10-21 08:11:51 +00:00
|
|
|
fn init(alloc: Allocator) !Self {
|
2022-10-21 08:12:07 +00:00
|
|
|
const buf = try alloc.alloc(u8, 0x18000);
|
2022-10-21 08:12:15 +00:00
|
|
|
std.mem.set(u8, buf, 0);
|
2022-10-21 08:12:07 +00:00
|
|
|
|
2022-10-21 08:11:51 +00:00
|
|
|
return Self{
|
2022-10-21 08:12:07 +00:00
|
|
|
.buf = buf,
|
2022-10-21 08:11:47 +00:00
|
|
|
.alloc = alloc,
|
2022-10-21 08:11:47 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:11:51 +00:00
|
|
|
fn deinit(self: Self) void {
|
2022-10-21 08:11:47 +00:00
|
|
|
self.alloc.free(self.buf);
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:11:52 +00:00
|
|
|
pub fn get32(self: *const Self, 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
|
|
|
}
|
|
|
|
|
2022-10-21 08:11:52 +00:00
|
|
|
pub fn set32(self: *Self, 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
|
|
|
}
|
|
|
|
|
2022-10-21 08:11:52 +00:00
|
|
|
pub fn get16(self: *const Self, idx: usize) u16 {
|
2022-10-21 08:11:47 +00:00
|
|
|
return (@as(u16, self.buf[idx + 1]) << 8) | @as(u16, self.buf[idx]);
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:11:52 +00:00
|
|
|
pub fn set16(self: *Self, idx: usize, halfword: u16) void {
|
2022-10-21 08:11:47 +00:00
|
|
|
self.buf[idx + 1] = @truncate(u8, halfword >> 8);
|
|
|
|
self.buf[idx] = @truncate(u8, halfword);
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:11:52 +00:00
|
|
|
pub fn get8(self: *const Self, idx: usize) u8 {
|
2022-10-21 08:11:47 +00:00
|
|
|
return self.buf[idx];
|
|
|
|
}
|
|
|
|
};
|