How to build a 8 bit wide hdl ALU

103 views Asked by At

Here is my attempted hdl code so far for a 8 bit wide ALU chip, but Im not sure how to do the rest.

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

PARTS:
// process the x input
Mux8(a=x, b=false, sel=zx, out=xOrZero);
Not8(in=xOrZero, out=xInverted);
Mux8(a=xOrZero, b=xInverted, sel=nx, out=xOperandToUse);

// process the y input
Mux8(a=y, b=false, sel=zy, out=yOrZero);
Not8(in=yOrZero, out=yInverted);
Mux8(a=yOrZero, b=yInverted, sel=ny, out=yOperandToUse);

// something for And

// something for Add

// something to choose between them

// something to negate the output if needed

// set the zero flag
Or8Way(in=/* outputValue */, out=resultNonZero);
Not(in=resultNonZero, out=zr);

// remember to set the negative flag too....

}

Any help would be greatly appreciated. Thank you so much in advance

1

There are 1 answers

0
MadOverlord On

You have made a good start. For both your X and Y inputs, you have a nice flow of data from top to bottom and the logic is correct (as far as it goes).

You just need to continue building on it, keeping in mind that things happen in parallel. So just as the blocks that generate xOperandToUse and yOperandToUse happen in parallel, do the same thing for the And and Add results; build components the generate both of them, then pick between them (based on f), and pass on the output to the next decision (negate ouput in this case).

Or to put it another way, in a programming language you do "if-else", in the chip you do "both-pick one".

My only other suggestion is you make your labels a little shorter. In my experience for stuff like this, it makes the code a little easier to read, because inside your head, it's less tokens per symbol. In my implementation of the 16-bit ALU, I did:

   Mux16(a=x,b=false,sel=zx,out=x0);           // x0 = x or 0, depending on zx
   Not16(in=x0,out=notx);                      // notx = !x0 (which is either 0 or a)
   Mux16(a=x0,b=notx,sel=nx,out=xin);          // xin = x0 or notx, depending on nx

With low-level code like this (or assembly), I also find it helpful to comment every line with a higher-level explanation of what the code is supposed to do -- it helps when reviewing the code at a later date.