Oricutron : ADC / SBC not properly handle V flag?

Comments, problems, suggestions about Oric emulators (Euphoric, Mess, Amoric, etc...) it's the right place to ask. And don't hesitate to give your tips and tricks that help using these emulations in the best possible way on your favorite operating system.
christian
Pilot Officer
Posts: 96
Joined: Sun Nov 24, 2013 9:58 pm

Oricutron : ADC / SBC not properly handle V flag?

Post 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
User avatar
Hialmar
Flight Lieutenant
Posts: 349
Joined: Tue Mar 04, 2014 11:25 am
Location: Toulouse, France
Contact:

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

Post by Hialmar »

I patched the code while uploading my modifications to iss's TCP modem emulation.
Hialmar
CEO and Silicium member.
User avatar
Xeron
Emulation expert
Posts: 426
Joined: Sat Mar 07, 2009 5:18 pm
Contact:

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

Post by Xeron »

Awesome work! Really good find.

This fixes Krocatile Waltz, which had me really stumped.
Post Reply