Skip to content

Commit

Permalink
All instructions fixed and nes testrom completes with no errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Blake LaFleur committed Nov 9, 2015
1 parent 2a58ee7 commit c265ecd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 22 deletions.
18 changes: 17 additions & 1 deletion src/Bundle/Command/RunEmu.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,23 @@ protected function execute(InputInterface $input, OutputInterface $output)
$cpu->getMemory()->write(0x4007, 0xFF);
$cpu->getMemory()->write(0x4015, 0xFF);

$cpu->step();
$cpu->run();
$output->writeln("Execution completed successfully!");

$err1 = $cpu->getMemory()->read(0x0002);
$err2 = $cpu->getMemory()->read(0x0003);
if ($err1 != 0x00) {
$output->writeln(sprintf("There was an error @ 0x0002! %02X", $err1));
} else {
$output->writeln("0x0002 shows no errors!");
}


if ($err2 != 0x00) {
$output->writeln(sprintf("There was an error @ 0x0003 %02X", $err2));
} else {
$output->writeln("0x0003 shows no errors!");
}
}
}

26 changes: 14 additions & 12 deletions src/CPU.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,9 @@ private function generateOpMap()
/**
* Steps using <enter> until exception is thrown
*/
public function step()
public function run()
{
for(;;) {
$this->execute();
usleep(1000);
while (!$this->execute()) {
}
}

Expand All @@ -140,6 +138,11 @@ public function execute($opcode=null)

if ($this->DEBUG) {
printf("%d %04X %02X\t%s %04X\t%s\n", $this->instrCounter++, $this->registers->getPC(), $instruction->getOpcode(), $instruction->getName(), $value, $this->registers->toString());

if (($this->registers->getSP() == 0xFD && $instruction->getName() == 'RTS')
|| $instruction->getName() == 'BRK') {
return true;
}
}

$cycles+= $instruction->getCycles($this->pageFlag);
Expand Down Expand Up @@ -227,7 +230,6 @@ public function zeroPageIndex($mode)
$reg = $this->getRegisterFromAddressingMode($mode);
$mem = $this->memory->read($this->registers->getPC() + 1);

printf("%02X %02X %02X\n", $reg, $mem, ($mem+$reg) & 0xFF);
return ($mem + $reg) & 0xFF;
}

Expand Down Expand Up @@ -291,7 +293,6 @@ public function indexIndirect()

$low = $this->memory->read($value);
$high = $this->memory->read(($value + 1) & 0x00FF);
//printf("%02X %04X %02X %02X %04X\n", $mem, $value, $low, $high, (($high << 8) | $low));

return (($high << 8) | $low);
}
Expand Down Expand Up @@ -611,7 +612,7 @@ public function plp($address)

public function rla($address, $mode)
{
$this->asl($address, $mode);
$this->rol($address, $mode);
$this->andA($address);
}

