Add8BitRegisterPlusAIntoA extends Instruction { @Override public void execute() { byte registerAValue = z80.get8BitRegisterValue(Register.A); byte sourceRegisterValue = z80.get8BitRegisterValue(sourceRegister); byte sum = (byte) (registerAValue + sourceRegisterValue); z80.set8BitRegisterValue(Register.A, sum); z80.setLastInstructionExecutionTime(1); z80.resetFlagF(); if (sum == 0) { z80.enableFlagZ(); } if ((sum & 0x100) == 0x100) { z80.enableFlagCY(); } if ((sum & 0x10) == 0x10) { z80.enableFlagH(); } } Add8BitRegisterPlusAIntoA(GBZ80 z80, Register sourceRegister); @Override void execute();  }
@Test public void shouldUseOneCycleAsLastInstructionExecutionTime() { Instruction instruction = new Add8BitRegisterPlusAIntoA(z80, ANY_8BIT_SOURCE_REGISTER); instruction.execute(); assertEquals(1, z80.getLastInstructionExecutionTime()); }
@Test(expected = IllegalArgumentException.class) public void shouldNotAccept16BitRegistersAsSourceRegister() { Instruction instruction = new Add8BitRegisterPlusAIntoA(z80, ANY_16BIT_SOURCE_REGISTER); instruction.execute(); }
@Test public void shouldSumSourceRegisterValuePlusARegisterValueAndUpdateA() { z80.set8BitRegisterValue(ANY_8BIT_SOURCE_REGISTER, ANY_8BIT_REGISTER_VALUE); z80.set8BitRegisterValue(Register.A, ANY_8BIT_REGISTER_VALUE); Instruction instruction = new Add8BitRegisterPlusAIntoA(z80, ANY_8BIT_SOURCE_REGISTER); instruction.execute(); byte expectedValue = ANY_8BIT_REGISTER_VALUE + ANY_8BIT_REGISTER_VALUE; assertEquals(expectedValue, z80.get8BitRegisterValue(Register.A)); }
@Test public void shouldEnableFlagZIfResultIsZero() { z80.set8BitRegisterValue(ANY_8BIT_SOURCE_REGISTER, (byte) 1); z80.set8BitRegisterValue(Register.A, (byte) -1); Instruction instruction = new Add8BitRegisterPlusAIntoA(z80, ANY_8BIT_SOURCE_REGISTER); instruction.execute(); assertTrue(z80.isFlagZEnabled()); }
@Test public void shouldDisableFlagZIfResultIsNotZero() { z80.set8BitRegisterValue(ANY_8BIT_SOURCE_REGISTER, (byte) 1); z80.set8BitRegisterValue(Register.A, (byte) 2); Instruction instruction = new Add8BitRegisterPlusAIntoA(z80, ANY_8BIT_SOURCE_REGISTER); instruction.execute(); assertFalse(z80.isFlagZEnabled()); }
@Test public void shouldDisableFlagN() { Instruction instruction = new Add8BitRegisterPlusAIntoA(z80, ANY_8BIT_SOURCE_REGISTER); instruction.execute(); assertFalse(z80.isFlagNEnabled()); }