Page 1 of 1

Can NMI while you're inside the NMI handler?

Posted: Tue May 26, 2020 9:51 pm
by mr_cannoli
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?

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

Posted: Tue May 26, 2020 9:57 pm
by lidnariq
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

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

Posted: Tue May 26, 2020 9:59 pm
by tokumaru
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.

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

Posted: Tue May 26, 2020 11:02 pm
by mr_cannoli
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!

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

Posted: Wed May 27, 2020 2:44 pm
by rainwarrior
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.