Metadata-Version: 2.4
Name: digsim-native
Version: 0.6.0
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Rust
License-File: LICENSE.md
Summary: Optional native simulation engine for DigSim
Author-email: Fredrik Andersson <freand@gmail.com>
Requires-Python: >=3.11
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: digsim, https://github.com/freand76/digsim
Project-URL: homepage, https://github.com/freand76/digsim-native

# digsim-native

The native simulation engine of [DigSim](https://github.com/freand76/digsim), in Rust
(PyO3). DigSim uses it when it can import `digsim_native`, and simulates in Python when
it can't. Both engines give the same results (the settled values, the native engine has no
delays inside a Yosys component), the native one 45 to 100 times faster on Yosys netlists.

## Install

The wheels are on [PyPI](https://pypi.org/project/digsim-native/) and the
[releases](https://github.com/freand76/digsim-native/releases), one per platform for every
Python from 3.11: Linux x86_64 and aarch64 (glibc and musl), Windows x64 and arm64, macOS
arm64 and x86_64.

```sh
pip install digsim-native
DIGSIM_ENGINE=python python ...     # the Python engine, with the package installed
```

## What it does

Rust runs the event loop and simulates every Yosys gate and functional cell, GND, VDD,
the bus ports up to 64 bits and one-bit ports with several drivers. Every other component
stays in Python and is called from the same event queue. When the wires change during a
simulation, the engine hands its state back to Python until the next `init()`.

A Yosys component is one compiled block (`src/block.rs`), simulated like a compiled
simulator (Verilator) runs its logic: the cell outputs are slots of words (the bits and
the undefined bits), the combinational cells are evaluated in the order of their ranks,
once each when a bit they read changed, and the flip-flops update their outputs in the
next round, like non-blocking assignments. The common cells are computed on 64-bit
words (the others by the cells of `src/word.rs`, so the values are the same), the gates by
a table, and a block of gates evaluates all its gates in a sweep when most of them
changed. Inside a block there are no delays: its outputs change 1 ns after an input. A
netlist with a combinational loop, tristate buffers or inout ports keeps one native cell
for each of its cells, with events, and so do the native cells outside blocks (with
levels instead of delays between them, see `src/levels.rs`). With `levels=False`
(`DIGSIM_BLOCKS=0` in DigSim) every cell has its delay and events, exactly like in Python.

`Engine.deposit_port()`, `deposit_word()` and `deposit_block()` are the deposits of DigSim
(`YosysComponent.deposit()`, like Verilog's `vpi_put_value()`): a cell output, or a slot
of a block, has a value now, and the cell keeps it until it drives again, a flip-flop at
its next active edge, a combinational cell when an input changes. A block with a deposit
on a gate evaluates only its dirty gates from then on, without sweeps.

`VcdWriter` (`src/vcd.rs`) writes the VCD files of DigSim, the same text as pyvcd. The
engine writes the changes of its ports to it without calling Python, a port's variables
next to each other in the writer.

`cargo build --release --features prof` adds time counters (`Engine.profile()`) and
`Engine.block_replay()`, which times the calls of a block again without Python.

## Build

```sh
pip install maturin
maturin build --release              # the wheel, in target/wheels/
pip install target/wheels/*.whl      # into the environment of DigSim
cargo fmt --check && cargo clippy --release --all-targets -- -D warnings && cargo test --release
```

`tests/` has the tests that don't need DigSim (`pip install <wheel> pyvcd==0.5.0 pytest`,
`pytest tests`), the CI runs them on every push. The tests of the engine are in DigSim
(its pytest, and `tools/native/`): `sh tools/native/test_local_native.sh` in DigSim builds
this folder (next to it) and runs them, before a push. The CI doesn't run DigSim, so a
push never depends on it.

A new version works with the DigSim that pins the version before it, and DigSim works
with both (it checks `INTERFACE`): either can be pushed first. A change that DigSim must
follow keeps the old interface until DigSim's pin has passed it.

## Releases

1. Bump `version` in `Cargo.toml` and `pyproject.toml`.
2. Push a tag `vX.Y.Z`. The `Wheels` workflow builds and tests the wheels and attaches
   them to a release.
3. To upload them to PyPI: `gh workflow run pypi.yaml -f tag=vX.Y.Z`. PyPI trusts the
   workflow (Trusted Publishing: publisher `freand76/digsim-native`, workflow
   `pypi.yaml`, environment `pypi`), so it needs no token.
4. Bump the pin in DigSim.

The interface to DigSim isn't stable, so DigSim pins the version it was written for.

