Sbc8BitRegisterToAIntoA extends Instruction { @Override public void execute() { byte registerValue = z80.get8BitRegisterValue(sourceRegister); byte registerAValue = z80.get8BitRegisterValue(Register.A); int carry = z80.isFlagCYEnabled() ? 1 : 0; byte result = (byte) (registerAValue - registerValue - carry); z80.set8BitRegisterValue(Register.A, result); 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(); } } Sbc8BitRegisterToAIntoA(GBZ80 z80, Register sourceRegister); @Override void execute();  }
@Test public void shouldUseOneCycleAsLastInstructionExecutionTime() { Instruction instruction = new Sbc8BitRegisterToAIntoA(z80, ANY_8BIT_SOURCE_REGISTER); instruction.execute(); assertEquals(1, z80.getLastInstructionExecutionTime()); }
@Test public void shouldSubtractTheRegisterValuePlusCYFlagPlusAAndStoreTheResultIntoA() { z80.enableFlagCY(); z80.set8BitRegisterValue(ANY_8BIT_SOURCE_REGISTER, (byte) 1); z80.set8BitRegisterValue(Register.A, (byte) 3); Instruction instruction = new Sbc8BitRegisterToAIntoA(z80, ANY_8BIT_SOURCE_REGISTER); instruction.execute(); assertEquals(1, z80.get8BitRegisterValue(Register.A)); }