News: 11 March 2016 - Forum Rules

Author Topic: SMB1 = Over 99% NMI Routine  (Read 4442 times)

Dr. Floppy

  • Restricted Access
  • Hero Member
  • *
  • Posts: 970
  • Make America GREAT Again!
    • View Profile
    • BaddestHacks.net
SMB1 = Over 99% NMI Routine
« on: October 25, 2015, 06:13:33 am »
99.99729%, to be exact. (And that doesn't count the tile screen layout data stored in CHR-ROM.)

The game starts up at $8000 and proceeds to an infinite loop at $8057. It's plucked out of this loop by the NMI Routine at V-Blank, wherein every other aspect of the game is finagled. Once the NMI Routine ends (usually around scanline 112), the RTI takes the game back to the infinite loop (thus negating the need for the usual AXY-juggling which bookends typical NMI Routines).

Is there any particular reason the OP's might have done it this way? Do any other NES games exhibit this odd arrangement?

henke37

  • Hero Member
  • *****
  • Posts: 643
    • View Profile
Re: SMB1 = Over 99% NMI Routine
« Reply #1 on: October 25, 2015, 06:17:52 am »
The most obvious fact is that they don't need to use any ram mirrors for any of the gpu registers, they can party without any restraints during vblank. It is also obviously a way to tie the main loop tick rate to the vblank rate.

Bregalad

  • Hero Member
  • *****
  • Posts: 2763
    • View Profile
Re: SMB1 = Over 99% NMI Routine
« Reply #2 on: October 25, 2015, 06:19:21 am »
Is there any particular reason the OP's might have done it this way?
None that I know off. It is a counter intuitive and awkward way of programming so I really don't see any real advantage of programming that way, nevertheless that's what they did.

Quote
Do any other NES games exhibit this odd arrangement?
Early Nintendo games and Konami games usually work that way (all in NMI). Other developers tend to either do the "All in Main" kind of programming (Squaresoft) or balance the work between Main and NMI (Capcom).

Nightcrawler

  • Hero Member
  • *****
  • Posts: 5792
    • View Profile
    • Nightcrawler's Translation Corporation
Re: SMB1 = Over 99% NMI Routine
« Reply #3 on: October 25, 2015, 10:07:48 am »
I think what you're seeing is a fundamental approach to game development. It's a fixed-step game loop rather than a variable-step game loop. So, rather than running the game loop as many times as possible with a varying amount of time elapsing between them, you run it only once per fixed interval of time. It can make many things about the game logic simpler. This approach can still be used on game's today. The XNA\Monogame frameworks have this option for example. If you use this approach, you are required that your game loop MUST complete in under the fixed interval of time, or you cannot use this method reliably. It's easy for these simpler games to meet that requirement.
TransCorp - Over 20 years of community dedication.
Dual Orb 2, Wozz, Emerald Dragon, Tenshi No Uta, Glory of Heracles IV SFC/SNES Translations

Bregalad

  • Hero Member
  • *****
  • Posts: 2763
    • View Profile
Re: SMB1 = Over 99% NMI Routine
« Reply #4 on: October 25, 2015, 05:53:25 pm »
If you go SMB style there's no really loop, rather a giant state machine, and the whole codes makes it go from one state to the next state.

NES games that does most of the work in the main thread does indeed have a game loop, but the amount of time in each iteration of the loop is typically one frame, so there is not what you mentioned (a varying amount of time elapsing between them). The NES is not technologically advanced enough to make such an approach practical.

In the end both approaches are equivalent and it would not be that hard to convert code from one approach to another (in other words, any game can theoretically be programmed with either of the approaches), however, it's still a fundamental difference in how this code is seen/perceived.

Nightcrawler

  • Hero Member
  • *****
  • Posts: 5792
    • View Profile
    • Nightcrawler's Translation Corporation
Re: SMB1 = Over 99% NMI Routine
« Reply #5 on: October 26, 2015, 06:15:59 pm »
NES games that does most of the work in the main thread does indeed have a game loop, but the amount of time in each iteration of the loop is typically one frame, so there is not what you mentioned (a varying amount of time elapsing between them). The NES is not technologically advanced enough to make such an approach practical.

I think it still may qualifies as far as the game's design goes. You can still use v-sync on modern games, and that doesn't discredit the main game loop's design pattern. Obviously, these games were designed well before established patterns emerged, so they won't fit perfectly.

It's at least similar in concept to what can still be used today. State machines are also a commonly used way to structure a game within a game loop. Either way, I don't see it as that odd of an approach to the game.
TransCorp - Over 20 years of community dedication.
Dual Orb 2, Wozz, Emerald Dragon, Tenshi No Uta, Glory of Heracles IV SFC/SNES Translations

oziphantom

  • Jr. Member
  • **
  • Posts: 32
    • View Profile
Re: SMB1 = Over 99% NMI Routine
« Reply #6 on: October 28, 2015, 04:02:37 am »
doing it all in the NMI just saves the main loop doing

LDA (VBLANK REG)
BNE -3
Same stuff here

but the issue is that it is NMI and not an IRQ, IRQ would be a lot safer, not sure why Nintendo linked it up the NMI pin and not the IRQ pin. As an NMI can re-enter itself so you MUST be done before it fires again... All the computers put it on an IRQ.

Dr. Floppy

  • Restricted Access
  • Hero Member
  • *
  • Posts: 970
  • Make America GREAT Again!
    • View Profile
    • BaddestHacks.net
Re: SMB1 = Over 99% NMI Routine
« Reply #7 on: October 28, 2015, 06:38:58 am »
As an NMI can re-enter itself so you MUST be done before it fires again... All the computers put it on an IRQ.

Makes sense. First thing the SMB1 NMI Routine does is clear d7 of $2000.

oziphantom

  • Jr. Member
  • **
  • Posts: 32
    • View Profile
Re: SMB1 = Over 99% NMI Routine
« Reply #8 on: October 30, 2015, 12:33:06 am »
Makes sense. First thing the SMB1 NMI Routine does is clear d7 of $2000.

So Nintendo made a maskable Non-Maskable Interupt genius. What does it use the IRQ to do that they could have used the built in 6502 system to do it?

Bregalad

  • Hero Member
  • *****
  • Posts: 2763
    • View Profile
Re: SMB1 = Over 99% NMI Routine
« Reply #9 on: October 30, 2015, 04:33:10 am »
It might have made more sense to use the IRQ however they didn't and by the time SMB was released it was way to late to make any changes to the hardware. It is perfectly safe to have re-entering NMI if they are programmed correctly, actually a lot of Konami games does exactly that, they do "everything in NMI" style but still have the game correctly run at a lower framerate, without the music slowing down nor the raster effects screwing up (SMB has both if the game lags).

IRQs were already reserved for both kind of APU IRQs, and cartridge IRQs. One of the kind of APU IRQs (the "Frame IRQ") is even more useless than any other hardware feature of the NES, and makes even less sense than anything else. It was only used in Door Door (J) and Dragon Quest (J) as a timebase to run the music engine, but it really makes no difference at all if you do it in the VBlank routine, and is a lot simpler/safer.

Bisqwit

  • Sr. Member
  • ****
  • Posts: 424
  • Polite, but politically incorrect.
    • View Profile
    • http://iki.fi/bisqwit/
Re: SMB1 = Over 99% NMI Routine
« Reply #10 on: November 18, 2015, 02:23:05 am »
The game starts up at $8000 and proceeds to an infinite loop at $8057.

In my experience, quite many NES games I have disassembled have been designed this way. The "main" loop is an infinite loop that does nothing, or at most it forever permutes the random number seed, and all of the game logic is done by the NMI.