Skip to content
Tutorials Center
My learning

Topics

Lesson 29 of 67 ยท CPU Design

The Control Unit: Hardwired and Microprogrammed

Learn how a control unit turns opcodes into control signals, as logic gates or as microcode in a ROM, with Python models of both and a cycle count comparison.

The datapath is a set of parts that can do many things. Nothing in it decides what happens on a given clock tick. That is the control unit's job. It looks at the instruction (mainly the opcode) and produces the control signals that set each multiplexer, enable each register write and select the ALU operation.

There are two classic ways to build it. A hardwired control unit is a circuit of logic gates: opcode bits go in, signals come out, and nothing else is stored. A microprogrammed control unit is a tiny computer inside the computer: each instruction is carried out by running a short microprogram read from a ROM. In this lesson you will build both in Python for the small RISC machine from the datapath lesson, compare them, and count the cycles the microprogrammed version takes.

What the control unit has to produce

For the datapath you saw last time, eight single-bit signals and an ALU operation cover every instruction. This table is the specification any control unit must meet:

Instruction reg_dst alu_src mem_to_reg reg_write mem_read mem_write branch jump ALU op
ADD / SUB / AND / OR 1 0 0 1 0 0 0 0 add / sub / and / or
ADDI 0 1 0 1 0 0 0 0 add
LW 0 1 1 1 1 0 0 0 add
SW 0 1 0 0 0 1 0 0 add
BEQ 0 0 0 0 0 0 1 0 sub
J 0 0 0 0 0 0 0 1 add

Everything in this lesson is a way of implementing this table. The first way needs no memory at all.

Hardwired control

If control signals depend only on the opcode, each signal is a simple Boolean function of four bits. Hardware builds it in two layers:

  1. A decoder turns the 4-bit opcode into one wire per instruction (exactly one is high). It is made of AND gates, like the decoders in the combinational circuits lesson.
  2. Each control signal is an OR of the instruction wires that need it. For example reg_write is on for ADD, SUB, AND, OR, ADDI and LW.
flowchart LR
  OP["opcode<br/>4 bits"] --> DEC["decoder<br/>one wire per instruction"]
  DEC --> OR1["OR gates"]
  OR1 --> CS["control signals<br/>reg_write, mem_read, ..."]

A hardwired control unit is a decoder followed by OR gates: pure logic, no state.

The model below describes each signal by the instructions that switch it on, evaluates the circuit for every opcode, prints the truth table, and checks the result against the table in the previous lesson. Opcodes 9 to 14 do not exist, and the circuit gives all zeros for them, so a bad instruction cannot write a register or memory.

Python A hardwired control unit

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The table prints one row per opcode. The rows for the unused opcodes 9 to 14 are all zeros, and so is HALT (15), which the rest of the machine handles by stopping. The check at the end finds no mismatches.

Strengths of hardwired control: it is the fastest option, because a few gate delays separate opcode from signals, and it needs little area for a small instruction set. Weaknesses: the logic is irregular, hard to modify, and grows quickly for large instruction sets. Fixing a bug means changing the circuit, not data. RISC processors, with their few regular instructions, usually use hardwired control.

Control as a finite state machine

A single-cycle machine needs only the combinational circuit above. But if instructions take several clock cycles (for example fetch, decode, execute, memory, write back, one step per cycle, sharing one ALU), the control unit must remember which step it is on. It becomes a finite state machine: the next state depends on the current state and the opcode, and the outputs depend on the state.

flowchart LR
  F["FETCH"] --> D["DECODE"]
  D -->|"R-format"| RX["R execute"] --> RW["R write back"] --> F
  D -->|"ADDI"| IX["I execute"] --> IW["I write back"] --> F
  D -->|"LW"| LA["address"] --> LM["memory read"] --> LW2["load write back"] --> F
  D -->|"SW"| SA["address"] --> SM["memory write"] --> F
  D -->|"BEQ"| B["branch"] --> F
  D -->|"J"| J["jump"] --> F

The multicycle control state machine: every path starts with fetch and decode and ends back at fetch.

You can implement this state machine with gates and flip-flops (a hardwired FSM). Or you can notice that "a list of steps, each producing a set of signals and naming the next step" looks exactly like a program, and store it in a memory.

Microprogrammed control

Maurice Wilkes proposed this in 1951. The idea:

  • Each step of the state machine is a microinstruction: a word whose bits are the control signals for that clock tick, plus a field saying what comes next.
  • All microinstructions are stored in a fast ROM called the control store.
  • A microprogram counter (ยตPC) picks the current microinstruction. Normally it counts up. At the end of an instruction it returns to the fetch microinstruction. After decode it jumps to the right microroutine using a small dispatch table indexed by the opcode.
flowchart LR
  IR["IR opcode"] --> DT["dispatch table"]
  DT --> MUX(["next-address mux"])
  UPC["ยตPC"] --> INC["+1"] --> MUX
  FETCHA["address 0 (fetch)"] --> MUX
  MUX --> UPC
  UPC --> CS["control store ROM"]
  CS -->|"control word"| SIG["control signals<br/>to the datapath"]
  CS -->|"sequencing field"| MUX

A microprogrammed control unit: the ยตPC reads a ROM; one field of each word chooses the next address.

The next TryIt implements it. The ROM has 13 rows. The executor performs whatever the current microinstruction asks (using the same operations as the datapath: read registers, ALU, memory, write register, update PC), then uses the seq field to pick the next ยตPC. It runs the same program as in the datapath lesson and reports how many cycles each instruction took. Note that in this multicycle version one single ALU serves the address calculation, the arithmetic and the branch comparison, each in its own step.

Python A microprogrammed control unit

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Each instruction's microroutine is easy to read: LW runs FETCH > DECODE > LD_ADDR > LD_READ > LD_WB (5 cycles), BEQ needs only 3, and every other instruction here takes 4. The seven completed instructions use 28 cycles, so the CPI is 4.00. The final register and memory contents match the single-cycle run in the datapath lesson, which is the point: same instruction set, different control.

Hardwired against microprogrammed

Hardwired Microprogrammed
Built from Gates and flip-flops A ROM (control store) plus a sequencer
Speed Fastest Slower: each step reads the ROM
Changing or fixing it Redesign the circuit Change ROM contents (even patch it in the field)
Complex instructions Painful Natural: just a longer microroutine
Typical users RISC chips, simple cores Classic CISC designs; microcode patches in x86

Modern x86 processors use both: common simple instructions are decoded by fast hardware into internal micro-operations, while rare or complicated ones (string moves, some system instructions) run from a microcode ROM, which vendors can update with a firmware patch. Microprogramming also makes it feasible to run one instruction set on very different hardware, which is how IBM's System/360 family spanned a wide range of machines in the 1960s.

How wide is a control word?

A horizontal microinstruction has one bit per control line, so a step can switch on many signals at once, with little decoding. A vertical one packs mutually exclusive choices into small encoded fields (for example, a 3-bit ALU operation instead of one line per operation) and needs a decoder, giving a narrower ROM but a slower step. The last TryIt measures both for our control store.

Python Control store size: horizontal vs vertical

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The printed table shows the trade per field: alu has three operations plus "unused", which encode in 2 bits but need 3 lines when spelled out, while flags such as branch and jump are one bit either way. Our word is tiny, so the gap is small (16 against 18 bits). In a real machine with dozens of ALU operations, many register ports and bus controls, a horizontal word can be many times wider than a vertical one. Its advantage is that no decoder sits between the ROM and the datapath, so each step is faster.

A real control store is far larger than our 13 rows, with thousands of microinstructions for a rich instruction set. Microcode rows also hold fields for memory timing, conditional branching inside the microprogram, and subroutine calls between microroutines.

Control unit and performance

How long a control design takes determines the clock and CPI, which feed straight into the execution time equation. Our microprogrammed machine has CPI 4.0, while the single-cycle design has CPI 1.0 but a long clock. Multicycle designs make the clock only as long as the slowest step instead of the slowest instruction, and save hardware by reusing the ALU and memory. The next lessons build the single-cycle CPU completely and then show how pipelining gets both a short clock and a CPI near 1.

Where microprogramming is used

Beyond running a machine's own instruction set, microprogrammed control has several practical uses:

  • Developing control units quickly. Changing a control ROM is easier than redrawing logic gates, so designers could correct and extend an instruction set late in the project.
  • Emulation. The microcode of one machine can imitate another instruction set, which let companies run old software on new hardware.
  • Diagnostics and debugging. Special microroutines test the processor's own registers, buses and memories, and help locate hardware faults without external equipment.
  • Special-purpose processors. Graphics, signal-processing and network processors define instructions (such as a multiply-accumulate or a checksum step) as microroutines tuned to their task.
  • Firmware updates. If the microcode lives in writable storage, a vendor can fix a bug or a security problem by shipping a patch, as modern x86 processors do.

