2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
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.
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.
20 #include "kernel/rtlil.h"
21 #include "kernel/register.h"
22 #include "kernel/sigtools.h"
23 #include "kernel/celltypes.h"
24 #include "kernel/log.h"
28 PRIVATE_NAMESPACE_BEGIN
34 RTLIL::Module
*module
;
39 dict
<IdString
, shared_str
> idcache
;
40 pool
<shared_str
> used_names
;
41 vector
<shared_str
> strbuf
;
43 pool
<Wire
*> partial_assignment_wires
;
44 dict
<SigBit
, std::pair
<const char*, int>> partial_assignment_bits
;
45 vector
<string
> assignments
;
50 shared_str
s(stringf("_%d", idcounter
++));
51 if (!used_names
.count(s
)) {
58 const char *cid(IdString id
, bool precache
= false)
60 if (!idcache
.count(id
))
62 string name
= stringf("_%s", id
.c_str());
64 if (name
.substr(0, 2) == "_\\")
65 name
= "_" + name
.substr(2);
67 for (auto &c
: name
) {
68 if (c
== '|' || c
== '$' || c
== '_') continue;
69 if (c
>= 'a' && c
<='z') continue;
70 if (c
>= 'A' && c
<='Z') continue;
71 if (c
>= '0' && c
<='9') continue;
72 if (precache
) return nullptr;
79 while (used_names
.count(name
))
87 return idcache
.at(id
).c_str();
90 SmvWorker(RTLIL::Module
*module
, bool verbose
, std::ostream
&f
) :
91 ct(module
->design
), sigmap(module
), module(module
), f(f
), verbose(verbose
), idcounter(0)
93 for (auto mod
: module
->design
->modules())
96 for (auto wire
: module
->wires())
97 cid(wire
->name
, true);
99 for (auto cell
: module
->cells()) {
100 cid(cell
->name
, true);
101 cid(cell
->type
, true);
102 for (auto &conn
: cell
->connections())
103 cid(conn
.first
, true);
107 const char *rvalue(SigSpec sig
, int width
= -1, bool is_signed
= false)
110 int count_chunks
= 0;
113 for (int i
= 0; i
< GetSize(sig
); i
++)
114 if (partial_assignment_bits
.count(sig
[i
]))
117 const auto &bit_a
= partial_assignment_bits
.at(sig
[i
]);
119 while (i
+width
< GetSize(sig
))
121 if (!partial_assignment_bits
.count(sig
[i
+width
]))
124 const auto &bit_b
= partial_assignment_bits
.at(sig
[i
+width
]);
125 if (strcmp(bit_a
.first
, bit_b
.first
))
127 if (bit_a
.second
+width
!= bit_b
.second
)
133 if (i
+width
< GetSize(sig
))
134 s
= stringf("%s :: ", rvalue(sig
.extract(i
+width
, GetSize(sig
)-(width
+i
))));
136 s
+= stringf("%s[%d:%d]", bit_a
.first
, bit_a
.second
+width
-1, bit_a
.second
);
139 s
+= stringf(" :: %s", rvalue(sig
.extract(0, i
)));
142 goto continue_with_resize
;
145 for (auto &c
: sig
.chunks()) {
150 if (c
.offset
!= 0 || c
.width
!= c
.wire
->width
)
151 s
= stringf("%s[%d:%d]", cid(c
.wire
->name
), c
.offset
+c
.width
-1, c
.offset
) + s
;
153 s
= cid(c
.wire
->name
) + s
;
155 string v
= stringf("0ub%d_", c
.width
);
156 for (int i
= c
.width
-1; i
>= 0; i
--)
157 v
+= c
.data
.at(i
) == State::S1
? '1' : '0';
162 continue_with_resize
:;
165 if (GetSize(sig
) > width
)
166 s
= stringf("signed(resize(%s, %d))", s
.c_str(), width
);
168 s
= stringf("resize(signed(%s), %d)", s
.c_str(), width
);
170 s
= stringf("resize(%s, %d)", s
.c_str(), width
);
171 } else if (is_signed
)
172 s
= stringf("signed(%s)", s
.c_str());
173 else if (count_chunks
> 1)
174 s
= stringf("(%s)", s
.c_str());
177 return strbuf
.back().c_str();
180 const char *rvalue_u(SigSpec sig
, int width
= -1)
182 return rvalue(sig
, width
, false);
185 const char *rvalue_s(SigSpec sig
, int width
= -1, bool is_signed
= true)
187 return rvalue(sig
, width
, is_signed
);
190 const char *lvalue(SigSpec sig
)
197 const char *temp_id
= cid();
198 f
<< stringf(" %s : unsigned word[%d]; -- %s\n", temp_id
, GetSize(sig
), log_signal(sig
));
201 for (auto bit
: sig
) {
202 log_assert(bit
.wire
!= nullptr);
203 partial_assignment_wires
.insert(bit
.wire
);
204 partial_assignment_bits
[bit
] = std::pair
<const char*, int>(temp_id
, offset
++);
212 f
<< stringf("MODULE %s\n", cid(module
->name
));
213 f
<< stringf(" VAR\n");
215 for (auto wire
: module
->wires())
217 if (SigSpec(wire
) != sigmap(wire
))
218 partial_assignment_wires
.insert(wire
);
220 f
<< stringf(" %s : unsigned word[%d]; -- %s\n", cid(wire
->name
), wire
->width
, log_id(wire
));
223 for (auto cell
: module
->cells())
225 // FIXME: $slice, $concat, $mem
227 if (cell
->type
.in("$shl", "$shr", "$sshl", "$sshr", "$shift", "$shiftx"))
229 SigSpec sig_a
= cell
->getPort("\\A");
230 SigSpec sig_b
= cell
->getPort("\\B");
232 int width_y
= GetSize(cell
->getPort("\\Y"));
233 int shift_b_width
= GetSize(sig_b
);
234 int width_ay
= std::max(GetSize(sig_a
), width_y
);
235 int width
= width_ay
;
237 for (int i
= 1, j
= 0;; i
<<= 1, j
++)
240 shift_b_width
= std::min(shift_b_width
, j
);
244 bool signed_a
= cell
->getParam("\\A_SIGNED").as_bool();
245 bool signed_b
= cell
->getParam("\\B_SIGNED").as_bool();
246 string op
= cell
->type
.in("$shl", "$sshl") ? "<<" : ">>";
249 if (cell
->type
== "$sshr" && signed_a
)
251 expr_a
= rvalue_s(sig_a
, width
);
252 expr
= stringf("resize(unsigned(%s %s %s), %d)", expr_a
.c_str(), op
.c_str(), rvalue(sig_b
.extract(0, shift_b_width
)), width_y
);
253 if (shift_b_width
< GetSize(sig_b
))
254 expr
= stringf("%s != 0ud%d_0 ? (bool(%s) ? !0ud%d_0 : 0ud%d_0) : %s",
255 rvalue(sig_b
.extract(shift_b_width
, GetSize(sig_b
) - shift_b_width
)), GetSize(sig_b
) - shift_b_width
,
256 rvalue(sig_a
[GetSize(sig_a
)-1]), width_y
, width_y
, expr
.c_str());
258 else if (cell
->type
.in("$shift", "$shiftx") && signed_b
)
260 expr_a
= rvalue_u(sig_a
, width
);
262 const char *b_shr
= rvalue_u(sig_b
);
263 const char *b_shl
= cid();
265 f
<< stringf(" %s : unsigned word[%d]; -- neg(%s)\n", b_shl
, GetSize(sig_b
), log_signal(sig_b
));
266 assignments
.push_back(stringf("%s := unsigned(-%s);", b_shl
, rvalue_s(sig_b
)));
268 string expr_shl
= stringf("resize(%s << %s[%d:0], %d)", expr_a
.c_str(), b_shl
, shift_b_width
-1, width_y
);
269 string expr_shr
= stringf("resize(%s >> %s[%d:0], %d)", expr_a
.c_str(), b_shr
, shift_b_width
-1, width_y
);
271 if (shift_b_width
< GetSize(sig_b
)) {
272 expr_shl
= stringf("%s[%d:%d] != 0ud%d_0 ? 0ud%d_0 : %s", b_shl
, GetSize(sig_b
)-1, shift_b_width
,
273 GetSize(sig_b
)-shift_b_width
, width_y
, expr_shl
.c_str());
274 expr_shr
= stringf("%s[%d:%d] != 0ud%d_0 ? 0ud%d_0 : %s", b_shr
, GetSize(sig_b
)-1, shift_b_width
,
275 GetSize(sig_b
)-shift_b_width
, width_y
, expr_shr
.c_str());
278 expr
= stringf("bool(%s) ? %s : %s", rvalue(sig_b
[GetSize(sig_b
)-1]), expr_shl
.c_str(), expr_shr
.c_str());
282 if (cell
->type
.in("$shift", "$shiftx") || !signed_a
)
283 expr_a
= rvalue_u(sig_a
, width
);
285 expr_a
= stringf("resize(unsigned(%s), %d)", rvalue_s(sig_a
, width_ay
), width
);
287 expr
= stringf("resize(%s %s %s[%d:0], %d)", expr_a
.c_str(), op
.c_str(), rvalue_u(sig_b
), shift_b_width
-1, width_y
);
288 if (shift_b_width
< GetSize(sig_b
))
289 expr
= stringf("%s[%d:%d] != 0ud%d_0 ? 0ud%d_0 : %s", rvalue_u(sig_b
), GetSize(sig_b
)-1, shift_b_width
,
290 GetSize(sig_b
)-shift_b_width
, width_y
, expr
.c_str());
293 assignments
.push_back(stringf("%s := %s;", lvalue(cell
->getPort("\\Y")), expr
.c_str()));
298 if (cell
->type
.in("$not", "$pos", "$neg"))
300 int width
= GetSize(cell
->getPort("\\Y"));
303 if (cell
->type
== "$not") op
= "!";
304 if (cell
->type
== "$pos") op
= "";
305 if (cell
->type
== "$neg") op
= "-";
307 if (cell
->getParam("\\A_SIGNED").as_bool())
309 assignments
.push_back(stringf("%s := unsigned(%s%s);", lvalue(cell
->getPort("\\Y")),
310 op
.c_str(), rvalue_s(cell
->getPort("\\A"), width
)));
314 assignments
.push_back(stringf("%s := %s%s;", lvalue(cell
->getPort("\\Y")),
315 op
.c_str(), rvalue_u(cell
->getPort("\\A"), width
)));
321 if (cell
->type
.in("$add", "$sub", "$mul", "$and", "$or", "$xor", "$xnor"))
323 int width
= GetSize(cell
->getPort("\\Y"));
324 string expr_a
, expr_b
, op
;
326 if (cell
->type
== "$add") op
= "+";
327 if (cell
->type
== "$sub") op
= "-";
328 if (cell
->type
== "$mul") op
= "*";
329 if (cell
->type
== "$and") op
= "&";
330 if (cell
->type
== "$or") op
= "|";
331 if (cell
->type
== "$xor") op
= "xor";
332 if (cell
->type
== "$xnor") op
= "xnor";
334 if (cell
->getParam("\\A_SIGNED").as_bool())
336 assignments
.push_back(stringf("%s := unsigned(%s %s %s);", lvalue(cell
->getPort("\\Y")),
337 rvalue_s(cell
->getPort("\\A"), width
), op
.c_str(), rvalue_s(cell
->getPort("\\B"), width
)));
341 assignments
.push_back(stringf("%s := %s %s %s;", lvalue(cell
->getPort("\\Y")),
342 rvalue_u(cell
->getPort("\\A"), width
), op
.c_str(), rvalue_u(cell
->getPort("\\B"), width
)));
348 if (cell
->type
.in("$div", "$mod"))
350 int width_y
= GetSize(cell
->getPort("\\Y"));
351 int width
= std::max(width_y
, GetSize(cell
->getPort("\\A")));
352 width
= std::max(width
, GetSize(cell
->getPort("\\B")));
353 string expr_a
, expr_b
, op
;
355 if (cell
->type
== "$div") op
= "/";
356 if (cell
->type
== "$mod") op
= "mod";
358 if (cell
->getParam("\\A_SIGNED").as_bool())
360 assignments
.push_back(stringf("%s := resize(unsigned(%s %s %s), %d);", lvalue(cell
->getPort("\\Y")),
361 rvalue_s(cell
->getPort("\\A"), width
), op
.c_str(), rvalue_s(cell
->getPort("\\B"), width
), width_y
));
365 assignments
.push_back(stringf("%s := resize(%s %s %s, %d);", lvalue(cell
->getPort("\\Y")),
366 rvalue_u(cell
->getPort("\\A"), width
), op
.c_str(), rvalue_u(cell
->getPort("\\B"), width
), width_y
));
372 if (cell
->type
.in("$eq", "$ne", "$eqx", "$nex", "$lt", "$le", "$ge", "$gt"))
374 int width
= std::max(GetSize(cell
->getPort("\\A")), GetSize(cell
->getPort("\\B")));
375 string expr_a
, expr_b
, op
;
377 if (cell
->type
== "$eq") op
= "=";
378 if (cell
->type
== "$ne") op
= "!=";
379 if (cell
->type
== "$eqx") op
= "=";
380 if (cell
->type
== "$nex") op
= "!=";
381 if (cell
->type
== "$lt") op
= "<";
382 if (cell
->type
== "$le") op
= "<=";
383 if (cell
->type
== "$ge") op
= ">=";
384 if (cell
->type
== "$gt") op
= ">";
386 if (cell
->getParam("\\A_SIGNED").as_bool())
388 expr_a
= stringf("resize(signed(%s), %d)", rvalue(cell
->getPort("\\A")), width
);
389 expr_b
= stringf("resize(signed(%s), %d)", rvalue(cell
->getPort("\\B")), width
);
393 expr_a
= stringf("resize(%s, %d)", rvalue(cell
->getPort("\\A")), width
);
394 expr_b
= stringf("resize(%s, %d)", rvalue(cell
->getPort("\\B")), width
);
397 assignments
.push_back(stringf("%s := resize(word1(%s %s %s), %d);", lvalue(cell
->getPort("\\Y")),
398 expr_a
.c_str(), op
.c_str(), expr_b
.c_str(), GetSize(cell
->getPort("\\Y"))));
403 if (cell
->type
.in("$reduce_and", "$reduce_or", "$reduce_bool"))
405 int width_a
= GetSize(cell
->getPort("\\A"));
406 int width_y
= GetSize(cell
->getPort("\\Y"));
407 const char *expr_a
= rvalue(cell
->getPort("\\A"));
408 const char *expr_y
= lvalue(cell
->getPort("\\Y"));
411 if (cell
->type
== "$reduce_and") expr
= stringf("%s = !0ub%d_0", expr_a
, width_a
);
412 if (cell
->type
== "$reduce_or") expr
= stringf("%s != 0ub%d_0", expr_a
, width_a
);
413 if (cell
->type
== "$reduce_bool") expr
= stringf("%s != 0ub%d_0", expr_a
, width_a
);
415 assignments
.push_back(stringf("%s := resize(word1(%s), %d);", expr_y
, expr
.c_str(), width_y
));
419 if (cell
->type
.in("$reduce_xor", "$reduce_xnor"))
421 int width_y
= GetSize(cell
->getPort("\\Y"));
422 const char *expr_y
= lvalue(cell
->getPort("\\Y"));
425 for (auto bit
: cell
->getPort("\\A")) {
431 if (cell
->type
== "$reduce_xnor")
432 expr
= "!(" + expr
+ ")";
434 assignments
.push_back(stringf("%s := resize(%s, %d);", expr_y
, expr
.c_str(), width_y
));
438 if (cell
->type
.in("$logic_and", "$logic_or"))
440 int width_a
= GetSize(cell
->getPort("\\A"));
441 int width_b
= GetSize(cell
->getPort("\\B"));
442 int width_y
= GetSize(cell
->getPort("\\Y"));
444 string expr_a
= stringf("(%s != 0ub%d_0)", rvalue(cell
->getPort("\\A")), width_a
);
445 string expr_b
= stringf("(%s != 0ub%d_0)", rvalue(cell
->getPort("\\B")), width_b
);
446 const char *expr_y
= lvalue(cell
->getPort("\\Y"));
449 if (cell
->type
== "$logic_and") expr
= expr_a
+ " & " + expr_b
;
450 if (cell
->type
== "$logic_or") expr
= expr_a
+ " | " + expr_b
;
452 assignments
.push_back(stringf("%s := resize(word1(%s), %d);", expr_y
, expr
.c_str(), width_y
));
456 if (cell
->type
.in("$logic_not"))
458 int width_a
= GetSize(cell
->getPort("\\A"));
459 int width_y
= GetSize(cell
->getPort("\\Y"));
461 string expr_a
= stringf("(%s = 0ub%d_0)", rvalue(cell
->getPort("\\A")), width_a
);
462 const char *expr_y
= lvalue(cell
->getPort("\\Y"));
464 assignments
.push_back(stringf("%s := resize(word1(%s), %d);", expr_y
, expr_a
.c_str(), width_y
));
468 if (cell
->type
.in("$mux", "$pmux"))
470 int width
= GetSize(cell
->getPort("\\Y"));
471 SigSpec sig_a
= cell
->getPort("\\A");
472 SigSpec sig_b
= cell
->getPort("\\B");
473 SigSpec sig_s
= cell
->getPort("\\S");
476 for (int i
= 0; i
< GetSize(sig_s
); i
++)
477 expr
+= stringf("bool(%s) ? %s : ", rvalue(sig_s
[i
]), rvalue(sig_b
.extract(i
*width
, width
)));
478 expr
+= rvalue(sig_a
);
480 assignments
.push_back(stringf("%s := %s;", lvalue(cell
->getPort("\\Y")), expr
.c_str()));
484 if (cell
->type
== "$dff")
486 // FIXME: use init property
487 assignments
.push_back(stringf("next(%s) := %s;", lvalue(cell
->getPort("\\Q")), rvalue(cell
->getPort("\\D"))));
491 if (cell
->type
.in("$_BUF_", "$_NOT_"))
493 string op
= cell
->type
== "$_NOT_" ? "!" : "";
494 assignments
.push_back(stringf("%s := %s%s;", lvalue(cell
->getPort("\\Y")), op
.c_str(), rvalue(cell
->getPort("\\A"))));
498 if (cell
->type
.in("$_AND_", "$_NAND_", "$_OR_", "$_NOR_", "$_XOR_", "$_XNOR_"))
502 if (cell
->type
.in("$_AND_", "$_NAND_")) op
= "&";
503 if (cell
->type
.in("$_OR_", "$_NOR_")) op
= "|";
504 if (cell
->type
.in("$_XOR_")) op
= "xor";
505 if (cell
->type
.in("$_XNOR_")) op
= "xnor";
507 if (cell
->type
.in("$_NAND_", "$_NOR_"))
508 assignments
.push_back(stringf("%s := !(%s %s %s);", lvalue(cell
->getPort("\\Y")),
509 rvalue(cell
->getPort("\\A")), op
.c_str(), rvalue(cell
->getPort("\\B"))));
511 assignments
.push_back(stringf("%s := %s %s %s;", lvalue(cell
->getPort("\\Y")),
512 rvalue(cell
->getPort("\\A")), op
.c_str(), rvalue(cell
->getPort("\\B"))));
516 if (cell
->type
== "$_MUX_")
518 assignments
.push_back(stringf("%s := bool(%s) ? %s : %s;", lvalue(cell
->getPort("\\Y")),
519 rvalue(cell
->getPort("\\S")), rvalue(cell
->getPort("\\B")), rvalue(cell
->getPort("\\A"))));
523 if (cell
->type
== "$_AOI3_")
525 assignments
.push_back(stringf("%s := !((%s & %s) | %s);", lvalue(cell
->getPort("\\Y")),
526 rvalue(cell
->getPort("\\A")), rvalue(cell
->getPort("\\B")), rvalue(cell
->getPort("\\C"))));
530 if (cell
->type
== "$_OAI3_")
532 assignments
.push_back(stringf("%s := !((%s | %s) & %s);", lvalue(cell
->getPort("\\Y")),
533 rvalue(cell
->getPort("\\A")), rvalue(cell
->getPort("\\B")), rvalue(cell
->getPort("\\C"))));
537 if (cell
->type
== "$_AOI4_")
539 assignments
.push_back(stringf("%s := !((%s & %s) | (%s & %s));", lvalue(cell
->getPort("\\Y")),
540 rvalue(cell
->getPort("\\A")), rvalue(cell
->getPort("\\B")), rvalue(cell
->getPort("\\C")), rvalue(cell
->getPort("\\D"))));
544 if (cell
->type
== "$_OAI4_")
546 assignments
.push_back(stringf("%s := !((%s | %s) & (%s | %s));", lvalue(cell
->getPort("\\Y")),
547 rvalue(cell
->getPort("\\A")), rvalue(cell
->getPort("\\B")), rvalue(cell
->getPort("\\C")), rvalue(cell
->getPort("\\D"))));
551 if (cell
->type
[0] == '$')
552 log_error("Found currently unsupported cell type %s (%s.%s).\n", log_id(cell
->type
), log_id(module
), log_id(cell
));
554 f
<< stringf(" %s : %s;\n", cid(cell
->name
), cid(cell
->type
));
556 for (auto &conn
: cell
->connections())
557 if (cell
->output(conn
.first
))
558 assignments
.push_back(stringf("%s := %s.%s;", lvalue(conn
.second
), cid(cell
->name
), cid(conn
.first
)));
560 assignments
.push_back(stringf("%s.%s := %s;", cid(cell
->name
), cid(conn
.first
), rvalue(conn
.second
)));
563 for (Wire
*wire
: partial_assignment_wires
)
567 for (int i
= 0; i
< wire
->width
; i
++)
569 SigBit bit
= sigmap(SigBit(wire
, i
));
572 expr
= " :: " + expr
;
574 if (partial_assignment_bits
.count(bit
))
577 const auto &bit_a
= partial_assignment_bits
.at(bit
);
579 while (i
+1 < wire
->width
)
581 SigBit next_bit
= sigmap(SigBit(wire
, i
+1));
583 if (!partial_assignment_bits
.count(next_bit
))
586 const auto &bit_b
= partial_assignment_bits
.at(next_bit
);
587 if (strcmp(bit_a
.first
, bit_b
.first
))
589 if (bit_a
.second
+width
!= bit_b
.second
)
595 expr
= stringf("%s[%d:%d]", bit_a
.first
, bit_a
.second
+width
-1, bit_a
.second
) + expr
;
597 else if (sigmap(SigBit(wire
, i
)).wire
== nullptr)
600 SigSpec sig
= sigmap(SigSpec(wire
, i
));
602 while (i
+1 < wire
->width
) {
603 SigBit next_bit
= sigmap(SigBit(wire
, i
+1));
604 if (next_bit
.wire
!= nullptr)
606 sig
.append(next_bit
);
610 for (int k
= GetSize(sig
)-1; k
>= 0; k
--)
611 bits
+= sig
[k
] == State::S1
? '1' : '0';
613 expr
= stringf("0ub%d_%s", GetSize(bits
), bits
.c_str()) + expr
;
618 SigSpec sig
= sigmap(SigSpec(wire
, i
));
620 while (i
+1 < wire
->width
) {
621 SigBit next_bit
= sigmap(SigBit(wire
, i
+1));
622 if (next_bit
.wire
== nullptr || partial_assignment_bits
.count(next_bit
))
624 sig
.append(next_bit
);
628 expr
= rvalue(sig
) + expr
;
632 assignments
.push_back(stringf("%s := %s;", cid(wire
->name
), expr
.c_str()));
635 f
<< stringf(" ASSIGN\n");
636 for (const string
&line
: assignments
)
637 f
<< stringf(" %s\n", line
.c_str());
641 struct SmvBackend
: public Backend
{
642 SmvBackend() : Backend("smv", "write design to SMV file") { }
645 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
647 log(" write_smv [options] [filename]\n");
649 log("Write an SMV description of the current design.\n");
652 log(" this will print the recursive walk used to export the modules.\n");
654 log(" -tpl <template_file>\n");
655 log(" use the given template file. the line containing only the token '%%%%'\n");
656 log(" is replaced with the regular output of this command.\n");
658 log("THIS COMMAND IS UNDER CONSTRUCTION\n");
661 virtual void execute(std::ostream
*&f
, std::string filename
, std::vector
<std::string
> args
, RTLIL::Design
*design
)
663 std::ifstream template_f
;
664 bool verbose
= false;
666 log_header("Executing SMV backend.\n");
669 for (argidx
= 1; argidx
< args
.size(); argidx
++)
671 if (args
[argidx
] == "-tpl" && argidx
+1 < args
.size()) {
672 template_f
.open(args
[++argidx
]);
673 if (template_f
.fail())
674 log_error("Can't open template file `%s'.\n", args
[argidx
].c_str());
677 if (args
[argidx
] == "-verbose") {
683 extra_args(f
, filename
, args
, argidx
);
685 pool
<Module
*> modules
;
687 for (auto module
: design
->modules())
688 if (!module
->get_bool_attribute("\\blackbox") && !module
->has_memories_warn() && !module
->has_processes_warn())
689 modules
.insert(module
);
691 if (template_f
.is_open())
694 while (std::getline(template_f
, line
))
697 while (indent
< GetSize(line
) && (line
[indent
] == ' ' || line
[indent
] == '\t'))
700 if (line
[indent
] == '%')
702 vector
<string
> stmt
= split_tokens(line
);
704 if (GetSize(stmt
) == 1 && stmt
[0] == "%%")
707 if (GetSize(stmt
) == 2 && stmt
[0] == "%module")
709 Module
*module
= design
->module(RTLIL::escape_id(stmt
[1]));
710 modules
.erase(module
);
712 if (module
== nullptr)
713 log_error("Module '%s' not found.\n", stmt
[1].c_str());
715 *f
<< stringf("-- SMV description generated by %s\n", yosys_version_str
);
717 log("Creating SMV representation of module %s.\n", log_id(module
));
718 SmvWorker
worker(module
, verbose
, *f
);
721 *f
<< stringf("-- end of yosys output\n");
725 log_error("Unknown template statement: '%s'", line
.c_str() + indent
);
728 *f
<< line
<< std::endl
;
732 if (!modules
.empty())
734 *f
<< stringf("-- SMV description generated by %s\n", yosys_version_str
);
736 for (auto module
: modules
) {
737 log("Creating SMV representation of module %s.\n", log_id(module
));
738 SmvWorker
worker(module
, verbose
, *f
);
742 *f
<< stringf("-- end of yosys output\n");
745 if (template_f
.is_open()) {
747 while (std::getline(template_f
, line
))
748 *f
<< line
<< std::endl
;
753 PRIVATE_NAMESPACE_END