Added equiv_miter
[yosys.git] / CodingReadme
1
2 This file contains some very brief documentation on things like programming APIs.
3 Also consult the Yosys manual and the section about programming in the presentation.
4 (Both can be downloaded as PDF from the yosys webpage.)
5
6
7 --snip-- only the lines below this mark are included in the yosys manual --snip--
8 Getting Started
9 ===============
10
11
12 Outline of a Yosys command
13 --------------------------
14
15 Here is a the C++ code for a "hello_world" Yosys command (hello.cc):
16
17 #include "kernel/yosys.h"
18
19 USING_YOSYS_NAMESPACE
20 PRIVATE_NAMESPACE_BEGIN
21
22 struct HelloWorldPass : public Pass {
23 HelloWorldPass() : Pass("hello_world") { }
24 virtual void execute(vector<string>, Design*) {
25 log("Hello World!\n");
26 }
27 } HelloWorldPass;
28
29 PRIVATE_NAMESPACE_END
30
31 This can be built into a Yosys module using the following command:
32
33 yosys-config --exec --cxx --cxxflags --ldflags -o hello.so -shared hello.cc --ldlibs
34
35 And then executed using the following command:
36
37 yosys -m hello.so -p hello_world
38
39
40 Yosys Data Structures
41 ---------------------
42
43 Here is a short list of data structures that you should make yourself familiar
44 with before you write C++ code for Yosys. The following data structures are all
45 defined when "kernel/yosys.h" is included and USING_YOSYS_NAMESPACE is used.
46
47 1. Yosys Container Classes
48
49 Yosys uses dict<K, T> and pool<T> as main container classes. dict<K, T> is
50 essentially a replacement for std::unordered_map<K, T> and pool<T> is a
51 replacement for std::unordered_set<T>. The main characteristics are:
52
53 - dict<K, T> and pool<T> are about 2x faster than the std containers
54
55 - references to elements in a dict<K, T> or pool<T> are invalidated by
56 insert and remove operations (similar to std::vector<T> on push_back()).
57
58 - some iterators are invalidated by erase(). specifically, iterators
59 that have not passed the erased element yet are invalidated. (erase()
60 itself returns valid iterator to the next element.)
61
62 - no iterators are invalidated by insert(). elements are inserted at
63 begin(). i.e. only a new iterator that starts at begin() will see the
64 inserted elements.
65
66 - the method .count(key, iterator) is like .count(key) but only
67 considers elements that can be reached via the iterator.
68
69 - iterators can be compared. it1 < it2 means that the position of t2
70 can be reached via t1 but not vice versa.
71
72 - dict<K, T> and pool<T> will have the same order of iteration across
73 all compilers, standard libraries and architectures.
74
75 In addition to dict<K, T> and pool<T> there is also an idict<K> that
76 creates a bijective map from K to the integers. For example:
77
78 idict<string, 42> si;
79 log("%d\n", si("hello")); // will print 42
80 log("%d\n", si("world")); // will print 43
81 log("%d\n", si.at("world")); // will print 43
82 log("%d\n", si.at("dummy")); // will throw exception
83 log("%s\n", si[42].c_str())); // will print hello
84 log("%s\n", si[43].c_str())); // will print world
85 log("%s\n", si[44].c_str())); // will throw exception
86
87 It is not possible to remove elements from an idict.
88
89 2. Standard STL data types
90
91 In Yosys we use std::vector<T> and std::string whenever applicable. When
92 dict<K, T> and pool<T> are not suitable then std::map<K, T> and std::set<T>
93 are used instead.
94
95 The types std::vector<T> and std::string are also available as vector<T>
96 and string in the Yosys namespace.
97
98 3. RTLIL objects
99
100 The current design (essentially a collection of modules, each defined by a
101 netlist) is stored in memory using RTLIL object (declared in kernel/rtlil.h,
102 automatically included by kernel/yosys.h). You should glance over at least
103 the declarations for the following types in kernel/rtlil.h:
104
105 RTLIL::IdString
106 This is a handle for an identifier (e.g. cell or wire name).
107 It feels a lot like a std::string, but is only a single int
108 in size. (The actual string is stored in a global lookup
109 table.)
110
111 RTLIL::SigBit
112 A single signal bit. I.e. either a constant (0, 1, x, z) or
113 a single bit from a wire.
114
115 RTLIL::SigSpec
116 Essentially a vector of SigBits.
117
118 RTLIL::Wire
119 RTLIL::Cell
120 The building blocks of the netlist in a module.
121
122 RTLIL::Module
123 RTLIL::Design
124 The module is a container with connected cells and wires
125 in it. The design is a container with modules in it.
126
127 All this types are also available without the RTLIL:: prefix in the Yosys
128 namespace.
129
130 4. SigMap and other Helper Classes
131
132 There are a couple of additional helper classes that are in wide use
133 in Yosys. Most importantly there is SigMap (declared in kernel/sigtools.h).
134
135 When a design has many wires in it that are connected to each other, then a
136 single signal bit can have multiple valid names. The SigMap object can be used
137 to map SigSpecs or SigBits to unique SigSpecs and SigBits that consitently
138 only use one wire from such a group of connected wires. For example:
139
140 SigBit a = module->addWire(NEW_ID);
141 SigBit b = module->addWire(NEW_ID);
142 module->connect(a, b);
143
144 log("%d\n", a == b); // will print 0
145
146 SigMap sigmap(module);
147 log("%d\n", sigmap(a) == sigmap(b)); // will print 1
148
149
150 Example Code
151 ------------
152
153 The following yosys commands are a good starting point if you are looking for examples
154 of how to use the Yosys API:
155
156 manual/CHAPTER_Prog/stubnets.cc
157 passes/opt/wreduce.cc
158 passes/techmap/maccmap.cc
159
160
161 Notes on the existing codebase
162 ------------------------------
163
164 For historical reasons not all parts of Yosys adhere to the current coding
165 style. When adding code to existing parts of the system, adhere to this guide
166 for the new code instead of trying to mimic the style of the surrounding code.
167
168
169
170 Coding Style
171 ============
172
173
174 Formatting of code
175 ------------------
176
177 - Yosys code is using tabs for indentation. Tabs are 8 characters.
178
179 - A continuation of a statement in the following line is indented by
180 two additional tabs.
181
182 - Lines are as long as you want them to be. A good rule of thumb is
183 to break lines at about column 150.
184
185 - Opening braces can be put on the same or next line as the statement
186 opening the block (if, switch, for, while, do). Put the opening brace
187 on its own line for larger blocks, especially blocks that contains
188 blank lines.
189
190 - Otherwise stick to the Linux Kernel Coding Stlye:
191 https://www.kernel.org/doc/Documentation/CodingStyle
192
193
194 C++ Langugage
195 -------------
196
197 Yosys is written in C++11. At the moment only constructs supported by
198 gcc 4.6 are allowed in Yosys code. This will change in future releases.
199
200 In general Yosys uses "int" instead of "size_t". To avoid compiler
201 warnings for implicit type casts, always use "GetSize(foobar)" instead
202 of "foobar.size()". (GetSize() is defined in kernel/yosys.h)
203
204 Use range-based for loops whenever applicable.
205
206
207 --snap-- only the lines above this mark are included in the yosys manual --snap--
208
209
210 Creating the Visual Studio Template Project
211 ===========================================
212
213 1. Create an empty Visual C++ Win32 Console App project
214
215 Microsoft Visual Studio Express 2013 for Windows Desktop
216 Open New Project Wizard (File -> New Project..)
217
218 Project Name: YosysVS
219 Solution Name: YosysVS
220 [X] Create directory for solution
221 [ ] Add to source control
222
223 [X] Console applications
224 [X] Empty Projcect
225 [ ] SDL checks
226
227 2. Open YosysVS Project Properties
228
229 Select Configuration: All Configurations
230
231 C/C++ -> General -> Additional Include Directories
232 Add: ..\yosys
233
234 C/C++ -> Preprocessor -> Preprocessor Definitions
235 Add: _YOSYS_;_CRT_SECURE_NO_WARNINGS
236
237 3. Resulting file system tree:
238
239 YosysVS/
240 YosysVS/YosysVS
241 YosysVS/YosysVS/YosysVS.vcxproj
242 YosysVS/YosysVS/YosysVS.vcxproj.filters
243 YosysVS/YosysVS.sdf
244 YosysVS/YosysVS.sln
245 YosysVS/YosysVS.v12.suo
246
247 4. Zip YosysVS as YosysVS-Tpl-v1.zip
248
249
250
251 Checklist for adding internal cell types
252 ========================================
253
254 Things to do right away:
255
256 - Add to kernel/celltypes.h (incl. eval() handling for non-mem cells)
257 - Add to InternalCellChecker::check() in kernel/rtlil.cc
258 - Add to techlibs/common/simlib.v
259 - Add to techlibs/common/techmap.v
260
261 Things to do after finalizing the cell interface:
262
263 - Add support to kernel/satgen.h for the new cell type
264 - Add to manual/CHAPTER_CellLib.tex (or just add a fixme to the bottom)
265 - Maybe add support to the verilog backend for dumping such cells as expression
266
267
268
269 Checklist for creating Yosys releases
270 =====================================
271
272 Update the CHANGELOG file:
273
274 cd ~yosys
275 gitk &
276 vi CHANGELOG
277
278
279 Run all tests with "make config-{clang,gcc,gcc-4.6}":
280
281 cd ~yosys
282 make clean
283 make test vloghtb
284 make install
285
286 cd ~yosys-bigsim
287 make clean
288 make full
289
290 cd ~vloghammer
291 make purge gen_issues gen_samples
292 make SYN_LIST="yosys" SIM_LIST="icarus yosim verilator" REPORT_FULL=1 world
293 chromium-browser report.html
294
295
296 Then with default config setting:
297
298 cd ~yosys
299 ./yosys -p 'proc; show' tests/simple/fiedler-cooley.v
300 ./yosys -p 'proc; opt; show' tests/simple/fiedler-cooley.v
301 ./yosys -p 'synth; show' tests/simple/fiedler-cooley.v
302
303 cd ~yosys
304 make manual
305 - sanity check the figures in the appnotes and presentation
306 - if there are any odd things -> investigate
307 - make cosmetic changes to the .tex files if necessary
308
309
310 Also with default config setting:
311
312 cd ~yosys/techlibs/cmos
313 bash testbench.sh
314
315 cd ~yosys/techlibs/xilinx/example_sim_counter
316 bash run_sim.sh
317
318 cd ~yosys/techlibs/xilinx/example_mojo_counter
319 bash example.sh
320
321
322 Finally if a current verific library is available:
323
324 cd ~yosys
325 cat frontends/verific/build_amd64.txt
326 - follow instructions
327
328 cd frontends/verific
329 ../../yosys test_navre.ys
330
331
332 Release candiate:
333
334 - create branch yosys-x.y.z-rc and push to github
335 - contact the usual suspects per mail and ask them to test
336 - post on the reddit and ask people to test
337 - commit KISS fixes to the -rc branch if necessary
338
339
340 Release:
341
342 - set YOSYS_VER to x.y.z in Makefile
343 - update version string in CHANGELOG
344 git commit -am "Yosys x.y.z"
345
346 - push tag to github
347 - post changelog on github
348 - post short release note on reddit
349 - delete -rc branch from github
350
351
352 Updating the website:
353
354 cd ~yosys
355 make manual
356 make install
357
358 - update pdf files on the website
359
360 cd ~yosys-web
361 make update_cmd
362 make update_show
363 git commit -am update
364 make push
365
366
367 In master branch:
368
369 git merge {release-tag}
370 - set version to x.y.z+ in Makefile
371 - add section "Yosys x.y.z .. x.y.z+" to CHANGELOG
372 git commit --amend -am "Yosys x.y.z+"
373
374