---
title: Concurrency in hardware
heading: Why a hardware description is not a program, every assignment as its own circuit running all the time, signals settling through levels of gates, the order of statements not mattering, and the throughput that comes from doing thousands of operations in every clock cycle
eyebrow: D10 · Lesson 1 · Concurrency in hardware
xp: 40
---

::: hook
Verilog looks like a programming language, which is the first trap it sets. A program runs one statement after another on one processor. A Verilog description has no "after": each `assign` describes a separate piece of circuitry, and all of them work at once, continuously, from the moment power is applied. Change an input and every gate that depends on it starts changing within picoseconds, in parallel. That's what lets an FPGA do a thousand multiplications in the time a processor does one, and it's why thinking in hardware feels so different from thinking in software.
:::

::: play
- Three assignments: A = X & Y, B = X | Y and C = A ^ B, each gate with a delay of one tick. Flip X and Y, or watch the scripted changes in the timing diagram.
- A and B respond together, one tick after the inputs change; C responds one tick after that, because it depends on them. The order the lines are written in makes no difference.
- The time for C to settle is set by the number of gate levels between it and the inputs: the **critical path**.
:::

```sim logic
input X, Y
output A, B, C
assign C = A ^ B;
assign A = X & Y;
assign B = X | Y;
stim X 0@0 1@4 1@12 0@20
stim Y 0@0 0@4 1@12 1@20
wave: X, Y, A, B, C
time: 28
```

::: predict
## A processor at 3 GHz does one addition per cycle. An FPGA at 300 MHz has 1000 adders working side by side. How many additions per second does each manage?

- [ ] Processor 3 billion, FPGA 300 million
- [x] Processor 3 billion, FPGA 300 billion
- [ ] Both 3 billion
- [ ] Processor 3 trillion, FPGA 300 billion

::: reveal
The processor: 3 × 10⁹ additions per second. The FPGA: 1000 adders × 3 × 10⁸ cycles = 3 × 10¹¹, a hundred times more despite a ten times slower clock. Hardware wins by doing many things at once, as long as the work can be split up.
:::
:::

::: nameit
## Concurrent statements

In Verilog, each `assign` and each `always` block describes hardware that exists and operates independently. Their textual order is irrelevant: `assign C = A ^ B;` can come before the lines that define A and B. Simulators model this with events: when a signal changes, every statement that reads it is scheduled to update after its delay.

## Settling time

A combinational output settles after the delay of the slowest path from the inputs to it. With $n$ levels of gates of delay $t_g$ each, that's $nt_g$, and a clocked design can run no faster than about $1/(nt_g)$ plus the flip-flops' overhead (lesson 8). Until it settles, an output may show **glitches**: brief wrong values as faster paths arrive before slower ones.

## Throughput from parallelism

Hardware throughput is the number of operations done per cycle times the clock rate. A processor executes a few operations per cycle at gigahertz rates; an FPGA or ASIC can lay down thousands of operators side by side at hundreds of megahertz. Hardware design is the art of finding that parallelism in a problem and laying it out in space.
:::

::: example
## A path passes through 6 levels of gates, each taking 80 ps. How long does it take to settle, and what clock rate does that allow?

::: solution
1. **Settling time.** Each of the 6 levels takes 80 ps:
   $$\begin{aligned} t &= 6 \times 80\ \text{ps} \\ &= 480\ \text{ps} \end{aligned}$$
2. **Clock rate.** The clock period must be at least 480 ps plus flip-flop overhead, so the clock must stay below about
   $$\frac{1}{480\ \text{ps}} = 2.08\ \text{GHz}$$
3. **Fewer levels.** Halving the levels would double the rate.

**Answer:** the path settles in 480 ps, which allows a clock below about 2.08 GHz.
:::
:::

::: example
## A video filter must process 1920 × 1080 pixels at 60 frames per second, with 25 multiplications per pixel. How many multipliers does it need at 150 MHz?

::: solution
1. **Multiplications per second.**
   $$\begin{aligned} &1920 \times 1080 \times 60 \times 25 \\ &= 3.11 \times 10^9\ \text{per second} \end{aligned}$$
2. **Divide by one multiplier's rate.** Each multiplier does $1.5 \times 10^8$ a second:
   $$\frac{3.11 \times 10^9}{1.5 \times 10^8} = 20.7$$
3. **Round up.** 20.7 rounds up to 21 multipliers working in parallel.

**Answer:** 21 multipliers working in parallel.
:::
:::

::: example
## 1024 numbers must be summed. How many adder delays does a processor adding one at a time need, and how many does a tree of adders in hardware need?

::: solution
1. **One at a time.** A processor needs 1023 additions in sequence, so 1023 adder delays.
2. **A tree of adders.** A tree adds pairs in parallel, halving the count at each level:
   $$\log_2 1024 = 10\ \text{levels}$$
   using 1023 adders in all.
3. **Speed for area.** Hardware buys speed with area, here a hundredfold.

**Answer:** 1023 adder delays one at a time, against 10 for a tree of 1023 adders.
:::
:::

::: check
given: n = 10 | 256 | 2000; f = 100 | 250 | 500
answer: n*f/1000
tolerance: 0.5%

## An FPGA design has {n} operators each finishing one operation per clock at {f} MHz. How many billion operations per second does it perform?

::: right
Right. {n} × {f} MHz = {answer} billion per second.
:::

::: misses
- [f/1000] Multiply by the number of operators working at once.
- [any] Not yet. Operators times clock rate.
:::
:::

::: check
given: n = 3 | 6 | 12; t = 50 | 80 | 150
answer: 1000/(n*t)
unit: GHz
tolerance: 0.5%

## A logic path has {n} levels of gates, each with a delay of {t} ps. Ignoring flip-flop overhead, what's the fastest clock it allows?

::: right
Right. $1/(nt_g)$ = {answer} GHz.
:::

::: misses
- [1000/t] All the levels are in series: multiply the delay by n.
- [any] Not yet. One over the path delay.
:::
:::

::: check
given: p = 640 | 1920 | 3840; q = 480 | 1080 | 2160; r = 30 | 60; m = 9 | 25
answer: ceil(p*q*r*m/150e6)
tolerance: 0.4

## A filter needs {m} multiplications per pixel on {p} × {q} video at {r} frames per second. How many multipliers running at 150 MHz does it need?

::: right
Right. {answer} multipliers.
:::

::: misses
- [any] Not yet. Multiplications per second over each multiplier's rate.
:::
:::

::: check
given: l = 2 | 4 | 7; d = 2 | 3 | 5
answer: l*d
tolerance: 0.4

## In a timing simulation, an output sits {l} gate levels from the inputs, and each gate takes {d} ticks. How many ticks after an input changes does the output settle?

::: right
Right. {answer} ticks.
:::

::: misses
- [l] Multiply by each gate's delay.
- [any] Not yet. Levels times delay.
:::
:::

::: check
kind: parts
given: n = 4 | 8 | 12; t = 40 | 80 | 150

## A path passes through {n} levels of gates of {t} ps each.

::: part
answer: n*t
unit: ps
tolerance: 0.5%

### How long does it take to settle?

::: misses
- [any] Levels × delay.
:::
:::

::: part
answer: 1e6/(n*t)
unit: MHz
tolerance: 0.5%

### What clock rate does that allow, ignoring flip-flop overhead?

::: misses
- [any] 1 ÷ delay.
:::
:::
:::

::: check
kind: parts
given: N = 64 | 1024 | 65536

## {N} numbers are summed.

::: part
answer: N - 1
tolerance: 0

### How many additions in sequence does one adder need?

::: misses
- [any] N − 1.
:::
:::

::: part
answer: log(N)/log(2)
tolerance: 0.5%

### How many levels does an adder tree need?

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

::: explain
## Why doesn't the order of assign statements matter in Verilog?

::: model
Each assign statement describes a piece of circuitry, a gate or a network of gates wired from some signals to another, and all of that circuitry exists at the same time once it's built. There's no processor stepping through the statements: the gates continuously compute their outputs from whatever their inputs currently are. Writing the line for C before the lines that compute A and B just lists the same wires and gates in a different order on the page. The simulator mirrors this by updating every statement whose inputs change, in whatever order events happen, so the result depends only on how the signals are connected.
:::
:::

::: practice

::: check
given: n = 1 | 5000; f = 50 | 800
answer: n*f/1000
tolerance: 0.5%

## Throughput: {n} operators at {f} MHz?

::: misses
- [any] Operators × clock.
:::
:::

::: check
given: n = 1 | 30; t = 10 | 300
answer: 1000/(n*t)
unit: GHz
tolerance: 0.5%

## Fastest clock: {n} levels of {t} ps?

::: misses
- [any] $1/(nt_g)$.
:::
:::
:::
