This page is a mirror of Tepples' nesdev forum mirror (URL TBD).
Last updated on Oct-18-2019 Download

Optimizing vblank use

Optimizing vblank use
by on (#64908)
I can't remember if I've asked this before, but the issue has come up again.

When vblank starts, is it better to do the sprite DMA first, and the $2007 streaming afterwards, or is it better to do the $2007 streaming first, and the DMA afterwards?

Also, another question; If I have rendering turned off, and I'm writing to $2006 and $2007 in my main code, and then I have two writes to $2005 taking place in the NMI, will that mess up the PPU address if the NMI takes place while I'm still writing to $2007?

by on (#64909)
Order of sprite DMA and PPU drawing doesn't matter, they still take the same amount of time regardless of what order you do them in. What matters is whether you have rendering disabled or not. With rendering disabled, you get to use the prerender line for additional draw time, then you can enable rendering afterwards once you've written your scroll values.

Writes to 2005 during NMI should not affect drawing code, since it only changes T, and not V.

What I did was make the first instruction of NMI an indirect jump, and use a different NMI handler for a screen which is off. I basically used a different NMI handler for each type of screen that needs different raster effects, since the NMI handler installs the first MMC3 interrupt handler.
Re: Optimizing vblank use
by on (#64910)
Drag wrote:
When vblank starts, is it better to do the sprite DMA first, and the $2007 streaming afterwards, or is it better to do the $2007 streaming first, and the DMA afterwards?

I don't think it matters, as long as both take place during VBlank.

Quote:
Also, another question; If I have rendering turned off, and I'm writing to $2006 and $2007 in my main code, and then I have two writes to $2005 taking place in the NMI, will that mess up the PPU address if the NMI takes place while I'm still writing to $2007?

I'm pretty sure it will screw up the PPU address. You should either disable NMIs along with the rendering or have your NMI check a flag that indicates whether it should perform PPU operations.

EDIT: Sorry, I didn't realize you write only to $2005, so yeah, Dwedit is right. But why do you write only to $2005, seeing as that alone isn't enough to fully set the scroll (you still have to select a name table)?
Re: Optimizing vblank use
by on (#64913)
tokumaru wrote:
I didn't realize you write only to $2005, so yeah, Dwedit is right. But why do you write only to $2005, seeing as that alone isn't enough to fully set the scroll (you still have to select a name table)?

I'd guess that the nametable bits will be set to the last VRAM address that was used. In one-screen mirroring, this Just Works, and it also Just Works if you're displaying the same nametable that you wrote to, such as in a non-scrolling game. For example, in horizontal mirroring, a write across all of $2000-$23FF will leave the nametable select bits pointing at either $2000 or $2400, and a write to $2800-$2BFF will leave the nametable select bits pointing at either $2800 or $2C00.

by on (#64920)
Oh oops, that's a good point, I forgot you have to rewrite to $2000 to put the correct nametable msbs in.

and yeah, I had a feeling that a random $2005 $2005 in the middle of a bunch of $2007s wouldn't mess up the PPU address while rendering was turned off.

I guess what I'm wondering, in regards to DMA-first or PPU-Stream-first: which one has worse side effects when it spills out of vblank? I know theoretically it should never happen, but I'm still curious.