Flag to Mask Conversions

Programming the 6502 microprocessor and its relatives in assembly and other languages.
User avatar
Sheep64
In Memoriam
Posts: 311
Joined: 11 Aug 2020
Location: A magnetic field

Re: Flag to Mask Conversions

Post 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.
TobyLobster
Posts: 37
Joined: 17 Jun 2021

Re: Flag to Mask Conversions

Post 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
+
Post Reply