Click here to Skip to main content
15,039,149 members
Articles / All Topics
Technical Blog
Posted 22 Jun 2016

Tagged as

Stats

10K views

Z80 CPU - Flag Register

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
22 Jun 2016CPOL2 min read
Z80 CPU - Flag Register

When we first looked at registers, I mentioned that there was a Flag Register. This differs from the other registers in that each of the Bits of the register has a specific meaning that some of the operations rely on.

  • Bit 7: Sign Flag
  • Bit 6: Zero Flag
  • Bit 5: Not Used
  • Bit 4: Half Carry Flag
  • Bit 3: Not Used
  • Bit 2: Parity / Overflow Flag
  • Bit 1: Add / Subtract Flag
  • Bit 0: Carry Flag

The Sign Flag is used to store the state of the most-significant bit of the Accumulator (which represents the sign of the value using twos-complement notation).

The Zero Flag is used by certain operations to specify if its result was Zero and for comparisons.

The Half Carry flag is used when using decimal mathematics.

The Parity / Overflow Flag is used to determine if various registers have overflowed based on the instruction being performed and is also used to indicate data parity when inputting a byte from an I/O device.

The Add/Subtract Flag is used for decimal mathematics to distinguish between the ADD and SUBTRACT instructions.

The Carry Flag is mainly used to indicate a carry for an addition or subtraction instruction.

At the time of writing, I have implemented a few operations that use the flag registers (8 bit Increment, Decrement and Add) and some of them have been very easy.

Flags such as the sign flag and the zero flag are very straight forward and the Add/Subtract flag has been easy so far.

The flag that has caused me the most trouble has been the half carry flag, which is to be set only if a carry from bit 3 occurs.

Originally, I interpreted this as if any addition occurs that results in a value of 32 or above, then the flag should be set. In which case, 64 + 64 would cause the flag to be set. This is incorrect.

The correct way of calculating whether a carry from bit 3 has occurred is to sum the lower nibbles of the values and then see if the value is 32 or above. (So I wasn't that far off).

The difference between the two approaches above is that by only summing the lower nibbles, I am explicitly dealing with a carry from the lower nibble to the higher nibble (which is the point of the flag) instead of just basing it on the value of the result.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

James Flanagan
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
QuestionHalf carry flag Pin
Member 1499335715-Nov-20 1:29
MemberMember 1499335715-Nov-20 1:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.