2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5 * 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 // https://stackoverflow.com/a/46137633
24 #define __builtin_bswap32 _byteswap_ulong
25 #elif defined(__APPLE__)
26 #include <libkern/OSByteOrder.h>
27 #define __builtin_bswap32 OSSwapInt32
30 #include "kernel/yosys.h"
31 #include "kernel/sigtools.h"
32 #include "kernel/utils.h"
35 PRIVATE_NAMESPACE_BEGIN
37 inline int32_t to_big_endian(int32_t i32
) {
38 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
39 return __builtin_bswap32(i32
);
40 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
43 #error "Unknown endianness"
47 void aiger_encode(std::ostream
&f
, int x
)
52 f
.put((x
& 0x7f) | 0x80);
64 pool
<SigBit
> input_bits
, output_bits
;
65 dict
<SigBit
, SigBit
> not_map
, alias_map
;
66 dict
<SigBit
, pair
<SigBit
, SigBit
>> and_map
;
67 vector
<std::tuple
<SigBit
,RTLIL::Cell
*,RTLIL::IdString
,int>> ci_bits
;
68 vector
<std::tuple
<SigBit
,RTLIL::Cell
*,RTLIL::IdString
,int,int>> co_bits
;
70 vector
<pair
<int, int>> aig_gates
;
71 vector
<int> aig_outputs
;
72 int aig_m
= 0, aig_i
= 0, aig_l
= 0, aig_o
= 0, aig_a
= 0;
74 dict
<SigBit
, int> aig_map
;
75 dict
<SigBit
, int> ordered_outputs
;
77 vector
<Cell
*> box_list
;
80 int mkgate(int a0
, int a1
)
83 aig_gates
.push_back(a0
> a1
? make_pair(a0
, a1
) : make_pair(a1
, a0
));
87 int bit2aig(SigBit bit
)
89 if (aig_map
.count(bit
) == 0)
93 if (not_map
.count(bit
)) {
94 int a
= bit2aig(not_map
.at(bit
)) ^ 1;
97 if (and_map
.count(bit
)) {
98 auto args
= and_map
.at(bit
);
99 int a0
= bit2aig(args
.first
);
100 int a1
= bit2aig(args
.second
);
101 aig_map
[bit
] = mkgate(a0
, a1
);
103 if (alias_map
.count(bit
)) {
104 aig_map
[bit
] = bit2aig(alias_map
.at(bit
));
107 if (bit
== State::Sx
|| bit
== State::Sz
)
108 log_error("Design contains 'x' or 'z' bits. Use 'setundef' to replace those constants.\n");
111 log_assert(aig_map
.at(bit
) >= 0);
112 return aig_map
.at(bit
);
115 XAigerWriter(Module
*module
, bool holes_mode
=false) : module(module
), sigmap(module
)
117 pool
<SigBit
> undriven_bits
;
118 pool
<SigBit
> unused_bits
;
120 // promote public wires
121 for (auto wire
: module
->wires())
122 if (wire
->name
[0] == '\\')
125 // promote input wires
126 for (auto wire
: module
->wires())
127 if (wire
->port_input
)
130 // promote output wires
131 for (auto wire
: module
->wires())
132 if (wire
->port_output
)
135 for (auto wire
: module
->wires())
137 bool keep
= wire
->attributes
.count("\\keep");
139 for (int i
= 0; i
< GetSize(wire
); i
++)
141 SigBit
wirebit(wire
, i
);
142 SigBit bit
= sigmap(wirebit
);
145 undriven_bits
.insert(bit
);
146 unused_bits
.insert(bit
);
149 if (wire
->port_input
|| keep
) {
151 alias_map
[bit
] = wirebit
;
152 input_bits
.insert(wirebit
);
155 if (wire
->port_output
|| keep
) {
156 if (bit
!= RTLIL::Sx
) {
158 alias_map
[wirebit
] = bit
;
159 output_bits
.insert(wirebit
);
162 log_debug("Skipping PO '%s' driven by 1'bx\n", log_signal(wirebit
));
167 for (auto bit
: input_bits
)
168 undriven_bits
.erase(sigmap(bit
));
169 for (auto bit
: output_bits
)
170 if (!bit
.wire
->port_input
)
171 unused_bits
.erase(bit
);
173 // TODO: Speed up toposort -- ultimately we care about
174 // box ordering, but not individual AIG cells
175 dict
<SigBit
, pool
<IdString
>> bit_drivers
, bit_users
;
176 TopoSort
<IdString
, RTLIL::sort_by_id_str
> toposort
;
177 bool abc_box_seen
= false;
179 for (auto cell
: module
->selected_cells()) {
180 if (cell
->type
== "$_NOT_")
182 SigBit A
= sigmap(cell
->getPort("\\A").as_bit());
183 SigBit Y
= sigmap(cell
->getPort("\\Y").as_bit());
184 unused_bits
.erase(A
);
185 undriven_bits
.erase(Y
);
188 toposort
.node(cell
->name
);
189 bit_users
[A
].insert(cell
->name
);
190 bit_drivers
[Y
].insert(cell
->name
);
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
);
205 toposort
.node(cell
->name
);
206 bit_users
[A
].insert(cell
->name
);
207 bit_users
[B
].insert(cell
->name
);
208 bit_drivers
[Y
].insert(cell
->name
);
213 log_assert(!holes_mode
);
215 RTLIL::Module
* inst_module
= module
->design
->module(cell
->type
);
216 if (inst_module
&& inst_module
->attributes
.count("\\abc_box_id")) {
220 toposort
.node(cell
->name
);
221 for (const auto &conn
: cell
->connections()) {
222 if (cell
->input(conn
.first
)) {
223 // Ignore inout for the sake of topographical ordering
224 if (cell
->output(conn
.first
)) continue;
225 for (auto bit
: sigmap(conn
.second
))
226 bit_users
[bit
].insert(cell
->name
);
229 if (cell
->output(conn
.first
))
230 for (auto bit
: sigmap(conn
.second
))
231 bit_drivers
[bit
].insert(cell
->name
);
236 for (const auto &c
: cell
->connections()) {
237 if (c
.second
.is_fully_const()) continue;
238 auto is_input
= cell
->input(c
.first
);
239 auto is_output
= cell
->output(c
.first
);
240 log_assert(is_input
|| is_output
);
243 for (auto b
: c
.second
.bits()) {
246 if (!w
->port_output
) {
247 SigBit I
= sigmap(b
);
250 output_bits
.insert(b
);
251 unused_bits
.erase(b
);
256 for (auto b
: c
.second
.bits()) {
259 input_bits
.insert(b
);
260 SigBit O
= sigmap(b
);
263 undriven_bits
.erase(O
);
269 //log_warning("Unsupported cell type: %s (%s)\n", log_id(cell->type), log_id(cell));
273 for (auto &it
: bit_users
)
274 if (bit_drivers
.count(it
.first
))
275 for (auto driver_cell
: bit_drivers
.at(it
.first
))
276 for (auto user_cell
: it
.second
)
277 toposort
.edge(driver_cell
, user_cell
);
279 pool
<RTLIL::Module
*> abc_carry_modules
;
282 toposort
.analyze_loops
= true;
284 bool no_loops
= toposort
.sort();
287 for (auto &it
: toposort
.loops
) {
288 log(" loop %d", i
++);
290 log(" %s", log_id(cell
));
294 log_assert(no_loops
);
296 for (auto cell_name
: toposort
.sorted
) {
297 RTLIL::Cell
*cell
= module
->cell(cell_name
);
298 RTLIL::Module
* box_module
= module
->design
->module(cell
->type
);
299 if (!box_module
|| !box_module
->attributes
.count("\\abc_box_id"))
302 if (box_module
->attributes
.count("\\abc_carry") && !abc_carry_modules
.count(box_module
)) {
303 RTLIL::Wire
* carry_in
= nullptr, *carry_out
= nullptr;
304 RTLIL::Wire
* last_in
= nullptr, *last_out
= nullptr;
305 for (const auto &port_name
: box_module
->ports
) {
306 RTLIL::Wire
* w
= box_module
->wire(port_name
);
309 if (w
->attributes
.count("\\abc_carry_in")) {
310 log_assert(!carry_in
);
313 log_assert(!last_in
|| last_in
->port_id
< w
->port_id
);
316 if (w
->port_output
) {
317 if (w
->attributes
.count("\\abc_carry_out")) {
318 log_assert(!carry_out
);
321 log_assert(!last_out
|| last_out
->port_id
< w
->port_id
);
328 std::swap(box_module
->ports
[carry_in
->port_id
-1], box_module
->ports
[last_in
->port_id
-1]);
329 std::swap(carry_in
->port_id
, last_in
->port_id
);
332 log_assert(last_out
);
333 std::swap(box_module
->ports
[carry_out
->port_id
-1], box_module
->ports
[last_out
->port_id
-1]);
334 std::swap(carry_out
->port_id
, last_out
->port_id
);
338 // Fully pad all unused input connections of this box cell with S0
339 // Fully pad all undriven output connections of this box cell with anonymous wires
340 // NB: Assume box_module->ports are sorted alphabetically
341 // (as RTLIL::Module::fixup_ports() would do)
342 for (const auto &port_name
: box_module
->ports
) {
343 RTLIL::Wire
* w
= box_module
->wire(port_name
);
345 auto it
= cell
->connections_
.find(port_name
);
348 if (it
!= cell
->connections_
.end()) {
349 if (GetSize(it
->second
) < GetSize(w
))
350 it
->second
.append(RTLIL::SigSpec(RTLIL::S0
, GetSize(w
)-GetSize(it
->second
)));
354 rhs
= RTLIL::SigSpec(RTLIL::S0
, GetSize(w
));
355 cell
->setPort(port_name
, rhs
);
359 for (auto b
: rhs
.bits()) {
360 SigBit I
= sigmap(b
);
365 alias_map
[b
] = RTLIL::S0
;
369 co_bits
.emplace_back(b
, cell
, port_name
, offset
++, 0);
370 unused_bits
.erase(b
);
373 if (w
->port_output
) {
375 auto it
= cell
->connections_
.find(w
->name
);
376 if (it
!= cell
->connections_
.end()) {
377 if (GetSize(it
->second
) < GetSize(w
))
378 it
->second
.append(module
->addWire(NEW_ID
, GetSize(w
)-GetSize(it
->second
)));
382 rhs
= module
->addWire(NEW_ID
, GetSize(w
));
383 cell
->setPort(port_name
, rhs
);
387 for (const auto &b
: rhs
.bits()) {
388 ci_bits
.emplace_back(b
, cell
, port_name
, offset
++);
389 SigBit O
= sigmap(b
);
392 undriven_bits
.erase(O
);
394 auto jt
= input_bits
.find(b
);
395 if (jt
!= input_bits
.end()) {
396 log_assert(b
.wire
->attributes
.count("\\keep"));
402 box_list
.emplace_back(cell
);
405 // TODO: Free memory from toposort, bit_drivers, bit_users
408 for (auto bit
: input_bits
) {
409 if (!output_bits
.count(bit
))
411 RTLIL::Wire
*wire
= bit
.wire
;
412 // If encountering an inout port, or a keep-ed wire, then create a new wire
413 // with $inout.out suffix, make it a PO driven by the existing inout, and
414 // inherit existing inout's drivers
415 if ((wire
->port_input
&& wire
->port_output
&& !undriven_bits
.count(bit
))
416 || wire
->attributes
.count("\\keep")) {
417 RTLIL::IdString wire_name
= wire
->name
.str() + "$inout.out";
418 RTLIL::Wire
*new_wire
= module
->wire(wire_name
);
420 new_wire
= module
->addWire(wire_name
, GetSize(wire
));
421 SigBit
new_bit(new_wire
, bit
.offset
);
422 module
->connect(new_bit
, bit
);
423 if (not_map
.count(bit
))
424 not_map
[new_bit
] = not_map
.at(bit
);
425 else if (and_map
.count(bit
))
426 and_map
[new_bit
] = and_map
.at(bit
);
427 else if (alias_map
.count(bit
))
428 alias_map
[new_bit
] = alias_map
.at(bit
);
431 alias_map
[new_bit
] = bit
;
432 output_bits
.erase(bit
);
433 output_bits
.insert(new_bit
);
437 for (auto bit
: unused_bits
)
438 undriven_bits
.erase(bit
);
440 if (!undriven_bits
.empty() && !holes_mode
) {
441 undriven_bits
.sort();
442 for (auto bit
: undriven_bits
) {
443 log_warning("Treating undriven bit %s.%s like $anyseq.\n", log_id(module
), log_signal(bit
));
444 input_bits
.insert(bit
);
446 log_warning("Treating a total of %d undriven bits in %s like $anyseq.\n", GetSize(undriven_bits
), log_id(module
));
450 struct sort_by_port_id
{
451 bool operator()(const RTLIL::SigBit
& a
, const RTLIL::SigBit
& b
) const {
452 return a
.wire
->port_id
< b
.wire
->port_id
;
455 input_bits
.sort(sort_by_port_id());
456 output_bits
.sort(sort_by_port_id());
466 aig_map
[State::S0
] = 0;
467 aig_map
[State::S1
] = 1;
469 for (auto bit
: input_bits
) {
471 log_assert(!aig_map
.count(bit
));
472 aig_map
[bit
] = 2*aig_m
;
475 for (auto &c
: ci_bits
) {
476 RTLIL::SigBit bit
= std::get
<0>(c
);
478 aig_map
[bit
] = 2*aig_m
;
481 for (auto &c
: co_bits
) {
482 RTLIL::SigBit bit
= std::get
<0>(c
);
483 std::get
<4>(c
) = ordered_outputs
[bit
] = aig_o
++;
484 aig_outputs
.push_back(bit2aig(bit
));
487 for (auto bit
: output_bits
) {
488 ordered_outputs
[bit
] = aig_o
++;
489 aig_outputs
.push_back(bit2aig(bit
));
492 if (output_bits
.empty()) {
494 aig_outputs
.push_back(0);
499 void write_aiger(std::ostream
&f
, bool ascii_mode
)
502 int aig_obcj
= aig_obc
;
503 int aig_obcjf
= aig_obcj
;
505 log_assert(aig_m
== aig_i
+ aig_l
+ aig_a
);
506 log_assert(aig_obcjf
== GetSize(aig_outputs
));
508 f
<< stringf("%s %d %d %d %d %d", ascii_mode
? "aag" : "aig", aig_m
, aig_i
, aig_l
, aig_o
, aig_a
);
513 for (int i
= 0; i
< aig_i
; i
++)
514 f
<< stringf("%d\n", 2*i
+2);
516 for (int i
= 0; i
< aig_obc
; i
++)
517 f
<< stringf("%d\n", aig_outputs
.at(i
));
519 for (int i
= aig_obc
; i
< aig_obcj
; i
++)
522 for (int i
= aig_obc
; i
< aig_obcj
; i
++)
523 f
<< stringf("%d\n", aig_outputs
.at(i
));
525 for (int i
= aig_obcj
; i
< aig_obcjf
; i
++)
526 f
<< stringf("%d\n", aig_outputs
.at(i
));
528 for (int i
= 0; i
< aig_a
; i
++)
529 f
<< stringf("%d %d %d\n", 2*(aig_i
+aig_l
+i
)+2, aig_gates
.at(i
).first
, aig_gates
.at(i
).second
);
533 for (int i
= 0; i
< aig_obc
; i
++)
534 f
<< stringf("%d\n", aig_outputs
.at(i
));
536 for (int i
= aig_obc
; i
< aig_obcj
; i
++)
539 for (int i
= aig_obc
; i
< aig_obcj
; i
++)
540 f
<< stringf("%d\n", aig_outputs
.at(i
));
542 for (int i
= aig_obcj
; i
< aig_obcjf
; i
++)
543 f
<< stringf("%d\n", aig_outputs
.at(i
));
545 for (int i
= 0; i
< aig_a
; i
++) {
546 int lhs
= 2*(aig_i
+aig_l
+i
)+2;
547 int rhs0
= aig_gates
.at(i
).first
;
548 int rhs1
= aig_gates
.at(i
).second
;
549 int delta0
= lhs
- rhs0
;
550 int delta1
= rhs0
- rhs1
;
551 aiger_encode(f
, delta0
);
552 aiger_encode(f
, delta1
);
558 if (!box_list
.empty()) {
559 auto write_buffer
= [](std::stringstream
&buffer
, int i32
) {
560 int32_t i32_be
= to_big_endian(i32
);
561 buffer
.write(reinterpret_cast<const char*>(&i32_be
), sizeof(i32_be
));
564 std::stringstream h_buffer
;
565 auto write_h_buffer
= std::bind(write_buffer
, std::ref(h_buffer
), std::placeholders::_1
);
567 log_debug("ciNum = %zu\n", input_bits
.size() + ci_bits
.size());
568 write_h_buffer(input_bits
.size() + ci_bits
.size());
569 log_debug("coNum = %zu\n", output_bits
.size() + co_bits
.size());
570 write_h_buffer(output_bits
.size() + co_bits
.size());
571 log_debug("piNum = %zu\n", input_bits
.size());
572 write_h_buffer(input_bits
.size());
573 log_debug("poNum = %zu\n", output_bits
.size());
574 write_h_buffer(output_bits
.size());
575 log_debug("boxNum = %zu\n", box_list
.size());
576 write_h_buffer(box_list
.size());
578 RTLIL::Module
*holes_module
= nullptr;
579 holes_module
= module
->design
->addModule("$__holes__");
580 log_assert(holes_module
);
584 for (auto cell
: box_list
) {
585 RTLIL::Module
* box_module
= module
->design
->module(cell
->type
);
586 int box_inputs
= 0, box_outputs
= 0;
587 Cell
*holes_cell
= nullptr;
588 if (box_module
->get_bool_attribute("\\whitebox")) {
589 holes_cell
= holes_module
->addCell(cell
->name
, cell
->type
);
590 holes_cell
->parameters
= cell
->parameters
;
593 // NB: Assume box_module->ports are sorted alphabetically
594 // (as RTLIL::Module::fixup_ports() would do)
595 for (const auto &port_name
: box_module
->ports
) {
596 RTLIL::Wire
*w
= box_module
->wire(port_name
);
598 RTLIL::Wire
*holes_wire
;
599 RTLIL::SigSpec port_wire
;
601 for (int i
= 0; i
< GetSize(w
); i
++) {
603 holes_wire
= holes_module
->wire(stringf("\\i%d", box_inputs
));
605 holes_wire
= holes_module
->addWire(stringf("\\i%d", box_inputs
));
606 holes_wire
->port_input
= true;
607 holes_wire
->port_id
= port_id
++;
608 holes_module
->ports
.push_back(holes_wire
->name
);
611 port_wire
.append(holes_wire
);
613 if (!port_wire
.empty())
614 holes_cell
->setPort(w
->name
, port_wire
);
616 if (w
->port_output
) {
617 box_outputs
+= GetSize(w
);
618 for (int i
= 0; i
< GetSize(w
); i
++) {
620 holes_wire
= holes_module
->addWire(stringf("%s.%s", cell
->name
.c_str(), w
->name
.c_str()));
622 holes_wire
= holes_module
->addWire(stringf("%s.%s[%d]", cell
->name
.c_str(), w
->name
.c_str(), i
));
623 holes_wire
->port_output
= true;
624 holes_wire
->port_id
= port_id
++;
625 holes_module
->ports
.push_back(holes_wire
->name
);
627 port_wire
.append(holes_wire
);
629 holes_module
->connect(holes_wire
, RTLIL::S0
);
631 if (!port_wire
.empty())
632 holes_cell
->setPort(w
->name
, port_wire
);
636 write_h_buffer(box_inputs
);
637 write_h_buffer(box_outputs
);
638 write_h_buffer(box_module
->attributes
.at("\\abc_box_id").as_int());
639 write_h_buffer(box_count
++);
643 std::string buffer_str
= h_buffer
.str();
644 int32_t buffer_size_be
= to_big_endian(buffer_str
.size());
645 f
.write(reinterpret_cast<const char*>(&buffer_size_be
), sizeof(buffer_size_be
));
646 f
.write(buffer_str
.data(), buffer_str
.size());
648 std::stringstream r_buffer
;
649 auto write_r_buffer
= std::bind(write_buffer
, std::ref(r_buffer
), std::placeholders::_1
);
653 buffer_str
= r_buffer
.str();
654 buffer_size_be
= to_big_endian(buffer_str
.size());
655 f
.write(reinterpret_cast<const char*>(&buffer_size_be
), sizeof(buffer_size_be
));
656 f
.write(buffer_str
.data(), buffer_str
.size());
659 // NB: fixup_ports() will sort ports by name
660 //holes_module->fixup_ports();
661 holes_module
->check();
663 holes_module
->design
->selection_stack
.emplace_back(false);
664 RTLIL::Selection
& sel
= holes_module
->design
->selection_stack
.back();
665 sel
.select(holes_module
);
667 // TODO: Should not need to opt_merge if we only instantiate
668 // each box type once...
669 Pass::call(holes_module
->design
, "opt_merge -share_all");
671 Pass::call(holes_module
->design
, "flatten -wb");
673 // TODO: Should techmap/aigmap/check all lib_whitebox-es just once,
674 // instead of per write_xaiger call
675 Pass::call(holes_module
->design
, "techmap");
676 Pass::call(holes_module
->design
, "aigmap");
677 for (auto cell
: holes_module
->cells())
678 if (!cell
->type
.in("$_NOT_", "$_AND_"))
679 log_error("Whitebox contents cannot be represented as AIG. Please verify whiteboxes are synthesisable.\n");
681 Pass::call(holes_module
->design
, "clean -purge");
683 std::stringstream a_buffer
;
684 XAigerWriter
writer(holes_module
, true /* holes_mode */);
685 writer
.write_aiger(a_buffer
, false /*ascii_mode*/);
687 holes_module
->design
->selection_stack
.pop_back();
690 std::string buffer_str
= a_buffer
.str();
691 int32_t buffer_size_be
= to_big_endian(buffer_str
.size());
692 f
.write(reinterpret_cast<const char*>(&buffer_size_be
), sizeof(buffer_size_be
));
693 f
.write(buffer_str
.data(), buffer_str
.size());
694 holes_module
->design
->remove(holes_module
);
698 f
<< stringf("Generated by %s\n", yosys_version_str
);
701 void write_map(std::ostream
&f
, bool verbose_map
)
703 dict
<int, string
> input_lines
;
704 dict
<int, string
> output_lines
;
705 dict
<int, string
> wire_lines
;
707 for (auto wire
: module
->wires())
709 //if (!verbose_map && wire->name[0] == '$')
712 SigSpec sig
= sigmap(wire
);
714 for (int i
= 0; i
< GetSize(wire
); i
++)
716 RTLIL::SigBit
b(wire
, i
);
717 if (input_bits
.count(b
)) {
718 int a
= aig_map
.at(b
);
719 log_assert((a
& 1) == 0);
720 input_lines
[a
] += stringf("input %d %d %s\n", (a
>> 1)-1, i
, log_id(wire
));
723 if (output_bits
.count(b
)) {
724 int o
= ordered_outputs
.at(b
);
725 output_lines
[o
] += stringf("output %lu %d %s\n", o
- co_bits
.size(), i
, log_id(wire
));
730 if (aig_map
.count(sig
[i
]) == 0)
733 int a
= aig_map
.at(sig
[i
]);
734 wire_lines
[a
] += stringf("wire %d %d %s\n", a
, i
, log_id(wire
));
740 for (auto &it
: input_lines
)
742 log_assert(input_lines
.size() == input_bits
.size());
745 for (auto cell
: box_list
)
746 f
<< stringf("box %d %d %s\n", box_count
++, 0, log_id(cell
->name
));
749 for (auto &it
: output_lines
)
751 log_assert(output_lines
.size() == output_bits
.size());
752 if (omode
&& output_bits
.empty())
753 f
<< "output " << output_lines
.size() << " 0 $__dummy__\n";
756 for (auto &it
: wire_lines
)
761 struct XAigerBackend
: public Backend
{
762 XAigerBackend() : Backend("xaiger", "write design to XAIGER file") { }
763 void help() YS_OVERRIDE
765 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
767 log(" write_xaiger [options] [filename]\n");
769 log("Write the current design to an XAIGER file. The design must be flattened and\n");
770 log("all unsupported cells will be converted into psuedo-inputs and pseudo-outputs.\n");
773 log(" write ASCII version of AIGER format\n");
775 log(" -map <filename>\n");
776 log(" write an extra file with port and latch symbols\n");
778 log(" -vmap <filename>\n");
779 log(" like -map, but more verbose\n");
782 void execute(std::ostream
*&f
, std::string filename
, std::vector
<std::string
> args
, RTLIL::Design
*design
) YS_OVERRIDE
784 bool ascii_mode
= false;
785 bool verbose_map
= false;
786 std::string map_filename
;
788 log_header(design
, "Executing XAIGER backend.\n");
791 for (argidx
= 1; argidx
< args
.size(); argidx
++)
793 if (args
[argidx
] == "-ascii") {
797 if (map_filename
.empty() && args
[argidx
] == "-map" && argidx
+1 < args
.size()) {
798 map_filename
= args
[++argidx
];
801 if (map_filename
.empty() && args
[argidx
] == "-vmap" && argidx
+1 < args
.size()) {
802 map_filename
= args
[++argidx
];
808 extra_args(f
, filename
, args
, argidx
);
810 Module
*top_module
= design
->top_module();
812 if (top_module
== nullptr)
813 log_error("Can't find top module in current design!\n");
815 XAigerWriter
writer(top_module
);
816 writer
.write_aiger(*f
, ascii_mode
);
818 if (!map_filename
.empty()) {
820 mapf
.open(map_filename
.c_str(), std::ofstream::trunc
);
822 log_error("Can't open file `%s' for writing: %s\n", map_filename
.c_str(), strerror(errno
));
823 writer
.write_map(mapf
, verbose_map
);
828 PRIVATE_NAMESPACE_END