Page 2 of 2

Re: Flag to Mask Conversions

Posted: Mon Nov 21, 2022 11:50 am
by Sheep64
Bithacks are surprisingly rare on 6502. Branchless, constant time operations are very useful on a deterministic processor. However, bithacks typically require more clock cycles than ballasted branches. This makes algorithms particularly tedious because it is another constraint in addition to balancing stacks. On 6507, illegal instructions are common and some of these can be used productively to counter the even/odd count of branches. It is also possible to jump into the end of an instruction. Therefore, one byte, two byte and three byte sequences may have very different semantics. This may be combined with address aliasing. Indeed, illegal instructions, instruction aliasing and address aliasing can all be used simultaneously.

Re: Flag to Mask Conversions

Posted: Fri Nov 25, 2022 12:34 pm
by TobyLobster

Code: Select all

    (lda <some value>)
    beq zero         ; 2/3 cycles
    lda #255         ; 2 cycles
zero
    beq +            ; 2/3 cycles
+
I think this should work. If you load A with a value first and this code lives in a single page then you can test the Z flag with a constant 6 cycles.

More generally (not depending on the value being in A) e.g. For the N flag, 7 cycles:

Code: Select all

    bpl non_negative
    lda #255
non_negative
    bmi +
    lda #0
+