nesemu/gui/dbg_pattern_table.c

114 lines
3.7 KiB
C
Raw Normal View History

2024-07-21 16:41:38 -04:00
//
// Created by william on 7/12/24.
//
#include <assert.h>
#include <stdbool.h>
2024-07-23 18:50:11 -04:00
#include <string.h>
2024-07-21 16:41:38 -04:00
#include "dbg_pattern_table.h"
#include "../include/ppu.h"
#include "../include/system.h"
DebugPatternTable pattern_table;
void dbg_pattern_table_build_bank(DebugPattern *bank, byte *pattern_memory) {
for (int tile_index = 0; tile_index < PATTERN_TABLE_SIZE; tile_index++) {
DebugPattern *pattern = &bank[tile_index];
address tile_addr = tile_index * PATTERN_BYTES;
memcpy(pattern->data_low, &pattern_memory[tile_addr], PATTERN_SIZE);
memcpy(pattern->data_high, &pattern_memory[tile_addr + PATTERN_SIZE], PATTERN_SIZE);
}
}
void dbg_pattern_table_init() {
byte *pattern_memory = system_get_mapper()->ppu_read(0);
dbg_pattern_table_build_bank(pattern_table.bank_0, pattern_memory);
dbg_pattern_table_build_bank(pattern_table.bank_1, &pattern_memory[PATTERN_BANK_SIZE]);
}
2024-07-23 18:50:11 -04:00
DebugPattern dbg_pattern_get(int pattern_id, int bank) {
2024-07-21 16:41:38 -04:00
switch (bank) {
case PATTERN_BANK_0:
2024-07-23 20:46:13 -04:00
return pattern_table.bank_1[pattern_id];
2024-07-21 16:41:38 -04:00
case PATTERN_BANK_1:
2024-07-23 18:50:11 -04:00
return pattern_table.bank_1[pattern_id];
2024-07-21 16:41:38 -04:00
default:
assert(false);
}
}
2024-07-23 18:50:11 -04:00
DebugPattern dbg_pattern_get_pos(int x, int y, int bank) {
assert(x >= 0);
assert(x < PATTERN_TABLE_WIDTH);
assert(y >= 0);
assert(y < PATTERN_TABLE_WIDTH);
address pattern_addr = x + y * PATTERN_TABLE_WIDTH;
return dbg_pattern_get(pattern_addr, bank);
}
2024-07-21 16:41:38 -04:00
void dbg_pattern_draw_borders(pixel *buffer, int buffer_width) {
for (int by = 0; by < PATTERN_DRAW_SIZE; by++) {
address pixel_addr = (by * buffer_width) + PATTERN_DRAW_SIZE - 1;
buffer[pixel_addr] = PATTERN_BORDER_COLOR;
}
for (int bx = 0; bx < PATTERN_DRAW_SIZE; bx++) {
address pixel_addr = ((PATTERN_DRAW_SIZE - 1) * buffer_width) + bx;
buffer[pixel_addr] = PATTERN_BORDER_COLOR;
}
}
2024-07-23 20:46:13 -04:00
void dbg_pattern_draw_pattern(DebugPattern pattern, pixel *buffer, int buffer_width) {
2024-07-21 16:41:38 -04:00
for (int fine_y = 0; fine_y < PATTERN_SIZE; fine_y++) {
byte data_high = pattern.data_high[fine_y];
byte data_low = pattern.data_low[fine_y];
for (int fine_x = 0; fine_x < PATTERN_SIZE; fine_x++) {
byte bitmask = 1 << (PATTERN_SIZE - fine_x - 1);
byte bit_high = data_high & bitmask;
byte bit_low = data_low & bitmask;
address pixel_addr = fine_x + fine_y * buffer_width;
pixel *pixel = &buffer[pixel_addr];
// TODO: Use palette colors
if (bit_high && bit_low) {
*pixel = 0xffffffff;
} else if (bit_low) {
*pixel = 0xffff0000;
} else if (bit_high) {
*pixel = 0xff00ffff;
} else {
*pixel = 0xff000000;
}
}
}
dbg_pattern_draw_borders(buffer, buffer_width);
}
2024-07-23 20:46:13 -04:00
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);
}
2024-07-21 16:41:38 -04:00
void dbg_pattern_draw_bank(int bank, pixel *buffer) {
int buffer_width = PATTERN_TABLE_WIDTH * PATTERN_DRAW_SIZE;
for (int x = 0; x < PATTERN_TABLE_WIDTH; x++) {
for (int y = 0; y < PATTERN_TABLE_WIDTH; y++) {
address row_addr = (y * PATTERN_DRAW_SIZE) * buffer_width;
address tile_addr = row_addr + (x * PATTERN_DRAW_SIZE);
2024-07-23 20:46:13 -04:00
dbg_pattern_draw_pos(x, y, bank, &buffer[tile_addr], buffer_width);
2024-07-21 16:41:38 -04:00
}
}
}