ALU8 (Arithmetic Logic Unit)

Inputs

PinTypeDescription
Abus8First operand
Bbus8Second operand (ignored by NOT, SHL, SHR)
Opcodebus8Operation code (0–7)

Outputs

PinTypeDescription
Resultbus88-bit operation result
Zerobit1 if Result = 0, otherwise 0. Used for conditional jumps (JZ).

How It Works

The ALU8 performs one of 8 operations selected by a 3-bit Opcode. The full 8-bit Opcode input is used, but only the lower 3 bits matter (values 0–7).

OpcodeOperationDescription
0ADDResult = A + B (mod 256)
1SUBResult = A − B (mod 256)
2ANDResult = A & B (bitwise)
3ORResult = A | B (bitwise)
4XORResult = A ^ B (bitwise)
5NOTResult = ~A (bitwise, B is ignored)
6SHLResult = A << 1 (left shift by 1, B is ignored)
7SHRResult = A >> 1 (right shift by 1, B is ignored)

Operations 5, 6, and 7 (NOT, SHL, SHR) are unary — they ignore the B input entirely and operate only on A.

The Zero flag outputs 1 when Result equals 0. This flag is critical for the CPU's conditional jump instruction (JZ): when the ALU produces a zero result, the processor can branch to a different ROM address.

Usage

Level 13. The ALU is the heart of the processor — it performs all arithmetic and logical operations. Inside the CPU (level 16), the accumulator feeds into the ALU's A input, RAM provides the B input, and the decoded opcode selects the operation. The result is written back to the accumulator or RAM. Available as a component in levels 13–18 and 25.