public function runInterrupt()
{
$bitShift = 0;
$testbit = 1;
$interrupts = $this->memory[0xffff] & $this->memory[0xff0f];
while ($bitShift < 5) {
//Check to see if an interrupt is enabled AND requested.
if (($testbit & $interrupts) == $testbit) {
$this->IME = false;
//Reset the interrupt enabling.
$this->memory[0xff0f] -= $testbit;
//Reset the interrupt request.
//Set the stack pointer to the current program counter value:
$this->stackPointer = $this->unswtuw($this->stackPointer - 1);
$this->memoryWrite($this->stackPointer, $this->programCounter >> 8);
$this->stackPointer = $this->unswtuw($this->stackPointer - 1);
$this->memoryWrite($this->stackPointer, $this->programCounter & 0xff);
//Set the program counter to the interrupt's address:
$this->programCounter = 0x40 + $bitShift * 0x8;
//Interrupts have a certain clock cycle length:
$this->CPUTicks += 5;
//People say it's around 5.
break;
//We only want the highest priority interrupt.
}
$testbit = 1 << ++$bitShift;
}
}