Load8BitImmPCPlusSPIntoHL extends Instruction { @Override public void execute() { int programCounter = z80.getProgramCounter(); byte memoryValue = mmu.readByte(programCounter); z80.incrementProgramCounter(); int stackPointer = z80.getStackPointer(); int registerValue = (stackPointer + memoryValue) & 0xFFFF; z80.set16BitRegisterValue(Register.HL, registerValue); int check = stackPointer ^ (memoryValue) ^ ((stackPointer + memoryValue) & 0xFFFF); z80.resetFlagF(); if ((check & 0x100) == 0x100) { z80.enableFlagCY(); } if ((check & 0x10) == 0x10) { z80.enableFlagH(); } z80.setLastInstructionExecutionTime(3); } Load8BitImmPCPlusSPIntoHL(GBZ80 z80, MMU mmu); @Override void execute();  }
@Test public void shouldIncrementProgramCounter() { z80.setProgramCounter(ANY_16BIT_REGISTER_VALUE); Instruction instruction = new Load8BitImmPCPlusSPIntoHL(z80, mmu); instruction.execute(); assertEquals(ANY_16BIT_REGISTER_VALUE + 1, z80.getProgramCounter()); }
@Test public void shouldUse3CyclesAsLastInstructionExecutionTime() { Instruction instruction = new Load8BitImmPCPlusSPIntoHL(z80, mmu); instruction.execute(); assertEquals(3, z80.getLastInstructionExecutionTime()); }
@Test public void shouldLoadTheContentOfTheMemoryPointedByPCPlusTheSPIntoTheRegisterHL() { z80.setProgramCounter(ANY_16BIT_REGISTER_VALUE); when(mmu.readByte(ANY_16BIT_REGISTER_VALUE)).thenReturn(ANY_MEMORY_BYTE_VALUE); z80.setStackPointer(ANY_STACK_POINTER_VALUE); Instruction instruction = new Load8BitImmPCPlusSPIntoHL(z80, mmu); instruction.execute(); int expectedValue = ANY_STACK_POINTER_VALUE + ANY_MEMORY_BYTE_VALUE; assertEquals(expectedValue, z80.get16BitRegisterValue(Register.HL)); }
@Test public void shouldSetZFlagToZero() { Instruction instruction = new Load8BitImmPCPlusSPIntoHL(z80, mmu); instruction.execute(); assertEquals(false, z80.isFlagZEnabled()); }
@Test public void shouldSetFlagNToZero() { Instruction instruction = new Load8BitImmPCPlusSPIntoHL(z80, mmu); instruction.execute(); assertEquals(false, z80.isFlagNEnabled()); }