Merge branch 'master' of github.com:YosysHQ/yosys
[yosys.git] / backends / blif / blif.cc
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
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 // [[CITE]] Berkeley Logic Interchange Format (BLIF)
21 // University of California. Berkeley. July 28, 1992
22 // http://www.ece.cmu.edu/~ee760/760docs/blif.pdf
23
24 #include "kernel/rtlil.h"
25 #include "kernel/register.h"
26 #include "kernel/sigtools.h"
27 #include "kernel/celltypes.h"
28 #include "kernel/log.h"
29 #include <string>
30
31 USING_YOSYS_NAMESPACE
32 PRIVATE_NAMESPACE_BEGIN
33
34 struct BlifDumperConfig
35 {
36 bool icells_mode;
37 bool conn_mode;
38 bool impltf_mode;
39 bool gates_mode;
40 bool cname_mode;
41 bool iname_mode;
42 bool param_mode;
43 bool attr_mode;
44 bool iattr_mode;
45 bool blackbox_mode;
46 bool noalias_mode;
47
48 std::string buf_type, buf_in, buf_out;
49 std::map<RTLIL::IdString, std::pair<RTLIL::IdString, RTLIL::IdString>> unbuf_types;
50 std::string true_type, true_out, false_type, false_out, undef_type, undef_out;
51
52 BlifDumperConfig() : icells_mode(false), conn_mode(false), impltf_mode(false), gates_mode(false),
53 cname_mode(false), iname_mode(false), param_mode(false), attr_mode(false), iattr_mode(false),
54 blackbox_mode(false), noalias_mode(false) { }
55 };
56
57 struct BlifDumper
58 {
59 std::ostream &f;
60 RTLIL::Module *module;
61 RTLIL::Design *design;
62 BlifDumperConfig *config;
63 CellTypes ct;
64
65 SigMap sigmap;
66 dict<SigBit, int> init_bits;
67
68 BlifDumper(std::ostream &f, RTLIL::Module *module, RTLIL::Design *design, BlifDumperConfig *config) :
69 f(f), module(module), design(design), config(config), ct(design), sigmap(module)
70 {
71 for (Wire *wire : module->wires())
72 if (wire->attributes.count("\\init")) {
73 SigSpec initsig = sigmap(wire);
74 Const initval = wire->attributes.at("\\init");
75 for (int i = 0; i < GetSize(initsig) && i < GetSize(initval); i++)
76 switch (initval[i]) {
77 case State::S0:
78 init_bits[initsig[i]] = 0;
79 break;
80 case State::S1:
81 init_bits[initsig[i]] = 1;
82 break;
83 default:
84 break;
85 }
86 }
87 }
88
89 vector<shared_str> cstr_buf;
90 pool<SigBit> cstr_bits_seen;
91
92 const char *cstr(RTLIL::IdString id)
93 {
94 std::string str = RTLIL::unescape_id(id);
95 for (size_t i = 0; i < str.size(); i++)
96 if (str[i] == '#' || str[i] == '=' || str[i] == '<' || str[i] == '>')
97 str[i] = '?';
98 cstr_buf.push_back(str);
99 return cstr_buf.back().c_str();
100 }
101
102 const char *cstr(RTLIL::SigBit sig)
103 {
104 cstr_bits_seen.insert(sig);
105
106 if (sig.wire == NULL) {
107 if (sig == RTLIL::State::S0) return config->false_type == "-" || config->false_type == "+" ? config->false_out.c_str() : "$false";
108 if (sig == RTLIL::State::S1) return config->true_type == "-" || config->true_type == "+" ? config->true_out.c_str() : "$true";
109 return config->undef_type == "-" || config->undef_type == "+" ? config->undef_out.c_str() : "$undef";
110 }
111
112 std::string str = RTLIL::unescape_id(sig.wire->name);
113 for (size_t i = 0; i < str.size(); i++)
114 if (str[i] == '#' || str[i] == '=' || str[i] == '<' || str[i] == '>')
115 str[i] = '?';
116
117 if (sig.wire->width != 1)
118 str += stringf("[%d]", sig.wire->upto ? sig.wire->start_offset+sig.wire->width-sig.offset-1 : sig.wire->start_offset+sig.offset);
119
120 cstr_buf.push_back(str);
121 return cstr_buf.back().c_str();
122 }
123
124 const char *cstr_init(RTLIL::SigBit sig)
125 {
126 sigmap.apply(sig);
127
128 if (init_bits.count(sig) == 0)
129 return " 2";
130
131 string str = stringf(" %d", init_bits.at(sig));
132
133 cstr_buf.push_back(str);
134 return cstr_buf.back().c_str();
135 }
136
137 const char *subckt_or_gate(std::string cell_type)
138 {
139 if (!config->gates_mode)
140 return "subckt";
141 if (!design->modules_.count(RTLIL::escape_id(cell_type)))
142 return "gate";
143 if (design->modules_.at(RTLIL::escape_id(cell_type))->get_blackbox_attribute())
144 return "gate";
145 return "subckt";
146 }
147
148 void dump_params(const char *command, dict<IdString, Const> &params)
149 {
150 for (auto &param : params) {
151 f << stringf("%s %s ", command, RTLIL::id2cstr(param.first));
152 if (param.second.flags & RTLIL::CONST_FLAG_STRING) {
153 std::string str = param.second.decode_string();
154 f << stringf("\"");
155 for (char ch : str)
156 if (ch == '"' || ch == '\\')
157 f << stringf("\\%c", ch);
158 else if (ch < 32 || ch >= 127)
159 f << stringf("\\%03o", ch);
160 else
161 f << stringf("%c", ch);
162 f << stringf("\"\n");
163 } else
164 f << stringf("%s\n", param.second.as_string().c_str());
165 }
166 }
167
168 void dump()
169 {
170 f << stringf("\n");
171 f << stringf(".model %s\n", cstr(module->name));
172
173 std::map<int, RTLIL::Wire*> inputs, outputs;
174
175 for (auto &wire_it : module->wires_) {
176 RTLIL::Wire *wire = wire_it.second;
177 if (wire->port_input)
178 inputs[wire->port_id] = wire;
179 if (wire->port_output)
180 outputs[wire->port_id] = wire;
181 }
182
183 f << stringf(".inputs");
184 for (auto &it : inputs) {
185 RTLIL::Wire *wire = it.second;
186 for (int i = 0; i < wire->width; i++)
187 f << stringf(" %s", cstr(RTLIL::SigSpec(wire, i)));
188 }
189 f << stringf("\n");
190
191 f << stringf(".outputs");
192 for (auto &it : outputs) {
193 RTLIL::Wire *wire = it.second;
194 for (int i = 0; i < wire->width; i++)
195 f << stringf(" %s", cstr(RTLIL::SigSpec(wire, i)));
196 }
197 f << stringf("\n");
198
199 if (module->get_blackbox_attribute()) {
200 f << stringf(".blackbox\n");
201 f << stringf(".end\n");
202 return;
203 }
204
205 if (!config->impltf_mode) {
206 if (!config->false_type.empty()) {
207 if (config->false_type == "+")
208 f << stringf(".names %s\n", config->false_out.c_str());
209 else if (config->false_type != "-")
210 f << stringf(".%s %s %s=$false\n", subckt_or_gate(config->false_type),
211 config->false_type.c_str(), config->false_out.c_str());
212 } else
213 f << stringf(".names $false\n");
214 if (!config->true_type.empty()) {
215 if (config->true_type == "+")
216 f << stringf(".names %s\n1\n", config->true_out.c_str());
217 else if (config->true_type != "-")
218 f << stringf(".%s %s %s=$true\n", subckt_or_gate(config->true_type),
219 config->true_type.c_str(), config->true_out.c_str());
220 } else
221 f << stringf(".names $true\n1\n");
222 if (!config->undef_type.empty()) {
223 if (config->undef_type == "+")
224 f << stringf(".names %s\n", config->undef_out.c_str());
225 else if (config->undef_type != "-")
226 f << stringf(".%s %s %s=$undef\n", subckt_or_gate(config->undef_type),
227 config->undef_type.c_str(), config->undef_out.c_str());
228 } else
229 f << stringf(".names $undef\n");
230 }
231
232 for (auto &cell_it : module->cells_)
233 {
234 RTLIL::Cell *cell = cell_it.second;
235
236 if (config->unbuf_types.count(cell->type)) {
237 auto portnames = config->unbuf_types.at(cell->type);
238 f << stringf(".names %s %s\n1 1\n",
239 cstr(cell->getPort(portnames.first)), cstr(cell->getPort(portnames.second)));
240 continue;
241 }
242
243 if (!config->icells_mode && cell->type == "$_NOT_") {
244 f << stringf(".names %s %s\n0 1\n",
245 cstr(cell->getPort("\\A")), cstr(cell->getPort("\\Y")));
246 goto internal_cell;
247 }
248
249 if (!config->icells_mode && cell->type == "$_AND_") {
250 f << stringf(".names %s %s %s\n11 1\n",
251 cstr(cell->getPort("\\A")), cstr(cell->getPort("\\B")), cstr(cell->getPort("\\Y")));
252 goto internal_cell;
253 }
254
255 if (!config->icells_mode && cell->type == "$_OR_") {
256 f << stringf(".names %s %s %s\n1- 1\n-1 1\n",
257 cstr(cell->getPort("\\A")), cstr(cell->getPort("\\B")), cstr(cell->getPort("\\Y")));
258 goto internal_cell;
259 }
260
261 if (!config->icells_mode && cell->type == "$_XOR_") {
262 f << stringf(".names %s %s %s\n10 1\n01 1\n",
263 cstr(cell->getPort("\\A")), cstr(cell->getPort("\\B")), cstr(cell->getPort("\\Y")));
264 goto internal_cell;
265 }
266
267 if (!config->icells_mode && cell->type == "$_NAND_") {
268 f << stringf(".names %s %s %s\n0- 1\n-0 1\n",
269 cstr(cell->getPort("\\A")), cstr(cell->getPort("\\B")), cstr(cell->getPort("\\Y")));
270 goto internal_cell;
271 }
272
273 if (!config->icells_mode && cell->type == "$_NOR_") {
274 f << stringf(".names %s %s %s\n00 1\n",
275 cstr(cell->getPort("\\A")), cstr(cell->getPort("\\B")), cstr(cell->getPort("\\Y")));
276 goto internal_cell;
277 }
278
279 if (!config->icells_mode && cell->type == "$_XNOR_") {
280 f << stringf(".names %s %s %s\n11 1\n00 1\n",
281 cstr(cell->getPort("\\A")), cstr(cell->getPort("\\B")), cstr(cell->getPort("\\Y")));
282 goto internal_cell;
283 }
284
285 if (!config->icells_mode && cell->type == "$_ANDNOT_") {
286 f << stringf(".names %s %s %s\n10 1\n",
287 cstr(cell->getPort("\\A")), cstr(cell->getPort("\\B")), cstr(cell->getPort("\\Y")));
288 goto internal_cell;
289 }
290
291 if (!config->icells_mode && cell->type == "$_ORNOT_") {
292 f << stringf(".names %s %s %s\n1- 1\n-0 1\n",
293 cstr(cell->getPort("\\A")), cstr(cell->getPort("\\B")), cstr(cell->getPort("\\Y")));
294 goto internal_cell;
295 }
296
297 if (!config->icells_mode && cell->type == "$_AOI3_") {
298 f << stringf(".names %s %s %s %s\n-00 1\n0-0 1\n",
299 cstr(cell->getPort("\\A")), cstr(cell->getPort("\\B")), cstr(cell->getPort("\\C")), cstr(cell->getPort("\\Y")));
300 goto internal_cell;
301 }
302
303 if (!config->icells_mode && cell->type == "$_OAI3_") {
304 f << stringf(".names %s %s %s %s\n00- 1\n--0 1\n",
305 cstr(cell->getPort("\\A")), cstr(cell->getPort("\\B")), cstr(cell->getPort("\\C")), cstr(cell->getPort("\\Y")));
306 goto internal_cell;
307 }
308
309 if (!config->icells_mode && cell->type == "$_AOI4_") {
310 f << stringf(".names %s %s %s %s %s\n-0-0 1\n-00- 1\n0--0 1\n0-0- 1\n",
311 cstr(cell->getPort("\\A")), cstr(cell->getPort("\\B")),
312 cstr(cell->getPort("\\C")), cstr(cell->getPort("\\D")), cstr(cell->getPort("\\Y")));
313 goto internal_cell;
314 }
315
316 if (!config->icells_mode && cell->type == "$_OAI4_") {
317 f << stringf(".names %s %s %s %s %s\n00-- 1\n--00 1\n",
318 cstr(cell->getPort("\\A")), cstr(cell->getPort("\\B")),
319 cstr(cell->getPort("\\C")), cstr(cell->getPort("\\D")), cstr(cell->getPort("\\Y")));
320 goto internal_cell;
321 }
322
323 if (!config->icells_mode && cell->type == "$_MUX_") {
324 f << stringf(".names %s %s %s %s\n1-0 1\n-11 1\n",
325 cstr(cell->getPort("\\A")), cstr(cell->getPort("\\B")),
326 cstr(cell->getPort("\\S")), cstr(cell->getPort("\\Y")));
327 goto internal_cell;
328 }
329
330 if (!config->icells_mode && cell->type == "$_FF_") {
331 f << stringf(".latch %s %s%s\n", cstr(cell->getPort("\\D")), cstr(cell->getPort("\\Q")),
332 cstr_init(cell->getPort("\\Q")));
333 goto internal_cell;
334 }
335
336 if (!config->icells_mode && cell->type == "$_DFF_N_") {
337 f << stringf(".latch %s %s fe %s%s\n", cstr(cell->getPort("\\D")), cstr(cell->getPort("\\Q")),
338 cstr(cell->getPort("\\C")), cstr_init(cell->getPort("\\Q")));
339 goto internal_cell;
340 }
341
342 if (!config->icells_mode && cell->type == "$_DFF_P_") {
343 f << stringf(".latch %s %s re %s%s\n", cstr(cell->getPort("\\D")), cstr(cell->getPort("\\Q")),
344 cstr(cell->getPort("\\C")), cstr_init(cell->getPort("\\Q")));
345 goto internal_cell;
346 }
347
348 if (!config->icells_mode && cell->type == "$_DLATCH_N_") {
349 f << stringf(".latch %s %s al %s%s\n", cstr(cell->getPort("\\D")), cstr(cell->getPort("\\Q")),
350 cstr(cell->getPort("\\E")), cstr_init(cell->getPort("\\Q")));
351 goto internal_cell;
352 }
353
354 if (!config->icells_mode && cell->type == "$_DLATCH_P_") {
355 f << stringf(".latch %s %s ah %s%s\n", cstr(cell->getPort("\\D")), cstr(cell->getPort("\\Q")),
356 cstr(cell->getPort("\\E")), cstr_init(cell->getPort("\\Q")));
357 goto internal_cell;
358 }
359
360 if (!config->icells_mode && cell->type == "$lut") {
361 f << stringf(".names");
362 auto &inputs = cell->getPort("\\A");
363 auto width = cell->parameters.at("\\WIDTH").as_int();
364 log_assert(inputs.size() == width);
365 for (int i = width-1; i >= 0; i--)
366 f << stringf(" %s", cstr(inputs.extract(i, 1)));
367 auto &output = cell->getPort("\\Y");
368 log_assert(output.size() == 1);
369 f << stringf(" %s", cstr(output));
370 f << stringf("\n");
371 RTLIL::SigSpec mask = cell->parameters.at("\\LUT");
372 for (int i = 0; i < (1 << width); i++)
373 if (mask[i] == RTLIL::S1) {
374 for (int j = width-1; j >= 0; j--) {
375 f << ((i>>j)&1 ? '1' : '0');
376 }
377 f << " 1\n";
378 }
379 goto internal_cell;
380 }
381
382 if (!config->icells_mode && cell->type == "$sop") {
383 f << stringf(".names");
384 auto &inputs = cell->getPort("\\A");
385 auto width = cell->parameters.at("\\WIDTH").as_int();
386 auto depth = cell->parameters.at("\\DEPTH").as_int();
387 vector<State> table = cell->parameters.at("\\TABLE").bits;
388 while (GetSize(table) < 2*width*depth)
389 table.push_back(State::S0);
390 log_assert(inputs.size() == width);
391 for (int i = 0; i < width; i++)
392 f << stringf(" %s", cstr(inputs.extract(i, 1)));
393 auto &output = cell->getPort("\\Y");
394 log_assert(output.size() == 1);
395 f << stringf(" %s", cstr(output));
396 f << stringf("\n");
397 for (int i = 0; i < depth; i++) {
398 for (int j = 0; j < width; j++) {
399 bool pat0 = table.at(2*width*i + 2*j + 0) == State::S1;
400 bool pat1 = table.at(2*width*i + 2*j + 1) == State::S1;
401 if (pat0 && !pat1) f << "0";
402 else if (!pat0 && pat1) f << "1";
403 else f << "-";
404 }
405 f << " 1\n";
406 }
407 goto internal_cell;
408 }
409
410 f << stringf(".%s %s", subckt_or_gate(cell->type.str()), cstr(cell->type));
411 for (auto &conn : cell->connections())
412 for (int i = 0; i < conn.second.size(); i++) {
413 if (conn.second.size() == 1)
414 f << stringf(" %s", cstr(conn.first));
415 else
416 f << stringf(" %s[%d]", cstr(conn.first), i);
417 f << stringf("=%s", cstr(conn.second.extract(i, 1)));
418 }
419 f << stringf("\n");
420
421 if (config->cname_mode)
422 f << stringf(".cname %s\n", cstr(cell->name));
423 if (config->attr_mode)
424 dump_params(".attr", cell->attributes);
425 if (config->param_mode)
426 dump_params(".param", cell->parameters);
427
428 if (0) {
429 internal_cell:
430 if (config->iname_mode)
431 f << stringf(".cname %s\n", cstr(cell->name));
432 if (config->iattr_mode)
433 dump_params(".attr", cell->attributes);
434 }
435 }
436
437 for (auto &conn : module->connections())
438 for (int i = 0; i < conn.first.size(); i++)
439 {
440 SigBit lhs_bit = conn.first[i];
441 SigBit rhs_bit = conn.second[i];
442
443 if (config->noalias_mode && cstr_bits_seen.count(lhs_bit) == 0)
444 continue;
445
446 if (config->conn_mode)
447 f << stringf(".conn %s %s\n", cstr(rhs_bit), cstr(lhs_bit));
448 else if (!config->buf_type.empty())
449 f << stringf(".%s %s %s=%s %s=%s\n", subckt_or_gate(config->buf_type), config->buf_type.c_str(),
450 config->buf_in.c_str(), cstr(rhs_bit), config->buf_out.c_str(), cstr(lhs_bit));
451 else
452 f << stringf(".names %s %s\n1 1\n", cstr(rhs_bit), cstr(lhs_bit));
453 }
454
455 f << stringf(".end\n");
456 }
457
458 static void dump(std::ostream &f, RTLIL::Module *module, RTLIL::Design *design, BlifDumperConfig &config)
459 {
460 BlifDumper dumper(f, module, design, &config);
461 dumper.dump();
462 }
463 };
464
465 struct BlifBackend : public Backend {
466 BlifBackend() : Backend("blif", "write design to BLIF file") { }
467 void help() YS_OVERRIDE
468 {
469 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
470 log("\n");
471 log(" write_blif [options] [filename]\n");
472 log("\n");
473 log("Write the current design to an BLIF file.\n");
474 log("\n");
475 log(" -top top_module\n");
476 log(" set the specified module as design top module\n");
477 log("\n");
478 log(" -buf <cell-type> <in-port> <out-port>\n");
479 log(" use cells of type <cell-type> with the specified port names for buffers\n");
480 log("\n");
481 log(" -unbuf <cell-type> <in-port> <out-port>\n");
482 log(" replace buffer cells with the specified name and port names with\n");
483 log(" a .names statement that models a buffer\n");
484 log("\n");
485 log(" -true <cell-type> <out-port>\n");
486 log(" -false <cell-type> <out-port>\n");
487 log(" -undef <cell-type> <out-port>\n");
488 log(" use the specified cell types to drive nets that are constant 1, 0, or\n");
489 log(" undefined. when '-' is used as <cell-type>, then <out-port> specifies\n");
490 log(" the wire name to be used for the constant signal and no cell driving\n");
491 log(" that wire is generated. when '+' is used as <cell-type>, then <out-port>\n");
492 log(" specifies the wire name to be used for the constant signal and a .names\n");
493 log(" statement is generated to drive the wire.\n");
494 log("\n");
495 log(" -noalias\n");
496 log(" if a net name is aliasing another net name, then by default a net\n");
497 log(" without fanout is created that is driven by the other net. This option\n");
498 log(" suppresses the generation of this nets without fanout.\n");
499 log("\n");
500 log("The following options can be useful when the generated file is not going to be\n");
501 log("read by a BLIF parser but a custom tool. It is recommended to not name the output\n");
502 log("file *.blif when any of this options is used.\n");
503 log("\n");
504 log(" -icells\n");
505 log(" do not translate Yosys's internal gates to generic BLIF logic\n");
506 log(" functions. Instead create .subckt or .gate lines for all cells.\n");
507 log("\n");
508 log(" -gates\n");
509 log(" print .gate instead of .subckt lines for all cells that are not\n");
510 log(" instantiations of other modules from this design.\n");
511 log("\n");
512 log(" -conn\n");
513 log(" do not generate buffers for connected wires. instead use the\n");
514 log(" non-standard .conn statement.\n");
515 log("\n");
516 log(" -attr\n");
517 log(" use the non-standard .attr statement to write cell attributes\n");
518 log("\n");
519 log(" -param\n");
520 log(" use the non-standard .param statement to write cell parameters\n");
521 log("\n");
522 log(" -cname\n");
523 log(" use the non-standard .cname statement to write cell names\n");
524 log("\n");
525 log(" -iname, -iattr\n");
526 log(" enable -cname and -attr functionality for .names statements\n");
527 log(" (the .cname and .attr statements will be included in the BLIF\n");
528 log(" output after the truth table for the .names statement)\n");
529 log("\n");
530 log(" -blackbox\n");
531 log(" write blackbox cells with .blackbox statement.\n");
532 log("\n");
533 log(" -impltf\n");
534 log(" do not write definitions for the $true, $false and $undef wires.\n");
535 log("\n");
536 }
537 void execute(std::ostream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
538 {
539 std::string top_module_name;
540 std::string buf_type, buf_in, buf_out;
541 std::string true_type, true_out;
542 std::string false_type, false_out;
543 BlifDumperConfig config;
544
545 log_header(design, "Executing BLIF backend.\n");
546
547 size_t argidx;
548 for (argidx = 1; argidx < args.size(); argidx++)
549 {
550 if (args[argidx] == "-top" && argidx+1 < args.size()) {
551 top_module_name = args[++argidx];
552 continue;
553 }
554 if (args[argidx] == "-buf" && argidx+3 < args.size()) {
555 config.buf_type = args[++argidx];
556 config.buf_in = args[++argidx];
557 config.buf_out = args[++argidx];
558 continue;
559 }
560 if (args[argidx] == "-unbuf" && argidx+3 < args.size()) {
561 RTLIL::IdString unbuf_type = RTLIL::escape_id(args[++argidx]);
562 RTLIL::IdString unbuf_in = RTLIL::escape_id(args[++argidx]);
563 RTLIL::IdString unbuf_out = RTLIL::escape_id(args[++argidx]);
564 config.unbuf_types[unbuf_type] = std::pair<RTLIL::IdString, RTLIL::IdString>(unbuf_in, unbuf_out);
565 continue;
566 }
567 if (args[argidx] == "-true" && argidx+2 < args.size()) {
568 config.true_type = args[++argidx];
569 config.true_out = args[++argidx];
570 continue;
571 }
572 if (args[argidx] == "-false" && argidx+2 < args.size()) {
573 config.false_type = args[++argidx];
574 config.false_out = args[++argidx];
575 continue;
576 }
577 if (args[argidx] == "-undef" && argidx+2 < args.size()) {
578 config.undef_type = args[++argidx];
579 config.undef_out = args[++argidx];
580 continue;
581 }
582 if (args[argidx] == "-icells") {
583 config.icells_mode = true;
584 continue;
585 }
586 if (args[argidx] == "-gates") {
587 config.gates_mode = true;
588 continue;
589 }
590 if (args[argidx] == "-conn") {
591 config.conn_mode = true;
592 continue;
593 }
594 if (args[argidx] == "-cname") {
595 config.cname_mode = true;
596 continue;
597 }
598 if (args[argidx] == "-param") {
599 config.param_mode = true;
600 continue;
601 }
602 if (args[argidx] == "-attr") {
603 config.attr_mode = true;
604 continue;
605 }
606 if (args[argidx] == "-iname") {
607 config.iname_mode = true;
608 continue;
609 }
610 if (args[argidx] == "-iattr") {
611 config.iattr_mode = true;
612 continue;
613 }
614 if (args[argidx] == "-blackbox") {
615 config.blackbox_mode = true;
616 continue;
617 }
618 if (args[argidx] == "-impltf") {
619 config.impltf_mode = true;
620 continue;
621 }
622 if (args[argidx] == "-noalias") {
623 config.noalias_mode = true;
624 continue;
625 }
626 break;
627 }
628 extra_args(f, filename, args, argidx);
629
630 if (top_module_name.empty())
631 for (auto & mod_it:design->modules_)
632 if (mod_it.second->get_bool_attribute("\\top"))
633 top_module_name = mod_it.first.str();
634
635 *f << stringf("# Generated by %s\n", yosys_version_str);
636
637 std::vector<RTLIL::Module*> mod_list;
638
639 design->sort();
640 for (auto module_it : design->modules_)
641 {
642 RTLIL::Module *module = module_it.second;
643 if (module->get_blackbox_attribute() && !config.blackbox_mode)
644 continue;
645
646 if (module->processes.size() != 0)
647 log_error("Found unmapped processes in module %s: unmapped processes are not supported in BLIF backend!\n", RTLIL::id2cstr(module->name));
648 if (module->memories.size() != 0)
649 log_error("Found unmapped memories in module %s: unmapped memories are not supported in BLIF backend!\n", RTLIL::id2cstr(module->name));
650
651 if (module->name == RTLIL::escape_id(top_module_name)) {
652 BlifDumper::dump(*f, module, design, config);
653 top_module_name.clear();
654 continue;
655 }
656
657 mod_list.push_back(module);
658 }
659
660 if (!top_module_name.empty())
661 log_error("Can't find top module `%s'!\n", top_module_name.c_str());
662
663 for (auto module : mod_list)
664 BlifDumper::dump(*f, module, design, config);
665 }
666 } BlifBackend;
667
668 PRIVATE_NAMESPACE_END