Skip to content

Commit c2b8ab5

Browse files
committed
projects 1 and 2 done
1 parent 10a323a commit c2b8ab5

22 files changed

+559
-0
lines changed

01/And.hdl

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// This file is part of www.nand2tetris.org
2+
// and the book "The Elements of Computing Systems"
3+
// by Nisan and Schocken, MIT Press.
4+
// File name: projects/01/And.hdl
5+
6+
/**
7+
* And gate:
8+
* out = 1 if (a == 1 and b == 1)
9+
* 0 otherwise
10+
*/
11+
12+
CHIP And {
13+
IN a, b;
14+
OUT out;
15+
16+
PARTS:
17+
// Put your code here:
18+
Nand(a=a, b=b, out=nandab);
19+
Nand(a=nandab, b=nandab, out=out);
20+
}

01/And16.hdl

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// This file is part of www.nand2tetris.org
2+
// and the book "The Elements of Computing Systems"
3+
// by Nisan and Schocken, MIT Press.
4+
// File name: projects/01/And16.hdl
5+
6+
/**
7+
* 16-bit bitwise And:
8+
* for i = 0..15: out[i] = (a[i] and b[i])
9+
*/
10+
11+
CHIP And16 {
12+
IN a[16], b[16];
13+
OUT out[16];
14+
15+
PARTS:
16+
// Put your code here:
17+
And(a=a[15], b=b[15], out=out[15]);
18+
And(a=a[14], b=b[14], out=out[14]);
19+
And(a=a[13], b=b[13], out=out[13]);
20+
And(a=a[12], b=b[12], out=out[12]);
21+
And(a=a[11], b=b[11], out=out[11]);
22+
And(a=a[10], b=b[10], out=out[10]);
23+
And(a=a[9], b=b[9], out=out[9]);
24+
And(a=a[8], b=b[8], out=out[8]);
25+
And(a=a[7], b=b[7], out=out[7]);
26+
And(a=a[6], b=b[6], out=out[6]);
27+
And(a=a[5], b=b[5], out=out[5]);
28+
And(a=a[4], b=b[4], out=out[4]);
29+
And(a=a[3], b=b[3], out=out[3]);
30+
And(a=a[2], b=b[2], out=out[2]);
31+
And(a=a[1], b=b[1], out=out[1]);
32+
And(a=a[0], b=b[0], out=out[0]);
33+
}

01/DMux.hdl

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// This file is part of www.nand2tetris.org
2+
// and the book "The Elements of Computing Systems"
3+
// by Nisan and Schocken, MIT Press.
4+
// File name: projects/01/DMux.hdl
5+
6+
/**
7+
* Demultiplexor:
8+
* {a, b} = {in, 0} if sel == 0
9+
* {0, in} if sel == 1
10+
*/
11+
12+
CHIP DMux {
13+
IN in, sel;
14+
OUT a, b;
15+
16+
PARTS:
17+
// Put your code here:
18+
Not(in=sel, out=notsel);
19+
And(a=in, b=notsel, out=a);
20+
And(a=in, b=sel, out=b);
21+
}

01/DMux4Way.hdl

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// This file is part of www.nand2tetris.org
2+
// and the book "The Elements of Computing Systems"
3+
// by Nisan and Schocken, MIT Press.
4+
// File name: projects/01/DMux4Way.hdl
5+
6+
/**
7+
* 4-way demultiplexor:
8+
* {a, b, c, d} = {in, 0, 0, 0} if sel == 00
9+
* {0, in, 0, 0} if sel == 01
10+
* {0, 0, in, 0} if sel == 10
11+
* {0, 0, 0, in} if sel == 11
12+
*/
13+
14+
CHIP DMux4Way {
15+
IN in, sel[2];
16+
OUT a, b, c, d;
17+
18+
PARTS:
19+
// Put your code here:
20+
DMux(in=in, sel=sel[1], a=out1, b=out2);
21+
DMux(in=out1, sel=sel[0], a=a, b=b);
22+
DMux(in=out2, sel=sel[0], a=c, b=d);
23+
}

01/DMux8Way.hdl

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// This file is part of www.nand2tetris.org
2+
// and the book "The Elements of Computing Systems"
3+
// by Nisan and Schocken, MIT Press.
4+
// File name: projects/01/DMux8Way.hdl
5+
6+
/**
7+
* 8-way demultiplexor:
8+
* {a, b, c, d, e, f, g, h} = {in, 0, 0, 0, 0, 0, 0, 0} if sel == 000
9+
* {0, in, 0, 0, 0, 0, 0, 0} if sel == 001
10+
* etc.
11+
* {0, 0, 0, 0, 0, 0, 0, in} if sel == 111
12+
*/
13+
14+
CHIP DMux8Way {
15+
IN in, sel[3];
16+
OUT a, b, c, d, e, f, g, h;
17+
18+
PARTS:
19+
// Put your code here:
20+
DMux(in=in, sel=sel[2], a=out1, b=out2);
21+
DMux4Way(in=out1, sel=sel[0..1], a=a, b=b, c=c, d=d);
22+
DMux4Way(in=out2, sel=sel[0..1], a=e, b=f, c=g, d=h);
23+
}

