diff --git a/gr-sim/6502_emulate.c b/gr-sim/6502_emulate.c index 4d81b8a9..9ad47a92 100644 --- a/gr-sim/6502_emulate.c +++ b/gr-sim/6502_emulate.c @@ -68,9 +68,11 @@ void sbc(int value) { int temp_value; temp_a=a&0xff; - temp_value=value&0xff; + temp_value=(~value)&0xff; - result=temp_a-temp_value-(!c); + result=temp_a+temp_value+c; + +// printf("SBC: %x - %x (%x) = %x\n",a,value,c,result); c=(result&0x100)>>8; n=(result&0x80)>>7; @@ -79,6 +81,8 @@ void sbc(int value) { a=result&0xff; z=(a==0); + + } void cmp(int value) { diff --git a/gr-sim/6502_test.c b/gr-sim/6502_test.c index b2448649..d0aa0262 100644 --- a/gr-sim/6502_test.c +++ b/gr-sim/6502_test.c @@ -54,7 +54,7 @@ static void test_sbc(void) { if (a!=((i-j-0)&0xff)) { printf("SBC: Error! %d-%d should be %d, not %d\n",i,j,i-j-0,a); } - if (c!=(((i-j-0)>>8)&0x1)) { + if (c==(((i-j-0)>>8)&0x1)) { printf("SBC: Error! Carry should be %d, not %d\n",((i-j-0)>>8)&0x1,c); } if ((a==0) && (z!=1)) printf("SBC error, zflag wrong\n"); @@ -71,7 +71,7 @@ static void test_sbc(void) { if (a!=((i-j-1)&0xff)) { printf("SBC: Error! %d-%d should be %d, not %d\n",i,j,i-j-1,a); } - if (c!=(((i-j-1)>>8)&0x1)) { + if (c==(((i-j-1)>>8)&0x1)) { printf("SBC: Error! Carry should be %d, not %d\n",((i-j-1)>>8)&0x1,c); } if ((a==0) && (z!=1)) printf("SBC error, zflag wrong\n");