Page 1 of 1

Saving A, X and Y in an interrupt: With PHP?

Posted: Sat Dec 30, 2017 1:01 pm
by DRW
During an interrupt, I do this:

Code: Select all

PHA
TXA
PHA
TYA
PHA

; Regular code.
; ...

PLA
TAY
PLA
TAX
PLA
Now I've seen an example where the last command before the regular code is PHP and the first command after the regular code is PLP (push and pull procssor status).

Is this necessary? Why? Why not?

Re: Saving A, X and Y in an interrupt: With PHP?

Posted: Sat Dec 30, 2017 1:12 pm
by pubby
Interrupts automatically push the status flags to the stack and RTI automatically pops them.

So no, you don't need to use PHP/PLP. BRK/RTI do the same plus more.

Re: Saving A, X and Y in an interrupt: With PHP?

Posted: Sat Dec 30, 2017 2:26 pm
by DRW
Thanks. That's good because I didn't do it in my previous game.

Re: Saving A, X and Y in an interrupt: With PHP?

Posted: Sat Dec 30, 2017 2:31 pm
by Dwedit
it's common to see PHP/PLP because not everyone who codes 6502 knows that interrupts and RTI instructions push and pop the flags.

Re: Saving A, X and Y in an interrupt: With PHP?

Posted: Sat Dec 30, 2017 4:38 pm
by Quietust
Even if you did do PHP/PLP, you'd want to do PHP first and PLP last so that the Z and X flags wouldn't get modified by the other instructions (TXA, TAX, TAY, TYA, and PLA all modify those flags).