Change the border format to SNES-style

This commit is contained in:
Lior Halphon 2021-03-31 00:54:55 +03:00
parent 8adaba237e
commit e460b0a7b4
5 changed files with 109 additions and 88 deletions

View File

@ -180,12 +180,17 @@ static void display_vblank(GB_gameboy_t *gb)
continue; continue;
} }
uint16_t tile = LE16(gb->borrowed_border.map[tile_x + tile_y * 32]); uint16_t tile = LE16(gb->borrowed_border.map[tile_x + tile_y * 32]);
uint8_t flip_x = (tile & 0x4000)? 0x7 : 0; uint8_t flip_x = (tile & 0x4000)? 0:7;
uint8_t flip_y = (tile & 0x8000)? 0x7 : 0; uint8_t flip_y = (tile & 0x8000)? 7:0;
uint8_t palette = (tile >> 10) & 3; uint8_t palette = (tile >> 10) & 3;
for (unsigned y = 0; y < 8; y++) { for (unsigned y = 0; y < 8; y++) {
unsigned base = (tile & 0xFF) * 32 + (y ^ flip_y) * 2;
for (unsigned x = 0; x < 8; x++) { for (unsigned x = 0; x < 8; x++) {
uint8_t color = gb->borrowed_border.tiles[(tile & 0xFF) * 64 + (x ^ flip_x) + (y ^ flip_y) * 8] & 0xF; uint8_t bit = 1 << (x ^ flip_x);
uint8_t color = ((gb->borrowed_border.tiles[base] & bit) ? 1 : 0) |
((gb->borrowed_border.tiles[base + 1] & bit) ? 2 : 0) |
((gb->borrowed_border.tiles[base + 16] & bit) ? 4 : 0) |
((gb->borrowed_border.tiles[base + 17] & bit) ? 8 : 0);
uint32_t *output = gb->screen + tile_x * 8 + x + (tile_y * 8 + y) * 256; uint32_t *output = gb->screen + tile_x * 8 + x + (tile_y * 8 + y) * 256;
if (color == 0) { if (color == 0) {
*output = border_colors[0]; *output = border_colors[0];

View File

@ -115,19 +115,7 @@ static void load_default_border(GB_gameboy_t *gb)
#define LOAD_BORDER() do { \ #define LOAD_BORDER() do { \
memcpy(gb->borrowed_border.map, tilemap, sizeof(tilemap));\ memcpy(gb->borrowed_border.map, tilemap, sizeof(tilemap));\
memcpy(gb->borrowed_border.palette, palette, sizeof(palette));\ memcpy(gb->borrowed_border.palette, palette, sizeof(palette));\
\ memcpy(gb->borrowed_border.tiles, tiles, sizeof(tiles));\
/* Expand tileset */\
for (unsigned tile = 0; tile < sizeof(tiles) / 32; tile++) {\
for (unsigned y = 0; y < 8; y++) {\
for (unsigned x = 0; x < 8; x++) {\
gb->borrowed_border.tiles[tile * 8 * 8 + y * 8 + x] =\
(tiles[tile * 32 + y * 2 + 0] & (1 << (7 ^ x)) ? 1 : 0) |\
(tiles[tile * 32 + y * 2 + 1] & (1 << (7 ^ x)) ? 2 : 0) |\
(tiles[tile * 32 + y * 2 + 16] & (1 << (7 ^ x)) ? 4 : 0) |\
(tiles[tile * 32 + y * 2 + 17] & (1 << (7 ^ x)) ? 8 : 0);\
}\
}\
}\
} while (false); } while (false);
#ifdef GB_BIG_ENDIAN #ifdef GB_BIG_ENDIAN

View File

@ -324,8 +324,8 @@ static void sanitize_state(GB_gameboy_t *gb)
if (gb->object_priority == GB_OBJECT_PRIORITY_UNDEFINED) { if (gb->object_priority == GB_OBJECT_PRIORITY_UNDEFINED) {
gb->object_priority = gb->cgb_mode? GB_OBJECT_PRIORITY_INDEX : GB_OBJECT_PRIORITY_X; gb->object_priority = gb->cgb_mode? GB_OBJECT_PRIORITY_INDEX : GB_OBJECT_PRIORITY_X;
} }
if (gb->sgb && !gb->sgb->v14_3) {
#ifdef GB_BIG_ENDIAN #ifdef GB_BIG_ENDIAN
if (gb->sgb && !gb->sgb->little_endian) {
for (unsigned i = 0; i < sizeof(gb->sgb->border.raw_data) / 2; i++) { for (unsigned i = 0; i < sizeof(gb->sgb->border.raw_data) / 2; i++) {
gb->sgb->border.raw_data[i] = LE16(gb->sgb->border.raw_data[i]); gb->sgb->border.raw_data[i] = LE16(gb->sgb->border.raw_data[i]);
} }
@ -341,8 +341,36 @@ static void sanitize_state(GB_gameboy_t *gb)
for (unsigned i = 0; i < sizeof(gb->sgb->ram_palettes) / 2; i++) { for (unsigned i = 0; i < sizeof(gb->sgb->ram_palettes) / 2; i++) {
gb->sgb->ram_palettes[i] = LE16(gb->sgb->ram_palettes[i]); gb->sgb->ram_palettes[i] = LE16(gb->sgb->ram_palettes[i]);
} }
}
#endif #endif
uint8_t converted_tiles[sizeof(gb->sgb->border.tiles)] = {0,};
for (unsigned tile = 0; tile < sizeof(gb->sgb->border.tiles_legacy) / 64; tile++) {
for (unsigned y = 0; y < 8; y++) {
unsigned base = tile * 32 + y * 2;
for (unsigned x = 0; x < 8; x++) {
uint8_t pixel = gb->sgb->border.tiles_legacy[tile * 8 * 8 + y * 8 + x];
if (pixel & 1) converted_tiles[base] |= (1 << (7 ^ x));
if (pixel & 2) converted_tiles[base + 1] |= (1 << (7 ^ x));
if (pixel & 4) converted_tiles[base + 16] |= (1 << (7 ^ x));
if (pixel & 8) converted_tiles[base + 17] |= (1 << (7 ^ x));
}
}
}
memcpy(gb->sgb->border.tiles, converted_tiles, sizeof(converted_tiles));
memset(converted_tiles, 0, sizeof(converted_tiles));
for (unsigned tile = 0; tile < sizeof(gb->sgb->pending_border.tiles_legacy) / 64; tile++) {
for (unsigned y = 0; y < 8; y++) {
unsigned base = tile * 32 + y * 2;
for (unsigned x = 0; x < 8; x++) {
uint8_t pixel = gb->sgb->pending_border.tiles_legacy[tile * 8 * 8 + y * 8 + x];
if (pixel & 1) converted_tiles[base] |= (1 << (7 ^ x));
if (pixel & 2) converted_tiles[base + 1] |= (1 << (7 ^ x));
if (pixel & 4) converted_tiles[base + 16] |= (1 << (7 ^ x));
if (pixel & 8) converted_tiles[base + 17] |= (1 << (7 ^ x));
}
}
}
memcpy(gb->sgb->pending_border.tiles, converted_tiles, sizeof(converted_tiles));
}
} }
static bool dump_section(virtual_file_t *file, const void *src, uint32_t size) static bool dump_section(virtual_file_t *file, const void *src, uint32_t size)
@ -437,7 +465,7 @@ static int save_state_internal(GB_gameboy_t *gb, virtual_file_t *file)
uint32_t sgb_offset = 0; uint32_t sgb_offset = 0;
if (GB_is_hle_sgb(gb)) { if (GB_is_hle_sgb(gb)) {
gb->sgb->little_endian = true; gb->sgb->v14_3 = true;
sgb_offset = file->tell(file) + 4; sgb_offset = file->tell(file) + 4;
if (!dump_section(file, gb->sgb, sizeof(*gb->sgb))) goto error; if (!dump_section(file, gb->sgb, sizeof(*gb->sgb))) goto error;
} }

View File

@ -609,64 +609,67 @@ void GB_sgb_render(GB_gameboy_t *gb)
if (gb->sgb->vram_transfer_countdown) { if (gb->sgb->vram_transfer_countdown) {
if (--gb->sgb->vram_transfer_countdown == 0) { if (--gb->sgb->vram_transfer_countdown == 0) {
if (gb->sgb->transfer_dest == TRANSFER_LOW_TILES || gb->sgb->transfer_dest == TRANSFER_HIGH_TILES) { unsigned size = 0;
uint8_t *base = &gb->sgb->pending_border.tiles[gb->sgb->transfer_dest == TRANSFER_HIGH_TILES ? 0x80 * 8 * 8 : 0]; uint16_t *data = NULL;
for (unsigned tile = 0; tile < 0x80; tile++) {
unsigned tile_x = (tile % 10) * 16; switch (gb->sgb->transfer_dest) {
unsigned tile_y = (tile / 10) * 8; case TRANSFER_LOW_TILES:
for (unsigned y = 0; y < 0x8; y++) { size = 0x100;
for (unsigned x = 0; x < 0x8; x++) { data = (uint16_t *)gb->sgb->pending_border.tiles;
base[tile * 8 * 8 + y * 8 + x] = gb->sgb->screen_buffer[(tile_x + x) + (tile_y + y) * 160] + break;
gb->sgb->screen_buffer[(tile_x + x + 8) + (tile_y + y) * 160] * 4; case TRANSFER_HIGH_TILES:
} size = 0x100;
} data = (uint16_t *)gb->sgb->pending_border.tiles + 0x800;
} break;
case TRANSFER_PALETTES:
size = 0x100;
data = gb->sgb->ram_palettes;
break;
case TRANSFER_BORDER_DATA:
size = 0x88;
data = gb->sgb->pending_border.raw_data;
break;
case TRANSFER_ATTRIBUTES:
size = 0xFE;
data = (uint16_t *)gb->sgb->attribute_files;
break;
default:
return; // Corrupt state?
} }
else {
unsigned size = 0; for (unsigned tile = 0; tile < size; tile++) {
uint16_t *data = NULL; unsigned tile_x = (tile % 20) * 8;
unsigned tile_y = (tile / 20) * 8;
switch (gb->sgb->transfer_dest) { for (unsigned y = 0; y < 0x8; y++) {
case TRANSFER_PALETTES: static const uint16_t pixel_to_bits[4] = {0x0000, 0x0080, 0x8000, 0x8080};
size = 0x100; *data = 0;
data = gb->sgb->ram_palettes; for (unsigned x = 0; x < 8; x++) {
break; *data |= pixel_to_bits[gb->sgb->screen_buffer[(tile_x + x) + (tile_y + y) * 160] & 3] >> x;
case TRANSFER_BORDER_DATA:
size = 0x88;
data = gb->sgb->pending_border.raw_data;
break;
case TRANSFER_ATTRIBUTES:
size = 0xFE;
data = (uint16_t *)gb->sgb->attribute_files;
break;
default:
return; // Corrupt state?
}
for (unsigned tile = 0; tile < size; tile++) {
unsigned tile_x = (tile % 20) * 8;
unsigned tile_y = (tile / 20) * 8;
for (unsigned y = 0; y < 0x8; y++) {
static const uint16_t pixel_to_bits[4] = {0x0000, 0x0080, 0x8000, 0x8080};
*data = 0;
for (unsigned x = 0; x < 8; x++) {
*data |= pixel_to_bits[gb->sgb->screen_buffer[(tile_x + x) + (tile_y + y) * 160] & 3] >> x;
}
#ifdef GB_BIG_ENDIAN
*data = __builtin_bswap16(*data);
#endif
data++;
} }
#ifdef GB_BIG_ENDIAN
*data = __builtin_bswap16(*data);
#endif
data++;
} }
if (gb->sgb->transfer_dest == TRANSFER_BORDER_DATA) { }
gb->sgb->border_animation = 64; if (gb->sgb->transfer_dest == TRANSFER_BORDER_DATA) {
} gb->sgb->border_animation = 64;
} }
} }
} }
if (!gb->screen || !gb->rgb_encode_callback || gb->disable_rendering) return; if (!gb->screen || !gb->rgb_encode_callback || gb->disable_rendering) {
if (gb->sgb->border_animation > 32) {
gb->sgb->border_animation--;
}
else if (gb->sgb->border_animation != 0) {
gb->sgb->border_animation--;
}
if (gb->sgb->border_animation == 32) {
memcpy(&gb->sgb->border, &gb->sgb->pending_border, sizeof(gb->sgb->border));
}
return;
}
uint32_t colors[4 * 4]; uint32_t colors[4 * 4];
for (unsigned i = 0; i < 4 * 4; i++) { for (unsigned i = 0; i < 4 * 4; i++) {
@ -764,12 +767,18 @@ void GB_sgb_render(GB_gameboy_t *gb)
continue; continue;
} }
uint16_t tile = LE16(gb->sgb->border.map[tile_x + tile_y * 32]); uint16_t tile = LE16(gb->sgb->border.map[tile_x + tile_y * 32]);
uint8_t flip_x = (tile & 0x4000)? 0x7 : 0; uint8_t flip_x = (tile & 0x4000)? 0:7;
uint8_t flip_y = (tile & 0x8000)? 0x7 : 0; uint8_t flip_y = (tile & 0x8000)? 7:0;
uint8_t palette = (tile >> 10) & 3; uint8_t palette = (tile >> 10) & 3;
for (unsigned y = 0; y < 8; y++) { for (unsigned y = 0; y < 8; y++) {
unsigned base = (tile & 0xFF) * 32 + (y ^ flip_y) * 2;
for (unsigned x = 0; x < 8; x++) { for (unsigned x = 0; x < 8; x++) {
uint8_t color = gb->sgb->border.tiles[(tile & 0xFF) * 64 + (x ^ flip_x) + (y ^ flip_y) * 8] & 0xF; uint8_t bit = 1 << (x ^ flip_x);
uint8_t color = ((gb->sgb->border.tiles[base] & bit) ? 1: 0) |
((gb->sgb->border.tiles[base + 1] & bit) ? 2: 0) |
((gb->sgb->border.tiles[base + 16] & bit) ? 4: 0) |
((gb->sgb->border.tiles[base + 17] & bit) ? 8: 0);
uint32_t *output = gb->screen; uint32_t *output = gb->screen;
if (gb->border_mode == GB_BORDER_NEVER) { if (gb->border_mode == GB_BORDER_NEVER) {
output += (tile_x - 6) * 8 + x + ((tile_y - 5) * 8 + y) * 160; output += (tile_x - 6) * 8 + x + ((tile_y - 5) * 8 + y) * 160;
@ -806,19 +815,7 @@ void GB_sgb_load_default_data(GB_gameboy_t *gb)
memcpy(gb->sgb->border.map, tilemap, sizeof(tilemap)); memcpy(gb->sgb->border.map, tilemap, sizeof(tilemap));
memcpy(gb->sgb->border.palette, palette, sizeof(palette)); memcpy(gb->sgb->border.palette, palette, sizeof(palette));
#endif #endif
memcpy(gb->sgb->border.tiles, tiles, sizeof(tiles));
/* Expand tileset */
for (unsigned tile = 0; tile < sizeof(tiles) / 32; tile++) {
for (unsigned y = 0; y < 8; y++) {
for (unsigned x = 0; x < 8; x++) {
gb->sgb->border.tiles[tile * 8 * 8 + y * 8 + x] =
(tiles[tile * 32 + y * 2 + 0] & (1 << (7 ^ x)) ? 1 : 0) |
(tiles[tile * 32 + y * 2 + 1] & (1 << (7 ^ x)) ? 2 : 0) |
(tiles[tile * 32 + y * 2 + 16] & (1 << (7 ^ x)) ? 4 : 0) |
(tiles[tile * 32 + y * 2 + 17] & (1 << (7 ^ x)) ? 8 : 0);
}
}
}
if (gb->model != GB_MODEL_SGB2) { if (gb->model != GB_MODEL_SGB2) {
/* Delete the "2" */ /* Delete the "2" */

View File

@ -6,7 +6,10 @@
typedef struct GB_sgb_s GB_sgb_t; typedef struct GB_sgb_s GB_sgb_t;
typedef struct { typedef struct {
uint8_t tiles[0x100 * 8 * 8]; /* High nibble not used*/ union {
uint8_t tiles[0x100 * 8 * 4];
uint8_t tiles_legacy[0x100 * 8 * 8]; /* High nibble not used; TODO: Remove when breaking save-state compatibility! */
};
union { union {
struct { struct {
uint16_t map[32 * 32]; uint16_t map[32 * 32];
@ -59,7 +62,7 @@ struct GB_sgb_s {
/* Multiplayer (cont) */ /* Multiplayer (cont) */
bool mlt_lock; bool mlt_lock;
bool little_endian; // True on save states created on 0.14.3 or newer bool v14_3; // True on save states created on 0.14.3 or newer; Remove when breaking save state compatibility!
}; };
void GB_sgb_write(GB_gameboy_t *gb, uint8_t value); void GB_sgb_write(GB_gameboy_t *gb, uint8_t value);