D10 · Lesson 5 · always blocks
Describing logic procedurally with always blocks, clocked blocks that become flip-flops, combinational blocks that must assign every output to avoid latches, and the difference between nonblocking assignment, which updates all registers together at the clock edge, and blocking assignment, which updates in order
Continuous assign statements describe wires. To describe registers, Verilog uses always blocks, which look like little programs: if-else chains, case statements, assignments in sequence. That resemblance is useful and dangerous. Inside a clocked block, writing a <= b; b <= a; swaps two registers, which no sequential program could do in two lines, because both right-hand sides are read before either register changes. Understanding when Verilog behaves like a program and when it behaves like hardware is most of the difference between code that synthesizes as intended and code that doesn't.
- A two-stage shift register written with nonblocking assignments:
always @(posedge CLK) begin Q1 <= Q0; Q0 <= D; end. - At each rising clock edge, both flip-flops update at once from the values just before the edge, so a 1 on D reaches Q0 after one clock and Q1 after two.
- The order of the two lines doesn't matter, because nonblocking assignments all take effect together.
Predict
A clocked always block says Q0 = D; Q1 = Q0; with blocking assignments. What does it synthesize?
Blocking assignments update immediately, in order, like a program: Q0 takes D, and then Q1 takes the new Q0, which is D. So both flip-flops load D at the same edge, and the intended shift is lost. With nonblocking assignments (<=) the right-hand sides are all read first, giving a true two-stage shift register in either order.
Name it
Clocked blocks
always @(posedge clk) begin
if (reset) count <= 0;
else if (en) count <= count + 1;
end
Every signal assigned in a clocked block becomes a flip-flop (or a register of them). Conditions that don't assign it, like en being 0 here, simply mean it holds its value: the tools build an enable.
Nonblocking and blocking
- Nonblocking
<=: all right-hand sides are evaluated at the clock edge, then all targets update together. This matches how flip-flops behave, so use it in clocked blocks. - Blocking
=: each assignment completes before the next line runs, like a program. Use it in combinational blocks.
Mixing them in one block, or using blocking assignments for registers, causes simulation and hardware to disagree.
Combinational blocks
always @(*) begin
case (sel)
2'b00: y = a;
2'b01: y = b;
default: y = 0;
endcase
end
@(*) re-evaluates whenever any input changes, describing pure logic. Every output must be assigned on every path: if some case leaves y unassigned, y must remember its old value, and the tools build an unintended latch. A default branch or an assignment at the top of the block prevents it.
Worked example
A shift register has 16 stages clocked at 50 MHz. How long does a bit take to come out the end?
Show the solution
- One stage per edge. Each nonblocking stage passes its value one stage per edge, so the bit takes 16 clock periods.
- Clock period.
- Total delay.
Answer 320 ns, 16 clock periods of 20 ns.
Worked example
always @(posedge clk) begin a <= b; b <= a; end starts with a = 1, b = 0. What are a and b after three clocks?
Show the solution
- What each edge does. The nonblocking assignments swap a and b at every edge.
- Follow three edges. Starting from (a, b) = (1, 0), they read (0, 1) after the first edge, (1, 0) after the second and (0, 1) after the third.
Answer after three clocks, a = 0 and b = 1.
Worked example
The swap is written with blocking assignments: always @(posedge clk) begin a = b; b = a; end, starting from a = 1, b = 0. What happens?
Show the solution
- The first statement.
a = bsets a to 0 immediately. - The second statement.
b = acopies that new a into b, so both become 0 at the first edge and stay there. - Why it differs. Blocking assignments run in order like software; nonblocking ones all read the old values, which is what flip-flops do.
Answer both a and b become 0 at the first edge and stay there.
Check
In a combinational always @(*) block, an if assigns y when sel is 1 but nothing assigns y when sel is 0. What do the synthesis tools build?
Check
A clocked block contains b <= a; c <= b;. After one clock edge, what does c hold?
Check
A bit enters a 16-stage shift register clocked at 25 MHz. How long until it reaches the output?
Write units the way you normally would: 1.5 ns, 1500 ps and 0.0015 µs are the same answer.
Check
Registers a and b start at a = 1, b = 0 and swap with nonblocking assignments at every clock edge. What is a after 4 edges?
Check
A shift register has 16 stages clocked at 25 MHz.
(a)
How long does a bit take to come out the end?
Write units the way you normally would: 1.5 ns, 1500 ps and 0.0015 µs are the same answer.
(b)
How many bits does it hold at once?
Check
always @(posedge clk) begin a <= b; b <= a; end starts with a = 0, b = 1.
(a)
What is a after 3 clocks?
(b)
And b?
Explain why
Why should clocked always blocks use nonblocking assignments?
Every flip-flop in a synchronous circuit samples its input at the same clock edge, so all of them see the values that existed just before the edge, and only afterward do their outputs change. Nonblocking assignments model exactly that: the simulator evaluates every right-hand side first and then updates all the targets together, so the order of the statements doesn't matter and the simulation matches the hardware. Blocking assignments update immediately, so a later line in the block sees an earlier line's new value, which can collapse a pipeline or shift register in simulation in ways that don't match, or don't even resemble, the hardware that synthesis builds.
Practice
Keep practicing
6 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.