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
- Frame Delay: Done
- Pattern Table Viewer: Done
- Nametable Viewer: Done
- CPU Debugger: To Do
- Memory Inspector: To Do
- PPU Debugger: To Do

View File

@ -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() {

1
main.c
View File

@ -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()) {

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() {
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);
}
}