NanoSchool / Blog / VLSI & Digital Design
Why a Verilog Circuit Passes One Test but Fails Another
Your Verilog circuit works perfectly in one simulation but fails when you change the test conditions. Discover why this happens and how to find hidden errors in digital circuit design.
- 4logic states in Verilog: 0, 1, X and Z
- 6common causes of hidden failures covered
- 8steps in a systematic debugging process
- 4simulation and waveform tools introduced
In this article
Why Does Correct Verilog Code Suddenly Fail?
Imagine designing a digital counter using Verilog.
You write the code, create a testbench, and run the simulation.
Everything looks correct. The counter produces the expected output, and your test passes.
But when you try another input sequence, the result is different.
The circuit fails.
You haven’t changed the design, so what went wrong?
This is a common challenge in digital electronics and VLSI design.
A Verilog circuit can pass one test and fail another because different tests examine different parts of its behavior.
Some tests reveal timing problems, incorrect reset conditions, missing logic, or unexpected signal values.
Others may simply fail to detect those errors.
Understanding these problems is an important part of Verilog Testbench Debugging.
Let’s explore why simulation results change and how engineers can identify the real problem.
What Is Verilog, and Why Is Simulation Important?
Verilog is a hardware description language used to design and verify digital electronic circuits.
Engineers use it to describe components such as:
- Counters and registers
- Multiplexers and decoders
- Arithmetic circuits
- Finite-state machines
- Memory interfaces
- Digital controllers
Before implementing a circuit on an FPGA or manufacturing an integrated circuit, engineers usually test its behavior through simulation.
A simulation includes two main parts.
Design Under Test
The digital circuit being evaluated.
The checker
A separate piece of code that provides inputs and checks the outputs.
For example, a testbench may apply different combinations of inputs to an adder.
If the output matches the expected result, the test passes.
However, one successful test does not prove that every possible condition has been checked.
Incomplete Test Coverage: The Hidden Problem
One common reason for inconsistent results is incomplete testing.
Imagine designing a four-bit binary adder.
You test the following values:
- 2 + 3 = 5
- 4 + 2 = 6
- 5 + 1 = 6
Every test passes.
But what happens when you try 15 + 15?
The answer is 30, which requires five binary bits.
If your circuit has only a four-bit output, the result will be truncated.
Example: Incorrect output width
module adder( input [3:0] a, b, output [3:0] sum ); assign sum = a + b; endmodule
This design discards any carry beyond the four-bit output.
That may be correct if the specification requires only the lowest four bits. But it is wrong if the full addition result is required.
A suitable correction is:
module adder( input [3:0] a, b, output [4:0] sum ); assign sum = {1'b0, a} + {1'b0, b}; endmodule
The five-bit output can now represent the complete result.
Key lesson: Always test boundary values, not just simple inputs.
Blocking and Nonblocking Assignments
Another common source of errors is the incorrect use of assignment operators.
Verilog supports two important assignment types:
= updates a variable immediately when the statement executes.
<= schedules updates for a later simulation event region.
Consider this code:
always @(posedge clk) begin q1 = d; q2 = q1; end
Here, q2 receives the newly assigned value of q1.
If the intention is to create two registers connected in sequence, this is not the expected behavior.
A better implementation is:
always @(posedge clk) begin q1 <= d; q2 <= q1; end
Now, q2 receives the previous value of q1.
This models the expected behavior of two cascaded flip-flops.
A useful guideline is to use nonblocking assignments for clocked sequential logic and blocking assignments for combinational logic.
Race Conditions: When Timing Changes the Result
Digital circuits depend heavily on timing.
A race condition occurs when the outcome depends on the order in which simulation events execute.
Imagine that your testbench changes an input at exactly the same moment the circuit reads it.
Which value will the circuit receive?
The old value or the new one?
Depending on the event scheduling, the result may not be reliable.
For example:
always @(posedge clk) data_in = 1'b1;
If the DUT also samples data_in at the rising edge, the testbench may create a race condition.
One way to avoid this in simple synchronous testbenches is to apply input changes at the falling edge when the design samples them at the rising edge.
More advanced verification environments can use SystemVerilog clocking blocks to control signal timing.
Important point: A circuit may appear correct in one test because the timing conditions do not expose the race.
Missing Reset Logic Can Cause Simulation Failures
Sequential circuits often depend on registers having a defined starting state.
Consider a counter:
reg [3:0] count; always @(posedge clk) count <= count + 1'b1;
What happens when the simulation starts?
In a conventional four-state Verilog simulation, count will generally begin with an unknown value, represented by X.
The counter may then continue producing unknown results.
A circuit that requires a reset could use:
always @(posedge clk or posedge rst) begin if (rst) count <= 4'b0000; else count <= count + 1'b1; end
This gives the register a defined value when reset is asserted.
However, a reset must also be applied correctly by the testbench. The appropriate reset architecture depends on the hardware requirements.
Incomplete Logic Can Create Unexpected Latches
Sometimes, a Verilog design fails because it does not define an output for every possible condition.
Consider:
always @(*) begin if (enable) y = a; end
What should happen when enable becomes zero?
The code does not provide a new value.
As a result, synthesis may infer a latch to preserve the previous value.
If the intended design is combinational, a better implementation is:
always @(*) begin if (enable) y = a; else y = 1'b0; end
Now the output is defined for both conditions.
A test that uses only enable = 1 may pass.
Another test that uses enable = 0 may reveal the problem.
Why Unknown Values Matter in Verilog
Verilog uses four logic states:
| Value | Meaning |
|---|---|
| 0 | Logic low |
| 1 | Logic high |
| X | Unknown |
| Z | High impedance |
Unknown values can appear because of uninitialized registers, conflicting drivers, or other unresolved conditions.
These values may affect calculations and comparisons.
A testbench must check outputs carefully.
For example, case equality (===) allows explicit comparison of four-state values.
However, matching unknown values should not be treated as proof of correct circuit behavior when the specification requires known outputs.
Understanding X and Z values is essential for reliable Verilog debugging.
A Practical Example: A Counter That Passes and Fails
Imagine testing a four-bit counter.
The testbench applies a reset and checks whether the counter increments correctly.
The counter starts counting. After reaching a particular value, the testbench activates the reset signal.
In Test 1, the output sequence is:
- 0
- →
- 1
- →
- 2
- →
- 3
- →
- 4
Everything works.
In Test 2, if the design does not respond according to its reset specification, the test fails.
The first test passed because it checked only normal counting behavior.
The second test introduced an additional condition.
This demonstrates why verification engineers need multiple test scenarios.
Passing one test means the circuit behaved correctly under that test’s conditions, not necessarily under every condition.
How to Debug Verilog Simulation Failures
When a circuit fails, engineers should follow a systematic debugging process.
- 1
Reproduce the error
Run the same test again and identify the failing input condition.
- 2
Check the expected output
Verify that the testbench is checking the correct behavior.
- 3
Examine waveforms
Inspect clock, reset, input, and output signals.
- 4
Look for unknown values
Identify unexpected
XorZvalues. - 5
Check assignment operators
Review blocking and nonblocking assignments.
- 6
Verify signal widths
Look for overflow, truncation, and signed-number errors.
- 7
Review timing
Confirm that test inputs and output checks occur at the appropriate time.
- 8
Add a new regression test
Preserve the failing case so the problem can be detected again in future simulations.
These steps can help engineers identify errors without randomly changing the code.
Which Tools Help with Verilog Debugging?
Several tools support RTL simulation and verification.
Icarus Verilog
An open-source tool for compiling and simulating Verilog designs.
Verilator
A tool supporting RTL linting and simulation.
GTKWave
A waveform viewer used to inspect signal changes during simulation.
ModelSim/Questa
Commercial simulation and verification tools used in many hardware development workflows.
Each tool provides different capabilities.
For beginners, starting with a simulator and waveform viewer is a useful way to learn how digital circuits behave.
Why Verilog Verification Matters in VLSI Engineering
Digital circuits are used in processors, communication devices, embedded systems, and many other electronic products.
An RTL error may affect a larger system if it remains undetected.
Finding errors early can reduce development risk and costly redesigns.
This makes verification an important skill for students and professionals interested in:
- VLSI design
- FPGA development
- ASIC verification
- Digital electronics
- Semiconductor engineering
- Embedded hardware systems
Understanding RTL simulation, testbench development, and debugging can help build a foundation for more advanced semiconductor design work.
Build Skills in VLSI and Digital Electronics with NanoSchool
Finding errors early can reduce development risk and costly redesigns, which makes verification an important skill for students and professionals.
At NSTC | NanoSchool, learners can explore educational opportunities in electronics, engineering, and emerging technologies.
Understanding RTL simulation, testbench development, and debugging can help build a foundation for more advanced semiconductor design work.
Frequently Asked Questions
1. Why does my Verilog code pass one test but fail another?
Different tests may expose different issues, including timing errors, missing reset conditions, incomplete logic, or signal-width problems.
2. What is Verilog Testbench Debugging?
It is the process of finding and correcting problems in a Verilog design or its testbench by checking inputs, outputs, timing, and expected behavior.
3. What is a race condition in Verilog?
A race condition occurs when simulation behavior depends on an unspecified order of events.
4. What is the difference between blocking and nonblocking assignments?
Blocking assignments update variables immediately during execution, while nonblocking assignments schedule updates for a later simulation event region.
5. Why do I see X values in my simulation?
Unknown values often appear because of uninitialized registers or unresolved signal conditions.
6. Which tools can beginners use to debug Verilog?
Icarus Verilog, Verilator, and GTKWave are useful tools for learning simulation and waveform analysis.
Conclusion: One Passing Test Is Not Enough
A Verilog circuit may work perfectly in one simulation but fail under different conditions.
The problem could involve incomplete test coverage, incorrect assignment operators, race conditions, missing reset logic, or signal-width errors.
The solution is careful verification.
By testing boundary conditions, reviewing timing, examining waveforms, and improving testbenches, engineers can identify hidden issues before hardware implementation.
Reliable digital design is not about passing one test. It is about ensuring that the circuit behaves correctly across the conditions defined by its specification.
Explore more learning opportunities in VLSI, digital electronics, and semiconductor technology at NanoSchool.
Technical note: The examples are simplified for educational purposes. Actual verification requirements depend on the circuit specification and implementation technology.
Technical References
View the four references
- IEEE. IEEE Standard for SystemVerilog (IEEE 1800-2023). https://standards.ieee.org/ieee/1800/7743/
- Icarus Verilog. Official Documentation. https://steveicarus.github.io/iverilog/
- Verilator. Official Documentation. https://verilator.org/guide/latest/
- GTKWave. Official Website. https://gtkwave.sourceforge.net/
