How a Simulator Thinks

Learn just enough of how a simulator works to read a waveform honestly. Meet events, time steps and the order of work in one step. Then see unknown values, and why two always blocks can race.

You Will Be Able To

  • Describe simulation as events processed at instants of simulated time.
  • Say what the standard fixes about the order of work in one time step, and what it leaves open.
  • Explain why two always blocks with blocking assignments can race, and how nonblocking assignments remove the race.
  • Read an unknown value x in a waveform, and say what an if statement does with one.
Lesson
B4.2
Time
22 min
Runs on
Your browser
Check
5 questions

Two registers should swap on every clock edge. On this site's simulator both end up holding 1; move two lines of code and both hold 2. The design did not change. Only the order of two lines did, and that is the most important clue to how a simulator thinks.

A Time Line of Events at One InstantStep through one rising edge of the clock. Simulated time stands still while the simulator works through that instant, in stages. Then it moves to the next time.
  • Clock / reset
  • Step
  • Control / FSM
  • Clock
  • Data
A Time Line of Events at One InstantStep through one rising edge of the clock. Simulated time stands still while the simulator works through that instant, in stages. Then it moves to the next time. clock reset Time 1: clk rises one event wakesevery process waitingon it step Active stage woken processes run,in an order thesimulator picks control Blocking a = b a changes at once;the next processsees it step Nonblocking stage every p <= q update,after all the readingis done clock reset Time 2 only when the instanthas nothing left wakesone processthenthen time moves
  1. clock reset Time 1: clk risesone event wakes every process waiting on it
    • wakes → Active stage
  2. step Active stagewoken processes run, in an order the simulator picks
    • one process → Blocking a = b
    • then → Nonblocking stage
  3. control Blocking a = ba changes at once; the next process sees it
  4. step Nonblocking stageevery p <= q update, after all the reading is done
    • then time moves → Time 2
  5. clock reset Time 2only when the instant has nothing left
  1. An event

    clk changes from 0 to 1. Every always block waiting for a rising edge wakes up.

  2. Woken processes run

    They run one at a time. The standard does not say which goes first.

  3. Blocking updates at once

    A blocking assignment changes its variable straight away, so a process that runs later sees the new value.

  4. Nonblocking updates later

    Nonblocking updates wait until every woken process has read its inputs. Then they all land.

  5. Time moves on

    Only when nothing is left at this instant does the simulator move to the next time.

Read the diagram as text
  • Time 1: clk rises: one event wakes every process waiting on it
  • Active stage: woken processes run, in an order the simulator picks
  • Blocking a = b: a changes at once; the next process sees it
  • Nonblocking stage: every p <= q update, after all the reading is done
  • Time 2: only when the instant has nothing left
  • Time 1: clk rises → Active stage: wakes
  • Active stage → Blocking a = b: one process
  • Active stage → Nonblocking stage: then
  • Nonblocking stage → Time 2: then time moves

An idea drawing of one time step. The standard divides a time step into more stages than these; a beginner needs these two.

Before You Start

Read The Verification Plan first. You should know what a flip-flop and a clock edge are: Modulo-N, Up/Down and Loadable Counters is a refresher.

Five questions this page answers
Question
1What is an event, and what is a time step?
2What does the standard fix about the order of work in one step, and what does it leave open?
3Why can two always blocks race?
4Why do nonblocking assignments remove the race?
5What is x, and what does if do with it?

The Idea: Time Stands Still While the Simulator Works

Real hardware does everything at once. A simulator is a program, so it does one thing at a time and pretends otherwise. It keeps a clock of simulated time and a list of events: changes to signals. At each instant it works through every event of that instant before moving on. Simulated time stands still while it works.

When clk rises, every process waiting for that edge wakes up. The simulator runs them one after another, in some order. The standard says the order of processes is not fixed. Inside one begin-end block, statements run in the order written. Between two always blocks, any order is legal.

Most of the time the order does not matter. It matters when one process writes a variable that another reads in the same instant. Then the result depends on who went first. That is a race, and nonblocking assignments exist to prevent it.

New Words

