Last active
September 3, 2022 16:12
-
-
Save Palmr/4526839 to your computer and use it in GitHub Desktop.
Subtract A, r instruction for the z80 in a gameboy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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