在 HDL 中处理数组

Dealing with arrays in HDL

如何在 HDL 中使用数组(表示总线)?

比如我有如下代码:

/**
 * 16-bit bitwise And:
 * for i = 0..15: out[i] = (a[i] and b[i])
 */

CHIP And16 {
    IN a[16], b[16];
    OUT out[16];

    PARTS:
    // Put your code here:
}

假设我已经实施了 And,我该如何实施?

我不想有以下内容:

And(a=a[0],b=b[0],out=out[0]);
And(a=a[1],b=b[1],out=out[1]);
...
And(a=a[14],b=b[14],out=out[14]);
And(a=a[15],b=b[15],out=out[15]);

There are no arrays in HDL. In section 1.3 of the nand2tetris companion book,他说

Since we already know how to implement the elementary versions of these gates, the implementation of their n-ary versions is simply a matter of constructing arrays of n elementary gates, having each gate operate separately on its bits. This implementation task is rather boring, but it will carry its weight when these multi-bit gates are used in more complex chips, as described in subsequent chapters.

因此,除了在 Python 中编写一个简单的脚本来避免所有输入之外,您不会遗漏任何东西。