New words on this page
WordMeaning
EventA change of a signal's value at some instant of simulated time.
Time stepAll the work a simulator does at one instant before it moves time forward.
ProcessA piece of code the simulator runs and wakes, such as one always or initial block.
Blocking assignmenta = b: the new value is stored at once, so later code in the same instant sees it.
Nonblocking assignmentp <= q: the right side is read now, and the update lands after every woken process has run.
RaceA result that depends on the order the simulator happens to pick for processes in one time step.
Always blockCode that runs again each time its event happens, such as every rising edge of clk.
xThe unknown value: a bit that could be 0 or 1. Four-state variables, such as logic, hold 0, 1, x or z (z: nothing drives it), and start as x.

The Smallest Example: Two Orders by Hand

Start with a = 1 and b = 2. At a clock edge, line A does a = b and line B does b = a, both with blocking assignments. Work out both orders.

One clock edge, two legal orders
OrderFirst stepSecond stepAfter the edge
A then Ba = b, so a becomes 2b = a, so b becomes 2a = 2, b = 2
B then Ab = a, so b becomes 1a = b, so a becomes 1a = 1, b = 1
nonblocking, any orderboth read: p sees 2, q sees 1both update togetherp = 2, q = 1: swapped

Neither blocking order swaps the values, and the two orders disagree with each other. The nonblocking version swaps them, whatever the order, because every process reads before any process writes.

The Rules

  1. Rule 1: Simulation is events at instants.

    The simulator finishes every event of one instant before it moves time on.

  2. Rule 2: Inside one block, statements run in order.

    The order of statements within a begin-end block is guaranteed.

  3. Rule 3: Between blocks, the order is open.

    Processes woken at the same instant may run in any order. The result must not depend on it.

  4. Rule 4: Nonblocking updates land after the reads.

    Use <= for flip-flops, so every process reads the old values first.

  5. Rule 5: x means unknown, and if treats it as false.

    A four-state variable starts as x. An if whose condition is x takes the else branch.

Sources for These Rules

Simulation as discrete events; update and evaluation events; time slots divided into regions: IEEE 1800-2023, clauses 4.3 and 4.4.

Statements in a begin-end block run in source order, and nonblocking updates keep their execution order: clause 4.6.

Active events may be processed in any order: clause 4.7. Race conditions: clause 4.8.

Default initial value x for four-state variables: clause 6.8, Table 6-7. An if condition that is x or z is treated as false: clause 12.4.

The Lab: A Race, a Swap, and Two Unknowns

The lab builds the two swaps from the table and adds a register with no reset and a flag nobody set. The code of the swaps.

@(posedge clk) waits for a rising edge of clk. Each always block is a separate process.
logic [3:0] a = 1, b = 2;           // should swap on every edge
always @(posedge clk) a = b;         // line A
always @(posedge clk) b = a;         // line B

logic [3:0] p = 1, q = 2;            // the same swap, nonblocking
always @(posedge clk) p <= q;
always @(posedge clk) q <= p;
Printed by the lab (Icarus Verilog version 12.0), with line A written first.
at the edge: p=1
nonblocking: p=2 q=1
at the edge: p=2
at the edge: p=1
at the edge: p=2
count with no reset after 4 edges: xxxx
if (flag) with flag = x took the else branch
blocking, line A written first: a=1 b=1

Read the last line first: blocking, line A written first: a=1 b=1. With line A written first, this simulator ran line B first. Swap the two lines in the source and it prints blocking, line B written first: a=2 b=2. Rule 3 at work: both are legal, and neither swapped anything. Another simulator, or another version of this one, may choose the other way.

The nonblocking pair swapped correctly, p = 2 and q = 1, in both versions of the source. Rule 4 at work. The first line shows the same rule from another side. A display running at the edge itself saw p = 1, the value before the update landed.

Then the unknowns. The register count has no reset, so it started as x. And x plus 1 is still x, four edges later. The flag was never set, so if (flag) took the else branch. Rule 5 at work: an x can quietly steer code down a branch.

Why the Standard Leaves the Order Open

Why does the standard leave the order open? Because hardware has no order: all flip-flops on a clock change at the same moment. Fixing an order would let code depend on something real chips never do. Simulators are also free to reorder work to run faster.

So the language gives you a tool instead: nonblocking assignments. Every flip-flop reads its input in one stage and updates in the next, exactly like real flops. Code written that way gives the same answer in every legal order.

