만들면서 배우기/Nand to Tetris

NAND to Tetris - 2장: Adder, Inc, ALU 실습

독-방 2021. 8. 3. 00:39

반가산기

반가산기의 진리표를 보면 sum은 두 입력의 XOR과 같고, carry는 두 입력의 AND와 같다.

 

전가산기

전가산기의 구현은 다음과 같다.

 

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비트 가산기

가장 오른쪽 비트부터 한 비트씩 전가산기로 더해가면서, carry를 다음 비트 덧셈으로 넘긴다.

 

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

    PARTS:
    FullAdder(a=a[0], b=b[0], c=false, 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);
}

 

증분기

위에서 만든 16비트 가산기의 두 번째 입력에 000... 01을 넣는다.

 

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

    PARTS:
    Add16(a=in, b[0]=true, out=out);
}

 

ALU

입력 x가 zx, nx에 따라 변환될 수 있는 경우의 수는 총 4가지이다. 따라서 zx, nx를 제어 비트로 하는 Mux4Way16 게이트를 사용하면  된다. 입력 y에 대해서도 마찬가지이다.

f에 따라서 x' + y'을 할지 x' & y'을 할지 선택하는 것도 Mux16을 활용해 구현할 수 있다.

마지막으로 no에 따라서 결과값을 반전시키는 여부도 Mux16으로 구현한다.

마지막 반환 값의 가장 왼쪽 비트가 0이면 결과가 양수, 1이면 음수이므로 이 비트를 그대로 ng 비트에 넣는다.

 

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:
    Not16(in=x, out=notx);
    Not16(in=y, out=noty);

    // nx와 zx에 따라 x, notx, 0, -1 중에서 선택합니다.
    Mux4Way16(a=x, b=notx, c=false, d=true, sel[0]=nx, sel[1]=zx, out=newx);
    // ny와 zy에 따라 y, noty, 0, -1 중에서 선택합니다.
    Mux4Way16(a=y, b=noty, c=false, d=true, sel[0]=ny, sel[1]=zy, out=newy);
    
    Add16(a=newx, b=newy, out=addxy);
    And16(a=newx, b=newy, out=andxy);

    // f값에 따라 x + y와 x & y 중에서 선택합니다.
    Mux16(a=andxy, b=addxy, sel=f, out=temp);

    Not16(in=temp, out=nottemp);
    // no값에 따라 temp와 nottemp 중에서 선택합니다.
    // out의 msb가 1이면 음수이고 0이면 양수이므로 msb를 그대로 ng에 넣습니다.
    Mux16(a=temp, b=nottemp, sel=no, out=out, out[15]=ng, out[0..7]=outleft, out[8..15]=outright);

    Or8Way(in=outleft, out=hasone1);
    Or8Way(in=outright, out=hasone2);
    Or(a=hasone1, b=hasone2, out=hasone);
    Not(in=hasone, out=zr);
}