S3 · Lesson 1 · RTL design and the HDL mindset
Describing hardware, not running a program
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.
- 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.
Predict
On a clock edge, registers a = 3 and b = 5 execute a <= b; b <= a; (nonblocking). What are they afterward?
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.
Name it
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.assignstatements andalways @*blocks describe combinational logic, with blocking=assignments and every output assigned on every path.
Two assignment rules that prevent most bugs
- Sequential blocks use
<=; combinational blocks use=. Mixing them gives simulation that disagrees with the synthesized hardware. - 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:
so the deepest path of logic between registers sets the maximum clock frequency (lesson 16 does this carefully).
Worked 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?
Show the solution
- Count the fixed parts. The counter has 32 flip-flops and the register file has
- Binary state encoding. Twelve states need flip-flops:
- One-hot state encoding. One-hot uses one flip-flop per state, 12:
- 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.
Worked 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?
Show the solution
- Shortest period. The clock must cover clock-to-Q, the 12 gates and setup:
- Fastest clock.
- With a register in the middle. Halving the logic depth (pipelining, lesson 23) gives 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.
Worked 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?
Show the solution
- One stage. Add the logic, clock-to-Q, setup and the clock's losses: which is 719 MHz.
- Two stages. Each stage holds 0.6 ns of logic and pays the same 190 ps overhead: which is 1.27 GHz.
- Compare. The clock is 1.76 times faster, short of twice, because every stage pays the same 190 ps of register and clock overhead:
- 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
How many flip-flops does a binary-encoded state machine with 12 states need?
Check
How many bits does a counter need to count from 0 to 999?
Check
A register-to-register path has 8 gates of 35 ps, with clock-to-Q 40 ps and setup 40 ps. What's the maximum clock frequency?
Write units the way you normally would: 1.5 GHz, 1500 MHz and 0.0015 THz are the same answer.
Check
On a clock edge, registers a = 6 and b = 1 execute a <= b; b <= a;. What's a afterward?
Check
With blocking assignments a = b; b = a; run in order, starting from a = 6 and b = 1, what's b afterward?
Check
A register file has 4 words of 16 bits. How many flip-flops does it take if built from registers?
Check
A design must run at 500 MHz with 50 ps clock-to-Q and 50 ps setup. How much time is left for logic between registers?
Write units the way you normally would: 1.5 ps and 0.0015 ns are the same answer.
Check
A register-to-register path passes through 12 gates of 25 ps; the flip-flops have 80 ps clock-to-Q and 50 ps setup, and 0 ps is lost to clock skew and jitter.
(a)
What is the fastest clock?
Write units the way you normally would: 1.5 MHz, 1500 kHz and 0.0015 GHz are the same answer.
(b)
And with the logic split evenly into two pipeline stages?
Check
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 1
What is the clock period?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
By what factor does the throughput rise over one stage?
Step 3
What is the limit as n grows without bound?
Explain why
Why do sequential always blocks use nonblocking assignments?
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
Keep practicing
11 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.