77659b4d8a5a84be0a7c062fc2b724a32c66f5de
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
, external_bits
;
82 dict
<SigBit
, SigBit
> not_map
, alias_map
;
83 dict
<SigBit
, pair
<SigBit
, SigBit
>> and_map
;
84 vector
<SigBit
> ci_bits
, co_bits
;
85 dict
<SigBit
, std::tuple
<SigBit
,int,int>> ff_bits
;
86 dict
<SigBit
, float> arrival_times
;
88 vector
<pair
<int, int>> aig_gates
;
89 vector
<int> aig_outputs
;
90 int aig_m
= 0, aig_i
= 0, aig_l
= 0, aig_o
= 0, aig_a
= 0;
92 dict
<SigBit
, int> aig_map
;
93 dict
<SigBit
, int> ordered_outputs
;
95 vector
<Cell
*> box_list
;
96 dict
<IdString
, std::vector
<IdString
>> box_ports
;
98 int mkgate(int a0
, int a1
)
101 aig_gates
.push_back(a0
> a1
? make_pair(a0
, a1
) : make_pair(a1
, a0
));
105 int bit2aig(SigBit bit
)
107 auto it
= aig_map
.find(bit
);
108 if (it
!= aig_map
.end()) {
109 log_assert(it
->second
>= 0);
113 // NB: Cannot use iterator returned from aig_map.insert()
114 // since this function is called recursively
117 if (not_map
.count(bit
)) {
118 a
= bit2aig(not_map
.at(bit
)) ^ 1;
120 if (and_map
.count(bit
)) {
121 auto args
= and_map
.at(bit
);
122 int a0
= bit2aig(args
.first
);
123 int a1
= bit2aig(args
.second
);
126 if (alias_map
.count(bit
)) {
127 a
= bit2aig(alias_map
.at(bit
));
130 if (bit
== State::Sx
|| bit
== State::Sz
) {
131 log_debug("Design contains 'x' or 'z' bits. Treating as 1'b0.\n");
132 a
= aig_map
.at(State::S0
);
140 XAigerWriter(Module
*module
) : module(module
), sigmap(module
)
142 pool
<SigBit
> undriven_bits
;
143 pool
<SigBit
> unused_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 keep wires
156 for (auto wire
: module
->wires())
157 if (wire
->get_bool_attribute(ID::keep
))
160 // First, collect all the ports in port_id order
161 // since module->wires() could be sorted
163 for (auto port
: module
->ports
) {
164 auto wire
= module
->wire(port
);
166 for (int i
= 0; i
< GetSize(wire
); i
++)
168 SigBit
wirebit(wire
, i
);
169 SigBit bit
= sigmap(wirebit
);
171 if (bit
.wire
== nullptr) {
172 if (wire
->port_output
) {
173 aig_map
[wirebit
] = (bit
== State::S1
) ? 1 : 0;
174 output_bits
.insert(wirebit
);
179 if (wire
->port_input
)
180 input_bits
.insert(bit
);
182 if (wire
->port_output
) {
184 alias_map
[wirebit
] = bit
;
185 output_bits
.insert(wirebit
);
190 for (auto wire
: module
->wires())
191 for (int i
= 0; i
< GetSize(wire
); i
++)
193 SigBit
wirebit(wire
, i
);
194 SigBit bit
= sigmap(wirebit
);
197 undriven_bits
.insert(bit
);
198 unused_bits
.insert(bit
);
202 // TODO: Speed up toposort -- ultimately we care about
203 // box ordering, but not individual AIG cells
204 dict
<SigBit
, pool
<IdString
>> bit_drivers
, bit_users
;
205 TopoSort
<IdString
, RTLIL::sort_by_id_str
> toposort
;
206 bool abc9_box_seen
= false;
207 std::vector
<Cell
*> flop_boxes
;
209 for (auto cell
: module
->selected_cells()) {
210 if (cell
->type
== "$_NOT_")
212 SigBit A
= sigmap(cell
->getPort("\\A").as_bit());
213 SigBit Y
= sigmap(cell
->getPort("\\Y").as_bit());
214 unused_bits
.erase(A
);
215 undriven_bits
.erase(Y
);
217 toposort
.node(cell
->name
);
218 bit_users
[A
].insert(cell
->name
);
219 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
);
232 toposort
.node(cell
->name
);
233 bit_users
[A
].insert(cell
->name
);
234 bit_users
[B
].insert(cell
->name
);
235 bit_drivers
[Y
].insert(cell
->name
);
239 if (cell
->type
== "$__ABC9_FF_")
241 SigBit D
= sigmap(cell
->getPort("\\D").as_bit());
242 SigBit Q
= sigmap(cell
->getPort("\\Q").as_bit());
243 unused_bits
.erase(D
);
244 undriven_bits
.erase(Q
);
246 auto r
= ff_bits
.insert(std::make_pair(D
, std::make_tuple(Q
, 0, 2)));
247 log_assert(r
.second
);
251 RTLIL::Module
* inst_module
= module
->design
->module(cell
->type
);
253 bool abc9_box
= inst_module
->attributes
.count("\\abc9_box_id") && !cell
->get_bool_attribute("\\abc9_keep");
255 for (const auto &conn
: cell
->connections()) {
256 auto port_wire
= inst_module
->wire(conn
.first
);
258 if (port_wire
->port_output
) {
260 auto it
= port_wire
->attributes
.find("\\abc9_arrival");
261 if (it
!= port_wire
->attributes
.end()) {
262 if (it
->second
.flags
!= 0)
263 log_error("Attribute 'abc9_arrival' on port '%s' of module '%s' is not an integer.\n", log_id(port_wire
), log_id(cell
->type
));
264 arrival
= it
->second
.as_int();
267 for (auto bit
: sigmap(conn
.second
))
268 arrival_times
[bit
] = arrival
;
272 // Ignore inout for the sake of topographical ordering
273 if (port_wire
->port_input
&& !port_wire
->port_output
)
274 for (auto bit
: sigmap(conn
.second
))
275 bit_users
[bit
].insert(cell
->name
);
276 if (port_wire
->port_output
)
277 for (auto bit
: sigmap(conn
.second
))
278 bit_drivers
[bit
].insert(cell
->name
);
283 abc9_box_seen
= true;
285 toposort
.node(cell
->name
);
287 if (inst_module
->get_bool_attribute("\\abc9_flop"))
288 flop_boxes
.push_back(cell
);
293 bool cell_known
= inst_module
|| cell
->known();
294 for (const auto &c
: cell
->connections()) {
295 if (c
.second
.is_fully_const()) continue;
296 auto port_wire
= inst_module
? inst_module
->wire(c
.first
) : nullptr;
297 auto is_input
= (port_wire
&& port_wire
->port_input
) || !cell_known
|| cell
->input(c
.first
);
298 auto is_output
= (port_wire
&& port_wire
->port_output
) || !cell_known
|| cell
->output(c
.first
);
299 if (!is_input
&& !is_output
)
300 log_error("Connection '%s' on cell '%s' (type '%s') not recognised!\n", log_id(c
.first
), log_id(cell
), log_id(cell
->type
));
303 for (auto b
: c
.second
) {
306 if (!w
->port_output
|| !cell_known
) {
307 SigBit I
= sigmap(b
);
310 output_bits
.insert(b
);
315 //log_warning("Unsupported cell type: %s (%s)\n", log_id(cell->type), log_id(cell));
319 dict
<IdString
, std::pair
<IdString
,int>> flop_q
;
320 for (auto cell
: flop_boxes
) {
321 auto r
= flop_q
.insert(std::make_pair(cell
->type
, std::make_pair(IdString(), 0)));
324 for (const auto &conn
: cell
->connections()) {
325 const SigSpec
&rhs
= conn
.second
;
328 if (!ff_bits
.count(rhs
))
330 r
.first
->second
.first
= conn
.first
;
331 Module
*inst_module
= module
->design
->module(cell
->type
);
332 Wire
*wire
= inst_module
->wire(conn
.first
);
334 auto jt
= wire
->attributes
.find("\\abc9_arrival");
335 if (jt
!= wire
->attributes
.end()) {
336 if (jt
->second
.flags
!= 0)
337 log_error("Attribute 'abc9_arrival' on port '%s' of module '%s' is not an integer.\n", log_id(wire
), log_id(cell
->type
));
338 r
.first
->second
.second
= jt
->second
.as_int();
341 log_assert(d
== sigmap(d
));
346 d
= cell
->getPort(r
.first
->second
.first
);
348 auto &rhs
= ff_bits
.at(d
);
350 auto it
= cell
->attributes
.find(ID(abc9_mergeability
));
351 log_assert(it
!= cell
->attributes
.end());
352 std::get
<1>(rhs
) = it
->second
.as_int();
353 cell
->attributes
.erase(it
);
355 it
= cell
->attributes
.find(ID(abc9_init
));
356 log_assert(it
!= cell
->attributes
.end());
357 log_assert(GetSize(it
->second
) == 1);
358 if (it
->second
[0] == State::S1
)
359 std::get
<2>(rhs
) = 1;
360 else if (it
->second
[0] == State::S0
)
361 std::get
<2>(rhs
) = 0;
363 log_assert(it
->second
[0] == State::Sx
);
364 std::get
<2>(rhs
) = 0;
366 cell
->attributes
.erase(it
);
368 const SigBit
&q
= std::get
<0>(rhs
);
369 auto arrival
= r
.first
->second
.second
;
371 arrival_times
[q
] = arrival
;
374 for (auto &it
: bit_users
)
375 if (bit_drivers
.count(it
.first
))
376 for (auto driver_cell
: bit_drivers
.at(it
.first
))
377 for (auto user_cell
: it
.second
)
378 toposort
.edge(driver_cell
, user_cell
);
381 toposort
.analyze_loops
= true;
383 bool no_loops
YS_ATTRIBUTE(unused
) = toposort
.sort();
386 for (auto &it
: toposort
.loops
) {
387 log(" loop %d\n", i
++);
388 for (auto cell_name
: it
) {
389 auto cell
= module
->cell(cell_name
);
391 log("\t%s (%s @ %s)\n", log_id(cell
), log_id(cell
->type
), cell
->get_src_attribute().c_str());
395 log_assert(no_loops
);
397 for (auto cell_name
: toposort
.sorted
) {
398 RTLIL::Cell
*cell
= module
->cell(cell_name
);
401 RTLIL::Module
* box_module
= module
->design
->module(cell
->type
);
402 if (!box_module
|| !box_module
->attributes
.count("\\abc9_box_id")
403 || cell
->get_bool_attribute("\\abc9_keep"))
406 bool blackbox
= box_module
->get_blackbox_attribute(true /* ignore_wb */);
408 auto r
= box_ports
.insert(cell
->type
);
410 // Make carry in the last PI, and carry out the last PO
411 // since ABC requires it this way
412 IdString carry_in
, carry_out
;
413 for (const auto &port_name
: box_module
->ports
) {
414 auto w
= box_module
->wire(port_name
);
416 if (w
->get_bool_attribute("\\abc9_carry")) {
418 if (carry_in
!= IdString())
419 log_error("Module '%s' contains more than one 'abc9_carry' input port.\n", log_id(box_module
));
420 carry_in
= port_name
;
422 if (w
->port_output
) {
423 if (carry_out
!= IdString())
424 log_error("Module '%s' contains more than one 'abc9_carry' output port.\n", log_id(box_module
));
425 carry_out
= port_name
;
429 r
.first
->second
.push_back(port_name
);
432 if (carry_in
!= IdString() && carry_out
== IdString())
433 log_error("Module '%s' contains an 'abc9_carry' input port but no output port.\n", log_id(box_module
));
434 if (carry_in
== IdString() && carry_out
!= IdString())
435 log_error("Module '%s' contains an 'abc9_carry' output port but no input port.\n", log_id(box_module
));
436 if (carry_in
!= IdString()) {
437 log_assert(carry_out
!= IdString());
438 r
.first
->second
.push_back(carry_in
);
439 r
.first
->second
.push_back(carry_out
);
443 // Fully pad all unused input connections of this box cell with S0
444 // Fully pad all undriven output connections of this box cell with anonymous wires
445 for (auto port_name
: r
.first
->second
) {
446 auto w
= box_module
->wire(port_name
);
448 auto it
= cell
->connections_
.find(port_name
);
451 if (it
!= cell
->connections_
.end()) {
452 if (GetSize(it
->second
) < GetSize(w
))
453 it
->second
.append(RTLIL::SigSpec(State::S0
, GetSize(w
)-GetSize(it
->second
)));
457 rhs
= RTLIL::SigSpec(State::S0
, GetSize(w
));
458 cell
->setPort(port_name
, rhs
);
462 SigBit I
= sigmap(b
);
467 alias_map
[b
] = State::S0
;
471 co_bits
.emplace_back(b
);
472 unused_bits
.erase(I
);
475 if (w
->port_output
) {
477 auto it
= cell
->connections_
.find(w
->name
);
478 if (it
!= cell
->connections_
.end()) {
479 if (GetSize(it
->second
) < GetSize(w
))
480 it
->second
.append(module
->addWire(NEW_ID
, GetSize(w
)-GetSize(it
->second
)));
484 Wire
*wire
= module
->addWire(NEW_ID
, GetSize(w
));
486 wire
->set_bool_attribute(ID(abc9_padding
));
488 cell
->setPort(port_name
, rhs
);
491 for (const auto &b
: rhs
.bits()) {
492 SigBit O
= sigmap(b
);
495 ci_bits
.emplace_back(b
);
496 undriven_bits
.erase(O
);
501 // Connect <cell>.$abc9_currQ (inserted by abc9_map.v) as an input to the flop box
502 if (box_module
->get_bool_attribute("\\abc9_flop")) {
503 SigSpec rhs
= module
->wire(stringf("%s.$abc9_currQ", cell
->name
.c_str()));
505 log_error("'%s.$abc9_currQ' is not a wire present in module '%s'.\n", log_id(cell
), log_id(module
));
508 SigBit I
= sigmap(b
);
513 alias_map
[b
] = State::S0
;
517 co_bits
.emplace_back(b
);
518 unused_bits
.erase(I
);
522 box_list
.emplace_back(cell
);
525 // TODO: Free memory from toposort, bit_drivers, bit_users
528 for (auto bit
: input_bits
)
529 undriven_bits
.erase(sigmap(bit
));
530 for (auto bit
: output_bits
)
531 unused_bits
.erase(sigmap(bit
));
532 for (auto bit
: unused_bits
)
533 undriven_bits
.erase(bit
);
535 // Make all undriven bits a primary input
536 for (auto bit
: undriven_bits
) {
537 input_bits
.insert(bit
);
538 undriven_bits
.erase(bit
);
541 aig_map
[State::S0
] = 0;
542 aig_map
[State::S1
] = 1;
544 // pool<> iterates in LIFO order...
545 for (int i
= input_bits
.size()-1; i
>= 0; i
--) {
546 const auto &bit
= *input_bits
.element(i
);
548 log_assert(!aig_map
.count(bit
));
549 aig_map
[bit
] = 2*aig_m
;
552 for (const auto &i
: ff_bits
) {
553 const SigBit
&q
= std::get
<0>(i
.second
);
555 log_assert(!aig_map
.count(q
));
556 aig_map
[q
] = 2*aig_m
;
559 for (auto &bit
: ci_bits
) {
561 log_assert(!aig_map
.count(bit
));
562 aig_map
[bit
] = 2*aig_m
;
565 for (auto bit
: co_bits
) {
566 ordered_outputs
[bit
] = aig_o
++;
567 aig_outputs
.push_back(bit2aig(bit
));
570 // pool<> iterates in LIFO order...
571 for (int i
= output_bits
.size()-1; i
>= 0; i
--) {
572 const auto &bit
= *output_bits
.element(i
);
573 ordered_outputs
[bit
] = aig_o
++;
574 aig_outputs
.push_back(bit2aig(bit
));
577 for (auto &i
: ff_bits
) {
578 const SigBit
&d
= i
.first
;
580 aig_outputs
.push_back(aig_map
.at(d
));
584 void write_aiger(std::ostream
&f
, bool ascii_mode
)
587 int aig_obcj
= aig_obc
;
588 int aig_obcjf
= aig_obcj
;
590 log_assert(aig_m
== aig_i
+ aig_l
+ aig_a
);
591 log_assert(aig_obcjf
== GetSize(aig_outputs
));
593 f
<< stringf("%s %d %d %d %d %d", ascii_mode
? "aag" : "aig", aig_m
, aig_i
, aig_l
, aig_o
, aig_a
);
598 for (int i
= 0; i
< aig_i
; i
++)
599 f
<< stringf("%d\n", 2*i
+2);
601 for (int i
= 0; i
< aig_obc
; i
++)
602 f
<< stringf("%d\n", aig_outputs
.at(i
));
604 for (int i
= aig_obc
; i
< aig_obcj
; i
++)
607 for (int i
= aig_obc
; i
< aig_obcj
; i
++)
608 f
<< stringf("%d\n", aig_outputs
.at(i
));
610 for (int i
= aig_obcj
; i
< aig_obcjf
; i
++)
611 f
<< stringf("%d\n", aig_outputs
.at(i
));
613 for (int i
= 0; i
< aig_a
; i
++)
614 f
<< stringf("%d %d %d\n", 2*(aig_i
+aig_l
+i
)+2, aig_gates
.at(i
).first
, aig_gates
.at(i
).second
);
618 for (int i
= 0; i
< aig_obc
; i
++)
619 f
<< stringf("%d\n", aig_outputs
.at(i
));
621 for (int i
= aig_obc
; i
< aig_obcj
; i
++)
624 for (int i
= aig_obc
; i
< aig_obcj
; i
++)
625 f
<< stringf("%d\n", aig_outputs
.at(i
));
627 for (int i
= aig_obcj
; i
< aig_obcjf
; i
++)
628 f
<< stringf("%d\n", aig_outputs
.at(i
));
630 for (int i
= 0; i
< aig_a
; i
++) {
631 int lhs
= 2*(aig_i
+aig_l
+i
)+2;
632 int rhs0
= aig_gates
.at(i
).first
;
633 int rhs1
= aig_gates
.at(i
).second
;
634 int delta0
= lhs
- rhs0
;
635 int delta1
= rhs0
- rhs1
;
636 aiger_encode(f
, delta0
);
637 aiger_encode(f
, delta1
);
643 auto write_buffer
= [](std::stringstream
&buffer
, int i32
) {
644 int32_t i32_be
= to_big_endian(i32
);
645 buffer
.write(reinterpret_cast<const char*>(&i32_be
), sizeof(i32_be
));
647 std::stringstream h_buffer
;
648 auto write_h_buffer
= std::bind(write_buffer
, std::ref(h_buffer
), std::placeholders::_1
);
650 log_debug("ciNum = %d\n", GetSize(input_bits
) + GetSize(ff_bits
) + GetSize(ci_bits
));
651 write_h_buffer(input_bits
.size() + ff_bits
.size() + ci_bits
.size());
652 log_debug("coNum = %d\n", GetSize(output_bits
) + GetSize(ff_bits
) + GetSize(co_bits
));
653 write_h_buffer(output_bits
.size() + GetSize(ff_bits
) + GetSize(co_bits
));
654 log_debug("piNum = %d\n", GetSize(input_bits
) + GetSize(ff_bits
));
655 write_h_buffer(input_bits
.size() + ff_bits
.size());
656 log_debug("poNum = %d\n", GetSize(output_bits
) + GetSize(ff_bits
));
657 write_h_buffer(output_bits
.size() + ff_bits
.size());
658 log_debug("boxNum = %d\n", GetSize(box_list
));
659 write_h_buffer(box_list
.size());
661 auto write_buffer_float
= [](std::stringstream
&buffer
, float f32
) {
662 buffer
.write(reinterpret_cast<const char*>(&f32
), sizeof(f32
));
664 std::stringstream i_buffer
;
665 auto write_i_buffer
= std::bind(write_buffer_float
, std::ref(i_buffer
), std::placeholders::_1
);
666 for (auto bit
: input_bits
)
667 write_i_buffer(arrival_times
.at(bit
, 0));
668 //std::stringstream o_buffer;
669 //auto write_o_buffer = std::bind(write_buffer_float, std::ref(o_buffer), std::placeholders::_1);
670 //for (auto bit : output_bits)
671 // write_o_buffer(0);
673 if (!box_list
.empty() || !ff_bits
.empty()) {
674 RTLIL::Module
*holes_module
= module
->design
->addModule("$__holes__");
675 log_assert(holes_module
);
677 dict
<IdString
, Cell
*> cell_cache
;
681 for (auto cell
: box_list
) {
682 RTLIL::Module
* orig_box_module
= module
->design
->module(cell
->type
);
683 log_assert(orig_box_module
);
684 IdString derived_name
= orig_box_module
->derive(module
->design
, cell
->parameters
);
685 RTLIL::Module
* box_module
= module
->design
->module(derived_name
);
686 if (box_module
->has_processes())
687 Pass::call_on_module(module
->design
, box_module
, "proc");
689 auto r
= cell_cache
.insert(std::make_pair(derived_name
, nullptr));
690 Cell
*holes_cell
= r
.first
->second
;
691 if (r
.second
&& box_module
->get_bool_attribute("\\whitebox")) {
692 holes_cell
= holes_module
->addCell(cell
->name
, cell
->type
);
693 holes_cell
->parameters
= cell
->parameters
;
694 r
.first
->second
= holes_cell
;
697 int box_inputs
= 0, box_outputs
= 0;
698 for (auto port_name
: box_ports
.at(cell
->type
)) {
699 RTLIL::Wire
*w
= box_module
->wire(port_name
);
701 RTLIL::Wire
*holes_wire
;
702 RTLIL::SigSpec port_sig
;
705 for (int i
= 0; i
< GetSize(w
); i
++) {
707 holes_wire
= holes_module
->wire(stringf("\\i%d", box_inputs
));
709 holes_wire
= holes_module
->addWire(stringf("\\i%d", box_inputs
));
710 holes_wire
->port_input
= true;
711 holes_wire
->port_id
= port_id
++;
712 holes_module
->ports
.push_back(holes_wire
->name
);
715 port_sig
.append(holes_wire
);
717 if (w
->port_output
) {
718 box_outputs
+= GetSize(w
);
719 for (int i
= 0; i
< GetSize(w
); i
++) {
721 holes_wire
= holes_module
->addWire(stringf("$abc%s.%s", cell
->name
.c_str(), log_id(w
->name
)));
723 holes_wire
= holes_module
->addWire(stringf("$abc%s.%s[%d]", cell
->name
.c_str(), log_id(w
->name
), i
));
724 holes_wire
->port_output
= true;
725 holes_wire
->port_id
= port_id
++;
726 holes_module
->ports
.push_back(holes_wire
->name
);
728 port_sig
.append(holes_wire
);
730 holes_module
->connect(holes_wire
, State::S0
);
733 if (!port_sig
.empty()) {
735 holes_cell
->setPort(w
->name
, port_sig
);
737 holes_module
->connect(holes_cell
->getPort(w
->name
), port_sig
);
741 // For flops only, create an extra 1-bit input that drives a new wire
742 // called "<cell>.$abc9_currQ" that is used below
743 if (box_module
->get_bool_attribute("\\abc9_flop")) {
744 log_assert(holes_cell
);
747 Wire
*holes_wire
= holes_module
->wire(stringf("\\i%d", box_inputs
));
749 holes_wire
= holes_module
->addWire(stringf("\\i%d", box_inputs
));
750 holes_wire
->port_input
= true;
751 holes_wire
->port_id
= port_id
++;
752 holes_module
->ports
.push_back(holes_wire
->name
);
754 Wire
*w
= holes_module
->addWire(stringf("%s.$abc9_currQ", cell
->name
.c_str()));
755 holes_module
->connect(w
, holes_wire
);
758 write_h_buffer(box_inputs
);
759 write_h_buffer(box_outputs
);
760 write_h_buffer(box_module
->attributes
.at("\\abc9_box_id").as_int());
761 write_h_buffer(box_count
++);
764 std::stringstream r_buffer
;
765 auto write_r_buffer
= std::bind(write_buffer
, std::ref(r_buffer
), std::placeholders::_1
);
766 log_debug("flopNum = %d\n", GetSize(ff_bits
));
767 write_r_buffer(ff_bits
.size());
769 std::stringstream s_buffer
;
770 auto write_s_buffer
= std::bind(write_buffer
, std::ref(s_buffer
), std::placeholders::_1
);
771 write_s_buffer(ff_bits
.size());
773 for (const auto &i
: ff_bits
) {
774 const SigBit
&q
= std::get
<0>(i
.second
);
775 int mergeability
= std::get
<1>(i
.second
);
776 log_assert(mergeability
> 0);
777 write_r_buffer(mergeability
);
778 int init
= std::get
<2>(i
.second
);
779 write_s_buffer(init
);
780 write_i_buffer(arrival_times
.at(q
, 0));
785 std::string buffer_str
= r_buffer
.str();
786 int32_t buffer_size_be
= to_big_endian(buffer_str
.size());
787 f
.write(reinterpret_cast<const char*>(&buffer_size_be
), sizeof(buffer_size_be
));
788 f
.write(buffer_str
.data(), buffer_str
.size());
791 buffer_str
= s_buffer
.str();
792 buffer_size_be
= to_big_endian(buffer_str
.size());
793 f
.write(reinterpret_cast<const char*>(&buffer_size_be
), sizeof(buffer_size_be
));
794 f
.write(buffer_str
.data(), buffer_str
.size());
799 // NB: fixup_ports() will sort ports by name
800 //holes_module->fixup_ports();
801 holes_module
->check();
803 // Cannot techmap/aigmap/check all lib_whitebox-es outside of write_xaiger
804 // since boxes may contain parameters in which case `flatten` would have
805 // created a new $paramod ...
806 Pass::call_on_module(holes_module
->design
, holes_module
, "flatten -wb; techmap; aigmap");
808 dict
<SigSig
, SigSig
> replace
;
809 for (auto it
= holes_module
->cells_
.begin(); it
!= holes_module
->cells_
.end(); ) {
810 auto cell
= it
->second
;
811 if (cell
->type
.in("$_DFF_N_", "$_DFF_NN0_", "$_DFF_NN1_", "$_DFF_NP0_", "$_DFF_NP1_",
812 "$_DFF_P_", "$_DFF_PN0_", "$_DFF_PN1", "$_DFF_PP0_", "$_DFF_PP1_")) {
813 SigBit D
= cell
->getPort("\\D");
814 SigBit Q
= cell
->getPort("\\Q");
815 // Remove the DFF cell from what needs to be a combinatorial box
816 it
= holes_module
->cells_
.erase(it
);
818 if (GetSize(Q
.wire
) == 1)
819 port
= holes_module
->wire(stringf("$abc%s", Q
.wire
->name
.c_str()));
821 port
= holes_module
->wire(stringf("$abc%s[%d]", Q
.wire
->name
.c_str(), Q
.offset
));
823 // Prepare to replace "assign <port> = DFF.Q;" with "assign <port> = DFF.D;"
824 // in order to extract the combinatorial control logic that feeds the box
825 // (i.e. clock enable, synchronous reset, etc.)
826 replace
.insert(std::make_pair(SigSig(port
,Q
), SigSig(port
,D
)));
827 // Since `flatten` above would have created wires named "<cell>.Q",
828 // extract the pre-techmap cell name
829 auto pos
= Q
.wire
->name
.str().rfind(".");
830 log_assert(pos
!= std::string::npos
);
831 IdString driver
= Q
.wire
->name
.substr(0, pos
);
832 // And drive the signal that was previously driven by "DFF.Q" (typically
833 // used to implement clock-enable functionality) with the "<cell>.$abc9_currQ"
834 // wire (which itself is driven an input port) we inserted above
835 Wire
*currQ
= holes_module
->wire(stringf("%s.$abc9_currQ", driver
.c_str()));
837 holes_module
->connect(Q
, currQ
);
840 else if (!cell
->type
.in("$_NOT_", "$_AND_"))
841 log_error("Whitebox contents cannot be represented as AIG. Please verify whiteboxes are synthesisable.\n");
845 for (auto &conn
: holes_module
->connections_
) {
846 auto it
= replace
.find(conn
);
847 if (it
!= replace
.end())
851 // Move into a new (temporary) design so that "clean" will only
852 // operate (and run checks on) this one module
853 RTLIL::Design
*holes_design
= new RTLIL::Design
;
854 module
->design
->modules_
.erase(holes_module
->name
);
855 holes_design
->add(holes_module
);
856 Pass::call(holes_design
, "opt -purge");
858 std::stringstream a_buffer
;
859 XAigerWriter
writer(holes_module
);
860 writer
.write_aiger(a_buffer
, false /*ascii_mode*/);
864 std::string buffer_str
= a_buffer
.str();
865 int32_t buffer_size_be
= to_big_endian(buffer_str
.size());
866 f
.write(reinterpret_cast<const char*>(&buffer_size_be
), sizeof(buffer_size_be
));
867 f
.write(buffer_str
.data(), buffer_str
.size());
874 std::string buffer_str
= h_buffer
.str();
875 int32_t buffer_size_be
= to_big_endian(buffer_str
.size());
876 f
.write(reinterpret_cast<const char*>(&buffer_size_be
), sizeof(buffer_size_be
));
877 f
.write(buffer_str
.data(), buffer_str
.size());
880 buffer_str
= i_buffer
.str();
881 buffer_size_be
= to_big_endian(buffer_str
.size());
882 f
.write(reinterpret_cast<const char*>(&buffer_size_be
), sizeof(buffer_size_be
));
883 f
.write(buffer_str
.data(), buffer_str
.size());
885 //buffer_str = o_buffer.str();
886 //buffer_size_be = to_big_endian(buffer_str.size());
887 //f.write(reinterpret_cast<const char*>(&buffer_size_be), sizeof(buffer_size_be));
888 //f.write(buffer_str.data(), buffer_str.size());
890 f
<< stringf("Generated by %s\n", yosys_version_str
);
892 module
->design
->scratchpad_set_int("write_xaiger.num_ands", and_map
.size());
893 module
->design
->scratchpad_set_int("write_xaiger.num_wires", aig_map
.size());
894 module
->design
->scratchpad_set_int("write_xaiger.num_inputs", input_bits
.size());
895 module
->design
->scratchpad_set_int("write_xaiger.num_outputs", output_bits
.size());
898 void write_map(std::ostream
&f
, bool verbose_map
)
900 dict
<int, string
> input_lines
;
901 dict
<int, string
> output_lines
;
902 dict
<int, string
> wire_lines
;
904 for (auto wire
: module
->wires())
906 //if (!verbose_map && wire->name[0] == '$')
909 SigSpec sig
= sigmap(wire
);
911 for (int i
= 0; i
< GetSize(wire
); i
++)
913 RTLIL::SigBit
b(wire
, i
);
914 if (input_bits
.count(b
)) {
915 int a
= aig_map
.at(b
);
916 log_assert((a
& 1) == 0);
917 input_lines
[a
] += stringf("input %d %d %s\n", (a
>> 1)-1, i
, log_id(wire
));
920 if (output_bits
.count(b
)) {
921 int o
= ordered_outputs
.at(b
);
923 output_lines
[o
] += stringf("output %d %d %s %d\n", o
- GetSize(co_bits
), i
, log_id(wire
), init
);
928 if (aig_map
.count(sig
[i
]) == 0)
931 int a
= aig_map
.at(sig
[i
]);
932 wire_lines
[a
] += stringf("wire %d %d %s\n", a
, i
, log_id(wire
));
938 for (auto &it
: input_lines
)
940 log_assert(input_lines
.size() == input_bits
.size());
943 for (auto cell
: box_list
)
944 f
<< stringf("box %d %d %s\n", box_count
++, 0, log_id(cell
->name
));
947 for (auto &it
: output_lines
)
949 log_assert(output_lines
.size() == output_bits
.size());
952 for (auto &it
: wire_lines
)
957 struct XAigerBackend
: public Backend
{
958 XAigerBackend() : Backend("xaiger", "write design to XAIGER file") { }
959 void help() YS_OVERRIDE
961 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
963 log(" write_xaiger [options] [filename]\n");
965 log("Write the current design to an XAIGER file. The design must be flattened and\n");
966 log("all unsupported cells will be converted into psuedo-inputs and pseudo-outputs.\n");
969 log(" write ASCII version of AIGER format\n");
971 log(" -map <filename>\n");
972 log(" write an extra file with port and box symbols\n");
974 log(" -vmap <filename>\n");
975 log(" like -map, but more verbose\n");
978 void execute(std::ostream
*&f
, std::string filename
, std::vector
<std::string
> args
, RTLIL::Design
*design
) YS_OVERRIDE
980 bool ascii_mode
= false;
981 bool verbose_map
= false;
982 std::string map_filename
;
984 log_header(design
, "Executing XAIGER backend.\n");
987 for (argidx
= 1; argidx
< args
.size(); argidx
++)
989 if (args
[argidx
] == "-ascii") {
993 if (map_filename
.empty() && args
[argidx
] == "-map" && argidx
+1 < args
.size()) {
994 map_filename
= args
[++argidx
];
997 if (map_filename
.empty() && args
[argidx
] == "-vmap" && argidx
+1 < args
.size()) {
998 map_filename
= args
[++argidx
];
1004 extra_args(f
, filename
, args
, argidx
, !ascii_mode
);
1006 Module
*top_module
= design
->top_module();
1008 if (top_module
== nullptr)
1009 log_error("Can't find top module in current design!\n");
1011 XAigerWriter
writer(top_module
);
1012 writer
.write_aiger(*f
, ascii_mode
);
1014 if (!map_filename
.empty()) {
1016 mapf
.open(map_filename
.c_str(), std::ofstream::trunc
);
1018 log_error("Can't open file `%s' for writing: %s\n", map_filename
.c_str(), strerror(errno
));
1019 writer
.write_map(mapf
, verbose_map
);
1024 PRIVATE_NAMESPACE_END