Traps

The first trap is a race that hides. Blocking assignments between always blocks work on your simulator, because it happens to pick the order you hoped for. A new simulator version picks another, and a test that passed for a year fails with no change to the design. Icarus can even change its choice from one clock edge to the next, as the debug problem of this lesson shows. Use nonblocking assignments for every flip-flop, in the design and in the bench.

The second trap is reading a waveform at the edge. Most viewers show a register's new value right at the clock edge. Code running at that same instant still sees the old value, as the lab's first line shows. Read the value just before the edge to know what a flip-flop sampled.

The third trap is x turned into a decision. A flag that was never reset is x, and an if quietly takes its else branch. The test passes, the chip powers up with a random value, and the same if goes the other way. Reset what must be known, and treat any x in a waveform as a question to answer.

Where This Goes Next

This was the minimum. These pages go deeper into the order of work and into unknown values.

Where each idea is taught in depth
The idea from this pageWhere it is taught
Scheduling stages and nonblocking updates in depthScheduling Regions and Nonblocking Updates
A race between a monitor and the designDebug Case: A Racing Monitor
An unknown that became a zeroDebug Case: The X That Became Zero
Four-state and two-state typesFour-State vs Two-State Types
Unknown values checked by a formal toolX-Propagation

Try It

Run it: the blocking pair races, the nonblocking pair swaps, and two unknowns show what x does.

Swap the order of line A and line B in the source. Predict the last line before you run it.

Give count a reset to 0 before the first edge. What does the count line print now?

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: at the edge: p=1 nonblocking: p=2 q=1 at the edge: p=2 at the edge: p=1 at the edge: p=2 count with no reset after 4 edges: xxxx if (flag) with flag = x took the else branch blocking, line A written first: a=1 b=1

Check Yourself

Predict the outputLine B is now written before line A. What is the last line the lab prints?
module tb;
  logic clk = 0;
  // Two registers that should swap on every rising edge.
  logic [3:0] a = 1, b = 2;
  always @(posedge clk) b = a;     // line B
  always @(posedge clk) a = b;     // line A
  // The same swap with nonblocking assignments (<=).
  logic [3:0] p = 1, q = 2;
  always @(posedge clk) p <= q;
  always @(posedge clk) q <= p;
  // A register with no reset, and a flag nobody set.
  logic [3:0] count;
  logic flag;
  always @(posedge clk) count <= count + 1;
  // At the edge itself, a display sees the old value of p.
  always @(posedge clk) $display("at the edge: p=%0d", p);
  initial begin
    #1 clk = 1;
    #1 $display("nonblocking: p=%0d q=%0d", p, q);
    repeat (3) begin #1 clk = 0; #1 clk = 1; end
    #1 $display("count with no reset after 4 edges: %b", count);
    if (flag) $display("if (flag) with flag = %b took the if branch", flag);
    else $display("if (flag) with flag = %b took the else branch", flag);
    $display("blocking, line B written first: a=%0d b=%0d", a, b);
  end
endmodule
ConceptWhat does the standard guarantee about two always blocks woken by the same clock edge?
ConceptWhy does the nonblocking swap work in every order?
Conceptcount has no reset and is incremented on every edge. What does it hold after four edges?
Conceptflag is x. Which branch does if (flag) ... else ... take?

Common Mistakes

Wrong Use blocking assignments for flip-flops in separate always blocks.

The result depends on the order the simulator picks, which the standard leaves open.

Fix Use nonblocking assignments (<=) for every flip-flop.

Wrong Read a register's value right at the clock edge in a waveform.

Code at that instant still sees the old value.

Fix Read the value just before the edge to see what was sampled.

Wrong Ignore x in a waveform because the test passed.

An x condition takes the else branch in simulation; real silicon may go either way.

Fix Reset what must be known, and explain every x.

Remember

  • A simulator works through every event of one instant before simulated time moves on.
  • Statements inside one block run in order; processes woken together may run in any order.
  • Blocking writes read by another block in the same instant race; nonblocking updates land after all the reads.
  • Four-state variables start as x, and an if with an x condition takes the else branch.

Practice

Report a problem with this page