Search code examples
hdlalu

ALU hdl produces wrong values


(nand2tetris course)

Expected result:

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

My HDL code's result:

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

My hdl code:

// 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);
}

Intermediate values:

enter image description here

Notice that yMuxNY should be -1, so that is where it starts to go wrong!

Full hardwareSimulater screenshot:

enter image description here


Solution

  • I fixed it! The input of Not16 was not x but xMuxZX, similarly for the y Not16. The following works:

    // 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);