Sub8BitRegisterToAIntoA extends Instruction { @Override public void execute() { byte sourceValue = z80.get8BitRegisterValue(sourceRegister); byte registerAValue = z80.get8BitRegisterValue(Register.A); byte result = (byte) (registerAValue - sourceValue); z80.set8BitRegisterValue(Register.A, result); z80.setLastInstructionExecutionTime(1); z80.resetFlagF(); z80.enableFlagN(); if (result == 0) { z80.enableFlagZ(); } if (((result & 0xF) < (sourceValue & 0xF))) { z80.enableFlagH(); } if ((result & 0xff) < (sourceValue & 0xff)) { z80.enableFlagCY(); } } Sub8BitRegisterToAIntoA(GBZ80 z80, Register sourceRegister); @Override void execute();  }
@Test public void shouldUseOneAsLastInstructionExecutionTime() { Instruction instruction = new Sub8BitRegisterToAIntoA(z80, ANY_8BIT_SOURCE_REGISTER); instruction.execute(); assertEquals(1, z80.getLastInstructionExecutionTime()); }
@Test public void shouldSubTheContentOfTheSourceRegisterAndStoreTheResultIntoA() { z80.set8BitRegisterValue(ANY_8BIT_SOURCE_REGISTER, ANY_MEMORY_BYTE_VALUE); z80.set8BitRegisterValue(Register.A, (byte) (ANY_MEMORY_BYTE_VALUE + 1)); Instruction instruction = new Sub8BitRegisterToAIntoA(z80, ANY_8BIT_SOURCE_REGISTER); instruction.execute(); assertEquals(1, z80.get8BitRegisterValue(Register.A)); }
@Test public void shouldEnableFlagZIfResultIsZero() { z80.set8BitRegisterValue(ANY_8BIT_SOURCE_REGISTER, ANY_MEMORY_BYTE_VALUE); z80.set8BitRegisterValue(Register.A, ANY_MEMORY_BYTE_VALUE); Instruction instruction = new Sub8BitRegisterToAIntoA(z80, ANY_8BIT_SOURCE_REGISTER); instruction.execute(); assertTrue(z80.isFlagZEnabled()); }
@Test public void shouldDisableFlagZIfResultIsNotZero() { z80.set8BitRegisterValue(ANY_8BIT_SOURCE_REGISTER, ANY_MEMORY_BYTE_VALUE); z80.set8BitRegisterValue(Register.A, (byte) (ANY_MEMORY_BYTE_VALUE + 1)); Instruction instruction = new Sub8BitRegisterToAIntoA(z80, ANY_8BIT_SOURCE_REGISTER); instruction.execute(); assertFalse(z80.isFlagZEnabled()); }
@Test public void shouldEnableFlagN() { Instruction instruction = new Sub8BitRegisterToAIntoA(z80, ANY_8BIT_SOURCE_REGISTER); instruction.execute(); assertTrue(z80.isFlagNEnabled()); }