It is currently Thu Jan 17, 2019 12:09 am

 All times are UTC - 7 hours

 Page 1 of 1 [ 7 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Problem with ADC and nestestPosted: Sat Aug 25, 2018 12:01 pm

Joined: Sat Aug 25, 2018 7:21 am
Posts: 29
Hello.
I am testing my 6502 emulator and I am having some trouble with ADC. I am using this nestest log for this purpose: http://www.qmtpro.com/~nes/misc/nestest.log

Code:
C966  A9 7F     LDA #\$7F                        A:FF X:00 Y:00 P:E4 SP:FB CYC: 95
C968  69 80     ADC #\$80                        A:7F X:00 Y:00 P:64 SP:FB CYC:101
C96A  10 0B     BPL \$C977                       A:FF X:00 Y:00 P:A4 SP:FB CYC:107

According to this, C = 0 before ADC. Therefore, the operation is: A = \$7F + \$80. The result should be A = \$FF. As the sign changes V flag should be 1. However, this log shows P = \$A4, when I think it should be P = \$E4. Why is this happening? Am I wrong?

Top

 Post subject: Re: Problem with ADC and nestestPosted: Sat Aug 25, 2018 12:09 pm

Joined: Sun Apr 13, 2008 11:12 am
Posts: 7964
Location: Seattle
That adds -128 to +127. The result, -1, still fits in a signed 8 bit number, so oVerflow is not set.

Top

 Post subject: Re: Problem with ADC and nestestPosted: Sat Aug 25, 2018 12:29 pm

Joined: Thu Mar 31, 2016 11:15 am
Posts: 462
Overflow flag isn't for sign changes. It's for if the result isn't represented correctly.

Here's a helpful page: http://www.righto.com/2012/12/the-6502- ... ained.html

Top

 Post subject: Re: Problem with ADC and nestestPosted: Sat Aug 25, 2018 3:46 pm

Joined: Sun Sep 19, 2004 9:28 pm
Posts: 3767
adc/sbc have been discussed heavily over the years, and still remain the top struggle point for people writing 6502 cores. Here's reference material, including actual (very simple) code you can use to accomplish proper implementation: viewtopic.php?p=119626#p119626

P.S. -- Are you just writing a 6502 core, or are you writing a NES emulator? Thanks.

Top

 Post subject: Re: Problem with ADC and nestestPosted: Sat Aug 25, 2018 9:38 pm

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 11086
Location: Rio de Janeiro - Brazil
Signed numbers range from -128 to 127, so whenever the output is in that range, V should be clear. No addition of inputs with different signs can generate an invalid output (e.g. 0 + -128 = -128; 127 + -128 = -1), but when the inputs have the same sign, it's possible for the result to go below -128 or over 127 (e.g. -128 + -1 = -129; 127 + 1 = 128), resulting in invalid 8-bit 2's complement values. This means that the rule for the V flag is: if both inputs have the same sign AND the output has a different sign, set the V flag, otherwise, clear it.

BTW, this logic is for ADC, but when emulating the 6502, the simplest way to implement SBC is to reuse the ADC code but XOR the operand with 255. If I'm not mistaken, even the real chip implements SBC that way.

Top

 Post subject: Re: Problem with ADC and nestestPosted: Sun Aug 26, 2018 4:33 am

Joined: Sat Aug 25, 2018 7:21 am
Posts: 29
Thank you all for your answers. I was clearly wrong about the V flag. Now I can go on with the emulator.

koitsu wrote:
adc/sbc have been discussed heavily over the years, and still remain the top struggle point for people writing 6502 cores. Here's reference material, including actual (very simple) code you can use to accomplish proper implementation: http://forums.nesdev.com/viewtopic.php?p=119626#p119626

P.S. -- Are you just writing a 6502 core, or are you writing a NES emulator? Thanks.

I am writing a NES emulator.

Top

 Post subject: Re: Problem with ADC and nestestPosted: Tue Dec 04, 2018 2:22 pm

Joined: Thu Sep 15, 2005 9:23 am
Posts: 1227
Location: Berlin, Germany
Here is how I handle the V flag. I hope this helps you understand better how overflow works.

int temp;

temp = (char)A + (char)DataBus + C;

V = (temp < -128 || temp > 127);

Edits: Formatting.

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 7 posts ]

 All times are UTC - 7 hours

#### Who is online

Users browsing this forum: No registered users and 3 guests

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

Search for:
 Jump to:  Select a forum ------------------ NES / Famicom    NESdev    NESemdev    NES Graphics    NES Music    Homebrew Projects       2018 NESdev Competition       2017 NESdev Competition       2016 NESdev Competition       2014 NESdev Competition       2011 NESdev Competition    Newbie Help Center    NES Hardware and Flash Equipment       Reproduction    NESdev International       FCdev       NESdev China       NESdev Middle East Other    General Stuff    Membler Industries    Other Retro Dev       SNESdev       GBDev    Test Forum Site Issues    phpBB Issues    Web Issues    nesdevWiki