Adc8BitHLAddressPlusAIntoA extends Instruction { @Override public void execute() { int address = z80.get16BitRegisterValue(Register.HL); byte memoryValue = mmu.readByte(address); byte registerAValue = z80.get8BitRegisterValue(Register.A); int carry = z80.isFlagCYEnabled() ? 1 : 0; byte sum = (byte) (registerAValue + memoryValue + carry); z80.set8BitRegisterValue(Register.A, sum); z80.incrementProgramCounter(); z80.setLastInstructionExecutionTime(2); registerAValue = z80.get8BitRegisterValue(Register.A); z80.resetFlagF(); if (((registerAValue & 0xF) + (memoryValue & 0xF) + carry) > 0xF) { z80.enableFlagH(); } if (((registerAValue & 0xff) + (memoryValue & 0xff) + carry) > 0xFF) { z80.enableFlagCY(); } if (registerAValue == 0) { z80.enableFlagZ(); } } Adc8BitHLAddressPlusAIntoA(GBZ80 z80, MMU mmu); @Override void execute();  }
@Test public void shouldUseTwoCycleAsLastInstructionExecutionTime() { Instruction instruction = new Adc8BitHLAddressPlusAIntoA(z80, mmu); instruction.execute(); assertEquals(2, z80.getLastInstructionExecutionTime()); }
@Test public void shouldLoadTheContentOfHLAddressPlusCarryPlusAIntoA() { z80.enableFlagCY(); z80.set16BitRegisterValue(Register.HL, ANY_16BIT_REGISTER_VALUE); when(mmu.readByte(ANY_16BIT_REGISTER_VALUE)).thenReturn((byte) 0); z80.set8BitRegisterValue(Register.A, (byte) -2); Instruction instruction = new Adc8BitHLAddressPlusAIntoA(z80, mmu); instruction.execute(); assertEquals(-1, z80.get8BitRegisterValue(Register.A)); }
@Test public void shouldEnableFlagZIfTheContentIsZero() { z80.enableFlagCY(); z80.set16BitRegisterValue(Register.HL, ANY_16BIT_REGISTER_VALUE); when(mmu.readByte(ANY_16BIT_REGISTER_VALUE)).thenReturn((byte) 1); z80.set8BitRegisterValue(Register.A, (byte) -2); Instruction instruction = new Adc8BitHLAddressPlusAIntoA(z80, mmu); instruction.execute(); assertTrue(z80.isFlagZEnabled()); }