Adc8BitRegisterPlusAIntoA extends Instruction { @Override public void execute() { byte registerValue = z80.get8BitRegisterValue(sourceRegister); byte registerAValue = z80.get8BitRegisterValue(Register.A); int carry = z80.isFlagCYEnabled() ? 1 : 0; byte sum = (byte) (registerAValue + registerValue + carry); z80.set8BitRegisterValue(Register.A, sum); z80.setLastInstructionExecutionTime(1); registerAValue = z80.get8BitRegisterValue(Register.A); z80.resetFlagF(); if (((registerAValue & 0xF) + (registerValue & 0xF) + carry) > 0xF) { z80.enableFlagH(); } if (((registerAValue & 0xff) + (registerValue & 0xff) + carry) > 0xFF) { z80.enableFlagCY(); } if (registerAValue == 0) { z80.enableFlagZ(); } } Adc8BitRegisterPlusAIntoA(GBZ80 z80, Register sourceRegister); @Override void execute();  }
@Test(expected = IllegalArgumentException.class) public void shouldNotAccept16BitRegistersAsSourceRegister() { Instruction instruction = new Adc8BitRegisterPlusAIntoA(z80, ANY_16BIT_DESTINY_REGISTER); instruction.execute(); }
@Test public void shouldUseOneCycleAsLastInstructionExecutionTime() { Instruction instruction = new Adc8BitRegisterPlusAIntoA(z80, ANY_8BIT_SOURCE_REGISTER); instruction.execute(); assertEquals(1, z80.getLastInstructionExecutionTime()); }
@Test public void shouldLoadTheContentOfTheSourceRegisterPlusCarryIntoA() { z80.enableFlagCY(); z80.set8BitRegisterValue(ANY_8BIT_SOURCE_REGISTER, ANY_8BIT_REGISTER_VALUE); z80.set8BitRegisterValue(Register.A, ANY_8BIT_REGISTER_VALUE); Instruction instruction = new Adc8BitRegisterPlusAIntoA(z80, ANY_8BIT_SOURCE_REGISTER); instruction.execute(); byte expectedSum = ANY_8BIT_REGISTER_VALUE + ANY_8BIT_REGISTER_VALUE + 1; assertEquals(expectedSum, z80.get8BitRegisterValue(Register.A)); }
@Test public void shouldEnableFlagZIfTheContentIsZero() { z80.enableFlagCY(); z80.set8BitRegisterValue(ANY_8BIT_SOURCE_REGISTER, (byte) 1); z80.set8BitRegisterValue(Register.A, (byte) -2); Instruction instruction = new Adc8BitRegisterPlusAIntoA(z80, ANY_8BIT_SOURCE_REGISTER); instruction.execute(); assertTrue(z80.isFlagZEnabled()); }