We have come across the arithmetic part, which was binary addition and subtraction. We have also come across the logical parts, such as AND, OR, NOT, and XOR gates. Now, the natural progression is to combine these circuitries to form a unit. So, that’s the ALU.
Let’s have my implementation presented as follows. I first had a 1-bit ALU, which is a simpler circuitry:
// control signals // [ input 1 invert, input 2 invert/negetive, ...operations(2 bit)] // operations // [0,0] -> AND // [0,1] -> OR // [1,0] -> XOR // [1,1] -> ADD export function aluBit1(carryIn: Bit, inp1: Bit, inp2: Bit, controlBits: Bit4): [result: Bit, carryOut: Bit] { const inp1Invert = controlBits[0]; const inp2Invert = controlBits[1]; const operationBits = controlBits.slice(2) as Bit2; const inp1Transformed = xorGate(inp1, inp1Invert); const inp2Transformed = xorGate(inp2, inp2Invert); const andResult = andGate(inp1Transformed, inp2Transformed); const orResult = orGate(inp1Transformed, inp2Transformed); const xorResult = xorGate(inp1Transformed, inp2Transformed); const [addResult, carryOut] = fullAdder(carryIn, inp1Transformed, inp2Transformed); const result = mux4To1( [ andResult, orResult, xorResult, addResult, ] , operationBits ); return [result, carryOut]; } There are four control bits in total. The first two are for inverting the two inputs, and the remaining two are inputs to the mux to select the desired primitive operations. With 2 bits for operation control, we get a total of four unique operations. I have chosen AND, OR, XOR, and ADD. You might think: where is the NOT operation? What about subtraction? How can we get other gates, such as NOR and NAND? Those are some legitimate queries.
...