Arithmetic Logic Unit
In questa pagina 6
In questa pagina 5
The ALU is the part of the processor that produces new information; everything else moves or stores data. It receives two operands (from registers or from the instruction), an operation code from the control unit (Hardwired and Microprogrammed ControlWhat the control unit does, control signals of a single-cycle datapath with the table for each instruction class, hardwired control (combinational or finite-state machine), microprogrammed control (control memory, horizontal and vertical microinstructions, microsequencer), and a comparison.Hardwired and Microprogrammed Control →), and produces the result and the flags N, Z, C, V stored in the status register (CPU Structure and RegistersArchitecture versus organization, the stored-program (von Neumann) machine, the internal components of a CPU (ALU, control unit, register file, internal bus), user-visible and control/status registers, the status word with flags and supervisor bit, and how many registers are worth having.CPU Structure and Registers →). Typical structure: an adder (AddersHalf adder and full adder, generate-propagate-delete decomposition, the ripple-carry adder and its linear worst-case delay, the adder-subtractor, and the fast structures carry-lookahead, carry-bypass, carry-select and square-root carry-select with their delay formulas and optimal block sizes.Adders →), a logic unit (AND, OR, XOR, NOT bit by bit, in parallel) and a multiplexer choosing the output.
1-bit ALU
Definition (1-bit ALU). For bit : an AND gate, an OR gate, a full adder and a multiplexer that selects which result goes to the output.
- inputs: , , carry , operation select;
- outputs: result , carry .
Example. With , , the full adder gives , ; the multiplexer chooses whether AND, OR or the sum goes out.
N-bit ALU
Definition (N-bit ALU: subtraction, NOR, slt, zero, overflow). one-bit ALUs with carries in cascade (or with carry lookahead). Additions:
- Subtraction: a signal Binvert passes instead of and sets (two's complement, see Binary ArithmeticBinary addition and two's complement subtraction, overflow for unsigned (carry) and signed (sign change) numbers, the N Z C V flags, multiplication by shifts and additions, Booth's algorithm with a worked example, division, and logical, arithmetic and rotate shifts.Binary Arithmetic →).
- NOR: with Ainvert and Binvert, the AND becomes (De Morgan).
- Set less than (
sltin MIPS): compute ; the sign bit of the result (corrected for overflow) becomes bit 0 of the output, other bits are 0.- Zero: (NOR of all result bits); used for conditional branches on equality.
- Overflow: .
Typical signal encoding (MIPS style):
| Ainvert | Binvert | Operation | Function |
|---|---|---|---|
| 0 | 0 | AND | |
| 0 | 0 | OR | |
| 0 | 0 | add | arithmetic |
| 0 | 1 | add with | |
| 0 | 1 | less | slt |
| 1 | 1 | AND | NOR |
Example (flags after an 8-bit subtraction). (80), (112): .
- signed result ;
- N = 1, Z = 0;
- C = 0: no carry out, i.e. a borrow occurred: unsigned ;
- V = 0: a subtraction of two numbers of the same sign cannot overflow (as a sum, has operands of different sign).
This is exactly what CMP does in ARM before a conditional branch.
Flags
- Z: all result bits are 0. N: MSB of the result.
- C: carry out of the last stage (unsigned: overflow in addition, no borrow in subtraction).
- V (signed overflow): operands of the same sign give a result of the opposite sign; equivalent to . Example: (4 bits) gives , , , .
The ALU critical path is the addition: speed depends on the adder used (AddersHalf adder and full adder, generate-propagate-delete decomposition, the ripple-carry adder and its linear worst-case delay, the adder-subtractor, and the fast structures carry-lookahead, carry-bypass, carry-select and square-root carry-select with their delay formulas and optimal block sizes.Adders →).
Comparators
Equality. iff every pair of bits is equal: (XNOR), (AND tree, or NOR of the XORs).
Formula (magnitude comparator). Compare bits from the most significant: if at the first differing bit , . With :
Example. , : , , , so ().
Property (comparison by subtraction and flags). Compute and read: for equality; for unsigned numbers if the carry out is (no borrow); for signed numbers if . This is how processors do it.
Example. , unsigned: , carry out , so .
Sharing a bus
On a bus several sources (registers, memory, ALU) transmit at different times on the same line. If two static CMOS outputs drive the line with different values there is a direct path from to ground through the two drivers: contention, an intermediate (invalid) voltage and high current. Outputs must be able to disconnect:
- Tri-state: an enable signal; when disabled the output is floating (high impedance, , neither 0 nor 1). At most one source at a time may have (guaranteed by the control unit through a decoder).
- Open-drain: gates have only the pull-down network and can only force ; a pull-up brings the bus to . The bus is the wired-AND of the outputs.
Other computation units
- Multiplier: sequential (add and shift, Booth) or combinational (adder tree); see Shift Registers, Shifters and MultipliersShift registers (serial-parallel conversion, hold constraint), combinational shifters and the barrel shifter with log2 N multiplexer levels, and multipliers: partial products, array multiplier with N squared AND gates and N(N-1) full adders, Wallace tree, Booth, sequential shift-and-add, and dedicated FPGA DSP blocks.Shift Registers, Shifters and Multipliers →.
- Divider: sequential, many cycles.
- Barrel shifter: shift by any amount in one cycle (in ARM in front of the second ALU input).
- Floating-point unit (FPU): exponent alignment, mantissa addition, normalization and rounding (Fixed-Point and Floating-Point NumbersFixed-point numbers as an integer divided by 2 to the F with resolution and a sensor example, floating point in base 2, the IEEE 754 single and double formats, conversions in both directions with worked examples, special values and denormals, range, precision, rounding and floating-point operations.Fixed-Point and Floating-Point Numbers →); in ARM a coprocessor with its own registers (VFP/NEON).
Common mistakes
- Thinking that in ARM C = 1 after a subtraction means a borrow: it is the opposite, C = 1 means no borrow ( unsigned).
- Computing Z from the most significant bit only.
- Confusing C (unsigned) and V (signed).
- Writing subtraction as without .
- Connecting two push-pull outputs to the same line without tri-state.
Versione ripasso
Produces results and flags N, Z, C, V from two operands and an opcode from the control unit (Hardwired and Microprogrammed ControlWhat the control unit does, control signals of a single-cycle datapath with the table for each instruction class, hardwired control (combinational or finite-state machine), microprogrammed control (control memory, horizontal and vertical microinstructions, microsequencer), and a comparison.Hardwired and Microprogrammed Control →); core: adder (AddersHalf adder and full adder, generate-propagate-delete decomposition, the ripple-carry adder and its linear worst-case delay, the adder-subtractor, and the fast structures carry-lookahead, carry-bypass, carry-select and square-root carry-select with their delay formulas and optimal block sizes.Adders →) + logic unit + output multiplexer.
Construction
- 1 bit: AND, OR, full adder, multiplexer.
- bits: cells, carries in cascade (or lookahead).
- Subtraction: Binvert and (Binary ArithmeticBinary addition and two's complement subtraction, overflow for unsigned (carry) and signed (sign change) numbers, the N Z C V flags, multiplication by shifts and additions, Booth's algorithm with a worked example, division, and logical, arithmetic and rotate shifts.Binary Arithmetic →). NOR: Ainvert + Binvert.
- slt: sign of (corrected for overflow) becomes bit 0.
- Zero: . Overflow: .
Flags
N = MSB; Z = result 0; C = carry out (unsigned overflow in addition, no borrow in subtraction); V = signed overflow.
Example: : N=1, Z=0, C=0 (borrow, ), V=0. This is CMP in ARM.
Comparators
Equality: AND of XNORs. Magnitude from the MSB: . By subtraction: equality; unsigned iff carry out ; signed iff .
Bus
Two push-pull outputs with different values: contention (high current). Use tri-state (enable, high impedance , one active source at a time) or open-drain (only pull-down, pull-up to 1, wired-AND).
Other units
Multiplier, divider, barrel shifter (Shift Registers, Shifters and MultipliersShift registers (serial-parallel conversion, hold constraint), combinational shifters and the barrel shifter with log2 N multiplexer levels, and multipliers: partial products, array multiplier with N squared AND gates and N(N-1) full adders, Wallace tree, Booth, sequential shift-and-add, and dedicated FPGA DSP blocks.Shift Registers, Shifters and Multipliers →), FPU (Fixed-Point and Floating-Point NumbersFixed-point numbers as an integer divided by 2 to the F with resolution and a sensor example, floating point in base 2, the IEEE 754 single and double formats, conversions in both directions with worked examples, special values and denormals, range, precision, rounding and floating-point operations.Fixed-Point and Floating-Point Numbers →).
Common mistakes: ARM C = 1 means no borrow; Z is not the MSB alone; C is unsigned and V signed; needs .