chore: write generic read/write for VRAM

This commit is contained in:
Rekai Nyangadzayi Musuka 2022-10-21 05:12:31 -03:00
parent 3c8390a248
commit effb6315e9
2 changed files with 28 additions and 25 deletions

View File

@ -61,7 +61,7 @@ pub fn read32(self: *const Self, addr: u32) u32 {
// Internal Display Memory
0x0500_0000...0x05FF_FFFF => self.ppu.palette.get32(addr & 0x3FF),
0x0600_0000...0x0601_7FFF => self.ppu.vram.get32(addr - 0x0600_0000),
0x0600_0000...0x0601_7FFF => self.ppu.vram.read(u32, addr - 0x0600_0000),
0x0700_0000...0x07FF_FFFF => self.ppu.oam.get32(addr & 0x3FF),
// External Memory (Game Pak)
@ -84,7 +84,7 @@ pub fn write32(self: *Self, addr: u32, word: u32) void {
// Internal Display Memory
0x0500_0000...0x05FF_FFFF => self.ppu.palette.set32(addr & 0x3FF, word),
0x0600_0000...0x0601_7FFF => self.ppu.vram.set32(addr - 0x0600_0000, word),
0x0600_0000...0x0601_7FFF => self.ppu.vram.write(u32, addr - 0x0600_0000, word),
0x0700_0000...0x07FF_FFFF => self.ppu.oam.set32(addr & 0x3FF, word),
else => undWrite("Tried to write 0x{X:0>8} to 0x{X:0>8}", .{ word, addr }),
@ -101,7 +101,7 @@ pub fn read16(self: *const Self, addr: u32) u16 {
// Internal Display Memory
0x0500_0000...0x05FF_FFFF => self.ppu.palette.get16(addr & 0x3FF),
0x0600_0000...0x0601_7FFF => self.ppu.vram.get16(addr - 0x0600_0000),
0x0600_0000...0x0601_7FFF => self.ppu.vram.read(u16, addr - 0x0600_0000),
0x0700_0000...0x07FF_FFFF => self.ppu.oam.get16(addr & 0x3FF),
// External Memory (Game Pak)
@ -123,7 +123,7 @@ pub fn write16(self: *Self, addr: u32, halfword: u16) void {
// Internal Display Memory
0x0500_0000...0x05FF_FFFF => self.ppu.palette.set16(addr & 0x3FF, halfword),
0x0600_0000...0x0601_7FFF => self.ppu.vram.set16(addr - 0x0600_0000, halfword),
0x0600_0000...0x0601_7FFF => self.ppu.vram.write(u16, addr - 0x0600_0000, halfword),
0x0700_0000...0x07FF_FFFF => self.ppu.oam.set16(addr & 0x3FF, halfword),
0x0800_00C4, 0x0800_00C6, 0x0800_00C8 => log.warn("Tried to write 0x{X:0>4} to GPIO", .{halfword}),
@ -141,7 +141,7 @@ pub fn read8(self: *const Self, addr: u32) u8 {
// Internal Display Memory
0x0500_0000...0x05FF_FFFF => self.ppu.palette.get8(addr & 0x3FF),
0x0600_0000...0x0601_7FFF => self.ppu.vram.get8(addr - 0x0600_0000),
0x0600_0000...0x0601_7FFF => self.ppu.vram.read(u8, addr - 0x0600_0000),
0x0700_0000...0x07FF_FFFF => self.ppu.oam.get8(addr & 0x3FF),
// External Memory (Game Pak)

View File

@ -241,7 +241,7 @@ pub const Ppu = struct {
// Grab the Screen Entry from VRAM
const entry_addr = screen_base + tilemapOffset(size, x, y);
const entry = @bitCast(ScreenEntry, self.vram.get16(entry_addr));
const entry = @bitCast(ScreenEntry, self.vram.read(u16, entry_addr));
// Calculate the Address of the Tile in the designated Charblock
// We also take this opportunity to flip tiles if necessary
@ -309,7 +309,7 @@ pub const Ppu = struct {
var i: usize = 0;
while (i < width) : (i += 1) {
const bgr555 = self.vram.get16(vram_base + i * @sizeOf(u16));
const bgr555 = self.vram.read(u16, vram_base + i * @sizeOf(u16));
std.mem.copy(u8, self.framebuf[fb_base + i * @sizeOf(u32) ..][0..4], &intToBytes(u32, toRgba8888(bgr555)));
}
},
@ -479,26 +479,29 @@ const Vram = struct {
self.alloc.free(self.buf);
}
pub fn get32(self: *const Self, idx: usize) u32 {
return (@as(u32, self.get16(idx + 2)) << 16) | @as(u32, self.get16(idx));
pub fn read(self: *const Self, comptime T: type, addr: usize) T {
switch (T) {
u32 => return (@as(T, self.buf[addr + 3]) << 24) | (@as(T, self.buf[addr + 2]) << 16) | (@as(T, self.buf[addr + 1]) << 8) | (@as(T, self.buf[addr])),
u16 => return (@as(T, self.buf[addr + 1]) << 8) | @as(T, self.buf[addr]),
u8 => return self.buf[addr],
else => @compileError("VRAM: Unsupported read width"),
}
}
pub fn set32(self: *Self, idx: usize, word: u32) void {
self.set16(idx + 2, @truncate(u16, word >> 16));
self.set16(idx, @truncate(u16, word));
pub fn write(self: *Self, comptime T: type, addr: usize, value: T) void {
switch (T) {
u32 => {
self.buf[addr + 3] = @truncate(u8, value >> 24);
self.buf[addr + 2] = @truncate(u8, value >> 16);
self.buf[addr + 1] = @truncate(u8, value >> 8);
self.buf[addr + 0] = @truncate(u8, value >> 0);
},
u16 => {
self.buf[addr + 1] = @truncate(u8, value >> 8);
self.buf[addr + 0] = @truncate(u8, value >> 0);
},
else => @compileError("VRAM: Unsupported write width"),
}
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];
}
};