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"
23 #include "kernel/celltypes.h"
24 #include "kernel/utils.h"
27 PRIVATE_NAMESPACE_BEGIN
29 void aiger_encode(std::ostream
&f
, int x
)
34 f
.put((x
& 0x7f) | 0x80);
47 dict
<SigBit
, bool> init_map
;
48 pool
<SigBit
> input_bits
, output_bits
;
49 dict
<SigBit
, SigBit
> not_map
, ff_map
, alias_map
;
50 dict
<SigBit
, pair
<SigBit
, SigBit
>> and_map
;
51 //pool<SigBit> initstate_bits;
52 vector
<std::pair
<SigBit
,int>> ci_bits
, co_bits
;
54 vector
<pair
<int, int>> aig_gates
;
55 vector
<int> aig_latchin
, aig_latchinit
, aig_outputs
;
56 int aig_m
= 0, aig_i
= 0, aig_l
= 0, aig_o
= 0, aig_a
= 0;
58 dict
<SigBit
, int> aig_map
;
59 dict
<SigBit
, int> ordered_outputs
;
60 dict
<SigBit
, int> ordered_latches
;
62 vector
<Cell
*> box_list
;
64 //dict<SigBit, int> init_inputs;
65 //int initstate_ff = 0;
67 int mkgate(int a0
, int a1
)
70 aig_gates
.push_back(a0
> a1
? make_pair(a0
, a1
) : make_pair(a1
, a0
));
74 int bit2aig(SigBit bit
)
76 if (aig_map
.count(bit
) == 0)
80 //if (initstate_bits.count(bit)) {
81 // log_assert(initstate_ff > 0);
82 // aig_map[bit] = initstate_ff;
84 if (not_map
.count(bit
)) {
85 int a
= bit2aig(not_map
.at(bit
)) ^ 1;
88 if (and_map
.count(bit
)) {
89 auto args
= and_map
.at(bit
);
90 int a0
= bit2aig(args
.first
);
91 int a1
= bit2aig(args
.second
);
92 aig_map
[bit
] = mkgate(a0
, a1
);
94 if (alias_map
.count(bit
)) {
95 aig_map
[bit
] = bit2aig(alias_map
.at(bit
));
98 if (bit
== State::Sx
|| bit
== State::Sz
)
99 log_error("Design contains 'x' or 'z' bits. Use 'setundef' to replace those constants.\n");
102 log_assert(aig_map
.at(bit
) >= 0);
103 return aig_map
.at(bit
);
106 XAigerWriter(Module
*module
, bool zinit_mode
, bool imode
, bool omode
, bool bmode
, bool holes_mode
=false) : module(module
), zinit_mode(zinit_mode
), sigmap(module
)
108 pool
<SigBit
> undriven_bits
;
109 pool
<SigBit
> unused_bits
;
111 // promote public wires
112 for (auto wire
: module
->wires())
113 if (wire
->name
[0] == '\\')
116 // promote input wires
117 for (auto wire
: module
->wires())
118 if (wire
->port_input
)
121 // promote output wires
122 for (auto wire
: module
->wires())
123 if (wire
->port_output
)
126 for (auto wire
: module
->wires())
128 if (wire
->attributes
.count("\\init")) {
129 SigSpec initsig
= sigmap(wire
);
130 Const initval
= wire
->attributes
.at("\\init");
131 for (int i
= 0; i
< GetSize(wire
) && i
< GetSize(initval
); i
++)
132 if (initval
[i
] == State::S0
|| initval
[i
] == State::S1
)
133 init_map
[initsig
[i
]] = initval
[i
] == State::S1
;
136 bool keep
= wire
->attributes
.count("\\keep");
138 for (int i
= 0; i
< GetSize(wire
); i
++)
140 SigBit
wirebit(wire
, i
);
141 SigBit bit
= sigmap(wirebit
);
143 if (bit
.wire
== nullptr) {
144 if (wire
->port_output
) {
145 aig_map
[wirebit
] = (bit
== State::S1
) ? 1 : 0;
146 output_bits
.insert(wirebit
);
151 undriven_bits
.insert(bit
);
152 unused_bits
.insert(bit
);
154 if (wire
->port_input
)
155 input_bits
.insert(bit
);
157 input_bits
.insert(wirebit
);
159 if (wire
->port_output
|| keep
) {
161 alias_map
[wirebit
] = bit
;
162 output_bits
.insert(wirebit
);
167 for (auto bit
: input_bits
)
168 undriven_bits
.erase(bit
);
170 for (auto bit
: output_bits
)
171 if (!bit
.wire
->port_input
)
172 unused_bits
.erase(bit
);
174 dict
<SigBit
, pool
<IdString
>> bit_drivers
, bit_users
;
175 TopoSort
<IdString
, RTLIL::sort_by_id_str
> toposort
;
176 bool abc_box_seen
= false;
178 for (auto cell
: module
->cells())
180 RTLIL::Module
* inst_module
= module
->design
->module(cell
->type
);
181 bool known_type
= yosys_celltypes
.cell_known(cell
->type
);
184 toposort
.node(cell
->name
);
185 for (const auto &conn
: cell
->connections())
187 if (!cell
->type
.in("$_NOT_", "$_AND_")) {
189 if (conn
.first
.in("\\Q", "\\CTRL_OUT", "\\RD_DATA"))
191 if (cell
->type
== "$memrd" && conn
.first
== "\\DATA")
196 RTLIL::Wire
* inst_module_port
= inst_module
->wire(conn
.first
);
197 log_assert(inst_module_port
);
199 if (inst_module_port
->attributes
.count("\\abc_flop_q"))
204 if (cell
->input(conn
.first
)) {
205 // Ignore inout for the sake of topographical ordering
206 if (cell
->output(conn
.first
)) continue;
207 for (auto bit
: sigmap(conn
.second
))
208 bit_users
[bit
].insert(cell
->name
);
211 if (cell
->output(conn
.first
))
212 for (auto bit
: sigmap(conn
.second
))
213 bit_drivers
[bit
].insert(cell
->name
);
217 if (cell
->type
== "$_NOT_")
219 SigBit A
= sigmap(cell
->getPort("\\A").as_bit());
220 SigBit Y
= sigmap(cell
->getPort("\\Y").as_bit());
221 unused_bits
.erase(A
);
222 undriven_bits
.erase(Y
);
227 //if (cell->type.in("$_FF_", "$_DFF_N_", "$_DFF_P_"))
229 // SigBit D = sigmap(cell->getPort("\\D").as_bit());
230 // SigBit Q = sigmap(cell->getPort("\\Q").as_bit());
231 // unused_bits.erase(D);
232 // undriven_bits.erase(Q);
237 if (cell
->type
== "$_AND_")
239 SigBit A
= sigmap(cell
->getPort("\\A").as_bit());
240 SigBit B
= sigmap(cell
->getPort("\\B").as_bit());
241 SigBit Y
= sigmap(cell
->getPort("\\Y").as_bit());
242 unused_bits
.erase(A
);
243 unused_bits
.erase(B
);
244 undriven_bits
.erase(Y
);
245 and_map
[Y
] = make_pair(A
, B
);
249 //if (cell->type == "$initstate")
251 // SigBit Y = sigmap(cell->getPort("\\Y").as_bit());
252 // undriven_bits.erase(Y);
253 // initstate_bits.insert(Y);
257 if (inst_module
&& inst_module
->attributes
.count("\\abc_box_id")) {
261 for (const auto &c
: cell
->connections()) {
262 if (c
.second
.is_fully_const()) continue;
263 for (auto b
: c
.second
.bits()) {
266 auto is_input
= cell
->input(c
.first
);
267 auto is_output
= cell
->output(c
.first
);
268 log_assert(is_input
|| is_output
);
270 if (!w
->port_input
) {
271 SigBit I
= sigmap(b
);
274 output_bits
.insert(b
);
275 unused_bits
.erase(b
);
279 SigBit O
= sigmap(b
);
280 input_bits
.insert(O
);
281 undriven_bits
.erase(O
);
287 //log_warning("Unsupported cell type: %s (%s)\n", log_id(cell->type), log_id(cell));
291 for (auto &it
: bit_users
)
292 if (bit_drivers
.count(it
.first
))
293 for (auto driver_cell
: bit_drivers
.at(it
.first
))
294 for (auto user_cell
: it
.second
)
295 toposort
.edge(driver_cell
, user_cell
);
298 for (auto cell_name
: toposort
.sorted
) {
299 RTLIL::Cell
*cell
= module
->cell(cell_name
);
300 RTLIL::Module
* box_module
= module
->design
->module(cell
->type
);
301 if (!box_module
|| !box_module
->attributes
.count("\\abc_box_id"))
304 // Fully pad all unused input connections of this box cell with S0
305 // Fully pad all undriven output connections of this box cell with anonymous wires
306 for (const auto w
: box_module
->wires()) {
308 auto it
= cell
->connections_
.find(w
->name
);
309 if (it
!= cell
->connections_
.end()) {
310 if (GetSize(it
->second
) < GetSize(w
))
311 it
->second
.append(RTLIL::SigSpec(RTLIL::S0
, GetSize(w
)-GetSize(it
->second
)));
314 cell
->connections_
[w
->name
] = RTLIL::SigSpec(RTLIL::S0
, GetSize(w
));
316 if (w
->port_output
) {
317 auto it
= cell
->connections_
.find(w
->name
);
318 if (it
!= cell
->connections_
.end()) {
319 if (GetSize(it
->second
) < GetSize(w
))
320 it
->second
.append(module
->addWire(NEW_ID
, GetSize(w
)-GetSize(it
->second
)));
323 cell
->connections_
[w
->name
] = module
->addWire(NEW_ID
, GetSize(w
));
327 // Box ordering is alphabetical
328 cell
->connections_
.sort(RTLIL::sort_by_id_str());
329 for (const auto &c
: cell
->connections()) {
330 for (auto b
: c
.second
.bits()) {
331 auto is_input
= cell
->input(c
.first
);
332 auto is_output
= cell
->output(c
.first
);
333 log_assert(is_input
|| is_output
);
335 SigBit I
= sigmap(b
);
338 co_bits
.emplace_back(b
, 0);
341 SigBit O
= sigmap(b
);
342 ci_bits
.emplace_back(O
, 0);
347 box_list
.emplace_back(cell
);
350 // TODO: Free memory from toposort, bit_drivers, bit_users
353 for (auto bit
: input_bits
) {
354 RTLIL::Wire
*wire
= bit
.wire
;
355 // If encountering an inout port, or a keep-ed wire, then create a new wire
356 // with $inout.out suffix, make it a PO driven by the existing inout, and
357 // inherit existing inout's drivers
358 if ((wire
->port_input
&& wire
->port_output
&& !undriven_bits
.count(bit
))
359 || wire
->attributes
.count("\\keep")) {
360 log_assert(input_bits
.count(bit
) && output_bits
.count(bit
));
361 RTLIL::Wire
*new_wire
= module
->wire(wire
->name
.str() + "$inout.out");
363 new_wire
= module
->addWire(wire
->name
.str() + "$inout.out", GetSize(wire
));
364 SigBit
new_bit(new_wire
, bit
.offset
);
365 module
->connect(new_bit
, bit
);
366 if (not_map
.count(bit
))
367 not_map
[new_bit
] = not_map
.at(bit
);
368 else if (and_map
.count(bit
))
369 and_map
[new_bit
] = and_map
.at(bit
);
370 else if (alias_map
.count(bit
))
371 alias_map
[new_bit
] = alias_map
.at(bit
);
374 alias_map
[new_bit
] = bit
;
375 output_bits
.erase(bit
);
376 output_bits
.insert(new_bit
);
380 // Do some CI/CO post-processing:
381 // CIs cannot be undriven
382 for (const auto &c
: ci_bits
)
383 undriven_bits
.erase(c
.first
);
384 // Erase all POs that are undriven
386 for (auto bit
: undriven_bits
)
387 output_bits
.erase(bit
);
388 for (auto bit
: unused_bits
)
389 undriven_bits
.erase(bit
);
391 if (!undriven_bits
.empty() && !holes_mode
) {
392 undriven_bits
.sort();
393 for (auto bit
: undriven_bits
) {
394 log_warning("Treating undriven bit %s.%s like $anyseq.\n", log_id(module
), log_signal(bit
));
395 input_bits
.insert(bit
);
397 log_warning("Treating a total of %d undriven bits in %s like $anyseq.\n", GetSize(undriven_bits
), log_id(module
));
407 aig_map
[State::S0
] = 0;
408 aig_map
[State::S1
] = 1;
410 for (auto bit
: input_bits
) {
412 aig_map
[bit
] = 2*aig_m
;
415 for (auto &c
: ci_bits
) {
418 aig_map
[c
.first
] = c
.second
;
421 if (imode
&& input_bits
.empty()) {
427 // for (auto it : ff_map) {
428 // if (init_map.count(it.first))
431 // init_inputs[it.first] = 2*aig_m;
435 for (auto it
: ff_map
) {
437 aig_map
[it
.first
] = 2*aig_m
;
438 ordered_latches
[it
.first
] = aig_l
-1;
439 if (init_map
.count(it
.first
) == 0)
440 aig_latchinit
.push_back(2);
442 aig_latchinit
.push_back(init_map
.at(it
.first
) ? 1 : 0);
445 //if (!initstate_bits.empty() || !init_inputs.empty()) {
447 // initstate_ff = 2*aig_m+1;
448 // aig_latchinit.push_back(0);
453 // for (auto it : ff_map)
455 // int l = ordered_latches[it.first];
457 // if (aig_latchinit.at(l) == 1)
458 // aig_map[it.first] ^= 1;
460 // if (aig_latchinit.at(l) == 2)
462 // int gated_ffout = mkgate(aig_map[it.first], initstate_ff^1);
463 // int gated_initin = mkgate(init_inputs[it.first], initstate_ff);
464 // aig_map[it.first] = mkgate(gated_ffout^1, gated_initin^1)^1;
469 for (auto it
: ff_map
) {
470 int a
= bit2aig(it
.second
);
471 int l
= ordered_latches
[it
.first
];
472 if (zinit_mode
&& aig_latchinit
.at(l
) == 1)
473 aig_latchin
.push_back(a
^ 1);
475 aig_latchin
.push_back(a
);
478 //if (!initstate_bits.empty() || !init_inputs.empty())
479 // aig_latchin.push_back(1);
481 for (auto &c
: co_bits
) {
482 RTLIL::SigBit bit
= c
.first
;
484 ordered_outputs
[bit
] = c
.second
;
485 aig_outputs
.push_back(bit2aig(bit
));
488 for (auto bit
: output_bits
) {
489 ordered_outputs
[bit
] = aig_o
++;
490 aig_outputs
.push_back(bit2aig(bit
));
493 if (omode
&& output_bits
.empty()) {
495 aig_outputs
.push_back(0);
500 aig_outputs
.push_back(0);
504 void write_aiger(std::ostream
&f
, bool ascii_mode
, bool miter_mode
, bool symbols_mode
, bool omode
)
507 int aig_obcj
= aig_obc
;
508 int aig_obcjf
= aig_obcj
;
510 log_assert(aig_m
== aig_i
+ aig_l
+ aig_a
);
511 log_assert(aig_l
== GetSize(aig_latchin
));
512 log_assert(aig_l
== GetSize(aig_latchinit
));
513 log_assert(aig_obcjf
== GetSize(aig_outputs
));
515 f
<< stringf("%s %d %d %d %d %d", ascii_mode
? "aag" : "aig", aig_m
, aig_i
, aig_l
, aig_o
, aig_a
);
520 for (int i
= 0; i
< aig_i
; i
++)
521 f
<< stringf("%d\n", 2*i
+2);
523 for (int i
= 0; i
< aig_l
; i
++) {
524 if (zinit_mode
|| aig_latchinit
.at(i
) == 0)
525 f
<< stringf("%d %d\n", 2*(aig_i
+i
)+2, aig_latchin
.at(i
));
526 else if (aig_latchinit
.at(i
) == 1)
527 f
<< stringf("%d %d 1\n", 2*(aig_i
+i
)+2, aig_latchin
.at(i
));
528 else if (aig_latchinit
.at(i
) == 2)
529 f
<< stringf("%d %d %d\n", 2*(aig_i
+i
)+2, aig_latchin
.at(i
), 2*(aig_i
+i
)+2);
532 for (int i
= 0; i
< aig_obc
; i
++)
533 f
<< stringf("%d\n", aig_outputs
.at(i
));
535 for (int i
= aig_obc
; i
< aig_obcj
; i
++)
538 for (int i
= aig_obc
; i
< aig_obcj
; i
++)
539 f
<< stringf("%d\n", aig_outputs
.at(i
));
541 for (int i
= aig_obcj
; i
< aig_obcjf
; i
++)
542 f
<< stringf("%d\n", aig_outputs
.at(i
));
544 for (int i
= 0; i
< aig_a
; i
++)
545 f
<< stringf("%d %d %d\n", 2*(aig_i
+aig_l
+i
)+2, aig_gates
.at(i
).first
, aig_gates
.at(i
).second
);
549 for (int i
= 0; i
< aig_l
; i
++) {
550 if (zinit_mode
|| aig_latchinit
.at(i
) == 0)
551 f
<< stringf("%d\n", aig_latchin
.at(i
));
552 else if (aig_latchinit
.at(i
) == 1)
553 f
<< stringf("%d 1\n", aig_latchin
.at(i
));
554 else if (aig_latchinit
.at(i
) == 2)
555 f
<< stringf("%d %d\n", aig_latchin
.at(i
), 2*(aig_i
+i
)+2);
558 for (int i
= 0; i
< aig_obc
; i
++)
559 f
<< stringf("%d\n", aig_outputs
.at(i
));
561 for (int i
= aig_obc
; i
< aig_obcj
; i
++)
564 for (int i
= aig_obc
; i
< aig_obcj
; i
++)
565 f
<< stringf("%d\n", aig_outputs
.at(i
));
567 for (int i
= aig_obcj
; i
< aig_obcjf
; i
++)
568 f
<< stringf("%d\n", aig_outputs
.at(i
));
570 for (int i
= 0; i
< aig_a
; i
++) {
571 int lhs
= 2*(aig_i
+aig_l
+i
)+2;
572 int rhs0
= aig_gates
.at(i
).first
;
573 int rhs1
= aig_gates
.at(i
).second
;
574 int delta0
= lhs
- rhs0
;
575 int delta1
= rhs0
- rhs1
;
576 aiger_encode(f
, delta0
);
577 aiger_encode(f
, delta1
);
583 dict
<string
, vector
<string
>> symbols
;
585 bool output_seen
= false;
586 for (auto wire
: module
->wires())
588 //if (wire->name[0] == '$')
591 SigSpec sig
= sigmap(wire
);
593 for (int i
= 0; i
< GetSize(wire
); i
++)
595 RTLIL::SigBit
b(wire
, i
);
596 if (input_bits
.count(b
)) {
597 int a
= aig_map
.at(sig
[i
]);
598 log_assert((a
& 1) == 0);
599 if (GetSize(wire
) != 1)
600 symbols
[stringf("i%d", (a
>> 1)-1)].push_back(stringf("%s[%d]", log_id(wire
), i
));
602 symbols
[stringf("i%d", (a
>> 1)-1)].push_back(stringf("%s", log_id(wire
)));
605 if (output_bits
.count(b
)) {
606 int o
= ordered_outputs
.at(b
);
607 output_seen
= !miter_mode
;
608 if (GetSize(wire
) != 1)
609 symbols
[stringf("%c%d", miter_mode
? 'b' : 'o', o
)].push_back(stringf("%s[%d]", log_id(wire
), i
));
611 symbols
[stringf("%c%d", miter_mode
? 'b' : 'o', o
)].push_back(stringf("%s", log_id(wire
)));
614 //if (init_inputs.count(sig[i])) {
615 // int a = init_inputs.at(sig[i]);
616 // log_assert((a & 1) == 0);
617 // if (GetSize(wire) != 1)
618 // symbols[stringf("i%d", (a >> 1)-1)].push_back(stringf("init:%s[%d]", log_id(wire), i));
620 // symbols[stringf("i%d", (a >> 1)-1)].push_back(stringf("init:%s", log_id(wire)));
623 if (ordered_latches
.count(sig
[i
])) {
624 int l
= ordered_latches
.at(sig
[i
]);
625 const char *p
= (zinit_mode
&& (aig_latchinit
.at(l
) == 1)) ? "!" : "";
626 if (GetSize(wire
) != 1)
627 symbols
[stringf("l%d", l
)].push_back(stringf("%s%s[%d]", p
, log_id(wire
), i
));
629 symbols
[stringf("l%d", l
)].push_back(stringf("%s%s", p
, log_id(wire
)));
634 if (omode
&& !output_seen
)
635 symbols
["o0"].push_back("__dummy_o__");
639 for (auto &sym
: symbols
) {
641 std::sort(sym
.second
.begin(), sym
.second
.end());
642 for (auto &s
: sym
.second
)
650 if (!box_list
.empty()) {
651 std::stringstream h_buffer
;
652 auto write_h_buffer
= [&h_buffer
](int i32
) {
653 // TODO: Don't assume we're on little endian
655 int i32_be
= _byteswap_ulong(i32
);
657 int i32_be
= __builtin_bswap32(i32
);
659 h_buffer
.write(reinterpret_cast<const char*>(&i32_be
), sizeof(i32_be
));
661 int num_outputs
= output_bits
.size();
662 if (omode
&& num_outputs
== 0)
665 write_h_buffer(input_bits
.size() + ci_bits
.size());
666 write_h_buffer(num_outputs
+ co_bits
.size());
667 write_h_buffer(input_bits
.size());
668 write_h_buffer(num_outputs
);
669 write_h_buffer(box_list
.size());
671 RTLIL::Module
*holes_module
= nullptr;
672 holes_module
= module
->design
->addModule("\\__holes__");
673 log_assert(holes_module
);
675 for (auto cell
: box_list
) {
676 RTLIL::Module
* box_module
= module
->design
->module(cell
->type
);
677 int box_inputs
= 0, box_outputs
= 0;
678 Cell
*holes_cell
= nullptr;
679 if (box_module
->get_bool_attribute("\\whitebox"))
680 holes_cell
= holes_module
->addCell(cell
->name
, cell
->type
);
682 RTLIL::Wire
*holes_wire
;
683 // TODO: Only sort once
684 box_module
->wires_
.sort(RTLIL::sort_by_id_str());
685 for (const auto w
: box_module
->wires()) {
686 RTLIL::SigSpec port_wire
;
688 for (int i
= 0; i
< GetSize(w
); i
++) {
690 holes_wire
= holes_module
->wire(stringf("\\i%d", box_inputs
));
692 holes_wire
= holes_module
->addWire(stringf("\\i%d", box_inputs
));
693 holes_wire
->port_input
= true;
696 port_wire
.append(holes_wire
);
698 if (!port_wire
.empty())
699 holes_cell
->setPort(w
->name
, port_wire
);
701 if (w
->port_output
) {
702 box_outputs
+= GetSize(w
);
703 for (int i
= 0; i
< GetSize(w
); i
++) {
705 holes_wire
= holes_module
->addWire(stringf("%s.%s", cell
->name
.c_str(), w
->name
.c_str()));
707 holes_wire
= holes_module
->addWire(stringf("%s.%s[%d]", cell
->name
.c_str(), w
->name
.c_str(), i
));
708 holes_wire
->port_output
= true;
710 port_wire
.append(holes_wire
);
712 holes_module
->connect(holes_wire
, RTLIL::S0
);
714 if (!port_wire
.empty())
715 holes_cell
->setPort(w
->name
, port_wire
);
719 write_h_buffer(box_inputs
);
720 write_h_buffer(box_outputs
);
721 write_h_buffer(box_module
->attributes
.at("\\abc_box_id").as_int());
722 write_h_buffer(0 /* OldBoxNum */);
726 std::string buffer_str
= h_buffer
.str();
727 // TODO: Don't assume we're on little endian
729 int buffer_size_be
= _byteswap_ulong(buffer_str
.size());
731 int buffer_size_be
= __builtin_bswap32(buffer_str
.size());
733 f
.write(reinterpret_cast<const char*>(&buffer_size_be
), sizeof(buffer_size_be
));
734 f
.write(buffer_str
.data(), buffer_str
.size());
737 holes_module
->fixup_ports();
739 holes_module
->design
->selection_stack
.emplace_back(false);
740 RTLIL::Selection
& sel
= holes_module
->design
->selection_stack
.back();
741 sel
.select(holes_module
);
743 // TODO: Should not need to opt_merge if we only instantiate
744 // each box type once...
745 Pass::call(holes_module
->design
, "opt_merge -share_all");
747 Pass::call(holes_module
->design
, "flatten -wb");
749 // TODO: Should techmap all lib_whitebox-es once
750 //Pass::call(holes_module->design, "techmap");
752 Pass::call(holes_module
->design
, "aigmap");
753 Pass::call(holes_module
->design
, "clean -purge");
755 holes_module
->design
->selection_stack
.pop_back();
757 std::stringstream a_buffer
;
758 XAigerWriter
writer(holes_module
, false /*zinit_mode*/, false /*imode*/, false /*omode*/, false /*bmode*/, true /* holes_mode */);
759 writer
.write_aiger(a_buffer
, false /*ascii_mode*/, false /*miter_mode*/, false /*symbols_mode*/, false /*omode*/);
762 std::string buffer_str
= a_buffer
.str();
763 // TODO: Don't assume we're on little endian
765 int buffer_size_be
= _byteswap_ulong(buffer_str
.size());
767 int buffer_size_be
= __builtin_bswap32(buffer_str
.size());
769 f
.write(reinterpret_cast<const char*>(&buffer_size_be
), sizeof(buffer_size_be
));
770 f
.write(buffer_str
.data(), buffer_str
.size());
771 holes_module
->design
->remove(holes_module
);
774 std::stringstream r_buffer
;
775 auto write_r_buffer
= [&r_buffer
](int i32
) {
776 // TODO: Don't assume we're on little endian
778 int i32_be
= _byteswap_ulong(i32
);
780 int i32_be
= __builtin_bswap32(i32
);
782 r_buffer
.write(reinterpret_cast<const char*>(&i32_be
), sizeof(i32_be
));
787 buffer_str
= r_buffer
.str();
788 // TODO: Don't assume we're on little endian
790 buffer_size_be
= _byteswap_ulong(buffer_str
.size());
792 buffer_size_be
= __builtin_bswap32(buffer_str
.size());
794 f
.write(reinterpret_cast<const char*>(&buffer_size_be
), sizeof(buffer_size_be
));
795 f
.write(buffer_str
.data(), buffer_str
.size());
798 f
<< stringf("Generated by %s\n", yosys_version_str
);
801 void write_map(std::ostream
&f
, bool verbose_map
, bool omode
)
803 dict
<int, string
> input_lines
;
804 dict
<int, string
> init_lines
;
805 dict
<int, string
> output_lines
;
806 dict
<int, string
> latch_lines
;
807 dict
<int, string
> wire_lines
;
809 for (auto wire
: module
->wires())
811 //if (!verbose_map && wire->name[0] == '$')
814 SigSpec sig
= sigmap(wire
);
816 for (int i
= 0; i
< GetSize(wire
); i
++)
818 RTLIL::SigBit
b(wire
, i
);
819 if (input_bits
.count(b
)) {
820 int a
= aig_map
.at(b
);
821 log_assert((a
& 1) == 0);
822 input_lines
[a
] += stringf("input %d %d %s\n", (a
>> 1)-1, i
, log_id(wire
));
825 if (output_bits
.count(b
)) {
826 int o
= ordered_outputs
.at(b
);
827 output_lines
[o
] += stringf("output %d %d %s\n", o
, i
, log_id(wire
));
831 //if (init_inputs.count(sig[i])) {
832 // int a = init_inputs.at(sig[i]);
833 // log_assert((a & 1) == 0);
834 // init_lines[a] += stringf("init %d %d %s\n", (a >> 1)-1, i, log_id(wire));
838 if (ordered_latches
.count(sig
[i
])) {
839 int l
= ordered_latches
.at(sig
[i
]);
840 if (zinit_mode
&& (aig_latchinit
.at(l
) == 1))
841 latch_lines
[l
] += stringf("invlatch %d %d %s\n", l
, i
, log_id(wire
));
843 latch_lines
[l
] += stringf("latch %d %d %s\n", l
, i
, log_id(wire
));
848 if (aig_map
.count(sig
[i
]) == 0)
851 int a
= aig_map
.at(sig
[i
]);
852 wire_lines
[a
] += stringf("wire %d %d %s\n", a
, i
, log_id(wire
));
857 for (const auto &c
: ci_bits
) {
858 RTLIL::SigBit b
= c
.first
;
859 RTLIL::Wire
*wire
= b
.wire
;
862 log_assert((a
& 1) == 0);
863 input_lines
[a
] += stringf("input %d %d %s\n", (a
>> 1)-1, i
, log_id(wire
));
866 for (const auto &c
: co_bits
) {
867 RTLIL::SigBit b
= c
.first
;
868 RTLIL::Wire
*wire
= b
.wire
;
871 output_lines
[o
] += stringf("output %d %d %s\n", o
, b
.offset
, log_id(wire
));
873 output_lines
[o
] += stringf("output %d %d __const%d__\n", o
, 0, b
.data
);
877 for (auto &it
: input_lines
)
879 log_assert(input_lines
.size() == input_bits
.size() + ci_bits
.size());
882 for (auto &it
: init_lines
)
886 for (auto &it
: output_lines
)
888 log_assert(output_lines
.size() == output_bits
.size() + co_bits
.size());
889 if (omode
&& output_bits
.empty())
890 f
<< "output " << output_lines
.size() << " 0 __dummy_o__\n";
893 for (auto &it
: latch_lines
)
897 for (auto &it
: wire_lines
)
902 struct XAigerBackend
: public Backend
{
903 XAigerBackend() : Backend("xaiger", "write design to XAIGER file") { }
904 void help() YS_OVERRIDE
906 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
908 log(" write_xaiger [options] [filename]\n");
910 log("Write the current design to an XAIGER file. The design must be flattened and\n");
911 log("all unsupported cells will be converted into psuedo-inputs and pseudo-outputs.\n");
914 log(" write ASCII version of AIGER format\n");
917 log(" convert FFs to zero-initialized FFs, adding additional inputs for\n");
918 log(" uninitialized FFs.\n");
921 log(" include a symbol table in the generated AIGER file\n");
923 log(" -map <filename>\n");
924 log(" write an extra file with port and latch symbols\n");
926 log(" -vmap <filename>\n");
927 log(" like -map, but more verbose\n");
929 log(" -I, -O, -B\n");
930 log(" If the design contains no input/output/assert then create one\n");
931 log(" dummy input/output/bad_state pin to make the tools reading the\n");
932 log(" AIGER file happy.\n");
935 void execute(std::ostream
*&f
, std::string filename
, std::vector
<std::string
> args
, RTLIL::Design
*design
) YS_OVERRIDE
937 bool ascii_mode
= false;
938 bool zinit_mode
= false;
939 bool miter_mode
= false;
940 bool symbols_mode
= false;
941 bool verbose_map
= false;
945 std::string map_filename
;
947 log_header(design
, "Executing XAIGER backend.\n");
950 for (argidx
= 1; argidx
< args
.size(); argidx
++)
952 if (args
[argidx
] == "-ascii") {
956 if (args
[argidx
] == "-zinit") {
960 if (args
[argidx
] == "-symbols") {
964 if (map_filename
.empty() && args
[argidx
] == "-map" && argidx
+1 < args
.size()) {
965 map_filename
= args
[++argidx
];
968 if (map_filename
.empty() && args
[argidx
] == "-vmap" && argidx
+1 < args
.size()) {
969 map_filename
= args
[++argidx
];
973 if (args
[argidx
] == "-I") {
977 if (args
[argidx
] == "-O") {
981 if (args
[argidx
] == "-B") {
987 extra_args(f
, filename
, args
, argidx
);
989 Module
*top_module
= design
->top_module();
991 if (top_module
== nullptr)
992 log_error("Can't find top module in current design!\n");
994 XAigerWriter
writer(top_module
, zinit_mode
, imode
, omode
, bmode
);
995 writer
.write_aiger(*f
, ascii_mode
, miter_mode
, symbols_mode
, omode
);
997 if (!map_filename
.empty()) {
999 mapf
.open(map_filename
.c_str(), std::ofstream::trunc
);
1001 log_error("Can't open file `%s' for writing: %s\n", map_filename
.c_str(), strerror(errno
));
1002 writer
.write_map(mapf
, verbose_map
, omode
);
1007 PRIVATE_NAMESPACE_END