45 lines
		
	
	
		
			900 B
		
	
	
	
		
			PHP
		
	
	
	
	
	
		
		
			
		
	
	
			45 lines
		
	
	
		
			900 B
		
	
	
	
		
			PHP
		
	
	
	
	
	
|  | <?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; | ||
|  |     } | ||
|  | } |