Verifying output with $finish in Verilog - Time & Space Complexity
When we use $finish in Verilog, it stops the simulation. We want to see how long the simulation runs before it stops.
How does the number of steps before $finish change as input size grows?
Analyze the time complexity of the following code snippet.
module test;
integer i;
initial begin
for (i = 0; i < 100; i = i + 1) begin
$display("Count: %0d", i);
end
$finish;
end
endmodule
This code counts from 0 to 99, printing each number, then stops the simulation with $finish.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
forloop that runs 100 times. - How many times: Exactly 100 times before
$finishstops the simulation.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 loop steps |
| 100 | 100 loop steps |
| 1000 | 1000 loop steps |
Pattern observation: The number of steps grows directly with the input size. Double the input, double the steps.
Time Complexity: O(n)
This means the simulation time grows in a straight line as the input size increases.
[X] Wrong: "The $finish command makes the simulation run instantly, so time complexity is constant."
[OK] Correct: $finish stops the simulation only after all loop steps run. The loop still takes time proportional to its size before $finish ends it.
Understanding how $finish interacts with loops helps you reason about simulation time and testbench efficiency. This skill shows you can think about how code runs, not just what it does.
"What if we replaced the for loop with a nested loop? How would the time complexity change?"
Practice
$finish command do in a Verilog simulation?Solution
Step 1: Understand the purpose of
The$finish$finishcommand is used to stop the Verilog simulation immediately when called.Step 2: Compare with other simulation commands
Unlike$stopwhich pauses the simulation,$finishends it completely.Final Answer:
Stops the simulation immediately -> Option BQuick Check:
$finish= stop simulation [OK]
- Confusing $finish with $stop
- Thinking $finish restarts simulation
- Assuming $finish only pauses simulation
Solution
Step 1: Identify correct syntax for ending simulation
In Verilog,$finish;is the correct system task to end simulation.Step 2: Check other options for syntax errors
$stop;pauses simulation,finish();andend_simulation;are invalid syntax.Final Answer:
$finish; -> Option AQuick Check:
Correct syntax to end simulation = $finish; [OK]
- Using $stop; instead of $finish;
- Writing finish() like a function
- Missing semicolon after $finish
initial begin
$display("Start");
$finish;
$display("End");
endSolution
Step 1: Trace the code execution order
The first$displayprints "Start". Then$finishstops simulation immediately.Step 2: Understand effect of
Because simulation ends at$finishon following statements$finish, the second$displayis never executed.Final Answer:
Start -> Option DQuick Check:
Output before $finish runs = Start [OK]
- Expecting output after $finish
- Confusing $finish with $stop
- Ignoring simulation stop effect
initial begin
$display("Test started");
$finish
$display("Test ended");
endSolution
Step 1: Check syntax of system tasks
System tasks like$finishrequire a semicolon at the end.Step 2: Identify missing semicolon
The code misses a semicolon after$finish, causing a syntax error.Final Answer:
Missing semicolon after $finish -> Option CQuick Check:
Always end $finish with semicolon [OK]
- Omitting semicolon after $finish
- Thinking $finish can't be in initial block
- Misusing $display syntax
$finish?Solution
Step 1: Understand simulation flow for multiple tests
Simulation should continue running until all tests complete to verify all outputs.Step 2: Place
Calling$finishafter last test$finishafter the last test ends simulation cleanly once all tests are done.Final Answer:
Call $finish at the end of the last test -> Option AQuick Check:
$finish after all tests = clean end [OK]
- Stopping simulation too early with $finish
- Calling $finish multiple times unnecessarily
- Avoiding $finish and letting simulation run forever
Ready to prove it?
Pass the test and this pattern moves unknown
