1
0
mirror of https://github.com/rkujawa/rk65c02.git synced 2025-04-09 12:38:19 +00:00

Also test overflow for SBC.

This commit is contained in:
Radosław Kujawa 2017-02-02 00:03:27 +01:00
parent 8e732f3b94
commit a153b4e77f

View File

@ -28,9 +28,9 @@ TEST: cld ; Clear decimal mode (just in case) for test
LOOP: jsr ADD ; Test ADC
cpx #1
beq DONE ; End if V and unsigned result do not agree (X = 1)
; jsr SUB ; Test SBC
; cpx #1
; beq DONE ; End if V and unsigned result do not agree (X = 1)
jsr SUB ; Test SBC
cpx #1
beq DONE ; End if V and unsigned result do not agree (X = 1)
inc S1
inc U1
bne LOOP ; Loop until all 256 possibilities of S1 and U1 are tested
@ -70,3 +70,36 @@ ADD3: bpl ADD4 ; U1 + U2 >= 256, A <= 127 if U1 + U2 <= 383 ($17F)
ADD4: rts
;
; Test SBC
;
; X is initialized to 0
; X is incremented when V = 1
; X is incremented when the unsigned result predicts an overflow
; Therefore, if the V flag and the unsigned result agree, X will be
; incremented zero or two times (returning X = 0 or X = 2), and if they do
; not agree X will be incremented once (returning X = 1)
;
SUB: cpy #0x1 ; Set carry when Y = 1, clear carry when Y = 0
lda S1 ; Test twos complement subtraction
sbc S2
ldx #0x0 ; Initialize X to 0
bvc SUB1
inx ; Increment X if V = 1
SUB1: cpy #0x1 ; Set carry when Y = 1, clear carry when Y = 0
lda U1 ; Test unsigned subtraction
sbc U2
pha ; Save the low byte of result on the stack
lda #0xFF
sbc #0 ; result = (65280 + U1) - U2, 65280 = $FF00
cmp #0xFE
bne SUB4 ; Branch if result >= 65280 ($FF00) or result < 65024 ($FE00)
pla ; Get the low byte of result
bmi SUB3 ; result < 65280 ($FF00), A >= 128 if result >= 65152 ($FE80)
SUB2: inx ; Increment X if result < 65152 ($FE80)
SUB3: rts
SUB4: pla ; Get the low byte of result (does not affect the carry flag)
bcc SUB2 ; The carry flag is clear if result < 65024 ($FE00)
bpl SUB5 ; result >= 65280 ($FF00), A <= 127 if result <= 65407 ($FF7F)
inx ; Increment X if result > 65407 ($FF7F)
SUB5: rts