battagline wrote:
So one thing I'm unclear on, is what happens when the NMI returns. Does it go back to where you were in the gameloop before?
Not specifically. It will return where the PC (program counter, the address of the last executed instruction) was before the NMI was executed. Which means, even though you check the flag to see in you can execute NMI, checking the flag overwrite A, which mean when you do RTI and return to the location of the previous instruction, if that code was doing some manipulation on A, it will have the content of the NMI flag instead and "bad things will happens"
So any register you overwrite must be backed up and restored and the end if NMI/Interrupt. Usually you push them on the stack end restore them at the end.
battagline wrote:
Because I wasn't sure, what I did was set a flag nmi_ready or something like that, that only lets the nmi execute if the gameloop has finished. Then I have something similar in gameloop. The intent is basically so that we alternate between gameloop and nmi no matter what. I have no idea if this is good practice, it's just what I came up with.
there is many gameloop type, it depends what your game do. I prefer that the main gameloop process data/input, buffer it then in the NMI if any data is ready it will push it to OAM, PPU etc. I usually put famitone at the end of NMI too, that way if your main gameloop slow down, the music is not affected. Since you game seem to not do heavy processing (I checked really, really fast ^^;;) maybe this is not an issue in your case.
In brief, there is no "best" way, it depends on your need.