home   |   primer index   |   1. Intro: Why 6502?   |   2. addr decode   |   3. mem map req.s   |   4. IRQ/NMI conx   |   5. 74 families & timing   |   6. clk gen   |   7. RST   |   8. mystery pins   |   9. AC performance construction   |   10. exp bus & interfaces   |   11. get more on a board   |   12. WW Q&A   |   13. custom PCBs   |   14. I/O ICs   |   15. displays   |   16. getting 65xx parts   |   17. project steps   |   18. program-writing   |   19. debugging   |   20. pgm tips   |   21. workbench equip   |   22. circuit potpourri


6502 PRIMER: Building your own 6502 computer


Interrupt Connections: IRQ and NMI

And since we mentioned interrupts in the previous chapter-- The two interrupt inputs on the 6502 are IRQ and NMI (Interrupt ReQuest and Non-Maskable Interrupt).  A low level on the IRQ or a falling edge on NMI input interrupts the normal program flow to take care of a special need, whether it's a byte that just arrived at the UART, a timer that has timed out indicating it's time to do something, or other external stimulus requiring immediate action has arrived.  If you couldn't use interrupts to do that, the program would have to keep checking to see if something needed service, and may not check at the right times to respond quickly enough.  The interrupt is analogous to a doorbell when there's a possibility someone will come over.  Without the doorbell (or knock), you would have to keep going to the door to see if someone is there.  But with it, you can go about your business efficiently and not waste time checking the door when no one is there yet.

Interrupts on the IRQ input can be masked if the main program is doing something that you don't want interrupted right then for some reason.  SEI SEts the Interrupt-disable bit (I) in the processor status register (P) so the IRQ pin will be ignored.  When you're ready for interrupts again, use CLI to CLear the Interrupt-disable bit.

Most interrupts would come in on the IRQ input.  Many of the 65-family I/O ICs have open-drain interrupt outputs so they can all go to the microprocessor by the same wire.  This is a "wire-OR" logic and you'll need a pull-up resistor to bring the IRQ line high (meaning false) when nothing is pulling it down.  3KΩ is usually recommended in the data books.  3.3K is a standard 5% (and even 10%) value and is close enough. 

For programming, note that there's a rather slow rise time with the pull-up resistor and the small but inevitable capacitance on the line, so it is good to clear the IC's interrupt condition early in your interrupt-service routine (ISR) to make sure the line has had time to float up before you reach the RTI and the processor status register is restored with the interrupt-disable bit clear.  At low clock speeds you won't have trouble; but turn it up later and it can be a problem.  I've been bit by it.

To remedy this, WDC's W65C22S's (note the "S" on the end) IRQ output is not open-drain, but a standard totem-pole output.  This makes the rising edge much faster, which becomes important at the higher clock speeds.  The down side to having the totem-pole IRQ outputs on WDC's W65C22S is that you need an AND-gate arrangement to connect two or more IRQ sources to the 6502's single IRQ input. 

An AND-gate substitute would be to put a Shottky diode in series with each WDC W65C22S's IRQ output, with the cathode toward the WDC W65C22S, and connect the anodes together to the IRQ input on the processor with a single 3.3K pull-up resistor.  This goes back to the rising-edge speed problem though; so I don't recommend it for the higher clock speeds of 5-10MHz and up.

It would have to be a very high priority to justify bringing an interrupt request in on the NMI input, and even then you normally would only have one source that can produce a falling edge on that input.  Since it's non-maskable, multiple edges in quick succession could produce a situation where interrupts are interrupting already-started ISRs before they're done.  If it's not intentional and under control, it could be a problem.

The only thing I have used NMI for regularly is a software time-of-day clock, where a counter in a 65c22 VIA produces an interrupt 100 times per second.  I don't normally need it for telling what time of day it is, but it gets used to time things like key debounce, delay before key repeat, key repeat rate, and scheduled jobs without resorting to delay loops that would keep the processor from being able to do other things while waiting.  A few jobs have required telling the 65c22 to stop the time-of-day clock interrupts on NMI so I could get better response time on the IRQ from another source without the jitter.  (These have used up to 125,000 interrupts per second on the IRQ input.)  I also have an "Abort" button and de-bounce circuit connected to the CA1 input of the same VIA, to get control back from a crashed program.  It's similar to pushing the Reset button, but less drastic, as it does not reset all the hardware, only make the processor get out of the crashed program and start over.

Many designers will tell you the NMI should be reserved for warning of imminent power failure.  However in most systems, the fraction of a second that the power supply takes to ramp down (if it matters at all) isn't enough time to do anything significant like store data to disc or even to a flash memory.  The non-maskable interrupt doesn't have to be something terrible, analogous to the house being on fire.  It could also be for something good, good enough to drop everything else!

If you're not sure which 6502 interrupt input you want a peripheral IC connected to, you might use a pin header to make it selectable later by putting the shorting jumper on the appropriate pair of pins.  A DIP switch could be used, but takes more room.  Again-- just remember that you'll have to take extra precautions if you have more than one source at a time connected to the NMI since it is edge-triggered and not level-sensitive.

If you don't need to connect anything to the processor's interrupt input pins yet, just connect each one to Vcc through the 3.3K resistor.

I have a much more complete primer on interrupts here.  Enjoy my outdated cartoons!


memory map requirements <--Previous   |   Next--> 74 families & timing

last updated Feb 28, 2015