Why does NESICIDE's Hello World wait twice for PPU boot-up?

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

Post Reply
Nukatha
Posts: 3
Joined: Sat Aug 24, 2019 9:42 am

Why does NESICIDE's Hello World wait twice for PPU boot-up?

Post by Nukatha » Sat Aug 24, 2019 11:19 am

I have a few very basic questions about the Hello World program that comes with NESICIDE 2.

First, in .proc reset, there is a brief loop:

Code: Select all

vwait1:
bit PPUSTATUS
bpl vwait1

Which is then followed by initializing a few values.
Then we get to

Code: Select all

vwait2:
bit PPUSTATUS
bpl vwait2
Why is vwait1 even included in the code?
It appears to me that this program loops the 'vwait1' portion until the PPU is ready, then sets a few values, then does 'wait2', but by that time the PPU is already warmed up.
If I comment out vwait1, (all three lines mentioned above) the compiled program appears to run just as well.
The basic process is then: Power on console. Initialize a few values, then wait for PPU before proceeding.

User avatar
rainwarrior
Posts: 7804
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Why does NESICIDE's Hello World wait twice for PPU boot-

Post by rainwarrior » Sat Aug 24, 2019 1:02 pm

Nukatha wrote:If I comment out vwait1, (all three lines mentioned above) the compiled program appears to run just as well.
Test it on hardware and you'll find out the difference. Emulators don't tend to cover this issue.

The PPU takes a little more than 1 full frame to initialize itself after reset. You need to wait at least that long before using it.

https://wiki.nesdev.com/w/index.php/PPU_power_up_state

Blargg's testing notes show a good example: viewtopic.php?f=2&t=3958

It's not just NESICIDE it's pretty much every NES program ever written. Though I've seen a few games get by with just "doing a bunch of stuff" before using the PPU rather than explicitly waiting like this. There's really very little reason to do it any other way besides the vblank waits, though. Two wait loops on $2002 is safe and effective and small code, easy to implement and not very prone to error. (Make sure to read $2002 once before the first wait loop though.)

A lot of people like to stick a standard memory clearing routine in between the two waits so that some work is being done during the wait, though really it doesn't matter much. Unless you're a robot one extra frame after reset isn't a significant thing.

There's some example "standard" initialization here: Wiki: Init code
Last edited by rainwarrior on Sat Aug 24, 2019 2:08 pm, edited 1 time in total.

lidnariq
Posts: 9319
Joined: Sun Apr 13, 2008 11:12 am
Location: Seattle

Re: Why does NESICIDE's Hello World wait twice for PPU boot-

Post by lidnariq » Sat Aug 24, 2019 2:08 pm

To very quickly summarize everything:
* The PPU is only ready after 262 (or 312 on PAL) scanlines have passed
* The bit in PPUSTATUS becomes true after only 240 scanlines have passed, so it's not sufficient to determine whether the PPU is ready
* The bit in PPUSTATUS may be incorrectly be set after a warm reboot.

The amount of time (and bytes of code) taken to wait for an extra vblank is so small and rare that it's not worth optimizing, and getting it wrong could cause glitchy results.

User avatar
tokumaru
Posts: 11648
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Why does NESICIDE's Hello World wait twice for PPU boot-

Post by tokumaru » Sat Aug 24, 2019 2:25 pm

rainwarrior wrote:Test it on hardware and you'll find out the difference.
Do note that "on hardware" in this context excludes Flash carts with a menu, such as the PowerPak or the Everdrive, because those only call your program after the system is fully initialized.

User avatar
rainwarrior
Posts: 7804
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Why does NESICIDE's Hello World wait twice for PPU boot-

Post by rainwarrior » Sat Aug 24, 2019 2:49 pm

tokumaru wrote:
rainwarrior wrote:Test it on hardware and you'll find out the difference.
Do note that "on hardware" in this context excludes Flash carts with a menu, such as the PowerPak or the Everdrive, because those only call your program after the system is fully initialized.
Actually, the PPU warmup state is triggered by reset, so you can test the problem with these on an NES. (Famicom does not reset its PPU so you can't do it there.)

Power-on state is still inaccessible but reset is enough to test this.

tepples
Posts: 21949
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Why does NESICIDE's Hello World wait twice for PPU boot-

Post by tepples » Sat Aug 24, 2019 3:43 pm

rainwarrior wrote:Unless you're a robot one extra frame after reset isn't a significant thing.
And even if you are a robot, you probably wait for the first pulse on $4016 D0 before you start replaying inputs.
rainwarrior wrote:Actually, the PPU warmup state is triggered by reset, so you can test the problem with these on an NES. (Famicom does not reset its PPU so you can't do it there.)
But only on a front-loading NES. Like the Famicom, the top-loading NES doesn't reset the PPU.

Nukatha
Posts: 3
Joined: Sat Aug 24, 2019 9:42 am

Re: Why does NESICIDE's Hello World wait twice for PPU boot-

Post by Nukatha » Sat Aug 24, 2019 8:51 pm

Many thanks to each of you.

Post Reply