01/Mux.hdl

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// This file is part of www.nand2tetris.org
2+
// and the book "The Elements of Computing Systems"
3+
// by Nisan and Schocken, MIT Press.
4+
// File name: projects/01/Mux.hdl
5+
6+
/**
7+
* Multiplexor:
8+
* out = a if sel == 0
9+
* b otherwise
10+
*/
11+
12+
CHIP Mux {
13+
IN a, b, sel;
14+
OUT out;
15+
16+
PARTS:
17+
// Put your code here:
18+
Not(in=sel, out=ns);
19+
And(a=a, b=ns, out=aandns);
20+
And(a=sel, b=b, out=bands);
21+
Or(a=aandns, b=bands, out=out);
22+
}

01/Mux16.hdl

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// This file is part of www.nand2tetris.org
2+
// and the book "The Elements of Computing Systems"
3+
// by Nisan and Schocken, MIT Press.
4+
// File name: projects/01/Mux16.hdl
5+
6+
/**
7+
* 16-bit multiplexor:
8+
* for i = 0..15 out[i] = a[i] if sel == 0
9+
* b[i] if sel == 1
10+
*/
11+
12+
CHIP Mux16 {
13+
IN a[16], b[16], sel;
14+
OUT out[16];
15+
16+
PARTS:
17+
// Put your code here:
18+
Mux(a=a[0], b=b[0], sel=sel, out=out[0]);
19+
Mux(a=a[1], b=b[1], sel=sel, out=out[1]);
20+
Mux(a=a[2], b=b[2], sel=sel, out=out[2]);
21+
Mux(a=a[3], b=b[3], sel=sel, out=out[3]);
22+
Mux(a=a[4], b=b[4], sel=sel, out=out[4]);
23+
Mux(a=a[5], b=b[5], sel=sel, out=out[5]);
24+
Mux(a=a[6], b=b[6], sel=sel, out=out[6]);
25+
Mux(a=a[7], b=b[7], sel=sel, out=out[7]);
26+
Mux(a=a[8], b=b[8], sel=sel, out=out[8]);
27+
Mux(a=a[9], b=b[9], sel=sel, out=out[9]);
28+
Mux(a=a[10], b=b[10], sel=sel, out=out[10]);
29+
Mux(a=a[11], b=b[11], sel=sel, out=out[11]);
30+
Mux(a=a[12], b=b[12], sel=sel, out=out[12]);
31+
Mux(a=a[13], b=b[13], sel=sel, out=out[13]);
32+
Mux(a=a[14], b=b[14], sel=sel, out=out[14]);
33+
Mux(a=a[15], b=b[15], sel=sel, out=out[15]);
34+
}

01/Mux4Way16.hdl

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// This file is part of www.nand2tetris.org
2+
// and the book "The Elements of Computing Systems"
3+
// by Nisan and Schocken, MIT Press.
4+
// File name: projects/01/Mux4Way16.hdl
5+
6+
/**
7+
* 4-way 16-bit multiplexor:
8+
* out = a if sel == 00
9+
* b if sel == 01
10+
* c if sel == 10
11+
* d if sel == 11
12+
*/
13+
14+
CHIP Mux4Way16 {
15+
IN a[16], b[16], c[16], d[16], sel[2];
16+
OUT out[16];
17+
18+
PARTS:
19+
// Put your code here:
20+
Mux16(a=a, b=b, sel=sel[0], out=ab);
21+
Mux16(a=c, b=d, sel=sel[0], out=cd);
22+
Mux16(a=ab, b=cd, sel=sel[1], out=out);
23+
}

01/Mux8Way16.hdl

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// This file is part of www.nand2tetris.org
2+
// and the book "The Elements of Computing Systems"
3+
// by Nisan and Schocken, MIT Press.
4+
// File name: projects/01/Mux8Way16.hdl
5+
6+
/**
7+
* 8-way 16-bit multiplexor:
8+
* out = a if sel == 000
9+
* b if sel == 001
10+
* etc.
11+
* h if sel == 111
12+
*/
13+
14+
CHIP Mux8Way16 {
15+
IN a[16], b[16], c[16], d[16],
16+
e[16], f[16], g[16], h[16],
17+
sel[3];
18+
OUT out[16];
19+
20+
PARTS:
21+
// Put your code here:
22+
Mux4Way16(a=a, b=b, c=c, d=d, sel=sel[0..1], out=out1);
23+
Mux4Way16(a=e, b=f, c=g, d=h, sel=sel[0..1], out=out2);
24+
Mux16(a=out1, b=out2, sel=sel[2], out=out);
25+
}

