Nametable debugger

This commit is contained in:
william 2024-07-12 18:52:54 -04:00
parent 97d46b4583
commit a533af7901
9 changed files with 81 additions and 36 deletions

View File

@ -32,13 +32,16 @@ bool gui_init() {
return false; return false;
} }
gui.debug_enabled = true; gui.debug_enabled = false;
main_window_init(&gui.main_window, gui.font); main_window_init(&gui.main_window, gui.font);
if (gui.debug_enabled) { if (gui.debug_enabled) {
pattern_window_init(&gui.pattern_window); byte *pattern_memory = system_get_mapper()->ppu_read(0);
nametable_window_init(&gui.nametable_window); pattern_window_init(&gui.pattern_window, pattern_memory);
PPUMemory *ppu_memory = &ppu_get_state()->memory;
nametable_window_init(&gui.nametable_window, ppu_memory->nametable_0, ppu_memory->nametable_1);
} }
return true; return true;
@ -56,16 +59,10 @@ void gui_uninit() {
} }
void gui_post_sysinit() { void gui_post_sysinit() {
byte *pattern_memory = system_get_mapper()->ppu_read(0); if (gui.debug_enabled) {
byte nametable_memory[0x0400 * 4]; pattern_window_build_table(&gui.pattern_window);
nametable_window_update(&gui.nametable_window);
memcpy(&nametable_memory, ppu_get_state()->memory.nametable_0, 0x0400); }
memcpy(&nametable_memory[0x0400], ppu_get_state()->memory.nametable_0, 0x0400);
memcpy(&nametable_memory[0x0800], ppu_get_state()->memory.nametable_1, 0x0400);
memcpy(&nametable_memory[0x0c00], ppu_get_state()->memory.nametable_1, 0x0400);
pattern_window_build_table(&gui.pattern_window, pattern_memory);
nametable_window_build_table(&gui.nametable_window, &nametable_memory[0]);
} }
int gui_input() { int gui_input() {
@ -85,6 +82,7 @@ void gui_render() {
if (gui.debug_enabled) { if (gui.debug_enabled) {
pattern_window_render(&gui.pattern_window); pattern_window_render(&gui.pattern_window);
nametable_window_render(&gui.nametable_window);
} }
} }
@ -93,6 +91,7 @@ void gui_present() {
if (gui.debug_enabled) { if (gui.debug_enabled) {
pattern_window_present(&gui.pattern_window); pattern_window_present(&gui.pattern_window);
nametable_window_present(&gui.nametable_window);
} }
} }

View File

