コンマ

メモ代わりにアウトプットしています。何か不備がありましたら、お気軽にコメント頂けると有り難いです。

【nand2tetris】1章 ブール論理(Not, And, Or/Xor/マルチプレクサ/デマルチプレクサ)

TL;DR

O'Reilly Japan - コンピュータシステムの理論と実装 1章 ブール論理(Not, And, Or/Xor/マルチプレクサ/デマルチプレクサ)について勉強したことをメモ代わりに記載しています。 また、ソースコードこちらにおいてあります。

関連情報

www.oreilly.co.jp

概要

1章 ブール論理では、最終的に論理ゲートの一つであるNANDを使って、他の基本論理ゲートNot,And,Or,Xor,マルチプレクサ,デマルチプレクサHDL(ハードウェア記述言語)で作成するというものです。

Nandゲート

基本となるNandゲートは、入力a, bに対してa=b=1ならば、0を出力し、それ以外は1を出力します。

Notゲート

では、早速Notゲートから作成してみます。 Notゲートは、入力0ならば1を出力し、入力1ならば0を出力する(インバータ)ので、Nandゲートから作成するためには、Nandゲートを1回通して上げればいけそうです。

// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/01/Not.hdl

/**
 * Not gate:
 * out = not in
 */

CHIP Not {
    IN in;
    OUT out;

    PARTS:
    // Put your code here:
    Nand(a=in, b=in, out=out);
}

Andゲート

AndゲートはNandゲートと反対になるので、入力a, bに対してa=b=1ならば、1を出力し、それ以外は0を出力します。 つまり、And = Not(Nand(a, b))になります。 Notゲートは先程、Nandゲートから作成したので以下のように作成できそうです。

// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/01/And.hdl

/**
 * And gate: 
 * out = 1 if (a == 1 and b == 1)
 *       0 otherwise
 */

CHIP And {
    IN a, b;
    OUT out;

    PARTS:
    // Put your code here;
    Nand(a=a, b=b, out=w);
    Nand(a=w, b=w, out=out);
}

Orゲート

Orゲートは、入力a, bに対してa=b=0ならば、0を出力し、それ以外は1を出力します。
ソースコードは以下みたいにしました。

// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/01/Or.hdl

 /**
 * Or gate:
 * out = 1 if (a == 1 or b == 1)
 *       0 otherwise
 */

CHIP Or {
    IN a, b;
    OUT out;

    PARTS:
    // Put your code here:
    Nand(a=a, b=a, out=nota);
    Nand(a=b, b=b, out=notb);
    Nand(a=nota, b=notb, out=out);
}

Xorゲート

Xorゲートは入力a, bが互いに同じなら1を出力し、別ならば0を出力します。 ソースコードは以下みたいにしました。

// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/01/Xor.hdl

/**
 * Exclusive-or gate:
 * out = not (a == b)
 */

CHIP Xor {
    IN a, b;
    OUT out;

    PARTS:
    // Put your code here:
    Nand(a=a, b=b, out=w1);
    Nand(a=a, b=w1, out=w2);
    Nand(a=b, b=w1, out=w3);
    Nand(a=w2, b=w3, out=out);
}

マルチプレクサ

マルチプレクサは、入力a, b, selがあり、sel=0ならば出力がaになり、 sel=1ならば出力がbになります。 以下のようにしました。

// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/01/Mux.hdl

/** 
 * Multiplexor:
 * out = a if sel == 0
 *       b otherwise
 */

CHIP Mux {
    IN a, b, sel;
    OUT out;

    PARTS:
    // Put your code here:
    Nand(a=sel, b=sel, out=w1);
    Nand(a=a, b=w1, out=w2);
    Nand(a=sel, b=b, out=w3);
    Nand(a=w2, b=w3, out=out);
}

デマルチプレクサ

デマルチプレクサは、入力in, selに対して、sel=0ならば、a=in, b=0を出力し、sel=1ならば、a=0, b=inの出力をします。 以下のようにしました。

// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/01/DMux.hdl

/**
 * Demultiplexor:
 * {a, b} = {in, 0} if sel == 0
 *          {0, in} if sel == 1
 */

CHIP DMux {
    IN in, sel;
    OUT a, b;

    PARTS:
    // Put your code here:
    Nand(a=in, b=sel, out=w1);
    Nand(a=in, b=w1, out=w2);
    Nand(a=w2, b=w2, out=a);
    Nand(a=w1, b=w1, out=b);
}

一旦基本論理ゲートは完了したので、次回は 多ビットNot/And/Orゲート, 多ビットマルチプレクサ, 複数入力ゲートをやろうと思います。