Forums

Sega Master System / Mark III / Game Gear
SG-1000 / SC-3000 / SF-7000 / OMV
Home - Forums - Games - Scans - Maps - Cheats - Credits
Music - Videos - Development - Hacks - Translations - Homebrew

View topic - Z80 INC instructions and flag affection...

Reply to topic
Author Message
Belgarath
  • Guest
Reply with quote
Z80 INC instructions and flag affection...
Post Posted: Mon Jul 09, 2001 4:44 pm
Just a quick question for Zoop and anyone else with experience with the Z80. According to the Z80 specs, the 8-bit INC instruction should affect the V (overflow) flag. My interpretation of this is that, the following code:

LD A, 0xFF
INC A

Should cause an overflow, resulting in the V flag being set. However, MasterGear (the one emulator I've tested so far :) doesn't seem to set V. So, the question is: what is the correct behaviour?
 
  • Site Admin
  • Joined: 25 Oct 1999
  • Posts: 2029
  • Location: Monterey, California
Reply with quote
Post Posted: Mon Jul 09, 2001 6:04 pm
Quote
> Should cause an overflow, resulting in the V flag being set. However, MasterGear (the one emulator I've tested so far :) doesn't seem to set V. So, the question is: what is the correct behaviour?

The overflow assumes signed operation. That is, it is set if a value crosses over the maximum possible integer (127, or 0x7f), or below the minimum possible integer (-128, or 0x80).

As for how it affects DEC/INC, V is set on inc if the result is 0x80, and V is set on dec if the result is 0x7f. Otherwise, it's cleared.
  View user's profile Send private message Visit poster's website
  • Joined: 18 Sep 1999
  • Posts: 498
  • Location: Portland, Oregon USA
Reply with quote
Post Posted: Mon Jul 09, 2001 9:41 pm
I'll expand a little on Heliophobe's response:

The definition of Overflow is Overflow = C7 XOR C6 where

C7 is the CARRY OUT of bit position 7 (C7 is the CARRY flag)
C6 is the CARRY OUT of bit position 6 (into bit position 7)

As Heliophobe said, Overflow only has meaning for SIGNED operations. Overflow is set whenever the SIGN of the result changes when it shouldn't. The case of INCrementing 0xFF does not result in overflow since 0xFF represents -1. Adding 1 to -1 (0xFF) results in the (correct) answer 0x00. Adding a positive number to a negative number never results in Overflow.

--
Eric Quinn
  View user's profile Send private message Visit poster's website
Belgarath
  • Guest
Reply with quote
Thanks guys. :)
Post Posted: Tue Jul 10, 2001 1:29 am
Quote
> The overflow assumes signed operation. That is, it is set if a
> value crosses over the maximum possible integer (127, or 0x7f),
> or below the minimum possible integer (-128, or 0x80).

> As for how it affects DEC/INC, V is set on inc if the result is
> 0x80, and V is set on dec if the result is 0x7f. Otherwise, it's
> cleared.

Ahh... that makes ever so much sense. For some reason, I thought that overflow indicated overflowing the value of the register (in this case, going from 0xFF to 0x00)... 'course, upon reflection, that doesn't really make sense when decrementing (it's an underflow, in that case), plus, it's indicated in the S bit, so that would really be a duplication of information. Anyway, thanks a lot (and thanks to Eric, as well :) for the clarification. Now I have to rewrite some code. :)
 
Reply to topic



Back to the top of this page

Back to SMS Power!