What are NMI, IRQ and RESET?

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

Post Reply
User avatar
Ian Malcolm
Posts: 5
Joined: Mon Jan 02, 2023 1:13 pm

What are NMI, IRQ and RESET?

Post by Ian Malcolm »

Hi, I'm here to ask the most primitive questions on NES development, I feel lost because my knowledge bits are scattered and can't wire them together.

As a side note, I don't have a problem understanding 6502 assembly (at least for now), my problem essentially is how the hardware works and how to organize code the wright way.
-------------------
So, Can you please explain to me what are interrupts and what is their order in the .asm file, should they be in the top or the end of file or where exactly?

Please explain to me in the simplest way possible, as you would do to a little chicken trying to understand what interrupts are?

I'll be asking other questions later because I'm determined to learn (yes I searched a lot but only getting confused, this forum is my last hope)

Thank you
Garth
Posts: 246
Joined: Wed Nov 30, 2016 4:45 pm
Location: Southern California
Contact:

Re: What are NMI, IRQ and RESET?

Post by Garth »

You can put the routines anywhere in the memory map, except that at least the reset routine will normally have to be in ROM, since it has to run before the processor has had a chance to load any code into RAM.  The three vectors do need to be in the last six bytes of the memory map, but they can point to the RST, IRQ, and NMI routines wherever you choose to put them.  I have a (non-NES-specific) 6502 interrupts primer at http://wilsonminesco.com/6502interrupts/ that explains everything.  It's one of the very first articles I ever wrote, years ago, as evidenced by my outdated cartoons which hopefully will make it more entertaining.
http://WilsonMinesCo.com/ lots of 6502 resources
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: What are NMI, IRQ and RESET?

Post by tokumaru »

Each interrupt handler is essentially a piece of code containing logic to deal with an event triggered by an external source. They can be placed anywhere in memory, which is why the 6502 requires a small table called the interrupt vector table at the very end of the memory indicating where each of them is.

When the CPU is reset, if grabs the address of the reset handler and jumps to that location. The code contained there is supposed to initialize the system and prepare everything that the program/game needs in order to function. Even though "reset" is technically an interrupt, it's not actually interrupting anything, in fact, it's the very first part of the program to run, so you don't need to return from it, you can simply carry on with the rest of the program once initialization is over.

The purpose of the NMI and IRQ interrupts vary from system to system, since each machine connects external hardware to the CPU differently. The main difference between them is that the program running on the CPU can choose to enable or disable IRQs via the I flag, while NMIs can never be disabled (by the CPU itself, that is). Once one of these fires, the CPU will stop whatever it's doing (i.e. be interrupted) and jump to the address that handles the interrupt in question. Once the handler is done, it returns so the CPU can go back to doing whatever it was doing before.

On the NES, the NMI line is controlled exclusively by the PPU (i.e. the graphics chip). It can optionally trigger an NMI every time that the vertical blank period starts. This is useful because this is the period when the program can update the video (e.g. sprites, backgrounds, palettes, etc.), so it's very convenient for the program to be "notified" that it has started. Also, since vblank happens at a steady rate (60Hz on NTSC, 50Hz on PAL), NMIs are often used to keep track of time.

The IRQ line is connected to the APU, and is also available to cartridges, meaning that cartridges are free to trigger IRQs as they see fit. It's most commonly used by scanline counters, which "notify" the program that the PPU is drawing specific scanlines, so the program can start special effects on that line.
unregistered
Posts: 1318
Joined: Thu Apr 23, 2009 11:21 pm
Location: cypress, texas

Re: What are NMI, IRQ and RESET?

Post by unregistered »

acronyms:
NMI == Non-Maskable Interrupt
IRQ == Interrupt ReQuest


As tokumaru said, NMI is vblank. It can be enabled/disabled by changing a certain bit in a PPU address.

Vblank is an excellent opportunity to adjust the PPU as it’s dormant then; but, vblank doesn’t last for a long time (it seems quite long when starting out, but as your game grows bigger, the vblank cycle amount seems much smaller).

My game still doesn’t use an IRQ. It does a lot though, but IRQs aren’t necessary to learn when you are starting out, imo. :)
Post Reply