From e7f646456499db4910ff3ef36aa8fcc0407dd1d8 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Fri, 21 Oct 2022 05:12:09 -0300 Subject: [PATCH] fix: resolve off by n * 2 when accessing Palette during BG Mode 4 --- src/ppu.zig | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/ppu.zig b/src/ppu.zig index 7d0d714..7949332 100644 --- a/src/ppu.zig +++ b/src/ppu.zig @@ -50,19 +50,20 @@ pub const Ppu = struct { std.mem.copy(u8, self.frame_buf[start..end], self.vram.buf[start..end]); }, 0x4 => { - const frame_select = io.dispcnt.frame_select.read(); + const select = io.dispcnt.frame_select.read(); + const vram_start = width * @as(usize, scanline); + const buf_start = vram_start * @sizeOf(u16); - 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; + const start = vram_start + if (select) 0xA000 else @as(usize, 0); + const end = start + width; // Each Entry is only a byte long + // Render Current Scanline for (self.vram.buf[start..end]) |byte, i| { - const fb_i = i * @sizeOf(u16); + const id = byte * 2; + const j = 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]; + self.frame_buf[buf_start + j + 1] = self.palette.buf[id + 1]; + self.frame_buf[buf_start + j] = self.palette.buf[id]; } }, else => {}, // std.debug.panic("[PPU] TODO: Implement BG Mode {}", .{bg_mode}),