第三週 - Sakura01210/co110a GitHub Wiki

HalfAdder

// 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/02/HalfAdder.hdl

/**
 * Computes the sum of two bits.
 */

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

// 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/02/FullAdder.hdl

/**
 * Computes the sum of three bits.
 */

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:
    Xor(a=a,b=b,out=ab);
    Xor(a=ab,b=c,out=sum);
    And(a=a,b=c,out=A);
    And(a=b,b=c,out=B);
    And(a=a,b=b,out=C);
    Or(a=A,b=B,out=AB);
    Or(a=AB,b=C,out=carry);
}

圖

Adder16

// 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/02/Adder16.hdl

/**
 * Adds two 16-bit values.
 * The most significant carry bit is ignored.
 */

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

    PARTS:
   HalfAdder(a=a[0],b=b[0],sum=out[0],carry=d);
   FullAdder(a=a[1],b=b[1],c=d,sum=out[1],carry=e);
   FullAdder(a=a[2],b=b[2],c=e,sum=out[2],carry=f);
   FullAdder(a=a[3],b=b[3],c=f,sum=out[3],carry=g);
   FullAdder(a=a[4],b=b[4],c=g,sum=out[4],carry=h);
   FullAdder(a=a[5],b=b[5],c=h,sum=out[5],carry=i);
   FullAdder(a=a[6],b=b[6],c=i,sum=out[6],carry=j);
   FullAdder(a=a[7],b=b[7],c=j,sum=out[7],carry=k);
   FullAdder(a=a[8],b=b[8],c=k,sum=out[8],carry=l);
   FullAdder(a=a[9],b=b[9],c=l,sum=out[9],carry=m);
   FullAdder(a=a[10],b=b[10],c=m,sum=out[10],carry=n);
   FullAdder(a=a[11],b=b[11],c=n,sum=out[11],carry=o);
   FullAdder(a=a[12],b=b[12],c=o,sum=out[12],carry=p);
   FullAdder(a=a[13],b=b[13],c=p,sum=out[13],carry=q);
   FullAdder(a=a[14],b=b[14],c=q,sum=out[14],carry=r);
   FullAdder(a=a[15],b=b[15],c=r,sum=out[15],carry=s);
}

圖

Inc16

// 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/02/Inc16.hdl

/**
 * 16-bit incrementer:
 * out = in + 1 (arithmetic addition)
 */

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

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

圖