diff --git a/README.md b/README.md index 2212f5f..c11c87a 100644 --- a/README.md +++ b/README.md @@ -1 +1,107 @@ # dlv + +Turn a Verilog module into an importable Python module — via [Verilator](https://www.veripool.org/verilator/) and [nanobind](https://github.com/wjakob/nanobind). + +```console +$ dlv build counter.v --clock clk -o out/ +✓ Checking verilator +✓ Reading ports +✓ Generating binding +✓ Running verilator 5.045 +✓ Compiling extension +``` + +## Requirements + +- Python ≥ 3.10 +- `verilator` ≥ 5.022 in `PATH` (ports are read with `verilator --json-only`) +- `g++` (C++17) in `PATH` + +## Install + +```console +pip install dlv # or: uv tool install dlv +``` + +## Quickstart + +`examples/counter.v`: + +```verilog +module counter #(parameter WIDTH = 8) ( + input wire clk, + input wire rst, + input wire en, + output reg [WIDTH-1:0] q +); + always @(posedge clk) begin + if (rst) q <= 0; + else if (en) q <= q + 1; + end +endmodule +``` + +```console +$ dlv build examples/counter.v --clock clk -o out/ +$ cd out && python +``` + +```python +>>> import counter, numpy as np +>>> counter.proc_ris({"rst": 1, "en": 0}) # reset, one clock +>>> counter.proc_ris({"rst": 0, "en": 1}) # count, one clock +>>> counter.cycle_ris(4) # four more clocks +>>> counter.get() +{'q': 5} +>>> counter.step(3, {"en": np.ones(3)}) # clock loop in C++ +{'q': array([6, 7, 8], dtype=uint8)} +``` + +## `dlv build` + +``` +dlv build FILE.v --clock PORT [-o DIR] [--module M] [--trace] [-v] +``` + +| Option | Meaning | +| --- | --- | +| `--clock PORT` | Master clock, required. Must be a 1-bit input. | +| `-o DIR` | Output directory (default: current directory). Result: `DIR/.cpython-3xx-….so` | +| `--module M` | Top module. Default: the file name without `.v` — if no module has that name, `--module` is required. | +| `--trace` | Enable FST tracing (`trace_open` / `trace_close`). | +| `-v` | Show Verilator/compiler output (otherwise only shown on failure). | + +Build files live in `.dlv/build//` in the current directory. Ports wider than 64 bits and `inout` ports are not supported (v0.1). + +## API of the generated module + +The module name is the Verilog module name. `Model()` creates an independent simulation instance; the module-level functions work on a default instance (created on first use) and have the same names. + +| Call | Meaning | +| --- | --- | +| `info()` | `{name, inputs: {port: width}, outputs: {port: width}, clock}` | +| `proc_ris(inputs={})` | Set inputs, then one clock period starting with a rising edge | +| `proc_fal(inputs={})` | Same, starting with a falling edge | +| `cycle_ris(n=1)` / `cycle_fal(n=1)` | `n` more clock periods | +| `get()` | `{output: int}` for all outputs | +| `step(n, inputs={})` | `n` rising-edge clock periods in a C++ loop. `inputs` maps names to array-likes of length `n` (or length 1 = constant). Returns `{output: numpy array}` (uint8/16/32/64 by width), sampled after each period. | +| `trace_open(path)` / `trace_close()` | Only with `--trace`: write an FST waveform. Call `trace_close()` to flush. | + +Ports up to 64 bits are Python `int`s. The clock cannot be set via `inputs`. Don't mix `_ris` and `_fal` calls on the same instance unless you accept an extra clock edge when switching. + +## Shell completion + +```console +dlv init [--shell bash|zsh|fish] +``` + +Installs argcomplete-based completion to `~/.local/share/bash-completion/completions/dlv`, `~/.local/share/zsh/site-functions/_dlv` or `~/.config/fish/completions/dlv.fish` (zsh: the directory must be in `fpath`). `--module` completes module names from `FILE.v`, `--clock` completes its 1-bit inputs. + +## Development + +```console +uv run pytest # tests needing verilator are skipped without it +uv build +``` + +MIT licensed. diff --git a/SPEC.md b/SPEC.md index 962e394..fd2ef5d 100644 --- a/SPEC.md +++ b/SPEC.md @@ -3,52 +3,52 @@ Verilog-Modul → per Verilator + nanobind → importierbares Python-Modul. ## CLI -- [ ] `pip install dlv`, Einstieg `dlv` (console_scripts) -- [ ] argparse + argcomplete, schwere Imports erst nach autocomplete() -- [ ] `dlv init` → bash/zsh/fish-Completion nach ~/.local/share/… installieren -- [ ] `dlv build FILE.v --clock PORT [-o DIR] [--module M] [--trace] [-v]` - - [ ] Modulname ≠ Dateiname und kein --module → Fehler - - [ ] --clock ist Pflicht (v0.1: Module ohne Master-Clock unzulässig) - - [ ] --clock muss 1-Bit-Input sein, sonst Fehler - - [ ] -o DIR (Default: cwd), Ergebnis: DIR/counter.cpython-3xx-….so -- [ ] Completion: --module → Module aus FILE, --clock → 1-Bit-Inputs +- [x] `pip install dlv`, Einstieg `dlv` (console_scripts) +- [x] argparse + argcomplete, schwere Imports erst nach autocomplete() +- [x] `dlv init` → bash/zsh/fish-Completion nach ~/.local/share/… installieren +- [x] `dlv build FILE.v --clock PORT [-o DIR] [--module M] [--trace] [-v]` + - [x] Modulname ≠ Dateiname und kein --module → Fehler + - [x] --clock ist Pflicht (v0.1: Module ohne Master-Clock unzulässig) + - [x] --clock muss 1-Bit-Input sein, sonst Fehler + - [x] -o DIR (Default: cwd), Ergebnis: DIR/counter.cpython-3xx-….so +- [x] Completion: --module → Module aus FILE, --clock → 1-Bit-Inputs ## Build-Ausgabe -- [ ] Verilator/Compiler via subprocess, Output wird gepuffert -- [ ] Output nur im Fehlerfall ausgeben (oder mit -v) -- [ ] Hübscher Fortschritt mit `rich`: Schritte mit Spinner → ✓ / ✗ +- [x] Verilator/Compiler via subprocess, Output wird gepuffert +- [x] Output nur im Fehlerfall ausgeben (oder mit -v) +- [x] Hübscher Fortschritt mit `rich`: Schritte mit Spinner → ✓ / ✗ (Verilator prüfen, Ports lesen, Binding generieren, verilieren, kompilieren) ## Mini-Verilog-Parser (nur Completion/Vorprüfung) -- [ ] Regex-Tokenizer + kleiner Automat, Kommentare/Strings/`ifdef grob ignorieren -- [ ] ANSI- und Nicht-ANSI-Ports, Breite nur als Text/Best Effort -- [ ] Cache nach (pfad, mtime, größe) in .dlv/ +- [x] Regex-Tokenizer + kleiner Automat, Kommentare/Strings/`ifdef grob ignorieren +- [x] ANSI- und Nicht-ANSI-Ports, Breite nur als Text/Best Effort +- [x] Cache nach (pfad, mtime, größe) in .dlv/ ## Build-Kette (Quelle der Wahrheit = Verilator) -- [ ] `verilator` aus PATH, Version ≥ 5.022 prüfen -- [ ] `verilator --json-only` → Ports (Name, Richtung, Breite) -- [ ] Ports > 64 Bit → klarer Fehler (v0.1) -- [ ] Jinja2-Template → nanobind-Binding (C++) -- [ ] `verilator --cc --build` + g++ direkt mit nanobind-Flags → .so +- [x] `verilator` aus PATH, Version ≥ 5.022 prüfen +- [x] `verilator --json-only` → Ports (Name, Richtung, Breite) +- [x] Ports > 64 Bit → klarer Fehler (v0.1) +- [x] Jinja2-Template → nanobind-Binding (C++) +- [x] `verilator --cc --build` + g++ direkt mit nanobind-Flags → .so ## Binding-API -- [ ] `counter.Model()` für mehrere Instanzen -- [ ] Modul-Funktionen auf Default-Instanz: info, proc_ris, proc_fal, cycle_ris, cycle_fal, get, step -- [ ] info() → {name, inputs:{p:w}, outputs:{p:w}, clock} -- [ ] proc_ris(dict) → Inputs setzen, 1 Takt mit führender steigender Flanke -- [ ] cycle_ris(n) → n weitere Takte; _fal-Varianten analog -- [ ] get() → dict aller Outputs -- [ ] step(n, {in: np.array}) → {out: np.array}, Taktschleife in C++ -- [ ] --trace: m.trace_open("x.fst") / m.trace_close() +- [x] `counter.Model()` für mehrere Instanzen +- [x] Modul-Funktionen auf Default-Instanz: info, proc_ris, proc_fal, cycle_ris, cycle_fal, get, step +- [x] info() → {name, inputs:{p:w}, outputs:{p:w}, clock} +- [x] proc_ris(dict) → Inputs setzen, 1 Takt mit führender steigender Flanke +- [x] cycle_ris(n) → n weitere Takte; _fal-Varianten analog +- [x] get() → dict aller Outputs +- [x] step(n, {in: np.array}) → {out: np.array}, Taktschleife in C++ +- [x] --trace: m.trace_open("x.fst") / m.trace_close() ## Doku -- [ ] README.md: Installation, Voraussetzungen, Quickstart (counter.v), API, Completion +- [x] README.md: Installation, Voraussetzungen, Quickstart (counter.v), API, Completion ## Qualität -- [ ] pytest: Parser gegen Verilator-JSON als Orakel -- [ ] Integrationstest: counter.v bauen, importieren, zählen -- [ ] Tests ohne Verilator → skip -- [ ] kein Code aus dspsim (AGPL) +- [x] pytest: Parser gegen Verilator-JSON als Orakel +- [x] Integrationstest: counter.v bauen, importieren, zählen +- [x] Tests ohne Verilator → skip +- [x] kein Code aus dspsim (AGPL) ## Repo & Packaging (festgelegt) - Lizenz: MIT