cpu V flag

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

Post Reply
User avatar
Anes
Posts: 621
Joined: Tue Dec 21, 2004 8:35 pm
Location: Mendoza, Argentina

cpu V flag

Post by Anes »

it seems im having problems with V Flag in ADC Y SBC my cpu emu. Can somebody tell my if what im doing is OK?:

ADC:

- if source and mem are + and result is - : Set Flag
- if source and mem are - and result is +: Set Flag

SBC:

The same above instead we change the mem to ~mem
ANes
User avatar
blargg
Posts: 3717
Joined: Mon Sep 27, 2004 8:33 am
Location: Central Texas, USA
Contact:

Post by blargg »

Maybe you aren't taking the carry into account?

The overflow flag is set based on the sum of the two signed 8-bit values and carry being outside the inclusive range of -128 to 127. One way is simple:

Code: Select all

int result = (signed char) source + (signed char) mem + carry;
overflow = (result < -128 || result > 127);
overflow = (result + 128) & 0x100; // equivalent; possibly faster
The signed char casts are essential. Note that the previous value of the overflow flag is always replaced with the new value. As for SBC, I just XOR the operand with 0xFF and then treat it as an ADC.
WedNESday
Posts: 1231
Joined: Thu Sep 15, 2005 9:23 am
Location: Berlin, Germany
Contact:

Post by WedNESday »

Here is how I do mine:

if( (char)Accumulator + (char)Data + Carry < -128 || (char)Accumulator + (char)Data + Carry > 127 )
SetOverflow

Btw this is rather a noob question and does not belong in this section.
User avatar
James
Posts: 429
Joined: Sat Jan 22, 2005 8:51 am
Location: Chicago, IL
Contact:

Post by James »

Btw this is rather a noob question and does not belong in this section.
:roll:
get nemulator
http://nemulator.com
WedNESday
Posts: 1231
Joined: Thu Sep 15, 2005 9:23 am
Location: Berlin, Germany
Contact:

Post by WedNESday »

Well isn't it? By now you should have phased out pathetically easy bugs like that.
User avatar
never-obsolete
Posts: 403
Joined: Wed Sep 07, 2005 9:55 am
Location: Phoenix, AZ
Contact:

Post by never-obsolete »

it's a legitimate question and i know by personal experience that not having the flags set correctly for each instruction is a pain.
User avatar
teaguecl
Posts: 210
Joined: Thu Oct 21, 2004 4:02 pm
Location: San Diego

Post by teaguecl »

Wednesday, there's no need to call the problem "pathetically easy". Maybe this should go in the newbie forum, but if not it certainly belongs here. This is the same forum where you asked how to load a value into a shift register, which not only is easy, but is well documented elsewhere, and nobody gave you grief about it.
http://nesdev.com/bbs/viewtopic.php?p=5 ... e0cde#5123
Plus, this is the second time your etiquette has been questionable in the 25 days you've been a member here:
http://nesdev.com/bbs/viewtopic.php?p=5 ... ight=#5086

Your enthusiasm is great, and I'm sure you don't mean any harm, but please be respectful to other people regardless of their level of knowledge.
User avatar
James
Posts: 429
Joined: Sat Jan 22, 2005 8:51 am
Location: Chicago, IL
Contact:

Post by James »

I use the following code to check for an overflow w/ ADC:

Code: Select all

SR.V = (~(A^M)) & (A^result) & 0x80 ? true : false;
and for SBC:

Code: Select all

SR.V = (A^M) & (A^result) & 0x80 ? true : false;
result = A + M + carry, or A - M - ~carry

In plain english, for ADC, if the high bit of the accumulator and memory are the same (i.e. they are both positive/negative) and the accumulator and the result have different signs, set the overflow flag. For SBC, set the flag if accumulator and memory are of opposite signs. File this under 'more complex than it needs to be' :)

I can't take the credit for this -- I saw this technique used elsewhere before, and I thought it was a clever solution, so I stuck with it.

James

edit: FWIW, I benchmarked the solutions posted here. This method is a few percent (2-7%) faster than the others. YM(or architecture)MV.
Last edited by James on Mon Oct 10, 2005 4:40 pm, edited 2 times in total.
get nemulator
http://nemulator.com
User avatar
Anes
Posts: 621
Joined: Tue Dec 21, 2004 8:35 pm
Location: Mendoza, Argentina

Post by Anes »

thanks james and blargg now i have correct emulation.
ANes
WedNESday
Posts: 1231
Joined: Thu Sep 15, 2005 9:23 am
Location: Berlin, Germany
Contact:

Post by WedNESday »

Yeah yeah, whatever.
User avatar
blargg
Posts: 3717
Joined: Mon Sep 27, 2004 8:33 am
Location: Central Texas, USA
Contact:

Post by blargg »

I will defend the question of proper V flag behavior as not being a newbie question. Before replying I checked the original Rockwell manual and it was not at all clear about V flag behavior. It's similar to adding the extra clock to a branch instruction when it "crosses pages". I've just investigated this on my NES and I imagine many people would answer wrong as to when it actually occurs (start a new thread if you're wondering).

But I'll also defend attempts to preserve the use of this section's usefulness by directing basic questions to the newbie forum, but not using put-downs. The best solution is for a few mini-moderators to be allowed to (silently) move topics if they would be better placed elsewhere.
aphex
Posts: 25
Joined: Thu Apr 22, 2010 1:35 pm
Location: England

Post by aphex »

A n00b question is

"How to write emulator? Plz give source!"

This is a question about a commonly misunderstood flag in the 6502 processor.
ReaperSMS
Posts: 174
Joined: Sun Sep 19, 2004 11:07 pm

Post by ReaperSMS »

Please, let the dead threads lie. What did they ever do to you?
Post Reply