Skip to content

Instantly share code, notes, and snippets.

@Palmr
Last active September 3, 2022 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Palmr/4526839 to your computer and use it in GitHub Desktop.
Save Palmr/4526839 to your computer and use it in GitHub Desktop.
Subtract A, r instruction for the z80 in a gameboy
void Instructions::do8bitSubRegToA(CPU* cpu, BYTE (Registers::*getRegFunc)()){
BYTE lInitialValue = cpu->reg->getA();
BYTE lToSub = (cpu->reg->*getRegFunc)();
WORD lTotal = lInitialValue - lToSub;
// If the lower nibble of the original value is less than the lower nibble of what we're subtracting, it'll need a half carry
cpu->reg->setFlagH((lInitialValue & 0x0f) < (lToSub & 0x0f)? 1 : 0);
// If the original value is less than we're subtracting it'll carry
cpu->reg->setFlagC(lInitialValue < lToSub ? 1 : 0);
// Mask the total value that can fit in the register
cpu->reg->setFlagZ((lTotal & 0xff) ? 0 : 1);
cpu->reg->setFlagN(1);
cpu->reg->setA((BYTE)(lTotal & 0xff));
cpu->clock.m += 1;
cpu->clock.t += 4;
cpu->reg->incPC();
}
void Instructions::do8bitSubCRegToA(CPU* cpu, BYTE (Registers::*getRegFunc)()){
BYTE lInitialValue = cpu->reg->getA();
BYTE lToSub = (cpu->reg->*getRegFunc)() - cpu->reg->getFlagC();
WORD lTotal = lInitialValue - lToSub;
// If the lower nibble of the original value is less than the lower nibble of what we're subtracting, it'll need a half carry
cpu->reg->setFlagH((lInitialValue & 0x0f) < (lToSub & 0x0f)? 1 : 0);
// If the original value is less than we're subtracting it'll carry
cpu->reg->setFlagC(lInitialValue < lToSub ? 1 : 0);
// Mask the total value that can fit in the register
cpu->reg->setFlagZ((lTotal & 0xff) ? 0 : 1);
cpu->reg->setFlagN(1);
cpu->reg->setA((BYTE)(lTotal & 0xff));
cpu->clock.m += 1;
cpu->clock.t += 4;
cpu->reg->incPC();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment