Do not think of the ALU as a collection of functions that you need to implement individually.
If you follow the instructions in the book and the pseudocode in the ALU.hdl skeleton file you will build an ALU that does all the required operations.
/**
* The ALU. Computes a pre-defined set of functions out = f(x,y)
* where x and y are two 16-bit inputs. The function f is selected
* by a set of 6 control bits denoted zx, nx, zy, ny, f, no.
* The ALU operation can be described using the following pseudocode:
* if zx=1 set x = 0 // 16-bit zero constant
* if nx=1 set x = !x // Bit-wise negation
* if zy=1 set y = 0 // 16-bit zero constant
* if ny=1 set y = !y // Bit-wise negation
* if f=1 set out = x + y // Integer 2's complement addition
* else set out = x & y // Bit-wise And
* if no=1 set out = !out // Bit-wise negation
*
* In addition to computing out, the ALU computes two 1-bit outputs:
* if out=0 set zr = 1 else zr = 0 // 16-bit equality comparison
* if out<0 set ng = 1 else ng = 0 // 2's complement comparison
*/
Since in HDL each signal needs a name, this pseudocode needs to be expanded to indicate the linear nature of hardware.
if zx then
x1 ← 0
else
x1 ← x
if nx then
x2 ← ! x1
else
x2 ← x1
...
Muxes make choices so they are used to implement the if/then/else concept. The first if/then/else can be implemented as
Mux16 (a=x, b=false, sel=zx, out=x1);
There is a worksheet that will help you see how the ALU computes the correct functions using this design.
--Mark