01/Not.hdl

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// This file is part of www.nand2tetris.org
2+
// and the book "The Elements of Computing Systems"
3+
// by Nisan and Schocken, MIT Press.
4+
// File name: projects/01/Not.hdl
5+
6+
/**
7+
* Not gate:
8+
* out = not in
9+
*/
10+
11+
CHIP Not {
12+
IN in;
13+
OUT out;
14+
15+
PARTS:
16+
// Put your code here:
17+
Nand(a=in, b=in, out=out);
18+
}

01/Not16.hdl

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// This file is part of www.nand2tetris.org
2+
// and the book "The Elements of Computing Systems"
3+
// by Nisan and Schocken, MIT Press.
4+
// File name: projects/01/Not16.hdl
5+
6+
/**
7+
* 16-bit Not:
8+
* for i=0..15: out[i] = not in[i]
9+
*/
10+
11+
CHIP Not16 {
12+
IN in[16];
13+
OUT out[16];
14+
15+
PARTS:
16+
// Put your code here:
17+
Not(in=in[15], out=out[15]);
18+
Not(in=in[14], out=out[14]);
19+
Not(in=in[13], out=out[13]);
20+
Not(in=in[12], out=out[12]);
21+
Not(in=in[11], out=out[11]);
22+
Not(in=in[10], out=out[10]);
23+
Not(in=in[9], out=out[9]);
24+
Not(in=in[8], out=out[8]);
25+
Not(in=in[7], out=out[7]);
26+
Not(in=in[6], out=out[6]);
27+
Not(in=in[5], out=out[5]);
28+
Not(in=in[4], out=out[4]);
29+
Not(in=in[3], out=out[3]);
30+
Not(in=in[2], out=out[2]);
31+
Not(in=in[1], out=out[1]);
32+
Not(in=in[0], out=out[0]);
33+
}

01/Or.hdl

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// This file is part of www.nand2tetris.org
2+
// and the book "The Elements of Computing Systems"
3+
// by Nisan and Schocken, MIT Press.
4+
// File name: projects/01/Or.hdl
5+
6+
/**
7+
* Or gate:
8+
* out = 1 if (a == 1 or b == 1)
9+
* 0 otherwise
10+
*/
11+
12+
CHIP Or {
13+
IN a, b;
14+
OUT out;
15+
16+
PARTS:
17+
// Put your code here:
18+
Nand(a=a, b=a, out=nota);
19+
Nand(a=b, b=b, out=notb);
20+
Nand(a=nota, b=notb, out=out);
21+
}

01/Or16.hdl

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// This file is part of www.nand2tetris.org
2+
// and the book "The Elements of Computing Systems"
3+
// by Nisan and Schocken, MIT Press.
4+
// File name: projects/01/Or16.hdl
5+
6+
/**
7+
* 16-bit bitwise Or:
8+
* for i = 0..15 out[i] = (a[i] or b[i])
9+
*/
10+
11+
CHIP Or16 {
12+
IN a[16], b[16];
13+
OUT out[16];
14+
15+
PARTS:
16+
// Put your code here:
17+
Or(a=a[15], b=b[15], out=out[15]);
18+
Or(a=a[14], b=b[14], out=out[14]);
19+
Or(a=a[13], b=b[13], out=out[13]);
20+
Or(a=a[12], b=b[12], out=out[12]);
21+
Or(a=a[11], b=b[11], out=out[11]);
22+
Or(a=a[10], b=b[10], out=out[10]);
23+
Or(a=a[9], b=b[9], out=out[9]);
24+
Or(a=a[8], b=b[8], out=out[8]);
25+
Or(a=a[7], b=b[7], out=out[7]);
26+
Or(a=a[6], b=b[6], out=out[6]);
27+
Or(a=a[5], b=b[5], out=out[5]);
28+
Or(a=a[4], b=b[4], out=out[4]);
29+
Or(a=a[3], b=b[3], out=out[3]);
30+
Or(a=a[2], b=b[2], out=out[2]);
31+
Or(a=a[1], b=b[1], out=out[1]);
32+
Or(a=a[0], b=b[0], out=out[0]);
33+
34+
}

01/Or8Way.hdl

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// This file is part of www.nand2tetris.org
2+
// and the book "The Elements of Computing Systems"
3+
// by Nisan and Schocken, MIT Press.
4+
// File name: projects/01/Or8Way.hdl
5+
6+
/**
7+
* 8-way Or:
8+
* out = (in[0] or in[1] or ... or in[7])
9+
*/
10+
11+
CHIP Or8Way {
12+
IN in[8];
13+
OUT out;
14+
15+
PARTS:
16+
// Put your code here:
17+
Or(a=in[0], b=in[1], out=out0);
18+
Or(a=out0, b=in[2], out=out1);
19+
Or(a=out1, b=in[3], out=out2);
20+
Or(a=out2, b=in[4], out=out3);
21+
Or(a=out3, b=in[5], out=out4);
22+
Or(a=out4, b=in[6], out=out5);
23+
Or(a=out5, b=in[7], out=out);
24+
}

0 commit comments

Comments
 (0)