Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Chiselsim] Exit simulation on $finish #4700

Open
seldridge opened this issue Feb 16, 2025 · 0 comments · Fixed by #4702
Open

[Chiselsim] Exit simulation on $finish #4700

seldridge opened this issue Feb 16, 2025 · 0 comments · Fixed by #4702
Assignees
Labels

Comments

@seldridge
Copy link
Member

Currently, Chiselsim will only terminate if the simulator exits or the commands that it is trying to run are done. However, different simulators handle things like $finish (which the Chisel stop API lowers to) differently. E.g., consider the following example code:

module Foo();

initial begin
  $display("hello");
  $finish;
  $display("world");
  $finish;
end

endmodule

Compiling this with Verilator (verilator --binary Foo.sv && ./obj_dir/VFoo) will produce:

hello
- Foo.sv:5: Verilog $finish
world
- Foo.sv:7: Verilog $finish

If you run this with VCS (vcs Foo.sv && ./simv) will produce:

hello
$finish called from file "Foo.sv", line 5.
$finish at simulation time                    0

The problem here is that Verilator doesn't terminate. This seems to violate the Verilog spec, based on the snippet below:

Image

That said, we can work around this by querying the VerilatedContext.getFinish() member function when in the run_simulation function (or elsewhere) to see if Verilator is reporting that the simulation is done.

This has two major benefits:

  1. Simulations will actually exit instead of running until they hit a timeout. For ChiselSpec, this is currently 10k half-periods. We can exit simulations way earlier without wasting cycles.
  2. We are hacking around this by post-processing the log looking for finish before an assert. All that checking logic can just go away if simulations actually terminate.
@seldridge seldridge added the bug label Feb 16, 2025
@seldridge seldridge self-assigned this Feb 16, 2025
seldridge added a commit that referenced this issue Feb 18, 2025
Fix a bug in how Verilator simulations work in svsim where the simulation
would _not_ terminate when a `$finish` was called.  This seems to be a bug
in Verilator where it will not terminate the simulation (as mandated by
the Verilog spec) unless you are running with time delays.

Fixes #4700.

Signed-off-by: Schuyler Eldridge <schuyler.eldridge@sifive.com>
seldridge added a commit that referenced this issue Feb 19, 2025
Fix a bug in how Verilator simulations work in svsim where the simulation
would _not_ terminate when a `$finish` was called.  This seems to be a bug
in Verilator where it will not terminate the simulation (as mandated by
the Verilog spec) unless you are running with time delays.

Fixes #4700.

Signed-off-by: Schuyler Eldridge <schuyler.eldridge@sifive.com>
@seldridge seldridge reopened this Feb 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant