6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sat Nov 23, 2024 8:53 am

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: 6502 Overflow Question
PostPosted: Sun Mar 02, 2014 12:53 pm 
Offline

Joined: Sun Mar 02, 2014 12:26 pm
Posts: 3
Hi, I am building a 6502 emulator from scratch. I am fairly new to 6502
machine language. My question is related to the SBC instruction and the Overflow flag.

SEC
LDA #$03
SBC #$F3

Is this correct?

Accumulator = $10
Zero = 0
Carry = 0
Negative = 0
Overflow = 0

My understanding is the Overflow flag should be set if the result is greater than
127 or less than -128. To me this seems ambiguous because if #$F3 is unsigned (243),
then the result would be 3 - 243 = -240 so the Overflow flag should be set. If #$F3 is
signed (-13), then the result is 3 - - 13 = 16 thus the Overflow flag should not be set.

What am I misunderstanding?

Thank you,

Chris


Top
 Profile  
Reply with quote  
PostPosted: Sun Mar 02, 2014 1:24 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10986
Location: England
Indeed, no overflow. The overflow flag is all about signed arithmetic, so think of all the arguments as signed. As you note, that means you are subtracting a small negative value. Note also that SBC is exactly like ADC except the operand is inverted. SBC #$f3 is just like ADC #$c.

For testing short programs, try http://skilldrick.github.io/easy6502/ and for a second opinion paste the hex code into visual6502 like this:
http://visual6502.org/JSSim/expert.html ... 03e9f3eaea

See also http://www.6502.org/tutorials/vflag.html

Oh, and welcome!

Cheers
Ed


Last edited by BigEd on Sun Mar 02, 2014 1:27 pm, edited 2 times in total.

Top
 Profile  
Reply with quote  
PostPosted: Sun Mar 02, 2014 1:25 pm 
Offline

Joined: Tue Jan 07, 2014 8:40 am
Posts: 91
If you add two positive numbers and get a negative result (MSB=1), that is an overflow condition. Similarly, if you add two negative numbers and get a positive result (MSB=0), that is an overflow. (If you add a positive number and a negative number, you cannot get an overflow condition.)

There's a fairly good explanation here: http://teaching.idallen.com/cst8214/08w ... erflow.txt When designing hardware, the "method 2" is normally used -- comparing the carry into bit 7 with the carry out of bit 7. When writing software, you don't have access to that internal carry bit, so you have to use his "method 1" -- examining the signs of the two operands, and the sign of the result.

_________________
Because there are never enough Forth implementations: http://www.camelforth.com


Top
 Profile  
Reply with quote  
PostPosted: Sun Mar 02, 2014 1:42 pm 
Offline

Joined: Sun Mar 02, 2014 12:26 pm
Posts: 3
Thank you for the very imformative responses!


Top
 Profile  
Reply with quote  
PostPosted: Sun Mar 02, 2014 2:53 pm 
Offline
User avatar

Joined: Mon Apr 23, 2012 12:28 am
Posts: 760
Location: Huntsville, AL
cgillies:

Since your aim is to write a simulator for the 6502, Klaus Dormann (Klaus2M5) has released a very comprehensive test program for the 6502 that you may want to consider using for validating your simulator. It has been very helpful to me in helping isolate the last few deficiencies in my 65C02 FPGA core. I have greater confidence in my core after using his test program. I histogrammed its test cases, and it covers all 6502 instructions and addressing modes.

You can find it on his GitHUB repository. He's also provided some extended test programs for the 65C02 opcodes and addressing modes in the event you decide to take your simulator in that direction as well.

_________________
Michael A.


Top
 Profile  
Reply with quote  
PostPosted: Sun Mar 02, 2014 3:37 pm 
Offline

Joined: Sun Mar 02, 2014 12:26 pm
Posts: 3
MichaelM - thank you for your post. That test program will be extremely helpful for me. I am almost to the point were I will use it. I just have to implement a few more instructions and their corresponding unit tests!


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 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: