Reading from $2002 resets bit 7... but from which register?

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
Reading from $2002 resets bit 7... but from which register?
by on (#46601)
From itself... (vblank)?

ps: here I also reset the flipflop to addresses.
Re: Reading from $2002 resets bit 7... but from which regist
by on (#46602)
dreampeppers99 wrote:
From itself... (vblank)?

Yes. Reading PPUSTATUS ($2002) acknowledges the NMI request, which turns off bit 7 of PPUSTATUS. If you read PPUSTATUS twice in a row like this:
Code:
  lda PPUSTATUS
  and PPUSTATUS

you'll virtually always end up with bit 7 of A (and the N flag) set to 0. There are four possibilities, three of which can happen:
  • false, false: most of the time
  • true, false: first read after vblank acknowledges the NMI; second reads that it has been acknowledged
  • false, true: vblank started while the CPU was fetching the second instruction
  • true, true: no possible way that I can think of, except an interrupt handler executing between the instructions, taking tens of thousands of cycles (longer than one frame) to execute, and not acknowledging NMI
Re: Reading from $2002 resets bit 7... but from which regist
by on (#46603)
dreampeppers99 wrote:
From itself... (vblank)?

Yup. Bit 7 of $2002 becomes 0 everytime $2002 is read.

Quote:
ps: here I also reset the flipflop to addresses.

Yes, which means the next write to $2005 or $2006 will be the first one.

by on (#46615)
"Yup. Bit 7 of $2002 becomes 0 everytime $2002 is read. "
But if the nes was still on VBlank period... it reset this state?
After read or before? I mean ... the value I get from will ever with bit 7 = 0?

I always thougth that this status would be util to know when the system was on the VBlank period... :?

thank you all

by on (#46616)
Nope; after $2002 gives you its current value, the flag is internally cleared. As you gather, that's less useful than if reading it didn't disturb its value.

by on (#46631)
dreampeppers99 wrote:
I always thougth that this status would be util to know when the system was on the VBlank period... :?

Sadly, it doesn't work like that. This would make detecting the end of Vblank much easier for NES programmers.