Skip to content

Commit

Permalink
More CPU operation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Blake LaFleur committed Nov 5, 2015
1 parent c7591ad commit 3d6168c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/CPU.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ public function indexIndirect()
$low = $this->memory->read($adr);
$high = $this->memory->read(($adr + 1) & 0x00FF);

return (($high << 8) & 0xFF) | $low;
$result = (($high << 8) & 0xFF) | $low;
return $result;
}


Expand Down Expand Up @@ -600,7 +601,7 @@ public function rts($address)
public function sbc($address)
{
$value = $this->registers->getA() - $this->getMemory()->read($address);
$value = $value - (~$this->registers->getStatus(Registers::c));
$value = $value - (1 - $this->registers->getStatus(Registers::C));
$this->registers->setOverflow($value);
$this->registers->setCarry($value);
$this->registers->setSign($value);
Expand Down
75 changes: 75 additions & 0 deletions test/unit/CpuTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,81 @@ public function testRtsWillSetPCCorrectly()
$this->assertEquals(0x0808, $this->cpu->getRegisters()->getPC());
}

public function testSetCarryAndSubtractGivesCorrectResult()
{
$this->cpu->getRegisters()->setPC(0xFFF0);
$this->cpu->execute(0x38);
$this->assertTrue($this->cpu->getRegisters()->getStatus(Registers::C));
$this->cpu->getRegisters()->setA(0x05);
$this->cpu->getRegisters()->setX(0x05);
$this->cpu->getMemory()->write($this->cpu->getRegisters()->getPC() + 1, 0xFF);
$this->cpu->getMemory()->write($this->cpu->getRegisters()->getPC(), 0xF0);
$this->cpu->getMemory()->write(0xFFF5, 0x05);
$this->cpu->execute(0xFD); //SBC absInx
$this->assertEquals(0x00, $this->cpu->getRegisters()->getA());
$this->assertTrue($this->cpu->getRegisters()->getStatus(Registers::Z));
}

public function testSedSetsDecimalModeFlag()
{
$this->cpu->getRegisters()->setStatusBit(Registers::D, 0);
$this->cpu->execute(0xF8);
$this->assertTrue($this->cpu->getRegisters()->getStatus(Registers::D));
}

public function testSeiSetsDecimalModeFlag()
{
$this->cpu->getRegisters()->setStatusBit(Registers::I, 0);
$this->cpu->execute(0x78);
$this->assertTrue($this->cpu->getRegisters()->getStatus(Registers::I));
}

public function testStaWillStoreACorrectlyWithIndirectIndex()
{
$this->cpu->getMemory()->write($this->cpu->getRegisters()->getPC(), 0x05);
$this->cpu->getMemory()->write(0x05, 0xF0);
$this->cpu->getRegisters()->setA(0x05);
$this->cpu->getRegisters()->setY(0x05);
$this->cpu->execute(0x91);
$this->assertEquals(0x05, $this->cpu->getMemory()->read(0xF5));
}

public function testStaWillStoreACorrectlyWithIndexIndirect()
{
$this->cpu->getMemory()->write($this->cpu->getRegisters()->getPC(), 0x10);
$this->cpu->getRegisters()->setX(0x02);
$this->cpu->getMemory()->write(0x12, 0x05);
$this->cpu->getRegisters()->setA(0x05);
$this->cpu->execute(0x81);
$this->assertEquals(0x05, $this->cpu->getMemory()->read(0x12));
}

public function testStxWillStoreXCorrectly()
{
$this->cpu->getMemory()->write($this->cpu->getRegisters()->getPC() + 1, 0xFF);
$this->cpu->getMemory()->write($this->cpu->getRegisters()->getPC(), 0xF0);
$this->cpu->getRegisters()->setX(0x05);
$this->cpu->execute(0x8E);
$this->assertEquals(0x05, $this->cpu->getMemory()->read(0xFFF0));
}

public function testStyWillStoreYCorrectly()
{
$this->cpu->getMemory()->write($this->cpu->getRegisters()->getPC() + 1, 0xFF);
$this->cpu->getMemory()->write($this->cpu->getRegisters()->getPC(), 0xF0);
$this->cpu->getRegisters()->setY(0x05);
$this->cpu->execute(0x8C);
$this->assertEquals(0x05, $this->cpu->getMemory()->read(0xFFF0));
}

/**
* @expectedException Exception
*/
public function testInvalidOpcodeWillThrowExeption()
{
$this->cpu->execute(0xFF);
}

protected function setUp()
{
$this->cpu = new CPU();
Expand Down

0 comments on commit 3d6168c

Please sign in to comment.