Login  Register

Shortening ALU Code

Posted by Zebo on May 19, 2011; 6:38am
URL: http://nand2tetris-questions-and-answers-forum.52.s1.nabble.com/Shortening-ALU-Code-tp2960233.html

I tried to avoid the "sub bus of an internal node" problem and just use the final out to make my flaggers but OF COURSE another error came up.

Here is the code:
CHIP ALU {

    IN  // 16-bit inputs:
        x[16], y[16],
        // Control bits:
        zx, // Zero the x input
        nx, // Negate the x input
        zy, // Zero the y input
        ny, // Negate the y input
        f,  // Function code: 1 for add, 0 for and
        no; // Negate the out output

    OUT // 16-bit output
        out[16],

        // ALU output flags
        zr, // 1 if out=0, 0 otherwise
        ng; // 1 if out<0, 0 otherwise

    PARTS:
    ... // Working parts removed
    Not16(in=d, out=notd);
    Mux16(a=d, b=notd, sel=no, out[0..15]=out[0..15]);
    Or8Way(in=out[0..7], out=p);
    Or8Way(in=out[8..15], out=q);
    Or(a=p, b=q, out=r);
    Not(in=r, out=zr);
    And(a=true, b=out[15], out=ng);
}
Am I going to have to just do a completely different setup or is there a way to make this work?