@@ -4,17 +4,21 @@ Scripting with Pyosys
44Pyosys is a limited subset of the Yosys C++ API (aka "libyosys") made available
55using the Python programming language.
66
7- It offers access both to writing Yosys scripts like ``.ys `` and ``.tcl `` files
8- with the amenities of the Python programming language (functions, flow control,
9- etc), but also allows some access to internal data structures at the same time
10- unlike those two platforms, allowing you to also implement complex functionality
11- that is would otherwise not possible without writing custom passes using C++.
7+ Like ``.ys `` and ``.tcl `` scripts, Pyosys provides an interface to write Yosys
8+ scripts in the Python programming language, giving you the benefits of
9+ a type system, control flow, object-oriented programming, and more; especially
10+ that the other options lack a type system and control flow/OOP in Tcl is
11+ limited.
12+
13+ Though unlike these two, Pyosys goes a bit further, allowing you to use the
14+ Yosys API to implement advanced functionality that would otherwise require
15+ custom passes written in C++.
1216
1317
1418Getting Pyosys
1519--------------
1620
17- Pyosys supports Python 3.8.1 or higher. You can access Pyosys using one of two
21+ Pyosys supports CPython 3.8 or higher. You can access Pyosys using one of two
1822methods:
1923
20241. Compiling Yosys with the Makefile flag ``ENABLE_PYOSYS=1 ``
@@ -26,8 +30,8 @@ methods:
2630
27312. Installing the Pyosys wheels
2832
29- On macOS and GNU/Linux (specifically, not musllinux,) you can install
30- pre-built wheels of Yosys using ``pip `` as follows :
33+ On macOS and GNU/Linux you can install pre-built wheels of Yosys using
34+ ``pip ``:
3135
3236 ``python3 -m pip install pyosys ``
3337
@@ -56,7 +60,7 @@ this import may be preferable for terseness:
5660
5761 Now, scripting is actually quite similar to ``.ys `` and ``.tcl `` script in that
5862you can provide mostly text commands. Albeit, you can construct your scripts
59- to use Python's amenities including flow controls , loops, and functions:
63+ to use Python's amenities like conditional execution , loops, and functions:
6064
6165.. code-block :: python
6266
@@ -67,9 +71,9 @@ to use Python's amenities including flow controls, loops, and functions:
6771 if do_flatten:
6872 ys.run_pass(" flatten" )
6973
70- …but this does not provide anything that Tcl scripts do not provide you with.
71- The real power of using Pyosys comes from the fact you can manually instantiate,
72- manage, and interact with the design database.
74+ …but this does not strictly provide anything that Tcl scripts do not provide you
75+ with. The real power of using Pyosys comes from the fact you can manually
76+ instantiate, manage, and interact with the design database.
7377
7478As an example, here is the same script with a manually instantiated design.
7579
@@ -79,8 +83,8 @@ As an example, here is the same script with a manually instantiated design.
7983 :language: python
8084
8185What's new here is that you can manually inspect the design's database. This
82- gives you access to huge chunk of the design database API as in declared in the
83- ``kernel/rtlil.h `` header.
86+ gives you access to a huge chunk of the design database API as declared in
87+ the ``kernel/rtlil.h `` header.
8488
8589For example, here's how to list the input and output ports of the top module
8690of your design:
@@ -95,12 +99,18 @@ of your design:
9599 C++ data structures in Yosys are bridged to Python such that they have a
96100 pretty similar API to Python objects, for example:
97101
98- - ``std::vector `` supports the same methods as iterables in Python.
99- - `` std::set `` and hashlib `` pool `` support the same methods as `` set `` \s in
102+ - ``std::vector `` supports the same methods as
103+ ` iterables < https://docs.python.org/3/glossary.html#term-iterable >`_ in
100104 Python.
105+ - ``std::set `` and hashlib ``pool `` support the same methods as ``set ``\s in
106+ Python. While ``set `` is ordered, ``pool `` is not and modifications may
107+ cause a complete reordering of the set.
101108 - ``dict `` supports the same methods as ``dict ``\s in Python, albeit it is
102109 unordered, and modifications may cause a complete reordering of the
103110 dictionary.
111+ - ``idict `` uses a custom set of methods because it doesn't map very cleanly
112+ to an existing Python data structure. See ``pyosys/hashlib.h `` for more
113+ info.
104114
105115 For most operations, the Python equivalents are also supported as arguments
106116 where they will automatically be cast to the right type, so you do not have
@@ -121,8 +131,8 @@ to modify it, and introduce new elements and/or changes to your design.
121131As a demonstrative example, let's assume we want to add an enable line to all
122132flip-flops in our fiedler-cooley design.
123133
124- First of all, we will run :yoscrypt: `synth ` to convert all of the logic to Yosys's
125- internal cell structure (see :ref: `sec:celllib_gates `):
134+ First of all, we will run :yoscrypt: `synth ` to convert all of the logic to
135+ Yosys's internal cell structure (see :ref: `sec:celllib_gates `):
126136
127137.. literalinclude :: /code_examples/pyosys/simple_database.py
128138 :start-after: # synth
@@ -163,7 +173,8 @@ Next, we can iterate over all constituent cells, and if they are of the type
163173 :language: python
164174
165175To verify that you did everything correctly, it is prudent to call ``.check() ``
166- on the module you're manipulating as follows:
176+ on the module you're manipulating as follows after you're done with a set of
177+ changes:
167178
168179.. literalinclude :: /code_examples/pyosys/simple_database.py
169180 :start-after: run check
@@ -177,15 +188,15 @@ file and :yoscrypt:`synth_ice40` to map it to the iCE40 architecture.
177188 :start-after: write output
178189 :language: python
179190
180- And voila , you will note that in the intermediate output, all ``always @ ``
181- statements have an ``if (enable) ``\.
191+ And voilà , you will note that in the intermediate output, all ``always @ ``
192+ statements should have an ``if (enable) ``\.
182193
183194Encapsulating as Passes
184195-----------------------
185196
186- Just like when writing C++, you can encapsulate behavior in terms of "passes",
187- which are the commands you access using `` run_pass `` \. This adds it to a global
188- registry of commands that you can use using ``run_pass ``.
197+ Just like when writing C++, you can encapsulate routines in terms of "passes",
198+ which adds your Pass to a global registry of commands accessible using
199+ ``run_pass ``\ .
189200
190201.. literalinclude :: /code_examples/pyosys/pass.py
191202 :language: python
0 commit comments