Skip to content

Commit

Permalink
Multi-way gates
Browse files Browse the repository at this point in the history
  • Loading branch information
icmor committed May 13, 2024
1 parent 457eb16 commit 860aaf7
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 0 deletions.
23 changes: 23 additions & 0 deletions logicGates/DMux4Way.hdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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/1/DMux4Way.hdl
/**
* 4-way demultiplexor:
* [a, b, c, d] = [in, 0, 0, 0] if sel = 00
* [0, in, 0, 0] if sel = 01
* [0, 0, in, 0] if sel = 10
* [0, 0, 0, in] if sel = 11
*/
CHIP DMux4Way {
IN in, sel[2];
OUT a, b, c, d;

PARTS:
DMux(in=in, sel=sel[0], a=ac, b=bd);
DMux(in=in, sel=sel[1], a=ab, b=cd);
And(a=ab, b=ac, out=a);
And(a=ab, b=bd, out=b);
And(a=cd, b=ac, out=c);
And(a=cd, b=bd, out=d);
}
31 changes: 31 additions & 0 deletions logicGates/DMux8Way.hdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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/1/DMux8Way.hdl
/**
* 8-way demultiplexor:
* [a, b, c, d, e, f, g, h] = [in, 0, 0, 0, 0, 0, 0, 0] if sel = 000
* [0, in, 0, 0, 0, 0, 0, 0] if sel = 001
* [0, 0, in, 0, 0, 0, 0, 0] if sel = 010
* [0, 0, 0, in, 0, 0, 0, 0] if sel = 011
* [0, 0, 0, 0, in, 0, 0, 0] if sel = 100
* [0, 0, 0, 0, 0, in, 0, 0] if sel = 101
* [0, 0, 0, 0, 0, 0, in, 0] if sel = 110
* [0, 0, 0, 0, 0, 0, 0, in] if sel = 111
*/
CHIP DMux8Way {
IN in, sel[3];
OUT a, b, c, d, e, f, g, h;

PARTS:
DMux4Way(in=in, sel=sel[0..1], a=ae, b=bf, c=cg, d=dh);
DMux(in=in, sel=sel[2], a=abcd, b=efgh);
And(a=ae, b=abcd, out=a);
And(a=bf, b=abcd, out=b);
And(a=cg, b=abcd, out=c);
And(a=dh, b=abcd, out=d);
And(a=ae, b=efgh, out=e);
And(a=bf, b=efgh, out=f);
And(a=cg, b=efgh, out=g);
And(a=dh, b=efgh, out=h);
}
20 changes: 20 additions & 0 deletions logicGates/Mux4Way16.hdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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/1/Mux4Way16.hdl
/**
* 4-way 16-bit multiplexor:
* out = a if sel = 00
* b if sel = 01
* c if sel = 10
* d if sel = 11
*/
CHIP Mux4Way16 {
IN a[16], b[16], c[16], d[16], sel[2];
OUT out[16];

PARTS:
Mux16(a=a, b=b, sel=sel[0], out=ab);
Mux16(a=c, b=d, sel=sel[0], out=cd);
Mux16(a=ab, b=cd, sel=sel[1], out=out);
}
26 changes: 26 additions & 0 deletions logicGates/Mux8Way16.hdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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/1/Mux8Way16.hdl
/**
* 8-way 16-bit multiplexor:
* out = a if sel = 000
* b if sel = 001
* c if sel = 010
* d if sel = 011
* e if sel = 100
* f if sel = 101
* g if sel = 110
* 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:
Mux4Way16(a=a, b=b, c=c, d=d, sel=sel[0..1], out=abcd);
Mux4Way16(a=e, b=f, c=g, d=h, sel=sel[0..1], out=efgh);
Mux16(a=abcd, b=efgh, sel=sel[2], out=out);
}
21 changes: 21 additions & 0 deletions logicGates/Or8Way.hdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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/1/Or8Way.hdl
/**
* 8-way Or gate:
* out = in[0] Or in[1] Or ... Or in[7]
*/
CHIP Or8Way {
IN in[8];
OUT out;

PARTS:
Or(a=in[0], b=in[1], out=two0);
Or(a=in[2], b=in[3], out=two1);
Or(a=in[4], b=in[5], out=two2);
Or(a=in[6], b=in[7], out=two3);
Or(a=two0, b=two1, out=four0);
Or(a=two2, b=two3, out=four1);
Or(a=four0, b=four1, out=out);
}

0 comments on commit 860aaf7

Please sign in to comment.