Saturday, August 25, 2012

4-bit ALU - Design

Today I finished coding my ALU in Hardware Description Language. I had already implemented a 16-bit ALU, so I didn't have to change much; I just switched all the 16-bit gates to 4-bit gates and substituted NAND gates for NOT gates, as I don't have any NOT gates. Though I came up with this particular implementation, the functionality was detailed in this book. Here's the truth table I used to build it:


As you can see, it performs a number of different functions, but it doesn't multiply or divide. While I may add that function to the 16-bit ALU, I'm sticking with these functions for the 4-bit ALU.

I've copied the code I wrote for the 4-bit ALU below. (NOTE: ALU CODE HAS BEEN CHANGED) The language is fairly self explanatory, but I've also included a link to the Hardware Simulator I used and materials explaining how to interpret it.

So far so good!

4-Bit ALU HDL Code v1.0

CHIP ALU4 {
    IN
        x[4], y[4],  // 4-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 out = x & y (if 0)
        no; // negate the out output?

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

    PARTS:
    Nand4(a[0]=zx, a[1]=zy, b=true, out[0]=w1, out[1]=w5);
    And4(a=x, b[0]=w1, b[1]=w1, b[2]=w1, b[3]=w1, out=w2);
    Nand4(a=w2, b=true, out=w3);
    Mux4(a=w2, b=w3, sel=nx, out=w4);
    And4(a=y, b[0]=w5, b[1]=w5, b[2]=w5, b[3]=w5, out=w6);
    Nand4(a=w6, b=true, out=w7);
    Mux4(a=w6, b=w7, sel=ny, out=w8);
    And4(a=w4, b=w8, out=w9);
    Add4(a=w4, b=w8, out=w10);
    Mux4(a=w9, b=w10, sel=f, out=w11);
    Nand4(a=w11, b=true, out=w12);
    Mux4(a=w11, b=w12, sel=no, out=w13, out=w14, out[3]=w15, out=out);
    Or4Way(in=w13, out=w16);
    Nand(a=w16, b=true, out=zr);
    And(a=w15, b=true, out=ng);
}

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. I had downloaded the programs even before I asked that question. Duh.

      Delete
    2. Lol, ok. Do you want me to leave this comment here? I think I can remove the whole thing if you want.

      Delete