---
title: Interfaces and handshakes
heading: Valid/ready flow control, buses, bursts, credits and line coding
eyebrow: S3 · Lesson 36 · Interfaces and handshakes
xp: 50
---

::: hook
Every block in a chip has to pass data to its neighbors, and neither side can assume the other is always ready. The standard answer is a two-signal handshake: the sender raises *valid* when it has data, the receiver raises *ready* when it can take it, and a word moves on every clock edge where both are high. That one rule composes into pipelines, FIFOs and whole on-chip networks, and the same ideas scale up to buses with bursts, to credit-based links between chips, and to serial lanes whose line codes trade a few percent of bandwidth for reliable clock recovery.
:::

::: play
- A valid/ready interface. FIRE = VALID & READY marks the transfers, and a 2-bit counter (C1, C0) counts them on each clock edge where FIRE is high.
- From 34 to 42 the sender has data but the receiver isn't ready: the word waits, and the sender must hold it unchanged. From 42 to 50 the receiver is ready but there's nothing to send.
- Count the edges where both are high: five transfers, so the counter ends at 01.
:::
:::

```sim logic
module handshake(input CLK, input VALID, input READY, output FIRE, output C0, output C1);
assign FIRE = VALID & READY;
always @(posedge CLK) C0 <= C0 ^ FIRE;
always @(posedge CLK) C1 <= C1 ^ (FIRE & C0);
endmodule
clock CLK 8
stim VALID 0@0 1@2 0@42 1@50
stim READY 0@0 1@18 0@34 1@42
wave: CLK, VALID, READY, FIRE, C0, C1
time: 72
```

::: predict
## A receiver registers its ready signal, so the sender sees it one cycle late, and data takes one more cycle to arrive. To never lose a word while running at full rate, how many words must the receiver be able to take after it drops ready?

- [ ] None: dropping ready stops the sender at once
- [ ] 1
- [x] 2
- [ ] 4

::: reveal
Two. When the receiver drops ready, the sender doesn't see it for a cycle and keeps sending, and words already on their way keep arriving: the round trip is two cycles, so two more words land after the decision. A small **skid buffer** absorbs them. Longer links between chips use the same idea with **credits**: the sender holds one credit per free buffer slot and needs at least a round trip's worth of them to run at full speed.
:::
:::

::: nameit
## The valid/ready handshake

A transfer happens at a clock edge where both VALID and READY are high. Two rules keep it safe:

- Once the sender raises VALID, it holds VALID and the data stable until the transfer happens.
- VALID must never depend combinationally on READY (the receiver may make READY depend on VALID), so no combinational loop can form between two blocks.

AXI-Stream and the five channels of AXI (read and write address, write data, write response, read data) all use it.

## Buses and bursts

A bus's peak bandwidth is width × clock. Each transaction also costs overhead cycles (address, arbitration, turnaround), so a burst of B beats with o overhead cycles has an efficiency of $\frac{B}{B + o}$. AXI4 allows bursts of up to 256 beats.

## Flow control over latency

If a ready or credit signal takes $r$ cycles to reach the sender and data takes $d$ cycles in flight, the receiver must be able to absorb $r + d$ words after it says stop. With credits, the sender needs at least $r + d$ credits to send every cycle; fewer credits cap throughput at $\frac{\text{credits}}{r + d}$ words per cycle.

## Serial links

Fast links send one bit stream per lane and recover the clock from the data, so the data must have enough transitions and no DC drift:

| Code | Payload per line bits | Used in |
|---|---|---|
| 8b/10b | 80% | older PCIe, SATA, USB 3.0 |
| 64b/66b | 96.97% | 10 G Ethernet |
| 128b/130b | 98.46% | PCIe 3.0 to 5.0 |

Payload rate = line rate × code efficiency × lanes.
:::

::: example
## A 64-bit bus runs at 250 MHz. Transfers are 16-beat bursts, each with 4 cycles of address and turnaround overhead. What's the sustained bandwidth?