@ -2,13 +2,20 @@
// Created by william on 12/07/24. // Created by william on 12/07/24.
// //
#include <assert.h>
#include "nametable_window.h" #include "nametable_window.h"
void nametable_window_init(NesNametableWindow *window) { #define NAMETABLE_BANK_SIZE 0x0400
void nametable_window_init(NesNametableWindow *window, byte *nametable_0, byte *nametable_1) {
int win_size = pattern_display_get_size(NW_ROW_TILE_COUNT); int win_size = pattern_display_get_size(NW_ROW_TILE_COUNT);
window->sdl_context = window_init("Nametable", win_size, win_size, NW_SCALE); window->sdl_context = window_init("Nametable", win_size, win_size, NW_SCALE);
pattern_display_init(&window->pattern_display, window->sdl_context.renderer, NW_ROW_TILE_COUNT, NW_ROW_TILE_COUNT); window->nametable_0 = nametable_0;
window->nametable_1 = nametable_1;
pattern_display_init(&window->pattern_display, window->sdl_context.renderer, NW_ROW_TILE_COUNT, NW_ROW_TILE_COUNT,
PATTERN_DISPLAY_DYNAMIC);
} }
void nametable_window_uninit(NesNametableWindow *window) { void nametable_window_uninit(NesNametableWindow *window) {
@ -16,8 +23,26 @@ void nametable_window_uninit(NesNametableWindow *window) {
window_uninit(window->sdl_context); window_uninit(window->sdl_context);
} }
void nametable_window_build_table(NesNametableWindow *window, byte *nametable_memory) { static byte *nametable_window_read_byte(address addr, void *data) {
pattern_display_build(&window->pattern_display, nametable_memory); assert(addr < NAMETABLE_BANK_SIZE * 4);
assert(data != NULL);
NesNametableWindow *window = (NesNametableWindow *) data;
int bank = addr / NAMETABLE_BANK_SIZE;
int bank_addr = addr % NAMETABLE_BANK_SIZE;
switch (bank) {
case 0:
case 1:
return &window->nametable_0[bank_addr];
default:
return &window->nametable_1[bank_addr];
}
}
void nametable_window_update(NesNametableWindow *window) {
pattern_display_build(&window->pattern_display, &nametable_window_read_byte, window);
} }
void nametable_window_render(NesNametableWindow *window) { void nametable_window_render(NesNametableWindow *window) {

View File

@ -15,12 +15,15 @@
typedef struct nes_nametable_window { typedef struct nes_nametable_window {
NesSdlContext sdl_context; NesSdlContext sdl_context;
PatternDisplay pattern_display; PatternDisplay pattern_display;
byte *nametable_0;
byte *nametable_1;
} NesNametableWindow; } NesNametableWindow;
void nametable_window_init(NesNametableWindow *window); void nametable_window_init(NesNametableWindow *window, byte* nametable_0, byte* nametable_1);
void nametable_window_uninit(NesNametableWindow *window); void nametable_window_uninit(NesNametableWindow *window);
void nametable_window_build_table(NesNametableWindow *window, byte* nametable_memory); void nametable_window_update(NesNametableWindow *window);
void nametable_window_render(NesNametableWindow *window); void nametable_window_render(NesNametableWindow *window);
void nametable_window_present(NesNametableWindow *window); void nametable_window_present(NesNametableWindow *window);

View File

@ -7,14 +7,14 @@
#define PATTERN_BYTES (PATTERN_SIZE * 2) #define PATTERN_BYTES (PATTERN_SIZE * 2)
void pattern_display_init(PatternDisplay *display, SDL_Renderer *renderer, int tiles_x, int tiles_y) { void pattern_display_init(PatternDisplay *display, SDL_Renderer *renderer, int tiles_x, int tiles_y, int display_type) {
assert(tiles_x > 0); assert(tiles_x > 0);
assert(tiles_y > 0); assert(tiles_y > 0);
display->width = tiles_x; display->width = tiles_x;
display->height = tiles_y; display->height = tiles_y;
display->texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, display->texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, display_type,
pattern_display_get_size(tiles_x), pattern_display_get_size(tiles_x),
pattern_display_get_size(tiles_y)); pattern_display_get_size(tiles_y));
} }
@ -35,13 +35,14 @@ void pattern_display_draw_borders(unsigned int *buffer, int win_width, int win_h
} }
} }
void pattern_display_build_table(PatternTile *tile_table, byte *memory, int tile_count) { void pattern_display_build_table(PatternTile *tile_table, int tile_count, read_func read_func,
void *read_func_data) {
for (int tile_index = 0; tile_index < tile_count; tile_index++) { for (int tile_index = 0; tile_index < tile_count; tile_index++) {
PatternTile *tile = &tile_table[tile_index]; PatternTile *tile = &tile_table[tile_index];
address tile_addr = tile_index * PATTERN_BYTES; address tile_addr = tile_index * PATTERN_BYTES;
memcpy(tile->data_low, &memory[tile_addr], 8); memcpy(tile->data_low, read_func(tile_addr, read_func_data), 8);
memcpy(tile->data_high, &memory[tile_addr + 8], 8); memcpy(tile->data_high, read_func(tile_addr + 8, read_func_data), 8);
} }
} }
@ -90,10 +91,10 @@ void pattern_display_draw_tile(PatternTile *tile, pixel *buffer, int tile_addr,
pattern_display_draw_tile_borders(tile_addr, buffer, win_width); pattern_display_draw_tile_borders(tile_addr, buffer, win_width);
} }
void pattern_display_build(PatternDisplay *display, byte *memory) { void pattern_display_build(PatternDisplay *display, read_func read_func, void *read_func_data) {
int tile_count = display->width * display->height; int tile_count = display->width * display->height;
PatternTile *tile_table = malloc(tile_count * sizeof(PatternTile)); PatternTile *tile_table = malloc(tile_count * sizeof(PatternTile));
pattern_display_build_table(tile_table, memory, tile_count); pattern_display_build_table(tile_table, tile_count, read_func, read_func_data);
int win_width = pattern_display_get_size(display->width); int win_width = pattern_display_get_size(display->width);
int win_height = pattern_display_get_size(display->height); int win_height = pattern_display_get_size(display->height);

View File

@ -8,6 +8,9 @@
#include <SDL.h> #include <SDL.h>
#include "../include/types.h" #include "../include/types.h"
#define PATTERN_DISPLAY_STATIC SDL_TEXTUREACCESS_STATIC
#define PATTERN_DISPLAY_DYNAMIC SDL_TEXTUREACCESS_STREAMING
#define PATTERN_SIZE 8 #define PATTERN_SIZE 8
#define PATTERN_BORDER_WIDTH 1 #define PATTERN_BORDER_WIDTH 1
#define PATTERN_BORDER_COLOR 0xff2223b2 #define PATTERN_BORDER_COLOR 0xff2223b2
@ -15,6 +18,8 @@
typedef unsigned int pixel; typedef unsigned int pixel;
typedef byte *(*read_func)(address, void *);
typedef struct pattern_tile { typedef struct pattern_tile {
byte data_low[8]; byte data_low[8];
byte data_high[8]; byte data_high[8];
@ -31,11 +36,11 @@ static inline int pattern_display_get_size(int tile_count) {
return tile_count * PATTERN_DRAW_SIZE + PATTERN_BORDER_WIDTH; return tile_count * PATTERN_DRAW_SIZE + PATTERN_BORDER_WIDTH;
} }
void pattern_display_init(PatternDisplay *display, SDL_Renderer *renderer, int tiles_x, int tiles_y); void pattern_display_init(PatternDisplay *display, SDL_Renderer *renderer, int tiles_x, int tiles_y, int display_type);
void pattern_display_uninit(PatternDisplay *display); void pattern_display_uninit(PatternDisplay *display);
void pattern_display_build(PatternDisplay *display, byte *memory); void pattern_display_build(PatternDisplay *display, read_func read_func, void *read_func_data);
void pattern_display_render(PatternDisplay *display, SDL_Renderer *renderer); void pattern_display_render(PatternDisplay *display, SDL_Renderer *renderer);

View File

@ -2,16 +2,18 @@
// Created by william on 6/14/24. // Created by william on 6/14/24.
// //
#include <assert.h>
#include "pattern_window.h" #include "pattern_window.h"
#define PW_SCALE 2 #define PW_SCALE 2
void pattern_window_init(NesPatternWindow *window) { void pattern_window_init(NesPatternWindow *window, byte *pattern_memory) {
int win_width = pattern_display_get_size(PW_ROW_TILE_COUNT); int win_width = pattern_display_get_size(PW_ROW_TILE_COUNT);
window->sdl_context = window_init("Pattern Table", win_width, win_width * 2, PW_SCALE); window->sdl_context = window_init("Pattern Table", win_width, win_width * 2, PW_SCALE);
window->pattern_memory = pattern_memory;
pattern_display_init(&window->pattern_display, window->sdl_context.renderer, PW_ROW_TILE_COUNT, pattern_display_init(&window->pattern_display, window->sdl_context.renderer, PW_ROW_TILE_COUNT,
PW_ROW_TILE_COUNT * 2); PW_ROW_TILE_COUNT * 2, PATTERN_DISPLAY_STATIC);
} }
void pattern_window_uninit(NesPatternWindow *window) { void pattern_window_uninit(NesPatternWindow *window) {
@ -19,8 +21,17 @@ void pattern_window_uninit(NesPatternWindow *window) {
window_uninit(window->sdl_context); window_uninit(window->sdl_context);
} }
void pattern_window_build_table(NesPatternWindow *window, byte *pattern_memory) { static byte *pattern_window_read_byte(address addr, void *data) {
pattern_display_build(&window->pattern_display, pattern_memory); assert(data != NULL);
assert(addr < 0x2000);
NesPatternWindow *window = (NesPatternWindow *) data;
return &window->pattern_memory[addr];
}
void pattern_window_build_table(NesPatternWindow *window) {
pattern_display_build(&window->pattern_display, &pattern_window_read_byte, window);
} }
void pattern_window_render(NesPatternWindow *window) { void pattern_window_render(NesPatternWindow *window) {

View File

@ -14,12 +14,13 @@
typedef struct nes_pattern_window { typedef struct nes_pattern_window {
NesSdlContext sdl_context; NesSdlContext sdl_context;
PatternDisplay pattern_display; PatternDisplay pattern_display;
byte* pattern_memory;
} NesPatternWindow; } NesPatternWindow;
void pattern_window_init(NesPatternWindow *window); void pattern_window_init(NesPatternWindow *window, byte *pattern_memory);
void pattern_window_uninit(NesPatternWindow *window); void pattern_window_uninit(NesPatternWindow *window);
void pattern_window_build_table(NesPatternWindow *window, byte* pattern_memory); void pattern_window_build_table(NesPatternWindow *window);
void pattern_window_render(NesPatternWindow *window); void pattern_window_render(NesPatternWindow *window);
void pattern_window_present(NesPatternWindow *window); void pattern_window_present(NesPatternWindow *window);

2
main.c
View File

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

View File

@ -208,7 +208,7 @@ void ppu_visible_frame(unsigned int cycle) {
ppu_state.ppu_address += 0x20; ppu_state.ppu_address += 0x20;
} else { } else {
ppu_state.ppu_address &= ~0x3e0; ppu_state.ppu_address &= ~0x3e0;
ppu_state.ppu_address ^= 0x0800; // ppu_state.ppu_address ^= 0x0800;
} }
} }
} }