From 7539742689990be0687f82b19d3bc9b312ed528d Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Sat, 16 Apr 2016 10:39:35 -0700 Subject: [PATCH] GB Video: Fix timing for vblank IRQs --- src/gb/video.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/gb/video.c b/src/gb/video.c index 5b7d43707..1aadb9b70 100644 --- a/src/gb/video.c +++ b/src/gb/video.c @@ -143,29 +143,35 @@ int32_t GBVideoProcessEvents(struct GBVideo* video, int32_t cycles) { case 1: // TODO: One M-cycle delay ++video->ly; - video->stat = GBRegisterSTATSetLYC(video->stat, lyc == video->ly); - if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS) { + if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS + 1) { video->ly = 0; + video->p->memory.io[REG_LY] = video->ly; video->nextMode = GB_VIDEO_MODE_2_LENGTH; video->mode = 2; if (GBRegisterSTATIsOAMIRQ(video->stat)) { video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT); + GBUpdateIRQs(video->p); } - } else { - video->nextMode = GB_VIDEO_HORIZONTAL_LENGTH; - } - if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS - 1) { + break; + } else if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS) { video->p->memory.io[REG_LY] = 0; + video->nextMode = GB_VIDEO_HORIZONTAL_LENGTH - 8; + } else if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS - 1) { + video->p->memory.io[REG_LY] = video->ly; + video->nextMode = 8; } else { video->p->memory.io[REG_LY] = video->ly; + video->nextMode = GB_VIDEO_HORIZONTAL_LENGTH; } - if (GBRegisterSTATIsLYCIRQ(video->stat) && lyc == video->ly) { + + video->stat = GBRegisterSTATSetLYC(video->stat, lyc == video->p->memory.io[REG_LY]); + if (GBRegisterSTATIsLYCIRQ(video->stat) && lyc == video->p->memory.io[REG_LY]) { video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT); + GBUpdateIRQs(video->p); } if (video->p->memory.mbcType == GB_MBC7 && video->p->memory.rotation && video->p->memory.rotation->sample) { video->p->memory.rotation->sample(video->p->memory.rotation); } - GBUpdateIRQs(video->p); break; case 2: _cleanOAM(video, video->ly);