// // Created by william on 7/12/24. // #include #include #include #include "dbg_pattern_table.h" #include "../include/ppu.h" #include "../include/system.h" #include "dbg_palette.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() { if (pattern_table.initialized) { // Already initialized return; } 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]); pattern_table.initialized = true; } DebugPattern dbg_pattern_get(int pattern_id, int bank) { // TODO Should not use same bank switch (bank) { case PATTERN_BANK_0: return pattern_table.bank_1[pattern_id]; case PATTERN_BANK_1: return pattern_table.bank_1[pattern_id]; default: assert(false); } } 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); } 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; } } void dbg_pattern_draw_pattern(DebugPattern pattern, pixel *buffer, int buffer_width, byte palette) { 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 offset = PATTERN_SIZE - fine_x - 1; byte bit_high = (data_high >> offset) & 1; byte bit_low = (data_low >> offset) & 1; address pixel_addr = fine_x + fine_y * buffer_width; byte palette_data = (bit_high << 1) | bit_low; buffer[pixel_addr] = dbg_get_background_color(palette, palette_data); } } dbg_pattern_draw_borders(buffer, buffer_width); } void dbg_pattern_draw(int pattern_id, int bank, pixel *buffer, int buffer_width, int palette) { DebugPattern pattern = dbg_pattern_get(pattern_id, bank); dbg_pattern_draw_pattern(pattern, buffer, buffer_width, palette); } void dbg_pattern_draw_pos(int x, int y, int bank, pixel *buffer, int buffer_width, int palette) { DebugPattern pattern = dbg_pattern_get_pos(x, y, bank); dbg_pattern_draw_pattern(pattern, buffer, buffer_width, palette); } 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++) { 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); dbg_pattern_draw_pos(x, y, bank, &buffer[tile_addr], buffer_width, palette); } } }