Implement basic logic gates: Not, And, Or, Xor, Mux, DMux

This commit is contained in:
2025-08-27 19:07:20 +02:00
parent beb668806a
commit ad5f774ae0
6 changed files with 32 additions and 13 deletions

View File

@@ -4,12 +4,14 @@
// File name: projects/1/And.hdl // File name: projects/1/And.hdl
/** /**
* And gate: * And gate:
* if (a and b) out = 1, else out = 0 * if (a and b) out = 1, else out = 0
*/ */
CHIP And { CHIP And {
IN a, b; IN a, b;
OUT out; OUT out;
PARTS: PARTS:
//// Replace this comment with your code. // And(a,b) = Not(Nand(a,b))
} Nand(a=a, b=b, out=nandOut);
Not(in=nandOut, out=out);
}

View File

@@ -12,5 +12,8 @@ CHIP DMux {
OUT a, b; OUT a, b;
PARTS: 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);
} }

View File

@@ -2,7 +2,7 @@
// and the book "The Elements of Computing Systems" // and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press. // by Nisan and Schocken, MIT Press.
// File name: projects/1/Mux.hdl // File name: projects/1/Mux.hdl
/** /**
* Multiplexor: * Multiplexor:
* if (sel = 0) out = a, else out = b * if (sel = 0) out = a, else out = b
*/ */
@@ -11,5 +11,9 @@ CHIP Mux {
OUT out; OUT out;
PARTS: PARTS:
//// Replace this comment with your code. // 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);
}

View File

@@ -11,5 +11,6 @@ CHIP Not {
OUT out; OUT out;
PARTS: PARTS:
//// Replace this comment with your code. // Not(in) = Nand(in, in)
Nand(a=in, b=in, out=out);
} }

View File

@@ -4,12 +4,16 @@
// File name: projects/1/Or.hdl // File name: projects/1/Or.hdl
/** /**
* Or gate: * Or gate:
* if (a or b) out = 1, else out = 0 * if (a or b) out = 1, else out = 0
*/ */
CHIP Or { CHIP Or {
IN a, b; IN a, b;
OUT out; OUT out;
PARTS: 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);
} }

View File

@@ -11,5 +11,10 @@ CHIP Xor {
OUT out; OUT out;
PARTS: PARTS:
//// Replace this comment with your code. // 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);
}