Is this an overflow condition?

Topics pertaining to the emulation or simulation of the 65xx microprocessors and their peripheral chips.
Post Reply
MadDoc
Posts: 6
Joined: 09 Dec 2012

Is this an overflow condition?

Post 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
leeeeee
In Memoriam
Posts: 347
Joined: 30 Aug 2002
Location: UK
Contact:

Re: Is this an overflow condition?

Post 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.
Klaus2m5
Posts: 442
Joined: 28 Jul 2012
Location: Wiesbaden, Germany

Re: Is this an overflow condition?

Post 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.
6502 sources on GitHub: https://github.com/Klaus2m5
MadDoc
Posts: 6
Joined: 09 Dec 2012

Re: Is this an overflow condition?

Post 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?
User avatar
GARTHWILSON
Forum Moderator
Posts: 8773
Joined: 30 Aug 2002
Location: Southern California
Contact:

Re: Is this an overflow condition?

Post 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.
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
User avatar
BigEd
Posts: 11463
Joined: 11 Dec 2008
Location: England
Contact:

Re: Is this an overflow condition?

Post 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/
User avatar
Dr Jefyll
Posts: 3525
Joined: 11 Dec 2009
Location: Ontario, Canada
Contact:

Re: Is this an overflow condition?

Post 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
Last edited by Dr Jefyll on Sun Dec 09, 2012 8:57 pm, edited 1 time in total.
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html
Klaus2m5
Posts: 442
Joined: 28 Jul 2012
Location: Wiesbaden, Germany

Re: Is this an overflow condition?

Post by Klaus2m5 »

6502 sources on GitHub: https://github.com/Klaus2m5
MadDoc
Posts: 6
Joined: 09 Dec 2012

Re: Is this an overflow condition?

Post by MadDoc »

Thank you for the links to the test programs - I shall take a look.

This really is a great community :)
User avatar
Dr Jefyll
Posts: 3525
Joined: 11 Dec 2009
Location: Ontario, Canada
Contact:

Re: Is this an overflow condition?

Post 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
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html
User avatar
dclxvi
Posts: 362
Joined: 11 Mar 2004

Re: Is this an overflow condition?

Post by dclxvi »

The overflow flag is described here:

http://6502.org/tutorials/vflag.html
User avatar
djmips
Posts: 17
Joined: 12 Jul 2005

Re: Is this an overflow condition?

Post 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
User avatar
BigEd
Posts: 11463
Joined: 11 Dec 2008
Location: England
Contact:

Re: Is this an overflow condition?

Post 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
Last edited by BigEd on Sun May 06, 2018 9:50 am, edited 1 time in total.
whartung
Posts: 1004
Joined: 13 Dec 2003

Re: Is this an overflow condition?

Post 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.
fachat
Posts: 1123
Joined: 05 Jul 2005
Location: near Heidelberg, Germany
Contact:

Re: Is this an overflow condition?

Post 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
Author of the GeckOS multitasking operating system, the usb65 stack, designer of the Micro-PET and many more 6502 content: http://6502.org/users/andre/
Post Reply