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.
- Clock / reset
- Step
- Control / FSM
- Clock
- Data
- An event
clk changes from 0 to 1. Every always block waiting for a rising edge wakes up.
- Woken processes run
They run one at a time. The standard does not say which goes first.
- Blocking updates at once
A blocking assignment changes its variable straight away, so a process that runs later sees the new value.
- Nonblocking updates later
Nonblocking updates wait until every woken process has read its inputs. Then they all land.
- 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.
| Question | |
|---|---|
| 1 | What is an event, and what is a time step? |
| 2 | What does the standard fix about the order of work in one step, and what does it leave open? |
| 3 | Why can two always blocks race? |
| 4 | Why do nonblocking assignments remove the race? |
| 5 | What 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
| Word | Meaning |
|---|---|
| Event | A change of a signal's value at some instant of simulated time. |
| Time step | All the work a simulator does at one instant before it moves time forward. |
| Process | A piece of code the simulator runs and wakes, such as one always or initial block. |
| Blocking assignment | a = b: the new value is stored at once, so later code in the same instant sees it. |
| Nonblocking assignment | p <= q: the right side is read now, and the update lands after every woken process has run. |
| Race | A result that depends on the order the simulator happens to pick for processes in one time step. |
| Always block | Code that runs again each time its event happens, such as every rising edge of clk. |
| x | The 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.
| Order | First step | Second step | After the edge |
|---|---|---|---|
| A then B | a = b, so a becomes 2 | b = a, so b becomes 2 | a = 2, b = 2 |
| B then A | b = a, so b becomes 1 | a = b, so a becomes 1 | a = 1, b = 1 |
| nonblocking, any order | both read: p sees 2, q sees 1 | both update together | p = 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
Rule 1: Simulation is events at instants.
The simulator finishes every event of one instant before it moves time on.
Rule 2: Inside one block, statements run in order.
The order of statements within a begin-end block is guaranteed.
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.
Rule 4: Nonblocking updates land after the reads.
Use <= for flip-flops, so every process reads the old values first.
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.
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;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=1Read 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.
| The idea from this page | Where it is taught |
|---|---|
| Scheduling stages and nonblocking updates in depth | Scheduling Regions and Nonblocking Updates |
| A race between a monitor and the design | Debug Case: A Racing Monitor |
| An unknown that became a zero | Debug Case: The X That Became Zero |
| Four-state and two-state types | Four-State vs Two-State Types |
| Unknown values checked by a formal tool | X-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?
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=1Check Yourself
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.