diff --git a/nand2tetris/projects/2/ALU-basic.out b/nand2tetris/projects/2/ALU-basic.out new file mode 100644 index 0000000..249461c --- /dev/null +++ b/nand2tetris/projects/2/ALU-basic.out @@ -0,0 +1,37 @@ +| x | y |zx |nx |zy |ny | f |no | out | +| 0000000000000000 | 1111111111111111 | 1 | 0 | 1 | 0 | 1 | 0 | 0000000000000000 | +| 0000000000000000 | 1111111111111111 | 1 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 | +| 0000000000000000 | 1111111111111111 | 1 | 1 | 1 | 0 | 1 | 0 | 1111111111111111 | +| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 0 | 0 | 0000000000000000 | +| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 0 | 0 | 1111111111111111 | +| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 0 | 1 | 1111111111111111 | +| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 0 | 1 | 0000000000000000 | +| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 1 | 1 | 0000000000000000 | +| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 1 | 1 | 0000000000000001 | +| 0000000000000000 | 1111111111111111 | 0 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 | +| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 1 | 1 | 1 | 0000000000000000 | +| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 1 | 0 | 1111111111111111 | +| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 1 | 0 | 1111111111111110 | +| 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 0 | 1 | 0 | 1111111111111111 | +| 0000000000000000 | 1111111111111111 | 0 | 1 | 0 | 0 | 1 | 1 | 0000000000000001 | +| 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 1 | 1 | 1 | 1111111111111111 | +| 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 0 | 0 | 0 | 0000000000000000 | +| 0000000000000000 | 1111111111111111 | 0 | 1 | 0 | 1 | 0 | 1 | 1111111111111111 | +| 0101101110100000 | 0001111011010010 | 1 | 0 | 1 | 0 | 1 | 0 | 0000000000000000 | +| 0101101110100000 | 0001111011010010 | 1 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 | +| 0101101110100000 | 0001111011010010 | 1 | 1 | 1 | 0 | 1 | 0 | 1111111111111111 | +| 0101101110100000 | 0001111011010010 | 0 | 0 | 1 | 1 | 0 | 0 | 0101101110100000 | +| 0101101110100000 | 0001111011010010 | 1 | 1 | 0 | 0 | 0 | 0 | 0001111011010010 | +| 0101101110100000 | 0001111011010010 | 0 | 0 | 1 | 1 | 0 | 1 | 1010010001011111 | +| 0101101110100000 | 0001111011010010 | 1 | 1 | 0 | 0 | 0 | 1 | 1110000100101101 | +| 0101101110100000 | 0001111011010010 | 0 | 0 | 1 | 1 | 1 | 1 | 1010010001100000 | +| 0101101110100000 | 0001111011010010 | 1 | 1 | 0 | 0 | 1 | 1 | 1110000100101110 | +| 0101101110100000 | 0001111011010010 | 0 | 1 | 1 | 1 | 1 | 1 | 0101101110100001 | +| 0101101110100000 | 0001111011010010 | 1 | 1 | 0 | 1 | 1 | 1 | 0001111011010011 | +| 0101101110100000 | 0001111011010010 | 0 | 0 | 1 | 1 | 1 | 0 | 0101101110011111 | +| 0101101110100000 | 0001111011010010 | 1 | 1 | 0 | 0 | 1 | 0 | 0001111011010001 | +| 0101101110100000 | 0001111011010010 | 0 | 0 | 0 | 0 | 1 | 0 | 0111101001110010 | +| 0101101110100000 | 0001111011010010 | 0 | 1 | 0 | 0 | 1 | 1 | 0011110011001110 | +| 0101101110100000 | 0001111011010010 | 0 | 0 | 0 | 1 | 1 | 1 | 1100001100110010 | +| 0101101110100000 | 0001111011010010 | 0 | 0 | 0 | 0 | 0 | 0 | 0001101010000000 | +| 0101101110100000 | 0001111011010010 | 0 | 1 | 0 | 1 | 0 | 1 | 0101111111110010 | diff --git a/nand2tetris/projects/2/ALU.hdl b/nand2tetris/projects/2/ALU.hdl index dca8ac1..06f1b04 100644 --- a/nand2tetris/projects/2/ALU.hdl +++ b/nand2tetris/projects/2/ALU.hdl @@ -41,5 +41,33 @@ CHIP ALU { ng; // if (out < 0) equals 1, else 0 PARTS: - //// Replace this comment with your code. + + // zx + Mux16(a = x, b = false, sel = zx, out = xOrZero); + // nx + Not16(in = xOrZero, out = notx); + Mux16(a = xOrZero, b = notx, sel = nx, out = xOrNotx); + // zy + Mux16(a = y, b = false, sel = zy, out = yOrZero); + // ny + Not16(in = yOrZero, out = noty); + Mux16(a = yOrZero, b = noty, sel = ny, out = yOrNoty); + // f + And16(a = xOrNotx, b = yOrNoty, out = xAndy); + Add16(a = xOrNotx, b = yOrNoty, out = xPlusy); + Mux16(a = xAndy, b = xPlusy, sel = f, out = outf); + + // no + Not16(in = outf, out = notout); + + // -- OUT -- + // out + Mux16(a = outf, b = notout, sel = no, + out[0..7] = outLsb, out[8..15] = outMsb, out = out, out[15] = ng); + // zr + Or8Way(in = outLsb, out = OrLsb); + Or8Way(in = outMsb, out = OrMsb); + Or(a = OrMsb, b = OrLsb, out = AnyOut); + Not(in = AnyOut, out = zr); + } \ No newline at end of file diff --git a/nand2tetris/projects/2/ALU.out b/nand2tetris/projects/2/ALU.out new file mode 100644 index 0000000..2424a73 --- /dev/null +++ b/nand2tetris/projects/2/ALU.out @@ -0,0 +1,37 @@ +| x | y |zx |nx |zy |ny | f |no | out |zr |ng | +| 0000000000000000 | 1111111111111111 | 1 | 0 | 1 | 0 | 1 | 0 | 0000000000000000 | 1 | 0 | +| 0000000000000000 | 1111111111111111 | 1 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 | 0 | 0 | +| 0000000000000000 | 1111111111111111 | 1 | 1 | 1 | 0 | 1 | 0 | 1111111111111111 | 0 | 1 | +| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 0 | 0 | 0000000000000000 | 1 | 0 | +| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 0 | 0 | 1111111111111111 | 0 | 1 | +| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 0 | 1 | 1111111111111111 | 0 | 1 | +| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 0 | 1 | 0000000000000000 | 1 | 0 | +| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 1 | 1 | 0000000000000000 | 1 | 0 | +| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 1 | 1 | 0000000000000001 | 0 | 0 | +| 0000000000000000 | 1111111111111111 | 0 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 | 0 | 0 | +| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 1 | 1 | 1 | 0000000000000000 | 1 | 0 | +| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 1 | 0 | 1111111111111111 | 0 | 1 | +| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 1 | 0 | 1111111111111110 | 0 | 1 | +| 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 0 | 1 | 0 | 1111111111111111 | 0 | 1 | +| 0000000000000000 | 1111111111111111 | 0 | 1 | 0 | 0 | 1 | 1 | 0000000000000001 | 0 | 0 | +| 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 1 | 1 | 1 | 1111111111111111 | 0 | 1 | +| 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 0 | 0 | 0 | 0000000000000000 | 1 | 0 | +| 0000000000000000 | 1111111111111111 | 0 | 1 | 0 | 1 | 0 | 1 | 1111111111111111 | 0 | 1 | +| 0000000000010001 | 0000000000000011 | 1 | 0 | 1 | 0 | 1 | 0 | 0000000000000000 | 1 | 0 | +| 0000000000010001 | 0000000000000011 | 1 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 | 0 | 0 | +| 0000000000010001 | 0000000000000011 | 1 | 1 | 1 | 0 | 1 | 0 | 1111111111111111 | 0 | 1 | +| 0000000000010001 | 0000000000000011 | 0 | 0 | 1 | 1 | 0 | 0 | 0000000000010001 | 0 | 0 | +| 0000000000010001 | 0000000000000011 | 1 | 1 | 0 | 0 | 0 | 0 | 0000000000000011 | 0 | 0 | +| 0000000000010001 | 0000000000000011 | 0 | 0 | 1 | 1 | 0 | 1 | 1111111111101110 | 0 | 1 | +| 0000000000010001 | 0000000000000011 | 1 | 1 | 0 | 0 | 0 | 1 | 1111111111111100 | 0 | 1 | +| 0000000000010001 | 0000000000000011 | 0 | 0 | 1 | 1 | 1 | 1 | 1111111111101111 | 0 | 1 | +| 0000000000010001 | 0000000000000011 | 1 | 1 | 0 | 0 | 1 | 1 | 1111111111111101 | 0 | 1 | +| 0000000000010001 | 0000000000000011 | 0 | 1 | 1 | 1 | 1 | 1 | 0000000000010010 | 0 | 0 | +| 0000000000010001 | 0000000000000011 | 1 | 1 | 0 | 1 | 1 | 1 | 0000000000000100 | 0 | 0 | +| 0000000000010001 | 0000000000000011 | 0 | 0 | 1 | 1 | 1 | 0 | 0000000000010000 | 0 | 0 | +| 0000000000010001 | 0000000000000011 | 1 | 1 | 0 | 0 | 1 | 0 | 0000000000000010 | 0 | 0 | +| 0000000000010001 | 0000000000000011 | 0 | 0 | 0 | 0 | 1 | 0 | 0000000000010100 | 0 | 0 | +| 0000000000010001 | 0000000000000011 | 0 | 1 | 0 | 0 | 1 | 1 | 0000000000001110 | 0 | 0 | +| 0000000000010001 | 0000000000000011 | 0 | 0 | 0 | 1 | 1 | 1 | 1111111111110010 | 0 | 1 | +| 0000000000010001 | 0000000000000011 | 0 | 0 | 0 | 0 | 0 | 0 | 0000000000000001 | 0 | 0 | +| 0000000000010001 | 0000000000000011 | 0 | 1 | 0 | 1 | 0 | 1 | 0000000000010011 | 0 | 0 | diff --git a/nand2tetris/projects/2/Add16.hdl b/nand2tetris/projects/2/Add16.hdl index 769330a..27ba872 100644 --- a/nand2tetris/projects/2/Add16.hdl +++ b/nand2tetris/projects/2/Add16.hdl @@ -11,5 +11,21 @@ CHIP Add16 { OUT out[16]; PARTS: - //// Replace this comment with your code. + FullAdder(a = a[0], b = b[0], c = false, carry = c0, sum = out[0]); + + FullAdder(a = a[1], b = b[1], c = c0, carry = c1, sum = out[1]); + FullAdder(a = a[2], b = b[2], c = c1, carry = c2, sum = out[2]); + FullAdder(a = a[3], b = b[3], c = c2, carry = c3, sum = out[3]); + FullAdder(a = a[4], b = b[4], c = c3, carry = c4, sum = out[4]); + FullAdder(a = a[5], b = b[5], c = c4, carry = c5, sum = out[5]); + FullAdder(a = a[6], b = b[6], c = c5, carry = c6, sum = out[6]); + FullAdder(a = a[7], b = b[7], c = c6, carry = c7, sum = out[7]); + FullAdder(a = a[8], b = b[8], c = c7, carry = c8, sum = out[8]); + FullAdder(a = a[9], b = b[9], c = c8, carry = c9, sum = out[9]); + FullAdder(a = a[10], b = b[10], c = c9, carry = c10, sum = out[10]); + FullAdder(a = a[11], b = b[11], c = c10, carry = c11, sum = out[11]); + FullAdder(a = a[12], b = b[12], c = c11, carry = c12, sum = out[12]); + FullAdder(a = a[13], b = b[13], c = c12, carry = c13, sum = out[13]); + FullAdder(a = a[14], b = b[14], c = c13, carry = c14, sum = out[14]); + FullAdder(a = a[15], b = b[15], c = c14, carry = c15, sum = out[15]); } \ No newline at end of file diff --git a/nand2tetris/projects/2/Add16.out b/nand2tetris/projects/2/Add16.out new file mode 100644 index 0000000..20f9c65 --- /dev/null +++ b/nand2tetris/projects/2/Add16.out @@ -0,0 +1,7 @@ +| a | b | out | +| 0000000000000000 | 0000000000000000 | 0000000000000000 | +| 0000000000000000 | 1111111111111111 | 1111111111111111 | +| 1111111111111111 | 1111111111111111 | 1111111111111110 | +| 1010101010101010 | 0101010101010101 | 1111111111111111 | +| 0011110011000011 | 0000111111110000 | 0100110010110011 | +| 0001001000110100 | 1001100001110110 | 1010101010101010 | diff --git a/nand2tetris/projects/2/FullAdder.hdl b/nand2tetris/projects/2/FullAdder.hdl index 9e0aa5d..f4e005f 100644 --- a/nand2tetris/projects/2/FullAdder.hdl +++ b/nand2tetris/projects/2/FullAdder.hdl @@ -11,5 +11,9 @@ CHIP FullAdder { carry; // Left bit of a + b + c PARTS: - //// Replace this comment with your code. + + HalfAdder(a = a, b = b, carry = h, sum = l); + HalfAdder(a = l, b = c, carry = h2, sum = sum); + + Or(a = h, b = h2, out = carry); } \ No newline at end of file diff --git a/nand2tetris/projects/2/FullAdder.out b/nand2tetris/projects/2/FullAdder.out new file mode 100644 index 0000000..90fbc1d --- /dev/null +++ b/nand2tetris/projects/2/FullAdder.out @@ -0,0 +1,9 @@ +| a | b | c |sum|carry| +| 0 | 0 | 0 | 0 | 0 | +| 0 | 0 | 1 | 1 | 0 | +| 0 | 1 | 0 | 1 | 0 | +| 0 | 1 | 1 | 0 | 1 | +| 1 | 0 | 0 | 1 | 0 | +| 1 | 0 | 1 | 0 | 1 | +| 1 | 1 | 0 | 0 | 1 | +| 1 | 1 | 1 | 1 | 1 | diff --git a/nand2tetris/projects/2/HalfAdder.hdl b/nand2tetris/projects/2/HalfAdder.hdl index 7cc3196..7509988 100644 --- a/nand2tetris/projects/2/HalfAdder.hdl +++ b/nand2tetris/projects/2/HalfAdder.hdl @@ -11,5 +11,20 @@ CHIP HalfAdder { carry; // Left bit of a + b PARTS: - //// Replace this comment with your code. + /* + Xor(a = a, b = b, out = sum); + And(a = a, b = b, out = carry); + */ + + // Xor + Nand(a = a, b = b, out = Nandab); + + Nand(a = a, b = Nandab, out = Xora); + Nand(a = Nandab, b = b, out = Xorb); + + Nand(a = Xora, b = Xorb, out = sum); + + // And + Nand(a = Nandab, b = Nandab, out = carry); + } diff --git a/nand2tetris/projects/2/HalfAdder.out b/nand2tetris/projects/2/HalfAdder.out new file mode 100644 index 0000000..d081087 --- /dev/null +++ b/nand2tetris/projects/2/HalfAdder.out @@ -0,0 +1,5 @@ +| a | b |sum|car| +| 0 | 0 | 0 | 0 | +| 0 | 1 | 1 | 0 | +| 1 | 0 | 1 | 0 | +| 1 | 1 | 0 | 1 | diff --git a/nand2tetris/projects/2/Inc16.hdl b/nand2tetris/projects/2/Inc16.hdl index 4cacae6..535e159 100644 --- a/nand2tetris/projects/2/Inc16.hdl +++ b/nand2tetris/projects/2/Inc16.hdl @@ -11,5 +11,5 @@ CHIP Inc16 { OUT out[16]; PARTS: - //// Replace this comment with your code. + Add16(a = in, b[0] = true, b[1..15] = false, out = out); } \ No newline at end of file diff --git a/nand2tetris/projects/2/Inc16.out b/nand2tetris/projects/2/Inc16.out new file mode 100644 index 0000000..78de1fe --- /dev/null +++ b/nand2tetris/projects/2/Inc16.out @@ -0,0 +1,5 @@ +| in | out | +| 0000000000000000 | 0000000000000001 | +| 1111111111111111 | 0000000000000000 | +| 0000000000000101 | 0000000000000110 | +| 1111111111111011 | 1111111111111100 | diff --git a/nand2tetris/projects/2/project2.zip b/nand2tetris/projects/2/project2.zip new file mode 100644 index 0000000..25a8034 Binary files /dev/null and b/nand2tetris/projects/2/project2.zip differ