6f089447e47daa12154d7fddd00698874e094a9c
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 #include "kernel/register.h"
22 #include "kernel/sigtools.h"
23 #include "kernel/utils.h"
24 #include "kernel/celltypes.h"
27 PRIVATE_NAMESPACE_BEGIN
31 inline std::string
remap_name(RTLIL::IdString abc9_name
)
33 return stringf("$abc$%d$%s", map_autoidx
, abc9_name
.c_str()+1);
36 void break_scc(RTLIL::Module
*module
)
38 // For every unique SCC found, (arbitrarily) find the first
39 // cell in the component, and convert all wires driven by
40 // its output ports into a new PO, and drive its previous
41 // sinks with a new PI
42 pool
<RTLIL::Const
> ids_seen
;
43 for (auto cell
: module
->cells()) {
44 auto it
= cell
->attributes
.find(ID(abc9_scc_id
));
45 if (it
== cell
->attributes
.end())
47 auto r
= ids_seen
.insert(it
->second
);
48 cell
->attributes
.erase(it
);
51 for (auto &c
: cell
->connections_
) {
52 if (c
.second
.is_fully_const()) continue;
53 if (cell
->output(c
.first
)) {
54 SigBit b
= c
.second
.as_bit();
57 // In this case, hopefully the loop break has been already created
58 // Get the non-prefixed wire
59 Wire
*wo
= module
->wire(stringf("%s.abco", b
.wire
->name
.c_str()));
60 log_assert(wo
!= nullptr);
61 log_assert(wo
->port_output
);
62 log_assert(b
.offset
< GetSize(wo
));
63 c
.second
= RTLIL::SigBit(wo
, b
.offset
);
66 // Create a new output/input loop break
68 w
= module
->wire(stringf("%s.abco", w
->name
.c_str()));
70 w
= module
->addWire(stringf("%s.abco", b
.wire
->name
.c_str()), GetSize(b
.wire
));
71 w
->port_output
= true;
74 log_assert(w
->port_input
);
75 log_assert(b
.offset
< GetSize(w
));
77 w
->set_bool_attribute(ID(abc9_scc_break
));
78 c
.second
= RTLIL::SigBit(w
, b
.offset
);
84 module
->fixup_ports();
87 void unbreak_scc(RTLIL::Module
*module
)
89 // Now 'unexpose' those wires by undoing
90 // the expose operation -- remove them from PO/PI
91 // and re-connecting them back together
92 for (auto wire
: module
->wires()) {
93 auto it
= wire
->attributes
.find(ID(abc9_scc_break
));
94 if (it
!= wire
->attributes
.end()) {
95 wire
->attributes
.erase(it
);
96 log_assert(wire
->port_output
);
97 wire
->port_output
= false;
98 std::string name
= wire
->name
.str();
99 RTLIL::Wire
*i_wire
= module
->wire(name
.substr(0, GetSize(name
) - 5));
101 log_assert(i_wire
->port_input
);
102 i_wire
->port_input
= false;
103 module
->connect(i_wire
, wire
);
106 module
->fixup_ports();
109 void prep_dff(RTLIL::Module
*module
)
111 auto design
= module
->design
;
114 SigMap
assign_map(module
);
116 typedef SigSpec clkdomain_t
;
117 dict
<clkdomain_t
, int> clk_to_mergeability
;
119 for (auto cell
: module
->cells()) {
120 if (cell
->type
!= "$__ABC9_FF_")
123 Wire
*abc9_clock_wire
= module
->wire(stringf("%s.clock", cell
->name
.c_str()));
124 if (abc9_clock_wire
== NULL
)
125 log_error("'%s.clock' is not a wire present in module '%s'.\n", cell
->name
.c_str(), log_id(module
));
126 SigSpec abc9_clock
= assign_map(abc9_clock_wire
);
128 clkdomain_t
key(abc9_clock
);
130 auto r
= clk_to_mergeability
.insert(std::make_pair(abc9_clock
, clk_to_mergeability
.size() + 1));
131 auto r2
YS_ATTRIBUTE(unused
) = cell
->attributes
.insert(std::make_pair(ID(abc9_mergeability
), r
.first
->second
));
132 log_assert(r2
.second
);
134 Wire
*abc9_init_wire
= module
->wire(stringf("%s.init", cell
->name
.c_str()));
135 if (abc9_init_wire
== NULL
)
136 log_error("'%s.init' is not a wire present in module '%s'.\n", cell
->name
.c_str(), log_id(module
));
137 log_assert(GetSize(abc9_init_wire
) == 1);
138 SigSpec abc9_init
= assign_map(abc9_init_wire
);
139 if (!abc9_init
.is_fully_const())
140 log_error("'%s.init' is not a constant wire present in module '%s'.\n", cell
->name
.c_str(), log_id(module
));
141 r2
= cell
->attributes
.insert(std::make_pair(ID(abc9_init
), abc9_init
.as_const()));
142 log_assert(r2
.second
);
145 RTLIL::Module
*holes_module
= design
->module(stringf("%s$holes", module
->name
.c_str()));
147 dict
<SigSig
, SigSig
> replace
;
148 for (auto it
= holes_module
->cells_
.begin(); it
!= holes_module
->cells_
.end(); ) {
149 auto cell
= it
->second
;
150 if (cell
->type
.in("$_DFF_N_", "$_DFF_NN0_", "$_DFF_NN1_", "$_DFF_NP0_", "$_DFF_NP1_",
151 "$_DFF_P_", "$_DFF_PN0_", "$_DFF_PN1", "$_DFF_PP0_", "$_DFF_PP1_")) {
152 SigBit D
= cell
->getPort("\\D");
153 SigBit Q
= cell
->getPort("\\Q");
154 // Remove the DFF cell from what needs to be a combinatorial box
155 it
= holes_module
->cells_
.erase(it
);
157 if (GetSize(Q
.wire
) == 1)
158 port
= holes_module
->wire(stringf("$abc%s", Q
.wire
->name
.c_str()));
160 port
= holes_module
->wire(stringf("$abc%s[%d]", Q
.wire
->name
.c_str(), Q
.offset
));
162 // Prepare to replace "assign <port> = DFF.Q;" with "assign <port> = DFF.D;"
163 // in order to extract the combinatorial control logic that feeds the box
164 // (i.e. clock enable, synchronous reset, etc.)
165 replace
.insert(std::make_pair(SigSig(port
,Q
), SigSig(port
,D
)));
166 // Since `flatten` above would have created wires named "<cell>.Q",
167 // extract the pre-techmap cell name
168 auto pos
= Q
.wire
->name
.str().rfind(".");
169 log_assert(pos
!= std::string::npos
);
170 IdString driver
= Q
.wire
->name
.substr(0, pos
);
171 // And drive the signal that was previously driven by "DFF.Q" (typically
172 // used to implement clock-enable functionality) with the "<cell>.$abc9_currQ"
173 // wire (which itself is driven an input port) we inserted above
174 Wire
*currQ
= holes_module
->wire(stringf("%s.abc9_ff.Q", driver
.c_str()));
176 holes_module
->connect(Q
, currQ
);
182 for (auto &conn
: holes_module
->connections_
)
183 conn
= replace
.at(conn
, conn
);
187 void prep_holes(RTLIL::Module
*module
, bool dff
)
189 auto design
= module
->design
;
192 SigMap
sigmap(module
);
194 dict
<SigBit
, pool
<IdString
>> bit_drivers
, bit_users
;
195 TopoSort
<IdString
, RTLIL::sort_by_id_str
> toposort
;
196 bool abc9_box_seen
= false;
198 for (auto cell
: module
->cells()) {
199 if (cell
->type
== "$__ABC9_FF_")
202 auto inst_module
= module
->design
->module(cell
->type
);
203 bool abc9_box
= inst_module
&& inst_module
->attributes
.count("\\abc9_box_id");
204 bool abc9_flop
= false;
206 abc9_flop
= inst_module
->get_bool_attribute("\\abc9_flop");
207 if (abc9_flop
&& !dff
)
209 abc9_box_seen
= abc9_box
;
211 else if (!yosys_celltypes
.cell_known(cell
->type
))
214 for (auto conn
: cell
->connections()) {
215 if (cell
->input(conn
.first
))
216 for (auto bit
: sigmap(conn
.second
))
217 bit_users
[bit
].insert(cell
->name
);
219 if (cell
->output(conn
.first
) && !abc9_flop
)
220 for (auto bit
: sigmap(conn
.second
))
221 bit_drivers
[bit
].insert(cell
->name
);
224 toposort
.node(cell
->name
);
230 for (auto &it
: bit_users
)
231 if (bit_drivers
.count(it
.first
))
232 for (auto driver_cell
: bit_drivers
.at(it
.first
))
233 for (auto user_cell
: it
.second
)
234 toposort
.edge(driver_cell
, user_cell
);
237 toposort
.analyze_loops
= true;
239 bool no_loops
YS_ATTRIBUTE(unused
) = toposort
.sort();
243 for (auto &it
: toposort
.loops
) {
244 log(" loop %d\n", i
++);
245 for (auto cell_name
: it
) {
246 auto cell
= module
->cell(cell_name
);
248 log("\t%s (%s @ %s)\n", log_id(cell
), log_id(cell
->type
), cell
->get_src_attribute().c_str());
253 log_assert(no_loops
);
255 vector
<Cell
*> box_list
;
256 for (auto cell_name
: toposort
.sorted
) {
257 RTLIL::Cell
*cell
= module
->cell(cell_name
);
260 RTLIL::Module
* box_module
= design
->module(cell
->type
);
261 if (!box_module
|| !box_module
->attributes
.count("\\abc9_box_id"))
264 bool blackbox
= box_module
->get_blackbox_attribute(true /* ignore_wb */);
266 // Fully pad all unused input connections of this box cell with S0
267 // Fully pad all undriven output connections of this box cell with anonymous wires
268 for (const auto &port_name
: box_module
->ports
) {
269 RTLIL::Wire
* w
= box_module
->wire(port_name
);
271 auto it
= cell
->connections_
.find(port_name
);
274 if (it
!= cell
->connections_
.end()) {
275 if (GetSize(it
->second
) < GetSize(w
))
276 it
->second
.append(RTLIL::SigSpec(State::S0
, GetSize(w
)-GetSize(it
->second
)));
280 rhs
= RTLIL::SigSpec(State::S0
, GetSize(w
));
281 cell
->setPort(port_name
, rhs
);
284 if (w
->port_output
) {
286 auto it
= cell
->connections_
.find(w
->name
);
287 if (it
!= cell
->connections_
.end()) {
288 if (GetSize(it
->second
) < GetSize(w
))
289 it
->second
.append(module
->addWire(NEW_ID
, GetSize(w
)-GetSize(it
->second
)));
293 Wire
*wire
= module
->addWire(NEW_ID
, GetSize(w
));
295 wire
->set_bool_attribute(ID(abc9_padding
));
297 cell
->setPort(port_name
, rhs
);
302 cell
->attributes
["\\abc9_box_seq"] = box_list
.size();
303 box_list
.emplace_back(cell
);
305 log_assert(!box_list
.empty());
307 RTLIL::Module
*holes_module
= design
->addModule(stringf("%s$holes", module
->name
.c_str()));
308 log_assert(holes_module
);
309 holes_module
->set_bool_attribute("\\abc9_holes");
311 dict
<IdString
, Cell
*> cell_cache
;
312 dict
<IdString
, std::vector
<IdString
>> box_ports
;
315 for (auto cell
: box_list
) {
316 RTLIL::Module
* orig_box_module
= design
->module(cell
->type
);
317 log_assert(orig_box_module
);
318 IdString derived_name
= orig_box_module
->derive(design
, cell
->parameters
);
319 RTLIL::Module
* box_module
= design
->module(derived_name
);
320 if (box_module
->has_processes())
321 Pass::call_on_module(design
, box_module
, "proc");
324 auto r
= cell_cache
.insert(std::make_pair(derived_name
, nullptr));
325 Cell
*holes_cell
= r
.first
->second
;
326 if (r
.second
&& box_module
->get_bool_attribute("\\whitebox")) {
327 holes_cell
= holes_module
->addCell(cell
->name
, cell
->type
);
328 holes_cell
->parameters
= cell
->parameters
;
329 r
.first
->second
= holes_cell
;
332 auto r2
= box_ports
.insert(cell
->type
);
334 // Make carry in the last PI, and carry out the last PO
335 // since ABC requires it this way
336 IdString carry_in
, carry_out
;
337 for (const auto &port_name
: box_module
->ports
) {
338 auto w
= box_module
->wire(port_name
);
340 if (w
->get_bool_attribute("\\abc9_carry")) {
342 if (carry_in
!= IdString())
343 log_error("Module '%s' contains more than one 'abc9_carry' input port.\n", log_id(box_module
));
344 carry_in
= port_name
;
346 if (w
->port_output
) {
347 if (carry_out
!= IdString())
348 log_error("Module '%s' contains more than one 'abc9_carry' output port.\n", log_id(box_module
));
349 carry_out
= port_name
;
353 r2
.first
->second
.push_back(port_name
);
356 if (carry_in
!= IdString() && carry_out
== IdString())
357 log_error("Module '%s' contains an 'abc9_carry' input port but no output port.\n", log_id(box_module
));
358 if (carry_in
== IdString() && carry_out
!= IdString())
359 log_error("Module '%s' contains an 'abc9_carry' output port but no input port.\n", log_id(box_module
));
360 if (carry_in
!= IdString()) {
361 r2
.first
->second
.push_back(carry_in
);
362 r2
.first
->second
.push_back(carry_out
);
366 for (const auto &port_name
: box_ports
.at(cell
->type
)) {
367 RTLIL::Wire
*w
= box_module
->wire(port_name
);
369 RTLIL::Wire
*holes_wire
;
370 RTLIL::SigSpec port_sig
;
372 for (int i
= 0; i
< GetSize(w
); i
++) {
374 holes_wire
= holes_module
->wire(stringf("\\i%d", box_inputs
));
376 holes_wire
= holes_module
->addWire(stringf("\\i%d", box_inputs
));
377 holes_wire
->port_input
= true;
378 holes_wire
->port_id
= port_id
++;
379 holes_module
->ports
.push_back(holes_wire
->name
);
382 port_sig
.append(holes_wire
);
385 for (int i
= 0; i
< GetSize(w
); i
++) {
387 holes_wire
= holes_module
->addWire(stringf("$abc%s.%s", cell
->name
.c_str(), log_id(w
->name
)));
389 holes_wire
= holes_module
->addWire(stringf("$abc%s.%s[%d]", cell
->name
.c_str(), log_id(w
->name
), i
));
390 holes_wire
->port_output
= true;
391 holes_wire
->port_id
= port_id
++;
392 holes_module
->ports
.push_back(holes_wire
->name
);
394 port_sig
.append(holes_wire
);
396 holes_module
->connect(holes_wire
, State::S0
);
398 if (!port_sig
.empty()) {
400 holes_cell
->setPort(w
->name
, port_sig
);
402 holes_module
->connect(holes_cell
->getPort(w
->name
), port_sig
);
406 // For flops only, create an extra 1-bit input that drives a new wire
407 // called "<cell>.$abc9_currQ" that is used below
408 if (box_module
->get_bool_attribute("\\abc9_flop")) {
409 log_assert(holes_cell
);
412 Wire
*holes_wire
= holes_module
->wire(stringf("\\i%d", box_inputs
));
414 holes_wire
= holes_module
->addWire(stringf("\\i%d", box_inputs
));
415 holes_wire
->port_input
= true;
416 holes_wire
->port_id
= port_id
++;
417 holes_module
->ports
.push_back(holes_wire
->name
);
419 Wire
*w
= holes_module
->addWire(stringf("%s.abc9_ff.Q", cell
->name
.c_str()));
420 holes_module
->connect(w
, holes_wire
);
425 void reintegrate(RTLIL::Module
*module
)
427 auto design
= module
->design
;
430 map_autoidx
= autoidx
++;
432 RTLIL::Module
*mapped_mod
= design
->module(stringf("%s$abc9", module
->name
.c_str()));
433 if (mapped_mod
== NULL
)
434 log_error("ABC output file does not contain a module `%s$abc'.\n", log_id(module
));
436 for (auto w
: mapped_mod
->wires())
437 module
->addWire(remap_name(w
->name
), GetSize(w
));
439 dict
<IdString
,IdString
> box_lookup
;
440 dict
<IdString
,std::vector
<IdString
>> box_ports
;
442 for (auto m
: design
->modules()) {
443 auto it
= m
->attributes
.find(ID(abc9_box_id
));
444 if (it
== m
->attributes
.end())
446 if (m
->name
.begins_with("$paramod"))
448 auto id
= it
->second
.as_int();
449 auto r
= box_lookup
.insert(std::make_pair(stringf("$__boxid%d", id
), m
->name
));
451 log_error("Module '%s' has the same abc9_box_id = %d value as '%s'.\n",
452 log_id(m
), id
, log_id(r
.first
->second
));
453 log_assert(r
.second
);
455 auto r2
= box_ports
.insert(m
->name
);
457 // Make carry in the last PI, and carry out the last PO
458 // since ABC requires it this way
459 IdString carry_in
, carry_out
;
460 for (const auto &port_name
: m
->ports
) {
461 auto w
= m
->wire(port_name
);
463 if (w
->get_bool_attribute("\\abc9_carry")) {
465 if (carry_in
!= IdString())
466 log_error("Module '%s' contains more than one 'abc9_carry' input port.\n", log_id(m
));
467 carry_in
= port_name
;
469 if (w
->port_output
) {
470 if (carry_out
!= IdString())
471 log_error("Module '%s' contains more than one 'abc9_carry' output port.\n", log_id(m
));
472 carry_out
= port_name
;
476 r2
.first
->second
.push_back(port_name
);
479 if (carry_in
!= IdString() && carry_out
== IdString())
480 log_error("Module '%s' contains an 'abc9_carry' input port but no output port.\n", log_id(m
));
481 if (carry_in
== IdString() && carry_out
!= IdString())
482 log_error("Module '%s' contains an 'abc9_carry' output port but no input port.\n", log_id(m
));
483 if (carry_in
!= IdString()) {
484 r2
.first
->second
.push_back(carry_in
);
485 r2
.first
->second
.push_back(carry_out
);
490 std::vector
<Cell
*> boxes
;
491 for (auto cell
: module
->cells().to_vector()) {
492 if (cell
->type
.in(ID($_AND_
), ID($_NOT_
), ID($__ABC9_FF_
)))
493 module
->remove(cell
);
494 else if (cell
->attributes
.erase("\\abc9_box_seq"))
495 boxes
.emplace_back(cell
);
498 dict
<SigBit
, pool
<IdString
>> bit_drivers
, bit_users
;
499 TopoSort
<IdString
, RTLIL::sort_by_id_str
> toposort
;
500 dict
<RTLIL::Cell
*,RTLIL::Cell
*> not2drivers
;
501 dict
<SigBit
, std::vector
<RTLIL::Cell
*>> bit2sinks
;
503 std::map
<IdString
, int> cell_stats
;
504 for (auto mapped_cell
: mapped_mod
->cells())
506 toposort
.node(mapped_cell
->name
);
508 if (mapped_cell
->type
== ID($_NOT_
)) {
509 RTLIL::SigBit a_bit
= mapped_cell
->getPort(ID::A
);
510 RTLIL::SigBit y_bit
= mapped_cell
->getPort(ID::Y
);
511 bit_users
[a_bit
].insert(mapped_cell
->name
);
512 bit_drivers
[y_bit
].insert(mapped_cell
->name
);
515 mapped_cell
->setPort(ID::Y
, module
->addWire(NEW_ID
));
516 RTLIL::Wire
*wire
= module
->wire(remap_name(y_bit
.wire
->name
));
518 module
->connect(RTLIL::SigBit(wire
, y_bit
.offset
), State::S1
);
521 RTLIL::Cell
* driver_lut
= nullptr;
522 // ABC can return NOT gates that drive POs
523 if (!a_bit
.wire
->port_input
) {
524 // If it's not a NOT gate that that comes from a PI directly,
525 // find the driver LUT and clone that to guarantee that we won't
526 // increase the max logic depth
527 // (TODO: Optimise by not cloning unless will increase depth)
528 RTLIL::IdString driver_name
;
529 if (GetSize(a_bit
.wire
) == 1)
530 driver_name
= stringf("%s$lut", a_bit
.wire
->name
.c_str());
532 driver_name
= stringf("%s[%d]$lut", a_bit
.wire
->name
.c_str(), a_bit
.offset
);
533 driver_lut
= mapped_mod
->cell(driver_name
);
537 // If a driver couldn't be found (could be from PI or box CI)
538 // then implement using a LUT
539 RTLIL::Cell
*cell
= module
->addLut(remap_name(stringf("%s$lut", mapped_cell
->name
.c_str())),
540 RTLIL::SigBit(module
->wires_
.at(remap_name(a_bit
.wire
->name
)), a_bit
.offset
),
541 RTLIL::SigBit(module
->wires_
.at(remap_name(y_bit
.wire
->name
)), y_bit
.offset
),
542 RTLIL::Const::from_string("01"));
543 bit2sinks
[cell
->getPort(ID::A
)].push_back(cell
);
544 cell_stats
[ID($lut
)]++;
547 not2drivers
[mapped_cell
] = driver_lut
;
552 if (mapped_cell
->type
.in(ID($lut
), ID($__ABC9_FF_
))) {
553 // Convert buffer into direct connection
554 if (mapped_cell
->type
== ID($lut
) &&
555 GetSize(mapped_cell
->getPort(ID::A
)) == 1 &&
556 mapped_cell
->getParam(ID(LUT
)) == RTLIL::Const::from_string("01")) {
557 SigSpec my_a
= module
->wires_
.at(remap_name(mapped_cell
->getPort(ID::A
).as_wire()->name
));
558 SigSpec my_y
= module
->wires_
.at(remap_name(mapped_cell
->getPort(ID::Y
).as_wire()->name
));
559 module
->connect(my_y
, my_a
);
563 RTLIL::Cell
*cell
= module
->addCell(remap_name(mapped_cell
->name
), mapped_cell
->type
);
564 cell
->parameters
= mapped_cell
->parameters
;
565 cell
->attributes
= mapped_cell
->attributes
;
567 for (auto &mapped_conn
: mapped_cell
->connections()) {
568 RTLIL::SigSpec newsig
;
569 for (auto c
: mapped_conn
.second
.chunks()) {
572 //log_assert(c.width == 1);
574 c
.wire
= module
->wires_
.at(remap_name(c
.wire
->name
));
577 cell
->setPort(mapped_conn
.first
, newsig
);
579 if (cell
->input(mapped_conn
.first
)) {
580 for (auto i
: newsig
)
581 bit2sinks
[i
].push_back(cell
);
582 for (auto i
: mapped_conn
.second
)
583 bit_users
[i
].insert(mapped_cell
->name
);
585 if (cell
->output(mapped_conn
.first
))
586 for (auto i
: mapped_conn
.second
)
587 bit_drivers
[i
].insert(mapped_cell
->name
);
591 RTLIL::Cell
*existing_cell
= module
->cell(mapped_cell
->name
);
592 log_assert(existing_cell
);
593 log_assert(mapped_cell
->type
.begins_with("$__boxid"));
595 auto type
= box_lookup
.at(mapped_cell
->type
, IdString());
596 if (type
== IdString())
597 log_error("No module with abc9_box_id = %s found.\n", mapped_cell
->type
.c_str() + strlen("$__boxid"));
598 mapped_cell
->type
= type
;
600 RTLIL::Cell
*cell
= module
->addCell(remap_name(mapped_cell
->name
), mapped_cell
->type
);
601 cell
->parameters
= existing_cell
->parameters
;
602 cell
->attributes
= existing_cell
->attributes
;
603 module
->swap_names(cell
, existing_cell
);
605 auto it
= mapped_cell
->connections_
.find("\\i");
606 log_assert(it
!= mapped_cell
->connections_
.end());
607 SigSpec inputs
= std::move(it
->second
);
608 mapped_cell
->connections_
.erase(it
);
609 it
= mapped_cell
->connections_
.find("\\o");
610 log_assert(it
!= mapped_cell
->connections_
.end());
611 SigSpec outputs
= std::move(it
->second
);
612 mapped_cell
->connections_
.erase(it
);
614 RTLIL::Module
* box_module
= design
->module(mapped_cell
->type
);
615 auto abc9_flop
= box_module
->attributes
.count("\\abc9_flop");
617 for (const auto &i
: inputs
)
618 bit_users
[i
].insert(mapped_cell
->name
);
619 for (const auto &i
: outputs
)
620 bit_drivers
[i
].insert(mapped_cell
->name
);
623 int input_count
= 0, output_count
= 0;
624 for (const auto &port_name
: box_ports
.at(cell
->type
)) {
625 RTLIL::Wire
*w
= box_module
->wire(port_name
);
630 sig
= inputs
.extract(input_count
, GetSize(w
));
631 input_count
+= GetSize(w
);
633 if (w
->port_output
) {
634 sig
= outputs
.extract(output_count
, GetSize(w
));
635 output_count
+= GetSize(w
);
639 for (auto c
: sig
.chunks()) {
642 //log_assert(c.width == 1);
644 c
.wire
= module
->wires_
.at(remap_name(c
.wire
->name
));
648 auto it
= existing_cell
->connections_
.find(port_name
);
649 if (it
== existing_cell
->connections_
.end())
651 if (GetSize(newsig
) > GetSize(it
->second
))
652 newsig
= newsig
.extract(0, GetSize(it
->second
));
654 log_assert(GetSize(newsig
) == GetSize(it
->second
));
656 cell
->setPort(port_name
, newsig
);
658 if (w
->port_input
&& !abc9_flop
)
659 for (const auto &i
: newsig
)
660 bit2sinks
[i
].push_back(cell
);
664 cell_stats
[mapped_cell
->type
]++;
667 for (auto cell
: boxes
)
668 module
->remove(cell
);
670 // Copy connections (and rename) from mapped_mod to module
671 for (auto conn
: mapped_mod
->connections()) {
672 if (!conn
.first
.is_fully_const()) {
673 auto chunks
= conn
.first
.chunks();
674 for (auto &c
: chunks
)
675 c
.wire
= module
->wires_
.at(remap_name(c
.wire
->name
));
676 conn
.first
= std::move(chunks
);
678 if (!conn
.second
.is_fully_const()) {
679 auto chunks
= conn
.second
.chunks();
680 for (auto &c
: chunks
)
682 c
.wire
= module
->wires_
.at(remap_name(c
.wire
->name
));
683 conn
.second
= std::move(chunks
);
685 module
->connect(conn
);
688 for (auto &it
: cell_stats
)
689 log("ABC RESULTS: %15s cells: %8d\n", it
.first
.c_str(), it
.second
);
690 int in_wires
= 0, out_wires
= 0;
692 // Stitch in mapped_mod's inputs/outputs into module
693 for (auto port
: mapped_mod
->ports
) {
694 RTLIL::Wire
*w
= mapped_mod
->wire(port
);
695 RTLIL::Wire
*wire
= module
->wire(port
);
697 RTLIL::Wire
*remap_wire
= module
->wire(remap_name(port
));
698 RTLIL::SigSpec
signal(wire
, 0, GetSize(remap_wire
));
699 log_assert(GetSize(signal
) >= GetSize(remap_wire
));
702 if (w
->port_output
) {
704 conn
.second
= remap_wire
;
706 module
->connect(conn
);
708 else if (w
->port_input
) {
709 conn
.first
= remap_wire
;
710 conn
.second
= signal
;
712 module
->connect(conn
);
716 for (auto &it
: bit_users
)
717 if (bit_drivers
.count(it
.first
))
718 for (auto driver_cell
: bit_drivers
.at(it
.first
))
719 for (auto user_cell
: it
.second
)
720 toposort
.edge(driver_cell
, user_cell
);
721 bool no_loops
YS_ATTRIBUTE(unused
) = toposort
.sort();
722 log_assert(no_loops
);
724 for (auto ii
= toposort
.sorted
.rbegin(); ii
!= toposort
.sorted
.rend(); ii
++) {
725 RTLIL::Cell
*not_cell
= mapped_mod
->cell(*ii
);
726 log_assert(not_cell
);
727 if (not_cell
->type
!= ID($_NOT_
))
729 auto it
= not2drivers
.find(not_cell
);
730 if (it
== not2drivers
.end())
732 RTLIL::Cell
*driver_lut
= it
->second
;
733 RTLIL::SigBit a_bit
= not_cell
->getPort(ID::A
);
734 RTLIL::SigBit y_bit
= not_cell
->getPort(ID::Y
);
735 RTLIL::Const driver_mask
;
737 a_bit
.wire
= module
->wires_
.at(remap_name(a_bit
.wire
->name
));
738 y_bit
.wire
= module
->wires_
.at(remap_name(y_bit
.wire
->name
));
740 auto jt
= bit2sinks
.find(a_bit
);
741 if (jt
== bit2sinks
.end())
744 for (auto sink_cell
: jt
->second
)
745 if (sink_cell
->type
!= ID($lut
))
748 // Push downstream LUTs past inverter
749 for (auto sink_cell
: jt
->second
) {
750 SigSpec A
= sink_cell
->getPort(ID::A
);
751 RTLIL::Const mask
= sink_cell
->getParam(ID(LUT
));
753 for (; index
< GetSize(A
); index
++)
754 if (A
[index
] == a_bit
)
756 log_assert(index
< GetSize(A
));
758 while (i
< GetSize(mask
)) {
759 for (int j
= 0; j
< (1 << index
); j
++)
760 std::swap(mask
[i
+j
], mask
[i
+j
+(1 << index
)]);
764 sink_cell
->setPort(ID::A
, A
);
765 sink_cell
->setParam(ID(LUT
), mask
);
768 // Since we have rewritten all sinks (which we know
769 // to be only LUTs) to be after the inverter, we can
770 // go ahead and clone the LUT with the expectation
771 // that the original driving LUT will become dangling
772 // and get cleaned away
774 driver_mask
= driver_lut
->getParam(ID(LUT
));
775 for (auto &b
: driver_mask
.bits
) {
776 if (b
== RTLIL::State::S0
) b
= RTLIL::State::S1
;
777 else if (b
== RTLIL::State::S1
) b
= RTLIL::State::S0
;
779 auto cell
= module
->addLut(NEW_ID
,
780 driver_lut
->getPort(ID::A
),
783 for (auto &bit
: cell
->connections_
.at(ID::A
)) {
784 bit
.wire
= module
->wires_
.at(remap_name(bit
.wire
->name
));
785 bit2sinks
[bit
].push_back(cell
);
789 //log("ABC RESULTS: internal signals: %8d\n", int(signal_list.size()) - in_wires - out_wires);
790 log("ABC RESULTS: input signals: %8d\n", in_wires
);
791 log("ABC RESULTS: output signals: %8d\n", out_wires
);
793 design
->remove(mapped_mod
);
796 struct Abc9OpsPass
: public Pass
{
797 Abc9OpsPass() : Pass("abc9_ops", "helper functions for ABC9") { }
798 void help() YS_OVERRIDE
800 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
802 log(" abc9_ops [options] [selection]\n");
805 void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) YS_OVERRIDE
807 log_header(design
, "Executing ABC9_OPS pass (helper functions for ABC9).\n");
809 bool break_scc_mode
= false;
810 bool unbreak_scc_mode
= false;
811 bool prep_dff_mode
= false;
812 bool prep_holes_mode
= false;
813 bool reintegrate_mode
= false;
814 bool dff_mode
= false;
817 for (argidx
= 1; argidx
< args
.size(); argidx
++) {
818 std::string arg
= args
[argidx
];
819 if (arg
== "-break_scc") {
820 break_scc_mode
= true;
823 if (arg
== "-unbreak_scc") {
824 unbreak_scc_mode
= true;
827 if (arg
== "-prep_dff") {
828 prep_dff_mode
= true;
831 if (arg
== "-prep_holes") {
832 prep_holes_mode
= true;
835 if (arg
== "-reintegrate") {
836 reintegrate_mode
= true;
845 extra_args(args
, argidx
, design
);
847 if (!(break_scc_mode
|| unbreak_scc_mode
|| prep_dff_mode
|| reintegrate_mode
))
848 log_cmd_error("At least one of -{,un}break_scc, -prep_{dff,holes}, -reintegrate must be specified.\n");
850 if (dff_mode
&& !prep_holes_mode
)
851 log_cmd_error("'-dff' option is only relevant for -prep_holes.\n");
853 for (auto mod
: design
->selected_modules()) {
854 if (mod
->get_bool_attribute("\\abc9_holes"))
857 if (mod
->processes
.size() > 0) {
858 log("Skipping module %s as it contains processes.\n", log_id(mod
));
862 if (!design
->selected_whole_module(mod
))
863 log_error("Can't handle partially selected module %s!\n", log_id(mod
));
867 if (unbreak_scc_mode
)
872 prep_holes(mod
, dff_mode
);
873 if (reintegrate_mode
)
879 PRIVATE_NAMESPACE_END