From 0066e77455c69508ef24acb9cddf5754c8e6e4b0 Mon Sep 17 00:00:00 2001 From: william Date: Tue, 6 Aug 2024 17:30:57 -0400 Subject: [PATCH] Add palette switch to the debug pattern view. --- README.md | 4 ++++ gui/dbg_pattern_table.c | 4 ++-- gui/dbg_pattern_table.h | 3 ++- gui/gui.c | 11 +++++++++-- gui/nametable_window.h | 2 +- gui/pattern_window.c | 18 ++++++++++++++++-- gui/pattern_window.h | 3 +++ main.c | 4 ++-- 8 files changed, 39 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 107b1a2..fdfd82c 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,10 @@ only tested on Linux. Here is how to run the project: - Build the project with Make: ```make``` - Run the emulator: ```./nes_emulator``` +## Controls + - `p`: Pauses the emulation + - `o`: Go to the next palette in the pattern viewer + ## Dependencies - GCC compiler - CMake diff --git a/gui/dbg_pattern_table.c b/gui/dbg_pattern_table.c index 9ad34f5..ac49fb1 100644 --- a/gui/dbg_pattern_table.c +++ b/gui/dbg_pattern_table.c @@ -93,7 +93,7 @@ void dbg_pattern_draw_pos(int x, int y, int bank, pixel *buffer, int buffer_widt dbg_pattern_draw_pattern(pattern, buffer, buffer_width, palette); } -void dbg_pattern_draw_bank(int bank, pixel *buffer) { +void dbg_pattern_draw_bank(int bank, pixel *buffer, int palette) { int buffer_width = PATTERN_TABLE_WIDTH * PATTERN_DRAW_SIZE; for (int x = 0; x < PATTERN_TABLE_WIDTH; x++) { @@ -101,7 +101,7 @@ void dbg_pattern_draw_bank(int bank, pixel *buffer) { address row_addr = (y * PATTERN_DRAW_SIZE) * buffer_width; address tile_addr = row_addr + (x * PATTERN_DRAW_SIZE); - dbg_pattern_draw_pos(x, y, bank, &buffer[tile_addr], buffer_width, 01); + dbg_pattern_draw_pos(x, y, bank, &buffer[tile_addr], buffer_width, palette); } } } \ No newline at end of file diff --git a/gui/dbg_pattern_table.h b/gui/dbg_pattern_table.h index e0916f3..337fbbb 100644 --- a/gui/dbg_pattern_table.h +++ b/gui/dbg_pattern_table.h @@ -78,7 +78,8 @@ void dbg_pattern_draw_pos(int x, int y, int bank, pixel *buffer, int buffer_widt * Draws a pattern bank to a buffer. Uses the palette #0. * @param bank The bank to draw (0 -> 0x0000, 1 -> 0x1000) * @param buffer The buffer to write the patterns data to. + * @param palette The background palette to use */ -void dbg_pattern_draw_bank(int bank, pixel *buffer); +void dbg_pattern_draw_bank(int bank, pixel *buffer, int palette); #endif //NES_EMULATOR_DBG_PATTERN_TABLE_H diff --git a/gui/gui.c b/gui/gui.c index ce274c1..ea9ee77 100644 --- a/gui/gui.c +++ b/gui/gui.c @@ -70,6 +70,7 @@ void gui_post_sysinit() { dbg_pattern_table_init(); dbg_nametable_init(); + // TODO: The texture is rendered before the palette data is in the PPU memory, so the only color is grey pattern_window_build_table(&gui.pattern_window); nametable_window_update(&gui.nametable_window); #endif @@ -83,8 +84,14 @@ int gui_input() { return -1; } - if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_p) { - system_toggle_pause(); + if (event.type == SDL_KEYUP) { + if (event.key.keysym.sym == SDLK_p) { + system_toggle_pause(); + } else { +#if DEBUG + pattern_window_key_up(&gui.pattern_window, event.key.keysym.sym); +#endif + } } } diff --git a/gui/nametable_window.h b/gui/nametable_window.h index 5acc932..3063c89 100644 --- a/gui/nametable_window.h +++ b/gui/nametable_window.h @@ -8,7 +8,7 @@ #include "window.h" #include "../include/types.h" -#define NW_SCALE 2 +#define NW_SCALE 1 #define NW_ROW_COUNT 60 #define NW_ROW_TILE_COUNT 64 diff --git a/gui/pattern_window.c b/gui/pattern_window.c index 7d5c014..e87a137 100644 --- a/gui/pattern_window.c +++ b/gui/pattern_window.c @@ -14,6 +14,7 @@ void pattern_window_init(NesPatternWindow *window) { window->texture = SDL_CreateTexture(window->sdl_context.renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, PW_WIDTH, PW_HEIGHT); + window->palette = 0; } void pattern_window_uninit(NesPatternWindow *window) { @@ -23,12 +24,25 @@ void pattern_window_uninit(NesPatternWindow *window) { void pattern_window_build_table(NesPatternWindow *window) { pixel buffer[PW_BUFFER_SIZE] = {0}; - dbg_pattern_draw_bank(PATTERN_BANK_0, buffer); - dbg_pattern_draw_bank(PATTERN_BANK_1, &buffer[PW_WIDTH * (PW_HEIGHT / 2)]); + dbg_pattern_draw_bank(PATTERN_BANK_0, buffer, window->palette); + dbg_pattern_draw_bank(PATTERN_BANK_1, &buffer[PW_WIDTH * (PW_HEIGHT / 2)], window->palette); SDL_UpdateTexture(window->texture, NULL, buffer, PW_WIDTH * sizeof(pixel)); } +void pattern_window_key_up(NesPatternWindow *window, SDL_KeyCode keycode) { + if (keycode != SDLK_o) { + return; + } + + window->palette++; + if (window->palette > PW_PALETTE_MAX) { + window->palette = 0; + } + + pattern_window_build_table(window); +} + void pattern_window_render(NesPatternWindow *window) { SDL_RenderClear(window->sdl_context.renderer); SDL_RenderCopy(window->sdl_context.renderer, window->texture, NULL, NULL); diff --git a/gui/pattern_window.h b/gui/pattern_window.h index 14e41ea..88f6550 100644 --- a/gui/pattern_window.h +++ b/gui/pattern_window.h @@ -10,10 +10,12 @@ #define PW_SCALE 2 #define PW_ROW_TILE_COUNT 16 +#define PW_PALETTE_MAX 3 typedef struct nes_pattern_window { NesSdlContext sdl_context; SDL_Texture *texture; + byte palette; } NesPatternWindow; void pattern_window_init(NesPatternWindow *window); @@ -21,6 +23,7 @@ void pattern_window_uninit(NesPatternWindow *window); void pattern_window_build_table(NesPatternWindow *window); +void pattern_window_key_up(NesPatternWindow *window, SDL_KeyCode keycode); void pattern_window_render(NesPatternWindow *window); void pattern_window_present(NesPatternWindow *window); diff --git a/main.c b/main.c index 6d62693..98bbd79 100644 --- a/main.c +++ b/main.c @@ -23,8 +23,8 @@ #include "gui.h" int main() { -// char *rom_path = "./test_roms/dk_japan.nes"; - char *rom_path = "./test_roms/smb.nes"; + char *rom_path = "./test_roms/dk_japan.nes"; +// char *rom_path = "./test_roms/smb.nes"; log_set_level(LOG_INFO); if (!gui_init()) {