Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
You are not logged in - nap
CSDb User Forums


Forums > C64 Coding > Z and N flags ever set together?
2023-12-10 14:55
Sasq

Registered: Apr 2004
Posts: 155
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).
2023-12-10 15:12
Krill

Registered: Apr 2002
Posts: 2844
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? =)
2023-12-10 15:16
trident

Registered: May 2002
Posts: 74
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.
2023-12-10 15:48
Sasq

Registered: Apr 2004
Posts: 155
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 :)
2023-12-10 15:56
Krill

Registered: Apr 2002
Posts: 2844
Hmm true, BIT does set N according to bit 7 of the operand only, and Z according to the result of "accu AND operand".
2023-12-10 23:54
Quiss

Registered: Nov 2016
Posts: 37
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.
RefreshSubscribe to this thread:

You need to be logged in to post in the forum.

Search the forum:
Search   for   in  
All times are CET.
Search CSDb
Advanced
Users Online
Yogibear/Protovision
Sentinel/Excess/TREX
KAL_123
Guests online: 123
Top Demos
1 Next Level  (9.8)
2 Mojo  (9.7)
3 Coma Light 13  (9.7)
4 Edge of Disgrace  (9.6)
5 Comaland 100%  (9.6)
6 No Bounds  (9.6)
7 Uncensored  (9.6)
8 Wonderland XIV  (9.6)
9 Memento Mori  (9.6)
10 Bromance  (9.5)
Top onefile Demos
1 It's More Fun to Com..  (9.7)
2 Party Elk 2  (9.7)
3 Cubic Dream  (9.6)
4 Copper Booze  (9.5)
5 TRSAC, Gabber & Pebe..  (9.5)
6 Rainbow Connection  (9.5)
7 Wafer Demo  (9.5)
8 Dawnfall V1.1  (9.5)
9 Quadrants  (9.5)
10 Daah, Those Acid Pil..  (9.5)
Top Groups
1 Oxyron  (9.3)
2 Nostalgia  (9.3)
3 Booze Design  (9.3)
4 Censor Design  (9.3)
5 Crest  (9.3)
Top Fullscreen Graphicians
1 Carrion  (9.8)
2 Joe  (9.8)
3 Duce  (9.8)
4 Mirage  (9.7)
5 Facet  (9.7)

Home - Disclaimer
Copyright © No Name 2001-2024
Page generated in: 0.042 sec.