First commit
This commit is contained in:
44
src/ProgramCounter.php
Normal file
44
src/ProgramCounter.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace SimAVRPHP;
|
||||
|
||||
use SimAVRPHP\Exceptions\InvalidAddressException;
|
||||
|
||||
class ProgramCounter
|
||||
{
|
||||
private const int RESET_PC_VALUE = 0;
|
||||
|
||||
private int $programCounter = self::RESET_PC_VALUE;
|
||||
|
||||
public function get(): int
|
||||
{
|
||||
return $this->programCounter;
|
||||
}
|
||||
|
||||
public function jump(int $address): void
|
||||
{
|
||||
if (($address % 2) != 0) {
|
||||
throw new InvalidAddressException("Invalid address", $address);
|
||||
}
|
||||
|
||||
$this->programCounter = $address;
|
||||
}
|
||||
|
||||
public function relativeJump(int $address): void
|
||||
{
|
||||
$this->jump($this->programCounter + $address);
|
||||
}
|
||||
|
||||
public function stepWords(int $word): int
|
||||
{
|
||||
$this->programCounter += ($word << 1);
|
||||
return $this->programCounter;
|
||||
}
|
||||
|
||||
public function reset(): void
|
||||
{
|
||||
$this->programCounter = self::RESET_PC_VALUE;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user