0293-Nand-实现基础逻辑门(三)

环境

  • Time 2023-07-06

前言

说明

参考:https://www.nand2tetris.org/

目标

接上一节,实现 Or8Way、Mux4Way16、Mux8Way16、DMux4Way、DMux8Way 五个基础门。

Or8Way

/**
 * 8-way Or:
 * out = (in[0] or in[1] or ... or in[7])
 */

CHIP Or8Way {
    IN in[8];
    OUT out;

    PARTS:
    // Put your code here:
    Or(a = in[0], b = in[1], out=o1);
    Or(a = in[2], b = o1, out=o2);
    Or(a = in[3], b = o2, out=o3);
    Or(a = in[4], b = o3, out=o4);
    Or(a = in[5], b = o4, out=o5);
    Or(a = in[6], b = o5, out=o6);
    Or(a = in[7], b = o6, out=out);
}

Mux4Way16

/**
 * 4-way 16-bit multiplexor:
 * out = a if sel == 00
 *       b if sel == 01
 *       c if sel == 10
 *       d if sel == 11
 */

CHIP Mux4Way16 {
    IN a[16], b[16], c[16], d[16], sel[2];
    OUT out[16];

    PARTS:
    // Put your code here:
    Mux16(a = a, b = b, sel = sel[0], out = o1);
    Mux16(a = c, b = d, sel = sel[0], out = o2);
    Mux16(a = o1, b = o2, sel = sel[1], out = out);
}

DMux4Way

/**
 * 4-way demultiplexor:
 * {a, b, c, d} = {in, 0, 0, 0} if sel == 00
 *                {0, in, 0, 0} if sel == 01
 *                {0, 0, in, 0} if sel == 10
 *                {0, 0, 0, in} if sel == 11
 */

CHIP DMux4Way {
    IN in, sel[2];
    OUT a, b, c, d;

    PARTS:
    // Put your code here:
    DMux(in = in, sel = sel[1], a = o1, b = o2);
    DMux(in = o1, sel = sel[0], a = a, b = b);
    DMux(in = o2, sel = sel[0], a = c, b = d);
}

DMux8Way

/**
 * 8-way demultiplexor:
 * {a, b, c, d, e, f, g, h} = {in, 0, 0, 0, 0, 0, 0, 0} if sel == 000
 *                            {0, in, 0, 0, 0, 0, 0, 0} if sel == 001
 *                            etc.
 *                            {0, 0, 0, 0, 0, 0, 0, in} if sel == 111
 */

CHIP DMux8Way {
    IN in, sel[3];
    OUT a, b, c, d, e, f, g, h;

    PARTS:
    // Put your code here:
    DMux(in = in, sel = sel[2], a = o1, b = o2);
    DMux4Way(in = o1, sel = sel[0..1], a = a, b = b, c = c, d = d);
    DMux4Way(in = o2, sel = sel[0..1], a = e, b = f, c = g, d = h);
}

总结

使用基础的逻辑门,实现了多路逻辑门。

附录

posted @ 2025-10-30 17:47  jiangbo4444  阅读(11)  评论(0)    收藏  举报