I understand the timing and logic behind the Sprite 0 hit flag, but I don't understand how the PPU knows which sprite is the 0th one. Here's what (I think) happens in the PPU when rendering a sprite on scanline N.
1. User loads 256 bytes of sprite data into OAM via $4014. Obviously the first 4 bytes are "Sprite 0"
2. During render phase, during first 256 clocks of scanline N-1, up to 8 sprites (32 bytes) copied into secondary OAM for use when rendering scanline N.
3. Starting at clock 257 of scanline N-1, secondary OAM is copied into the render latches (for x_counters, sprite attributes, sprite bitmaps)
4. During clocks 0-256 of scanline N, render latches are used (along with Background latches/shift registers) to render pixels. At this point, the pixel is evaluated to see if a Sprite 0 hit (BG !=0 and SPR !=0).
So my question is, how would the PPU distinguish between Sprite 0, and any other sprite. Lets say Sprite 0 in OAM contained A8 10 00 30 (It's drawn on scanline 169, x position 48). What if Sprite 4 also contained A8 10 00 30? The renderer can't tell whether the copy of the sprite in secondary OAM came from sprite 0 or 4. It makes little difference in this case since they are identical - but what if they weren't? What if they were almost the same - same X and Y positions, but slightly different attributes.
Can the PPU be fooled into thinking a sprite N is sprite 0?
To make things easier in my emulator, I'm using one of the "Unimplemented" bits in the Sprite attributes byte to indicate "is Sprite 0". Does anyone know what mechanism a real NES uses?
1. User loads 256 bytes of sprite data into OAM via $4014. Obviously the first 4 bytes are "Sprite 0"
2. During render phase, during first 256 clocks of scanline N-1, up to 8 sprites (32 bytes) copied into secondary OAM for use when rendering scanline N.
3. Starting at clock 257 of scanline N-1, secondary OAM is copied into the render latches (for x_counters, sprite attributes, sprite bitmaps)
4. During clocks 0-256 of scanline N, render latches are used (along with Background latches/shift registers) to render pixels. At this point, the pixel is evaluated to see if a Sprite 0 hit (BG !=0 and SPR !=0).
So my question is, how would the PPU distinguish between Sprite 0, and any other sprite. Lets say Sprite 0 in OAM contained A8 10 00 30 (It's drawn on scanline 169, x position 48). What if Sprite 4 also contained A8 10 00 30? The renderer can't tell whether the copy of the sprite in secondary OAM came from sprite 0 or 4. It makes little difference in this case since they are identical - but what if they weren't? What if they were almost the same - same X and Y positions, but slightly different attributes.
Can the PPU be fooled into thinking a sprite N is sprite 0?
To make things easier in my emulator, I'm using one of the "Unimplemented" bits in the Sprite attributes byte to indicate "is Sprite 0". Does anyone know what mechanism a real NES uses?