---
title: "Block diagrams: datapath and control"
heading: 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
eyebrow: D10 · Lesson 3 · Datapath and control
xp: 40
---

::: hook
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.
:::

::: play
- 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.
:::

```sim logic
clock CLK 8
input EN, LOAD, D
output Q
always @(posedge CLK) Q <= EN ? (LOAD ? D : Q ^ D) : Q;
stim EN 1@0 1@16 0@32
stim LOAD 1@0 0@8
stim D 1@0 1@16 0@24 1@40
wave: CLK, EN, LOAD, D, Q
time: 56
```

::: 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?

- [ ] 10 ns
- [x] 320 ns
- [ ] 3.2 µs
- [ ] 32 ns

::: reveal
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.
:::
:::

::: nameit
## 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 $\lceil\log_2N\rceil$ 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 $\lceil\log_2S\rceil$ 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.
:::

::: 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?

::: solution
1. **Binary encoding.**
   $$\lceil\log_2 6\rceil = 3\ \text{flip-flops}$$
2. **One-hot encoding.** One flip-flop per state: 6.
3. **Select bits for 5 sources.**
   $$\lceil\log_2 5\rceil = 3\ \text{select bits}$$
   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.
:::
:::

::: 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?

::: solution
1. **Storage.**
   $$32 \times 32 = 1024\ \text{bits}$$
2. **Address bits for one port.** Choosing one of 32 registers takes
   $$\lceil\log_2 32\rceil = 5\ \text{bits}$$
3. **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.
:::
:::

::: example
## Multiplying two 8-bit numbers by repeated addition takes up to how many cycles? And by shift-and-add?

::: solution
1. **Repeated addition.** It adds the multiplicand once per unit of the multiplier, and the largest 8-bit multiplier is
   $$2^8 - 1 = 255$$
   so it takes up to 255 cycles.
2. **Shift-and-add.** It handles one bit of the multiplier per cycle: 8 cycles, with the same adder and a shifter.
3. **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
given: N = 3 | 5 | 8 | 12 | 32
answer: ceil(log(N)/log(2))
tolerance: 0.4

## A multiplexer chooses among {N} inputs. How many select bits does it need?

::: right
Right. $\lceil\log_2N\rceil$ = {answer}.
:::

::: misses
- [N] Select bits are binary: $\lceil\log_2N\rceil$ of them can choose among N.
- [any] Not yet. Enough bits to number N inputs.
:::
:::

::: check
given: n = 8 | 16 | 32 | 64; f = 50 | 100 | 250
answer: n*1000/f
unit: ns
tolerance: 0.5%

## A shift-and-add multiplier handles one bit of an {n}-bit multiplier per cycle at {f} MHz. How long does a multiplication take?

::: right
Right. {n} cycles at {f} MHz: {answer} ns.
:::

::: misses
- [1000/f] One bit per cycle: n cycles.
- [any] Not yet. Cycles times the clock period.
:::
:::

::: check
given: S = 5 | 9 | 13 | 20
answer: ceil(log(S)/log(2))
tolerance: 0.4

## A state machine has {S} states. How many flip-flops does binary encoding need?

::: right
Right. $\lceil\log_2S\rceil$ = {answer}.
:::

::: misses
- [S] That's one-hot encoding.
- [any] Not yet. Enough bits to number the states.
:::
:::

::: check
given: R = 8 | 16 | 32; W = 8 | 32 | 64
answer: R*W
tolerance: 0.4

## A register file holds {R} registers of {W} bits. How many bits does it store?

::: right
Right. R × W = {answer} bits.
:::

::: misses
- [any] Not yet. Registers times width.
:::
:::

::: check
kind: parts
given: s = 5 | 9 | 17 | 33

## A controller has {s} states.

::: part
answer: ceil(log(s)/log(2))
tolerance: 0

### How many flip-flops does binary encoding need?

::: misses
- [any] $\lceil\log_2 s\rceil$.
:::
:::

::: part
answer: s
tolerance: 0

### And one-hot?

::: misses
- [any] One per state.
:::
:::
:::

::: check
kind: parts
given: r = 16 | 32 | 64; w = 16 | 32 | 64; p = 2 | 3

## A register file has {r} registers of {w} bits and {p} read ports.

::: part
answer: r*w
unit: bits
tolerance: 0

### How much does it store?

::: misses
- [any] Registers × width.
:::
:::

::: part
answer: p*round(log(r)/log(2))
unit: bits
tolerance: 0

### How many read-address bits does it need in all?

::: misses
- [any] $\log_2 r$ per port.
:::
:::
:::

::: explain
## Why do designers separate a design into a datapath and a controller?

::: model
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

::: check
given: N = 2 | 3 | 4 | 6 | 7 | 9 | 16 | 17 | 64 | 100
answer: ceil(log(N)/log(2))
tolerance: 0.4

## Select bits for {N} inputs?

::: misses
- [any] $\lceil\log_2N\rceil$.
:::
:::

::: check
given: n = 4 | 8 | 12 | 16 | 24 | 32 | 48 | 64 | 128; f = 10 | 500
answer: n*1000/f
unit: ns
tolerance: 0.5%

## Shift-and-add time: {n} bits at {f} MHz?

::: misses
- [any] n cycles.
:::
:::
:::
