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
));
222 if (wire
->attributes
.count("\\init"))
223 assignments
.push_back(stringf("init(%s) := %s;", lvalue(wire
), rvalue(wire
->attributes
.at("\\init"))));
226 for (auto cell
: module
->cells())
228 // FIXME: $slice, $concat, $mem
230 if (cell
->type
.in("$shl", "$shr", "$sshl", "$sshr", "$shift", "$shiftx"))
232 SigSpec sig_a
= cell
->getPort("\\A");
233 SigSpec sig_b
= cell
->getPort("\\B");
235 int width_y
= GetSize(cell
->getPort("\\Y"));
236 int shift_b_width
= GetSize(sig_b
);
237 int width_ay
= std::max(GetSize(sig_a
), width_y
);
238 int width
= width_ay
;
240 for (int i
= 1, j
= 0;; i
<<= 1, j
++)
243 shift_b_width
= std::min(shift_b_width
, j
);
247 bool signed_a
= cell
->getParam("\\A_SIGNED").as_bool();
248 bool signed_b
= cell
->getParam("\\B_SIGNED").as_bool();
249 string op
= cell
->type
.in("$shl", "$sshl") ? "<<" : ">>";
252 if (cell
->type
== "$sshr" && signed_a
)
254 expr_a
= rvalue_s(sig_a
, width
);
255 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
);
256 if (shift_b_width
< GetSize(sig_b
))
257 expr
= stringf("%s != 0ud%d_0 ? (bool(%s) ? !0ud%d_0 : 0ud%d_0) : %s",
258 rvalue(sig_b
.extract(shift_b_width
, GetSize(sig_b
) - shift_b_width
)), GetSize(sig_b
) - shift_b_width
,
259 rvalue(sig_a
[GetSize(sig_a
)-1]), width_y
, width_y
, expr
.c_str());
261 else if (cell
->type
.in("$shift", "$shiftx") && signed_b
)
263 expr_a
= rvalue_u(sig_a
, width
);
265 const char *b_shr
= rvalue_u(sig_b
);
266 const char *b_shl
= cid();
268 f
<< stringf(" %s : unsigned word[%d]; -- neg(%s)\n", b_shl
, GetSize(sig_b
), log_signal(sig_b
));
269 assignments
.push_back(stringf("%s := unsigned(-%s);", b_shl
, rvalue_s(sig_b
)));
271 string expr_shl
= stringf("resize(%s << %s[%d:0], %d)", expr_a
.c_str(), b_shl
, shift_b_width
-1, width_y
);
272 string expr_shr
= stringf("resize(%s >> %s[%d:0], %d)", expr_a
.c_str(), b_shr
, shift_b_width
-1, width_y
);
274 if (shift_b_width
< GetSize(sig_b
)) {
275 expr_shl
= stringf("%s[%d:%d] != 0ud%d_0 ? 0ud%d_0 : %s", b_shl
, GetSize(sig_b
)-1, shift_b_width
,
276 GetSize(sig_b
)-shift_b_width
, width_y
, expr_shl
.c_str());
277 expr_shr
= stringf("%s[%d:%d] != 0ud%d_0 ? 0ud%d_0 : %s", b_shr
, GetSize(sig_b
)-1, shift_b_width
,
278 GetSize(sig_b
)-shift_b_width
, width_y
, expr_shr
.c_str());
281 expr
= stringf("bool(%s) ? %s : %s", rvalue(sig_b
[GetSize(sig_b
)-1]), expr_shl
.c_str(), expr_shr
.c_str());
285 if (cell
->type
.in("$shift", "$shiftx") || !signed_a
)
286 expr_a
= rvalue_u(sig_a
, width
);
288 expr_a
= stringf("resize(unsigned(%s), %d)", rvalue_s(sig_a
, width_ay
), width
);
290 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
);
291 if (shift_b_width
< GetSize(sig_b
))
292 expr
= stringf("%s[%d:%d] != 0ud%d_0 ? 0ud%d_0 : %s", rvalue_u(sig_b
), GetSize(sig_b
)-1, shift_b_width
,
293 GetSize(sig_b
)-shift_b_width
, width_y
, expr
.c_str());
296 assignments
.push_back(stringf("%s := %s;", lvalue(cell
->getPort("\\Y")), expr
.c_str()));
301 if (cell
->type
.in("$not", "$pos", "$neg"))
303 int width
= GetSize(cell
->getPort("\\Y"));
306 if (cell
->type
== "$not") op
= "!";
307 if (cell
->type
== "$pos") op
= "";
308 if (cell
->type
== "$neg") op
= "-";
310 if (cell
->getParam("\\A_SIGNED").as_bool())
312 assignments
.push_back(stringf("%s := unsigned(%s%s);", lvalue(cell
->getPort("\\Y")),
313 op
.c_str(), rvalue_s(cell
->getPort("\\A"), width
)));
317 assignments
.push_back(stringf("%s := %s%s;", lvalue(cell
->getPort("\\Y")),
318 op
.c_str(), rvalue_u(cell
->getPort("\\A"), width
)));
324 if (cell
->type
.in("$add", "$sub", "$mul", "$and", "$or", "$xor", "$xnor"))
326 int width
= GetSize(cell
->getPort("\\Y"));
327 string expr_a
, expr_b
, op
;
329 if (cell
->type
== "$add") op
= "+";
330 if (cell
->type
== "$sub") op
= "-";
331 if (cell
->type
== "$mul") op
= "*";
332 if (cell
->type
== "$and") op
= "&";
333 if (cell
->type
== "$or") op
= "|";
334 if (cell
->type
== "$xor") op
= "xor";
335 if (cell
->type
== "$xnor") op
= "xnor";
337 if (cell
->getParam("\\A_SIGNED").as_bool())
339 assignments
.push_back(stringf("%s := unsigned(%s %s %s);", lvalue(cell
->getPort("\\Y")),
340 rvalue_s(cell
->getPort("\\A"), width
), op
.c_str(), rvalue_s(cell
->getPort("\\B"), width
)));
344 assignments
.push_back(stringf("%s := %s %s %s;", lvalue(cell
->getPort("\\Y")),
345 rvalue_u(cell
->getPort("\\A"), width
), op
.c_str(), rvalue_u(cell
->getPort("\\B"), width
)));
351 if (cell
->type
.in("$div", "$mod"))
353 int width_y
= GetSize(cell
->getPort("\\Y"));
354 int width
= std::max(width_y
, GetSize(cell
->getPort("\\A")));
355 width
= std::max(width
, GetSize(cell
->getPort("\\B")));
356 string expr_a
, expr_b
, op
;
358 if (cell
->type
== "$div") op
= "/";
359 if (cell
->type
== "$mod") op
= "mod";
361 if (cell
->getParam("\\A_SIGNED").as_bool())
363 assignments
.push_back(stringf("%s := resize(unsigned(%s %s %s), %d);", lvalue(cell
->getPort("\\Y")),
364 rvalue_s(cell
->getPort("\\A"), width
), op
.c_str(), rvalue_s(cell
->getPort("\\B"), width
), width_y
));
368 assignments
.push_back(stringf("%s := resize(%s %s %s, %d);", lvalue(cell
->getPort("\\Y")),
369 rvalue_u(cell
->getPort("\\A"), width
), op
.c_str(), rvalue_u(cell
->getPort("\\B"), width
), width_y
));
375 if (cell
->type
.in("$eq", "$ne", "$eqx", "$nex", "$lt", "$le", "$ge", "$gt"))
377 int width
= std::max(GetSize(cell
->getPort("\\A")), GetSize(cell
->getPort("\\B")));
378 string expr_a
, expr_b
, op
;
380 if (cell
->type
== "$eq") op
= "=";
381 if (cell
->type
== "$ne") op
= "!=";
382 if (cell
->type
== "$eqx") op
= "=";
383 if (cell
->type
== "$nex") op
= "!=";
384 if (cell
->type
== "$lt") op
= "<";
385 if (cell
->type
== "$le") op
= "<=";
386 if (cell
->type
== "$ge") op
= ">=";
387 if (cell
->type
== "$gt") op
= ">";
389 if (cell
->getParam("\\A_SIGNED").as_bool())
391 expr_a
= stringf("resize(signed(%s), %d)", rvalue(cell
->getPort("\\A")), width
);
392 expr_b
= stringf("resize(signed(%s), %d)", rvalue(cell
->getPort("\\B")), width
);
396 expr_a
= stringf("resize(%s, %d)", rvalue(cell
->getPort("\\A")), width
);
397 expr_b
= stringf("resize(%s, %d)", rvalue(cell
->getPort("\\B")), width
);
400 assignments
.push_back(stringf("%s := resize(word1(%s %s %s), %d);", lvalue(cell
->getPort("\\Y")),
401 expr_a
.c_str(), op
.c_str(), expr_b
.c_str(), GetSize(cell
->getPort("\\Y"))));
406 if (cell
->type
.in("$reduce_and", "$reduce_or", "$reduce_bool"))
408 int width_a
= GetSize(cell
->getPort("\\A"));
409 int width_y
= GetSize(cell
->getPort("\\Y"));
410 const char *expr_a
= rvalue(cell
->getPort("\\A"));
411 const char *expr_y
= lvalue(cell
->getPort("\\Y"));
414 if (cell
->type
== "$reduce_and") expr
= stringf("%s = !0ub%d_0", expr_a
, width_a
);
415 if (cell
->type
== "$reduce_or") expr
= stringf("%s != 0ub%d_0", expr_a
, width_a
);
416 if (cell
->type
== "$reduce_bool") expr
= stringf("%s != 0ub%d_0", expr_a
, width_a
);
418 assignments
.push_back(stringf("%s := resize(word1(%s), %d);", expr_y
, expr
.c_str(), width_y
));
422 if (cell
->type
.in("$reduce_xor", "$reduce_xnor"))
424 int width_y
= GetSize(cell
->getPort("\\Y"));
425 const char *expr_y
= lvalue(cell
->getPort("\\Y"));
428 for (auto bit
: cell
->getPort("\\A")) {
434 if (cell
->type
== "$reduce_xnor")
435 expr
= "!(" + expr
+ ")";
437 assignments
.push_back(stringf("%s := resize(%s, %d);", expr_y
, expr
.c_str(), width_y
));
441 if (cell
->type
.in("$logic_and", "$logic_or"))
443 int width_a
= GetSize(cell
->getPort("\\A"));
444 int width_b
= GetSize(cell
->getPort("\\B"));
445 int width_y
= GetSize(cell
->getPort("\\Y"));
447 string expr_a
= stringf("(%s != 0ub%d_0)", rvalue(cell
->getPort("\\A")), width_a
);
448 string expr_b
= stringf("(%s != 0ub%d_0)", rvalue(cell
->getPort("\\B")), width_b
);
449 const char *expr_y
= lvalue(cell
->getPort("\\Y"));
452 if (cell
->type
== "$logic_and") expr
= expr_a
+ " & " + expr_b
;
453 if (cell
->type
== "$logic_or") expr
= expr_a
+ " | " + expr_b
;
455 assignments
.push_back(stringf("%s := resize(word1(%s), %d);", expr_y
, expr
.c_str(), width_y
));
459 if (cell
->type
.in("$logic_not"))
461 int width_a
= GetSize(cell
->getPort("\\A"));
462 int width_y
= GetSize(cell
->getPort("\\Y"));
464 string expr_a
= stringf("(%s = 0ub%d_0)", rvalue(cell
->getPort("\\A")), width_a
);
465 const char *expr_y
= lvalue(cell
->getPort("\\Y"));
467 assignments
.push_back(stringf("%s := resize(word1(%s), %d);", expr_y
, expr_a
.c_str(), width_y
));
471 if (cell
->type
.in("$mux", "$pmux"))
473 int width
= GetSize(cell
->getPort("\\Y"));
474 SigSpec sig_a
= cell
->getPort("\\A");
475 SigSpec sig_b
= cell
->getPort("\\B");
476 SigSpec sig_s
= cell
->getPort("\\S");
479 for (int i
= 0; i
< GetSize(sig_s
); i
++)
480 expr
+= stringf("bool(%s) ? %s : ", rvalue(sig_s
[i
]), rvalue(sig_b
.extract(i
*width
, width
)));
481 expr
+= rvalue(sig_a
);
483 assignments
.push_back(stringf("%s := %s;", lvalue(cell
->getPort("\\Y")), expr
.c_str()));
487 if (cell
->type
== "$dff")
489 assignments
.push_back(stringf("next(%s) := %s;", lvalue(cell
->getPort("\\Q")), rvalue(cell
->getPort("\\D"))));
493 if (cell
->type
.in("$_BUF_", "$_NOT_"))
495 string op
= cell
->type
== "$_NOT_" ? "!" : "";
496 assignments
.push_back(stringf("%s := %s%s;", lvalue(cell
->getPort("\\Y")), op
.c_str(), rvalue(cell
->getPort("\\A"))));
500 if (cell
->type
.in("$_AND_", "$_NAND_", "$_OR_", "$_NOR_", "$_XOR_", "$_XNOR_"))
504 if (cell
->type
.in("$_AND_", "$_NAND_")) op
= "&";
505 if (cell
->type
.in("$_OR_", "$_NOR_")) op
= "|";
506 if (cell
->type
.in("$_XOR_")) op
= "xor";
507 if (cell
->type
.in("$_XNOR_")) op
= "xnor";
509 if (cell
->type
.in("$_NAND_", "$_NOR_"))
510 assignments
.push_back(stringf("%s := !(%s %s %s);", lvalue(cell
->getPort("\\Y")),
511 rvalue(cell
->getPort("\\A")), op
.c_str(), rvalue(cell
->getPort("\\B"))));
513 assignments
.push_back(stringf("%s := %s %s %s;", lvalue(cell
->getPort("\\Y")),
514 rvalue(cell
->getPort("\\A")), op
.c_str(), rvalue(cell
->getPort("\\B"))));
518 if (cell
->type
== "$_MUX_")
520 assignments
.push_back(stringf("%s := bool(%s) ? %s : %s;", lvalue(cell
->getPort("\\Y")),
521 rvalue(cell
->getPort("\\S")), rvalue(cell
->getPort("\\B")), rvalue(cell
->getPort("\\A"))));
525 if (cell
->type
== "$_AOI3_")
527 assignments
.push_back(stringf("%s := !((%s & %s) | %s);", lvalue(cell
->getPort("\\Y")),
528 rvalue(cell
->getPort("\\A")), rvalue(cell
->getPort("\\B")), rvalue(cell
->getPort("\\C"))));
532 if (cell
->type
== "$_OAI3_")
534 assignments
.push_back(stringf("%s := !((%s | %s) & %s);", lvalue(cell
->getPort("\\Y")),
535 rvalue(cell
->getPort("\\A")), rvalue(cell
->getPort("\\B")), rvalue(cell
->getPort("\\C"))));
539 if (cell
->type
== "$_AOI4_")
541 assignments
.push_back(stringf("%s := !((%s & %s) | (%s & %s));", lvalue(cell
->getPort("\\Y")),
542 rvalue(cell
->getPort("\\A")), rvalue(cell
->getPort("\\B")), rvalue(cell
->getPort("\\C")), rvalue(cell
->getPort("\\D"))));
546 if (cell
->type
== "$_OAI4_")
548 assignments
.push_back(stringf("%s := !((%s | %s) & (%s | %s));", lvalue(cell
->getPort("\\Y")),
549 rvalue(cell
->getPort("\\A")), rvalue(cell
->getPort("\\B")), rvalue(cell
->getPort("\\C")), rvalue(cell
->getPort("\\D"))));
553 if (cell
->type
[0] == '$')
554 log_error("Found currently unsupported cell type %s (%s.%s).\n", log_id(cell
->type
), log_id(module
), log_id(cell
));
556 f
<< stringf(" %s : %s;\n", cid(cell
->name
), cid(cell
->type
));
558 for (auto &conn
: cell
->connections())
559 if (cell
->output(conn
.first
))
560 assignments
.push_back(stringf("%s := %s.%s;", lvalue(conn
.second
), cid(cell
->name
), cid(conn
.first
)));
562 assignments
.push_back(stringf("%s.%s := %s;", cid(cell
->name
), cid(conn
.first
), rvalue(conn
.second
)));
565 for (Wire
*wire
: partial_assignment_wires
)
569 for (int i
= 0; i
< wire
->width
; i
++)
571 SigBit bit
= sigmap(SigBit(wire
, i
));
574 expr
= " :: " + expr
;
576 if (partial_assignment_bits
.count(bit
))
579 const auto &bit_a
= partial_assignment_bits
.at(bit
);
581 while (i
+1 < wire
->width
)
583 SigBit next_bit
= sigmap(SigBit(wire
, i
+1));
585 if (!partial_assignment_bits
.count(next_bit
))
588 const auto &bit_b
= partial_assignment_bits
.at(next_bit
);
589 if (strcmp(bit_a
.first
, bit_b
.first
))
591 if (bit_a
.second
+width
!= bit_b
.second
)
597 expr
= stringf("%s[%d:%d]", bit_a
.first
, bit_a
.second
+width
-1, bit_a
.second
) + expr
;
599 else if (sigmap(SigBit(wire
, i
)).wire
== nullptr)
602 SigSpec sig
= sigmap(SigSpec(wire
, i
));
604 while (i
+1 < wire
->width
) {
605 SigBit next_bit
= sigmap(SigBit(wire
, i
+1));
606 if (next_bit
.wire
!= nullptr)
608 sig
.append(next_bit
);
612 for (int k
= GetSize(sig
)-1; k
>= 0; k
--)
613 bits
+= sig
[k
] == State::S1
? '1' : '0';
615 expr
= stringf("0ub%d_%s", GetSize(bits
), bits
.c_str()) + expr
;
620 SigSpec sig
= sigmap(SigSpec(wire
, i
));
622 while (i
+1 < wire
->width
) {
623 SigBit next_bit
= sigmap(SigBit(wire
, i
+1));
624 if (next_bit
.wire
== nullptr || partial_assignment_bits
.count(next_bit
))
626 sig
.append(next_bit
);
630 expr
= rvalue(sig
) + expr
;
634 assignments
.push_back(stringf("%s := %s;", cid(wire
->name
), expr
.c_str()));
637 f
<< stringf(" ASSIGN\n");
638 for (const string
&line
: assignments
)
639 f
<< stringf(" %s\n", line
.c_str());
643 struct SmvBackend
: public Backend
{
644 SmvBackend() : Backend("smv", "write design to SMV file") { }
647 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
649 log(" write_smv [options] [filename]\n");
651 log("Write an SMV description of the current design.\n");
654 log(" this will print the recursive walk used to export the modules.\n");
656 log(" -tpl <template_file>\n");
657 log(" use the given template file. the line containing only the token '%%%%'\n");
658 log(" is replaced with the regular output of this command.\n");
660 log("THIS COMMAND IS UNDER CONSTRUCTION\n");
663 virtual void execute(std::ostream
*&f
, std::string filename
, std::vector
<std::string
> args
, RTLIL::Design
*design
)
665 std::ifstream template_f
;
666 bool verbose
= false;
668 log_header("Executing SMV backend.\n");
671 for (argidx
= 1; argidx
< args
.size(); argidx
++)
673 if (args
[argidx
] == "-tpl" && argidx
+1 < args
.size()) {
674 template_f
.open(args
[++argidx
]);
675 if (template_f
.fail())
676 log_error("Can't open template file `%s'.\n", args
[argidx
].c_str());
679 if (args
[argidx
] == "-verbose") {
685 extra_args(f
, filename
, args
, argidx
);
687 pool
<Module
*> modules
;
689 for (auto module
: design
->modules())
690 if (!module
->get_bool_attribute("\\blackbox") && !module
->has_memories_warn() && !module
->has_processes_warn())
691 modules
.insert(module
);
693 if (template_f
.is_open())
696 while (std::getline(template_f
, line
))
699 while (indent
< GetSize(line
) && (line
[indent
] == ' ' || line
[indent
] == '\t'))
702 if (line
[indent
] == '%')
704 vector
<string
> stmt
= split_tokens(line
);
706 if (GetSize(stmt
) == 1 && stmt
[0] == "%%")
709 if (GetSize(stmt
) == 2 && stmt
[0] == "%module")
711 Module
*module
= design
->module(RTLIL::escape_id(stmt
[1]));
712 modules
.erase(module
);
714 if (module
== nullptr)
715 log_error("Module '%s' not found.\n", stmt
[1].c_str());
717 *f
<< stringf("-- SMV description generated by %s\n", yosys_version_str
);
719 log("Creating SMV representation of module %s.\n", log_id(module
));
720 SmvWorker
worker(module
, verbose
, *f
);
723 *f
<< stringf("-- end of yosys output\n");
727 log_error("Unknown template statement: '%s'", line
.c_str() + indent
);
730 *f
<< line
<< std::endl
;
734 if (!modules
.empty())
736 *f
<< stringf("-- SMV description generated by %s\n", yosys_version_str
);
738 for (auto module
: modules
) {
739 log("Creating SMV representation of module %s.\n", log_id(module
));
740 SmvWorker
worker(module
, verbose
, *f
);
744 *f
<< stringf("-- end of yosys output\n");
747 if (template_f
.is_open()) {
749 while (std::getline(template_f
, line
))
750 *f
<< line
<< std::endl
;
755 PRIVATE_NAMESPACE_END