Oricutron : ADC / SBC not properly handle V flag?
Posted: Thu Oct 23, 2014 2:41 am
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: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?
More informations: http://www.6502.org/tutorials/vflag.html
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
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;\
}