96 lines
3.1 KiB
C
96 lines
3.1 KiB
C
//
|
|
// Created by william on 7/12/24.
|
|
//
|
|
|
|
#include <assert.h>
|
|
#include <stdbool.h>
|
|
#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]);
|
|
}
|
|
|
|
DebugPattern dbg_pattern_get(int x, int y, int bank) {
|
|
address pattern_addr = x + y * PATTERN_TABLE_WIDTH;
|
|
|
|
switch (bank) {
|
|
case PATTERN_BANK_0:
|
|
return pattern_table.bank_0[pattern_addr];
|
|
case PATTERN_BANK_1:
|
|
return pattern_table.bank_1[pattern_addr];
|
|
default:
|
|
assert(false);
|
|
}
|
|
}
|
|
|
|
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(int x, int y, int bank, pixel *buffer, int buffer_width) {
|
|
DebugPattern pattern = dbg_pattern_get(x, y, bank);
|
|
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);
|
|
}
|
|
|
|
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);
|
|
|
|
dbg_pattern_draw(x, y, bank, &buffer[tile_addr], buffer_width);
|
|
}
|
|
}
|
|
} |