From 96af510b19df6508e58b6c4683e746bd2de14e99 Mon Sep 17 00:00:00 2001 From: FyloZ Date: Thu, 25 Jul 2024 22:08:08 -0400 Subject: [PATCH] Less buggy PPU --- README.md | 1 + gui/gui.c | 12 ++++++------ main.c | 1 + ppu/ppu.c | 29 ++++++++++++++++++++++------- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index d37c551..107b1a2 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ only tested on Linux. Here is how to run the project: - Debug - Frame Delay: Done - Pattern Table Viewer: Done + - Nametable Viewer: Done - CPU Debugger: To Do - Memory Inspector: To Do - PPU Debugger: To Do diff --git a/gui/gui.c b/gui/gui.c index 3460a3f..b377ab9 100644 --- a/gui/gui.c +++ b/gui/gui.c @@ -37,8 +37,6 @@ bool gui_init() { return false; } - main_window_init(&gui.main_window); - #if DEBUG gui.tick = 0; pattern_window_init(&gui.pattern_window); @@ -47,6 +45,8 @@ bool gui_init() { char_map_init(gui.main_window.sdl_context.renderer, gui.font); #endif + main_window_init(&gui.main_window); + return true; } @@ -86,8 +86,6 @@ int gui_input() { } void gui_render() { - main_window_render(&gui.main_window, ppu_get_state()->pixels); - #if DEBUG pattern_window_render(&gui.pattern_window); @@ -96,15 +94,17 @@ void gui_render() { gui.tick++; #endif + + main_window_render(&gui.main_window, ppu_get_state()->pixels); } void gui_present() { - main_window_present(&gui.main_window); - #if DEBUG pattern_window_present(&gui.pattern_window); nametable_window_present(&gui.nametable_window); #endif + + main_window_present(&gui.main_window); } void gui_delay() { diff --git a/main.c b/main.c index ec210e7..70075cf 100644 --- a/main.c +++ b/main.c @@ -24,6 +24,7 @@ int main() { char *rom_path = "./test_roms/dk_jp.nes"; +// char *rom_path = "./test_roms/nes-test-roms/other/BLOCKS.NES"; log_set_level(LOG_INFO); if (!gui_init()) { diff --git a/ppu/ppu.c b/ppu/ppu.c index 6a3ac31..3600108 100644 --- a/ppu/ppu.c +++ b/ppu/ppu.c @@ -109,7 +109,19 @@ static inline void ppu_pixel_set_color(PPUPixel *pixel, byte pt_low, byte pt_hig void ppu_draw_tile() { PPUTileFetch fetch = ppu_state.tile_queue.displayed_fetch; - unsigned int pixel_index = ppu_state.scanline * PPU_VISIBLE_FRAME_END + ppu_state.cycle; + unsigned int y = ppu_state.scanline; + unsigned int x = ppu_state.cycle + 0; + +// if (x > PPU_LINE_WIDTH) { +// x -= PPU_LINE_WIDTH; +// y++; +// } +// +// if (y > PPU_PRE_RENDER_LINE) { +// y -= PPU_PRE_RENDER_LINE; +// } + + unsigned int pixel_index = y * PPU_VISIBLE_FRAME_END + x; PPUPixel *pixel = &ppu_state.pixels[pixel_index]; ppu_pixel_set_color(pixel, fetch.pattern_table_tile_low, fetch.pattern_table_tile_high); } @@ -120,7 +132,7 @@ byte ppu_get_pattern(byte tile_index, byte high) { return ppu_read(pattern_addr); } -void ppu_fetch_tile() { +void ppu_fetch_tile(bool render) { byte fetch_cycle = (ppu_state.cycle - 1) % 8; if (fetch_cycle == 1) { @@ -150,7 +162,10 @@ void ppu_fetch_tile() { } else if (fetch_cycle == 7) { ppu_state.fetch.pattern_table_tile_high = ppu_get_pattern(ppu_state.fetch.nametable, 1); ppu_state.tile_queue.displayed_fetch = ppu_state.fetch; - ppu_draw_tile(); + + if (render) { + ppu_draw_tile(); + } if ((ppu_state.ppu_address & 0x1f) == 0x1f) { ppu_state.ppu_address &= ~0x1f; @@ -169,8 +184,8 @@ void ppu_visible_frame(unsigned int cycle) { if (cycle == 0) { // Idle... - } else if (cycle <= 256) { - ppu_fetch_tile(); + } else if (cycle >= 8 && cycle <= 256) { + ppu_fetch_tile(true); if (cycle == 256) { if ((ppu_state.ppu_address & 0x7000) != 0x7000) { @@ -194,8 +209,8 @@ void ppu_visible_frame(unsigned int cycle) { ppu_state.ppu_address = (ppu_state.ppu_address & 0xfbe0) | (ppu_state.temp_ppu_addr & ~0xfbe0); ppu_state.x_scroll = 0; } - } else if (cycle <= 336) { - ppu_fetch_tile(); + } else if (cycle <= 328) { + ppu_fetch_tile(false); } }