Question about interrupt

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
sunny
Posts: 8
Joined: Wed Nov 17, 2004 1:31 am
Location: Hong Kong

Question about interrupt

Post by sunny »

Hi, I would like to ask something about interrupt...

I would like to know how will the 6502 handle following case:
1. a subroutine is just called from main loop...
2. a timer interrupt is triggerred during the mid-way of the subroutine described in (1).

How will the 6502 handle the case? Will it push all the register and halt the subroutine, and then call the interrupt subroutine?
or, the interrupt subroutine will run until the subroutine running is finished?

thanks very much

Regards,
Sunny
User avatar
Quietust
Posts: 1920
Joined: Sun Sep 19, 2004 10:59 pm
Contact:

Re: Question about interrupt

Post by Quietust »

Interrupts tell the processor to stop what it is doing and IMMEDIATELY (well, as soon as it finishes the current instruction) jump to the interrupt handler, pushing the return address and flags in the process. Other registers (i.e. A/X/Y) need to be pushed/popped manually in the interrupt routine. Interrupt routines are terminated using the RTI instruction, at which point the CPU will resume execution right where it left off.
Quietust, QMT Productions
P.S. If you don't get this note, let me know and I'll write you another.
User avatar
Memblers
Site Admin
Posts: 4044
Joined: Mon Sep 20, 2004 6:04 am
Location: Indianapolis
Contact:

Post by Memblers »

It will call the interrupt routine immediately (as soon as the current instruction finishes). It will automatically save and restore the CPU's status flags (no need for PHP/PLP instructions), everything else needs to be pushed/pulled by your code.

So if you want to use A,X,Y in your IRQ routine, you'll do this to preserve it:

Code: Select all

irq_start:
 pha
 txa
 pha
 tya
 pha

;insert IRQ or NMI code here

 pla
 tay
 pla
 tax
 pla
 rti
sunny
Posts: 8
Joined: Wed Nov 17, 2004 1:31 am
Location: Hong Kong

Thank you very much

Post by sunny »

Thank you all,

I am now totally understand ^_^

Regards,
Sunny
Post Reply