evandotpro/php-computer

使用 OOP PHP 实现的低级计算概念的完全无用的库。

dev-master 2015-02-12 01:17 UTC

This package is not auto-updated.

Last update: 2024-09-10 02:08:40 UTC


README

Build Status

这个项目是拖延症与我对计算机底层组件理解提升的欲望相结合的结果。简单来说,这是一个计算机底层组件的 OOP PHP 表示。我可能会根据我的学习和理解添加更多组件,但我乐于接受 pull requests。

除了可能具有娱乐或教育价值之外,这个项目并不旨在有任何实际用途。然而,它并不一定旨在帮助 教授 它所表示的概念,而且可能更适用于那些已经对计算机底层工作原理有基本了解的人。为了更好地理解这些概念,我强烈推荐 J. Clark Scott 所著的书籍 But How Do It Know

注意:虽然这可以发展成为 PHP 的硬件仿真层(也许吧),但这并不是这个项目的目标。在撰写此内容时,我从未看过一行硬件虚拟化代码,而且尝试用 PHP 做这样的事情简直就是愚蠢。别再想这种糟糕的事情了。

哦,如果你喜欢这个,你可能也会喜欢 JointJS 的逻辑电路

安装

通过 Composer

{
    "require": {
        "evandotpro/php-computer": "dev-master"
    }
}

用法

比特

在计算机中,一切都是由 比特 组成的。比特只是一个信息单元(电路),可以是开(1)或关(0)。就是这样。因此,我们有一个比特

<?php
use Edp\PhpComputer\Bit;

$bit = new Bit;
$bit->on();    // turns the bit on (default state is off)
$bit->off();   // turns the bit off
$bit->state(); // returns the current state of the bit (1 for on, 0 for off)

你可以将比特视为一段导线,要么有电荷,要么没有电荷。

现在,由于我们将将这些比特连接起来,这些比特在状态改变时需要被通知,所以我们利用 观察者模式。比特是一个“被观察者”,因此,观察者可以“附加”到比特上,以便在它改变状态时得到通知

<?php
use Edp\PhpComputer\Bit;

$bit = new Bit;

$observer = new SomeObserver; // must implement Edp\PhpComputer\Util\ObserverInterface, which simply has an update() method.
$bit->attach($observer);

$bit->on(); // if the bit was off (it is by default), it is turned on, and $observer->update() is called

$bit->detach($observer); // You can also detach an observer if it is no longer "connected" to that bit.

与非门

与非门 是构成现代电子设备和 CPU 的许多 逻辑门 之一。与非门很重要,因为技术上,你可以使用一个或多个与非门实现 所有 可能的计算机操作(它们具有 功能完备性)。

简单来说,与非门是一个非常基本的电气电路,有两个输入和一个输出。如果两个输入都打开,则输出关闭。任何其他组合(两个输入都关闭或一个打开而另一个关闭)都会使输出打开。(这就是你需要知道的一切,但如果你想知道这是如何从电子上实现的,与非门还具有自己的恒定电源,除了输入外。两个输入的电源作用于晶体管,以将与非门的输出连接到地或电源。)

与非门可以用以下真值表表示

<?php
use Edp\PhpComputer\Bit;
use Edp\PhpComputer\LogicGate\Nand;

$input1 = new Bit; // off by default
$input2 = new Bit; // off by default

$nand = new Nand;
$nand->attachInputs($input1, $input2);

var_dump($nand->state()) // int(1) (on) because both inputs are off.

$input1->on();
$input2->on();

var_dump($nand->state()) // int(0) (off) because both inputs are on.

但是等等,还有更多!因为NAND门输出一位,所以Nand类实际上也是一个Bit(它扩展了Bit类)。这意味着我们可以将多个NAND门连接起来!

<?php
use Edp\PhpComputer\Bit;
use Edp\PhpComputer\LogicGate\Nand;

$input1 = new Bit; // off by default
$input2 = new Bit; // off by default

$nand1 = new Nand;
$nand1->attachInputs($input1, $input2);

$nand2 = new Nand;
$nand2->attachInputs($input1, $nand1); // Now this NAND gate will get input from the $input1 Bit and the output bit of $nand1. Neat, huh?

现在你可以开始做一些真正酷的事情。例如,仅使用四个NAND门就创建一个持久RAM位!

在最基本的层面上,RAM是这样工作的:你有一根输入线和一个“设置”线,还有一个总是代表存储位的输出线。要存储一个位,你可以将输入线打开(1)或关闭(0),然后短暂地将设置线“打开”,这告诉电路“锁定”输入线的任何状态。

以下是使用NAND门实现的样子

<?php
use Edp\PhpComputer\Bit;
use Edp\PhpComputer\LogicGate\Nand;

// Create the input and set "wires" (bits)
$setBit   = new Bit;
$inputBit = new Bit;

// Wire up our NAND gates to make some RAM!
$nand1 = Nand;
$nand1->attachInputs($inputBit, $setBit);

$nand2 = new Nand;
$nand2->attachInputs($nand1, $setBit);

$nand3 = new Nand;
$nand4 = new Nand;

$nand3->attachInputs($nand1, $nand4);
$nand4->attachInputs($nand2, $nand3);

// Now we can use it...

$inputBit->on(); // We are going to store a '1'

$setBit->on()->off(); // Momentarily flip the "set" bit on to store the input bit in our RAM circuit.

var_dump($nand3->state()); // int(1)

$inputBit->off(); // Now we are going to update it to '0'... the RAM is not updated to match the input until we flip the set bit on again...
$setBit->on()->off(); // Momentarily flip the "set" bit on to store the input bit in our RAM circuit.

var_dump($nand3->state()); // int(0)

$inputBit->on();

var_dump($nand3->state()); // Still int(0) because the "set" bit is off, thus the input bit is having no effect on our RAM circuit

// As long as the "set" bit is on, $nand3's state will always mirror the input bit.
$setBit->on();
$inputBit->on();
var_dump($nand3->state()); // int(1)
$inputBit->off();
var_dump($nand3->state()); // int(0)

内存位

正如我刚才演示的那样,你可以通过组合4个NAND门来创建单个位持久RAM。这需要很多输入,所以有一个MemoryBit类可以为你构建这个电路。

<?php
use Edp\PhpComputer\MemoryBit;

$memory = new MemoryBit;
var_dump($memory->read()); // int(1)

$memory->write(0);
var_dump($memory->read()); // int(0)

测试

$ phpunit

贡献

请参阅贡献指南以获取详细信息。

致谢

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件