Expand All @@ -635,14 +636,15 @@ public function rol($address, $mode)
public function ror($address, $mode)
{
if ($mode != InstructionSet::ADR_ACC) {
$origAddr = $address;
$address = $this->getMemory()->read($address);
$value = $this->getMemory()->read($address);
} else {
$value = $address;
}

$shifted = $this->rotateRight($address);
$shifted = $this->rotateRight($value);

if ($mode != InstructionSet::ADR_ACC) {
$this->getMemory()->write($origAddr, $shifted);
$this->getMemory()->write($address, $shifted);
} else {
$this->registers->setA($shifted);
}
Expand Down Expand Up @@ -795,7 +797,7 @@ private function rotateLeft($value)
private function rotateRight($value)
{
$bit7 = $value & Registers::C;
$shifted = ($value >> 1 & 0x7F) | ($this->registers->getStatus(Registers::C) ? 0x80 : 0x00);
$shifted = (($value >> 1) & 0x7F) | ($this->registers->getStatus(Registers::C) ? 0x80 : 0x00);
$this->registers->setStatusBit(Registers::C, $bit7);
$this->registers->setSign($shifted);
$this->registers->setZero($shifted);
Expand Down
18 changes: 9 additions & 9 deletions src/Instructions/CPU/InstructionSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,13 @@ public static function createDefault()

$set->addInstruction(new Instruction('ROL', 0x2A, self::ADR_ACC, 1, 2));
$set->addInstruction(new Instruction('ROL', 0x26, self::ADR_ZP, 2, 5));
$set->addInstruction(new Instruction('ROL', 0x36, self::ADR_ZPY, 2, 6));
$set->addInstruction(new Instruction('ROL', 0x36, self::ADR_ZPX, 2, 6));
$set->addInstruction(new Instruction('ROL', 0x2E, self::ADR_ABS, 3, 6));
$set->addInstruction(new Instruction('ROL', 0x3E, self::ADR_ABSX, 3, 7));

$set->addInstruction(new Instruction('ROR', 0x6A, self::ADR_ACC, 1, 2));
$set->addInstruction(new Instruction('ROR', 0x66, self::ADR_ZP, 2, 5));
$set->addInstruction(new Instruction('ROR', 0x76, self::ADR_ZPY, 2, 6));
$set->addInstruction(new Instruction('ROR', 0x76, self::ADR_ZPX, 2, 6));
$set->addInstruction(new Instruction('ROR', 0x6E, self::ADR_ABS, 3, 6));
$set->addInstruction(new Instruction('ROR', 0x7E, self::ADR_ABSX, 3, 7));

Expand Down Expand Up @@ -203,7 +203,7 @@ public static function createDefault()
$set->addInstruction(new Instruction('PLP', 0x28, self::ADR_IMP, 1, 4));

$set->addInstruction(new Instruction('STX', 0x86, self::ADR_ZP, 2, 3));
$set->addInstruction(new Instruction('STX', 0x96, self::ADR_ZPX, 2, 4));
$set->addInstruction(new Instruction('STX', 0x96, self::ADR_ZPY, 2, 4));
$set->addInstruction(new Instruction('STX', 0x8E, self::ADR_ABS, 3, 4));

$set->addInstruction(new Instruction('STY', 0x84, self::ADR_ZP, 2, 3));
Expand Down Expand Up @@ -265,7 +265,7 @@ public static function createDefault()

//DCP
$set->addInstruction(new Instruction('*DCP', 0xC7, self::ADR_ZP, 2, 5));
$set->addInstruction(new Instruction('*DCP', 0xD7, self::ADR_ZPY, 2, 6));
$set->addInstruction(new Instruction('*DCP', 0xD7, self::ADR_ZPX, 2, 6));
$set->addInstruction(new Instruction('*DCP', 0xCF, self::ADR_ABS, 3, 6));
$set->addInstruction(new Instruction('*DCP', 0xDF, self::ADR_ABSX, 3, 7));
$set->addInstruction(new Instruction('*DCP', 0xDB, self::ADR_ABSY, 3, 7));
Expand All @@ -274,7 +274,7 @@ public static function createDefault()

//ISB
$set->addInstruction(new Instruction('*ISB', 0xE7, self::ADR_ZP, 2, 5));
$set->addInstruction(new Instruction('*ISB', 0xF7, self::ADR_ZPY, 2, 6));
$set->addInstruction(new Instruction('*ISB', 0xF7, self::ADR_ZPX, 2, 6));
$set->addInstruction(new Instruction('*ISB', 0xEF, self::ADR_ABS, 3, 6));
$set->addInstruction(new Instruction('*ISB', 0xFF, self::ADR_ABSX, 3, 7));
$set->addInstruction(new Instruction('*ISB', 0xFB, self::ADR_ABSY, 3, 7));
Expand All @@ -283,7 +283,7 @@ public static function createDefault()

//SLO
$set->addInstruction(new Instruction('*SLO', 0x07, self::ADR_ZP, 2, 5));
$set->addInstruction(new Instruction('*SLO', 0x17, self::ADR_ZPY, 2, 6));
$set->addInstruction(new Instruction('*SLO', 0x17, self::ADR_ZPX, 2, 6));
$set->addInstruction(new Instruction('*SLO', 0x0F, self::ADR_ABS, 3, 6));
$set->addInstruction(new Instruction('*SLO', 0x1F, self::ADR_ABSX, 3, 7));
$set->addInstruction(new Instruction('*SLO', 0x1B, self::ADR_ABSY, 3, 7));
Expand All @@ -292,7 +292,7 @@ public static function createDefault()

//RLA
$set->addInstruction(new Instruction('*RLA', 0x27, self::ADR_ZP, 2, 5));
$set->addInstruction(new Instruction('*RLA', 0x37, self::ADR_ZPY, 2, 6));
$set->addInstruction(new Instruction('*RLA', 0x37, self::ADR_ZPX, 2, 6));
$set->addInstruction(new Instruction('*RLA', 0x2F, self::ADR_ABS, 3, 6));
$set->addInstruction(new Instruction('*RLA', 0x3F, self::ADR_ABSX, 3, 7));
$set->addInstruction(new Instruction('*RLA', 0x3B, self::ADR_ABSY, 3, 7));
Expand All @@ -301,7 +301,7 @@ public static function createDefault()

//SRE
$set->addInstruction(new Instruction('*SRE', 0x47, self::ADR_ZP, 2, 5));
$set->addInstruction(new Instruction('*SRE', 0x57, self::ADR_ZPY, 2, 6));
$set->addInstruction(new Instruction('*SRE', 0x57, self::ADR_ZPX, 2, 6));
$set->addInstruction(new Instruction('*SRE', 0x4F, self::ADR_ABS, 3, 6));
$set->addInstruction(new Instruction('*SRE', 0x5F, self::ADR_ABSX, 3, 7));
$set->addInstruction(new Instruction('*SRE', 0x5B, self::ADR_ABSY, 3, 7));
Expand All @@ -310,7 +310,7 @@ public static function createDefault()

//RRA
$set->addInstruction(new Instruction('*RRA', 0x67, self::ADR_ZP, 2, 5));
$set->addInstruction(new Instruction('*RRA', 0x77, self::ADR_ZPY, 2, 6));
$set->addInstruction(new Instruction('*RRA', 0x77, self::ADR_ZPX, 2, 6));
$set->addInstruction(new Instruction('*RRA', 0x6F, self::ADR_ABS, 3, 6));
$set->addInstruction(new Instruction('*RRA', 0x7F, self::ADR_ABSX, 3, 7));
$set->addInstruction(new Instruction('*RRA', 0x7B, self::ADR_ABSY, 3, 7));
Expand Down

0 comments on commit c265ecd

Please sign in to comment.