This page is a mirror of Tepples' nesdev forum mirror (URL TBD).
Last updated on Oct-18-2019 Download

Help Separating Game Logic from NMI

Help Separating Game Logic from NMI
by on (#222461)
hello,
This past week, I decided to take Disch's advice and try separating my game logic from my NMI/Drawing code. The problem is, when I try this, the code in the game loop doesn't seem to execute.

Essentially, my problem is this -- when I put code in the main game loop (which, in the working version, just loops forever while we wait for an NMI, and the NMI handles all of the game code like in Super Mario Bros), none of this code actually executes. Specifically, controllers aren't read, the game engine doesn't do anything, and test tones don't play. The screen still scrolls, but that happens during NMI.

Here is a link to the main file on pastebin. The sound engine is empty right now and the reset file has basic initialization routines that I haven't changed in moving the logic code to a separate loop. The only really relevant code in reset.asm is

lda #$00
sta sleep_flag
sta draw_flag
so that sleep_flag and draw_flag are both initially not set.

When the main game loop has content besides an unconditional jump to create a forever loop, that code doesn't appear to execute. If I put in a test tone, like so:

LDA #%00111000
STA $4000
LDA #C2
ASL A
TAY
LDA note_table, y
STA SQ1_LOW
LDA note_table+1, y
STA SQ1_HIGH
that code also does not execute -- I hear no tone, even though the same code works inside the NMI.

Any ideas as to what could cause this? I'm at a loss...I'm also a pretty new 6502 programmer. What I find really odd is that the code works perfectly fine if

jsr ReadController
jsr GameEngine
are in the NMI. However, Disch recommends putting this logic outside the NMI, which makes sense. I would like to figure out what's going on so I don't encounter this same problem in the future.

Thank you so much for your help!
Re: Help Separating Game Logic from NMI
by on (#222464)
Looks like you forgot the pastebin link, so there's not much I can say, except that this is very similar to this other post that was made recently: viewtopic.php?f=2&t=17555
Re: Help Separating Game Logic from NMI
by on (#222475)
You really need to get used to using a debugger. Even if people can look at your code and guess the issue, this is a perfect example of a problem that can easily be solved using a debugger, something that I feel is absolutely necessary in any kind of development if you want to solve your problems, rather than just dig around blindly for days, using abstract methods such as playing a test tone.
FCEUX is the typical go-to for debugging, but I'd recommend Mesen as it has an extremely user friendly interface, and a ton of useful tools.

For an issue like this where it seems like the game never enters the main loop, I'd just put a breakpoint at the reset vector, and step through all the code, seeing where it goes, to find the point where it doesn't go to your main loop, or whereever you expected it to.
Or maybe start out with a breakpoint slightly further in, such as the start of the loop, to see if it does enter it, but simply refuses to loop back after NMI has been executed. And if it never hits that breakpoint, place one slightly further back.

Right now, as I haven't seen your code, my best guess is that either your sleep flag never gets unset (I assume the main loop sets it when it reaches the end, and loops until it's reset in NMI?), or something causes your code to never reach the main game logic in the first place.
Re: Help Separating Game Logic from NMI
by on (#222531)
The post appears to be copy-pasted from this 4-month-old thread on Reddit. Signature neutralized (again).
Re: Help Separating Game Logic from NMI
by on (#222539)
ouch! This one seemed legit at first (but out of nowhere) but now that I talked with Koitsu on the subject then it would ends in my suspicious list. We will have to be careful about new users posts from now.
Re: Help Separating Game Logic from NMI
by on (#222550)
Is there a particular reason for keeping these threads open (or even keeping them at all) after they're determined to be spam?
Re: Help Separating Game Logic from NMI
by on (#222565)
Well then.
I hope the spambot appreciated my help :|