Hi Mike
By way of experiment here's a test program which runs through all ADC operands, with carry set and clear, and compares the V flag with the expected result.
It passes on NMOS 6502, on 65816, on lib6502 and on my patched up py65. There's a problem with my copy of lib65816 which I haven't yet run to ground. (Edit: passes now on lib65816 too - BRK was mishandled.)
This is assembled with ORG=0:
Code:
S1230000A90085A085A118201B4038201B40E6A0D0F4E6A1D0F000204F4B00A5A065A130DC
S122002014A5A01004A5A1300370036070FD00204641494C00A5A030F0A5A130EC10ED97
Here's the source with ORG=4000:
Code:
;; test ADC's setting of V flag by running exhaustive test
zpbase = $a0
operanda = zpbase
operandb = zpbase+1
.org $4000
lda #0
sta operanda
sta operandb
top
clc
jsr testadd
sec
jsr testadd
inc operanda
bne top
inc operandb
bne top
brk ;; or jsr putmsg
.byte " OK"
brk
testadd
lda operanda
adc operandb
testvflag
;; for add, overflow is when the inputs are same sign and output is different
bmi resultisnegative
resultispositive
lda operanda
bpl assertvclear ; no sign change so no overflow
;; a<0 and a+b>=0 so b ought to be positive
lda operandb
bmi assertvset
assertvclear
bvs fail
ok
rts
assertvset
bvs ok
fail
brk ;; or jsr putmsg
.byte " FAIL"
brk
resultisnegative
lda operanda
bmi assertvclear ; no sign change so no overflow
;; a>=0, a+b<0
lda operandb
bmi assertvclear
bpl assertvset
Among the caveats: we use the machine to test itself, so there could be compensatory flaws; we haven't proved that we exercise all the interesting cases; we check the arithmetic but not say the addressing modes; not checking BCD mode.
I'm not sure how easily these ideas carry over to SBC. For the C flag, one might want also to test the various compare operations.
I'm thinking one could test the carry out by using ASR to right-shift the operands, accounting for the LSBs which land in C, and thus performing a 9-bit addition without relying on ADC's own C output. That is, bit 7 of that sum should match the C flag of the original sum.
But in any case, the V flag seems to cause the most trouble to get right, along with BCD mode. So it might not be worth checking C in this way: a few conventional tests would be much easier to write.
Comments welcome.
Cheers
Ed