Page 1 of 1

Screen moving involuntary

Posted: Tue Mar 24, 2020 2:18 pm
by Pennywise
ImageImageImage

I'm having an issue with the screen moving down when calling a menu in certain areas. I'm not entirely sure why this is happening, but I think it has something to do with the PPU register $2002. Through tracing the code, I've noticed this glitch happens when $2002 gets sets to #$00 and I don't know enough about the NES PPU to understand what exactly is happening. Would anyone have an idea of what the problem is?

Re: Screen moving involuntary

Posted: Tue Mar 24, 2020 2:24 pm
by Dwedit
Whenever you set the PPU address, you are also scrolling at the same time, since PPU address and PPU scroll use the same internal latches.
So always set scroll AFTER you do your PPU memory writes, and if you do PPU memory writes, always set scroll.

Re: Screen moving involuntary

Posted: Tue Mar 24, 2020 2:57 pm
by tokumaru
$2002 is a read-only register, so it doesn't really "get set" to anything. Like Dwedit said, any unintentional screen movement is most likely due to corruption of the scroll registers.

Scrolling on the NES is achieved via indirect manipulation of the VRAM addresses register (via $2000 and $2005) prior to the beginning of rendering, so when you change the VRAM addresses register directly (via $2006 and $2007) you're effectively messing up the scroll. ALWAYS set the scroll to the desired values once you're done with all PPU updates in your NMI handler, so that the new frame can begin rendering with the proper scroll values.

To properly set the scroll during vblank you need 3 PPU writes: one write to $2000 (lower 2 bits select the name table) and two writes to $2005 (first sets the X scroll, second sets the Y scroll).