Less buggy PPU

This commit is contained in:
FyloZ 2024-07-25 22:08:08 -04:00
parent 54825ecb5a
commit 96af510b19
Signed by: william
GPG Key ID: 835378AE9AF4AE97
4 changed files with 30 additions and 13 deletions

View File

@ -37,6 +37,7 @@ only tested on Linux. Here is how to run the project:
- Debug - Debug
- Frame Delay: Done - Frame Delay: Done
- Pattern Table Viewer: Done - Pattern Table Viewer: Done
- Nametable Viewer: Done
- CPU Debugger: To Do - CPU Debugger: To Do
- Memory Inspector: To Do - Memory Inspector: To Do
- PPU Debugger: To Do - PPU Debugger: To Do

View File

@ -37,8 +37,6 @@ bool gui_init() {
return false; return false;
} }
main_window_init(&gui.main_window);
#if DEBUG #if DEBUG
gui.tick = 0; gui.tick = 0;
pattern_window_init(&gui.pattern_window); pattern_window_init(&gui.pattern_window);
@ -47,6 +45,8 @@ bool gui_init() {
char_map_init(gui.main_window.sdl_context.renderer, gui.font); char_map_init(gui.main_window.sdl_context.renderer, gui.font);
#endif #endif
main_window_init(&gui.main_window);
return true; return true;
} }
@ -86,8 +86,6 @@ int gui_input() {
} }
void gui_render() { void gui_render() {
main_window_render(&gui.main_window, ppu_get_state()->pixels);
#if DEBUG #if DEBUG
pattern_window_render(&gui.pattern_window); pattern_window_render(&gui.pattern_window);
@ -96,15 +94,17 @@ void gui_render() {
gui.tick++; gui.tick++;
#endif #endif
main_window_render(&gui.main_window, ppu_get_state()->pixels);
} }
void gui_present() { void gui_present() {
main_window_present(&gui.main_window);
#if DEBUG #if DEBUG
pattern_window_present(&gui.pattern_window); pattern_window_present(&gui.pattern_window);
nametable_window_present(&gui.nametable_window); nametable_window_present(&gui.nametable_window);
#endif #endif
main_window_present(&gui.main_window);
} }
void gui_delay() { void gui_delay() {

1
main.c
View File

@ -24,6 +24,7 @@
int main() { int main() {
char *rom_path = "./test_roms/dk_jp.nes"; char *rom_path = "./test_roms/dk_jp.nes";
// char *rom_path = "./test_roms/nes-test-roms/other/BLOCKS.NES";
log_set_level(LOG_INFO); log_set_level(LOG_INFO);
if (!gui_init()) { if (!gui_init()) {

View File

@ -109,7 +109,19 @@ static inline void ppu_pixel_set_color(PPUPixel *pixel, byte pt_low, byte pt_hig
void ppu_draw_tile() { void ppu_draw_tile() {
PPUTileFetch fetch = ppu_state.tile_queue.displayed_fetch; 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]; PPUPixel *pixel = &ppu_state.pixels[pixel_index];
ppu_pixel_set_color(pixel, fetch.pattern_table_tile_low, fetch.pattern_table_tile_high); 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); return ppu_read(pattern_addr);
} }
void ppu_fetch_tile() { void ppu_fetch_tile(bool render) {
byte fetch_cycle = (ppu_state.cycle - 1) % 8; byte fetch_cycle = (ppu_state.cycle - 1) % 8;
if (fetch_cycle == 1) { if (fetch_cycle == 1) {
@ -150,7 +162,10 @@ void ppu_fetch_tile() {
} else if (fetch_cycle == 7) { } else if (fetch_cycle == 7) {
ppu_state.fetch.pattern_table_tile_high = ppu_get_pattern(ppu_state.fetch.nametable, 1); 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_state.tile_queue.displayed_fetch = ppu_state.fetch;
if (render) {
ppu_draw_tile(); ppu_draw_tile();
}
if ((ppu_state.ppu_address & 0x1f) == 0x1f) { if ((ppu_state.ppu_address & 0x1f) == 0x1f) {
ppu_state.ppu_address &= ~0x1f; ppu_state.ppu_address &= ~0x1f;
@ -169,8 +184,8 @@ void ppu_visible_frame(unsigned int cycle) {
if (cycle == 0) { if (cycle == 0) {
// Idle... // Idle...
} else if (cycle <= 256) { } else if (cycle >= 8 && cycle <= 256) {
ppu_fetch_tile(); ppu_fetch_tile(true);
if (cycle == 256) { if (cycle == 256) {
if ((ppu_state.ppu_address & 0x7000) != 0x7000) { 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.ppu_address = (ppu_state.ppu_address & 0xfbe0) | (ppu_state.temp_ppu_addr & ~0xfbe0);
ppu_state.x_scroll = 0; ppu_state.x_scroll = 0;
} }
} else if (cycle <= 336) { } else if (cycle <= 328) {
ppu_fetch_tile(); ppu_fetch_tile(false);
} }
} }