2016年2月2日火曜日

開発環境

  • OS X El Capitan - Apple (OS)
  • Emacs(Text Editor)
  • Java (実行環境)

コンピュータシステムの理論と実装 (Noam Nisan (著)、Shimon Schocken (著)、斎藤 康毅(翻訳)、オライリージャパン)の2章(ブール算術)、2.5(プロジェクト(1.2.4(多入力の基本ゲート、多入力Or、多入力/多ビットマルチプレクサ、多出力/多ビットデマルチプレクサ))を取り組んでみる。

2.5(プロジェクト)

コード(Emacs)

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

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=sumab, carry=carryab);
    HalfAdder(a=sumab, b=c, sum=sum, carry=carryabc);
    Or(a=carryab, b=carryabc, out=carry);
}

Add16.hdl

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

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

Inc16.hdl

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

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

ALU.hdl

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:
    Mux16(a=x, b[0..15]=false, sel=zx, out=muxx);

    Not16(in=muxx, out=notmuxx);
    Mux16(a=muxx, b=notmuxx, sel=nx, out=x0);

    Mux16(a=y, b[0..15]=false, sel=zy, out=muxy);

    Not16(in=muxy, out=notmuxy);
    Mux16(a=muxy, b=notmuxy, sel=ny, out=y0);

    Add16(a=x0, b=y0, out=addxy);
    And16(a=x0, b=y0, out=andxy);
    Mux16(a=andxy, b=addxy, sel=f, out=fxy);

    Not16(in=fxy, out=notfxy);
    Mux16(a=fxy, b=notfxy, sel=no, out=out, out[0..7]=mux1, out[8..15]=mux2,
    out[15]=sign);

    Or8Way(in=mux1, out=or1);
    Or8Way(in=mux2, out=or2);
    Or(a=or1, b=or2, out=or3);
    Not(in=or3, out=zr);

    And(a=sign, b=true, out=ng);
}

0 コメント:

コメントを投稿

Comments on Google+: