D10 · Lesson 3 · Datapath and control
Splitting a digital design into a datapath of registers, multiplexers and arithmetic units that holds and transforms data, and a controller that tells it what to do each cycle, the select and enable signals between them, and timing a shift-and-add multiplier cycle by cycle
Every nontrivial digital design, from a calculator to a CPU, splits into two parts. The datapath is where the data lives and moves: registers that hold values, multiplexers that pick which value goes where, adders and shifters that compute. The controller is a state machine that decides, cycle by cycle, which multiplexer inputs to select and which registers to load. Drawing that split as a block diagram before writing any Verilog is the single most useful habit in hardware design: the diagram is the design, and the code just describes it.
- A one-bit datapath: a register Q fed through a multiplexer. When EN is 1 at a clock edge, Q loads either the input D (when LOAD is 1) or Q XOR D; when EN is 0, Q holds.
- EN and LOAD are the control signals: in a full design a state machine would drive them. Here you drive them, as the controller.
- The timing diagram shows a scripted sequence: load a 1, then accumulate with XOR, then hold.
Predict
A 32-bit shift-and-add multiplier handles one bit of the multiplier each clock cycle at 100 MHz. How long does one multiplication take?
Each cycle the controller examines one multiplier bit and, if it's 1, tells the datapath to add the shifted multiplicand to the running product: 32 cycles of 10 ns each, 320 ns. A fully parallel multiplier (C7) answers in one cycle using about 32 times the hardware: the classic trade of time against area.
Name it
The datapath
- Registers hold values between clock edges; an enable decides whether a register loads on a given edge.
- Multiplexers choose among sources; selecting among N inputs takes select bits.
- Functional units: adders, comparators, shifters, ALUs.
- Register files hold several registers with read and write ports: R registers of W bits store RW bits.
The controller
A finite state machine (lesson 6) whose outputs are the datapath's select and enable signals, and whose inputs include status signals from the datapath, such as "the count is zero" or "A is less than B". Each state corresponds to what the datapath does in one cycle. With S states the controller needs flip-flops in binary encoding, or S in one-hot encoding, which uses more flip-flops but simpler logic.
Designing with diagrams
Draw the datapath first, with every register and multiplexer; then write down, for each step of the algorithm, which control signals must be set; those steps become the controller's states. The Verilog then follows the diagram line for line.
Worked example
A controller has 6 states. How many flip-flops does it need in binary and one-hot encoding? A datapath picks one of 5 sources for a register: how many select bits?
Show the solution
- Binary encoding.
- One-hot encoding. One flip-flop per state: 6.
- Select bits for 5 sources. Three bits make 8 codes, so three codes go unused.
Answer 3 flip-flops in binary and 6 in one-hot, and 3 select bits for the 5 sources.
Worked example
A register file has 32 registers of 32 bits with two read ports. How much does it store, and how wide are its read-address inputs?
Show the solution
- Storage.
- Address bits for one port. Choosing one of 32 registers takes
- Both read ports. Two ports need 10 address bits, as in the RISC-V register file of C8.
Answer it stores 1024 bits, and each read port takes a 5-bit address, 10 address bits for the two.
Worked example
Multiplying two 8-bit numbers by repeated addition takes up to how many cycles? And by shift-and-add?
Show the solution
- Repeated addition. It adds the multiplicand once per unit of the multiplier, and the largest 8-bit multiplier is so it takes up to 255 cycles.
- Shift-and-add. It handles one bit of the multiplier per cycle: 8 cycles, with the same adder and a shifter.
- What makes the difference. The datapath barely changes; the controller's algorithm makes the difference.
Answer up to 255 cycles by repeated addition, and 8 by shift-and-add.
Check
A multiplexer chooses among 5 inputs. How many select bits does it need?
Check
A shift-and-add multiplier handles one bit of a 16-bit multiplier per cycle at 50 MHz. How long does a multiplication take?
Write units the way you normally would: 1.5 ns, 1500 ps and 0.0015 µs are the same answer.
Check
A state machine has 9 states. How many flip-flops does binary encoding need?
Check
A register file holds 16 registers of 8 bits. How many bits does it store?
Check
A controller has 9 states.
(a)
How many flip-flops does binary encoding need?
(b)
And one-hot?
Check
A register file has 32 registers of 16 bits and 3 read ports.
(a)
How much does it store?
(b)
How many read-address bits does it need in all?
Explain why
Why do designers separate a design into a datapath and a controller?
The datapath and the controller change for different reasons and have very different shapes. The datapath is regular and wide: the same register, multiplexer or adder repeated across every bit of the data, which is easy to draw, reuse and optimize for speed. The controller is narrow and irregular: a state machine that encodes the algorithm's sequence of steps and decisions as a handful of control bits per cycle. Keeping them apart lets the datapath be designed once, as a set of operations it can perform, and the algorithm be changed by changing only the controller's states, and it makes each part far easier to verify than a single tangle of logic would be.
Practice
Keep practicing
8 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.