| |
Sasq
Registered: Apr 2004 Posts: 157 |
Z and N flags ever set together?
These flags are always set together, and it seems the only way to set both flags is to modify the SR on the stack and use RTI ?
Is there any conceivable use case for this?
The reason I am asking is that an optimization in my emulator currently depends on N and Z never being set together.
(I know there are 650c2 opcodes that only set the Z flag, but that's a separate problem). |
|
| |
Krill
Registered: Apr 2002 Posts: 2997 |
The only way to set Z and N independently (such that the "result" can be negative-zero) seems indeed to be PLP/RTI.
Use case... maybe returning additional 4-bit state via the flags (N V Z C) from a subroutine, or, well... a negative zero for float arithmetic? =) |
| |
trident
Registered: May 2002 Posts: 96 |
i think the BIT instruction can also be used to set the N and Z flags somewhat independently of each other, but i've never really used the BIT instruction for anything, except burning cycles, so i'm not 100% certain.
BIT supposedly takes the 7th bit of the operand to be the N flag, the 6th bit to be the V flag, then does a logical AND between the accumulator and the operand and sets the Z flag if the result is zero.
so something like
lda #$80
sta addr
lda #$0
bit addr
would set the Z and the N bits, and
lda #$40
sta addr
lda #$0
bit addr
would set the V and the N and the Z bits, whereas
lda #$80
sta addr
lda #$80
bit addr
would set the N bit, but not the Z bit. |
| |
Sasq
Registered: Apr 2004 Posts: 157 |
bit-instruction, good point.
Funny though, when I went to check how to make bit compatible with my optimization I saw that I had already thought of that particular issue :) |
| |
Krill
Registered: Apr 2002 Posts: 2997 |
Hmm true, BIT does set N according to bit 7 of the operand only, and Z according to the result of "accu AND operand". |
| |
Quiss
Registered: Nov 2016 Posts: 43 |
I initially thought you could get decimal mode to set both Z and N, since Z is set according to what the calculation would have yielded without decimal mode.
However, the highest value you can reach via decimal adc (while also setting the Z flag) is $66, which is still <$80. |