---
title: RTL design and the HDL mindset
heading: Describing hardware, not running a program
eyebrow: S3 · Lesson 1 · RTL design and the HDL mindset
xp: 50
---

::: hook
Verilog looks like a programming language, and that's the trap. A program runs one line after another; a hardware description says what circuits exist, and every one of them works all the time, in parallel. Register-transfer level (RTL) design describes a digital system as registers, updated together on a clock edge, with combinational logic between them computing their next values. Learn to read every line as "what gates and flip-flops does this make?" and the language's odd rules (two kinds of assignment, sensitivity lists, inferred latches) stop being odd. This course builds on C7's logic and Verilog and on C8's processor, at the depth of a senior digital design course.
:::

::: play
- A two-stage shift register written as `always @(posedge CLK) begin Q1 <= Q0; Q0 <= D; end`. The waveforms show the clock, the input D and the two flip-flops.
- On each rising edge both flip-flops sample their inputs at the same instant, then update: D moves into Q0 while Q0's old value moves into Q1. The order of the two lines doesn't matter.
- Change D just after an edge and watch it take one clock to reach Q0 and two to reach Q1: each register is one clock of latency.
:::
:::

```sim logic
module shift2(input D, input CLK, output Q1);
always @(posedge CLK) begin Q1 <= Q0; Q0 <= D; end
endmodule
clock CLK 8
stim D 0@0 1@2 0@26 1@34
wave: CLK, D, Q0, Q1
time: 48
```

::: predict
## On a clock edge, registers a = 3 and b = 5 execute `a <= b; b <= a;` (nonblocking). What are they afterward?

- [ ] a = 5, b = 5
- [x] a = 5, b = 3: they swap
- [ ] a = 3, b = 3
- [ ] It depends on which line is first

::: reveal
They swap. Nonblocking assignments all read their right-hand sides at the clock edge, using the old values, and only then update. That's exactly how flip-flops behave: each samples its input at the edge, then changes. With blocking assignments (`a = b; b = a;`) the lines would run in order, and both would end up 5. In sequential logic, always use nonblocking assignments.
:::
:::

::: nameit
## Register-transfer level

A synchronous design is registers plus combinational logic. On each clock edge every register loads the value its logic computed from the registers' previous values. Describing that is RTL:

- `always @(posedge clk)` blocks describe **registers**, with nonblocking `<=` assignments.
- `assign` statements and `always @*` blocks describe **combinational logic**, with blocking `=` assignments and every output assigned on every path.

## Two assignment rules that prevent most bugs

1. Sequential blocks use `<=`; combinational blocks use `=`. Mixing them gives simulation that disagrees with the synthesized hardware.
2. In a combinational block, assign every output in every branch (or give defaults at the top). An output not assigned on some path must remember its old value, and the synthesizer builds a **latch**: almost never what was meant, and a timing hazard.

## What synthesizes

Synthesis maps RTL to gates and flip-flops. Loops are unrolled into parallel hardware, so their bounds must be constants; `#` delays and most system tasks are ignored (they belong to testbenches, lesson 25). Every `<=` in a clocked block makes as many flip-flops as the register has bits.

## Timing in one equation

Between two registers the signal must leave one flip-flop, pass through the logic and arrive before the next edge:

$$T_\text{clk} \geq t_{clk\text{-}q} + t_\text{logic} + t_\text{setup}$$

so the deepest path of logic between registers sets the maximum clock frequency (lesson 16 does this carefully).
:::

::: example
## A design has a 32-bit counter, a 12-state machine and a 16-word × 8-bit register file. How many flip-flops does it need, with the state machine encoded in binary and then one-hot?

::: solution
1. **Count the fixed parts.** The counter has 32 flip-flops and the register file has
   $$16 \times 8 = 128$$
2. **Binary state encoding.** Twelve states need $\lceil\log_2 12\rceil = 4$ flip-flops:
   $$32 + 128 + 4 = 164$$
3. **One-hot state encoding.** One-hot uses one flip-flop per state, 12:
   $$32 + 128 + 12 = 172$$
4. **Why pay for one-hot.** It spends flip-flops (cheap in an FPGA) to make the next-state logic simpler and faster (lesson 4).

**Answer:** 164 flip-flops with a binary state machine, 172 with one-hot.
:::
:::

