Is this an overflow condition?
Is this an overflow condition?
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
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?
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.
Lee.
Re: Is this an overflow condition?
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.
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
Re: Is this an overflow condition?
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?
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?
- GARTHWILSON
- Forum Moderator
- Posts: 8775
- Joined: 30 Aug 2002
- Location: Southern California
- Contact:
Re: Is this an overflow condition?
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?
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?
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
Re: Is this an overflow condition?
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/
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?
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.
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!
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
https://laughtonelectronics.com/Arcana/ ... mmary.html
Re: Is this an overflow condition?
6502 sources on GitHub: https://github.com/Klaus2m5
Re: Is this an overflow condition?
Thank you for the links to the test programs - I shall take a look.
This really is a great community
This really is a great community
Re: Is this an overflow condition?
MadDoc wrote:
This really is a great community 
-- Jeff
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html
https://laughtonelectronics.com/Arcana/ ... mmary.html
Re: Is this an overflow condition?
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
note this keeps a NOT zero flag instead of a zero flag
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;
Re: Is this an overflow condition?
djmips wrote:
... Note that the javascript 6502 emulator you referenced has bugs, I have examined the code and it needs to be corrected
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.
Re: Is this an overflow condition?
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?
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?
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/