11

On the page 87 of the Game Boy CPU Manual it is claimed that the CP n instruction sets the carry flag when there was no borrow and that it means that A < n. This seems to conflict itself, because the carry flag is set when A > n.

An example: If A=0 and B=1, CP B sets the flags like SUB A, B, which is 0 - 1. This becomes 0 + 255 = 255 and the carry flag is not set, even though A < B.

I came across this same issue in other Z80 documents as well, so I don't believe this is a typo.

Am I misunderstanding how borrow and SUB work or is there something else going on? Is SUB not equal to ADD with two's complement in terms of flags?

Hassedev
  • 621
  • 6
  • 17

1 Answers1

14

The GameBoy CPU manual has it backwards. SUB, SBC and CP all set the carry flag when there is borrow. If SUB/SBC/CP A,n is executed then carry is set if n > A otherwise it is clear.

This is consistent with Z-80 (and 8080) operation. As well MAME and MESS implement carry the same way.

George Phillips
  • 4,564
  • 27
  • 25