cpu V flag
Moderator: Moderators
cpu V flag
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
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
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:
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.
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
Btw this is rather a noob question and does not belong in this section.
get nemulator
http://nemulator.com
http://nemulator.com
- never-obsolete
- Posts: 403
- Joined: Wed Sep 07, 2005 9:55 am
- Location: Phoenix, AZ
- Contact:
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.
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.
I use the following code to check for an overflow w/ ADC:
and for SBC:
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.
Code: Select all
SR.V = (~(A^M)) & (A^result) & 0x80 ? true : false;Code: Select all
SR.V = (A^M) & (A^result) & 0x80 ? true : false;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
http://nemulator.com
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.
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.