::: solution
1. **Peak.**
   $$8\ \text{bytes} \times 250\ \text{MHz} = 2\ \text{GB/s}$$
2. **Efficiency.**
   $$\frac{16}{16 + 4} = 0.8$$
   so 1.6 GB/s sustained.
3. **Single beats.** Single-beat transfers would give 1/5 of peak, 0.4 GB/s, which is why DMA engines move data in long bursts.

**Answer:** 1.6 GB/s sustained, 80% of the 2 GB/s peak.
:::
:::

::: example
## What payload rate does a PCIe 3.0 x4 link carry (8 GT/s per lane, 128b/130b), and a 10 G Ethernet lane (10.3125 Gb/s, 64b/66b)?

::: solution
1. **PCIe.**
   $$\begin{aligned} R &= 8 \times 4 \times \frac{128}{130} \\ &= 31.5\ \text{Gb/s} \end{aligned}$$
   which is 3.94 GB/s each way.
2. **Ethernet.**
   $$\begin{aligned} R &= 10.3125 \times \frac{64}{66} \\ &= 10.0\ \text{Gb/s} \end{aligned}$$
   which is why the odd line rate was chosen.

**Answer:** 31.5 Gb/s (3.94 GB/s) each way for PCIe 3.0 x4, and 10.0 Gb/s for the Ethernet lane.
:::
:::

::: example
## Credits take 3 cycles to return to a sender, and data spends 5 cycles in flight. How many credits keep the link busy every cycle, and what throughput do 6 credits give?

::: solution
1. **Round trip.** The sender must cover the whole round trip:
   $$3 + 5 = 8\ \text{cycles}$$
   so it needs 8 credits (and the receiver 8 words of buffering).
2. **With 6 credits.** It sends 6 words, then waits for the first credit to come back: 6 of every 8 cycles, 75% of the link.

**Answer:** 8 credits keep the link busy; 6 credits give 75%.
:::
:::

::: check
given: w = 32 | 64 | 128; f = 100 | 250 | 400
answer: w/8*f*1e6/1e9
unit: GB/s
tolerance: 0.5%

## A {w}-bit bus runs at {f} MHz. What's its peak bandwidth?

::: right
Right. Width in bytes times clock: {answer} GB/s.
:::

::: misses
- [w*f*1e6/1e9] Convert bits to bytes.
- [any] Not yet. Bytes per beat times beats per second.
:::
:::

::: check
given: B = 4 | 8 | 16; o = 2 | 5
answer: 100*B/(B + o)
unit: %
tolerance: 0.5%

## Bursts of {B} beats each cost {o} cycles of overhead. What fraction of peak bandwidth is sustained?

::: right
Right. $\frac{B}{B + o}$ = {answer}%.
:::

::: misses
- [100*(1 - o/B)] Overhead adds to each burst's cycles.
- [any] Not yet. Useful cycles over total cycles.
:::
:::

::: check
given: r = 1 | 2 | 4; d = 1 | 3 | 6
answer: r + d
tolerance: 0

## A ready signal takes {r} cycles to reach the sender, and data takes {d} cycles in flight. How many words must the receiver absorb after it drops ready, to lose nothing at full rate?

::: right
Right. The round trip, {answer} words.
:::

::: misses
- [r] Words already in flight also arrive.
- [d] The sender keeps sending until it sees ready drop.
- [any] Not yet. Count the round trip.
:::
:::

::: check
given: c = 2 | 4 | 6; r = 4 | 8; d = 4 | 10
answer: 100*c/(r + d)
unit: %
tolerance: 0.5%

## A credit-based link has a {r}-cycle credit return and {d} cycles of data latency, and the receiver has only {c} buffer slots. What fraction of full rate can it sustain?

::: right
Right. $\frac{\text{credits}}{r + d}$ = {answer}%.
:::

