Metadata-Version: 2.4
Name: vunit-yavs
Version: 0.21.2
Summary: The yavs VHDL simulator for VUnit, as a VUnit package
License-Expression: MPL-2.0
Project-URL: Source, https://github.com/ru551n/yavs/tree/main/python/vunit-yavs
Keywords: vunitpkg
Requires-Python: >=3.11
Description-Content-Type: text/markdown
Requires-Dist: vunit_hdl>=5.0.0.dev16

# vunit-yavs

The yavs simulator for VUnit, as a VUnit package. Add it in the run script before anything else,
then select yavs with `VUNIT_SIMULATOR=yavs`:

```python
from vunit import VUnit

vu = VUnit.from_argv()
vu.add_package("vunit-yavs", allow_setup=True)  # first: it provides the simulator
vu.add_vhdl_builtins()
vu.add_library("lib").add_source_files("*.vhd")
vu.main()
```

```sh
pip install vunit-yavs
VUNIT_SIMULATOR=yavs python run.py
```

The [guide](https://yavs.readthedocs.io/en/latest/vunit.html) goes from installing to running, debugging and
coverage, and the [debugger tutorial](https://yavs.readthedocs.io/en/latest/debugger-tutorial.html) debugs a failing test with `--yavs-debug`, `--yavs-surfer` and `--yavs-interactive`. `yavs` is found on `PATH`, or in the directory named by `VUNIT_YAVS_PATH`. It needs VUnit 5.0.0.dev16
or later (`pip install vunit-yavs` installs it, a pre-release) and yavs 0.21.2 or later.

## Options

| Sim or compile option | Passed to |
|---|---|
| `yavs.global_flags` | `yavs analyze`, `run` and `elab` |
| `yavs.a_flags` | `yavs analyze` |
| `yavs.elab_flags` | `yavs run` and `elab` |
| `yavs.sim_flags` | `yavs run`, such as `--assert-summary`, `--activity`, `--hang-report`, `--wave-include`: they pass through to the run |
| `yavs.max_threads` (integer, at least 1, or `"auto"`) | `yavs run --threads=N`, the most threads of one simulation; `"auto"` is `--threads=auto`, more threads only while they make the run faster |
| `yavs.tier_compile` (`"wait"` or `"background"`) | `yavs run --tier-compile=MODE`: `wait` (yavs's default) makes a simulation wait while a function compiles, `background` compiles alongside it on a thread at the lowest system priority |
| `yavs.cycle` (`"off"`, `"on"` or `"strict"`) | `yavs run --cycle` for `"on"`, `--cycle-strict` for `"strict"` (cycle mode, below); default `"off"`. Not passed to `elab` |
| `enable_coverage` (VUnit's option) | `yavs run --cov-file=<test output>/yavs/coverage.ycov --cov-test=<test name>` with the kinds below (coverage, below) |
| `yavs.cov_kind` (list of strings) | `yavs run --cov-kind=LIST`: `statement`, `branch`, `toggle`, `condition`, `assert`, `expression`, `combination`, `fsm`. No kind is on by default |
| `yavs.init_files.after_load` | Console command files sourced after each `vunit_load` |
| `yavs.init_files.before_run` | Console command files sourced before each `vunit_run` |
| `yavs.init_file.gui` | A console command file sourced by `vunit_user_init` and at the start of `--yavs-interactive` |

| Command line | What it does |
|---|---|
| `--yavs-debug[=PORT]` | Runs the selected test under `yavs run --dap=PORT` (default 4711, `0` picks a free port) and prints where to attach; write the port as `--yavs-debug=PORT` |
| `--yavs-interactive` | Runs the selected test at the yavs console: a `yavs>` prompt at time 0 (below). Not with `--yavs-debug`: one of them drives the run |
| `--yavs-surfer[=HOST:PORT]` | Shows the waves of the one selected test in Surfer 0.6.0 or later while it runs (`yavs run --surfer`); strictly opt-in. Combines with `--yavs-debug` and `--yavs-interactive`, where the cursor follows the stops |
| `-j N`, `--jobs N` | Compiles with up to N `yavs analyze` calls at once, into different libraries (below); a bare `-j` is one per CPU (put test names before it). Without it, one call at a time. Independent of `-p` |
| `--yavs-max-threads N` | `yavs.max_threads` (a number or `auto`) for every test that does not set it (environment variable `VUNIT_YAVS_MAX_THREADS` for run scripts with a command line of their own) |
| `--yavs-lint` | After compiling, runs `yavs lint` on every library of the project that is not external; a finding of severity error fails the compile step. To leave out VUnit's own libraries, put `exclude: ['**/vunit/vhdl/**']` in `yavs-lint.yaml`. `file_list` in that file is not read: the project says which files a library has. An unknown key, rule or rule group in that file or in the waiver file is error E0134 (told once, before any library is checked) and fails the compile step |
| `--yavs-cov=LETTERS` | Collects coverage in every simulation, as `enable_coverage` does for the tests it is set for, of the kinds the letters name: `s` statement, `b` branch, `c` condition, `e` expression, `t` toggle, `f` fsm, `a` assert, `m` combination (`--yavs-cov=sbt`). No kind is on by default; coverage without a kind fails the simulation, naming this flag, `--yavs-cov-kind` and `yavs.cov_kind`. Write the letters with `=` |
| `--yavs-cov-directives MODE` | `apply` or `ignore`: what `yavs analyze`, `elab` and `run` do with the `-- yavs coverage exclude` comments in the sources (`--coverage-directives`); without it yavs's default, `apply`, which checks them when compiling |
| `--yavs-cov-html DIR` | `results.merge_coverage()` also writes the HTML coverage report into DIR |
| `--yavs-cov-fail-under PCT` | `results.merge_coverage()` prints the coverage report and the run exits with status 1 when the total is below PCT percent; per kind with `statement:90,branch:80` |
| `--yavs-cov-cobertura FILE`, `--yavs-cov-json FILE`, `--yavs-cov-lcov FILE`, `--yavs-cov-sonarqube FILE` | `results.merge_coverage()` also writes that export; in a script `merge_coverage("coverage", args=["--lcov", "c.info"])` and so on |
| `--yavs-cov-embed-sources` | Passes `--cov-embed-sources`, as the sim option `yavs.cov_embed_sources` does for the tests it is set for: the HTML report then needs no checkout, and the source text travels with the database |
| `--yavs-cov-below PCT` | The report of `results.merge_coverage()` limits its table of instances to those below PCT percent, worst first |
| `--yavs-cov-detailed` | That report also lists what never ran, under each instance. In a script both go through `merge_coverage("coverage", args=["--below=100", "--detailed"])` |
| `--yavs-cov-exclude FILE` | `results.merge_coverage()` reports without the items this exclusion file lists (`yavs cov report --exclude`); repeatable. A run script passes the same in `merge_coverage("coverage", args=["--exclude", file])` |
| `--yavs-cov-config FILE` | `results.merge_coverage()` reads exclusion files, root and kinds from this file (`yavs cov report --config`). A run script passes `args=["--config", file]` |
| `--yavs-no-jobserver` | Runs without the token server (below), which is on by default (environment variable `VUNIT_YAVS_JOBSERVER=off` for run scripts with a command line of their own) |
| `--yavs-jobserver` | The token server, the default; accepted for scripts written when it was not |

Compiling (the dependency-graph batching and `-j` need a release newer than 0.19.1): files go to `yavs analyze` in the project's dependency order, the out-of-date ones only. A call takes a ready
file and the later files of its library, standard and flags whose dependencies are done or in the call (always on); a
failing call is repeated file by file to name the failing file, and what depends on it is skipped, as in VUnit. With `-j N`
up to N calls run at once, never two into one library (yavs locks a library for a whole call), nor, with yavs 0.19.1
or older, into libraries that read each other (they could wait for each other's lock, yavs #1150), so a project of many libraries gains most. Output is printed per finished call. With a VUnit that has `--batch-compile` and `--parallel-compile`
(5.0.0.dev16 does not have them), those flags hand the compile to VUnit instead, and `-j` has no effect.

With the token server (the default) every simulation runs with `--threads=auto` unless `yavs.max_threads` or
`--yavs-max-threads` gives a number. Without the server and without those, no thread count is passed, and yavs runs
with its default, one thread. An explicit `--threads` in `yavs.sim_flags` comes after the cap and wins.

## Cycle mode

Cycle mode is experimental: the option, its values and what yavs gives up under it may change in a later release.

```python
vu.set_sim_option("yavs.cycle", "on")                                       # every test
vu.library("lib").test_bench("tb_counter").set_sim_option("yavs.cycle", "strict")  # one testbench; also per test or configuration
```

`"on"` runs the clocked and combinational processes that fit cycle mode on a static schedule and leaves the rest to the
event kernel; the test output shows `cycle mode: N of M processes run cycle-based` (VUnit prints a passing test's output with `-v`). `"strict"` makes a process below
the top-level unit that does not fit an error. A wrong value is refused at setup, naming the three values.

- Cycle-based processes drop their `report` and `assert` statements, and library warnings.
- They are two-state: metavalues read as `'0'`. Outside processes keep full semantics.
- There are no waves, and none of coverage, `--yavs-debug`, `--yavs-interactive` or `--yavs-surfer` in the same test (the test fails before yavs starts, error E4102 of yavs).

All the rules are in the `--cycle` entry of the yavs command line reference. To see what would join, set `yavs.sim_flags` to `["--cycle-report"]` for one test with `yavs.cycle` `"off"`: yavs prints the report and does not simulate, so that test fails. `yavs.sim_flags` `["--cycle-exact-meta"]` with `yavs.cycle` `"on"` or `"strict"` keeps the exact metavalue rules for a test, and the values in the message of a failed check.

## Coverage

```python
vu.set_sim_option("enable_coverage", True)      # or run with --yavs-cov=sb
vu.set_sim_option("yavs.cov_kind", ["statement", "branch"])   # the kinds; none is on by default
vu.main(post_run=lambda results: results.merge_coverage("coverage"))
```

Each simulation writes a yavs coverage database named after its test (`yavs run --cov-file`). The kinds come from the `yavs.cov_kind` sim option, else `--yavs-cov-kind`, else the letters of `--yavs-cov=LETTERS`; the letters and `--yavs-cov-kind` are not given together. `results.merge_coverage("coverage")`
merges them into `coverage.ycov` (`yavs cov merge`); with `--yavs-cov-html DIR` or
`--yavs-cov-fail-under PCT`, or arguments in `merge_coverage("coverage", args=["--cobertura", "c.xml"])`, it
then runs `yavs cov report` on the merged database. A total below the threshold makes the run exit with status 1
after VUnit has written its reports (a JUnit file included). For a CI gate:

```sh
VUNIT_SIMULATOR=yavs python run.py --yavs-cov=sbt --yavs-cov-html cov-html --yavs-cov-fail-under 85
```

Coverage cannot be combined with `--yavs-debug`. Reports, exclusions and the numbers are in the yavs
documentation page on coverage. The coverage options need the yavs release that has `yavs cov report --fail-under`.

## Native libraries of VUnit packages

A VUnit package that loads a native library can ask the simulator interface how: `YavsInterface`
has `foreign_language_interface = "VHPIDIRECT"` (the VHDL calls the library through foreign
subprograms with a `"VHPIDIRECT ..."` attribute) and `native_library_hooks(library_file)`, which
returns the simulator hooks that pass `--load=<library_file>` to every `yavs elab` and `yavs run` (so `--elaborate` works for a design that calls the library while it is elaborated); `native_library_hooks(library_file, args="fat-pointer")`
selects that argument layout for the library (a `run.py` without a package gives `--load=LIB:args=fat-pointer` in `yavs.elab_flags`). A package registers them with
`context.register_simulator_hooks("yavs", **YavsInterface.native_library_hooks(library))`. The repository's
`tests/project_foreign` is a package that builds a C library, with Python embedded where the host allows it, and a
testbench that calls Python from VHDL. A VHDL string reaches the library as one byte per character, and yavs reads a
UTF-8 source file as Latin-1 characters: give `--encoding=latin1` to `yavs.a_flags` and (as a sim option) to
`yavs.global_flags` when the library needs the UTF-8 bytes of an accented literal.

## Debugging one test

```sh
VUNIT_SIMULATOR=yavs python run.py lib.tb.test_name --yavs-debug
```

yavs waits for a Debug Adapter Protocol client on `127.0.0.1:4711` before the simulation starts;
press F5 or run "yavs: Attach" in VS Code with the yavs extension: no launch configuration is needed. Set breakpoints
in your own files: with location or check preprocessing on, the debugger maps the rewritten copies to them. When the
session ends, the test is reported as usual.

`--yavs-debug` runs one simulation. A second simulation of the run (a second test, or the same
test in another configuration) is an error before yavs starts, naming both tests. `--yavs-debug`
together with `--elaborate` or `-p` above 1 is an error at the start. A bare
`--yavs-debug` reads a following test name as its port: put test names first, or write
`--yavs-debug=4711`.

Every simulation (not `--yavs-interactive`) is started with `--exit-on-stdin-close`: it ends when the pipe the
runner keeps to its standard input closes, so killing only `python run.py`, even with SIGKILL, does not leave yavs
waiting for a debugger on the port. The waves and coverage files of such a run are not finished.

## Token server

By default (Linux and Windows), at any `-p`, the run script starts a token server on 127.0.0.1 with one slot for
each CPU the run may use (its CPU affinity, so `taskset` counts; none with one CPU, nor under `--yavs-debug` or
`--yavs-interactive`). Every simulation runs with `--threads=auto` (unless `yavs.max_threads` or
`--yavs-max-threads` says otherwise) and gets `YAVS_TOKENS=<port>:<secret>`, a secret of its own; yavs runs its
extra threads only on tokens the server lends, and asks for them only while it has work for them (a group of
processes worth sharing, or a cycle region to compile). Each running test keeps one slot, and so does each of the
`-p N` workers for `GRACE` (1 second) after its test ends, and all N until the first test starts. Measured under
the test runner (2026-11-17, 336 test starts of `-p 4`), a worker started its next test 2 ms after its last one
ended in the median and 0.33 s at most; the grace is three times that. The other slots are lent: breadth first
(every test gets one extra before any gets a second) and newest test first within a round, at most
`yavs.max_threads - 1` per test (`min(C, 8) - 1` with `auto`, for `C` slots); `yavs.max_threads` set to 1 takes
none. So a worker's next test finds its slot free, and in the tail of a regression the slots of the workers out
of tests are lent a second after they ran out. With `auto` a simulation whose threads do not make it faster asks
for no tokens after its first fraction of a second. A token in use is never taken back: a worker that idles past
the grace and then starts a test again shares its slot until a simulation lowers its want. A simulation gives its
tokens back when it stops using them and when it ends in any way, also when it is killed. If the server cannot
start (a blocked local socket) the run warns and every simulation uses one thread.

`--yavs-no-jobserver` (or `VUNIT_YAVS_JOBSERVER=off`) runs without it: then `--yavs-max-threads N` gives every
simulation N threads of its own, whatever the others use. Under the server the threads of `--yavs-max-threads N`
past the first run only on lent tokens.

## The console

```sh
VUNIT_SIMULATOR=yavs python run.py lib.tb.test_name --yavs-interactive
```

The test stops at time 0 at a `yavs>` prompt. All commands of the yavs console work (`run`, `step`,
`examine`, `force`, `break`, `restart`, `source`, `help`, `quit`), and this package adds VUnit's own,
which behave as in VUnit's other simulator interfaces. They are also in the Debug Console under
`--yavs-debug`.

| Command | What it does | Fails (an error at the console, the end of a script) when |
|---|---|---|
| `vunit_help` | Lists these commands | |
| `vunit_load [flags]` | Loads the test at time 0, with extra `yavs run` flags if given, and sources `yavs.init_files.after_load` | The design does not elaborate, a flag is unknown or an init file fails |
| `vunit_user_init` | Sources `yavs.init_file.gui` | The file fails |
| `vunit_run` | Sources `yavs.init_files.before_run`, runs, and prints `Test Run Passed!` | The test suite did not finish, also when a breakpoint stops the run: `Test Run Failed!` |
| `vunit_compile` | Runs `run.py --compile` with your arguments, without `--clean` | The compile fails |
| `vunit_restart` | `vunit_compile`, and if that worked: the new design at time 0, breakpoints kept, and `vunit_run` | As `vunit_compile`, then `vunit_run` |

The yavs `restart` goes back to time 0 with the design as loaded; `vunit_restart` compiles first. Init files are
files of console commands; `$vunit_tb_path` and `$vunit_tb_name` are replaced as VUnit defines them. When you `quit`,
the test is reported as usual. A session that compiled at `vunit_compile` and failed keeps the old design loaded.

## Licence

Mozilla Public License 2.0 (https://mozilla.org/MPL/2.0/), see the header of each file.
