Files
yosys/docs/source/using_yosys/more_scripting/model_checking.rst
T

149 lines
4.7 KiB
ReStructuredText
Raw Normal View History

2024-01-26 13:06:02 +13:00
Symbolic model checking
-----------------------
.. todo:: check text context
2026-02-15 09:00:04 +01:00
.. note::
While it is possible to perform model checking directly in Yosys, it
2024-01-26 13:06:02 +13:00
is highly recommended to use SBY or EQY for formal hardware verification.
Symbolic Model Checking (SMC) is used to formally prove that a circuit has (or
has not) a given property.
One application is Formal Equivalence Checking: Proving that two circuits are
identical. For example this is a very useful feature when debugging custom
passes in Yosys.
Other applications include checking if a module conforms to interface standards.
2024-05-03 13:38:01 +12:00
The `sat` command in Yosys can be used to perform Symbolic Model Checking.
2024-01-26 13:06:02 +13:00
Checking techmap
~~~~~~~~~~~~~~~~
.. todo:: add/expand supporting text
2024-01-30 13:31:00 +13:00
Let's take a look at an example included in the Yosys code base under
|code_examples/synth_flow|_:
.. |code_examples/synth_flow| replace:: :file:`docs/source/code_examples/synth_flow`
2024-03-24 00:41:40 -04:00
.. _code_examples/synth_flow: https://github.com/YosysHQ/yosys/tree/main/docs/source/code_examples/synth_flow
2024-01-26 13:06:02 +13:00
.. literalinclude:: /code_examples/synth_flow/techmap_01_map.v
:language: verilog
2024-01-30 13:31:00 +13:00
:caption: :file:`techmap_01_map.v`
2024-01-26 13:06:02 +13:00
.. literalinclude:: /code_examples/synth_flow/techmap_01.v
:language: verilog
2024-01-30 13:31:00 +13:00
:caption: :file:`techmap_01.v`
2024-01-26 13:06:02 +13:00
.. literalinclude:: /code_examples/synth_flow/techmap_01.ys
:language: yoscrypt
2024-01-30 13:31:00 +13:00
:caption: :file:`techmap_01.ys`
2024-01-26 13:06:02 +13:00
To see if it is correct we can use the following code:
2024-01-30 13:31:00 +13:00
.. todo:: replace inline code
2024-01-26 13:06:02 +13:00
.. code:: yoscrypt
# read test design
read_verilog techmap_01.v
hierarchy -top test
# create two version of the design: test_orig and test_mapped
copy test test_orig
rename test test_mapped
# apply the techmap only to test_mapped
techmap -map techmap_01_map.v test_mapped
# create a miter circuit to test equivalence
miter -equiv -make_assert -make_outputs test_orig test_mapped miter
flatten miter
# run equivalence check
sat -verify -prove-asserts -show-inputs -show-outputs miter
Result:
.. code::
Solving problem with 945 variables and 2505 clauses..
SAT proof finished - no model found: SUCCESS!
AXI4 Stream Master
~~~~~~~~~~~~~~~~~~
2024-01-30 13:31:00 +13:00
The code used in this section is included in the Yosys code base under
|code_examples/axis|_.
.. |code_examples/axis| replace:: :file:`docs/source/code_examples/axis`
2024-03-24 00:41:40 -04:00
.. _code_examples/axis: https://github.com/YosysHQ/yosys/tree/main/docs/source/code_examples/axis
2024-01-30 13:31:00 +13:00
2024-01-26 13:06:02 +13:00
The following AXI4 Stream Master has a bug. But the bug is not exposed if the
slave keeps ``tready`` asserted all the time. (Something a test bench might do.)
Symbolic Model Checking can be used to expose the bug and find a sequence of
values for ``tready`` that yield the incorrect behavior.
.. todo:: add/expand supporting text
.. literalinclude:: /code_examples/axis/axis_master.v
:language: verilog
2024-01-30 13:31:00 +13:00
:caption: :file:`axis_master.v`
2024-01-26 13:06:02 +13:00
.. literalinclude:: /code_examples/axis/axis_test.v
:language: verilog
2024-01-30 13:31:00 +13:00
:caption: :file:`axis_test.v`
2024-01-26 13:06:02 +13:00
.. literalinclude:: /code_examples/axis/axis_test.ys
:language: yoscrypt
2024-01-30 13:31:00 +13:00
:caption: :file:`test.ys`
Result with unmodified :file:`axis_master.v`:
2024-01-26 13:06:02 +13:00
2024-01-30 13:31:00 +13:00
.. todo:: replace inline code
2024-01-26 13:06:02 +13:00
.. code::
Solving problem with 159344 variables and 442126 clauses..
SAT proof finished - model found: FAIL!
2024-01-30 13:31:00 +13:00
Result with fixed :file:`axis_master.v`:
2024-01-26 13:06:02 +13:00
.. code::
Solving problem with 159144 variables and 441626 clauses..
SAT proof finished - no model found: SUCCESS!
2026-02-15 09:00:04 +01:00
Witness framework and per-property tracking
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2026-02-25 16:42:05 +01:00
When using AIGER-based formal verification flows (such as the ``abc`` engine in
2026-02-18 09:24:41 +01:00
SBY), Yosys provides infrastructure for tracking individual formal
2026-02-15 09:00:04 +01:00
properties through the verification pipeline.
2026-02-25 16:42:05 +01:00
The `rename -witness` pass assigns public names to all cells that participate in
the witness framework:
2026-02-15 09:00:04 +01:00
2026-02-18 09:24:41 +01:00
- Witness signal cells: `$anyconst`, `$anyseq`, `$anyinit`,
`$allconst`, `$allseq`
- Formal property cells: `$assert`, `$assume`, `$cover`, `$live`,
`$fair`, `$check`
2026-02-15 09:00:04 +01:00
These public names allow downstream tools to refer to individual properties by
their hierarchical path rather than by anonymous internal identifiers.
2026-02-18 09:29:35 +01:00
The `write_aiger -ywmap` option generates a map file for conversion to and from
Yosys witness traces, and also allows for mapping AIGER bad-state properties and
invariant constraints back to individual formal properties by name. This enables
2026-02-25 16:42:05 +01:00
features like per-property pass/fail reporting (e.g. ``abc pdr`` with
2026-02-18 09:29:35 +01:00
``--keep-going`` mode).
2026-02-15 09:00:04 +01:00
2026-02-18 09:24:41 +01:00
The `write_smt2` backend similarly uses the public witness names when emitting
2026-02-18 09:29:35 +01:00
SMT2 comments. Cells whose ``hdlname`` attribute contains the ``_witness_``
marker are treated as having private names for comment purposes, keeping solver
output clean.