zba/src/ppu.zig

160 lines
4.5 KiB
Zig
Raw Normal View History

2022-01-04 01:47:26 +00:00
const std = @import("std");
const EventKind = @import("scheduler.zig").EventKind;
const Io = @import("bus/io.zig").Io;
2022-01-08 00:00:42 +00:00
const Scheduler = @import("scheduler.zig").Scheduler;
const Allocator = std.mem.Allocator;
2022-01-12 04:46:20 +00:00
pub const width = 240;
pub const height = 160;
2022-01-09 00:30:57 +00:00
pub const buf_pitch = width * @sizeOf(u16);
const buf_len = buf_pitch * height;
2022-01-04 01:47:26 +00:00
pub const Ppu = struct {
2022-01-10 03:34:33 +00:00
const Self = @This();
2022-01-04 01:47:26 +00:00
vram: Vram,
palette: Palette,
sched: *Scheduler,
2022-01-09 00:30:57 +00:00
frame_buf: []u8,
alloc: Allocator,
2022-01-04 01:47:26 +00:00
2022-01-10 03:34:33 +00:00
pub fn init(alloc: Allocator, sched: *Scheduler) !Self {
// Queue first Hblank
2022-01-10 03:34:33 +00:00
sched.push(.Draw, sched.tick + (240 * 4));
2022-01-09 00:30:57 +00:00
2022-01-10 03:34:33 +00:00
return Self{
2022-01-04 01:47:26 +00:00
.vram = try Vram.init(alloc),
.palette = try Palette.init(alloc),
.sched = sched,
.frame_buf = try alloc.alloc(u8, buf_len),
2022-01-09 00:30:57 +00:00
.alloc = alloc,
2022-01-04 01:47:26 +00:00
};
}
2022-01-04 02:08:55 +00:00
2022-01-10 03:34:33 +00:00
pub fn deinit(self: Self) void {
2022-01-09 00:30:57 +00:00
self.alloc.free(self.frame_buf);
2022-01-04 02:08:55 +00:00
self.vram.deinit();
self.palette.deinit();
2022-01-04 02:08:55 +00:00
}
2022-01-10 03:34:33 +00:00
pub fn drawScanline(self: *Self, io: *const Io) void {
const bg_mode = io.dispcnt.bg_mode.read();
const scanline = io.vcount.scanline.read();
switch (bg_mode) {
0x3 => {
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]);
},
0x4 => {
const frame_select = io.dispcnt.frame_select.read();
const fb_start = buf_pitch * @as(usize, scanline);
const vram_start = fb_start >> 1;
const start = if (frame_select) 0xA000 + vram_start else vram_start;
const end = start + width;
for (self.vram.buf[start..end]) |byte, i| {
const fb_i = i * @sizeOf(u16);
self.frame_buf[fb_start + fb_i + 1] = self.palette.buf[byte + 1];
self.frame_buf[fb_start + fb_i] = self.palette.buf[byte];
}
},
else => {}, // std.debug.panic("[PPU] TODO: Implement BG Mode {}", .{bg_mode}),
}
}
2022-01-04 01:47:26 +00:00
};
const Palette = struct {
2022-01-10 03:34:33 +00:00
const Self = @This();
buf: []u8,
alloc: Allocator,
2022-01-10 03:34:33 +00:00
fn init(alloc: Allocator) !Self {
return Self{
.buf = try alloc.alloc(u8, 0x400),
.alloc = alloc,
};
}
2022-01-10 03:34:33 +00:00
fn deinit(self: Self) void {
self.alloc.free(self.buf);
}
2022-01-10 05:24:14 +00:00
pub fn get32(self: *const Self, idx: usize) u32 {
return (@as(u32, self.get16(idx + 2)) << 16) | @as(u32, self.get16(idx));
}
2022-01-10 05:24:14 +00:00
pub fn set32(self: *Self, idx: usize, word: u32) void {
self.set16(idx + 2, @truncate(u16, word >> 16));
self.set16(idx, @truncate(u16, word));
}
2022-01-10 05:24:14 +00:00
pub fn get16(self: *const Self, idx: usize) u16 {
return (@as(u16, self.buf[idx + 1]) << 8) | @as(u16, self.buf[idx]);
}
2022-01-10 05:24:14 +00:00
pub fn set16(self: *Self, idx: usize, halfword: u16) void {
self.buf[idx + 1] = @truncate(u8, halfword >> 8);
self.buf[idx] = @truncate(u8, halfword);
}
2022-01-10 05:24:14 +00:00
pub fn get8(self: *const Self, idx: usize) u8 {
return self.buf[idx];
}
};
2022-01-04 01:47:26 +00:00
const Vram = struct {
2022-01-10 03:34:33 +00:00
const Self = @This();
2022-01-04 01:47:26 +00:00
buf: []u8,
2022-01-04 02:08:55 +00:00
alloc: Allocator,
2022-01-04 01:47:26 +00:00
2022-01-10 03:34:33 +00:00
fn init(alloc: Allocator) !Self {
2022-01-30 06:39:16 +00:00
// In Modes 3 and 4, parts of the VRAM are copied to the
// frame buffer, therefore we want to zero-initialize Vram
//
// some programs like Armwrestler assume that VRAM is zeroed-out.
const black = std.mem.zeroes([0x18000]u8);
const buf = try alloc.alloc(u8, 0x18000);
std.mem.copy(u8, buf, &black);
2022-01-10 03:34:33 +00:00
return Self{
2022-01-30 06:39:16 +00:00
.buf = buf,
2022-01-04 02:08:55 +00:00
.alloc = alloc,
2022-01-04 01:47:26 +00:00
};
}
2022-01-10 03:34:33 +00:00
fn deinit(self: Self) void {
2022-01-04 02:08:55 +00:00
self.alloc.free(self.buf);
}
2022-01-10 05:24:14 +00:00
pub fn get32(self: *const Self, idx: usize) u32 {
return (@as(u32, self.get16(idx + 2)) << 16) | @as(u32, self.get16(idx));
2022-01-04 01:47:26 +00:00
}
2022-01-10 05:24:14 +00:00
pub fn set32(self: *Self, idx: usize, word: u32) void {
self.set16(idx + 2, @truncate(u16, word >> 16));
self.set16(idx, @truncate(u16, word));
2022-01-04 01:47:26 +00:00
}
2022-01-10 05:24:14 +00:00
pub fn get16(self: *const Self, idx: usize) u16 {
2022-01-04 01:47:26 +00:00
return (@as(u16, self.buf[idx + 1]) << 8) | @as(u16, self.buf[idx]);
}
2022-01-10 05:24:14 +00:00
pub fn set16(self: *Self, idx: usize, halfword: u16) void {
2022-01-04 01:47:26 +00:00
self.buf[idx + 1] = @truncate(u8, halfword >> 8);
self.buf[idx] = @truncate(u8, halfword);
}
2022-01-10 05:24:14 +00:00
pub fn get8(self: *const Self, idx: usize) u8 {
2022-01-04 01:47:26 +00:00
return self.buf[idx];
}
};