From bfdad9fa320091c2a4c238668e95979f1cfbb013 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Fri, 21 Oct 2022 05:12:19 -0300 Subject: [PATCH] feat: implement OAM --- src/Bus.zig | 10 +++++----- src/ppu.zig | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/Bus.zig b/src/Bus.zig index 07524ad..ec3ec77 100644 --- a/src/Bus.zig +++ b/src/Bus.zig @@ -49,7 +49,7 @@ pub fn read32(self: *const Self, addr: u32) u32 { // Internal Display Memory 0x0500_0000...0x0500_03FF => self.ppu.palette.get32(addr - 0x0500_0000), 0x0600_0000...0x0601_7FFF => self.ppu.vram.get32(addr - 0x0600_0000), - 0x0700_0000...0x0700_03FF => std.debug.panic("[Bus:32] read from 0x{X:} in OAM", .{addr}), + 0x0700_0000...0x0700_03FF => self.ppu.oam.get32(addr - 0x0700_0000), // External Memory (Game Pak) 0x0800_0000...0x09FF_FFFF => self.pak.get32(addr - 0x0800_0000), @@ -75,7 +75,7 @@ pub fn write32(self: *Self, addr: u32, word: u32) void { // Internal Display Memory 0x0500_0000...0x0500_03FF => self.ppu.palette.set32(addr - 0x0500_0000, word), 0x0600_0000...0x0601_7FFF => self.ppu.vram.set32(addr - 0x0600_0000, word), - 0x0700_0000...0x0700_03FF => std.debug.panic("[Bus:32] wrote 0x{X:} to 0x{X:} in OAM", .{ word, addr }), + 0x0700_0000...0x0700_03FF => self.ppu.oam.set32(addr - 0x0700_0000, word), else => log.warn("32-bit write of 0x{X:0>8} to 0x{X:0>8}", .{ word, addr }), } @@ -92,7 +92,7 @@ pub fn read16(self: *const Self, addr: u32) u16 { // Internal Display Memory 0x0500_0000...0x0500_03FF => self.ppu.palette.get16(addr - 0x0500_0000), 0x0600_0000...0x0601_7FFF => self.ppu.vram.get16(addr - 0x0600_0000), - 0x0700_0000...0x0700_03FF => std.debug.panic("[Bus:16] read from 0x{X:} in OAM", .{addr}), + 0x0700_0000...0x0700_03FF => self.ppu.oam.get16(addr - 0x0700_0000), // External Memory (Game Pak) 0x0800_0000...0x09FF_FFFF => self.pak.get16(addr - 0x0800_0000), @@ -117,7 +117,7 @@ pub fn write16(self: *Self, addr: u32, halfword: u16) void { // Internal Display Memory 0x0500_0000...0x0500_03FF => self.ppu.palette.set16(addr - 0x0500_0000, halfword), 0x0600_0000...0x0601_7FFF => self.ppu.vram.set16(addr - 0x0600_0000, halfword), - 0x0700_0000...0x0700_03FF => std.debug.panic("[Bus:16] write 0x{X:} to 0x{X:} in OAM", .{ halfword, addr }), + 0x0700_0000...0x0700_03FF => self.ppu.oam.set16(addr - 0x0700_0000, halfword), else => log.warn("16-bit write of 0x{X:0>4} to 0x{X:0>8}", .{ halfword, addr }), } @@ -134,7 +134,7 @@ pub fn read8(self: *const Self, addr: u32) u8 { // Internal Display Memory 0x0500_0000...0x0500_03FF => self.ppu.palette.get8(addr - 0x0500_0000), 0x0600_0000...0x0601_7FFF => self.ppu.vram.get8(addr - 0x0600_0000), - 0x0700_0000...0x0700_03FF => std.debug.panic("[Bus:8] read from 0x{X:} in OAM", .{addr}), + 0x0700_0000...0x0700_03FF => self.ppu.oam.get8(addr - 0x0700_0000), // External Memory (Game Pak) 0x0800_0000...0x09FF_FFFF => self.pak.get8(addr - 0x0800_0000), diff --git a/src/ppu.zig b/src/ppu.zig index 865022e..c89d00f 100644 --- a/src/ppu.zig +++ b/src/ppu.zig @@ -14,6 +14,7 @@ pub const Ppu = struct { vram: Vram, palette: Palette, + oam: Oam, sched: *Scheduler, framebuf: []u8, alloc: Allocator, @@ -28,6 +29,7 @@ pub const Ppu = struct { return Self{ .vram = try Vram.init(alloc), .palette = try Palette.init(alloc), + .oam = try Oam.init(alloc), .sched = sched, .framebuf = framebuf, .alloc = alloc, @@ -158,3 +160,44 @@ const Vram = struct { return self.buf[idx]; } }; + +const Oam = struct { + const Self = @This(); + + buf: []u8, + alloc: Allocator, + + fn init(alloc: Allocator) !Self { + const buf = try alloc.alloc(u8, 0x400); + std.mem.set(u8, buf, 0); + + return Self{ + .buf = buf, + .alloc = alloc, + }; + } + + pub fn get32(self: *const Self, idx: usize) u32 { + return (@as(u32, self.buf[idx + 3]) << 24) | (@as(u32, self.buf[idx + 2]) << 16) | (@as(u32, self.buf[idx + 1]) << 8) | (@as(u32, self.buf[idx])); + } + + pub fn set32(self: *Self, idx: usize, word: u32) void { + self.buf[idx + 3] = @truncate(u8, word >> 24); + self.buf[idx + 2] = @truncate(u8, word >> 16); + self.buf[idx + 1] = @truncate(u8, word >> 8); + self.buf[idx] = @truncate(u8, word); + } + + pub fn get16(self: *const Self, idx: usize) u16 { + return (@as(u16, self.buf[idx + 1]) << 8) | @as(u16, self.buf[idx]); + } + + pub fn set16(self: *Self, idx: usize, halfword: u16) void { + self.buf[idx + 1] = @truncate(u8, halfword >> 8); + self.buf[idx] = @truncate(u8, halfword); + } + + pub fn get8(self: *const Self, idx: usize) u8 { + return self.buf[idx]; + } +};