flatten: preserve original object names via hdlname attribute.
[yosys.git] / README.md
1 ```
2 yosys -- Yosys Open SYnthesis Suite
3
4 Copyright (C) 2012 - 2020 Claire Wolf <claire@symbioticeda.com>
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 ```
18
19
20 yosys – Yosys Open SYnthesis Suite
21 ===================================
22
23 This is a framework for RTL synthesis tools. It currently has
24 extensive Verilog-2005 support and provides a basic set of
25 synthesis algorithms for various application domains.
26
27 Yosys can be adapted to perform any synthesis job by combining
28 the existing passes (algorithms) using synthesis scripts and
29 adding additional passes as needed by extending the yosys C++
30 code base.
31
32 Yosys is free software licensed under the ISC license (a GPL
33 compatible license that is similar in terms to the MIT license
34 or the 2-clause BSD license).
35
36
37 Web Site and Other Resources
38 ============================
39
40 More information and documentation can be found on the Yosys web site:
41 - http://www.clifford.at/yosys/
42
43 The "Documentation" page on the web site contains links to more resources,
44 including a manual that even describes some of the Yosys internals:
45 - http://www.clifford.at/yosys/documentation.html
46
47 The file `CodingReadme` in this directory contains additional information
48 for people interested in using the Yosys C++ APIs.
49
50 Users interested in formal verification might want to use the formal verification
51 front-end for Yosys, SymbiYosys:
52 - https://symbiyosys.readthedocs.io/en/latest/
53 - https://github.com/YosysHQ/SymbiYosys
54
55
56 Setup
57 ======
58
59 You need a C++ compiler with C++11 support (up-to-date CLANG or GCC is
60 recommended) and some standard tools such as GNU Flex, GNU Bison, and GNU Make.
61 TCL, readline and libffi are optional (see ``ENABLE_*`` settings in Makefile).
62 Xdot (graphviz) is used by the ``show`` command in yosys to display schematics.
63
64 For example on Ubuntu Linux 16.04 LTS the following commands will install all
65 prerequisites for building yosys:
66
67 $ sudo apt-get install build-essential clang bison flex \
68 libreadline-dev gawk tcl-dev libffi-dev git \
69 graphviz xdot pkg-config python3 libboost-system-dev \
70 libboost-python-dev libboost-filesystem-dev zlib1g-dev
71
72 Similarily, on Mac OS X Homebrew can be used to install dependencies (from within cloned yosys repository):
73
74 $ brew tap Homebrew/bundle && brew bundle
75
76 or MacPorts:
77
78 $ sudo port install bison flex readline gawk libffi \
79 git graphviz pkgconfig python36 boost zlib tcl
80
81 On FreeBSD use the following command to install all prerequisites:
82
83 # pkg install bison flex readline gawk libffi\
84 git graphviz pkgconf python3 python36 tcl-wrapper boost-libs
85
86 On FreeBSD system use gmake instead of make. To run tests use:
87 % MAKE=gmake CC=cc gmake test
88
89 For Cygwin use the following command to install all prerequisites, or select these additional packages:
90
91 setup-x86_64.exe -q --packages=bison,flex,gcc-core,gcc-g++,git,libffi-devel,libreadline-devel,make,pkg-config,python3,tcl-devel,boost-build,zlib-devel
92
93 There are also pre-compiled Yosys binary packages for Ubuntu and Win32 as well
94 as a source distribution for Visual Studio. Visit the Yosys download page for
95 more information: http://www.clifford.at/yosys/download.html
96
97 To configure the build system to use a specific compiler, use one of
98
99 $ make config-clang
100 $ make config-gcc
101
102 For other compilers and build configurations it might be
103 necessary to make some changes to the config section of the
104 Makefile.
105
106 $ vi Makefile # ..or..
107 $ vi Makefile.conf
108
109 To build Yosys simply type 'make' in this directory.
110
111 $ make
112 $ sudo make install
113
114 Note that this also downloads, builds and installs ABC (using yosys-abc
115 as executable name).
116
117 Tests are located in the tests subdirectory and can be executed using the test target. Note that you need gawk as well as a recent version of iverilog (i.e. build from git). Then, execute tests via:
118
119 $ make test
120
121 To use a separate (out-of-tree) build directory, provide a path to the Makefile.
122
123 $ mkdir build; cd build
124 $ make -f ../Makefile
125
126 Out-of-tree builds require a clean source tree.
127
128 Getting Started
129 ===============
130
131 Yosys can be used with the interactive command shell, with
132 synthesis scripts or with command line arguments. Let's perform
133 a simple synthesis job using the interactive command shell:
134
135 $ ./yosys
136 yosys>
137
138 the command ``help`` can be used to print a list of all available
139 commands and ``help <command>`` to print details on the specified command:
140
141 yosys> help help
142
143 reading and elaborating the design using the Verilog frontend:
144
145 yosys> read -sv tests/simple/fiedler-cooley.v
146 yosys> hierarchy -top up3down5
147
148 writing the design to the console in Yosys's internal format:
149
150 yosys> write_ilang
151
152 convert processes (``always`` blocks) to netlist elements and perform
153 some simple optimizations:
154
155 yosys> proc; opt
156
157 display design netlist using ``xdot``:
158
159 yosys> show
160
161 the same thing using ``gv`` as postscript viewer:
162
163 yosys> show -format ps -viewer gv
164
165 translating netlist to gate logic and perform some simple optimizations:
166
167 yosys> techmap; opt
168
169 write design netlist to a new Verilog file:
170
171 yosys> write_verilog synth.v
172
173 or using a simple synthesis script:
174
175 $ cat synth.ys
176 read -sv tests/simple/fiedler-cooley.v
177 hierarchy -top up3down5
178 proc; opt; techmap; opt
179 write_verilog synth.v
180
181 $ ./yosys synth.ys
182
183 If ABC is enabled in the Yosys build configuration and a cell library is given
184 in the liberty file ``mycells.lib``, the following synthesis script will
185 synthesize for the given cell library:
186
187 # read design
188 read -sv tests/simple/fiedler-cooley.v
189 hierarchy -top up3down5
190
191 # the high-level stuff
192 proc; fsm; opt; memory; opt
193
194 # mapping to internal cell library
195 techmap; opt
196
197 # mapping flip-flops to mycells.lib
198 dfflibmap -liberty mycells.lib
199
200 # mapping logic to mycells.lib
201 abc -liberty mycells.lib
202
203 # cleanup
204 clean
205
206 If you do not have a liberty file but want to test this synthesis script,
207 you can use the file ``examples/cmos/cmos_cells.lib`` from the yosys sources
208 as simple example.
209
210 Liberty file downloads for and information about free and open ASIC standard
211 cell libraries can be found here:
212
213 - http://www.vlsitechnology.org/html/libraries.html
214 - http://www.vlsitechnology.org/synopsys/vsclib013.lib
215
216 The command ``synth`` provides a good default synthesis script (see
217 ``help synth``):
218
219 read -sv tests/simple/fiedler-cooley.v
220 synth -top up3down5
221
222 # mapping to target cells
223 dfflibmap -liberty mycells.lib
224 abc -liberty mycells.lib
225 clean
226
227 The command ``prep`` provides a good default word-level synthesis script, as
228 used in SMT-based formal verification.
229
230
231 Unsupported Verilog-2005 Features
232 =================================
233
234 The following Verilog-2005 features are not supported by
235 Yosys and there are currently no plans to add support
236 for them:
237
238 - Non-synthesizable language features as defined in
239 IEC 62142(E):2005 / IEEE Std. 1364.1(E):2002
240
241 - The ``tri``, ``triand`` and ``trior`` net types
242
243 - The ``config`` and ``disable`` keywords and library map files
244
245
246 Verilog Attributes and non-standard features
247 ============================================
248
249 - The ``full_case`` attribute on case statements is supported
250 (also the non-standard ``// synopsys full_case`` directive)
251
252 - The ``parallel_case`` attribute on case statements is supported
253 (also the non-standard ``// synopsys parallel_case`` directive)
254
255 - The ``// synopsys translate_off`` and ``// synopsys translate_on``
256 directives are also supported (but the use of ``` `ifdef .. `endif ```
257 is strongly recommended instead).
258
259 - The ``nomem2reg`` attribute on modules or arrays prohibits the
260 automatic early conversion of arrays to separate registers. This
261 is potentially dangerous. Usually the front-end has good reasons
262 for converting an array to a list of registers. Prohibiting this
263 step will likely result in incorrect synthesis results.
264
265 - The ``mem2reg`` attribute on modules or arrays forces the early
266 conversion of arrays to separate registers.
267
268 - The ``nomeminit`` attribute on modules or arrays prohibits the
269 creation of initialized memories. This effectively puts ``mem2reg``
270 on all memories that are written to in an ``initial`` block and
271 are not ROMs.
272
273 - The ``nolatches`` attribute on modules or always-blocks
274 prohibits the generation of logic-loops for latches. Instead
275 all not explicitly assigned values default to x-bits. This does
276 not affect clocked storage elements such as flip-flops.
277
278 - The ``nosync`` attribute on registers prohibits the generation of a
279 storage element. The register itself will always have all bits set
280 to 'x' (undefined). The variable may only be used as blocking assigned
281 temporary variable within an always block. This is mostly used internally
282 by Yosys to synthesize Verilog functions and access arrays.
283
284 - The ``nowrshmsk`` attribute on a register prohibits the generation of
285 shift-and-mask type circuits for writing to bit slices of that register.
286
287 - The ``onehot`` attribute on wires mark them as one-hot state register. This
288 is used for example for memory port sharing and set by the fsm_map pass.
289
290 - The ``blackbox`` attribute on modules is used to mark empty stub modules
291 that have the same ports as the real thing but do not contain information
292 on the internal configuration. This modules are only used by the synthesis
293 passes to identify input and output ports of cells. The Verilog backend
294 also does not output blackbox modules on default. ``read_verilog``, unless
295 called with ``-noblackbox`` will automatically set the blackbox attribute
296 on any empty module it reads.
297
298 - The ``noblackbox`` attribute set on an empty module prevents ``read_verilog``
299 from automatically setting the blackbox attribute on the module.
300
301 - The ``whitebox`` attribute on modules triggers the same behavior as
302 ``blackbox``, but is for whitebox modules, i.e. library modules that
303 contain a behavioral model of the cell type.
304
305 - The ``lib_whitebox`` attribute overwrites ``whitebox`` when ``read_verilog``
306 is run in `-lib` mode. Otherwise it's automatically removed.
307
308 - The ``dynports`` attribute is used by the Verilog front-end to mark modules
309 that have ports with a width that depends on a parameter.
310
311 - The ``hdlname`` attribute is used by some passes to document the original
312 (HDL) name of a module when renaming a module. It should contain a single
313 name, or, when describing a hierarchical name in a flattened design, multiple
314 names separated by a single space character.
315
316 - The ``keep`` attribute on cells and wires is used to mark objects that should
317 never be removed by the optimizer. This is used for example for cells that
318 have hidden connections that are not part of the netlist, such as IO pads.
319 Setting the ``keep`` attribute on a module has the same effect as setting it
320 on all instances of the module.
321
322 - The ``keep_hierarchy`` attribute on cells and modules keeps the ``flatten``
323 command from flattening the indicated cells and modules.
324
325 - The ``init`` attribute on wires is set by the frontend when a register is
326 initialized "FPGA-style" with ``reg foo = val``. It can be used during
327 synthesis to add the necessary reset logic.
328
329 - The ``top`` attribute on a module marks this module as the top of the
330 design hierarchy. The ``hierarchy`` command sets this attribute when called
331 with ``-top``. Other commands, such as ``flatten`` and various backends
332 use this attribute to determine the top module.
333
334 - The ``src`` attribute is set on cells and wires created by to the string
335 ``<hdl-file-name>:<line-number>`` by the HDL front-end and is then carried
336 through the synthesis. When entities are combined, a new |-separated
337 string is created that contains all the string from the original entities.
338
339 - The ``defaultvalue`` attribute is used to store default values for
340 module inputs. The attribute is attached to the input wire by the HDL
341 front-end when the input is declared with a default value.
342
343 - The ``parameter`` and ``localparam`` attributes are used to mark wires
344 that represent module parameters or localparams (when the HDL front-end
345 is run in ``-pwires`` mode).
346
347 - Wires marked with the ``hierconn`` attribute are connected to wires with the
348 same name (format ``cell_name.identifier``) when they are imported from
349 sub-modules by ``flatten``.
350
351 - The ``clkbuf_driver`` attribute can be set on an output port of a blackbox
352 module to mark it as a clock buffer output, and thus prevent ``clkbufmap``
353 from inserting another clock buffer on a net driven by such output.
354
355 - The ``clkbuf_sink`` attribute can be set on an input port of a module to
356 request clock buffer insertion by the ``clkbufmap`` pass.
357
358 - The ``clkbuf_inv`` attribute can be set on an output port of a module
359 with the value set to the name of an input port of that module. When
360 the ``clkbufmap`` would otherwise insert a clock buffer on this output,
361 it will instead try inserting the clock buffer on the input port (this
362 is used to implement clock inverter cells that clock buffer insertion
363 will "see through").
364
365 - The ``clkbuf_inhibit`` is the default attribute to set on a wire to prevent
366 automatic clock buffer insertion by ``clkbufmap``. This behaviour can be
367 overridden by providing a custom selection to ``clkbufmap``.
368
369 - The ``invertible_pin`` attribute can be set on a port to mark it as
370 invertible via a cell parameter. The name of the inversion parameter
371 is specified as the value of this attribute. The value of the inversion
372 parameter must be of the same width as the port, with 1 indicating
373 an inverted bit and 0 indicating a non-inverted bit.
374
375 - The ``iopad_external_pin`` attribute on a blackbox module's port marks
376 it as the external-facing pin of an I/O pad, and prevents ``iopadmap``
377 from inserting another pad cell on it.
378
379 - The module attribute ``abc9_lut`` is an integer attribute indicating to
380 `abc9` that this module describes a LUT with an area cost of this value, and
381 propagation delays described using `specify` statements.
382
383 - The module attribute ``abc9_box`` is a boolean specifying a black/white-box
384 definition, with propagation delays described using `specify` statements, for
385 use by `abc9`.
386
387 - The port attribute ``abc9_carry`` marks the carry-in (if an input port) and
388 carry-out (if output port) ports of a box. This information is necessary for
389 `abc9` to preserve the integrity of carry-chains. Specifying this attribute
390 onto a bus port will affect only its most significant bit.
391
392 - The module attribute ``abc9_flop`` is a boolean marking the module as a
393 flip-flop. This allows `abc9` to analyse its contents in order to perform
394 sequential synthesis.
395
396 - The frontend sets attributes ``always_comb``, ``always_latch`` and
397 ``always_ff`` on processes derived from SystemVerilog style always blocks
398 according to the type of the always. These are checked for correctness in
399 ``proc_dlatch``.
400
401 - The cell attribute ``wildcard_port_conns`` represents wildcard port
402 connections (SystemVerilog ``.*``). These are resolved to concrete
403 connections to matching wires in ``hierarchy``.
404
405 - In addition to the ``(* ... *)`` attribute syntax, Yosys supports
406 the non-standard ``{* ... *}`` attribute syntax to set default attributes
407 for everything that comes after the ``{* ... *}`` statement. (Reset
408 by adding an empty ``{* *}`` statement.)
409
410 - In module parameter and port declarations, and cell port and parameter
411 lists, a trailing comma is ignored. This simplifies writing Verilog code
412 generators a bit in some cases.
413
414 - Modules can be declared with ``module mod_name(...);`` (with three dots
415 instead of a list of module ports). With this syntax it is sufficient
416 to simply declare a module port as 'input' or 'output' in the module
417 body.
418
419 - When defining a macro with `define, all text between triple double quotes
420 is interpreted as macro body, even if it contains unescaped newlines. The
421 triple double quotes are removed from the macro body. For example:
422
423 `define MY_MACRO(a, b) """
424 assign a = 23;
425 assign b = 42;
426 """
427
428 - The attribute ``via_celltype`` can be used to implement a Verilog task or
429 function by instantiating the specified cell type. The value is the name
430 of the cell type to use. For functions the name of the output port can
431 be specified by appending it to the cell type separated by a whitespace.
432 The body of the task or function is unused in this case and can be used
433 to specify a behavioral model of the cell type for simulation. For example:
434
435 module my_add3(A, B, C, Y);
436 parameter WIDTH = 8;
437 input [WIDTH-1:0] A, B, C;
438 output [WIDTH-1:0] Y;
439 ...
440 endmodule
441
442 module top;
443 ...
444 (* via_celltype = "my_add3 Y" *)
445 (* via_celltype_defparam_WIDTH = 32 *)
446 function [31:0] add3;
447 input [31:0] A, B, C;
448 begin
449 add3 = A + B + C;
450 end
451 endfunction
452 ...
453 endmodule
454
455 - The ``wiretype`` attribute is added by the verilog parser for wires of a
456 typedef'd type to indicate the type identifier.
457
458 - Various ``enum_value_{value}`` attributes are added to wires of an enumerated type
459 to give a map of possible enum items to their values.
460
461 - The ``enum_base_type`` attribute is added to enum items to indicate which
462 enum they belong to (enums -- anonymous and otherwise -- are
463 automatically named with an auto-incrementing counter). Note that enums
464 are currently not strongly typed.
465
466 - A limited subset of DPI-C functions is supported. The plugin mechanism
467 (see ``help plugin``) can be used to load .so files with implementations
468 of DPI-C routines. As a non-standard extension it is possible to specify
469 a plugin alias using the ``<alias>:`` syntax. For example:
470
471 module dpitest;
472 import "DPI-C" function foo:round = real my_round (real);
473 parameter real r = my_round(12.345);
474 endmodule
475
476 $ yosys -p 'plugin -a foo -i /lib/libm.so; read_verilog dpitest.v'
477
478 - Sized constants (the syntax ``<size>'s?[bodh]<value>``) support constant
479 expressions as ``<size>``. If the expression is not a simple identifier, it
480 must be put in parentheses. Examples: ``WIDTH'd42``, ``(4+2)'b101010``
481
482 - The system tasks ``$finish``, ``$stop`` and ``$display`` are supported in
483 initial blocks in an unconditional context (only if/case statements on
484 expressions over parameters and constant values are allowed). The intended
485 use for this is synthesis-time DRC.
486
487 - There is limited support for converting ``specify`` .. ``endspecify``
488 statements to special ``$specify2``, ``$specify3``, and ``$specrule`` cells,
489 for use in blackboxes and whiteboxes. Use ``read_verilog -specify`` to
490 enable this functionality. (By default these blocks are ignored.)
491
492
493 Non-standard or SystemVerilog features for formal verification
494 ==============================================================
495
496 - Support for ``assert``, ``assume``, ``restrict``, and ``cover`` is enabled
497 when ``read_verilog`` is called with ``-formal``.
498
499 - The system task ``$initstate`` evaluates to 1 in the initial state and
500 to 0 otherwise.
501
502 - The system function ``$anyconst`` evaluates to any constant value. This is
503 equivalent to declaring a reg as ``rand const``, but also works outside
504 of checkers. (Yosys also supports ``rand const`` outside checkers.)
505
506 - The system function ``$anyseq`` evaluates to any value, possibly a different
507 value in each cycle. This is equivalent to declaring a reg as ``rand``,
508 but also works outside of checkers. (Yosys also supports ``rand``
509 variables outside checkers.)
510
511 - The system functions ``$allconst`` and ``$allseq`` can be used to construct
512 formal exist-forall problems. Assumptions only hold if the trace satisfies
513 the assumption for all ``$allconst/$allseq`` values. For assertions and cover
514 statements it is sufficient if just one ``$allconst/$allseq`` value triggers
515 the property (similar to ``$anyconst/$anyseq``).
516
517 - Wires/registers declared using the ``anyconst/anyseq/allconst/allseq`` attribute
518 (for example ``(* anyconst *) reg [7:0] foobar;``) will behave as if driven
519 by a ``$anyconst/$anyseq/$allconst/$allseq`` function.
520
521 - The SystemVerilog tasks ``$past``, ``$stable``, ``$rose`` and ``$fell`` are
522 supported in any clocked block.
523
524 - The syntax ``@($global_clock)`` can be used to create FFs that have no
525 explicit clock input (``$ff`` cells). The same can be achieved by using
526 ``@(posedge <netname>)`` or ``@(negedge <netname>)`` when ``<netname>``
527 is marked with the ``(* gclk *)`` Verilog attribute.
528
529
530 Supported features from SystemVerilog
531 =====================================
532
533 When ``read_verilog`` is called with ``-sv``, it accepts some language features
534 from SystemVerilog:
535
536 - The ``assert`` statement from SystemVerilog is supported in its most basic
537 form. In module context: ``assert property (<expression>);`` and within an
538 always block: ``assert(<expression>);``. It is transformed to an ``$assert`` cell.
539
540 - The ``assume``, ``restrict``, and ``cover`` statements from SystemVerilog are
541 also supported. The same limitations as with the ``assert`` statement apply.
542
543 - The keywords ``always_comb``, ``always_ff`` and ``always_latch``, ``logic``
544 and ``bit`` are supported.
545
546 - Declaring free variables with ``rand`` and ``rand const`` is supported.
547
548 - Checkers without a port list that do not need to be instantiated (but instead
549 behave like a named block) are supported.
550
551 - SystemVerilog packages are supported. Once a SystemVerilog file is read
552 into a design with ``read_verilog``, all its packages are available to
553 SystemVerilog files being read into the same design afterwards.
554
555 - typedefs are supported (including inside packages)
556 - type casts are currently not supported
557
558 - enums are supported (including inside packages)
559 - but are currently not strongly typed
560
561 - packed structs and unions are supported.
562
563 - SystemVerilog interfaces (SVIs) are supported. Modports for specifying whether
564 ports are inputs or outputs are supported.
565
566
567 Building the documentation
568 ==========================
569
570 Note that there is no need to build the manual if you just want to read it.
571 Simply download the PDF from http://www.clifford.at/yosys/documentation.html
572 instead.
573
574 On Ubuntu, texlive needs these packages to be able to build the manual:
575
576 sudo apt-get install texlive-binaries
577 sudo apt-get install texlive-science # install algorithm2e.sty
578 sudo apt-get install texlive-bibtex-extra # gets multibib.sty
579 sudo apt-get install texlive-fonts-extra # gets skull.sty and dsfont.sty
580 sudo apt-get install texlive-publishers # IEEEtran.cls
581
582 Also the non-free font luximono should be installed, there is unfortunately
583 no Ubuntu package for this so it should be installed separately using
584 `getnonfreefonts`:
585
586 wget https://tug.org/fonts/getnonfreefonts/install-getnonfreefonts
587 sudo texlua install-getnonfreefonts # will install to /usr/local by default, can be changed by editing BINDIR at MANDIR at the top of the script
588 getnonfreefonts luximono # installs to /home/user/texmf
589
590 Then execute, from the root of the repository:
591
592 make manual
593
594 Notes:
595
596 - To run `make manual` you need to have installed Yosys with `make install`,
597 otherwise it will fail on finding `kernel/yosys.h` while building
598 `PRESENTATION_Prog`.