ALU(Arithmetic Logic Unit) 만들기

ALU(Arithmetic Logic Unit) 만들기

가산기(Adder)

이 세 개의 가산기 칩들은 나중에 멀티비트 가산기 칩으로 이어진다.

HalfAdder

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);
}

Full-Adder

FullAdder

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=s1, carry=c1);
    HalfAdder(a=s1, b=c, sum=sum, carry=c2);
    Xor(a=c1, b=c2, out=carry);
}

Adder

Adder

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

    PARTS:
    HalfAdder(a=a[0], b=b[0], sum=out[0], carry=c0);
    FullAdder(a=a[1], b=b[1], c=c0, sum=out[1], carry=c1);
    FullAdder(a=a[2], b=b[2], c=c1, sum=out[2], carry=c2);
    FullAdder(a=a[3], b=b[3], c=c2, sum=out[3], carry=c3);
    FullAdder(a=a[4], b=b[4], c=c3, sum=out[4], carry=c4);
    FullAdder(a=a[5], b=b[5], c=c4, sum=out[5], carry=c5);
    FullAdder(a=a[6], b=b[6], c=c5, sum=out[6], carry=c6);
    FullAdder(a=a[7], b=b[7], c=c6, sum=out[7], carry=c7);
    FullAdder(a=a[8], b=b[8], c=c7, sum=out[8], carry=c8);
    FullAdder(a=a[9], b=b[9], c=c8, sum=out[9], carry=c9);
    FullAdder(a=a[10], b=b[10], c=c9, sum=out[10], carry=c10);
    FullAdder(a=a[11], b=b[11], c=c10, sum=out[11], carry=c11);
    FullAdder(a=a[12], b=b[12], c=c11, sum=out[12], carry=c12);
    FullAdder(a=a[13], b=b[13], c=c12, sum=out[13], carry=c13);
    FullAdder(a=a[14], b=b[14], c=c13, sum=out[14], carry=c14);
    FullAdder(a=a[15], b=b[15], c=c14, sum=out[15], carry=c15);
}

Overflow는 무시한다.

Incrementor

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

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

ALU

ALU ALUTruthTable

6개의 제어비트를 통해서 ALU가 어떤 함수 f(x, y)를 계산하도록 프로그래밍 할 수 있다.
이 계산들은 모두 설계의 결과이다.

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:
    // if zx == 1 set x = 0
    Mux16(a=x, b=false, sel=zx, out=x0);
    
    // !x
    Not16(in=x0, out=notx);
    
    // if nx == 1 set x = !x
    Mux16(a=x0, b=notx, sel=nx, out=x1);

    // if zy == 1 set y = 0
    Mux16(a=y, b=false, sel=zy, out=y0);

    // !y 
    Not16(in=y0, out=noty);

    // if ny == 1 set y = !y
    Mux16(a=y0, b=noty, sel=ny, out=y1);

    // x + y
    Add16(a=x1, b=y1, out=xPLUSy);

    // x & y
    And16(a=x1, b=y1, out=xANDy);

    // if f == 1 set out = x + y, if f == 0 set out = x & y
    Mux16(a=xANDy, b=xPLUSy, sel=f, out=tout);

    // !tout
    Not16(in=tout, out=ntout);

    // if no == 1 set out = !out
    Mux16(a=tout, b=ntout, sel=no, out=fout);

    // !nout
    Not16(in=fout, out=nout);
    Not16(in=nout, out[0..7]=outsub0, out[8..15]=outsub1, out[15]=outsub2, out=out);

    // if out == 0 zr = 1
    Or8Way(in=outsub0, out=zrout0);
    Or8Way(in=outsub1, out=zrout1);
    Or(a=zrout0, b=zrout1, out=zrout2);
    Not(in=zrout2, out=zr);
    
    // if out < 0 ng = 1
    And(a=outsub2, b=true, out=ng);
}