From 39b88d546ba4c180683e32367e47ab895a9ba192 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 21 Feb 2020 21:59:03 +0200 Subject: [PATCH] The upper bits of SCX might mid-line --- Core/display.c | 6 ++++-- Core/save_state.c | 2 ++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Core/display.c b/Core/display.c index 13bd955..62e2acd 100644 --- a/Core/display.c +++ b/Core/display.c @@ -581,18 +581,20 @@ static void advance_fetcher_state_machine(GB_gameboy_t *gb) /* Todo: Verified for DMG (Tested: SGB2), CGB timing is wrong. */ uint8_t y = fetcher_y(gb); + uint8_t x = gb->in_window? gb->fetcher_x : + (((gb->io_registers[GB_IO_SCX] / 8) + (gb->position_in_line / 8) + 1) & 0x1F); if (gb->model > GB_MODEL_CGB_C) { /* This value is cached on the CGB-D and newer, so it cannot be used to mix tiles together */ gb->fetcher_y = y; } - gb->current_tile = gb->vram[map + gb->fetcher_x + y / 8 * 32]; + gb->current_tile = gb->vram[map + x + y / 8 * 32]; if (gb->vram_ppu_blocked) { gb->current_tile = 0xFF; } if (GB_is_cgb(gb)) { /* The CGB actually accesses both the tile index AND the attributes in the same T-cycle. This probably means the CGB has a 16-bit data bus for the VRAM. */ - gb->current_tile_attributes = gb->vram[map + gb->fetcher_x + y / 8 * 32 + 0x2000]; + gb->current_tile_attributes = gb->vram[map + x + y / 8 * 32 + 0x2000]; if (gb->vram_ppu_blocked) { gb->current_tile_attributes = 0xFF; } diff --git a/Core/save_state.c b/Core/save_state.c index a0d88c2..8f10152 100644 --- a/Core/save_state.c +++ b/Core/save_state.c @@ -266,6 +266,7 @@ int GB_load_state(GB_gameboy_t *gb, const char *path) gb->oam_fifo.read_end &= 0xF; gb->oam_fifo.write_end &= 0xF; gb->object_low_line_address &= gb->vram_size & ~1; + gb->fetcher_x &= 0x1f; if (gb->object_priority == GB_OBJECT_PRIORITY_UNDEFINED) { gb->object_priority = gb->cgb_mode? GB_OBJECT_PRIORITY_INDEX : GB_OBJECT_PRIORITY_X; @@ -376,6 +377,7 @@ int GB_load_state_from_buffer(GB_gameboy_t *gb, const uint8_t *buffer, size_t le gb->oam_fifo.read_end &= 0xF; gb->oam_fifo.write_end &= 0xF; gb->object_low_line_address &= gb->vram_size & ~1; + gb->fetcher_x &= 0x1f; if (gb->object_priority == GB_OBJECT_PRIORITY_UNDEFINED) { gb->object_priority = gb->cgb_mode? GB_OBJECT_PRIORITY_INDEX : GB_OBJECT_PRIORITY_X;