Sprite 0 hit

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
Sprite 0 hit
by on (#115046)
Making a NES emulator, here's where I'm at: http://imgur.com/O79p9sl

I'm trying to get sprites to display properly and I've discovered the hangup involves the sprite 0 hit flag. There are loops in the game I'm trying (super mario bros) that wait for VBLANK by cycling endlessly until the flag is set, then cleared. When should I be setting this flag? Is there a way to fake this operation, i.e. set the 0 hit flag at the first scanline? Sorry if my question is too game-specific.
Re: Sprite 0 hit
by on (#115047)
Sprite zero hit turns off at line 261 (311 for PAL NES and Dendy) and turns on the moment a nontransparent pixel of sprite 0 is drawn overlapping a nontransparent pixel of the background (or vice versa; priority is disregarded). If the highest such overlap is at (64, 30), for example, then it'll turn on 64 dots (21 cycles) into line 30. To fake it for SMB1, set it on line 30.
Re: Sprite 0 hit
by on (#115054)
I assume you didn't add the $2007 read buffer (post-fetch), since the screen is scrambled. Do it and trust me - it'll look much better. ;)
Re: Sprite 0 hit
by on (#115070)
This early in your emulator you maybe shouldn't worry about accurately emulating Sprite hit Zero. As tepples suggests, just fake it by setting the flag around the scanline that the sprite appears. There are many other important things you'll want to get done before Sprite Hit Zero. Many games don't require sprite hit zero emulation. Emulating the flag can be tricky depending on how you emulate the CPU/PPU together and how you eumlate the PPU exactly.
Re: Sprite 0 hit
by on (#115074)
You might want to start with a different NROM game. Emulating SMB1 can be tricky.
Re: Sprite 0 hit
by on (#115136)
@Tepples - Setting the flag at line 30 did the trick, for now
@zepper - yes! That fixed it the screen scrambling

I tested Mario, Excitebike, Donkey Kong Jr., and Donkey Kong, they all seemed pretty good in non-scrolling bits