Page 1 of 1

Is this an overflow condition?

Posted: Sun Dec 09, 2012 7:07 pm
by MadDoc
Hi,

I'm working on a 6502 emulator in Realbasic (a Visual Basic like language).

I have implemented my ADC (add with carry) instruction thanks to a lot of help from this question on stackoverflow. My output was exactly as the question's answer's example output.

The problem is, I'm now not sure if there is a bug in that answer since I thought 255 + 1 would cause both a carry and an overflow (since 255 + 1 = 256, not 0). With the approach taken in the answered question above, I only get a carry, not an overflow.

The reason I'm doubting myself is because if I use this online javascript 6502 emulator, set the accumulator to $FF and add $01 it says that both the carry and the overflow flags should be set whereas my routine only sets the carry flag.

Which is correct (if either)?

Many thanks

Re: Is this an overflow condition?

Posted: Sun Dec 09, 2012 8:01 pm
by leeeeee
An overflow is only true when the carry from b6 to b7 and the carry out from b7 differ. As both carry bits are 1 in this case there is no overflow.

Lee.

Re: Is this an overflow condition?

Posted: Sun Dec 09, 2012 8:04 pm
by Klaus2m5
Carry refers to unsigned numbers and lower bytes of signed numbers. So $ff + 1 is 0 with carry set.

Overflow refers to signed numbers only and only the highest order Byte. So $ff represents -1. 1 - 1 is 0 with overflow clear.

Generally speaking overflow marks a result as having suffered an overflow into the sign bit position. If adding numbers with different sign, an overflow can never occur. Only if both numbers have the same sign and a carry occurs into the sign bit, than the sign bit and the whole number is no longer valid and an overflow is indicated. Example 127 + 1 = -128 (0x7f +0x01 = 0x80). Obviously that is an overflow.

So the javascript emulator is wrong.

Re: Is this an overflow condition?

Posted: Sun Dec 09, 2012 8:11 pm
by MadDoc
Excellent explanations - thank you very much.

I should have more faith in my own code!

On a related note, does anyone know of a good way of testing individual opcodes? Perhaps some software suite that I can check my code against?

Re: Is this an overflow condition?

Posted: Sun Dec 09, 2012 8:37 pm
by GARTHWILSON
Quote:
On a related note, does anyone know of a good way of testing individual opcodes? Perhaps some software suite that I can check my code against?
André Fachat, if you are reading this-- Do you know if the 65GZ032 group dissolved? I remember someone on the 65GZ032 Yahoo forum posting a link to an 6502 instruction-testing program, but I just went to try to find it, and I can't even find the group. Edit: I see André has some description of, and links to, it, at http://6502.org/users/andre/adv65/index.html#65gz032.

Re: Is this an overflow condition?

Posted: Sun Dec 09, 2012 8:41 pm
by BigEd
I test individual instructions by feeding them into the visual6502 simulator.
For example:
http://www.visual6502.org/JSSim/expert. ... 6cff20eaea
loads a short program and runs for 50 steps (25 clock cycles)

For a relatively full test, use Klaus' testsuite as linked here:
http://6502.org/tools/emu/

Re: Is this an overflow condition?

Posted: Sun Dec 09, 2012 8:44 pm
by Dr Jefyll
Quote:
I remember someone on the 65GZ032 Yahoo forum posting a link to an 6502 instruction-testing program, but I just went to try to find it, and I can't even find the group.
I too feel the urge to test this. My take is that, per signed notation,

11111111 is negative 1
00000001 is positive 1, and
00000000 is their sum! The sign is correct, so no overflow has occured. I expect the flag will be clear. Sorry for the double-take.

Lots of interest in 6502 emulators lately, BTW -- I think it's great! :D

Jeff

Re: Is this an overflow condition?

Posted: Sun Dec 09, 2012 8:57 pm
by Klaus2m5

Re: Is this an overflow condition?

