diff --git a/1_Boolean_Arithmetic/And.hdl b/Assignments/1_Boolean_Logic/And.hdl similarity index 100% rename from 1_Boolean_Arithmetic/And.hdl rename to Assignments/1_Boolean_Logic/And.hdl diff --git a/1_Boolean_Arithmetic/And16.hdl b/Assignments/1_Boolean_Logic/And16.hdl similarity index 100% rename from 1_Boolean_Arithmetic/And16.hdl rename to Assignments/1_Boolean_Logic/And16.hdl diff --git a/1_Boolean_Arithmetic/DMux.hdl b/Assignments/1_Boolean_Logic/DMux.hdl similarity index 100% rename from 1_Boolean_Arithmetic/DMux.hdl rename to Assignments/1_Boolean_Logic/DMux.hdl diff --git a/1_Boolean_Arithmetic/DMux4Way.hdl b/Assignments/1_Boolean_Logic/DMux4Way.hdl similarity index 100% rename from 1_Boolean_Arithmetic/DMux4Way.hdl rename to Assignments/1_Boolean_Logic/DMux4Way.hdl diff --git a/1_Boolean_Arithmetic/DMux8Way.hdl b/Assignments/1_Boolean_Logic/DMux8Way.hdl similarity index 100% rename from 1_Boolean_Arithmetic/DMux8Way.hdl rename to Assignments/1_Boolean_Logic/DMux8Way.hdl diff --git a/1_Boolean_Arithmetic/Mux.hdl b/Assignments/1_Boolean_Logic/Mux.hdl similarity index 100% rename from 1_Boolean_Arithmetic/Mux.hdl rename to Assignments/1_Boolean_Logic/Mux.hdl diff --git a/1_Boolean_Arithmetic/Mux16.hdl b/Assignments/1_Boolean_Logic/Mux16.hdl similarity index 100% rename from 1_Boolean_Arithmetic/Mux16.hdl rename to Assignments/1_Boolean_Logic/Mux16.hdl diff --git a/1_Boolean_Arithmetic/Mux4Way16.hdl b/Assignments/1_Boolean_Logic/Mux4Way16.hdl similarity index 100% rename from 1_Boolean_Arithmetic/Mux4Way16.hdl rename to Assignments/1_Boolean_Logic/Mux4Way16.hdl diff --git a/1_Boolean_Arithmetic/Mux8Way16.hdl b/Assignments/1_Boolean_Logic/Mux8Way16.hdl similarity index 100% rename from 1_Boolean_Arithmetic/Mux8Way16.hdl rename to Assignments/1_Boolean_Logic/Mux8Way16.hdl diff --git a/1_Boolean_Arithmetic/Not.hdl b/Assignments/1_Boolean_Logic/Not.hdl similarity index 100% rename from 1_Boolean_Arithmetic/Not.hdl rename to Assignments/1_Boolean_Logic/Not.hdl diff --git a/1_Boolean_Arithmetic/Not16.hdl b/Assignments/1_Boolean_Logic/Not16.hdl similarity index 100% rename from 1_Boolean_Arithmetic/Not16.hdl rename to Assignments/1_Boolean_Logic/Not16.hdl diff --git a/1_Boolean_Arithmetic/Or.hdl b/Assignments/1_Boolean_Logic/Or.hdl similarity index 100% rename from 1_Boolean_Arithmetic/Or.hdl rename to Assignments/1_Boolean_Logic/Or.hdl diff --git a/1_Boolean_Arithmetic/Or16.hdl b/Assignments/1_Boolean_Logic/Or16.hdl similarity index 100% rename from 1_Boolean_Arithmetic/Or16.hdl rename to Assignments/1_Boolean_Logic/Or16.hdl diff --git a/1_Boolean_Arithmetic/Or8Way.hdl b/Assignments/1_Boolean_Logic/Or8Way.hdl similarity index 100% rename from 1_Boolean_Arithmetic/Or8Way.hdl rename to Assignments/1_Boolean_Logic/Or8Way.hdl diff --git a/1_Boolean_Arithmetic/Xor.hdl b/Assignments/1_Boolean_Logic/Xor.hdl similarity index 100% rename from 1_Boolean_Arithmetic/Xor.hdl rename to Assignments/1_Boolean_Logic/Xor.hdl diff --git a/Assignments/2_Boolean_Arithmetic/ALU.hdl b/Assignments/2_Boolean_Arithmetic/ALU.hdl new file mode 100644 index 0000000..93f71d2 --- /dev/null +++ b/Assignments/2_Boolean_Arithmetic/ALU.hdl @@ -0,0 +1,64 @@ +// 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/2/ALU.hdl +/** + * ALU (Arithmetic Logic Unit): + * Computes out = one of the following functions: + * 0, 1, -1, + * x, y, !x, !y, -x, -y, + * x + 1, y + 1, x - 1, y - 1, + * x + y, x - y, y - x, + * x & y, x | y + * on the 16-bit inputs x, y, + * according to the input bits zx, nx, zy, ny, f, no. + * In addition, computes the two output bits: + * if (out == 0) zr = 1, else zr = 0 + * if (out < 0) ng = 1, else ng = 0 + */ +// Implementation: Manipulates the x and y inputs +// and operates on the resulting values, as follows: +// if (zx == 1) sets x = 0 // 16-bit constant +// if (nx == 1) sets x = !x // bitwise not +// if (zy == 1) sets y = 0 // 16-bit constant +// if (ny == 1) sets y = !y // bitwise not +// if (f == 1) sets out = x + y // integer 2's complement addition +// if (f == 0) sets out = x & y // bitwise and +// if (no == 1) sets out = !out // bitwise not + +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) or (out = x & y)? + no; // negate the out output? + OUT + out[16], // 16-bit output + zr, // if (out == 0) equals 1, else 0 + ng; // if (out < 0) equals 1, else 0 + + PARTS: + Mux16(a=x, b=false, sel=zx, out=zxr); + Not16(in=zxr, out=nxr); + Mux16(a=zxr, b=nxr, sel=nx, out=xr); + + Mux16(a=y, b=false, sel=zy, out=zyr); + Not16(in=zyr, out=nyr); + Mux16(a=zyr, b=nyr, sel=ny, out=yr); + + Add16(a=xr, b=yr, out=s); + And16(a=xr, b=yr, out=a); + Mux16(a=a, b=s, sel=f, out=r); + + Not16(in=r, out=nr); + Mux16(a=r, b=nr, sel=no, out=out, out[15]=ng, out[0..7]=r1, out[8..15]=r2); + + Or8Way(in=r1, out=o1); + Or8Way(in=r2, out=o2); + Or(a=o1, b=o2, out=nzr); + Not(in=nzr, out=zr); + +} \ No newline at end of file diff --git a/Assignments/2_Boolean_Arithmetic/Add16.hdl b/Assignments/2_Boolean_Arithmetic/Add16.hdl new file mode 100644 index 0000000..fefbcc1 --- /dev/null +++ b/Assignments/2_Boolean_Arithmetic/Add16.hdl @@ -0,0 +1,30 @@ +// 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/2/Add16.hdl +/** + * 16-bit adder: Adds two 16-bit two's complement values. + * The most significant carry bit is ignored. + */ +CHIP Add16 { + IN a[16], b[16]; + OUT out[16]; + + PARTS: + HalfAdder(a=a[0], b=b[0], sum=out[0], carry=c1); + FullAdder(a=a[1], b=b[1], c=c1, sum=out[1], carry=c2); + FullAdder(a=a[2], b=b[2], c=c2, sum=out[2], carry=c3); + FullAdder(a=a[3], b=b[3], c=c3, sum=out[3], carry=c4); + FullAdder(a=a[4], b=b[4], c=c4, sum=out[4], carry=c5); + FullAdder(a=a[5], b=b[5], c=c5, sum=out[5], carry=c6); + FullAdder(a=a[6], b=b[6], c=c6, sum=out[6], carry=c7); + FullAdder(a=a[7], b=b[7], c=c7, sum=out[7], carry=c8); + FullAdder(a=a[8], b=b[8], c=c8, sum=out[8], carry=c9); + FullAdder(a=a[9], b=b[9], c=c9, sum=out[9], carry=c10); + FullAdder(a=a[10], b=b[10], c=c10, sum=out[10], carry=c11); + FullAdder(a=a[11], b=b[11], c=c11, sum=out[11], carry=c12); + FullAdder(a=a[12], b=b[12], c=c12, sum=out[12], carry=c13); + FullAdder(a=a[13], b=b[13], c=c13, sum=out[13], carry=c14); + FullAdder(a=a[14], b=b[14], c=c14, sum=out[14], carry=c15); + FullAdder(a=a[15], b=b[15], c=c15, sum=out[15], carry=c16); +} \ No newline at end of file diff --git a/Assignments/2_Boolean_Arithmetic/FullAdder.hdl b/Assignments/2_Boolean_Arithmetic/FullAdder.hdl new file mode 100644 index 0000000..6a36fe9 --- /dev/null +++ b/Assignments/2_Boolean_Arithmetic/FullAdder.hdl @@ -0,0 +1,17 @@ +// 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/2/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=x, carry=c1); + HalfAdder(a=x, b=c, sum=sum, carry=c2); + Or(a=c1, b=c2, out=carry); +} \ No newline at end of file diff --git a/Assignments/2_Boolean_Arithmetic/HalfAdder.hdl b/Assignments/2_Boolean_Arithmetic/HalfAdder.hdl new file mode 100644 index 0000000..ce7f43d --- /dev/null +++ b/Assignments/2_Boolean_Arithmetic/HalfAdder.hdl @@ -0,0 +1,16 @@ +// 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/2/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); +} \ No newline at end of file diff --git a/Assignments/2_Boolean_Arithmetic/Inc16.hdl b/Assignments/2_Boolean_Arithmetic/Inc16.hdl new file mode 100644 index 0000000..cb496b0 --- /dev/null +++ b/Assignments/2_Boolean_Arithmetic/Inc16.hdl @@ -0,0 +1,15 @@ +// 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/2/Inc16.hdl +/** + * 16-bit incrementer: + * out = in + 1 + */ +CHIP Inc16 { + IN in[16]; + OUT out[16]; + + PARTS: + Add16(a=in, b[0]=true, b[1..15]=false, out=out); +} \ No newline at end of file diff --git a/Chapters/1_Boolean_Logic.pdf b/Chapters/1_Boolean_Logic.pdf new file mode 100644 index 0000000..872d99d Binary files /dev/null and b/Chapters/1_Boolean_Logic.pdf differ diff --git a/Chapters/2_Boolean_Arithmetic.pdf b/Chapters/2_Boolean_Arithmetic.pdf new file mode 100644 index 0000000..a1184ec Binary files /dev/null and b/Chapters/2_Boolean_Arithmetic.pdf differ diff --git a/README.md b/README.md index f6cbacc..741776d 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,10 @@ В этом репозитории я сохраняю свои работы в ходе прохождения курса [nand2tetris](https://www.nand2tetris.org) Курс состоит из двух частей, в первой части 6 проектов: -- [Project 1: Boolean Logic](./1_Boolean_Arithmetic) - создание -- Project 2: Boolean Arithmetic +- [Project 1: Boolean Logic](./Assignments/1_Boolean_Logic) + > Реализация [NAND-логики](https://en.wikipedia.org/wiki/NAND_logic), то есть создание основных логических блоков (AND, OR, NOT, XOR, MUX, DMUX, а так же их версий для работы с 16-битной шиной) с помощью операции NAND ([И-НЕ или Штрих Шеффера](https://ru.wikipedia.org/wiki/Штрих_Шеффера)) +- [Project 2: Boolean Arithmetic](./Assignments/2_Boolean_Arithmetic/) + > Создание [простого арифметико-логического устройства (ALU)](./Assignments/2_Boolean_Arithmetic/ALU.hdl), способного складывать и вычитать 16-битные числа с помощью логических блоков из первого проекта - Project 3: Memory - Project 4: Machine Language - Project 5: Computer Architecture