Game Boy display disable/enable and frame timing

Discussion of programming and development for the original Game Boy and Game Boy Color.
Post Reply
bendm
Posts: 4
Joined: Wed Jun 10, 2015 8:20 pm

Game Boy display disable/enable and frame timing

Post 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?
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Game Boy display disable/enable and frame timing

Post 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.
bendm
Posts: 4
Joined: Wed Jun 10, 2015 8:20 pm

Re: Game Boy display disable/enable and frame timing

Post 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.
Shonumi
Posts: 342
Joined: Sun Jan 26, 2014 9:31 am

Re: Game Boy display disable/enable and frame timing

Post 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.
zerowalker
Posts: 68
Joined: Tue May 17, 2016 10:15 pm

Re: Game Boy display disable/enable and frame timing

Post 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?
Shonumi
Posts: 342
Joined: Sun Jan 26, 2014 9:31 am

Re: Game Boy display disable/enable and frame timing

Post 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.
zerowalker
Posts: 68
Joined: Tue May 17, 2016 10:15 pm

Re: Game Boy display disable/enable and frame timing

Post by zerowalker »

My bad missed that part about LY being reset directly on Disabled.

Will check the tests about the timing, thanks:)
Post Reply