Sub topic 14
What the Control Unit Does
This chapter assumes you know the following: binary and hexadecimal numbers, basic gates, multiplexers and flip-flops, and what registers and memory are. It also assumes you have seen the fetch sequence and the idea of a datapath. Here we look at the part of the processor that drives the datapath: the control unit.
The role of the control unit
The datapath holds the registers, the ALU, and the buses. These parts do nothing on their own. Something must decide, at each clock cycle, which register puts its value on a bus, which register loads from a bus, and which operation the ALU performs. That something is the control unit.
The control unit takes in the current instruction and the state of the machine. It produces a set of control signals, which are single-bit lines that open or close paths in the datapath or select an operation.
A control unit has three jobs:
- Sequencing. It steps through the fetch, decode and execute phases in the right order.
- Signal generation. In each time step it asserts exactly the signals needed for that step.
- Decision making. It reacts to the opcode, to status flags such as zero or carry, and to external events such as interrupts.
Inputs and outputs
The control unit has four kinds of input.
| Input | Source | Purpose |
|---|---|---|
| Opcode bits | Instruction register (IR) | Selects which instruction's sequence to run |
| Status flags | Flag register | Lets conditional branches decide |
| Clock | System clock | Divides the work into time steps |
| External signals | Interrupt lines, reset, memory-ready | Handles events outside the program |
Its outputs are the control signals. They fall into these groups:
- Register-transfer signals that gate a register onto a bus or load a register from a bus, for example
PCout,MARin,IRin. - ALU function signals that choose add, subtract, AND, OR, shift, and so on.
- Memory signals such as
ReadandWrite. - Sequencing signals that increment the PC or select the next step.
Control signals in the fetch sequence
Recall the fetch sequence from the datapath chapter.
Fetch sequence as register transfers:
T0: MAR <- PC
T1: MDR <- M[MAR], PC <- PC + 1
T2: IR <- MDR
T3: decode IR; control unit selects the execute sequenceEach line is a time step. Each register transfer needs specific control signals during that step. The table below shows one possible assignment on a single-bus datapath.
| Step | Transfer | Signals asserted |
|---|---|---|
| T0 | MAR <- PC | `PCout`, `MARin` |
| T1 | MDR <- M[MAR], PC <- PC + 1 | `Read`, `MDRin`, `PC increment` |
| T2 | IR <- MDR | `MDRout`, `IRin` |
| T3 | Decode | none on the datapath; the opcode feeds the decoder |
Two things to notice. First, the signals in T0 to T2 are the same for every instruction, because fetch does not depend on the opcode. Second, from T3 onward the signals depend on the opcode held in IR. This is where the control unit does its real work.
A rule that matters on a single-bus datapath: only one register may drive the bus in a time step. The control unit must never assert two out signals at the same time. Several in signals may be asserted together, because several registers can load the same bus value.
Time steps and the control step counter
The control unit needs to know which step it is in. A control step counter counts clock cycles within an instruction. The counter is reset to zero at the end of each instruction, so the next fetch starts at T0.
The signal for a given line is true only for certain combinations of opcode and step. For example, PCout is asserted in T0 for every instruction, and again later for a branch instruction that must compute a target address from the PC. A signal is therefore a Boolean function of the step, the opcode, and the flags:
PCout = T0 + T_k · BRANCH + ...
This formula is the key idea behind the two design styles covered in the next chapters. A hardwired unit implements such formulas directly with gates. A microprogrammed unit stores the signal patterns in a memory and reads them out step by step.
An execute example
Take an instruction ADD R1, R2 meaning R1 <- R1 + R2, on a single-bus datapath with a temporary register Y and an ALU output register Z. The execute steps could be:
| Step | Transfer | Signals |
|---|---|---|
| T3 | Y <- R1 | `R1out`, `Yin` |
| T4 | Z <- Y + R2 | `R2out`, `Add`, `Zin` |
| T5 | R1 <- Z | `Zout`, `R1in` |
In T4 the bus carries R2 while the ALU takes its other input from Y. The ALU result goes into Z, not directly onto the bus, which avoids the bus being both read and written in one step. The instruction takes six steps in total (T0 to T5).
Control signals and clock timing
All register loads happen on a clock edge. The control signals must be stable before the edge and held until it. The time for the control unit to generate signals, plus the time for data to cross the bus and ALU, sets the minimum clock period. A longer signal-generation path forces a slower clock. This is one reason control design matters for performance.
Where control signal counts come from
GATE questions often ask how many control signals or bits a design needs. The count follows from the design.
- If every signal has its own wire, the control word has one bit per signal.
- If signals are encoded, as in a vertical microinstruction, a group of mutually exclusive signals needs only ⌈log₂(n)⌉ bits for n signals, plus possibly a code for "none".
A small example. Suppose the ALU has 8 functions, of which only one can be active at a time, and there are 12 register-transfer signals that can occur independently. An unencoded design needs 8 + 12 = 20 bits. Encoding the ALU function takes 3 bits (8 functions, with no "no operation" code needed if one function is harmless) so the total is 3 + 12 = 15 bits. If a "no operation" state is required, 9 codes need 4 bits, giving 16 bits.
Common mistakes
- Asserting two bus drivers at once. On a single bus only one
outsignal may be active per step. - Treating fetch as instruction-specific. Fetch is identical for all instructions. Only execute depends on the opcode.
- Forgetting the flags as inputs. A conditional branch needs the flag register as a control input.
- Confusing the control unit with the ALU. The ALU computes. The control unit only tells the ALU what to compute.
- Counting steps wrongly. The step in which the opcode is decoded may or may not be counted as its own step, depending on the textbook. Check how the question numbers its steps.
Exercise
On a single-bus datapath, the instruction SUB R3, R4 (R3 <- R3 - R4) is executed after fetch using a temporary register Y and an ALU output register Z. Write the execute steps with the control signals asserted in each. How many time steps does the whole instruction take, counting the four fetch steps T0 to T3?
Show answer
T3 (decode, counted as part of fetch here, no datapath signals). Execute: T4: Y <- R3 using R3out, Yin. T5: Z <- Y - R4 using R4out, Sub, Zin. T6: R3 <- Z using Zout, R3in. The instruction takes seven steps, T0 to T6. Under the convention that decode is not a separate step, the total is six.
Exercise
A control unit has an ALU with 16 functions (one active at a time), 20 independent register-transfer signals, and 2 memory signals (Read, Write, which are never active together, and either may be absent). Find the number of control bits with no encoding and with encoding of each mutually exclusive group.
Show answer
No encoding: 16 + 20 + 2 = 38 bits. With encoding: ALU needs ⌈log₂16⌉ = 4 bits if a function is always selected (or 5 bits if a "no operation" code is also required, since 17 codes are needed). The memory group has three states (none, Read, Write), so 2 bits. The 20 independent signals stay at 20 bits. Total: 4 + 20 + 2 = 26 bits, or 27 bits with a no-operation ALU code.