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 - Flag and Carry Instructions

Reply to topic
Author Message
Chris
  • Guest
Reply with quote
Flag and Carry Instructions
Post Posted: Mon Jul 05, 1999 11:24 pm
What does it mean to perform an instruction with a carry? Like there are shifts with carry,
add with carry, subtract with carry. But where is the carry value held!!?? Like if I
find an instruction like:

SHLC A

Shift Left w/ carry and send value to A. What do I have to do to carry? What is carry?
I was assuming this was right but I have a feeling it's wrong....

*Came Across a SHLC A instruction*

byte temp;
temp = z80.reg.flag_a;
z80.reg.flag_a << 7;
z80.reg.a << 1;
z80.reg.a += z80.reg.flag_a;
temp++;
z80.reg.flag_a = temp;

My code is something like that. I don't want to continue on and within 2 weeks I still can't
get my emu to run because of some stupid error like that. Please help.

Chris :o)
 
Eric
  • Guest
Reply with quote
Post Posted: Tue Jul 06, 1999 4:59 pm
The Z80 has a 'FLAGS' register called 'F.' It is the low 8-bits of register AF. The FLAGS register contains various flag values calculated during arithmetic/logic operations. One of these flags is the 'CARRY' flag (denoted C, bit position 0 in the FLAGS register.)

The CARRY flag is identical to the carry used when adding (or subtracting, in which case it's called a 'borrow') by hand. An example:

UNSIGNED addition:

00100101 11101100
+ 00101011 + 01110000
-------------------- --------------------
(0) 01010000 (1) 01011100

The CARRY bit is in parentheses. You can see that in the second example, the CARRY bit is set. This means that the result does not fit into 8-bits, it requires at least 9. (In SIGNED addition the OVERFLOW flag would also need to be examined to determin this fact. If the second example was SIGNED addition, the 8-bit result would be valid since OVERFLOW, in this case, is 0.) The Z80 programmer can use the CARRY flag, then, to string along 8-bit additions into an arbitary length addition. The ADC (ADD with CARRY) is provided exactly for this purpose. The same is true of SUB and SBC.

So, to answer your question, CARRY is simply held in bit postion 0 of the FLAGS register. When implementing ADC or SBC you must also account for the value of the CARRY bit. When shifting with carry, you must place the bit shifted out of the 8-bit register into the CARRY flag, and use the (old) CARRY flag as the bit shifted into the register.

Implementing the Z80 flags is tricky (I speak from experience.) I suggest you study-up on boolean arithmetic. A book on basic digital design should help. You should at least be familiar with the following if you are going to write your own Z80 emulator:

Carry
Overflow
2's Complement
Signed vs. Unsigned arithmetic.

These are just some of the things you really need to have a "working knowledge" of to successfully complete a Z80 emulator, simply knowing what they are is not sufficient. I suggest doing a few boolean arithmetic problems by hand, converting back and forth between decimal and binary, to check your results. The material is tricky, don't get discouraged. Good luck.

Eric
 
Eric
  • Guest
Reply with quote
Post Posted: Tue Jul 06, 1999 5:05 pm
It looks like the formatting got messed up on the examples. Sorry.

Let me try some HTML:


00100101 11101100
+ 00101011 + 01110000
------------------------------------------------------
(0) 01010000 (1) 01011100



I hope this works.

Eric
 
Chris
  • Guest
Reply with quote
Ahh! I see now!
Post Posted: Wed Jul 07, 1999 4:50 am
Quote
> It looks like the formatting got messed up on the examples. Sorry.

> Let me try some HTML:

>

> 00100101 11101100
> + 00101011 + 01110000
> ------------------------------------------------------
> (0) 01010000 (1) 01011100
>


>
> I hope this works.

The Carry flag is nothing more than an extra bit used in arithmetic. But wait, if the number overflows
does the overflow bit change to 1? Like if the programmer says:

LD HL, 255h
LD BC, 2h
ADC BC, HL

or even just
ADD BC, HL

From what you told me the carry bit would be set to one since the number is greater than the register
but would the overflow bit show? Also, is F the only flag register throught the whole processor? All
of the registers have to share that same flags register? I hope not or this is where the confusion comes
in like you said earlier. That's the only thing I hate about the Z80. Too many instructions and not enough
logic. Meanwhile you got a little wimpy processor like the m6502 who can handle loads of bullshit
like the Apple ][ and the NES.

Chris :o)
 
Eric
  • Guest
Reply with quote
Re: Ahh! I see now!
Post Posted: Wed Jul 07, 1999 4:48 pm
Quote
> The Carry flag is nothing more than an extra bit used in arithmetic. But wait, if the number overflows
> does the overflow bit change to 1?

Yes, but remember, OVERFLOW only has meaning when doing SIGNED operations.

Quote
> Like if the programmer says:

> LD HL, 255h
> LD BC, 2h
> ADC BC, HL

> or even just
> ADD BC, HL

> From what you told me the carry bit would be set to one since the number is greater than the register
> but would the overflow bit show?

In this case, no. 255 is not a valid 8-bit 2's complement number. In SIGNED addition, the bit pattern for 255 actually represents -1, so adding 2 and -1 yields 1, which easily fits into 8 bits, so no overflow.

Here's an example with overflow:

SIGNED ADDITION:

01111000 (8-bit 2's complement -> 120 decimal)
+ 00010000 (8-bit 2's complement -> 16 decimal)
---------------------
(0) 10001000 (8-bit 2's complement -> -112)


In 2's complement representation you can tell the SIGN of a number by the left-most bit: 0 is positive, 1 is negative. Notice that in the example above if we just look at the signs of the operands they are both postive, but the result has a 1, so it's negative. How can two postive numbers be added to get a negative result? They can't, OVERFLOW has occurred. If we look at the carry bit, we see that it's 0. Taking the CARRY bit, along with the 8-bit result, we get the correct answer, a positive 136. So in SIGNED addition OVERFLOW and CARRY are used together to determine the correct result. In UNSIGNED addition OVERFLOW has no meaning. It is the (Z80) programmer's responsiblity to understand whether he/she is using SIGNED or UNSIGNED arithmetic, and examine the OVERFLOW bit if necessary.

Again, there's a lot of material here, I can't explain it all. Finding a book on basic digital design is your best bet.

Quote
> Also, is F the only flag register throught the whole processor? All
> of the registers have to share that same flags register? I hope not or this is where the confusion comes
> in like you said earlier.

There is only one F register. The FLAGS always represent the status of the most recent arithmetic operation, regardless of which registers where used in the calculation.

Eric
 
Reply to topic



Back to the top of this page

Back to SMS Power!