LoginSignup
4
1

More than 5 years have passed since last update.

[Nand2Tetris]第2章 プール算術

Last updated at Posted at 2017-05-03

はじめに

CourseraのNand2Tetrisコースをみて、「コンピュータシステムの理論と実装」よみまとめてます。

2章 プール演算

本性は、論理ゲートを用いて算術論理演算機(ALU)を実装することが目的である。
コンピュータによって、行われる算術演算と論理演算はすべてこのALU回路によって行われている。
ALUを実装するために

  • HalfAdder
  • FullAdder
  • Increment

を実装する必要がある。

加算機

半加算機 (HalfAdder)

2進数の加算を行うためのはじめの一歩。二つのビットに対して、加算を行う。

入力 出力
a b carry sum
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 0

この操作を行うHDLは以下のようになる。

CHIP HalfAdder {
    IN a, b;    // 1-bit inputs
    OUT sum,    // Right bit of a + b 
        carry;  // Left bit of a + b

    PARTS:
    Xor(a=a, b=b, out=sum);
    And(a=a, b=b, out=carry);
}

全加算器 (FullAdder)

全加算器とは、3つのビットわを求める加算器です。全加算器の出力も2本有り、それぞれ「和の最下位ビット」とキャリービット」である。

実装は上でHalfAdderを活用する。


CHIP FullAdder {
    IN a, b, c;  // 1-bit inputs
    OUT sum,     // Right bit of a + b + c
        carry;   // Left bit of a + b + c

    PARTS:
    HalfAdder(a=a, b=b, sum=sum1, carry=carry1);
    HalfAdder(a=sum1, b=c, sum=sum, carry=carry2);
    Or(a=carry1, b=carry2, out=carry);
}

16ビット加算器

ここで16ビット加算器を実装する。

CHIP Add16 {
    IN a[16], b[16];
    OUT out[16];

    PARTS:
   // Put you code here:
   HalfAdder(a=a[0], b=b[0], sum=out[0], carry=carry0);
   FullAdder(a=a[1], b=b[1], c=carry0, sum=out[1], carry=carry1);
   FullAdder(a=a[2], b=b[2], c=carry1, sum=out[2], carry=carry2);
   FullAdder(a=a[3], b=b[3], c=carry2, sum=out[3], carry=carry3);
   FullAdder(a=a[4], b=b[4], c=carry3, sum=out[4], carry=carry4);
   FullAdder(a=a[5], b=b[5], c=carry4, sum=out[5], carry=carry5);
   FullAdder(a=a[6], b=b[6], c=carry5, sum=out[6], carry=carry6);
   FullAdder(a=a[7], b=b[7], c=carry6, sum=out[7], carry=carry7);
   FullAdder(a=a[8], b=b[8], c=carry7, sum=out[8], carry=carry8);
   FullAdder(a=a[9], b=b[9], c=carry8, sum=out[9], carry=carry9);
   FullAdder(a=a[10], b=b[10], c=carry9, sum=out[10], carry=carry10);
   FullAdder(a=a[11], b=b[11], c=carry10, sum=out[11], carry=carry11);
   FullAdder(a=a[12], b=b[12], c=carry11, sum=out[12], carry=carry12);
   FullAdder(a=a[13], b=b[13], c=carry12, sum=out[13], carry=carry13);
   FullAdder(a=a[14], b=b[14], c=carry13, sum=out[14], carry=carry14);
   FullAdder(a=a[15], b=b[15], c=carry14, sum=out[15], carry=carry15);
}

インクリメンタ

与えられた数字に1を加算することができる回路。

CHIP Inc16 {
    IN in[16];
    OUT out[16];

    PARTS:
   // Put you code here:
   Add16(a[0]=true, a[1..15]=false, b=in, out=out);
}

ALU

ALUは全部で18種類の関数がある。その関数を実行させるかを指定するには制御ビットと呼ばれる6ビットの入力ビットを用いる。
実装は以下のようになる。

CHIP ALU {
    IN  
        x[16], y[16],  // 16-bit inputs        
        zx, // zero the x input?
        nx, // negate the x input?
        zy, // zero the y input?
        ny, // negate the y input?
        f,  // compute out = x + y (if 1) or x & y (if 0)
        no; // negate the out output?

    OUT 
        out[16], // 16-bit output
        zr, // 1 if (out == 0), 0 otherwise
        ng; // 1 if (out < 0),  0 otherwise

    PARTS:
   // Put you code here:
   Mux16(sel=zx, b=false, a=true, out=NOT16zx);
   And16(a=x, b=NOT16zx, out=x1);

   Not16(in=x1, out=NOTx1);
   Mux16(sel=nx, b=NOTx1, a=x1, out=x2);

   Mux16(sel=zy, b=false, a=true, out=NOT16zy);
   And16(a=y, b=NOT16zy, out=y1);

   Not16(in=y1, out=NOTy1);
   Mux16(sel=ny, b=NOTy1, a=y1, out=y2);

   And16(a=x2, b=y2, out=x2ANDy2);
   Add16(a=x2, b=y2, out=x2ADDy2);
   Mux16(sel=f, b=x2ADDy2, a=x2ANDy2, out=out1);
   Not16(in=out1, out=NOTout1);
   Mux16(sel=no, b=NOTout1, a=out1, out[0..7]=out2Low, out[8..14]=out2High, out[15]=out2HighHigh);

   Or8Way(in=out2Low, out=orOut1);
   Or8Way(in[0..6]=out2High, in[7]=out2HighHigh, out=orOut2);

   Or(a=orOut1,b=orOut2, out=orOut);
   Not(in=orOut, out=zr);  

   And(a=out2HighHigh, b=true, out=ng);
   And16(a[0..7]=out2Low, a[8..14]=out2High, a[15]=out2HighHigh, b=true, out=out);
}

まとめ

Muxを使えば論理回路ないでif分を表現することができる!

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1