::: example
## A path between two registers passes through 12 gates of 30 ps each. The flip-flops have 50 ps clock-to-Q and 30 ps setup. What's the fastest clock?

::: solution
1. **Shortest period.** The clock must cover clock-to-Q, the 12 gates and setup:
   $$\begin{aligned} T_\text{clk} &\geq 50 + 12 \times 30 + 30 \\ &= 440\ \text{ps} \end{aligned}$$
2. **Fastest clock.**
   $$\begin{aligned} f_\text{max} &= \frac{1}{440\ \text{ps}} \\ &= 2.27\ \text{GHz} \end{aligned}$$
3. **With a register in the middle.** Halving the logic depth (pipelining, lesson 23) gives
   $$\begin{aligned} T_\text{clk} &= 50 + 180 + 30 \\ &= 260\ \text{ps} \end{aligned}$$
   which is 3.8 GHz, at the cost of one more clock of latency.

**Answer:** the fastest clock is 2.27 GHz (a 440 ps period); splitting the logic in two would allow 3.8 GHz.
:::
:::

::: example
## A stage's logic takes 1.2 ns; its flip-flops have 80 ps clock-to-Q and 50 ps setup, and the clock loses 60 ps to skew and jitter. What is the fastest clock, and what does splitting the logic into two pipeline stages buy?

::: solution
1. **One stage.** Add the logic, clock-to-Q, setup and the clock's losses:
   $$\begin{aligned} T &= 1.2 + 0.08 + 0.05 + 0.06 \\ &= 1.39\ \text{ns} \end{aligned}$$
   which is 719 MHz.
2. **Two stages.** Each stage holds 0.6 ns of logic and pays the same 190 ps overhead:
   $$\begin{aligned} T &= 0.6 + 0.19 \\ &= 0.79\ \text{ns} \end{aligned}$$
   which is 1.27 GHz.
3. **Compare.** The clock is 1.76 times faster, short of twice, because every stage pays the same 190 ps of register and clock overhead:
   $$\frac{1.39}{0.79} = 1.76$$
4. **Deeper pipelines.** For the same reason, each further split returns less and less.

**Answer:** one stage runs at 719 MHz; two stages run at 1.27 GHz, 1.76 times faster.
:::
:::

::: check
given: N = 5 | 12 | 20 | 100
answer: ceil(log(N)/log(2))
tolerance: 0

## How many flip-flops does a binary-encoded state machine with {N} states need?

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

::: misses
- [N] That's one-hot encoding.
- [any] Not yet. Enough bits to give every state its own code.
:::
:::

::: check
given: M = 59 | 999 | 100000
answer: ceil(log(M + 1)/log(2))
tolerance: 0

## How many bits does a counter need to count from 0 to {M}?

::: right
Right. $\lceil\log_2(M + 1)\rceil$ = {answer} bits.
:::

::: misses
- [any] Not yet. {M} + 1 distinct values must fit in $2^n$.
:::
:::

::: check
given: c = 40 | 60; n = 8 | 15 | 25; g = 20 | 35; s = 20 | 40
answer: 1/((c + n*g + s)*1e-12)/1e9
unit: GHz
tolerance: 1%

## A register-to-register path has {n} gates of {g} ps, with clock-to-Q {c} ps and setup {s} ps. What's the maximum clock frequency?

::: right
Right. $\frac{1}{t_{clk\text{-}q} + t_\text{logic} + t_\text{setup}}$ = {answer} GHz.
:::

::: misses
- [1/((n*g)*1e-12)/1e9] Include the flip-flops' clock-to-Q and setup.
- [any] Not yet. Add up the path from one register to the next.
:::
:::

::: check
given: a = 3 | 6 | 9; b = 1 | 4 | 7
answer: b
tolerance: 0

## On a clock edge, registers a = {a} and b = {b} execute `a <= b; b <= a;`. What's a afterward?

::: right
Right. {answer}: nonblocking assignments read the old values, so the registers swap.
:::

::: misses
- [a] The assignment did happen: a takes b's old value.
- [any] Not yet. Read every right-hand side first, then update.
:::
:::

::: check
given: a = 3 | 6 | 9; b = 1 | 4 | 7
answer: b
tolerance: 0

## With blocking assignments `a = b; b = a;` run in order, starting from a = {a} and b = {b}, what's b afterward?

::: right
Right. {answer}: a becomes b first, then b copies the new a. Blocking assignments don't swap.
:::

