Functioning nametable view

This commit is contained in:
FyloZ 2024-07-23 20:46:13 -04:00
parent 139966c4cf
commit 662d7efa59
Signed by: william
GPG Key ID: 835378AE9AF4AE97
8 changed files with 114 additions and 25 deletions

View File

@ -4,9 +4,19 @@
#include "dbg_nametable.h" #include "dbg_nametable.h"
#include "../include/ppu.h" #include "../include/ppu.h"
#include "../include/system.h"
#include "../include/rom.h"
// TODO: Add support for palettes
#define NAMETABLE_ROW_DRAW_WIDTH (NAMETABLE_ROW_WIDTH * PATTERN_DRAW_SIZE)
DebugNameTable dbg_nametable; DebugNameTable dbg_nametable;
void dbg_nametable_init() {
dbg_nametable.vertical_mirroring = rom_get()->nametable_mirrored;
}
void dbg_nametable_build_bank(byte *nametable, DebugTile *bank) { void dbg_nametable_build_bank(byte *nametable, DebugTile *bank) {
for (int i = 0; i < NAMETABLE_BANK_SIZE; i++) { for (int i = 0; i < NAMETABLE_BANK_SIZE; i++) {
DebugTile *tile = &bank[i]; DebugTile *tile = &bank[i];
@ -19,3 +29,39 @@ void dbg_nametable_update() {
dbg_nametable_build_bank(ppu_get_state()->memory.nametable_0, dbg_nametable.bank_0); dbg_nametable_build_bank(ppu_get_state()->memory.nametable_0, dbg_nametable.bank_0);
dbg_nametable_build_bank(ppu_get_state()->memory.nametable_1, dbg_nametable.bank_1); dbg_nametable_build_bank(ppu_get_state()->memory.nametable_1, dbg_nametable.bank_1);
} }
static inline DebugTile *dbg_nametable_get_bank(int bank) {
if (dbg_nametable.vertical_mirroring) {
if (bank % 2 == 0) {
return dbg_nametable.bank_0;
}
return dbg_nametable.bank_1;
}
// Horizontal mirroring
if (bank < 2) {
return dbg_nametable.bank_0;
}
return dbg_nametable.bank_1;
}
void dbg_nametable_render_bank(int bank, pixel *buffer) {
DebugTile *table = dbg_nametable_get_bank(bank);
int pattern_bank = ppu_get_state()->bg_pattern_table_addr >> 12;
for (int i = 0; i < NAMETABLE_BANK_SIZE; i++) {
int pattern_id = table[i].tile_id;
// Tile Index:
// 0000 00RR RRRC CCCC
// || |||+ ++++-- Tile column
// ++ +++-------- Tile row
int row = (i & ~(NAMETABLE_ROW_WIDTH - 1)) >> 5;
int col = i & (NAMETABLE_ROW_WIDTH - 1);
int tile_pixel_addr =
(row * PATTERN_DRAW_SIZE * PATTERN_DRAW_SIZE * NAMETABLE_ROW_WIDTH) + (col * PATTERN_DRAW_SIZE);
pixel *tile_buffer = &buffer[tile_pixel_addr];
dbg_pattern_draw(pattern_id, pattern_bank, tile_buffer, NAMETABLE_ROW_DRAW_WIDTH);
}
}

View File

@ -9,10 +9,12 @@
#include "../include/types.h" #include "../include/types.h"
#include "dbg_pattern_table.h" #include "dbg_pattern_table.h"
#define NAMETABLE_BANK_SIZE 0x3bf #define NAMETABLE_BANK_SIZE 0x3c0
#define NAMETABLE_ROW_WIDTH 32
#define NAMETABLE_COL_HEIGHT 30
typedef struct db_tile { typedef struct db_tile {
byte tile_id; int tile_id;
} DebugTile; } DebugTile;
typedef struct dbg_nametable { typedef struct dbg_nametable {
@ -21,6 +23,11 @@ typedef struct dbg_nametable {
bool vertical_mirroring; bool vertical_mirroring;
} DebugNameTable; } DebugNameTable;
/**
* Initializes the debug nametable.
*/
void dbg_nametable_init();
/** /**
* Updates the debug nametable. Updates the tiles from the PPU memory. * Updates the debug nametable. Updates the tiles from the PPU memory.
*/ */

View File

@ -31,7 +31,7 @@ void dbg_pattern_table_init() {
DebugPattern dbg_pattern_get(int pattern_id, int bank) { DebugPattern dbg_pattern_get(int pattern_id, int bank) {
switch (bank) { switch (bank) {
case PATTERN_BANK_0: case PATTERN_BANK_0:
return pattern_table.bank_0[pattern_id]; return pattern_table.bank_1[pattern_id];
case PATTERN_BANK_1: case PATTERN_BANK_1:
return pattern_table.bank_1[pattern_id]; return pattern_table.bank_1[pattern_id];
default: default:
@ -61,8 +61,7 @@ void dbg_pattern_draw_borders(pixel *buffer, int buffer_width) {
} }
} }
void dbg_pattern_draw(int x, int y, int bank, pixel *buffer, int buffer_width) { void dbg_pattern_draw_pattern(DebugPattern pattern, pixel *buffer, int buffer_width) {
DebugPattern pattern = dbg_pattern_get_pos(x, y, bank);
for (int fine_y = 0; fine_y < PATTERN_SIZE; fine_y++) { for (int fine_y = 0; fine_y < PATTERN_SIZE; fine_y++) {
byte data_high = pattern.data_high[fine_y]; byte data_high = pattern.data_high[fine_y];
byte data_low = pattern.data_low[fine_y]; byte data_low = pattern.data_low[fine_y];
@ -91,6 +90,16 @@ void dbg_pattern_draw(int x, int y, int bank, pixel *buffer, int buffer_width) {
dbg_pattern_draw_borders(buffer, buffer_width); dbg_pattern_draw_borders(buffer, buffer_width);
} }
void dbg_pattern_draw(int pattern_id, int bank, pixel *buffer, int buffer_width) {
DebugPattern pattern = dbg_pattern_get(pattern_id, bank);
dbg_pattern_draw_pattern(pattern, buffer, buffer_width);
}
void dbg_pattern_draw_pos(int x, int y, int bank, pixel *buffer, int buffer_width) {
DebugPattern pattern = dbg_pattern_get_pos(x, y, bank);
dbg_pattern_draw_pattern(pattern, buffer, buffer_width);
}
void dbg_pattern_draw_bank(int bank, pixel *buffer) { void dbg_pattern_draw_bank(int bank, pixel *buffer) {
int buffer_width = PATTERN_TABLE_WIDTH * PATTERN_DRAW_SIZE; int buffer_width = PATTERN_TABLE_WIDTH * PATTERN_DRAW_SIZE;
@ -99,7 +108,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(x, y, bank, &buffer[tile_addr], buffer_width); dbg_pattern_draw_pos(x, y, bank, &buffer[tile_addr], buffer_width);
} }
} }
} }

View File

@ -53,6 +53,15 @@ DebugPattern dbg_pattern_get(int pattern_id, int bank);
*/ */
DebugPattern dbg_pattern_get_pos(int x, int y, int bank); DebugPattern dbg_pattern_get_pos(int x, int y, int bank);
/**
* Draws a pattern to a buffer. The pattern is determined by its ID.
* @param pattern_id The ID of the pattern
* @param bank The bank of the pattern
* @param buffer The buffer to write the pattern data to
* @param buffer_width The width of a pixel row in the buffer
*/
void dbg_pattern_draw(int pattern_id, int bank, pixel *buffer, int buffer_width);
/** /**
* Draws a pattern to a buffer. The pattern is determined by its position. * Draws a pattern to a buffer. The pattern is determined by its position.
* @param x The x position of the pattern in the table * @param x The x position of the pattern in the table
@ -61,7 +70,7 @@ DebugPattern dbg_pattern_get_pos(int x, int y, int bank);
* @param buffer The buffer to write the pattern data to * @param buffer The buffer to write the pattern data to
* @param buffer_width The width of a pixel row in the buffer * @param buffer_width The width of a pixel row in the buffer
*/ */
void dbg_pattern_draw(int x, int y, int bank, pixel *dbgdbgbuffer, int buffer_width); void dbg_pattern_draw_pos(int x, int y, int bank, pixel *buffer, int buffer_width);
/** /**
* Draws a pattern bank to a buffer. * Draws a pattern bank to a buffer.

View File

@ -10,6 +10,7 @@
#include "../include/system.h" #include "../include/system.h"
#include "nametable_window.h" #include "nametable_window.h"
#include "dbg_pattern_table.h" #include "dbg_pattern_table.h"
#include "dbg_nametable.h"
typedef struct nes_gui { typedef struct nes_gui {
NesMainWindow main_window; NesMainWindow main_window;
@ -41,7 +42,7 @@ bool gui_init() {
if (gui.debug_enabled) { if (gui.debug_enabled) {
pattern_window_init(&gui.pattern_window); pattern_window_init(&gui.pattern_window);
// nametable_window_init(&gui.nametable_window); nametable_window_init(&gui.nametable_window);
} }
return true; return true;
@ -52,7 +53,7 @@ void gui_uninit() {
if (gui.debug_enabled) { if (gui.debug_enabled) {
pattern_window_uninit(&gui.pattern_window); pattern_window_uninit(&gui.pattern_window);
// nametable_window_uninit(&gui.nametable_window); nametable_window_uninit(&gui.nametable_window);
} }
TTF_CloseFont(gui.font); TTF_CloseFont(gui.font);
@ -61,8 +62,10 @@ void gui_uninit() {
void gui_post_sysinit() { void gui_post_sysinit() {
if (gui.debug_enabled) { if (gui.debug_enabled) {
dbg_pattern_table_init(); dbg_pattern_table_init();
dbg_nametable_init();
pattern_window_build_table(&gui.pattern_window); pattern_window_build_table(&gui.pattern_window);
nametable_window_update(&gui.nametable_window);
} }
} }
@ -85,10 +88,9 @@ void gui_render() {
pattern_window_render(&gui.pattern_window); pattern_window_render(&gui.pattern_window);
// if (gui.tick % 60 == 0) { // if (gui.tick % 60 == 0) {
// PPUMemory *ppu_memory = &ppu_get_state()->memory; nametable_window_update(&gui.nametable_window);
// nametable_window_update(&gui.nametable_window, ppu_memory->nametable_0, ppu_memory->nametable_1);
// } // }
// nametable_window_render(&gui.nametable_window); nametable_window_render(&gui.nametable_window);
} }
gui.tick++; gui.tick++;
@ -99,7 +101,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); nametable_window_present(&gui.nametable_window);
} }
} }

View File

@ -2,12 +2,12 @@
// 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"
#include "dbg_nametable.h"
#define NAMETABLE_BANK_SIZE 0x0400
#define NW_WIDTH (NW_ROW_TILE_COUNT * PATTERN_DRAW_SIZE) #define NW_WIDTH (NW_ROW_TILE_COUNT * PATTERN_DRAW_SIZE)
#define NW_HEIGHT (NW_ROW_COUNT * PATTERN_DRAW_SIZE) #define NW_HEIGHT (NW_ROW_COUNT * PATTERN_DRAW_SIZE)
#define NW_BUFFER_SIZE (NAMETABLE_ROW_WIDTH * NAMETABLE_COL_HEIGHT * PATTERN_DRAW_SIZE * PATTERN_DRAW_SIZE)
void nametable_window_init(NesNametableWindow *window) { void nametable_window_init(NesNametableWindow *window) {
int win_size = pattern_display_get_size(NW_ROW_TILE_COUNT); int win_size = pattern_display_get_size(NW_ROW_TILE_COUNT);
@ -22,10 +22,26 @@ void nametable_window_uninit(NesNametableWindow *window) {
window_uninit(window->sdl_context); window_uninit(window->sdl_context);
} }
void nametable_window_update(NesNametableWindow *window, byte *nametable_0, byte *nametable_1) { void nametable_window_update_bank(NesNametableWindow *window, int bank, pixel* buffer) {
byte **nametables[2]; dbg_nametable_render_bank(bank, buffer);
nametables[0] = &nametable_0;
nametables[1] = &nametable_1; SDL_Rect rect;
rect.w = NAMETABLE_ROW_WIDTH * PATTERN_DRAW_SIZE;
rect.h = NAMETABLE_COL_HEIGHT * PATTERN_DRAW_SIZE;
rect.x = (bank & 1) * rect.w;
rect.y = ((bank & 2) >> 1) * rect.h;
SDL_UpdateTexture(window->texture, &rect, buffer, (NW_WIDTH / 2) * sizeof(pixel));
}
void nametable_window_update(NesNametableWindow *window) {
dbg_nametable_update();
pixel buffer[NW_BUFFER_SIZE * 4] = {0};
nametable_window_update_bank(window, 0, buffer);
nametable_window_update_bank(window, 1, buffer);
nametable_window_update_bank(window, 2, buffer);
nametable_window_update_bank(window, 3, buffer);
} }
void nametable_window_render(NesNametableWindow *window) { void nametable_window_render(NesNametableWindow *window) {

View File

@ -9,9 +9,9 @@
#include "../include/types.h" #include "../include/types.h"
#include "pattern_display.h" #include "pattern_display.h"
#define NW_SCALE 2 #define NW_SCALE 1
#define NW_ROW_COUNT 30 #define NW_ROW_COUNT 60
#define NW_ROW_TILE_COUNT 32 #define NW_ROW_TILE_COUNT 64
typedef struct nes_nametable_window { typedef struct nes_nametable_window {
NesSdlContext sdl_context; NesSdlContext sdl_context;
@ -21,7 +21,7 @@ typedef struct nes_nametable_window {
void nametable_window_init(NesNametableWindow *window); void nametable_window_init(NesNametableWindow *window);
void nametable_window_uninit(NesNametableWindow *window); void nametable_window_uninit(NesNametableWindow *window);
void nametable_window_update(NesNametableWindow *window, byte* nametable_0, byte* nametable_1); 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);

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_japan.nes"; char *rom_path = "./test_roms/dk_jp.nes";
log_set_level(LOG_INFO); log_set_level(LOG_INFO);
if (!gui_init()) { if (!gui_init()) {