I have recovered an old 6502 emulator I did years ago to implement some new features. During testing I discovered something wrong, surely due to an error in my implementation.
I have to loop through a 16 bit subtraction until the result is negative: quite simple, no? Here is an example:
V1 equ $90
V2 equ $01
Label:
sec
lda V2
sbc #2
sta V2
lda V1
sbc #10
sta V1
"Branch" to Label if result is >0, otherwise exit
rts
Now, the problem is to identify which branch to select or to find a different solution.
The BCS
is not valid if the V2 subtraction clears the carry.
The BPL
in not valid if V1 is 'negative' (>$80).
It should be easy, but...
EDIT
I did not find in the aswers a real solution.
Let me try to follow the logic, firstly with the original values as in code.
- Carry is set by
SEC
- The fist sub (1-2) clears the carry.
V1
= $FF - the second sub ($90-$0A-1 (not borrow)) results in
V2
=$85 - carry is cleared; result ($85FF) is still negative)
I can not test the result withBCS
(to jump to label) norBMI
sinceV2
is negative.
So?
With a different set, i.e. V1
=$1 and V2
=$0A I will have a result < 0, which is my goal to stop iterations.
Any suggestion?