diff --git a/01/And.hdl b/01/And.hdl index 658ff73..d48a48e 100644 --- a/01/And.hdl +++ b/01/And.hdl @@ -4,12 +4,14 @@ // File name: projects/1/And.hdl /** * And gate: - * if (a and b) out = 1, else out = 0 + * if (a and b) out = 1, else out = 0 */ CHIP And { IN a, b; OUT out; - + PARTS: - //// Replace this comment with your code. -} \ No newline at end of file + // And(a,b) = Not(Nand(a,b)) + Nand(a=a, b=b, out=nandOut); + Not(in=nandOut, out=out); +} diff --git a/01/DMux.hdl b/01/DMux.hdl index 9dbdfb9..6d290e1 100644 --- a/01/DMux.hdl +++ b/01/DMux.hdl @@ -12,5 +12,8 @@ CHIP DMux { OUT a, b; PARTS: - //// Replace this comment with your code. + // DMux(in,sel) -> a = And(in, Not(sel)), b = And(in, sel) + Not(in=sel, out=notSel); + And(a=in, b=notSel, out=a); + And(a=in, b=sel, out=b); } diff --git a/01/Mux.hdl b/01/Mux.hdl index 9f9e720..35fc312 100644 --- a/01/Mux.hdl +++ b/01/Mux.hdl @@ -2,7 +2,7 @@ // and the book "The Elements of Computing Systems" // by Nisan and Schocken, MIT Press. // File name: projects/1/Mux.hdl -/** +/** * Multiplexor: * if (sel = 0) out = a, else out = b */ @@ -11,5 +11,9 @@ CHIP Mux { OUT out; PARTS: - //// Replace this comment with your code. -} \ No newline at end of file + // Mux(a,b,sel) = Or(And(a, Not(sel)), And(b, sel)) + Not(in=sel, out=notSel); + And(a=a, b=notSel, out=aAndNotSel); + And(a=b, b=sel, out=bAndSel); + Or(a=aAndNotSel, b=bAndSel, out=out); +} diff --git a/01/Not.hdl b/01/Not.hdl index 9ce0063..7924706 100644 --- a/01/Not.hdl +++ b/01/Not.hdl @@ -11,5 +11,6 @@ CHIP Not { OUT out; PARTS: - //// Replace this comment with your code. + // Not(in) = Nand(in, in) + Nand(a=in, b=in, out=out); } diff --git a/01/Or.hdl b/01/Or.hdl index 1aa2dd7..306d5e5 100644 --- a/01/Or.hdl +++ b/01/Or.hdl @@ -4,12 +4,16 @@ // File name: projects/1/Or.hdl /** * Or gate: - * if (a or b) out = 1, else out = 0 + * if (a or b) out = 1, else out = 0 */ CHIP Or { IN a, b; OUT out; PARTS: - //// Replace this comment with your code. + // Or(a,b) = Not(And(Not(a), Not(b))) - De Morgan's law + Not(in=a, out=notA); + Not(in=b, out=notB); + And(a=notA, b=notB, out=andOut); + Not(in=andOut, out=out); } diff --git a/01/Xor.hdl b/01/Xor.hdl index c6d90f9..8e93f6e 100644 --- a/01/Xor.hdl +++ b/01/Xor.hdl @@ -11,5 +11,10 @@ CHIP Xor { OUT out; PARTS: - //// Replace this comment with your code. -} \ No newline at end of file + // Xor(a,b) = Or(And(a, Not(b)), And(Not(a), b)) + Not(in=a, out=notA); + Not(in=b, out=notB); + And(a=a, b=notB, out=aAndNotB); + And(a=notA, b=b, out=notAAndB); + Or(a=aAndNotB, b=notAAndB, out=out); +}