4-Bit ALU Demonstration
Complete 4-bit ALU performing arithmetic and logic operations. Advanced combinational circuit for CPU design.
您将学到什么
- Perform arithmetic (add, subtract) and logical (AND, OR, XOR) operations on 4-bit operands.
- Use an operation-select code to route the appropriate functional unit's output.
- Generate status flags (zero, carry, overflow) as side outputs.
- Recognize the ALU as the heart of every CPU.
- Apply two's complement for subtraction using add-with-inverted-B.
工作原理
An Arithmetic Logic Unit (ALU) performs arithmetic and logical operations on multi-bit inputs. This 4-bit ALU takes two 4-bit operands (A, B) and an operation-select code, producing a 4-bit result and status flags (zero, carry, overflow).
Typical operation set for a small ALU: - Arithmetic: ADD (A + B), SUB (A − B via two's-complement add). - Logical: AND, OR, XOR (bitwise on each bit pair). - Other: NOT (invert A), shift left/right, sometimes increment.
The operation-select code is decoded by combinational logic that drives an internal MUX to choose which functional unit's output reaches the ALU output: - ADD/SUB use a 4-bit ripple-carry adder with the SUB control inverting B and setting Cin = 1 (two's complement). - Bitwise operations are pure combinational gate networks per bit. - A final MUX picks among them based on the op-select.
Status flags are generated as side outputs: - Zero (Z): OR-NOR-reduce of result bits — high when result is 0. - Carry (C): Carry-out of the adder. - Overflow (V): Detects signed-arithmetic overflow.
ALUs are at the heart of every CPU. Modern CPUs scale this 4-bit demo to 32 or 64 bits, with many more operations and aggressive optimization (carry-lookahead adders, dedicated multipliers, etc.).
逐步尝试
在上方嵌入式电路中设置输入,然后阅读预期结果并验证。
- 1A = 0011 B = 0010 Op = ADD预期:
Result = 0101 (= 5), Z=0, C=0您将看到: 3 + 2 = 5. Sum bit pattern visible. No overflow because result fits in 4 bits. - 2A = 0101 B = 0011 Op = SUB预期:
Result = 0010 (= 2), Z=0, C=1您将看到: 5 − 3 = 2. Internally A + ~B + 1; the carry-out C = 1 indicates no borrow. - 3A = 1100 B = 1010 Op = AND预期:
Result = 1000您将看到: Bitwise AND — only bits where both inputs are 1 stay high. 12 AND 10 = 8. - 4A = 0011 B = 1100 Op = OR预期:
Result = 1111您将看到: Bitwise OR — every bit is set in at least one input, so result is all 1s. - 5A = 1010 B = 1010 Op = XOR预期:
Result = 0000, Z=1您将看到: Bitwise XOR — equal inputs produce 0. Zero flag fires. Useful for equality testing.
使用的组件
实际应用
Every CPU. Each integer ALU in a CPU implements addition, subtraction, AND, OR, XOR, NOT, shifts, and comparisons. The 4-bit demo scales to 32 or 64 bits in production.
Programmable controllers. PLCs use simple ALUs for setpoint comparisons, arithmetic on sensor data, and Boolean control logic.
DSP processors. DSPs include specialized ALUs for fixed-point arithmetic plus dedicated multiplier units for sum-of-products operations.
Educational architectures (MIPS, RISC-V). Teaching CPU design typically starts with a small ALU as the first component built and verified.
Hardware accelerators. Custom-designed accelerators (cryptography, signal processing) often have specialized ALUs tuned to their algorithm's specific operations.