ALU hdl 产生错误的值

ALU hdl produces wrong values

(nand2tetris课程)

预期结果:

|        x         |        y         |zx |nx |zy |ny | f |no |       out        |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 |

我的 HDL 代码的结果:

|        x         |        y         |zx |nx |zy |ny | f |no |       out        |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 1 | 1 | 1 | 1 | 0000000000000000 |

我的hdl代码:

// Implementation: the ALU logic manipulates the x and y inputs
// and operates on the resulting values, as follows:
// if (zx == 1) set x = 0        // 16-bit constant
// if (nx == 1) set x = !x       // bitwise not
// if (zy == 1) set y = 0        // 16-bit constant
// if (ny == 1) set y = !y       // bitwise not
// if (f == 1)  set out = x + y  // integer 2's complement addition
// if (f == 0)  set out = x & y  // bitwise and
// if (no == 1) set out = !out   // bitwise not
// if (out == 0) set zr = 1
// if (out < 0) set ng = 1

CHIP ALU {
    IN  
        x[16], y[16],  // 16-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[16], // 16-bit output
        zr, // 1 if (out == 0), 0 otherwise
        ng; // 1 if (out < 0),  0 otherwise

    PARTS:
    // Input Transformation
    Not16(in=y,out=noty);
    Not16(in=x,out=notx);

    // Pre-setting x
    Mux16 (a=x,      b=false, sel=zx, out=xMuxZX);
    Mux16 (a=xMuxZX, b=notx,  sel=nx, out=xMuxNX);

    // Pre-setting y
    Mux16 (a=y,      b=false, sel=zy, out=yMuxZY);   
    Mux16 (a=yMuxZY, b=noty,  sel=ny, out=yMuxNY);

    // f  (Selecting between computing + or &)
    And16 (a=xMuxNX, b=yMuxNY, out=yANDx);
    Add16 (a=xMuxNX, b=yMuxNY, out=yADDx);
    Mux16 (a=yANDx,  b=yADDx,  sel=f, out=outF);

    // no (Post-setting the output)
    Not16 (in=outF,out=NOToutF);
    Mux16 (a=outF, b=NOToutF, sel=no, out=out);
}

中间值:

注意 yMuxNY 应该是-1,所以这就是它开始出错的地方!

完整 hardwareSimulater 屏幕截图:

我修好了! Not16 的输入不是 x 而是 xMuxZX,与 y Not16 类似。以下作品:

// Pre-setting x
Mux16 (a=x,      b=false, sel=zx, out=xMuxZX);
Not16 (in=xMuxZX, out=notx);
Mux16 (a=xMuxZX, b=notx,  sel=nx, out=xMuxNX);

// Pre-setting y
Mux16 (a=y,      b=false, sel=zy, out=yMuxZY); 
Not16 (in=yMuxZY, out=noty);  
Mux16 (a=yMuxZY, b=noty,  sel=ny, out=yMuxNY); 

// f  (Selecting between computing + or &)
And16 (a=xMuxNX, b=yMuxNY, out=yANDx);
Add16 (a=xMuxNX, b=yMuxNY, out=yADDx);
Mux16 (a=yANDx,  b=yADDx,  sel=f, out=outF);

// no (Post-setting the output)
Not16 (in=outF,out=NOToutF);
Mux16 (a=outF, b=NOToutF, sel=no, out=out);