Can NMI while you're inside the NMI handler?

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
mr_cannoli
Posts: 4
Joined: Mon Apr 27, 2020 5:30 pm
Location: Pacific Northwest

Can NMI while you're inside the NMI handler?

Post by mr_cannoli » Tue May 26, 2020 9:51 pm

I'm sure this is posted somewhere, but I wasn't able to find it.

I'm working on a small project where the main for my game is in NMI. It got me thinking, is it possible if your NMI runs too many instructions, could the NMI interrupt again while you're already executing code?

If so would the remedy just to disable and re-enable NMI every time you enter the NMI loop?

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

Re: Can NMI while you're inside the NMI handler?

Post by lidnariq » Tue May 26, 2020 9:57 pm

mr_cannoli wrote:
Tue May 26, 2020 9:51 pm
I'm working on a small project where the main for my game is in NMI. It got me thinking, is it possible if your NMI runs too many instructions, could the NMI interrupt again while you're already executing code?
Yes.
If so would the remedy just to disable and re-enable NMI every time you enter the NMI loop?
Yes, but the 2C02 has a bug that requires working around if you use this method. See our wiki

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

Re: Can NMI while you're inside the NMI handler?

Post by tokumaru » Tue May 26, 2020 9:59 pm

mr_cannoli wrote:
Tue May 26, 2020 9:51 pm
I'm working on a small project where the main for my game is in NMI. It got me thinking, is it possible if your NMI runs too many instructions, could the NMI interrupt again while you're already executing code?
Yes.
If so would the remedy just to disable and re-enable NMI every time you enter the NMI loop?
Some games do that, but unfortunately, there's a bug in the PPU that causes a graphical glitch if a write to $2000 happens at certain times. A better solution would be to control this entirely in software, using a flag that indicates whether the frame calculations are over or not. This also gives you the option to carry out tasks that need to be done even during lag frames, such as playing music (otherwise the music will slow down with the gameplay) or splitting the screen for a status bar (otherwise status bars timed with sprite 0 hits at the top of the screen will glitch), before returning and letting the previous instance of the NMI handler finish its job.

mr_cannoli
Posts: 4
Joined: Mon Apr 27, 2020 5:30 pm
Location: Pacific Northwest

Re: Can NMI while you're inside the NMI handler?

Post by mr_cannoli » Tue May 26, 2020 11:02 pm

Some games do that, but unfortunately, there's a bug in the PPU that causes a graphical glitch if a write to $2000 happens at certain times.
Fascinating read, thanks guys!

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

Re: Can NMI while you're inside the NMI handler?

Post by rainwarrior » Wed May 27, 2020 2:44 pm

Yeah, in more or less all my projects, immediately after entering NMI, I set some variable that indicates the NMI should skip directly to the RTI in case of a self-interrupted NMI routine, so it never tries to do any work if more than 1 deep.

In practice, it never actually comes up for me because my NMI handler should always run in a limited amount of time, but I still like to have it there for safety's sake. In case of an unexpected bug, I'd rather the game be able to keep running rather than hang/crash.

Post Reply