D105 of 13
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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?

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
  1. One stage per edge. Each nonblocking stage passes its value one stage per edge, so the bit takes 16 clock periods.
  2. Clock period. 150MHz=20ns
  3. Total delay. 16×20ns=320ns

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
  1. What each edge does. The nonblocking assignments swap a and b at every edge.
  2. 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
  1. The first statement. a = b sets a to 0 immediately.
  2. The second statement. b = a copies that new a into b, so both become 0 at the first edge and stay there.
  3. 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?

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.