::: misses
- [a] That's what nonblocking assignments would give.
- [any] Not yet. Run the lines in order.
:::
:::

::: check
given: w = 8 | 16 | 32; d = 4 | 64 | 256
answer: w*d
tolerance: 0

## A register file has {d} words of {w} bits. How many flip-flops does it take if built from registers?

::: right
Right. {answer}. (Large memories use SRAM arrays instead, lesson 12.)
:::

::: misses
- [any] Not yet. Words times bits.
:::
:::

::: check
given: f = 250 | 500 | 800; c = 50 | 80; s = 30 | 50
answer: 1/(f*1e6)*1e12 - c - s
unit: ps
tolerance: 1%

## A design must run at {f} MHz with {c} ps clock-to-Q and {s} ps setup. How much time is left for logic between registers?

::: right
Right. $T_\text{clk} - t_{clk\text{-}q} - t_\text{setup}$ = {answer} ps.
:::

::: misses
- [1/(f*1e6)*1e12] Subtract the flip-flop overheads.
- [any] Not yet. The period minus the register overhead.
:::
:::

::: check
kind: parts
given: g = 8 | 12 | 20; d = 25 | 40; q = 50 | 80; s = 30 | 50; k = 0 | 50

## A register-to-register path passes through {g} gates of {d} ps; the flip-flops have {q} ps clock-to-Q and {s} ps setup, and {k} ps is lost to clock skew and jitter.

::: part
answer: 1/((g*d + q + s + k)*1e-12)/1e6
unit: MHz
tolerance: 0.5%

### What is the fastest clock?

::: misses
- [1/((g*d)*1e-12)/1e6] Add the flip-flops' overhead.
- [any] $T \ge t_{clk\text{-}q} + t_\text{logic} + t_\text{setup} + t_\text{skew}$.
:::
:::

::: part
answer: 1/((g*d/2 + q + s + k)*1e-12)/1e6
unit: MHz
tolerance: 0.5%

### And with the logic split evenly into two pipeline stages?

::: misses
- [2/((g*d + q + s + k)*1e-12)/1e6] Each stage still pays the full overhead.
- [any] Halve the logic, keep the overhead.
:::
:::
:::

::: check
kind: derivation
vars: L = 100..5000, o = 50..300, n = 1..20

## A path of logic delay L is cut into n equal pipeline stages, each paying an overhead o for its register and the clock.

::: step
answer: L/n + o

### What is the clock period?
:::

::: step
answer: (L + o)/(L/n + o)

### By what factor does the throughput rise over one stage?
:::

::: step
answer: (L + o)/o

### What is the limit as n grows without bound?
:::

::: right
Right. The overhead caps the gain, which is why pipelines stop at a depth where the overhead is a sizable fraction of each stage.
:::
:::

::: explain
## Why do sequential always blocks use nonblocking assignments?

::: model
In real hardware, every flip-flop on a clock samples its input at the same instant, the clock edge, and only afterward do the outputs change. Nonblocking assignments model exactly that: every right-hand side is evaluated using the values from before the edge, and all the left-hand sides update together afterward, so the order of the statements doesn't matter. Blocking assignments update immediately, so a later line sees an earlier line's new value, which describes a chain of logic within one clock, not a set of registers updating together. Using blocking assignments for registers makes simulation depend on statement order and disagree with the synthesized circuit.
:::
:::

::: practice

::: check
given: N = 5 | 6 | 11 | 13 | 20 | 30 | 45
answer: ceil(log(N)/log(2))
tolerance: 0

## Binary state bits for {N} states?

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

::: check
given: c = 45 | 70; n = 10 | 20; g = 25 | 40; s = 25 | 35
answer: 1/((c + n*g + s)*1e-12)/1e9
unit: GHz
tolerance: 1%

## $f_\text{max}$: {n} gates × {g} ps, {c} ps clock-to-Q, {s} ps setup?

::: misses
- [any] $1/(t_{cq} + t_\text{logic} + t_\text{setup})$.
:::
:::

::: check
given: M = 9 | 23 | 59 | 99 | 999 | 5000
answer: ceil(log(M + 1)/log(2))
tolerance: 0

## Counter bits to reach {M}?

::: misses
- [any] $\lceil\log_2(M + 1)\rceil$.
:::
:::
:::
