66ab3878e9f4c9181571c9eb5a5d9858bdf0896f
2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5 * Copyright (C) 2019 Eddie Hung <eddie@fpgeh.com>
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include "kernel/yosys.h"
22 #include "kernel/sigtools.h"
25 PRIVATE_NAMESPACE_BEGIN
27 void aiger_encode(std::ostream
&f
, int x
)
32 f
.put((x
& 0x7f) | 0x80);
45 dict
<SigBit
, bool> init_map
;
46 pool
<SigBit
> input_bits
, output_bits
;
47 dict
<SigBit
, SigBit
> not_map
, ff_map
, alias_map
;
48 dict
<SigBit
, pair
<SigBit
, SigBit
>> and_map
;
49 pool
<SigBit
> initstate_bits
;
50 vector
<std::pair
<SigBit
,int>> ci_bits
, co_bits
;
51 dict
<IdString
, unsigned> type_map
;
53 vector
<pair
<int, int>> aig_gates
;
54 vector
<int> aig_latchin
, aig_latchinit
, aig_outputs
;
55 int aig_m
= 0, aig_i
= 0, aig_l
= 0, aig_o
= 0, aig_a
= 0;
57 dict
<SigBit
, int> aig_map
;
58 dict
<SigBit
, int> ordered_outputs
;
59 dict
<SigBit
, int> ordered_latches
;
61 dict
<SigBit
, int> init_inputs
;
64 vector
<Cell
*> box_list
;
66 int mkgate(int a0
, int a1
)
69 aig_gates
.push_back(a0
> a1
? make_pair(a0
, a1
) : make_pair(a1
, a0
));
73 int bit2aig(SigBit bit
)
75 if (aig_map
.count(bit
) == 0)
79 if (initstate_bits
.count(bit
)) {
80 log_assert(initstate_ff
> 0);
81 aig_map
[bit
] = initstate_ff
;
83 if (not_map
.count(bit
)) {
84 int a
= bit2aig(not_map
.at(bit
)) ^ 1;
87 if (and_map
.count(bit
)) {
88 auto args
= and_map
.at(bit
);
89 int a0
= bit2aig(args
.first
);
90 int a1
= bit2aig(args
.second
);
91 aig_map
[bit
] = mkgate(a0
, a1
);
93 if (alias_map
.count(bit
)) {
94 aig_map
[bit
] = bit2aig(alias_map
.at(bit
));
97 if (bit
== State::Sx
|| bit
== State::Sz
)
98 log_error("Design contains 'x' or 'z' bits. Use 'setundef' to replace those constants.\n");
101 log_assert(aig_map
.at(bit
) >= 0);
102 return aig_map
.at(bit
);
105 XAigerWriter(Module
*module
, bool zinit_mode
, bool imode
, bool omode
, bool bmode
) : module(module
), zinit_mode(zinit_mode
), sigmap(module
)
107 pool
<SigBit
> undriven_bits
;
108 pool
<SigBit
> unused_bits
;
110 // promote public wires
111 for (auto wire
: module
->wires())
112 if (wire
->name
[0] == '\\')
115 // promote input wires
116 for (auto wire
: module
->wires())
117 if (wire
->port_input
)
120 // promote output wires
121 for (auto wire
: module
->wires())
122 if (wire
->port_output
)
125 for (auto wire
: module
->wires())
127 if (wire
->attributes
.count("\\init")) {
128 SigSpec initsig
= sigmap(wire
);
129 Const initval
= wire
->attributes
.at("\\init");
130 for (int i
= 0; i
< GetSize(wire
) && i
< GetSize(initval
); i
++)
131 if (initval
[i
] == State::S0
|| initval
[i
] == State::S1
)
132 init_map
[initsig
[i
]] = initval
[i
] == State::S1
;
135 for (int i
= 0; i
< GetSize(wire
); i
++)
137 SigBit
wirebit(wire
, i
);
138 SigBit bit
= sigmap(wirebit
);
140 if (bit
.wire
== nullptr) {
141 if (wire
->port_output
) {
142 aig_map
[wirebit
] = (bit
== State::S1
) ? 1 : 0;
143 output_bits
.insert(wirebit
);
148 undriven_bits
.insert(bit
);
149 unused_bits
.insert(bit
);
151 if (wire
->port_input
)
152 input_bits
.insert(bit
);
154 if (wire
->port_output
) {
156 alias_map
[wirebit
] = bit
;
157 output_bits
.insert(wirebit
);
162 for (auto bit
: input_bits
) {
163 if (!bit
.wire
->port_output
)
164 undriven_bits
.erase(bit
);
165 // Erase POs that are also PIs
166 output_bits
.erase(bit
);
169 for (auto bit
: output_bits
)
170 if (!bit
.wire
->port_input
)
171 unused_bits
.erase(bit
);
173 for (auto cell
: module
->cells())
175 if (cell
->type
== "$_NOT_")
177 SigBit A
= sigmap(cell
->getPort("\\A").as_bit());
178 SigBit Y
= sigmap(cell
->getPort("\\Y").as_bit());
179 unused_bits
.erase(A
);
180 undriven_bits
.erase(Y
);
185 //if (cell->type.in("$_FF_", "$_DFF_N_", "$_DFF_P_"))
187 // SigBit D = sigmap(cell->getPort("\\D").as_bit());
188 // SigBit Q = sigmap(cell->getPort("\\Q").as_bit());
189 // unused_bits.erase(D);
190 // undriven_bits.erase(Q);
195 if (cell
->type
== "$_AND_")
197 SigBit A
= sigmap(cell
->getPort("\\A").as_bit());
198 SigBit B
= sigmap(cell
->getPort("\\B").as_bit());
199 SigBit Y
= sigmap(cell
->getPort("\\Y").as_bit());
200 unused_bits
.erase(A
);
201 unused_bits
.erase(B
);
202 undriven_bits
.erase(Y
);
203 and_map
[Y
] = make_pair(A
, B
);
207 if (cell
->type
== "$initstate")
209 SigBit Y
= sigmap(cell
->getPort("\\Y").as_bit());
210 undriven_bits
.erase(Y
);
211 initstate_bits
.insert(Y
);
215 RTLIL::Module
* box_module
= module
->design
->module(cell
->type
);
216 bool abc_box
= box_module
&& box_module
->attributes
.count("\\abc_box_id");
218 for (const auto &c
: cell
->connections()) {
219 /*if (c.second.is_fully_const()) continue;*/
220 for (auto b
: c
.second
.bits()) {
221 auto is_input
= cell
->input(c
.first
);
222 auto is_output
= cell
->output(c
.first
);
223 log_assert(is_input
|| is_output
);
225 /*if (!w->port_input)*/ {
226 SigBit I
= sigmap(b
);
229 /*if (!output_bits.count(b))*/
231 co_bits
.emplace_back(b
, 0);
233 output_bits
.insert(b
);
234 if (!b
.wire
->port_input
)
235 unused_bits
.erase(b
);
240 SigBit O
= sigmap(b
);
241 /*if (!input_bits.count(O))*/
243 ci_bits
.emplace_back(O
, 0);
245 input_bits
.insert(O
);
246 if (!O
.wire
->port_output
)
247 undriven_bits
.erase(O
);
251 if (!type_map
.count(cell
->type
))
252 type_map
[cell
->type
] = type_map
.size()+1;
256 box_list
.emplace_back(cell
);
257 //log_warning("Unsupported cell type: %s (%s)\n", log_id(cell->type), log_id(cell));
260 for (auto bit
: input_bits
) {
261 RTLIL::Wire
*wire
= bit
.wire
;
262 // If encountering an inout port, then create a new wire with $inout.out
263 // suffix, make it a PO driven by the existing inout, and inherit existing
265 if (wire
->port_input
&& wire
->port_output
&& !undriven_bits
.count(bit
)) {
266 RTLIL::Wire
*new_wire
= module
->wire(wire
->name
.str() + "$inout.out");
268 new_wire
= module
->addWire(wire
->name
.str() + "$inout.out", GetSize(wire
));
269 SigBit
new_bit(new_wire
, bit
.offset
);
270 module
->connect(new_bit
, bit
);
271 if (not_map
.count(bit
))
272 not_map
[new_bit
] = not_map
.at(bit
);
273 else if (and_map
.count(bit
))
274 and_map
[new_bit
] = and_map
.at(bit
);
275 else if (alias_map
.count(bit
))
276 alias_map
[new_bit
] = alias_map
.at(bit
);
277 output_bits
.insert(new_bit
);
281 // Do some CI/CO post-processing:
282 // Erase all POs and COs that are undriven
283 for (auto bit
: undriven_bits
) {
284 //co_bits.erase(bit);
285 output_bits
.erase(bit
);
287 // Erase all CIs that are also COs
288 //for (auto bit : co_bits)
289 // ci_bits.erase(bit);
290 // CIs cannot be undriven
291 for (const auto &c
: ci_bits
)
292 undriven_bits
.erase(c
.first
);
294 for (auto bit
: unused_bits
)
295 undriven_bits
.erase(bit
);
297 if (!undriven_bits
.empty()) {
298 undriven_bits
.sort();
299 for (auto bit
: undriven_bits
) {
300 log_warning("Treating undriven bit %s.%s like $anyseq.\n", log_id(module
), log_signal(bit
));
301 input_bits
.insert(bit
);
303 log_warning("Treating a total of %d undriven bits in %s like $anyseq.\n", GetSize(undriven_bits
), log_id(module
));
313 aig_map
[State::S0
] = 0;
314 aig_map
[State::S1
] = 1;
316 for (auto &c
: ci_bits
) {
319 aig_map
[c
.first
] = c
.second
;
322 for (auto bit
: input_bits
) {
324 aig_map
[bit
] = 2*aig_m
;
327 if (imode
&& input_bits
.empty()) {
333 for (auto it
: ff_map
) {
334 if (init_map
.count(it
.first
))
337 init_inputs
[it
.first
] = 2*aig_m
;
341 for (auto it
: ff_map
) {
343 aig_map
[it
.first
] = 2*aig_m
;
344 ordered_latches
[it
.first
] = aig_l
-1;
345 if (init_map
.count(it
.first
) == 0)
346 aig_latchinit
.push_back(2);
348 aig_latchinit
.push_back(init_map
.at(it
.first
) ? 1 : 0);
351 if (!initstate_bits
.empty() || !init_inputs
.empty()) {
353 initstate_ff
= 2*aig_m
+1;
354 aig_latchinit
.push_back(0);
359 for (auto it
: ff_map
)
361 int l
= ordered_latches
[it
.first
];
363 if (aig_latchinit
.at(l
) == 1)
364 aig_map
[it
.first
] ^= 1;
366 if (aig_latchinit
.at(l
) == 2)
368 int gated_ffout
= mkgate(aig_map
[it
.first
], initstate_ff
^1);
369 int gated_initin
= mkgate(init_inputs
[it
.first
], initstate_ff
);
370 aig_map
[it
.first
] = mkgate(gated_ffout
^1, gated_initin
^1)^1;
375 for (auto it
: ff_map
) {
376 int a
= bit2aig(it
.second
);
377 int l
= ordered_latches
[it
.first
];
378 if (zinit_mode
&& aig_latchinit
.at(l
) == 1)
379 aig_latchin
.push_back(a
^ 1);
381 aig_latchin
.push_back(a
);
384 if (!initstate_bits
.empty() || !init_inputs
.empty())
385 aig_latchin
.push_back(1);
387 for (auto &c
: co_bits
) {
388 RTLIL::SigBit bit
= c
.first
;
390 ordered_outputs
[bit
] = c
.second
;
391 aig_outputs
.push_back(bit2aig(bit
));
394 for (auto bit
: output_bits
) {
395 ordered_outputs
[bit
] = aig_o
++;
396 aig_outputs
.push_back(bit2aig(bit
));
399 if (omode
&& output_bits
.empty()) {
401 aig_outputs
.push_back(0);
406 aig_outputs
.push_back(0);
410 void write_aiger(std::ostream
&f
, bool ascii_mode
, bool miter_mode
, bool symbols_mode
, bool omode
)
413 int aig_obcj
= aig_obc
;
414 int aig_obcjf
= aig_obcj
;
416 log_assert(aig_m
== aig_i
+ aig_l
+ aig_a
);
417 log_assert(aig_l
== GetSize(aig_latchin
));
418 log_assert(aig_l
== GetSize(aig_latchinit
));
419 log_assert(aig_obcjf
== GetSize(aig_outputs
));
421 f
<< stringf("%s %d %d %d %d %d", ascii_mode
? "aag" : "aig", aig_m
, aig_i
, aig_l
, aig_o
, aig_a
);
426 for (int i
= 0; i
< aig_i
; i
++)
427 f
<< stringf("%d\n", 2*i
+2);
429 for (int i
= 0; i
< aig_l
; i
++) {
430 if (zinit_mode
|| aig_latchinit
.at(i
) == 0)
431 f
<< stringf("%d %d\n", 2*(aig_i
+i
)+2, aig_latchin
.at(i
));
432 else if (aig_latchinit
.at(i
) == 1)
433 f
<< stringf("%d %d 1\n", 2*(aig_i
+i
)+2, aig_latchin
.at(i
));
434 else if (aig_latchinit
.at(i
) == 2)
435 f
<< stringf("%d %d %d\n", 2*(aig_i
+i
)+2, aig_latchin
.at(i
), 2*(aig_i
+i
)+2);
438 for (int i
= 0; i
< aig_obc
; i
++)
439 f
<< stringf("%d\n", aig_outputs
.at(i
));
441 for (int i
= aig_obc
; i
< aig_obcj
; i
++)
444 for (int i
= aig_obc
; i
< aig_obcj
; i
++)
445 f
<< stringf("%d\n", aig_outputs
.at(i
));
447 for (int i
= aig_obcj
; i
< aig_obcjf
; i
++)
448 f
<< stringf("%d\n", aig_outputs
.at(i
));
450 for (int i
= 0; i
< aig_a
; i
++)
451 f
<< stringf("%d %d %d\n", 2*(aig_i
+aig_l
+i
)+2, aig_gates
.at(i
).first
, aig_gates
.at(i
).second
);
455 for (int i
= 0; i
< aig_l
; i
++) {
456 if (zinit_mode
|| aig_latchinit
.at(i
) == 0)
457 f
<< stringf("%d\n", aig_latchin
.at(i
));
458 else if (aig_latchinit
.at(i
) == 1)
459 f
<< stringf("%d 1\n", aig_latchin
.at(i
));
460 else if (aig_latchinit
.at(i
) == 2)
461 f
<< stringf("%d %d\n", aig_latchin
.at(i
), 2*(aig_i
+i
)+2);
464 for (int i
= 0; i
< aig_obc
; i
++)
465 f
<< stringf("%d\n", aig_outputs
.at(i
));
467 for (int i
= aig_obc
; i
< aig_obcj
; i
++)
470 for (int i
= aig_obc
; i
< aig_obcj
; i
++)
471 f
<< stringf("%d\n", aig_outputs
.at(i
));
473 for (int i
= aig_obcj
; i
< aig_obcjf
; i
++)
474 f
<< stringf("%d\n", aig_outputs
.at(i
));
476 for (int i
= 0; i
< aig_a
; i
++) {
477 int lhs
= 2*(aig_i
+aig_l
+i
)+2;
478 int rhs0
= aig_gates
.at(i
).first
;
479 int rhs1
= aig_gates
.at(i
).second
;
480 int delta0
= lhs
- rhs0
;
481 int delta1
= rhs0
- rhs1
;
482 aiger_encode(f
, delta0
);
483 aiger_encode(f
, delta1
);
489 dict
<string
, vector
<string
>> symbols
;
491 bool output_seen
= false;
492 for (auto wire
: module
->wires())
494 //if (wire->name[0] == '$')
497 SigSpec sig
= sigmap(wire
);
499 for (int i
= 0; i
< GetSize(wire
); i
++)
501 RTLIL::SigBit
b(wire
, i
);
502 if (input_bits
.count(b
)) {
503 int a
= aig_map
.at(sig
[i
]);
504 log_assert((a
& 1) == 0);
505 if (GetSize(wire
) != 1)
506 symbols
[stringf("i%d", (a
>> 1)-1)].push_back(stringf("%s[%d]", log_id(wire
), i
));
508 symbols
[stringf("i%d", (a
>> 1)-1)].push_back(stringf("%s", log_id(wire
)));
511 if (output_bits
.count(b
)) {
512 int o
= ordered_outputs
.at(b
);
513 output_seen
= !miter_mode
;
514 if (GetSize(wire
) != 1)
515 symbols
[stringf("%c%d", miter_mode
? 'b' : 'o', o
)].push_back(stringf("%s[%d]", log_id(wire
), i
));
517 symbols
[stringf("%c%d", miter_mode
? 'b' : 'o', o
)].push_back(stringf("%s", log_id(wire
)));
520 if (init_inputs
.count(sig
[i
])) {
521 int a
= init_inputs
.at(sig
[i
]);
522 log_assert((a
& 1) == 0);
523 if (GetSize(wire
) != 1)
524 symbols
[stringf("i%d", (a
>> 1)-1)].push_back(stringf("init:%s[%d]", log_id(wire
), i
));
526 symbols
[stringf("i%d", (a
>> 1)-1)].push_back(stringf("init:%s", log_id(wire
)));
529 if (ordered_latches
.count(sig
[i
])) {
530 int l
= ordered_latches
.at(sig
[i
]);
531 const char *p
= (zinit_mode
&& (aig_latchinit
.at(l
) == 1)) ? "!" : "";
532 if (GetSize(wire
) != 1)
533 symbols
[stringf("l%d", l
)].push_back(stringf("%s%s[%d]", p
, log_id(wire
), i
));
535 symbols
[stringf("l%d", l
)].push_back(stringf("%s%s", p
, log_id(wire
)));
540 if (omode
&& !output_seen
)
541 symbols
["o0"].push_back("__dummy_o__");
545 for (auto &sym
: symbols
) {
547 std::sort(sym
.second
.begin(), sym
.second
.end());
548 for (auto &s
: sym
.second
)
556 if (!box_list
.empty()) {
557 std::stringstream h_buffer
;
558 auto write_h_buffer
= [&h_buffer
](int i32
) {
559 // TODO: Don't assume we're on little endian
561 int i32_be
= _byteswap_ulong(i32
);
563 int i32_be
= __builtin_bswap32(i32
);
565 h_buffer
.write(reinterpret_cast<const char*>(&i32_be
), sizeof(i32_be
));
567 int num_outputs
= output_bits
.size();
568 if (omode
&& num_outputs
== 0)
571 write_h_buffer(input_bits
.size() + ci_bits
.size());
572 write_h_buffer(num_outputs
+ co_bits
.size());
573 write_h_buffer(input_bits
.size());
574 write_h_buffer(num_outputs
);
575 write_h_buffer(box_list
.size());
577 RTLIL::Module
*holes_module
= nullptr;
578 holes_module
= module
->design
->addModule("\\__holes__");
580 for (auto cell
: box_list
) {
581 int box_inputs
= 0, box_outputs
= 0;
582 int box_id
= module
->design
->module(cell
->type
)->attributes
.at("\\abc_box_id").as_int();
583 Cell
*holes_cell
= nullptr;
584 if (holes_module
&& !holes_module
->cell(stringf("\\u%d", box_id
)))
585 holes_cell
= holes_module
->addCell(stringf("\\u%d", box_id
), cell
->type
);
586 RTLIL::Wire
*holes_wire
;
588 for (const auto &c
: cell
->connections()) {
589 if (cell
->input(c
.first
)) {
590 box_inputs
+= c
.second
.size();
592 holes_wire
= holes_module
->wire(stringf("\\i%d", num_inputs
));
594 holes_wire
= holes_module
->addWire(stringf("\\i%d", num_inputs
));
595 holes_wire
->port_input
= true;
598 holes_cell
->setPort(c
.first
, holes_wire
);
601 if (cell
->output(c
.first
)) {
602 box_outputs
+= c
.second
.size();
604 holes_wire
= holes_module
->addWire(stringf("\\%s.%s", cell
->type
.c_str(), c
.first
.c_str()));
605 holes_wire
->port_output
= true;
606 holes_cell
->setPort(c
.first
, holes_wire
);
610 write_h_buffer(box_inputs
);
611 write_h_buffer(box_outputs
);
612 write_h_buffer(box_id
);
613 write_h_buffer(0 /* OldBoxNum */);
617 std::string buffer_str
= h_buffer
.str();
618 // TODO: Don't assume we're on little endian
620 int buffer_size_be
= _byteswap_ulong(buffer_str
.size());
622 int buffer_size_be
= __builtin_bswap32(buffer_str
.size());
624 f
.write(reinterpret_cast<const char*>(&buffer_size_be
), sizeof(buffer_size_be
));
625 f
.write(buffer_str
.data(), buffer_str
.size());
628 holes_module
->fixup_ports();
630 holes_module
->design
->selection_stack
.emplace_back(false);
631 RTLIL::Selection
& sel
= holes_module
->design
->selection_stack
.back();
632 sel
.select(holes_module
);
634 Pass::call(holes_module
->design
, "flatten; aigmap");
636 holes_module
->design
->selection_stack
.pop_back();
638 std::stringstream a_buffer
;
639 XAigerWriter
writer(holes_module
, false /*zinit_mode*/, false /*imode*/, false /*omode*/, false /*bmode*/);
640 writer
.write_aiger(a_buffer
, false /*ascii_mode*/, false /*miter_mode*/, false /*symbols_mode*/, false /*omode*/);
643 std::string buffer_str
= a_buffer
.str();
644 // TODO: Don't assume we're on little endian
646 int buffer_size_be
= _byteswap_ulong(buffer_str
.size());
648 int buffer_size_be
= __builtin_bswap32(buffer_str
.size());
650 f
.write(reinterpret_cast<const char*>(&buffer_size_be
), sizeof(buffer_size_be
));
651 f
.write(buffer_str
.data(), buffer_str
.size());
652 holes_module
->design
->remove(holes_module
);
656 f
<< stringf("Generated by %s\n", yosys_version_str
);
659 void write_map(std::ostream
&f
, bool verbose_map
, bool omode
)
661 dict
<int, string
> input_lines
;
662 dict
<int, string
> init_lines
;
663 dict
<int, string
> output_lines
;
664 dict
<int, string
> latch_lines
;
665 dict
<int, string
> wire_lines
;
667 for (auto wire
: module
->wires())
669 //if (!verbose_map && wire->name[0] == '$')
672 SigSpec sig
= sigmap(wire
);
674 for (int i
= 0; i
< GetSize(wire
); i
++)
676 RTLIL::SigBit
b(wire
, i
);
677 if (input_bits
.count(b
)) {
678 int a
= aig_map
.at(sig
[i
]);
679 log_assert((a
& 1) == 0);
680 input_lines
[a
] += stringf("input %d %d %s\n", (a
>> 1)-1, i
, log_id(wire
));
683 if (output_bits
.count(b
)) {
684 int o
= ordered_outputs
.at(b
);
685 output_lines
[o
] += stringf("output %d %d %s\n", o
, i
, log_id(wire
));
689 if (init_inputs
.count(sig
[i
])) {
690 int a
= init_inputs
.at(sig
[i
]);
691 log_assert((a
& 1) == 0);
692 init_lines
[a
] += stringf("init %d %d %s\n", (a
>> 1)-1, i
, log_id(wire
));
696 if (ordered_latches
.count(sig
[i
])) {
697 int l
= ordered_latches
.at(sig
[i
]);
698 if (zinit_mode
&& (aig_latchinit
.at(l
) == 1))
699 latch_lines
[l
] += stringf("invlatch %d %d %s\n", l
, i
, log_id(wire
));
701 latch_lines
[l
] += stringf("latch %d %d %s\n", l
, i
, log_id(wire
));
706 if (aig_map
.count(sig
[i
]) == 0)
709 int a
= aig_map
.at(sig
[i
]);
710 wire_lines
[a
] += stringf("wire %d %d %s\n", a
, i
, log_id(wire
));
715 for (const auto &c
: ci_bits
) {
716 RTLIL::SigBit b
= c
.first
;
717 RTLIL::Wire
*wire
= b
.wire
;
720 log_assert((a
& 1) == 0);
721 input_lines
[a
] += stringf("input %d %d %s\n", (a
>> 1)-1, i
, log_id(wire
));
724 for (const auto &c
: co_bits
) {
725 RTLIL::SigBit b
= c
.first
;
726 RTLIL::Wire
*wire
= b
.wire
;
729 output_lines
[o
] += stringf("output %d %d %s\n", o
, b
.offset
, log_id(wire
));
731 output_lines
[o
] += stringf("output %d %d __const%d__\n", o
, 0, b
.data
);
735 for (auto &it
: input_lines
)
737 log_assert(input_lines
.size() == input_bits
.size() + ci_bits
.size());
740 for (auto &it
: init_lines
)
744 for (auto &it
: output_lines
)
746 log_assert(output_lines
.size() == output_bits
.size() + co_bits
.size());
747 if (omode
&& output_bits
.empty())
748 f
<< "output " << output_lines
.size() << " 0 __dummy_o__\n";
751 for (auto &it
: latch_lines
)
755 for (auto &it
: wire_lines
)
760 struct XAigerBackend
: public Backend
{
761 XAigerBackend() : Backend("xaiger", "write design to XAIGER file") { }
762 void help() YS_OVERRIDE
764 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
766 log(" write_xaiger [options] [filename]\n");
768 log("Write the current design to an XAIGER file. The design must be flattened and\n");
769 log("all unsupported cells will be converted into psuedo-inputs and pseudo-outputs.\n");
772 log(" write ASCII version of AGIER format\n");
775 log(" convert FFs to zero-initialized FFs, adding additional inputs for\n");
776 log(" uninitialized FFs.\n");
779 log(" include a symbol table in the generated AIGER file\n");
781 log(" -map <filename>\n");
782 log(" write an extra file with port and latch symbols\n");
784 log(" -vmap <filename>\n");
785 log(" like -map, but more verbose\n");
787 log(" -I, -O, -B\n");
788 log(" If the design contains no input/output/assert then create one\n");
789 log(" dummy input/output/bad_state pin to make the tools reading the\n");
790 log(" AIGER file happy.\n");
793 void execute(std::ostream
*&f
, std::string filename
, std::vector
<std::string
> args
, RTLIL::Design
*design
) YS_OVERRIDE
795 bool ascii_mode
= false;
796 bool zinit_mode
= false;
797 bool miter_mode
= false;
798 bool symbols_mode
= false;
799 bool verbose_map
= false;
803 std::string map_filename
;
805 log_header(design
, "Executing XAIGER backend.\n");
808 for (argidx
= 1; argidx
< args
.size(); argidx
++)
810 if (args
[argidx
] == "-ascii") {
814 if (args
[argidx
] == "-zinit") {
818 if (args
[argidx
] == "-symbols") {
822 if (map_filename
.empty() && args
[argidx
] == "-map" && argidx
+1 < args
.size()) {
823 map_filename
= args
[++argidx
];
826 if (map_filename
.empty() && args
[argidx
] == "-vmap" && argidx
+1 < args
.size()) {
827 map_filename
= args
[++argidx
];
831 if (args
[argidx
] == "-I") {
835 if (args
[argidx
] == "-O") {
839 if (args
[argidx
] == "-B") {
845 extra_args(f
, filename
, args
, argidx
);
847 Module
*top_module
= design
->top_module();
849 if (top_module
== nullptr)
850 log_error("Can't find top module in current design!\n");
852 XAigerWriter
writer(top_module
, zinit_mode
, imode
, omode
, bmode
);
853 writer
.write_aiger(*f
, ascii_mode
, miter_mode
, symbols_mode
, omode
);
855 if (!map_filename
.empty()) {
857 mapf
.open(map_filename
.c_str(), std::ofstream::trunc
);
859 log_error("Can't open file `%s' for writing: %s\n", map_filename
.c_str(), strerror(errno
));
860 writer
.write_map(mapf
, verbose_map
, omode
);
865 PRIVATE_NAMESPACE_END