Skip to content

Add Verilator simulation backend - #57

Draft
FrancescoConti wants to merge 4 commits into
mainfrom
fc/verilator
Draft

Add Verilator simulation backend#57
FrancescoConti wants to merge 4 commits into
mainfrom
fc/verilator

Conversation

@FrancescoConti

@FrancescoConti FrancescoConti commented Aug 10, 2026

Copy link
Copy Markdown
Member

Depends on pulp-platform/MAGIA#98, which should be merged first.

Add Verilator as a simulation backend

Adds verilator alongside rtl and gvsoc as a simulation platform, covering both building the model and running tests on it.
The verilator flow provides an intermediate step between a commercial RTL flow and the non-cycle-accurate gvsoc flow in terms of speed, signal visibility, and supported features.

Usage

The flow is similar to the existing rtl flow.

make MAGIA platform=verilator tiles=4        # verilate the mesh (few minutes)
make build test=test_helloworld tiles=4
make run  test=test_helloworld platform=verilator tiles=4

make MAGIA platform=verilator applies exactly the same parameter edits to the RTL as the commercial build (num_cores, core, N_TILES_X/Y, FSYNC_STALL) and shares the whole environment bootstrap; only the final build command differs — verilate instead of build-hw.

New options: verilator_jobs (16), verilator_threads (4), MAGIA_VERILATOR_BIN (model path). make rtl-clean platform=verilator runs clean-verilate. platform defaults to rtl for MAGIA and rtl-clean, so existing invocations are unchanged; make run still requires it explicitly. By default, the verilator model does not trace waveforms; it does if gui=1 is passed, saving them in FST format.

Requires Verilator >= 5.046 and a MAGIA checkout containing verilator/verilator.mk.

Runtime comparison

test_helloworld, 4x4 mesh, target_platform=magia_v3, on a Intel Xeon Platinum 8480+.

Platform Speedup vs rtl
rtl 1.0x
verilator (1 thread, gui=0) ~2.1x
verilator (4 threads, gui=0) ~3.9x
verilator (4 threads, gui=1) ~1.0x
gvsoc ~48x

Note: these results are heavily dependent on the specific configuration and test, and are provided here only to frame the relative speed of the various flows.

Notes for review

make run invokes Vmagia_tb directly rather than going through the MAGIA repo's make verilate-run. That target declares verilate-run: all, and all reaches $(BIN): $(CRT) $(OBJ), so it would try to compile sw/tests/<test>.c inside the RTL repo — a file that does not exist for SDK-injected tests.

Stimuli generation replaced. parse_s19.pl | s19tomem.py becomes a single Rust pass over the SREC (scripts/s19tomem.rs), dropping the intermediate verif.txt. Built on demand with rustc into build/tools/s19tomem. Both backends pick it up — they share the rtl_stimuli target, which this PR also factors out of the rtl branch. Output is byte-identical to the old pipeline. This is not specifically related to Verilator, but I added it here because the old Python pipeline was very slow and this costs basically nothing.

make MAGIA rejects tiles=1/mesh_dv=0 (the flow is mesh-only) and target_platform=magia_v1 (selects CV32E40X, whose hierarchical core traces would need per-tile filenames resolved at run time), with an explanatory error rather than a failure later in the build.

No CI changes in this PR.

make run platform=verilator runs a prebuilt Vmagia_tb from the MAGIA
repo. Stimuli generation is factored out of the rtl branch into a
shared rtl_stimuli target; both backends drive the same testbench with
the same plusargs.

The model is not built here. MAGIA_VERILATOR_BIN must point at one
built with `make verilate` in the MAGIA repo, and since the mesh size
is compiled into the model, tiles= has to match it.

Verilator prints no "Errors:" summary and exits 0 when it runs out of
events without reaching $finish, so sim_ret_errors.sh does not apply.
Add sim_ret_errors_verilator.sh, keyed on the testbench's
end-of-simulation line.

Also fixes the hardcoded ./build/bin path in the stimuli step, which
broke whenever CMAKE_BUILDDIR was overridden.
@FrancescoConti FrancescoConti self-assigned this Aug 10, 2026
make MAGIA platform=verilator verilates the mesh instead of building the
QuestaSim libraries. Parameter seds, environment bootstrap and the rest
of the chain are shared with the rtl path; only the final build command
differs. tiles= now reparameterizes the model, so the previous "model is
fixed at 4x4" caveat is gone.

platform defaults to rtl for MAGIA and rtl-clean, leaving existing
invocations and CI unchanged. run still requires it explicitly.

Reject up front what verilator.mk rejects later: tiles=1 or mesh_dv=0
(the flow is mesh-only) and magia_v1 (selects CV32E40X).

rtl-clean platform=verilator runs clean-verilate. Note hw-clean-all
deletes .bender, which invalidates a verilator model built from the same
checkout.
Replaces the parse_s19.pl | s19tomem.py pipeline with a single Rust pass
over the SREC, dropping the intermediate verif.txt. Both rtl and
verilator pick it up: they already share rtl_stimuli. Built on demand
with rustc into build/tools/s19tomem; one file, so no cargo project.

Vendored from the MAGIA repo, where it is not committed and absent from
the pinned commit. That copy ports MAGIA's s19tomem.py, which uses
DATA_SIZE=0x30000; the SDK's used 0x60000, kept here so the data window
does not shrink and drop everything above 0xcc04_0000.

Output is byte-identical to the old pipeline on test_helloworld,
trailing-newline asymmetry included.
@FrancescoConti FrancescoConti changed the title Add verilator simulation backend Add Verilator simulation backend Aug 10, 2026
@FrancescoConti

Copy link
Copy Markdown
Member Author

Left in draft to avoid merging accidentally before pulp-platform/MAGIA#98

Drops verilator_fst: gui already means "let me look at the waveform", and
for verilator that is an FST rather than an interactive window. The dump
goes to <test>.fst in the test dir, overridable via VERILATOR_FST, and
the run prints gtkwave and surfer commands for it when it ends.

fast_sim stays QuestaSim-only.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant