What is Blocking assignment?

Verilog's "=" assignment, which updates the left side immediately so the next statement sees the new value. Used in combinational always blocks and testbenches.

Field: HDL & Verification

Japanese name: ブロッキング代入

How blocking assignment works

A blocking assignment, written with a single equals sign, evaluates its right-hand side and updates the left-hand variable before the next statement in the same procedural block runs, much like assignment in C. Statements therefore act in order, and a later line sees the value an earlier line just wrote. A nonblocking assignment, by contrast, evaluates every right-hand side first and applies all the updates together later in the time step, so each statement sees the values from before the clock edge.

Why it breaks clocked logic

Inside a clocked always block, blocking assignments let one register see another's new value within the same edge. Writing "q1 = d; q2 = q1;" for a two-stage shift register gives q2 the freshly updated q1, which is d, so both simulation and the synthesized circuit delay by only one stage. Likewise "a = b; b = a;" leaves both equal to the old b instead of swapping them. Using nonblocking assignments for every register in a clocked block makes the code behave like parallel flip-flops regardless of statement order.

Where blocking is the right choice

Blocking assignment belongs in combinational always blocks, written as always @(*) or SystemVerilog always_comb, where an intermediate variable computed on one line feeds the next and the result describes a chain of gates. It is also natural in testbench procedural code and for temporary variables inside functions and tasks. The widely followed rules are: nonblocking for clocked logic, blocking for combinational logic, and never assign the same variable with both kinds.

Races in testbenches

Driving a design input with a blocking assignment at exactly the same simulation time as the active clock edge creates a race: whether the design's flip-flops capture the old or the new value depends on which process the simulator happens to execute first, and results can differ between simulators. Common fixes are changing stimulus on the opposite clock edge or a short delay after the active edge, driving inputs with nonblocking assignments, or using SystemVerilog clocking blocks.

Try it: questions about Blocking assignment

In a testbench, changing a design input with a blocking assignment at the same instant as the rising clock edge can cause problems. Which pairs the reason with a fix correctly?

  1. AWhether the design captures the old or new value depends on execution order (a race); change inputs away from the clock edge or with nonblocking assignments
  2. BThe clock frequency automatically drops; make the clock faster
  3. CBlocking assignments are not allowed in testbenches; use assign everywhere
Show answer and explanation

Answer: A. Whether the design captures the old or new value depends on execution order (a race); change inputs away from the clock edge or with nonblocking assignments

When the testbench updates an input at the same instant the design's flip-flops sample it, the captured value can be old or new depending on which the simulator executes first. This is a simulation race, and results may even differ between simulators. It is prevented by changing inputs on the falling edge or slightly after the edge, or by using nonblocking assignments or SystemVerilog clocking blocks.

FAQ

When should I use blocking vs nonblocking assignment in Verilog?

Use nonblocking assignment for registers in clocked always blocks and blocking assignment in combinational blocks and for temporary variables. Do not mix both kinds on the same variable.

Why does my Verilog shift register only delay by one clock?

Most likely the stages are written with blocking assignments, so each stage receives the value the previous line just updated. Switching to nonblocking assignments makes every stage take the pre-edge value.

Is a VHDL variable assignment like a Verilog blocking assignment?

Yes, roughly: a VHDL variable assignment takes effect immediately within the process, while a VHDL signal assignment is scheduled and behaves more like a nonblocking assignment.

Questions that mention Blocking assignment (2)

Blocking assignment appears in 3 practice questions across 1 topic, including HDL & Verification.

  1. In the code above, a = 0 and b = 1 just before a rising clock edge. What are a and b after the edge?
  2. Intending a two-stage shift register, someone wrote the code above with blocking assignments (=). What goes wrong?

Books on HDL & Verification Ad

As an Amazon Associate I earn from qualifying purchases.

Comparisons that include this term

This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.

Back to the glossary