Initial Commit with everything done

This commit is contained in:
2021-04-13 00:07:23 +03:00
commit 20cf599d89
33 changed files with 1019 additions and 0 deletions
+63
View File
@@ -0,0 +1,63 @@
// 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/02/ALU.hdl
/**
* The ALU (Arithmetic Logic Unit).
* Computes one of the following functions:
* x+y, x-y, y-x, 0, 1, -1, x, y, -x, -y, !x, !y,
* x+1, y+1, x-1, y-1, x&y, x|y on two 16-bit inputs,
* according to 6 input bits denoted notZX,nx,notZY,ny,f,no.
* In addition, the ALU computes two 1-bit outputs:
* if the ALU output == 0, zr is set to 1; otherwise zr is set to 0;
* if the ALU output < 0, ng is set to 1; otherwise ng is set to 0.
*/
// Implementation: the ALU logic manipulates the x and y inputs
// and operates on the resulting values, as follows:
// if (notZX == 1) set x = 0 // 16-bit constant
// if (nx == 1) set x = !x // bitwise not
// if (notZY == 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:
Not(in=zx,out=notZX);
Not(in=zy,out=notZY);
And16(a[0]=notZX,a[1]=notZX,a[2]=notZX,a[3]=notZX,a[4]=notZX,a[5]=notZX,a[6]=notZX,a[7]=notZX,a[8]=notZX,a[9]=notZX,a[10]=notZX,a[11]=notZX,a[12]=notZX,a[13]=notZX,a[14]=notZX,a[15]=notZX,b=x,out=ZX);
And16(a[0]=notZY,a[1]=notZY,a[2]=notZY,a[3]=notZY,a[4]=notZY,a[5]=notZY,a[6]=notZY,a[7]=notZY,a[8]=notZY,a[9]=notZY,a[10]=notZY,a[11]=notZY,a[12]=notZY,a[13]=notZY,a[14]=notZY,a[15]=notZY,b=y,out=ZY);
Xor16(a[0]=nx,a[1]=nx,a[2]=nx,a[3]=nx,a[4]=nx,a[5]=nx,a[6]=nx,a[7]=nx,a[8]=nx,a[9]=nx,a[10]=nx,a[11]=nx,a[12]=nx,a[13]=nx,a[14]=nx,a[15]=nx,b=ZX,out=NX);
Xor16(a[0]=ny,a[1]=ny,a[2]=ny,a[3]=ny,a[4]=ny,a[5]=ny,a[6]=ny,a[7]=ny,a[8]=ny,a[9]=ny,a[10]=ny,a[11]=ny,a[12]=ny,a[13]=ny,a[14]=ny,a[15]=ny,b=ZY,out=NY);
Add16(a=NX,b=NY,out=sumout);
And16(a=NX,b=NY,out=andout);
Not(in=f,out=notF);
And16(a[0]=f,a[1]=f,a[2]=f,a[3]=f,a[4]=f,a[5]=f,a[6]=f,a[7]=f,a[8]=f,a[9]=f,a[10]=f,a[11]=f,a[12]=f,a[13]=f,a[14]=f,a[15]=f,b=sumout,out=out1);
And16(a[0]=notF,a[1]=notF,a[2]=notF,a[3]=notF,a[4]=notF,a[5]=notF,a[6]=notF,a[7]=notF,a[8]=notF,a[9]=notF,a[10]=notF,a[11]=notF,a[12]=notF,a[13]=notF,a[14]=notF,a[15]=notF,b=andout,out=out2);
Xor16(a=out1,b=out2,out=out3);
Xor16(a[0]=no,a[1]=no,a[2]=no,a[3]=no,a[4]=no,a[5]=no,a[6]=no,a[7]=no,a[8]=no,a[9]=no,a[10]=no,a[11]=no,a[12]=no,a[13]=no,a[14]=no,a[15]=no,b=out3,out=output);
And16(a=output,b=output,out=out);
Not16(in=output,out=notOut);
Add16Way(in=notOut,out=zr);
CheckPin16(in=output,out=ng);
}
+33
View File
@@ -0,0 +1,33 @@
// 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/02/Adder16.hdl
/**
* Adds two 16-bit values.
* The most significant carry bit is ignored.
*/
CHIP Add16 {
IN a[16], b[16];
OUT out[16];
PARTS:
// Put you code here:
FullAdder(a=a[0],b=b[0],c=false,sum=out[0],carry=carry1);
FullAdder(a=a[1],b=b[1],c=carry1,sum=out[1],carry=carry2);
FullAdder(a=a[2],b=b[2],c=carry2,sum=out[2],carry=carry3);
FullAdder(a=a[3],b=b[3],c=carry3,sum=out[3],carry=carry4);
FullAdder(a=a[4],b=b[4],c=carry4,sum=out[4],carry=carry5);
FullAdder(a=a[5],b=b[5],c=carry5,sum=out[5],carry=carry6);
FullAdder(a=a[6],b=b[6],c=carry6,sum=out[6],carry=carry7);
FullAdder(a=a[7],b=b[7],c=carry7,sum=out[7],carry=carry8);
FullAdder(a=a[8],b=b[8],c=carry8,sum=out[8],carry=carry9);
FullAdder(a=a[9],b=b[9],c=carry9,sum=out[9],carry=carry10);
FullAdder(a=a[10],b=b[10],c=carry10,sum=out[10],carry=carry11);
FullAdder(a=a[11],b=b[11],c=carry11,sum=out[11],carry=carry12);
FullAdder(a=a[12],b=b[12],c=carry12,sum=out[12],carry=carry13);
FullAdder(a=a[13],b=b[13],c=carry13,sum=out[13],carry=carry14);
FullAdder(a=a[14],b=b[14],c=carry14,sum=out[14],carry=carry15);
FullAdder(a=a[15],b=b[15],c=carry15,sum=out[15],carry=carry16);
}
+19
View File
@@ -0,0 +1,19 @@
// 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/02/FullAdder.hdl
/**
* Computes the sum of three bits.
*/
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:
HalfAdder(a=a,b=b,sum=sum1,carry=carry1);
HalfAdder(a=sum1,b=c,sum=sum,carry=carry2);
Xor(a=carry1,b=carry2,out=carry);
}
+18
View File
@@ -0,0 +1,18 @@
// 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/02/HalfAdder.hdl
/**
* Computes the sum of two bits.
*/
CHIP HalfAdder {
IN a, b; // 1-bit inputs
OUT sum, // Right bit of a + b
carry; // Left bit of a + b
PARTS:
Xor(a=a,b=b,out=sum);
And(a=a,b=b,out=carry);
}
+33
View File
@@ -0,0 +1,33 @@
// 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/02/Inc16.hdl
/**
* 16-bit incrementer:
* out = in + 1 (arithmetic addition)
*/
CHIP Inc16 {
IN in[16];
OUT out[16];
PARTS:
HalfAdder(a=in[0],b=true,sum=out[0],carry=carry1);
HalfAdder(a=in[1],b=carry1,sum=out[1],carry=carry2);
HalfAdder(a=in[2],b=carry2,sum=out[2],carry=carry3);
HalfAdder(a=in[3],b=carry3,sum=out[3],carry=carry4);
HalfAdder(a=in[4],b=carry4,sum=out[4],carry=carry5);
HalfAdder(a=in[5],b=carry5,sum=out[5],carry=carry6);
HalfAdder(a=in[6],b=carry6,sum=out[6],carry=carry7);
HalfAdder(a=in[7],b=carry7,sum=out[7],carry=carry8);
HalfAdder(a=in[8],b=carry8,sum=out[8],carry=carry9);
HalfAdder(a=in[9],b=carry9,sum=out[9],carry=carry10);
HalfAdder(a=in[10],b=carry10,sum=out[10],carry=carry11);
HalfAdder(a=in[11],b=carry11,sum=out[11],carry=carry12);
HalfAdder(a=in[12],b=carry12,sum=out[12],carry=carry13);
HalfAdder(a=in[13],b=carry13,sum=out[13],carry=carry14);
HalfAdder(a=in[14],b=carry14,sum=out[14],carry=carry15);
HalfAdder(a=in[15],b=carry15,sum=out[15],carry=carry16);
}