Add palette switch to the debug pattern view.

This commit is contained in:
william 2024-08-06 17:30:57 -04:00
parent 81451b24ad
commit 0066e77455
8 changed files with 39 additions and 10 deletions

View File

@ -8,6 +8,10 @@ only tested on Linux. Here is how to run the project:
- Build the project with Make: ```make``` - Build the project with Make: ```make```
- Run the emulator: ```./nes_emulator``` - Run the emulator: ```./nes_emulator```
## Controls
- `p`: Pauses the emulation
- `o`: Go to the next palette in the pattern viewer
## Dependencies ## Dependencies
- GCC compiler - GCC compiler
- CMake - CMake

View File

@ -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); 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; int buffer_width = PATTERN_TABLE_WIDTH * PATTERN_DRAW_SIZE;
for (int x = 0; x < PATTERN_TABLE_WIDTH; x++) { 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 row_addr = (y * PATTERN_DRAW_SIZE) * buffer_width;
address tile_addr = row_addr + (x * PATTERN_DRAW_SIZE); 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);
} }
} }
} }

View File

@ -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. * Draws a pattern bank to a buffer. Uses the palette #0.
* @param bank The bank to draw (0 -> 0x0000, 1 -> 0x1000) * @param bank The bank to draw (0 -> 0x0000, 1 -> 0x1000)
* @param buffer The buffer to write the patterns data to. * @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 #endif //NES_EMULATOR_DBG_PATTERN_TABLE_H

View File

@ -70,6 +70,7 @@ void gui_post_sysinit() {
dbg_pattern_table_init(); dbg_pattern_table_init();
dbg_nametable_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); pattern_window_build_table(&gui.pattern_window);
nametable_window_update(&gui.nametable_window); nametable_window_update(&gui.nametable_window);
#endif #endif
@ -83,8 +84,14 @@ int gui_input() {
return -1; return -1;
} }
if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_p) { if (event.type == SDL_KEYUP) {
system_toggle_pause(); 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
}
} }
} }

View File

@ -8,7 +8,7 @@
#include "window.h" #include "window.h"
#include "../include/types.h" #include "../include/types.h"
#define NW_SCALE 2 #define NW_SCALE 1
#define NW_ROW_COUNT 60 #define NW_ROW_COUNT 60
#define NW_ROW_TILE_COUNT 64 #define NW_ROW_TILE_COUNT 64

View File

@ -14,6 +14,7 @@ void pattern_window_init(NesPatternWindow *window) {
window->texture = SDL_CreateTexture(window->sdl_context.renderer, SDL_PIXELFORMAT_ARGB8888, window->texture = SDL_CreateTexture(window->sdl_context.renderer, SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STATIC, PW_WIDTH, PW_HEIGHT); SDL_TEXTUREACCESS_STATIC, PW_WIDTH, PW_HEIGHT);
window->palette = 0;
} }
void pattern_window_uninit(NesPatternWindow *window) { void pattern_window_uninit(NesPatternWindow *window) {
@ -23,12 +24,25 @@ void pattern_window_uninit(NesPatternWindow *window) {
void pattern_window_build_table(NesPatternWindow *window) { void pattern_window_build_table(NesPatternWindow *window) {
pixel buffer[PW_BUFFER_SIZE] = {0}; pixel buffer[PW_BUFFER_SIZE] = {0};
dbg_pattern_draw_bank(PATTERN_BANK_0, buffer); dbg_pattern_draw_bank(PATTERN_BANK_0, buffer, window->palette);
dbg_pattern_draw_bank(PATTERN_BANK_1, &buffer[PW_WIDTH * (PW_HEIGHT / 2)]); 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)); 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) { void pattern_window_render(NesPatternWindow *window) {
SDL_RenderClear(window->sdl_context.renderer); SDL_RenderClear(window->sdl_context.renderer);
SDL_RenderCopy(window->sdl_context.renderer, window->texture, NULL, NULL); SDL_RenderCopy(window->sdl_context.renderer, window->texture, NULL, NULL);

View File

@ -10,10 +10,12 @@
#define PW_SCALE 2 #define PW_SCALE 2
#define PW_ROW_TILE_COUNT 16 #define PW_ROW_TILE_COUNT 16
#define PW_PALETTE_MAX 3
typedef struct nes_pattern_window { typedef struct nes_pattern_window {
NesSdlContext sdl_context; NesSdlContext sdl_context;
SDL_Texture *texture; SDL_Texture *texture;
byte palette;
} NesPatternWindow; } NesPatternWindow;
void pattern_window_init(NesPatternWindow *window); 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_build_table(NesPatternWindow *window);
void pattern_window_key_up(NesPatternWindow *window, SDL_KeyCode keycode);
void pattern_window_render(NesPatternWindow *window); void pattern_window_render(NesPatternWindow *window);
void pattern_window_present(NesPatternWindow *window); void pattern_window_present(NesPatternWindow *window);

4
main.c
View File

@ -23,8 +23,8 @@
#include "gui.h" #include "gui.h"
int main() { int main() {
// char *rom_path = "./test_roms/dk_japan.nes"; char *rom_path = "./test_roms/dk_japan.nes";
char *rom_path = "./test_roms/smb.nes"; // char *rom_path = "./test_roms/smb.nes";
log_set_level(LOG_INFO); log_set_level(LOG_INFO);
if (!gui_init()) { if (!gui_init()) {