Do 2002.7 = 1 and NMI occur at the same time?

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

Post Reply
User avatar
Petruza
Posts: 311
Joined: Mon Dec 22, 2008 10:45 pm
Location: Argentina

Do 2002.7 = 1 and NMI occur at the same time?

Post by Petruza » Sun May 24, 2015 8:31 am

Is it correct to wait for 262x341* PPU cycles have passed and then set 2002.7 to 1 and immediately start NMI?
Should one of them start earlier? how much?

*: I'm not accounting for the 340 cycle scanline odd frames yet, and this is NTSC only of course.

User avatar
Dwedit
Posts: 4333
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Re: Do 2002.7 = 1 and NMI occur at the same time?

Post by Dwedit » Sun May 24, 2015 12:34 pm

I think they happen at the same time. But note that your typical read instruction does the read at the last cycle, so it's possible to begin a read instruction before NMI happens. The read instruction will read the vblank flag before the NMI happens, and that would cancel the NMI from ever happening.
Also, if the read happens on the exact cycle when NMI is set, it will read back 0 for that bit, and cancel NMI as well.

In PocketNES, I used a hack so that any pollable flag will be set 4 CPU cycles before the event happens, so the read instruction will see the flag change before the event happens.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!

User avatar
Petruza
Posts: 311
Joined: Mon Dec 22, 2008 10:45 pm
Location: Argentina

Re: Do 2002.7 = 1 and NMI occur at the same time?

Post by Petruza » Mon May 25, 2015 10:09 am

Oh, so this might happen very frequently, as the main game loop usually enters a loop { LDA $2002; BPL } to wait for the next frame, right?
PS: Do you have an emulator written in other than Assembler?

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

Re: Do 2002.7 = 1 and NMI occur at the same time?

Post by tokumaru » Mon May 25, 2015 10:40 am

Petruza wrote:Oh, so this might happen very frequently, as the main game loop usually enters a loop { LDA $2002; BPL } to wait for the next frame, right?
This is what early homebrew programs did, before we found out about this issue. In order to reliably detect VBlank, you have to use the NMI.

User avatar
Anes
Posts: 605
Joined: Tue Dec 21, 2004 8:35 pm
Location: Mendoza, Argentina

Re: Do 2002.7 = 1 and NMI occur at the same time?

Post by Anes » Tue May 26, 2015 1:15 am

I don't know why, but im my emu i do the following "hack" to pass Blargg's test: at cc 1 of vblank i set the flag then at cc 3 check if vblank flag is set and if nmi_on_vblank is set and set a preliminay "ppu_nmi_pending" flag. Now if this ppu_nmi_pending flag is set i delay to cc 15 to set the "nmi_pending" flag on the cpu. This throw me an error in 1942 that leaves screen letters unredeable. And at the same way if i trigger the nmi the same time the flag is set Bomberman hangs in its init screen.
ANes

User avatar
Petruza
Posts: 311
Joined: Mon Dec 22, 2008 10:45 pm
Location: Argentina

Re: Do 2002.7 = 1 and NMI occur at the same time?

Post by Petruza » Tue May 26, 2015 6:45 pm

tokumaru wrote:This is what early homebrew programs did, before we found out about this issue. In order to reliably detect VBlank, you have to use the NMI.
Oh ok, so they set some flag in RAM from the NMI subroutine and then check for that instead of 2002.7? because the main game loop has to know where to stop and loop again.

User avatar
koitsu
Posts: 4218
Joined: Sun Sep 19, 2004 9:28 pm
Location: A world gone mad

Re: Do 2002.7 = 1 and NMI occur at the same time?

Post by koitsu » Tue May 26, 2015 6:46 pm

That's correct.

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

Re: Do 2002.7 = 1 and NMI occur at the same time?

Post by tepples » Tue May 26, 2015 6:47 pm

so they set some flag in RAM from the NMI subroutine and then check for that instead of 2002.7?
NMI#Race condition describes how this works.

Post Reply