This page is a mirror of Tepples' nesdev forum mirror (URL TBD).
Last updated on Oct-18-2019 Download

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

Saving A, X and Y in an interrupt: With PHP?
by on (#210992)
During an interrupt, I do this:

Code:
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?
by on (#210994)
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?
by on (#211000)
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?
by on (#211001)
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?
by on (#211015)
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).