Page 1 of 1

Oricutron : ADC / SBC not properly handle V flag?

Posted: Thu Oct 23, 2014 2:41 am
by christian
Hi Xeron,

I have a program that does not work with Oricutron but works well with Euphoric.
After some digging, I think intructions ADC and SBC do not properly handle the V flag (at least in binary mode)

This code should end at $500A with V flag set:

Code: Select all

5000    CLC
5001    CLV
5002    LDA #$B4
5004    ADC #$B4
5006    BVS ok
5008    BRK
ok:
500A    BRK
But it ends at $5008 with V flag unset.

Explanation:
$B4 is negative, so the result must be negative too but $B4+$B4 = $168 => ACC = $68, C=1
As $68 is a positive number the N flag is unset and the V flag must be set to indicate an overflow condition.

Same kind of test can be done with SBC.

May i suggest this patch?

Code: Select all

--- 6502.c	2014-10-23 02:38:49.179408990 +0200
+++ 6502.c.new	2014-10-23 02:38:29.210833886 +0200
@@ -120,7 +120,7 @@
                  cpu->f_n = cpu->a&0x80;\
                } else {\
                  r = cpu->a + v + cpu->f_c;\
-                 cpu->f_v = ((cpu->a^v)&(cpu->a^(r&0xff))&0x80) ? 1 : 0;\
+                 cpu->f_v = (~(cpu->a^v)&(cpu->a^r)) & FF_N ? 1 : 0;\
                  FLAG_ZCN(r);\
                  cpu->a = r;\
                }
@@ -172,7 +172,7 @@
                  cpu->f_n = cpu->a&0x80;\
                } else {\
                  r = (cpu->a - v) - (cpu->f_c^1);\
-                 cpu->f_v = ((cpu->a^v)&(cpu->a^(r&0xff))&0x80) ? 1 : 0;\
+                 cpu->f_v = ((cpu->a^v)&(cpu->a^r)) & FF_N ? 1 : 0;\
                  FLAG_SZCN(r);\
                  cpu->a = r;\
                }
More informations: http://www.6502.org/tutorials/vflag.html

Re: Oricutron : ADC / SBC not properly handle V flag?

Posted: Fri Oct 24, 2014 8:37 am
by Hialmar
I patched the code while uploading my modifications to iss's TCP modem emulation.

Re: Oricutron : ADC / SBC not properly handle V flag?

Posted: Fri Oct 24, 2014 11:13 pm
by Xeron
Awesome work! Really good find.

This fixes Krocatile Waltz, which had me really stumped.