Negating 80h on the Z80

Is anyone set up to test the original, discontinued Z80 still? On page 176 of the official user’s manual on the NEG instruction, I found this note:

The 80h address remains unchanged

which I think is trying to say that if the value in the accumulator is 80h, then NEG doesn’t change it. That would be -128 in two’s complement and +128 would be out of range. I don’t see any address involved here. Would be nice if we could verify the flags affected as well.

The last time this document was updated was Aug 2016, and most of the customer support links are now dead. Who here is actively maintaining Z80 documentation?

I know rather little about Z80 internals, and I don’t know the definitive answer on this, but the behavior would make sense: if NEG does a ones’ complement and then increments by one for two’s complement, 80h should overflow to its original state.

1000.0000 -> 0111.1111 + 1 => 1000.0000

Meaning, this isn’t necessarily a special case or exception, but rather a consequence of normal operations.

If so, flags should be set accordingly (as normal). Notably, P/V is set, if the accumulator was 80h before execution, so this state is detectable.

Yeah, no point in adding special circuitry to detect a special case if you get the correct behavior for free.

The funny thing being, there kind of is, for the set/reset of P/V flag necessitates that 80h should be detected. :slight_smile:

(Notably, the parity of 1000.0000 is odd, meaning, normally P/V should be unset. From the description in the manual follows that P/V is only set, if the accumulator was 80h before, which requires some circuitry.)

Not sure what you mean by that. I could always just test if the accumulator is 80h.

P/V is set if Accumulator was 80h before operation; otherwise, it is reset.

Z80 CPU User Manual, 2016; p. 176

Oh, that flag also serves double duty as the overflow flag. If you think of NEG as subtracting the accumulator from zero, then 80h is the only value which can underflow/overflow. I think that’s how we should be interpreting the P/V flag for NEG.

Oh, so, in the end, it’s just a subtraction from zero.

(I was thinking of older examples, like instructions to complement the contents of the accumulater and making this two’s complement, but, in actuallity, it may be just a shortcut implementation of standard arithmetic.)

For one’s complement you want CPL on page 175.