Skip to content

Instantly share code, notes, and snippets.

@sleexyz
Created April 2, 2014 16:46
Show Gist options
  • Save sleexyz/9937983 to your computer and use it in GitHub Desktop.
Save sleexyz/9937983 to your computer and use it in GitHub Desktop.
/*
* ALU
* Sean Lee 2014-04-02
*
* Complementary Chips implemented:
* - Mult: takes one bit and outputs 16 bits of the same value
* - Xor16: 16-bit Xor
* - Or16Way: 16-way Or
* - IsNegative: returns 1 iff MSB == 1
*/
CHIP ALU {
IN x[16], y[16], zx, nx, zy, ny, f, no;
OUT out[16], zr, ng;
PARTS:
// zx
Not(in=zx, out=notzx);
Mult(in=notzx, out=notzx16);
And16(a=x, b=notzx16, out=xafterzx);
//nx
Mult(in=nx, out=nx16);
Xor16(a=xafterzx, b=nx16, out=xafternx);
// zy
Not(in=zy, out=notzy);
Mult(in=notzy, out=notzy16);
And16(a=y, b=notzy16, out=yafterzy);
//ny
Mult(in=ny, out=ny16);
Xor16(a=yafterzy, b=ny16, out=yafterny);
//f
Mult(in=f, out=f16);
Not16(in=f16, out=notf16);
Add16(a=xafternx, b=yafterny, out=xplusy);
And16(a=xplusy, b=f16, out=xplusymasked);
And16(a=xafternx, b=yafterny, out=xandy);
And16(a=xandy, b=notf16, out=xandymasked);
Or16(a=xplusymasked, b=xandymasked, out=outbeforeno);
//no
Mult(in=no, out=no16);
Xor16(a=outbeforeno, b=no16, out=toout);
// -> out
Not16(in=toout, out=nottoout);
Not16(in=nottoout, out=out);
// -> zr
Or16Way(in=toout, out=notzr);
Not(in=notzr, out=zr);
// -> ng
IsNegative(in=toout, out=ng);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment