All episodes · From One Switch to a CPU · Episode 11

Who Tells the CPU What to Do? Control Unit, Hardwired vs Microcode

Pressing play loads the video from YouTube.

Every step of fetch, decode and execute means turning the right wires on at the right moment. Something has to do that.
We list the control lines, write them as control words, see what the control unit reads (the opcode and the step), then build it two ways: hardwired from logic gates, and microprogrammed from a small table called microcode.

Every animation in this video is drawn by code, and the narration is an AI voice.

Transcript

Look at all the parts we built last time. On every single step, dozens of tiny switches inside them must flip in exactly the right pattern. Load this register, not that one. Add, don't subtract. Read memory, don't write it. Who decides? Today, we meet the control unit.

Every part has control inputs: a line that says “load now”, or a line that picks add or subtract. Our little CPU has eight of them. Written side by side, their ones and zeros form a control word. For fetch, the word is always the same: read memory, load the instruction register, and add one to the program counter. For ADD, during execute: tell the ALU to add, and load the result into the register. Every step of every instruction has its own control word.

The control unit's job is to produce the right control word at the right moment. It looks at three things: the opcode, which step we're on, and the flags. The step comes from a tiny counter, ticking fetch, decode, execute, and starting over with each new instruction.

There are two classic ways to build it. The first is hardwired: work out the logic for each control line, and build it straight out of gates. For example, “load a register” is on during execute if the opcode is a load, an add, or a subtract. Three AND gates and one OR gate. Hardwired control is fast. But it's a fixed tangle of gates, and changing one instruction means redesigning the circuit.

The second way is microprogrammed: store every control word in a small memory inside the control unit, one row per step of each instruction. The opcode and the step together form an address. The control unit simply reads that row, and the bits in it become the control lines. It's a tiny program that runs the program, called microcode. Adding a complicated instruction just means adding more rows. It's slower, because every step means a memory read. But it's much easier to design, and on some processors it can even be updated after the chip is sold.

Modern processors mix both: simple instructions run on fast hardwired logic, and rare, complex ones fall back to microcode.

So: the control unit turns each opcode and step into a control word, either with gates, or from a little memory of microcode. But right now, each instruction takes several steps, and most parts sit idle while one of them works. Next time: the assembly-line trick called pipelining. See you there.

Next episode: pipelining. Working on several instructions at once.

Buy the Developer a Coffee

Entirely up to you: it works the same either way.

Every cup turns into the next update.

WeChat Pay QR code
WeChat Pay
Alipay QR code
Alipay
PayPal QR code
PayPal