diff --git a/src/Cpu.php b/src/Cpu.php index 7109231..03e9759 100644 --- a/src/Cpu.php +++ b/src/Cpu.php @@ -28,11 +28,11 @@ class Cpu { } public function popU8(): int { - return $this->io->read($this->state->sPage | $this->state->s--); + return $this->io->read($this->state->sPage | $this->state->s++); } public function pushU8(int $value): void { - $this->io->write($this->state->sPage | $this->state->s++, $value); + $this->io->write($this->state->sPage | $this->state->s--, $value); } public function popU16(): int { @@ -64,13 +64,19 @@ class Cpu { return; } - $vector = match($irq) { - CpuInterruptRequest::Maskable => self::IV_IRQ, - CpuInterruptRequest::NonMaskable => self::IV_NMI, - default => throw new RuntimeException('unexpected irq value') - }; + $pc = $this->state->pc; - $this->pushU16($this->state->pc); + if($irq === CpuInterruptRequest::Break) { + $vector = self::IV_IRQ; + ++$pc; + } else + $vector = match($irq) { + CpuInterruptRequest::Maskable => self::IV_IRQ, + CpuInterruptRequest::NonMaskable => self::IV_NMI, + default => throw new RuntimeException('unexpected irq value') + }; + + $this->pushU16($pc); $this->pushU8($this->state->p); $this->state->pc = $this->readIoU16($vector); }