::: misses
- [100*c/r] Each credit is tied up for the whole round trip.
- [any] Not yet. Each credit can be used once per round trip.
:::
:::

::: check
given: g = 2.5 | 5 | 8 | 16; n = 1 | 4 | 16; e = 0.8 | 0.9846
answer: g*n*e
unit: Gb/s
tolerance: 0.5%

## A link runs {n} lane(s) at {g} Gb/s each, with a line code of efficiency {e}. What's its payload rate?

::: right
Right. {answer} Gb/s.
:::

::: misses
- [g*n] The line code takes its share.
- [any] Not yet. Line rate times lanes times efficiency.
:::
:::

::: check
given: N = 4096 | 65536 | 1000000; w = 8 | 16; m = 16 | 256
answer: ceil(N/(w*m) - 0.0001)
tolerance: 0

## A DMA engine moves {N} bytes over a bus of {w}-byte beats, with bursts of at most {m} beats. How many bursts does it take?

::: right
Right. $\lceil N/(wm) \rceil$ = {answer}.
:::

::: misses
- [ceil(N/m - 0.0001)] Each beat carries w bytes.
- [any] Not yet. Bytes per burst first.
:::
:::

::: check
kind: parts
given: w = 32 | 64 | 128; f = 200 | 400; B = 4 | 16 | 64; o = 2 | 4

## A {w}-bit bus at {f} MHz moves {B}-beat bursts, each with {o} cycles of overhead.

::: part
answer: w/8*f/1000
unit: GB/s
tolerance: 0.5%

### What is its peak bandwidth?

::: misses
- [w*f/1000] Convert bits to bytes.
- [any] Width × clock.
:::
:::

::: part
answer: B/(B + o)*100
unit: %
tolerance: 0.5%

### What fraction of cycles carry data?

::: misses
- [any] $B/(B + o)$.
:::
:::

::: part
answer: w/8*f/1000*B/(B + o)
unit: GB/s
tolerance: 0.5%

### What bandwidth does it sustain?

::: misses
- [any] Peak × efficiency.
:::
:::
:::

::: check
kind: derivation
vars: r = 1..20, d = 1..40, c = 1..16

## A credit-based link has r cycles of credit-return latency and d cycles of data latency; the sender holds c credits, fewer than the round trip.

::: step
answer: r + d

### How many credits would keep it busy every cycle?
:::

::: step
answer: c/(r + d)

### What fraction of cycles does it use with c credits?
:::

::: right
Right. Credits, like a FIFO's depth, must cover the round-trip latency.
:::
:::

::: explain
## Why must a sender's valid signal never depend combinationally on the receiver's ready signal?

::: model
The receiver is allowed to compute ready from valid, for example to accept a word only when one is offered. If the sender also computed valid from ready, the two signals would form a loop through combinational logic in two different blocks: a change in one would ripple to the other and back within the same clock period. Such a loop can oscillate or settle to a value that depends on gate delays, and timing analysis can't handle it. Requiring valid to depend only on the sender's own state breaks the loop, so any two blocks that follow the rule can be connected safely without either designer knowing how the other built theirs.
:::
:::

::: practice

::: check
given: w = 16 | 256; f = 150 | 500
answer: w/8*f*1e6/1e9
unit: GB/s
tolerance: 0.5%

## Peak bandwidth of a {w}-bit bus at {f} MHz?

::: misses
- [any] Bytes per beat times clock.
:::
:::

::: check
given: B = 8 | 32; o = 3 | 6
answer: 100*B/(B + o)
unit: %
tolerance: 0.5%

## Efficiency of {B}-beat bursts with {o} overhead cycles?

::: misses
- [any] $B/(B + o)$.
:::
:::

::: check
given: r = 2 | 3; d = 2 | 5
answer: r + d
tolerance: 0

## Skid buffer depth: {r}-cycle ready, {d}-cycle data?

::: misses
- [any] The round trip.
:::
:::
:::
