Do Blargg's test roms write to LCDC outside V-Blank period?

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
Do Blargg's test roms write to LCDC outside V-Blank period?
by on (#200663)
Hello, I'm writing a Gameboy emulator and I ran into problems.

Some time, each test rom (from cpu_instrs) that my emulator executes, writes to LCDC not during V-Blank period, which is bad.

Are there knowledgeable people who can help me, is this problem of my emulator or not?

UPDATE:
I just have understood how dumb this question is. Sorry for wasting your time, my problem is solved.
Re: Do Blargg's test roms write to LCDC outside V-Blank peri
by on (#200683)
You can write to LCDC outside VBlank, just as long as you don't turn off the LCD (bit 7 set to 0).
Re: Do Blargg's test roms write to LCDC outside V-Blank peri
by on (#200687)
Thank you for reply.

Yes, I see. The problem was my emulator didn't correctly handle writing to LCDC.

That was incorrect code:
Code:
void GPU::write_lcd_control(unsigned value) noexcept
{
    control = value;
    if (control & CONTROL_MASK_LCD_DISPLAY_ENABLE) {
        stat = (stat & ~STAT_MASK_MODE_FLAG) | STAT_MODE_READ_OAM;   mode_clock = 0;
    }
    else {
        stat &= ~(STAT_MASK_MODE_FLAG | STAT_MASK_COINCIDENCE_FLAG);         ly = 0;
    }
}


I rewrote the code as follows:
Code:
void GPU::write_lcd_control(unsigned value) noexcept
{
    if ((control ^ value) & CONTROL_MASK_LCD_DISPLAY_ENABLE)
    {
        if (value & CONTROL_MASK_LCD_DISPLAY_ENABLE) {
            stat = (stat & ~STAT_MASK_MODE_FLAG) | STAT_MODE_READ_OAM;      mode_clock = 0;
        } else {
            stat &= ~(STAT_MASK_MODE_FLAG | STAT_MASK_COINCIDENCE_FLAG);    ly = 0;
        }
    }
    control = value;
}