Which flag bits are set by the PLP instruction on 6502?

Building your first 6502-based project? We'll help you get started here.
Post Reply
randomkiwi
Posts: 5
Joined: 26 Oct 2023

Which flag bits are set by the PLP instruction on 6502?

Post by randomkiwi »

I was going through the instruction set of 6502 at https://www.masswerk.at/6502/6502_instr ... t.html#PLA . Now below the PLP (Pull Processor Status from Stack) instruction, it is stated that
Quote:
The status register will be pulled with the break flag and bit 5 ignored.
As far PLA instruction is concerned, it is clear that status bit N and Z will be affected by the operation, but I am unable to understand which bits will be affected by the PLP instruction.
gfoot
Posts: 871
Joined: 09 Jul 2021

Re: Which flag bits are set by the PLP instruction on 6502?

Post by gfoot »

The status register only really has 6 bits - C, D, I, N, V, Z - and they are all affected by pulling it from the stack, each being set to the value of one of the bits in the value pulled. The values of bits 4 and 5 in the byte from the stack are discarded.
User avatar
BigEd
Posts: 11463
Joined: 11 Dec 2008
Location: England
Contact:

Re: Which flag bits are set by the PLP instruction on 6502?

Post by BigEd »

PLP brings in six bits as literals from the byte on the stack: NV—DIZC
The other two bits of the byte have no effect.

(By way of illustration, you wouldn’t normally see both Z and N set, but you could set them both with PLP)

Edit oops crossed in post!
User avatar
BigDumbDinosaur
Posts: 9425
Joined: 28 May 2009
Location: Midwestern USA (JB Pritzker’s dystopia)
Contact:

Re: Which flag bits are set by the PLP instruction on 6502?

Post by BigDumbDinosaur »

randomkiwi wrote:
I was going through the instruction set of 6502 at https://www.masswerk.at/6502/6502_instr ... t.html#PLA . Now below the PLP (Pull Processor Status from Stack) instruction, it is stated that
Quote:
The status register will be pulled with the break flag and bit 5 ignored.
As far PLA instruction is concerned, it is clear that status bit N and Z will be affected by the operation, but I am unable to understand which bits will be affected by the PLP instruction.

As the others noted, bits 4 and 5 are not “real” bits in the 6502/65C02—and in the 65C816 when the latter is operated in emulation mode.  Bit 4 only exists in the stack copy of the status register (SR) that is pushed during a response to an IRQ or as part of executing a BRK (software interrupt) instruction.  If an IRQ is being processed, bit 4 will be clear, otherwise bit 4 will be set.  This behavior enables the IRQ/BRK service routine to distinguish between the two interrupt types.

When SR is pulled from the stack with a PLP instruction, bits 4 and 5 will not be affected by whatever is on the stack.  The sequence PHP - PLA will result in bits 4 and 5 always being set in the accumulator copy of SR.

For detailed information on the 65xx instruction set, get yourself a copy of “Programming the 65816, Including the 6502, 65C02, and 65802” by Eyes & Lichty.  I usually caution people about getting this kind of information from arbitrary websites, as such sites may propagate errors posted on other sites.  The Eyes & Lichty manual may be considered authoritative for all permutations of the 6502.
x86?  We ain't got no x86.  We don't NEED no stinking x86!
Post Reply