59 lines
1.1 KiB
PHP
59 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace Automata;
|
|
|
|
use Automata\Memory\MemoryController;
|
|
use Automata\Memory\ProgramMemory;
|
|
use Automata\Memory\ToolMemory;
|
|
|
|
class Machine
|
|
{
|
|
public DataRegisters $DR;
|
|
public InstructionSet $IS;
|
|
public ToolMemory $TM;
|
|
public ProgramMemory $SPM;
|
|
public ProgramMemory $PPM;
|
|
public MemoryController $SPMC;
|
|
public MemoryController $PPMC;
|
|
public ?MemoryController $AMC;
|
|
|
|
|
|
public function __construct()
|
|
{
|
|
$this->DR = new DataRegisters();
|
|
$this->IS = new InstructionSet($this);
|
|
$this->TM = new ToolMemory();
|
|
$this->SPM = new ProgramMemory();
|
|
$this->PPM = new ProgramMemory();
|
|
$this->SPMC = new MemoryController($this->SPM);
|
|
$this->PPMC = new MemoryController($this->PPM);
|
|
$this->AMC = $this->PPMC;
|
|
}
|
|
|
|
|
|
public function loadProgram(int $N): void
|
|
{
|
|
$this->PPMC->loadProgram($N);
|
|
$this->AMC = $this->PPMC;
|
|
}
|
|
|
|
|
|
public function step(): void
|
|
{
|
|
$block = $this->AMC->next();
|
|
if (is_null($block) && $this->AMC === $this->SPMC) {
|
|
// SP ended, back to PP
|
|
$this->AMC = $this->PPMC;
|
|
$block = $this->AMC->next();
|
|
}
|
|
$this->IS->apply($block);
|
|
}
|
|
|
|
|
|
public function reset(): void
|
|
{
|
|
$this->PPMC->reset();
|
|
$this->SPMC->reset();
|
|
}
|
|
}
|