ALU8 (Arithmetic Logic Unit)
Inputs
| Pin | Type | Description |
|---|---|---|
| A | bus8 | First operand |
| B | bus8 | Second operand (ignored by NOT, SHL, SHR) |
| Opcode | bus8 | Operation code (0–7) |
Outputs
| Pin | Type | Description |
|---|---|---|
| Result | bus8 | 8-bit operation result |
| Zero | bit | 1 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).
| Opcode | Operation | Description |
|---|---|---|
| 0 | ADD | Result = A + B (mod 256) |
| 1 | SUB | Result = A − B (mod 256) |
| 2 | AND | Result = A & B (bitwise) |
| 3 | OR | Result = A | B (bitwise) |
| 4 | XOR | Result = A ^ B (bitwise) |
| 5 | NOT | Result = ~A (bitwise, B is ignored) |
| 6 | SHL | Result = A << 1 (left shift by 1, B is ignored) |
| 7 | SHR | Result = 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.