Posted: Sun Dec 09, 2012 9:11 pm
by MadDoc
Thank you for the links to the test programs - I shall take a look.

This really is a great community :)

Re: Is this an overflow condition?

Posted: Sun Dec 09, 2012 9:23 pm
by Dr Jefyll
MadDoc wrote:
This really is a great community :)
Glad to have you with us, MadDoc. In my confusion I forgot to say welcome!

-- Jeff

Re: Is this an overflow condition?

Posted: Wed Dec 19, 2012 3:19 am
by dclxvi
The overflow flag is described here:

http://6502.org/tutorials/vflag.html

Re: Is this an overflow condition?

Posted: Mon Feb 04, 2013 6:10 am
by djmips
And if you haven't seen it, this was recently posted.

http://www.righto.com/2012/12/the-6502- ... ained.html

Note that the javascript 6502 emulator you referenced has bugs, I have examined the code and it needs to be corrected.

Ken Shirrif's blog notes that for code, you can use

(A ^ sum) & (operand ^ sum) & 0x80 is nonzero. Overflow occurs when the signs of A and the operand are the same and when the sign of A and the sum are the same.

Another C++ formula is !((M^N) & 0x80) && ((M^result) & 0x80) "

The Stella emulator uses the latter formulation except it uses bitwise operators and eschews the logical && to avoid doing the & 0x80 twice.

C code executed after an add

V = ~(A ^ operand) & (A ^ sum) & 0x80;

Many emulators do the same thing that the 6502 does and use the same code for subtract but first do a ones complement of the operand prior to adding.

Here is some branchless javascript code to do SBC (in non decimal mode); It works similar to the 6502 in the aspect that it does the ones complement first and then just uses add logic. In this emulation, the flags are kept in variables and are reassembled into the actual P register when required for PHP

Code: Select all

        // javascript code for SBC , 'a' is the accumulator and 'operand' is the data to be subtracted
        // ADC would be the same except the ones complement ~operand & 0xff is not needed
        operand = ~operand & 0xff;
        var sum = a + operand + (Cf>>8);
        Nf = sum & 0x80;
        Vf = (~(a ^ operand) & (a ^ sum) & 0x80);
        Cf = (sum & 0x100);
        a = sum & 0xff;
        nZf = a;
note this keeps a NOT zero flag instead of a zero flag

Re: Is this an overflow condition?

Posted: Mon Feb 04, 2013 9:55 am
by BigEd
djmips wrote:
... Note that the javascript 6502 emulator you referenced has bugs, I have examined the code and it needs to be corrected
Just to note: that's the emulator at http://www.masswerk.at/6502 and I agree it has bugs - Nick's in-browser emulator at http://skilldrick.github.com/easy6502/ and Mike's command-line emulator at https://github.com/mnaberez/py65 both pass Klaus' testsuite and should be preferred.

Cheers
Ed

Edit: http://www.masswerk.at/6502 replaces e-tradition dot net

Re: Is this an overflow condition?

Posted: Mon Feb 04, 2013 9:26 pm
by whartung
I used http://www.6502.org/tutorials/vflag.html to test my simulator. I had similar issues with a buggy JavaScript 6502 simulator.

Re: Is this an overflow condition?

Posted: Sun Mar 03, 2013 3:46 pm
by fachat
GARTHWILSON wrote:
Quote:
On a related note, does anyone know of a good way of testing individual opcodes? Perhaps some software suite that I can check my code against?
André Fachat, if you are reading this-- Do you know if the 65GZ032 group dissolved? I remember someone on the 65GZ032 Yahoo forum posting a link to an 6502 instruction-testing program, but I just went to try to find it, and I can't even find the group.
Sorry to pick that up only now. It looks the group has indeed resolved. Even the links on my pages are broken in the meantime.
Maybe ask on the 1541ultimate page for Gideon, the creator: http://www.commodorefree.com/magazine/v ... tml#GIDEON

Andre