diff --git a/Core/display.c b/Core/display.c index 85fc858..e01c789 100644 --- a/Core/display.c +++ b/Core/display.c @@ -524,6 +524,7 @@ static void render_pixel_if_possible(GB_gameboy_t *gb) } gb->position_in_line++; + gb->window_is_being_fetched = false; } /* All verified CGB timings are based on CGB CPU E. CGB CPUs >= D are known to have @@ -960,9 +961,19 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) fifo_clear(&gb->bg_fifo); gb->fetcher_state = 0; window_got_activated = true; + gb->window_is_being_fetched = true; } } } + + if (!GB_is_cgb(gb) && gb->wx_triggered && !gb->window_is_being_fetched && + gb->fetcher_state == 0 && gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 7) ) { + // Insert a pixel right at the FIFO's end + gb->bg_fifo.read_end--; + gb->bg_fifo.read_end &= GB_FIFO_LENGTH - 1; + gb->bg_fifo.fifo[gb->bg_fifo.read_end] = (GB_fifo_item_t){0,}; + gb->window_is_being_fetched = false; + } /* Handle objects */ /* When the sprite enabled bit is off, this proccess is skipped entirely on the DMG, but not on the CGB. diff --git a/Core/gb.h b/Core/gb.h index 65cfb4c..15c6442 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -500,7 +500,7 @@ struct GB_gameboy_internal_s { uint8_t current_tile_attributes; uint8_t current_tile_data[2]; uint8_t fetcher_state; - GB_PADDING(bool,bg_fifo_paused); + bool window_is_being_fetched; GB_PADDING(bool,oam_fifo_paused); bool wx_triggered; uint8_t visible_objs[10];