环境
- Time 2023-07-06
前言
说明
参考:https://www.nand2tetris.org/
目标
通过最基础的 Nand(与非门)实现 Not、And、Or、Xor、Mux、DMux 几个基础门。
Not
/*** Not gate:* out = not in*/
/** Sets out = in Nand in */
CHIP Not {IN in;OUT out;PARTS:// Put your code here:Nand(a = in, b = in, out = out);
}
And
/*** And gate:* out = 1 if (a == 1 and b == 1)*       0 otherwise*/
/** Sets out = Not(a Nand b) */
CHIP And {IN a, b;OUT out;PARTS:// Put your code here:Nand(a = a, b = b, out=aNandb);Not(in = aNandb, out = out);
}
Or
 /*** Or gate:* out = 1 if (a == 1 or b == 1)*       0 otherwise*/
/** Sets out = Not(Not(a) And Not(b)) */
CHIP Or {IN a, b;OUT out;PARTS:// Put your code here:Not(in = a, out = Nota);Not(in = b, out = Notb);And(a = Nota, b = Notb, out = NotaAndNotb);Not(in = NotaAndNotb, out = out);
}
Xor
/*** Exclusive-or gate:* out = not (a == b)*/
/** Sets out = ((a And Not(b)) Or (Not(a) And b)) */
CHIP Xor {IN a, b;OUT out;PARTS:// Put your code here:Not(in = b, out = Notb);And(a = a, b = Notb, out = aAndNotb);Not(in = a, out = Nota);And(a = Nota, b = b, out = NotaAndb);Or(a = aAndNotb, b = NotaAndb, out = out);
}
Mux
/*** Multiplexor:* out = a if sel == 0*       b otherwise*/
/** Sets out = ((a And Not(sel)) Or (b And sel)) */
CHIP Mux {IN a, b, sel;OUT out;PARTS:// Put your code here: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);
}
DMux
/*** Demultiplexor:* {a, b} = {in, 0} if sel == 0*          {0, in} if sel == 1*/
/** Sets a = (in And (Not(sel))) */
/** Sets b = (in And sel) */
CHIP DMux {IN in, sel;OUT a, b;PARTS:// Put your code here:Not(in = sel, out = Notsel);And(a = in, b = Notsel, out = a);And(a = in, b = sel, out = b);
}
总结
使用 Nand 这一个最基础的逻辑门,实现其它的基础逻辑门。