diff --git a/Core/display.c b/Core/display.c index f5ba685..d13a344 100644 --- a/Core/display.c +++ b/Core/display.c @@ -123,7 +123,12 @@ static bool window_enabled(GB_gameboy_t *gb) static void display_vblank(GB_gameboy_t *gb) { gb->vblank_just_occured = true; - + + /* TODO: Slow in trubo mode! */ + if (GB_is_sgb(gb)) { + GB_sgb_render(gb); + } + if (gb->turbo) { if (GB_timing_sync_turbo(gb)) { return; @@ -133,7 +138,7 @@ static void display_vblank(GB_gameboy_t *gb) if (!gb->disable_rendering && ((!(gb->io_registers[GB_IO_LCDC] & 0x80) || gb->stopped) || gb->frame_skip_state == GB_FRAMESKIP_LCD_TURNED_ON)) { /* LCD is off, set screen to white or black (if LCD is on in stop mode) */ if (gb->sgb) { - uint8_t color = (gb->io_registers[GB_IO_LCDC] & 0x80) && gb->stopped ? 0 : 0xFF; + uint8_t color = (gb->io_registers[GB_IO_LCDC] & 0x80) && gb->stopped ? 0x3 : 0x0; for (unsigned i = 0; i < WIDTH * LINES; i++) { gb->sgb->screen_buffer[i] = color; } @@ -148,9 +153,6 @@ static void display_vblank(GB_gameboy_t *gb) } } - if (GB_is_sgb(gb)) { - GB_sgb_render(gb); - } gb->vblank_callback(gb); GB_timing_sync(gb); } diff --git a/Core/sgb.c b/Core/sgb.c index c60afff..ce3016c 100644 --- a/Core/sgb.c +++ b/Core/sgb.c @@ -2,6 +2,8 @@ enum { MLT_REQ = 0x11, + CHR_TRN = 0x13, + PCT_TRN = 0x14, MASK_EN = 0x17, }; @@ -57,6 +59,17 @@ static void command_ready(GB_gameboy_t *gb) case MASK_EN: gb->sgb->mask_mode = gb->sgb->command[1] & 3; break; + case CHR_TRN: + gb->sgb->vram_transfer_countdown = 2; + gb->sgb->tile_transfer = true; + gb->sgb->data_transfer = false; + gb->sgb->tile_transfer_high = gb->sgb->command[1] & 1; + break; + case PCT_TRN: + gb->sgb->vram_transfer_countdown = 2; + gb->sgb->tile_transfer = false; + gb->sgb->data_transfer = true; + break; default: GB_log(gb, "Unimplemented SGB command %x: ", gb->sgb->command[0] >> 3); for (unsigned i = 0; i < gb->sgb->command_write_index / 8; i++) { @@ -139,11 +152,28 @@ void GB_sgb_write(GB_gameboy_t *gb, uint8_t value) } } +static inline uint8_t scale_channel(uint8_t x) +{ + return (x << 3) | (x >> 2); +} + +static uint32_t convert_rgb15(GB_gameboy_t *gb, uint16_t color) +{ + uint8_t r = (color) & 0x1F; + uint8_t g = (color >> 5) & 0x1F; + uint8_t b = (color >> 10) & 0x1F; + + r = scale_channel(r); + g = scale_channel(g); + b = scale_channel(b); + + return gb->rgb_encode_callback(gb, r, g, b); +} + void GB_sgb_render(GB_gameboy_t *gb) { if (!gb->screen || !gb->rgb_encode_callback) return; - uint32_t border = gb->rgb_encode_callback(gb, 0x9c, 0x9c, 0xa5); uint32_t colors[] = { gb->rgb_encode_callback(gb, 0xf7, 0xe7, 0xc6), gb->rgb_encode_callback(gb, 0xd6, 0x8e, 0x49), @@ -151,8 +181,9 @@ void GB_sgb_render(GB_gameboy_t *gb) gb->rgb_encode_callback(gb, 0x33, 0x1e, 0x50) }; - for (unsigned i = 0; i < 256 * 224; i++) { - gb->screen[i] = border; + uint32_t border_colors[16 * 4]; + for (unsigned i = 0; i < 16 * 4; i++) { + border_colors[i] = convert_rgb15(gb, gb->sgb->border_palette[i]); } switch ((mask_mode_t) gb->sgb->mask_mode) { @@ -175,7 +206,40 @@ void GB_sgb_render(GB_gameboy_t *gb) sizeof(gb->sgb->effective_screen_buffer)); } - uint32_t *output = &gb->screen[48 + 39 * 256]; + if (gb->sgb->vram_transfer_countdown) { + if (--gb->sgb->vram_transfer_countdown == 0) { + if (gb->sgb->tile_transfer) { + uint8_t *base = &gb->sgb->tiles[gb->sgb->tile_transfer_high? 0x80 * 8 * 8 : 0]; + for (unsigned tile = 0; tile < 0x80; tile++) { + unsigned tile_x = (tile % 10) * 16; + unsigned tile_y = (tile / 10) * 8; + for (unsigned y = 0; y < 0x8; y++) { + for (unsigned x = 0; x < 0x8; x++) { + base[tile * 8 * 8 + y * 8 + x] = gb->sgb->screen_buffer[(tile_x + x) + (tile_y + y) * 160] + + gb->sgb->screen_buffer[(tile_x + x + 8) + (tile_y + y) * 160] * 4; + } + } + } + + } + else if (gb->sgb->data_transfer) { + for (unsigned tile = 0; tile < 0x88; 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}; + uint16_t *data = &gb->sgb->raw_pct_data[tile * 8 + y]; + *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; + } + } + } + } + } + } + + uint32_t *output = &gb->screen[48 + 40 * 256]; uint8_t *input = gb->sgb->effective_screen_buffer; for (unsigned y = 0; y < 144; y++) { for (unsigned x = 0; x < 160; x++) { @@ -183,4 +247,25 @@ void GB_sgb_render(GB_gameboy_t *gb) } output += 256 - 160; } + + for (unsigned tile_y = 0; tile_y < 28; tile_y++) { + for (unsigned tile_x = 0; tile_x < 32; tile_x++) { + bool gb_area = false; + if (tile_x >= 6 && tile_x < 26 && tile_y >= 5 && tile_y < 23) { + gb_area = true; + } + uint16_t tile = gb->sgb->map[tile_x + tile_y * 32]; + uint8_t flip_x = (tile & 0x4000)? 0x7 : 0; + uint8_t flip_y = (tile & 0x8000)? 0x7 : 0; + uint8_t palette = (tile >> 10) & 3; + for (unsigned y = 0; y < 8; y++) { + for (unsigned x = 0; x < 8; x++) { + uint8_t color = gb->sgb->tiles[(tile & 0xFF) * 64 + (x ^ flip_x) + (y ^ flip_y) * 8] & 0xF; + if (color == 0 && gb_area) continue; + gb->screen[tile_x * 8 + x + (tile_y * 8 + y) * 0x100] = + border_colors[palette * 16 + color]; + } + } + } + } } diff --git a/Core/sgb.h b/Core/sgb.h index bafde35..d911125 100644 --- a/Core/sgb.h +++ b/Core/sgb.h @@ -22,6 +22,19 @@ typedef struct { /* Mask */ uint8_t mask_mode; + + /* Border */ + uint8_t vram_transfer_countdown; + bool tile_transfer, tile_transfer_high, data_transfer; + uint8_t tiles[0x100 * 8 * 8]; /* High nibble not used*/ + union { + struct { + uint16_t map[32 * 32]; + uint16_t border_palette[16 * 4]; + }; + uint16_t raw_pct_data[0x440]; + }; + } GB_sgb_t; void GB_sgb_write(GB_gameboy_t *gb, uint8_t value);