如何构建8位宽的HDL ALU

问题描述 投票:0回答:1

这是我到目前为止尝试的 8 位宽 ALU 芯片的 hdl 代码,但我不知道如何做其余的事情。

CHIP ALU {
IN
    x[8], y[8],  // 8-bit inputs
    zx, // zero the x input?
    nx, // negate the x input?
    zy, // zero the y input?
    ny, // negate the y input?
    f,  // compute out = x + y (if 1) or x & y (if 0)
    no; // negate the out output?

OUT
    out[8], // 8-bit output
    zr, // 1 if (out == 0), 0 otherwise
    ng; // 1 if (out < 0),  0 otherwise

PARTS:
// process the x input
Mux8(a=x, b=false, sel=zx, out=xOrZero);
Not8(in=xOrZero, out=xInverted);
Mux8(a=xOrZero, b=xInverted, sel=nx, out=xOperandToUse);

// process the y input
Mux8(a=y, b=false, sel=zy, out=yOrZero);
Not8(in=yOrZero, out=yInverted);
Mux8(a=yOrZero, b=yInverted, sel=ny, out=yOperandToUse);

// something for And

// something for Add

// something to choose between them

// something to negate the output if needed

// set the zero flag
Or8Way(in=/* outputValue */, out=resultNonZero);
Not(in=resultNonZero, out=zr);

// remember to set the negative flag too....

}

任何帮助将不胜感激。提前非常感谢您

hdl alu nand2tetris
1个回答
0
投票

你已经有了一个好的开始。对于您的 X 和 Y 输入,您有一个从上到下的良好数据流,并且逻辑是正确的(就目前而言)。

你只需要继续在此基础上进行构建,记住事情是并行发生的。因此,就像生成 xOperandToUse 和 yOperandToUse 的块并行发生一样,对 And 和 Add 结果执行相同的操作;构建组件生成它们,然后在它们之间进行选择(基于 f),并将输出传递到下一个决策(在本例中否定输出)。

或者换句话说,在编程语言中你执行“if-else”,在芯片中你执行“both-pick one”。

我唯一的其他建议是你让你的标签短一点。根据我对此类内容的经验,它使代码更易于阅读,因为在您的头脑中,每个符号的标记更少。在我的 16 位 ALU 实现中,我做了:

   Mux16(a=x,b=false,sel=zx,out=x0);           // x0 = x or 0, depending on zx
   Not16(in=x0,out=notx);                      // notx = !x0 (which is either 0 or a)
   Mux16(a=x0,b=notx,sel=nx,out=xin);          // xin = x0 or notx, depending on nx

对于像这样的低级代码(或程序集),我还发现用对代码应该执行的操作的更高级别解释来注释每一行很有帮助——这有助于以后查看代码。

© www.soinside.com 2019 - 2024. All rights reserved.