6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Fri Nov 22, 2024 6:45 pm

All times are UTC




Post new topic Reply to topic  [ 15 posts ] 
Author Message
PostPosted: Sun Dec 09, 2012 7:07 pm 
Offline

Joined: Sun Dec 09, 2012 6:59 pm
Posts: 6
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


Top
 Profile  
Reply with quote  
PostPosted: Sun Dec 09, 2012 8:01 pm 
Offline

Joined: Fri Aug 30, 2002 2:05 pm
Posts: 347
Location: UK
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.


Top
 Profile  
Reply with quote  
PostPosted: Sun Dec 09, 2012 8:04 pm 
Offline

Joined: Sat Jul 28, 2012 11:41 am
Posts: 442
Location: Wiesbaden, Germany
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


Top
 Profile  
Reply with quote  
PostPosted: Sun Dec 09, 2012 8:11 pm 
Offline

Joined: Sun Dec 09, 2012 6:59 pm
Posts: 6
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?


Top
 Profile  
Reply with quote  
PostPosted: Sun Dec 09, 2012 8:37 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8543
Location: Southern California
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?


Top
 Profile  
Reply with quote  
PostPosted: Sun Dec 09, 2012 8:41 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10985
Location: England
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/


Top
 Profile  
Reply with quote  
PostPosted: Sun Dec 09, 2012 8:44 pm 
Offline
User avatar

Joined: Fri Dec 11, 2009 3:50 pm
Posts: 3367
Location: Ontario, Canada
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

_________________
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html


Last edited by Dr Jefyll on Sun Dec 09, 2012 8:57 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
PostPosted: Sun Dec 09, 2012 8:57 pm 
Offline

Joined: Sat Jul 28, 2012 11:41 am
Posts: 442
Location: Wiesbaden, Germany
http://en.wikipedia.org/wiki/Two's_complement

I see it is corrected.

_________________
6502 sources on GitHub: https://github.com/Klaus2m5


Top
 Profile  
Reply with quote  
PostPosted: Sun Dec 09, 2012 9:11 pm 
Offline

Joined: Sun Dec 09, 2012 6:59 pm
Posts: 6
Thank you for the links to the test programs - I shall take a look.

This really is a great community :)


Top
 Profile  
Reply with quote  
PostPosted: Sun Dec 09, 2012 9:23 pm 
Offline
User avatar

Joined: Fri Dec 11, 2009 3:50 pm
Posts: 3367
Location: Ontario, Canada
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


Top
 Profile  
Reply with quote  
PostPosted: Wed Dec 19, 2012 3:19 am 
Offline
User avatar

Joined: Thu Mar 11, 2004 7:42 am
Posts: 362
The overflow flag is described here:

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


Top
 Profile  
Reply with quote  
PostPosted: Mon Feb 04, 2013 6:10 am 
Offline
User avatar

Joined: Tue Jul 12, 2005 6:29 am
Posts: 16
And if you haven't seen it, this was recently posted.

http://www.righto.com/2012/12/the-6502-overflow-flag-explained.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:
        // 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


Top
 Profile  
Reply with quote  
PostPosted: Mon Feb 04, 2013 9:55 am 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10985
Location: England
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.

Top
 Profile  
Reply with quote  
PostPosted: Mon Feb 04, 2013 9:26 pm 
Offline

Joined: Sat Dec 13, 2003 3:37 pm
Posts: 1004
I used http://www.6502.org/tutorials/vflag.html to test my simulator. I had similar issues with a buggy JavaScript 6502 simulator.


Top
 Profile  
Reply with quote  
PostPosted: Sun Mar 03, 2013 3:46 pm 
Offline

Joined: Tue Jul 05, 2005 7:08 pm
Posts: 1043
Location: near Heidelberg, Germany
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/


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 15 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: