nand2tetris Mux8Way16.hdl
// 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/01/Mux8Way16.hdl | |
/** | |
* 8-way 16-bit multiplexor: | |
* out = a if sel == 000 | |
* b if sel == 001 | |
* etc. | |
* h if sel == 111 | |
*/ | |
CHIP Mux8Way16 { | |
IN a[16], b[16], c[16], d[16], | |
e[16], f[16], g[16], h[16], | |
sel[3]; | |
OUT out[16]; | |
PARTS: | |
// make sel selector | |
Not(in=sel[2], out=sel2n); | |
Not(in=sel[1], out=sel1n); | |
Not(in=sel[0], out=sel0n); | |
// 000 | |
And(a=sel2n, b=sel1n, out=sel000tmp); | |
And(a=sel000tmp, b=sel0n, out=sel000); | |
And16(a=a, b[0]=sel000, b[1]=sel000, b[2]=sel000, b[3]=sel000, b[4]=sel000, b[5]=sel000, b[6]=sel000, b[7]=sel000, b[8]=sel000, b[9]=sel000, b[10]=sel000, b[11]=sel000, b[12]=sel000, b[13]=sel000, b[14]=sel000, b[15]=sel000, out=aOut); | |
// 001 | |
And(a=sel2n, b=sel1n, out=sel001tmp); | |
And(a=sel001tmp, b=sel[0], out=sel001); | |
And16(a=b, b[0]=sel001, b[1]=sel001, b[2]=sel001, b[3]=sel001, b[4]=sel001, b[5]=sel001, b[6]=sel001, b[7]=sel001, b[8]=sel001, b[9]=sel001, b[10]=sel001, b[11]=sel001, b[12]=sel001, b[13]=sel001, b[14]=sel001, b[15]=sel001, out=bOut); | |
// 010 | |
And(a=sel2n, b=sel[1], out=sel010tmp); | |
And(a=sel010tmp, b=sel0n, out=sel010); | |
And16(a=c, b[0]=sel010, b[1]=sel010, b[2]=sel010, b[3]=sel010, b[4]=sel010, b[5]=sel010, b[6]=sel010, b[7]=sel010, b[8]=sel010, b[9]=sel010, b[10]=sel010, b[11]=sel010, b[12]=sel010, b[13]=sel010, b[14]=sel010, b[15]=sel010, out=cOut); | |
// 011 | |
And(a=sel2n, b=sel[1], out=sel011tmp); | |
And(a=sel011tmp, b=sel[0], out=sel011); | |
And16(a=d, b[0]=sel011, b[1]=sel011, b[2]=sel011, b[3]=sel011, b[4]=sel011, b[5]=sel011, b[6]=sel011, b[7]=sel011, b[8]=sel011, b[9]=sel011, b[10]=sel011, b[11]=sel011, b[12]=sel011, b[13]=sel011, b[14]=sel011, b[15]=sel011, out=dOut); | |
//// | |
// 100 | |
And(a=sel[2], b=sel1n, out=sel100tmp); | |
And(a=sel100tmp, b=sel0n, out=sel100); | |
And16(a=e, b[0]=sel100, b[1]=sel100, b[2]=sel100, b[3]=sel100, b[4]=sel100, b[5]=sel100, b[6]=sel100, b[7]=sel100, b[8]=sel100, b[9]=sel100, b[10]=sel100, b[11]=sel100, b[12]=sel100, b[13]=sel100, b[14]=sel100, b[15]=sel100, out=eOut); | |
// 101 | |
And(a=sel[2], b=sel1n, out=sel101tmp); | |
And(a=sel101tmp, b=sel[0], out=sel101); | |
And16(a=f, b[0]=sel101, b[1]=sel101, b[2]=sel101, b[3]=sel101, b[4]=sel101, b[5]=sel101, b[6]=sel101, b[7]=sel101, b[8]=sel101, b[9]=sel101, b[10]=sel101, b[11]=sel101, b[12]=sel101, b[13]=sel101, b[14]=sel101, b[15]=sel101, out=fOut); | |
// 110 | |
And(a=sel[2], b=sel[1], out=sel110tmp); | |
And(a=sel110tmp, b=sel0n, out=sel110); | |
And16(a=g, b[0]=sel110, b[1]=sel110, b[2]=sel110, b[3]=sel110, b[4]=sel110, b[5]=sel110, b[6]=sel110, b[7]=sel110, b[8]=sel110, b[9]=sel110, b[10]=sel110, b[11]=sel110, b[12]=sel110, b[13]=sel110, b[14]=sel110, b[15]=sel110, out=gOut); | |
// 111 | |
And(a=sel[2], b=sel[1], out=sel111tmp); | |
And(a=sel111tmp, b=sel[0], out=sel111); | |
And16(a=h, b[0]=sel111, b[1]=sel111, b[2]=sel111, b[3]=sel111, b[4]=sel111, b[5]=sel111, b[6]=sel111, b[7]=sel111, b[8]=sel111, b[9]=sel111, b[10]=sel111, b[11]=sel111, b[12]=sel111, b[13]=sel111, b[14]=sel111, b[15]=sel111, out=hOut); | |
Or16(a=aOut, b=bOut, out=abOut); | |
Or16(a=cOut, b=dOut, out=cdOut); | |
Or16(a=eOut, b=fOut, out=efOut); | |
Or16(a=gOut, b=hOut, out=ghOut); | |
Or16(a=abOut, b=cdOut, out=abcdOut); | |
Or16(a=efOut, b=ghOut, out=efghOut); | |
Or16(a=abcdOut, b=efghOut, out=out); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment