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"
|
2024-08-03 21:51:31 -04:00
|
|
|
#include "dbg_palette.h"
|
2024-07-21 16:41:38 -04:00
|
|
|
|
|
|
|
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() {
|
2024-10-03 19:24:03 -04:00
|
|
|
if (pattern_table.initialized) {
|
|
|
|
// Already initialized
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-21 16:41:38 -04:00
|
|
|
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-10-03 19:24:03 -04:00
|
|
|
pattern_table.initialized = true;
|
2024-07-21 16:41:38 -04:00
|
|
|
}
|
|
|
|
|
2024-07-23 18:50:11 -04:00
|
|
|
DebugPattern dbg_pattern_get(int pattern_id, int bank) {
|
2024-08-03 21:51:31 -04:00
|
|
|
// TODO Should not use same 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-08-03 21:51:31 -04:00
|
|
|
void dbg_pattern_draw_pattern(DebugPattern pattern, pixel *buffer, int buffer_width, byte palette) {
|
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++) {
|
2024-08-03 21:51:31 -04:00
|
|
|
byte offset = PATTERN_SIZE - fine_x - 1;
|
|
|
|
byte bit_high = (data_high >> offset) & 1;
|
|
|
|
byte bit_low = (data_low >> offset) & 1;
|
2024-07-21 16:41:38 -04:00
|
|
|
|
|
|
|
address pixel_addr = fine_x + fine_y * buffer_width;
|
2024-08-03 21:51:31 -04:00
|
|
|
|
|
|
|
byte palette_data = (bit_high << 1) | bit_low;
|
|
|
|
buffer[pixel_addr] = dbg_get_background_color(palette, palette_data);
|
2024-07-21 16:41:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dbg_pattern_draw_borders(buffer, buffer_width);
|
|
|
|
}
|
|
|
|
|
2024-08-03 21:51:31 -04:00
|
|
|
void dbg_pattern_draw(int pattern_id, int bank, pixel *buffer, int buffer_width, int palette) {
|
2024-07-23 20:46:13 -04:00
|
|
|
DebugPattern pattern = dbg_pattern_get(pattern_id, bank);
|
2024-08-03 21:51:31 -04:00
|
|
|
dbg_pattern_draw_pattern(pattern, buffer, buffer_width, palette);
|
2024-07-23 20:46:13 -04:00
|
|
|
}
|
|
|
|
|
2024-08-03 21:51:31 -04:00
|
|
|
void dbg_pattern_draw_pos(int x, int y, int bank, pixel *buffer, int buffer_width, int palette) {
|
2024-07-23 20:46:13 -04:00
|
|
|
DebugPattern pattern = dbg_pattern_get_pos(x, y, bank);
|
2024-08-03 21:51:31 -04:00
|
|
|
dbg_pattern_draw_pattern(pattern, buffer, buffer_width, palette);
|
2024-07-23 20:46:13 -04:00
|
|
|
}
|
|
|
|
|
2024-08-06 17:30:57 -04:00
|
|
|
void dbg_pattern_draw_bank(int bank, pixel *buffer, int palette) {
|
2024-07-21 16:41:38 -04:00
|
|
|
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-08-06 17:30:57 -04:00
|
|
|
dbg_pattern_draw_pos(x, y, bank, &buffer[tile_addr], buffer_width, palette);
|
2024-07-21 16:41:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|