Created
March 2, 2022 20:58
-
-
Save megakode/4edf7137bb0420c784898fd200e8c4ea to your computer and use it in GitHub Desktop.
Gameboy DAA instruction
This file contains 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
// DAA | |
// opcode: 0x27 | |
// cycles: 4 | |
// flags: Z H C | |
void CPU::daa(){ | |
if (!getFlag(FlagBitmaskN)) { // after an addition, adjust if (half-)carry occurred or if result is out of bounds | |
if (getFlag(FlagBitmaskC) || regs.A > 0x99) { regs.A += 0x60; setFlag(FlagBitmaskC,true); } | |
if (getFlag(FlagBitmaskHalfCarry) || (regs.A & 0x0f) > 0x09) { regs.A += 0x6; } | |
} else { // after a subtraction, only adjust if (half-)carry occurred | |
if (getFlag(FlagBitmaskC)) { regs.A -= 0x60; } | |
if (getFlag(FlagBitmaskHalfCarry)) { regs.A -= 0x6; } | |
} | |
setFlag(FlagBitmaskZero, (regs.A == 0) ); // the usual z flag | |
setFlag(FlagBitmaskHalfCarry,false); // h flag is always cleared |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment