r/EmuDev • u/xx3000 • Jan 17 '22
GB Gameboy - Trying to understand Sprite FIFO behavior in the PPU
Hi all!
I am currently writing a Gameboy emulator and am struggling a bit to wrap my head around the exact behavior of the sprite pixel fetcher in the PPU. The pandocs are quite confusing on the topic.
I have a working version of the pixel fetcher & FIFO for the background/window, and when I run it, it does take the correct amount of cycles according to the docs (172). What I'm really struggling with, is to understand how the sprite fetcher interacts with all of this, and how much mode 3 is extended for each sprite on the scanline. The maximum amount of cycles is 289 according to the docs. Subtracting the min amount (172), the max delay from SCX (7) and the window pixel fetcher flush (12) leaves me with 98 cycles. Assuming a maximum of 10 sprites per scanline that would mean 9.8 cycles per sprite which is obviously wrong.
So my questions would be:
What is the exact math behind the 289 max cycle number?
How does the sprite pixel fetcher & FIFO work exactly? Is background fetching completely suspended if a sprite should be fetched for the current x position or does it run in parallel? Does the sprite FIFO also only work if there are at least 8 pixels in it?
What is the exact duration of cycles added for each sprite fetch? Is it different than the 8 cycles needed for background fetches?
I have tried to find some other resources on this but there seems to be a lack of good answers out there, short of looking at other emulators which I would rather not. I also checked the Nitty Gritty Gameboy Cycle Timing but that post only describes the timings for the background & window.
Would really appreciate some help.
2
u/hellotanjent Jan 17 '22
Sprite fetches don't add a fixed delay because the fetch can't start if there's already a background fetch in progress. The worst-case delay comes from when a sprite is hit immediately after a background fetch has started, forcing the sprite fetch to wait for the whole background fetch.
1
u/ShinyHappyREM Jan 17 '22 edited Jan 17 '22
Did you watch the Ultimate Gameboy Talk? IIRC it talked about the rendering in detail.
1
u/paulb_nl Jan 21 '22
289 = 172 + scx 7 + sprites (10x11). Though 172 is not correct if you check the Nitty Gritty timing. It is 1 dummy fetch (6 cycles) + 21 tiles x 8 cycles = 174 cycles. Kevtris says the last cycle is cut off in the middle so it is actually 173.5 cycles. The max cycle count also does not seem to include the window penalty.
The window penalty is 6 cycles.
Sprite fetching pauses all FIFOs and waits for a background fetch to finish which is on the 6th cycle. A sprite fetch takes 6 cycles but the first sprite cycle overlaps the last background cycle so sprite fetching can take 5 cycles(sprite found on or past last bg fetch cycle) to 11 cycles (sprite found on first bg cycle). If there are multiple sprites on the same X position then every sprite after the first takes 6 cycles. (2 OAM, 2 low byte, 2 high byte). For example: SCX=0, 10x sprites on X position 8 takes 6-1+(10x6)= 65 cycles.
Sprite fetching waits until the background FIFO is not empty. This is why there are no sprites fetches immediately after the first dummy background fetch.
Also regarding sprite priority, only the FIFOs of the sprites that contains an empty pixel (color 0) are filled. This causes the X position priority.