Shortening ALU Code

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Shortening ALU Code

Zebo
This post was updated on .
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?
Reply | Threaded
Open this post in threaded view
|

Re: Shortening ALU Code

cadet1620
Administrator
Zebo wrote
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:
    ...
    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);
    ...
Am I going to have to just do a completely different setup or is there a way to make this work?
You're very close.  The clue that you're missing is that you can connect more than one bus or wire to an output:
    Mux16(a=d, b=notd, sel=no, out=out, out[0..7]=lowbyte, etc.);
    Or8Way(in=lowbyte, out=p);
    Or8Way(in=highbyte, out=q);
And you get the ng flag for free this way, too.

--Mark

[Plese edit your post to remove the working part of your ALU.  It's better for people to design it themselves rather than find it by searching the forum.]