// File name: projects/02/ALU.hdl
// Implementation: the ALU logic manipulates the x and y inputs // and operates on the resulting values, as follows: // if (zx == 1) set x = 0 // 16-bit constant // if (nx == 1) set x = !x // bitwise not // if (zy == 1) set y = 0 // 16-bit constant // if (ny == 1) set y = !y // bitwise not // if (f == 1) set out = x + y // integer 2's complement addition // if (f == 0) set out = x & y // bitwise and // if (no == 1) set out = !out // bitwise not // if (out == 0) set zr = 1 // if (out < 0) set ng = 1
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: // Put you code here: Mux16(a=x, b=false, sel=zx, out=xout); Not16(in=xout, out=notxout); Mux16(a=xout, b=notxout, sel=nx, out=nxout);
Mux16(a=y, b=false, sel=zy, out=yout); Not16(in=yout, out=notyout); Mux16(a=yout, b=notyout, sel=ny, out=nyout);
Add16(a=nxout, b=nyout, out=addout); And16(a=nxout, b=nyout, out=andout);
Mux16(a=andout, b=addout, sel=f, out=fout);
Not16(in=fout, out=notfout); Mux16(a=fout, b=notfout, sel=no, out=finalout); Or16(a=finalout, b=false, out=out);
// Adder16(a=finalout, b=true, out=o, carry=sign); // Not(in=sign, out=zr); Or16Way(in=finalout, out=sign); Not(in=sign, out=zr);
Or16(a=finalout, b=false, out[15]=ng); } // File name: projects/02/Adder16.hdl
CHIP Add16 { IN a[16], b[16]; OUT out[16];
PARTS: // Put you code here: HalfAdder(a=a[0], b=b[0], sum=out[0], carry=ca); FullAdder(a=a[1], b=b[1], c=ca, sum=out[1], carry=cb); FullAdder(a=a[2], b=b[2], c=cb, sum=out[2], carry=cc); FullAdder(a=a[3], b=b[3], c=cc, sum=out[3], carry=cd); FullAdder(a=a[4], b=b[4], c=cd, sum=out[4], carry=ce); FullAdder(a=a[5], b=b[5], c=ce, sum=out[5], carry=cf); FullAdder(a=a[6], b=b[6], c=cf, sum=out[6], carry=cg); FullAdder(a=a[7], b=b[7], c=cg, sum=out[7], carry=ch); FullAdder(a=a[8], b=b[8], c=ch, sum=out[8], carry=ci); FullAdder(a=a[9], b=b[9], c=ci, sum=out[9], carry=cj); FullAdder(a=a[10], b=b[10], c=cj, sum=out[10], carry=ck); FullAdder(a=a[11], b=b[11], c=ck, sum=out[11], carry=cl); FullAdder(a=a[12], b=b[12], c=cl, sum=out[12], carry=cm); FullAdder(a=a[13], b=b[13], c=cm, sum=out[13], carry=cn); FullAdder(a=a[14], b=b[14], c=cn, sum=out[14], carry=co); FullAdder(a=a[15], b=b[15], c=co, sum=out[15], carry=cp); } // File name: projects/02/Adder16.hdl
CHIP Adder16 { IN a[16], b[16]; OUT out[16], carry;
PARTS: // Put you code here: HalfAdder(a=a[0], b=b[0], sum=out[0], carry=ca); FullAdder(a=a[1], b=b[1], c=ca, sum=out[1], carry=cb); FullAdder(a=a[2], b=b[2], c=cb, sum=out[2], carry=cc); FullAdder(a=a[3], b=b[3], c=cc, sum=out[3], carry=cd); FullAdder(a=a[4], b=b[4], c=cd, sum=out[4], carry=ce); FullAdder(a=a[5], b=b[5], c=ce, sum=out[5], carry=cf); FullAdder(a=a[6], b=b[6], c=cf, sum=out[6], carry=cg); FullAdder(a=a[7], b=b[7], c=cg, sum=out[7], carry=ch); FullAdder(a=a[8], b=b[8], c=ch, sum=out[8], carry=ci); FullAdder(a=a[9], b=b[9], c=ci, sum=out[9], carry=cj); FullAdder(a=a[10], b=b[10], c=cj, sum=out[10], carry=ck); FullAdder(a=a[11], b=b[11], c=ck, sum=out[11], carry=cl); FullAdder(a=a[12], b=b[12], c=cl, sum=out[12], carry=cm); FullAdder(a=a[13], b=b[13], c=cm, sum=out[13], carry=cn); FullAdder(a=a[14], b=b[14], c=cn, sum=out[14], carry=co); FullAdder(a=a[15], b=b[15], c=co, sum=out[15], carry=carry); } // File name: projects/02/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: // Put you code here: HalfAdder(a=a, b=b, sum=sumab, carry=carryab); Xor(a=sumab, b=c, out=sum); And(a=a, b=b, out=aband); And(a=b, b=c, out=bcand); And(a=a, b=c, out=acand); Or(a=aband, b=bcand, out=abcor); Or(a=abcor, b=acand, out=carry); }
CHIP HalfAdder { IN a, b; // 1-bit inputs OUT sum, // Right bit of a + b carry; // Left bit of a + b
PARTS: // Put you code here: Xor(a=a, b=b, out=sum); And(a=a, b=b, out=carry); }
// File name: projects/02/Inc16.hdl
CHIP Inc16 { IN in[16]; OUT out[16];
PARTS: // Put you code here: HalfAdder(a=in[0], b=true, sum=out[0], carry=ca); FullAdder(a=in[1], b=false, c=ca, sum=out[1], carry=cb); FullAdder(a=in[2], b=false, c=cb, sum=out[2], carry=cc); FullAdder(a=in[3], b=false, c=cc, sum=out[3], carry=cd); FullAdder(a=in[4], b=false, c=cd, sum=out[4], carry=ce); FullAdder(a=in[5], b=false, c=ce, sum=out[5], carry=cf); FullAdder(a=in[6], b=false, c=cf, sum=out[6], carry=cg); FullAdder(a=in[7], b=false, c=cg, sum=out[7], carry=ch); FullAdder(a=in[8], b=false, c=ch, sum=out[8], carry=ci); FullAdder(a=in[9], b=false, c=ci, sum=out[9], carry=cj); FullAdder(a=in[10], b=false, c=cj, sum=out[10], carry=ck); FullAdder(a=in[11], b=false, c=ck, sum=out[11], carry=cl); FullAdder(a=in[12], b=false, c=cl, sum=out[12], carry=cm); FullAdder(a=in[13], b=false, c=cm, sum=out[13], carry=cn); FullAdder(a=in[14], b=false, c=cn, sum=out[14], carry=co); FullAdder(a=in[15], b=false, c=co, sum=out[15], carry=cp); } // File name: projects/01/Or8Way.hdl
CHIP Or16Way { IN in[16]; OUT out;
PARTS: // Put your code here: Or8Way(in=in[0..7], out=a); Or8Way(in=in[8..15], out=b); Or(a=a, b=b, out=out); }
|