Page 1 of 1

Game Boy display disable/enable and frame timing

Posted: Tue Jul 07, 2015 6:35 pm
by bendm
I'm trying to work out how to sync between my nascent Game Boy emulator's CPU/video/audio processors, and there's a sticky edge case: when a write to 0xff40 disables the LCD display.

Before implementing audio, I ran everything on a per-frame basis. When I hit the end of vblank, I would yield control to the renderer, wait until the next frame, and pick up. If display was disabled, I'd keep running until it was re-enabled and the following rendering frame completed. As a result frames would have a variable number of CPU cycles (which I think did cause some subtle timing bugs) but mostly it worked fine.

Now that I've added audio processing, I'm generating 735 audio samples (44100 / 60) per one frame's worth of CPU cycles (usually 456 * 154 = 70224 cycles.) This works until LCD is disabled and enabled again later, which results in too many audio samples being pushed to the audio buffer that frame, and the audio playback gets progressively more backed up. I've tried cutting the audio sample generation off after exactly 735 samples each frame, but this doesn't seem correct either and results in audible gaps in playback.

Any advice on how to resolve this, and how many cycles to run each processor per frame?

Re: Game Boy display disable/enable and frame timing

Posted: Tue Jul 07, 2015 6:40 pm
by tepples
If LCD is disabled, generate an all-white frame (Game Boy) or repeat the previous frame (Super Game Boy).

Licensed games aren't allowed to turn rendering on and off mid-frame because the output drivers don't get clocked to the next scanline. This results in visible artifacts and, over the long term, possible burn-in.

Re: Game Boy display disable/enable and frame timing

Posted: Tue Jul 07, 2015 7:04 pm
by bendm
Thanks for the tip. Can I rely on having it disabled for exact multiples of a frame or can the disabled period be an arbitrary number of cycles? If it's disabled for say 50 scanlines worth of CPU cycles and then re-enabled, and I treat the disabled period as a separate frame, I'll have an audio buffer underflow.

Re: Game Boy display disable/enable and frame timing

Posted: Tue Jul 07, 2015 7:21 pm
by Shonumi
The disabled period can be arbitrary. As far as I know, the LCD's internal clock gets reset to 0 (e.g. when it's re-enabled, your next VBlank is in 70224 cycles), but there's no restriction on when it can be re-enabled. It can be 20 cycles, or 20 minutes. It's up the the game, so don't bet on any consistency.

Re: Game Boy display disable/enable and frame timing

Posted: Sun Sep 03, 2017 7:46 am
by zerowalker
In that case it LY also reset, cause otherwise it would go out of sync with that clock right?

And also, when it's disabled, is the clock stopped, or does it run and it just resets when re-enabled?

Re: Game Boy display disable/enable and frame timing

Posted: Sun Sep 03, 2017 1:10 pm
by Shonumi
zerowalker wrote:In that case it LY also reset, cause otherwise it would go out of sync with that clock right?
Already mentioned it here but yes, LY gets reset to 0 when the LCD is disabled. Otherwise weird things would happen.
zerowalker wrote:And also, when it's disabled, is the clock stopped, or does it run and it just resets when re-enabled?
The LCD's internal timing is frozen. That means nothing related to the LCD clocks. LY isn't incremented. No STAT or VBlank interrupts. We tried testing this a while ago just to be sure.

Re: Game Boy display disable/enable and frame timing

Posted: Sun Sep 03, 2017 11:40 pm
by zerowalker
My bad missed that part about LY being reset directly on Disabled.

Will check the tests about the timing, thanks:)