Help Separating Game Logic from NMI

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems. See the NESdev wiki for more information.

Moderator: Moderators

Post Reply
williamgomes
Posts: 1
Joined: Tue Jul 31, 2018 9:55 pm

Help Separating Game Logic from NMI

Post by williamgomes »

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!
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Help Separating Game Logic from NMI

Post by tokumaru »

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: https://forums.nesdev.com/viewtopic.php?f=2&t=17555
User avatar
Sumez
Posts: 919
Joined: Thu Sep 15, 2016 6:29 am
Location: Denmark (PAL)

Re: Help Separating Game Logic from NMI

Post by Sumez »

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.
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Help Separating Game Logic from NMI

Post by tepples »

The post appears to be copy-pasted from this 4-month-old thread on Reddit. Signature neutralized (again).
User avatar
Banshaku
Posts: 2417
Joined: Tue Jun 24, 2008 8:38 pm
Location: Japan
Contact:

Re: Help Separating Game Logic from NMI

Post by Banshaku »

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.
Revenant
Posts: 462
Joined: Sat Apr 25, 2015 1:47 pm
Location: FL

Re: Help Separating Game Logic from NMI

Post by Revenant »

Is there a particular reason for keeping these threads open (or even keeping them at all) after they're determined to be spam?
User avatar
Sumez
Posts: 919
Joined: Thu Sep 15, 2016 6:29 am
Location: Denmark (PAL)

Re: Help Separating Game Logic from NMI

Post by Sumez »

Well then.
I hope the spambot appreciated my help :|
Post Reply