f49ecbedabda1d85be52b5184ab653ec824e5fd6
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 bswap32 _byteswap_ulong
25 #elif defined(__APPLE__)
26 #include <libkern/OSByteOrder.h>
27 #define bswap32 OSSwapInt32
28 #elif defined(__GNUC__)
29 #define bswap32 __builtin_bswap32
32 inline static uint32_t bswap32(uint32_t x
)
34 // https://stackoverflow.com/a/27796212
35 register uint32_t value
= number_to_be_reversed
;
36 uint8_t lolo
= (value
>> 0) & 0xFF;
37 uint8_t lohi
= (value
>> 8) & 0xFF;
38 uint8_t hilo
= (value
>> 16) & 0xFF;
39 uint8_t hihi
= (value
>> 24) & 0xFF;
47 #include "kernel/yosys.h"
48 #include "kernel/sigtools.h"
49 #include "kernel/utils.h"
52 PRIVATE_NAMESPACE_BEGIN
54 inline int32_t to_big_endian(int32_t i32
) {
55 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
57 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
60 #error "Unknown endianness"
64 void aiger_encode(std::ostream
&f
, int x
)
69 f
.put((x
& 0x7f) | 0x80);
81 pool
<SigBit
> input_bits
, output_bits
;
82 dict
<SigBit
, SigBit
> not_map
, alias_map
;
83 dict
<SigBit
, pair
<SigBit
, SigBit
>> and_map
;
84 vector
<std::tuple
<SigBit
,RTLIL::Cell
*,RTLIL::IdString
,int>> ci_bits
;
85 vector
<std::tuple
<SigBit
,RTLIL::Cell
*,RTLIL::IdString
,int,int>> co_bits
;
87 vector
<pair
<int, int>> aig_gates
;
88 vector
<int> aig_outputs
;
89 int aig_m
= 0, aig_i
= 0, aig_l
= 0, aig_o
= 0, aig_a
= 0;
91 dict
<SigBit
, int> aig_map
;
92 dict
<SigBit
, int> ordered_outputs
;
94 vector
<Cell
*> box_list
;
97 int mkgate(int a0
, int a1
)
100 aig_gates
.push_back(a0
> a1
? make_pair(a0
, a1
) : make_pair(a1
, a0
));
104 int bit2aig(SigBit bit
)
106 auto it
= aig_map
.find(bit
);
107 if (it
!= aig_map
.end()) {
108 log_assert(it
->second
>= 0);
112 // NB: Cannot use iterator returned from aig_map.insert()
113 // since this function is called recursively
116 if (not_map
.count(bit
)) {
117 a
= bit2aig(not_map
.at(bit
)) ^ 1;
119 if (and_map
.count(bit
)) {
120 auto args
= and_map
.at(bit
);
121 int a0
= bit2aig(args
.first
);
122 int a1
= bit2aig(args
.second
);
125 if (alias_map
.count(bit
)) {
126 a
= bit2aig(alias_map
.at(bit
));
129 if (bit
== State::Sx
|| bit
== State::Sz
) {
130 log_debug("Design contains 'x' or 'z' bits. Treating as 1'b0.\n");
131 a
= aig_map
.at(State::S0
);
139 XAigerWriter(Module
*module
, bool holes_mode
=false) : module(module
), sigmap(module
)
141 pool
<SigBit
> undriven_bits
;
142 pool
<SigBit
> unused_bits
;
143 pool
<SigBit
> keep_bits
;
145 // promote public wires
146 for (auto wire
: module
->wires())
147 if (wire
->name
[0] == '\\')
150 // promote input wires
151 for (auto wire
: module
->wires())
152 if (wire
->port_input
)
155 // promote output wires
156 for (auto wire
: module
->wires())
157 if (wire
->port_output
)
160 for (auto wire
: module
->wires())
162 bool keep
= wire
->attributes
.count("\\keep");
164 for (int i
= 0; i
< GetSize(wire
); i
++)
166 SigBit
wirebit(wire
, i
);
167 SigBit bit
= sigmap(wirebit
);
170 undriven_bits
.insert(bit
);
171 unused_bits
.insert(bit
);
175 keep_bits
.insert(bit
);
177 if (wire
->port_input
|| keep
) {
179 alias_map
[bit
] = wirebit
;
180 input_bits
.insert(wirebit
);
183 if (wire
->port_output
|| keep
) {
184 if (bit
!= RTLIL::Sx
) {
186 alias_map
[wirebit
] = bit
;
187 output_bits
.insert(wirebit
);
190 log_debug("Skipping PO '%s' driven by 1'bx\n", log_signal(wirebit
));
195 for (auto bit
: input_bits
)
196 undriven_bits
.erase(sigmap(bit
));
197 for (auto bit
: output_bits
)
198 if (!bit
.wire
->port_input
)
199 unused_bits
.erase(bit
);
201 // TODO: Speed up toposort -- ultimately we care about
202 // box ordering, but not individual AIG cells
203 dict
<SigBit
, pool
<IdString
>> bit_drivers
, bit_users
;
204 TopoSort
<IdString
, RTLIL::sort_by_id_str
> toposort
;
205 bool abc_box_seen
= false;
207 for (auto cell
: module
->selected_cells()) {
208 if (cell
->type
== "$_NOT_")
210 SigBit A
= sigmap(cell
->getPort("\\A").as_bit());
211 SigBit Y
= sigmap(cell
->getPort("\\Y").as_bit());
212 unused_bits
.erase(A
);
213 undriven_bits
.erase(Y
);
216 toposort
.node(cell
->name
);
217 bit_users
[A
].insert(cell
->name
);
218 bit_drivers
[Y
].insert(cell
->name
);
223 if (cell
->type
== "$_AND_")
225 SigBit A
= sigmap(cell
->getPort("\\A").as_bit());
226 SigBit B
= sigmap(cell
->getPort("\\B").as_bit());
227 SigBit Y
= sigmap(cell
->getPort("\\Y").as_bit());
228 unused_bits
.erase(A
);
229 unused_bits
.erase(B
);
230 undriven_bits
.erase(Y
);
231 and_map
[Y
] = make_pair(A
, B
);
233 toposort
.node(cell
->name
);
234 bit_users
[A
].insert(cell
->name
);
235 bit_users
[B
].insert(cell
->name
);
236 bit_drivers
[Y
].insert(cell
->name
);
241 log_assert(!holes_mode
);
243 RTLIL::Module
* inst_module
= module
->design
->module(cell
->type
);
244 if (inst_module
&& inst_module
->attributes
.count("\\abc_box_id")) {
248 toposort
.node(cell
->name
);
249 for (const auto &conn
: cell
->connections()) {
250 if (cell
->input(conn
.first
)) {
251 // Ignore inout for the sake of topographical ordering
252 if (cell
->output(conn
.first
)) continue;
253 for (auto bit
: sigmap(conn
.second
))
254 bit_users
[bit
].insert(cell
->name
);
257 if (cell
->output(conn
.first
))
258 for (auto bit
: sigmap(conn
.second
))
259 bit_drivers
[bit
].insert(cell
->name
);
264 bool cell_known
= cell
->known();
265 for (const auto &c
: cell
->connections()) {
266 if (c
.second
.is_fully_const()) continue;
267 auto is_input
= !cell_known
|| cell
->input(c
.first
);
268 auto is_output
= !cell_known
|| cell
->output(c
.first
);
269 if (!is_input
&& !is_output
)
270 log_error("Connection '%s' on cell '%s' (type '%s') not recognised!\n", log_id(c
.first
), log_id(cell
), log_id(cell
->type
));
273 for (auto b
: c
.second
.bits()) {
276 if (!w
->port_output
|| !cell_known
) {
277 SigBit I
= sigmap(b
);
280 output_bits
.insert(b
);
281 unused_bits
.erase(b
);
289 for (auto b
: c
.second
.bits()) {
292 input_bits
.insert(b
);
293 SigBit O
= sigmap(b
);
296 undriven_bits
.erase(O
);
302 //log_warning("Unsupported cell type: %s (%s)\n", log_id(cell->type), log_id(cell));
306 for (auto &it
: bit_users
)
307 if (bit_drivers
.count(it
.first
))
308 for (auto driver_cell
: bit_drivers
.at(it
.first
))
309 for (auto user_cell
: it
.second
)
310 toposort
.edge(driver_cell
, user_cell
);
313 toposort
.analyze_loops
= true;
315 bool no_loops
= toposort
.sort();
318 for (auto &it
: toposort
.loops
) {
319 log(" loop %d\n", i
++);
320 for (auto cell_name
: it
) {
321 auto cell
= module
->cell(cell_name
);
323 log("\t%s (%s @ %s)\n", log_id(cell
), log_id(cell
->type
), cell
->get_src_attribute().c_str());
327 log_assert(no_loops
);
329 pool
<IdString
> seen_boxes
;
330 for (auto cell_name
: toposort
.sorted
) {
331 RTLIL::Cell
*cell
= module
->cell(cell_name
);
334 RTLIL::Module
* box_module
= module
->design
->module(cell
->type
);
335 if (!box_module
|| !box_module
->attributes
.count("\\abc_box_id"))
338 if (seen_boxes
.insert(cell
->type
).second
) {
339 auto it
= box_module
->attributes
.find("\\abc_carry");
340 if (it
!= box_module
->attributes
.end()) {
341 RTLIL::Wire
*carry_in
= nullptr, *carry_out
= nullptr;
342 auto carry_in_out
= it
->second
.decode_string();
343 auto pos
= carry_in_out
.find(',');
344 if (pos
== std::string::npos
)
345 log_error("'abc_carry' attribute on module '%s' does not contain ','.\n", log_id(cell
->type
));
346 auto carry_in_name
= RTLIL::escape_id(carry_in_out
.substr(0, pos
));
347 carry_in
= box_module
->wire(carry_in_name
);
348 if (!carry_in
|| !carry_in
->port_input
)
349 log_error("'abc_carry' on module '%s' contains '%s' which does not exist or is not an input port.\n", log_id(cell
->type
), carry_in_name
.c_str());
351 auto carry_out_name
= RTLIL::escape_id(carry_in_out
.substr(pos
+1));
352 carry_out
= box_module
->wire(carry_out_name
);
353 if (!carry_out
|| !carry_out
->port_output
)
354 log_error("'abc_carry' on module '%s' contains '%s' which does not exist or is not an output port.\n", log_id(cell
->type
), carry_out_name
.c_str());
356 auto &ports
= box_module
->ports
;
357 for (auto jt
= ports
.begin(); jt
!= ports
.end(); ) {
358 RTLIL::Wire
* w
= box_module
->wire(*jt
);
360 if (w
== carry_in
|| w
== carry_out
) {
361 jt
= ports
.erase(jt
);
364 if (w
->port_id
> carry_in
->port_id
)
366 if (w
->port_id
> carry_out
->port_id
)
368 log_assert(w
->port_input
|| w
->port_output
);
369 log_assert(ports
[w
->port_id
-1] == w
->name
);
372 ports
.push_back(carry_in
->name
);
373 carry_in
->port_id
= ports
.size();
374 ports
.push_back(carry_out
->name
);
375 carry_out
->port_id
= ports
.size();
379 // Fully pad all unused input connections of this box cell with S0
380 // Fully pad all undriven output connections of this box cell with anonymous wires
381 // NB: Assume box_module->ports are sorted alphabetically
382 // (as RTLIL::Module::fixup_ports() would do)
383 for (const auto &port_name
: box_module
->ports
) {
384 RTLIL::Wire
* w
= box_module
->wire(port_name
);
386 auto it
= cell
->connections_
.find(port_name
);
389 if (it
!= cell
->connections_
.end()) {
390 if (GetSize(it
->second
) < GetSize(w
))
391 it
->second
.append(RTLIL::SigSpec(State::S0
, GetSize(w
)-GetSize(it
->second
)));
395 rhs
= RTLIL::SigSpec(State::S0
, GetSize(w
));
396 cell
->setPort(port_name
, rhs
);
400 for (auto b
: rhs
.bits()) {
401 SigBit I
= sigmap(b
);
406 alias_map
[b
] = State::S0
;
410 co_bits
.emplace_back(b
, cell
, port_name
, offset
++, 0);
411 unused_bits
.erase(b
);
414 if (w
->port_output
) {
416 auto it
= cell
->connections_
.find(w
->name
);
417 if (it
!= cell
->connections_
.end()) {
418 if (GetSize(it
->second
) < GetSize(w
))
419 it
->second
.append(module
->addWire(NEW_ID
, GetSize(w
)-GetSize(it
->second
)));
423 rhs
= module
->addWire(NEW_ID
, GetSize(w
));
424 cell
->setPort(port_name
, rhs
);
428 for (const auto &b
: rhs
.bits()) {
429 ci_bits
.emplace_back(b
, cell
, port_name
, offset
++);
430 SigBit O
= sigmap(b
);
433 undriven_bits
.erase(O
);
435 auto jt
= input_bits
.find(b
);
436 if (jt
!= input_bits
.end()) {
437 log_assert(keep_bits
.count(O
));
443 box_list
.emplace_back(cell
);
446 // TODO: Free memory from toposort, bit_drivers, bit_users
449 for (auto bit
: input_bits
) {
450 if (!output_bits
.count(bit
))
452 RTLIL::Wire
*wire
= bit
.wire
;
453 // If encountering an inout port, or a keep-ed wire, then create a new wire
454 // with $inout.out suffix, make it a PO driven by the existing inout, and
455 // inherit existing inout's drivers
456 if ((wire
->port_input
&& wire
->port_output
&& !undriven_bits
.count(bit
))
457 || keep_bits
.count(bit
)) {
458 RTLIL::IdString wire_name
= wire
->name
.str() + "$inout.out";
459 RTLIL::Wire
*new_wire
= module
->wire(wire_name
);
461 new_wire
= module
->addWire(wire_name
, GetSize(wire
));
462 SigBit
new_bit(new_wire
, bit
.offset
);
463 module
->connect(new_bit
, bit
);
464 if (not_map
.count(bit
)) {
465 auto a
= not_map
.at(bit
);
466 not_map
[new_bit
] = a
;
468 else if (and_map
.count(bit
)) {
469 auto a
= and_map
.at(bit
);
470 and_map
[new_bit
] = a
;
472 else if (alias_map
.count(bit
)) {
473 auto a
= alias_map
.at(bit
);
474 alias_map
[new_bit
] = a
;
477 alias_map
[new_bit
] = bit
;
478 output_bits
.erase(bit
);
479 output_bits
.insert(new_bit
);
483 for (auto bit
: unused_bits
)
484 undriven_bits
.erase(bit
);
486 if (!undriven_bits
.empty() && !holes_mode
) {
487 undriven_bits
.sort();
488 for (auto bit
: undriven_bits
) {
489 log_warning("Treating undriven bit %s.%s like $anyseq.\n", log_id(module
), log_signal(bit
));
490 input_bits
.insert(bit
);
492 log_warning("Treating a total of %d undriven bits in %s like $anyseq.\n", GetSize(undriven_bits
), log_id(module
));
496 struct sort_by_port_id
{
497 bool operator()(const RTLIL::SigBit
& a
, const RTLIL::SigBit
& b
) const {
498 return a
.wire
->port_id
< b
.wire
->port_id
;
501 input_bits
.sort(sort_by_port_id());
502 output_bits
.sort(sort_by_port_id());
512 aig_map
[State::S0
] = 0;
513 aig_map
[State::S1
] = 1;
515 for (auto bit
: input_bits
) {
517 log_assert(!aig_map
.count(bit
));
518 aig_map
[bit
] = 2*aig_m
;
521 for (auto &c
: ci_bits
) {
522 RTLIL::SigBit bit
= std::get
<0>(c
);
524 aig_map
[bit
] = 2*aig_m
;
527 for (auto &c
: co_bits
) {
528 RTLIL::SigBit bit
= std::get
<0>(c
);
529 std::get
<4>(c
) = ordered_outputs
[bit
] = aig_o
++;
530 aig_outputs
.push_back(bit2aig(bit
));
533 for (auto bit
: output_bits
) {
534 ordered_outputs
[bit
] = aig_o
++;
535 aig_outputs
.push_back(bit2aig(bit
));
538 if (output_bits
.empty()) {
540 aig_outputs
.push_back(0);
545 void write_aiger(std::ostream
&f
, bool ascii_mode
)
548 int aig_obcj
= aig_obc
;
549 int aig_obcjf
= aig_obcj
;
551 log_assert(aig_m
== aig_i
+ aig_l
+ aig_a
);
552 log_assert(aig_obcjf
== GetSize(aig_outputs
));
554 f
<< stringf("%s %d %d %d %d %d", ascii_mode
? "aag" : "aig", aig_m
, aig_i
, aig_l
, aig_o
, aig_a
);
559 for (int i
= 0; i
< aig_i
; i
++)
560 f
<< stringf("%d\n", 2*i
+2);
562 for (int i
= 0; i
< aig_obc
; i
++)
563 f
<< stringf("%d\n", aig_outputs
.at(i
));
565 for (int i
= aig_obc
; i
< aig_obcj
; i
++)
568 for (int i
= aig_obc
; i
< aig_obcj
; i
++)
569 f
<< stringf("%d\n", aig_outputs
.at(i
));
571 for (int i
= aig_obcj
; i
< aig_obcjf
; i
++)
572 f
<< stringf("%d\n", aig_outputs
.at(i
));
574 for (int i
= 0; i
< aig_a
; i
++)
575 f
<< stringf("%d %d %d\n", 2*(aig_i
+aig_l
+i
)+2, aig_gates
.at(i
).first
, aig_gates
.at(i
).second
);
579 for (int i
= 0; i
< aig_obc
; i
++)
580 f
<< stringf("%d\n", aig_outputs
.at(i
));
582 for (int i
= aig_obc
; i
< aig_obcj
; i
++)
585 for (int i
= aig_obc
; i
< aig_obcj
; i
++)
586 f
<< stringf("%d\n", aig_outputs
.at(i
));
588 for (int i
= aig_obcj
; i
< aig_obcjf
; i
++)
589 f
<< stringf("%d\n", aig_outputs
.at(i
));
591 for (int i
= 0; i
< aig_a
; i
++) {
592 int lhs
= 2*(aig_i
+aig_l
+i
)+2;
593 int rhs0
= aig_gates
.at(i
).first
;
594 int rhs1
= aig_gates
.at(i
).second
;
595 int delta0
= lhs
- rhs0
;
596 int delta1
= rhs0
- rhs1
;
597 aiger_encode(f
, delta0
);
598 aiger_encode(f
, delta1
);
604 if (!box_list
.empty()) {
605 auto write_buffer
= [](std::stringstream
&buffer
, int i32
) {
606 int32_t i32_be
= to_big_endian(i32
);
607 buffer
.write(reinterpret_cast<const char*>(&i32_be
), sizeof(i32_be
));
610 std::stringstream h_buffer
;
611 auto write_h_buffer
= std::bind(write_buffer
, std::ref(h_buffer
), std::placeholders::_1
);
613 log_debug("ciNum = %d\n", GetSize(input_bits
) + GetSize(ci_bits
));
614 write_h_buffer(input_bits
.size() + ci_bits
.size());
615 log_debug("coNum = %d\n", GetSize(output_bits
) + GetSize(co_bits
));
616 write_h_buffer(output_bits
.size() + co_bits
.size());
617 log_debug("piNum = %d\n", GetSize(input_bits
));
618 write_h_buffer(input_bits
.size());
619 log_debug("poNum = %d\n", GetSize(output_bits
));
620 write_h_buffer(output_bits
.size());
621 log_debug("boxNum = %d\n", GetSize(box_list
));
622 write_h_buffer(box_list
.size());
624 RTLIL::Module
*holes_module
= module
->design
->addModule("$__holes__");
625 log_assert(holes_module
);
629 for (auto cell
: box_list
) {
630 RTLIL::Module
* box_module
= module
->design
->module(cell
->type
);
631 int box_inputs
= 0, box_outputs
= 0;
632 Cell
*holes_cell
= nullptr;
633 if (box_module
->get_bool_attribute("\\whitebox")) {
634 holes_cell
= holes_module
->addCell(cell
->name
, cell
->type
);
635 holes_cell
->parameters
= cell
->parameters
;
638 // NB: Assume box_module->ports are sorted alphabetically
639 // (as RTLIL::Module::fixup_ports() would do)
640 for (const auto &port_name
: box_module
->ports
) {
641 RTLIL::Wire
*w
= box_module
->wire(port_name
);
643 RTLIL::Wire
*holes_wire
;
644 RTLIL::SigSpec port_wire
;
646 for (int i
= 0; i
< GetSize(w
); i
++) {
648 holes_wire
= holes_module
->wire(stringf("\\i%d", box_inputs
));
650 holes_wire
= holes_module
->addWire(stringf("\\i%d", box_inputs
));
651 holes_wire
->port_input
= true;
652 holes_wire
->port_id
= port_id
++;
653 holes_module
->ports
.push_back(holes_wire
->name
);
656 port_wire
.append(holes_wire
);
658 if (!port_wire
.empty())
659 holes_cell
->setPort(w
->name
, port_wire
);
661 if (w
->port_output
) {
662 box_outputs
+= GetSize(w
);
663 for (int i
= 0; i
< GetSize(w
); i
++) {
665 holes_wire
= holes_module
->addWire(stringf("%s.%s", cell
->name
.c_str(), w
->name
.c_str()));
667 holes_wire
= holes_module
->addWire(stringf("%s.%s[%d]", cell
->name
.c_str(), w
->name
.c_str(), i
));
668 holes_wire
->port_output
= true;
669 holes_wire
->port_id
= port_id
++;
670 holes_module
->ports
.push_back(holes_wire
->name
);
672 port_wire
.append(holes_wire
);
674 holes_module
->connect(holes_wire
, State::S0
);
676 if (!port_wire
.empty())
677 holes_cell
->setPort(w
->name
, port_wire
);
681 write_h_buffer(box_inputs
);
682 write_h_buffer(box_outputs
);
683 write_h_buffer(box_module
->attributes
.at("\\abc_box_id").as_int());
684 write_h_buffer(box_count
++);
688 std::string buffer_str
= h_buffer
.str();
689 int32_t buffer_size_be
= to_big_endian(buffer_str
.size());
690 f
.write(reinterpret_cast<const char*>(&buffer_size_be
), sizeof(buffer_size_be
));
691 f
.write(buffer_str
.data(), buffer_str
.size());
693 std::stringstream r_buffer
;
694 auto write_r_buffer
= std::bind(write_buffer
, std::ref(r_buffer
), std::placeholders::_1
);
698 buffer_str
= r_buffer
.str();
699 buffer_size_be
= to_big_endian(buffer_str
.size());
700 f
.write(reinterpret_cast<const char*>(&buffer_size_be
), sizeof(buffer_size_be
));
701 f
.write(buffer_str
.data(), buffer_str
.size());
706 // NB: fixup_ports() will sort ports by name
707 //holes_module->fixup_ports();
708 holes_module
->check();
710 holes_module
->design
->selection_stack
.emplace_back(false);
711 RTLIL::Selection
& sel
= holes_module
->design
->selection_stack
.back();
712 sel
.select(holes_module
);
714 // TODO: Should not need to opt_merge if we only instantiate
715 // each box type once...
716 Pass::call(holes_module
->design
, "opt_merge -share_all");
718 Pass::call(holes_module
->design
, "flatten -wb");
720 // TODO: Should techmap/aigmap/check all lib_whitebox-es just once,
721 // instead of per write_xaiger call
722 Pass::call(holes_module
->design
, "techmap");
723 Pass::call(holes_module
->design
, "aigmap");
724 for (auto cell
: holes_module
->cells())
725 if (!cell
->type
.in("$_NOT_", "$_AND_"))
726 log_error("Whitebox contents cannot be represented as AIG. Please verify whiteboxes are synthesisable.\n");
728 holes_module
->design
->selection_stack
.pop_back();
730 // Move into a new (temporary) design so that "clean" will only
731 // operate (and run checks on) this one module
732 RTLIL::Design
*holes_design
= new RTLIL::Design
;
733 holes_module
->design
->modules_
.erase(holes_module
->name
);
734 holes_design
->add(holes_module
);
735 Pass::call(holes_design
, "clean -purge");
737 std::stringstream a_buffer
;
738 XAigerWriter
writer(holes_module
, true /* holes_mode */);
739 writer
.write_aiger(a_buffer
, false /*ascii_mode*/);
744 std::string buffer_str
= a_buffer
.str();
745 int32_t buffer_size_be
= to_big_endian(buffer_str
.size());
746 f
.write(reinterpret_cast<const char*>(&buffer_size_be
), sizeof(buffer_size_be
));
747 f
.write(buffer_str
.data(), buffer_str
.size());
753 f
<< stringf("Generated by %s\n", yosys_version_str
);
756 void write_map(std::ostream
&f
, bool verbose_map
)
758 dict
<int, string
> input_lines
;
759 dict
<int, string
> output_lines
;
760 dict
<int, string
> wire_lines
;
762 for (auto wire
: module
->wires())
764 //if (!verbose_map && wire->name[0] == '$')
767 SigSpec sig
= sigmap(wire
);
769 for (int i
= 0; i
< GetSize(wire
); i
++)
771 RTLIL::SigBit
b(wire
, i
);
772 if (input_bits
.count(b
)) {
773 int a
= aig_map
.at(b
);
774 log_assert((a
& 1) == 0);
775 input_lines
[a
] += stringf("input %d %d %s\n", (a
>> 1)-1, i
, log_id(wire
));
778 if (output_bits
.count(b
)) {
779 int o
= ordered_outputs
.at(b
);
780 output_lines
[o
] += stringf("output %d %d %s\n", o
- GetSize(co_bits
), i
, log_id(wire
));
785 if (aig_map
.count(sig
[i
]) == 0)
788 int a
= aig_map
.at(sig
[i
]);
789 wire_lines
[a
] += stringf("wire %d %d %s\n", a
, i
, log_id(wire
));
795 for (auto &it
: input_lines
)
797 log_assert(input_lines
.size() == input_bits
.size());
800 for (auto cell
: box_list
)
801 f
<< stringf("box %d %d %s\n", box_count
++, 0, log_id(cell
->name
));
804 for (auto &it
: output_lines
)
806 log_assert(output_lines
.size() == output_bits
.size());
807 if (omode
&& output_bits
.empty())
808 f
<< "output " << output_lines
.size() << " 0 $__dummy__\n";
811 for (auto &it
: wire_lines
)
816 struct XAigerBackend
: public Backend
{
817 XAigerBackend() : Backend("xaiger", "write design to XAIGER file") { }
818 void help() YS_OVERRIDE
820 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
822 log(" write_xaiger [options] [filename]\n");
824 log("Write the current design to an XAIGER file. The design must be flattened and\n");
825 log("all unsupported cells will be converted into psuedo-inputs and pseudo-outputs.\n");
828 log(" write ASCII version of AIGER format\n");
830 log(" -map <filename>\n");
831 log(" write an extra file with port and latch symbols\n");
833 log(" -vmap <filename>\n");
834 log(" like -map, but more verbose\n");
837 void execute(std::ostream
*&f
, std::string filename
, std::vector
<std::string
> args
, RTLIL::Design
*design
) YS_OVERRIDE
839 bool ascii_mode
= false;
840 bool verbose_map
= false;
841 std::string map_filename
;
843 log_header(design
, "Executing XAIGER backend.\n");
846 for (argidx
= 1; argidx
< args
.size(); argidx
++)
848 if (args
[argidx
] == "-ascii") {
852 if (map_filename
.empty() && args
[argidx
] == "-map" && argidx
+1 < args
.size()) {
853 map_filename
= args
[++argidx
];
856 if (map_filename
.empty() && args
[argidx
] == "-vmap" && argidx
+1 < args
.size()) {
857 map_filename
= args
[++argidx
];
863 extra_args(f
, filename
, args
, argidx
);
865 Module
*top_module
= design
->top_module();
867 if (top_module
== nullptr)
868 log_error("Can't find top module in current design!\n");
870 XAigerWriter
writer(top_module
);
871 writer
.write_aiger(*f
, ascii_mode
);
873 if (!map_filename
.empty()) {
875 mapf
.open(map_filename
.c_str(), std::ofstream::trunc
);
877 log_error("Can't open file `%s' for writing: %s\n", map_filename
.c_str(), strerror(errno
));
878 writer
.write_map(mapf
, verbose_map
);
883 PRIVATE_NAMESPACE_END