The disadvantages mirror the advantages. Every machine instruction needs several ROM reads, so it is slower than a hardwired decoder; the control store takes chip area; and the design is harder to pipeline. That is why high-performance RISC cores favor hardwired control.

The microprogram sequencer and next-address logic

In a microprogrammed control unit, the ROM holds the microinstructions, but something must decide which microinstruction comes next. That circuit is the microprogram sequencer (or microprogram controller), and its main component is a microprogram counter (also called the control address register, CAR).

flowchart LR
  IR["Instruction register<br/>(opcode)"] --> MAP["Mapping logic<br/>opcode to start address"]
  MAP --> MUX["Next-address<br/>multiplexer"]
  CAR["Control address<br/>register (CAR)"] --> INC["+1"] --> MUX
  FIELD["Branch address field<br/>of the microinstruction"] --> MUX
  SUB["Subroutine register<br/>(return address)"] --> MUX
  COND["Status flags and<br/>condition select"] --> LOGIC["Branch logic"] --> MUX
  MUX --> CAR
  CAR --> ROM["Control memory (ROM)"]
  ROM -->|"microinstruction"| CW["Control word<br/>to datapath"]
  ROM --> FIELD
  ROM --> COND

The next-address multiplexer chooses among four sources every cycle.

How the next address is chosen

Each microinstruction has a branch field that tells the sequencer what to do. The options are:

  1. Increment: take the CAR + 1 (the normal case, straight-line microcode).
  2. Branch: load an address from the microinstruction. It can be unconditional, or conditional on a status flag (zero, carry, sign...) chosen by a condition-select field. If the condition is false the sequencer simply increments.
  3. Map: at the start of every machine instruction, load the starting address of the matching microroutine from a mapping ROM indexed by the opcode. This is what makes microcode "decode" the instruction.
  4. Call and return: save CAR + 1 in a subroutine register and jump to a shared microroutine (such as the effective-address calculation), then return. This avoids duplicating common sequences.

At the end of each microroutine, a final microinstruction branches back to the fetch routine, which loads the next machine instruction.

A microcontroller in numbers

The size of a microinstruction is the sum of its fields. For a control memory of 256 words, the CAR is 8 bits and the branch address field is 8 bits. With 4 condition choices (2 bits) and a 2-bit branch type, the sequencer-related fields take 12 bits; the remaining bits form the control word that drives the datapath. This is a horizontal format. A vertical format encodes groups of control signals into small fields that a decoder expands, which makes the microinstruction narrower but slower.

Python A tiny microprogram sequencer

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The trace shows the three behaviours: the MAP step jumps from the fetch routine into the right microroutine, NEXT steps through straight-line microcode, and BR chooses between two paths using a flag. Real sequencers (for instance the AMD Am2910) add loop counters and a small stack on top of exactly these ideas.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is a hardwired control unit made of?

    Hardwired control is pure logic: the opcode goes in and the control signals come out through gates.
  2. 2. What does the control store of a microprogrammed unit contain?

    Each ROM word holds the control signals for one clock step and a field that says which microinstruction comes next.
  3. 3. Why is microprogrammed control usually slower than hardwired control?

    Every step needs a ROM access to fetch the next microinstruction, which adds delay compared to a few gate delays.
  4. 4. In our microprogrammed machine, which instruction takes 5 cycles?

    A load needs fetch, decode, address calculation, memory read and write back. A BEQ needs only three cycles.
  5. 5. What is an advantage of microcode?

    Because behavior is stored as data, changing or patching it does not require redesigning the circuit.

Summary

  • The control unit turns the opcode (and, in multicycle designs, the current step) into control signals for the datapath.
  • Hardwired control is a decoder plus OR gates, or a gate-level state machine: fast, but inflexible.
  • Microprogrammed control stores one microinstruction per step in a ROM; a microprogram counter walks through them and a dispatch table jumps to the right microroutine.
  • Horizontal microcode is wide and fast; vertical microcode is narrow and needs decoding.
  • Multicycle control makes each instruction take several short cycles, here CPI 4.0, and reuses one ALU.
  • Modern CPUs combine hardwired decoding for common instructions with microcode for rare ones.

Put control and datapath together in Building a Single-Cycle CPU.