4da10d94b612c379d04eaaf295c815c6ca870955
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();
56 w
->set_bool_attribute(ID::keep
);
61 module
->fixup_ports();
64 void unbreak_scc(RTLIL::Module
*module
)
66 // Now 'unexpose' those wires by undoing
67 // the expose operation -- remove them from PO/PI
68 // and re-connecting them back together
69 for (auto wire
: module
->wires()) {
70 auto it
= wire
->attributes
.find(ID(abc9_scc_break
));
71 if (it
!= wire
->attributes
.end()) {
72 wire
->attributes
.erase(it
);
73 log_assert(wire
->port_output
);
74 wire
->port_output
= false;
75 std::string name
= wire
->name
.str();
76 RTLIL::Wire
*i_wire
= module
->wire(name
.substr(0, GetSize(name
) - 5));
78 log_assert(i_wire
->port_input
);
79 i_wire
->port_input
= false;
80 module
->connect(i_wire
, wire
);
83 module
->fixup_ports();
86 void prep_dff(RTLIL::Module
*module
)
88 auto design
= module
->design
;
91 SigMap
assign_map(module
);
93 typedef SigSpec clkdomain_t
;
94 dict
<clkdomain_t
, int> clk_to_mergeability
;
96 for (auto cell
: module
->cells()) {
97 if (cell
->type
!= "$__ABC9_FF_")
100 Wire
*abc9_clock_wire
= module
->wire(stringf("%s.clock", cell
->name
.c_str()));
101 if (abc9_clock_wire
== NULL
)
102 log_error("'%s.clock' is not a wire present in module '%s'.\n", cell
->name
.c_str(), log_id(module
));
103 SigSpec abc9_clock
= assign_map(abc9_clock_wire
);
105 clkdomain_t
key(abc9_clock
);
107 auto r
= clk_to_mergeability
.insert(std::make_pair(abc9_clock
, clk_to_mergeability
.size() + 1));
108 auto r2
YS_ATTRIBUTE(unused
) = cell
->attributes
.insert(std::make_pair(ID(abc9_mergeability
), r
.first
->second
));
109 log_assert(r2
.second
);
111 Wire
*abc9_init_wire
= module
->wire(stringf("%s.init", cell
->name
.c_str()));
112 if (abc9_init_wire
== NULL
)
113 log_error("'%s.init' is not a wire present in module '%s'.\n", cell
->name
.c_str(), log_id(module
));
114 log_assert(GetSize(abc9_init_wire
) == 1);
115 SigSpec abc9_init
= assign_map(abc9_init_wire
);
116 if (!abc9_init
.is_fully_const())
117 log_error("'%s.init' is not a constant wire present in module '%s'.\n", cell
->name
.c_str(), log_id(module
));
118 r2
= cell
->attributes
.insert(std::make_pair(ID(abc9_init
), abc9_init
.as_const()));
119 log_assert(r2
.second
);
122 RTLIL::Module
*holes_module
= design
->module(stringf("%s$holes", module
->name
.c_str()));
124 SigMap
sigmap(holes_module
);
126 dict
<SigSpec
, SigSpec
> replace
;
127 for (auto it
= holes_module
->cells_
.begin(); it
!= holes_module
->cells_
.end(); ) {
128 auto cell
= it
->second
;
129 if (cell
->type
.in("$_DFF_N_", "$_DFF_NN0_", "$_DFF_NN1_", "$_DFF_NP0_", "$_DFF_NP1_",
130 "$_DFF_P_", "$_DFF_PN0_", "$_DFF_PN1", "$_DFF_PP0_", "$_DFF_PP1_")) {
131 SigBit D
= cell
->getPort("\\D");
132 SigBit Q
= cell
->getPort("\\Q");
133 // Remove the $_DFF_* cell from what needs to be a combinatorial box
134 it
= holes_module
->cells_
.erase(it
);
136 if (GetSize(Q
.wire
) == 1)
137 port
= holes_module
->wire(stringf("$abc%s", Q
.wire
->name
.c_str()));
139 port
= holes_module
->wire(stringf("$abc%s[%d]", Q
.wire
->name
.c_str(), Q
.offset
));
141 // Prepare to replace "assign <port> = $_DFF_*.Q;" with "assign <port> = $_DFF_*.D;"
142 // in order to extract just the combinatorial control logic that feeds the box
143 // (i.e. clock enable, synchronous reset, etc.)
144 replace
.insert(std::make_pair(Q
,D
));
145 // Since `flatten` above would have created wires named "<cell>.Q",
146 // extract the pre-techmap cell name
147 auto pos
= Q
.wire
->name
.str().rfind(".");
148 log_assert(pos
!= std::string::npos
);
149 IdString driver
= Q
.wire
->name
.substr(0, pos
);
150 // And drive the signal that was previously driven by "DFF.Q" (typically
151 // used to implement clock-enable functionality) with the "<cell>.$abc9_currQ"
152 // wire (which itself is driven an by input port) we inserted above
153 Wire
*currQ
= holes_module
->wire(stringf("%s.abc9_ff.Q", driver
.c_str()));
155 holes_module
->connect(Q
, currQ
);
161 for (auto &conn
: holes_module
->connections_
)
162 conn
.second
= replace
.at(sigmap(conn
.second
), conn
.second
);
166 void prep_holes(RTLIL::Module
*module
, bool dff
)
168 auto design
= module
->design
;
171 SigMap
sigmap(module
);
173 dict
<SigBit
, pool
<IdString
>> bit_drivers
, bit_users
;
174 TopoSort
<IdString
, RTLIL::sort_by_id_str
> toposort
;
175 bool abc9_box_seen
= false;
177 for (auto cell
: module
->cells()) {
178 if (cell
->type
== "$__ABC9_FF_")
181 auto inst_module
= module
->design
->module(cell
->type
);
182 bool abc9_box
= inst_module
&& inst_module
->attributes
.count("\\abc9_box_id");
183 bool abc9_flop
= false;
185 abc9_flop
= inst_module
->get_bool_attribute("\\abc9_flop");
186 if (abc9_flop
&& !dff
)
188 abc9_box_seen
= abc9_box
;
190 else if (!yosys_celltypes
.cell_known(cell
->type
))
193 for (auto conn
: cell
->connections()) {
194 if (cell
->input(conn
.first
))
195 for (auto bit
: sigmap(conn
.second
))
196 bit_users
[bit
].insert(cell
->name
);
198 if (cell
->output(conn
.first
) && !abc9_flop
)
199 for (auto bit
: sigmap(conn
.second
))
200 bit_drivers
[bit
].insert(cell
->name
);
203 toposort
.node(cell
->name
);
209 for (auto &it
: bit_users
)
210 if (bit_drivers
.count(it
.first
))
211 for (auto driver_cell
: bit_drivers
.at(it
.first
))
212 for (auto user_cell
: it
.second
)
213 toposort
.edge(driver_cell
, user_cell
);
216 toposort
.analyze_loops
= true;
218 bool no_loops
YS_ATTRIBUTE(unused
) = toposort
.sort();
222 for (auto &it
: toposort
.loops
) {
223 log(" loop %d\n", i
++);
224 for (auto cell_name
: it
) {
225 auto cell
= module
->cell(cell_name
);
227 log("\t%s (%s @ %s)\n", log_id(cell
), log_id(cell
->type
), cell
->get_src_attribute().c_str());
232 log_assert(no_loops
);
234 vector
<Cell
*> box_list
;
235 for (auto cell_name
: toposort
.sorted
) {
236 RTLIL::Cell
*cell
= module
->cell(cell_name
);
239 RTLIL::Module
* box_module
= design
->module(cell
->type
);
240 if (!box_module
|| !box_module
->attributes
.count("\\abc9_box_id"))
243 bool blackbox
= box_module
->get_blackbox_attribute(true /* ignore_wb */);
245 // Fully pad all unused input connections of this box cell with S0
246 // Fully pad all undriven output connections of this box cell with anonymous wires
247 for (const auto &port_name
: box_module
->ports
) {
248 RTLIL::Wire
* w
= box_module
->wire(port_name
);
250 auto it
= cell
->connections_
.find(port_name
);
253 if (it
!= cell
->connections_
.end()) {
254 if (GetSize(it
->second
) < GetSize(w
))
255 it
->second
.append(RTLIL::SigSpec(State::S0
, GetSize(w
)-GetSize(it
->second
)));
259 rhs
= RTLIL::SigSpec(State::S0
, GetSize(w
));
260 cell
->setPort(port_name
, rhs
);
263 if (w
->port_output
) {
265 auto it
= cell
->connections_
.find(w
->name
);
266 if (it
!= cell
->connections_
.end()) {
267 if (GetSize(it
->second
) < GetSize(w
))
268 it
->second
.append(module
->addWire(NEW_ID
, GetSize(w
)-GetSize(it
->second
)));
272 Wire
*wire
= module
->addWire(NEW_ID
, GetSize(w
));
274 wire
->set_bool_attribute(ID(abc9_padding
));
276 cell
->setPort(port_name
, rhs
);
281 cell
->attributes
["\\abc9_box_seq"] = box_list
.size();
282 box_list
.emplace_back(cell
);
284 log_assert(!box_list
.empty());
286 RTLIL::Module
*holes_module
= design
->addModule(stringf("%s$holes", module
->name
.c_str()));
287 log_assert(holes_module
);
288 holes_module
->set_bool_attribute("\\abc9_holes");
290 dict
<IdString
, Cell
*> cell_cache
;
291 dict
<IdString
, std::vector
<IdString
>> box_ports
;
294 for (auto cell
: box_list
) {
295 RTLIL::Module
* orig_box_module
= design
->module(cell
->type
);
296 log_assert(orig_box_module
);
297 IdString derived_name
= orig_box_module
->derive(design
, cell
->parameters
);
298 RTLIL::Module
* box_module
= design
->module(derived_name
);
300 auto r
= cell_cache
.insert(derived_name
);
301 auto &holes_cell
= r
.first
->second
;
303 if (box_module
->has_processes())
304 Pass::call_on_module(design
, box_module
, "proc");
306 auto r2
= box_ports
.insert(cell
->type
);
308 // Make carry in the last PI, and carry out the last PO
309 // since ABC requires it this way
310 IdString carry_in
, carry_out
;
311 for (const auto &port_name
: box_module
->ports
) {
312 auto w
= box_module
->wire(port_name
);
314 if (w
->get_bool_attribute("\\abc9_carry")) {
316 if (carry_in
!= IdString())
317 log_error("Module '%s' contains more than one 'abc9_carry' input port.\n", log_id(box_module
));
318 carry_in
= port_name
;
320 if (w
->port_output
) {
321 if (carry_out
!= IdString())
322 log_error("Module '%s' contains more than one 'abc9_carry' output port.\n", log_id(box_module
));
323 carry_out
= port_name
;
327 r2
.first
->second
.push_back(port_name
);
330 if (carry_in
!= IdString() && carry_out
== IdString())
331 log_error("Module '%s' contains an 'abc9_carry' input port but no output port.\n", log_id(box_module
));
332 if (carry_in
== IdString() && carry_out
!= IdString())
333 log_error("Module '%s' contains an 'abc9_carry' output port but no input port.\n", log_id(box_module
));
334 if (carry_in
!= IdString()) {
335 r2
.first
->second
.push_back(carry_in
);
336 r2
.first
->second
.push_back(carry_out
);
340 if (box_module
->get_bool_attribute("\\whitebox")) {
341 holes_cell
= holes_module
->addCell(cell
->name
, derived_name
);
344 for (auto port_name
: box_ports
.at(cell
->type
)) {
345 RTLIL::Wire
*w
= box_module
->wire(port_name
);
347 log_assert(!w
->port_input
|| !w
->port_output
);
348 auto &conn
= holes_cell
->connections_
[port_name
];
350 for (int i
= 0; i
< GetSize(w
); i
++) {
352 RTLIL::Wire
*holes_wire
= holes_module
->wire(stringf("\\i%d", box_inputs
));
354 holes_wire
= holes_module
->addWire(stringf("\\i%d", box_inputs
));
355 holes_wire
->port_input
= true;
356 holes_wire
->port_id
= port_id
++;
357 holes_module
->ports
.push_back(holes_wire
->name
);
359 conn
.append(holes_wire
);
362 else if (w
->port_output
)
363 conn
= holes_module
->addWire(stringf("%s.%s", derived_name
.c_str(), log_id(port_name
)), GetSize(w
));
366 // For flops only, create an extra 1-bit input that drives a new wire
367 // called "<cell>.abc9_ff.Q" that is used below
368 if (box_module
->get_bool_attribute("\\abc9_flop")) {
370 Wire
*holes_wire
= holes_module
->wire(stringf("\\i%d", box_inputs
));
372 holes_wire
= holes_module
->addWire(stringf("\\i%d", box_inputs
));
373 holes_wire
->port_input
= true;
374 holes_wire
->port_id
= port_id
++;
375 holes_module
->ports
.push_back(holes_wire
->name
);
377 Wire
*Q
= holes_module
->addWire(stringf("%s.abc9_ff.Q", cell
->name
.c_str()));
378 holes_module
->connect(Q
, holes_wire
);
381 else // box_module is a blackbox
382 log_assert(holes_cell
== nullptr);
385 for (auto port_name
: box_ports
.at(cell
->type
)) {
386 RTLIL::Wire
*w
= box_module
->wire(port_name
);
390 Wire
*holes_wire
= holes_module
->addWire(stringf("$abc%s.%s", cell
->name
.c_str(), log_id(port_name
)), GetSize(w
));
391 holes_wire
->port_output
= true;
392 holes_wire
->port_id
= port_id
++;
393 holes_module
->ports
.push_back(holes_wire
->name
);
394 if (holes_cell
) // whitebox
395 holes_module
->connect(holes_wire
, holes_cell
->getPort(port_name
));
397 holes_module
->connect(holes_wire
, Const(State::S0
, GetSize(w
)));
402 void reintegrate(RTLIL::Module
*module
)
404 auto design
= module
->design
;
407 map_autoidx
= autoidx
++;
409 RTLIL::Module
*mapped_mod
= design
->module(stringf("%s$abc9", module
->name
.c_str()));
410 if (mapped_mod
== NULL
)
411 log_error("ABC output file does not contain a module `%s$abc'.\n", log_id(module
));
413 for (auto w
: mapped_mod
->wires())
414 module
->addWire(remap_name(w
->name
), GetSize(w
));
416 dict
<IdString
,IdString
> box_lookup
;
417 dict
<IdString
,std::vector
<IdString
>> box_ports
;
419 for (auto m
: design
->modules()) {
420 auto it
= m
->attributes
.find(ID(abc9_box_id
));
421 if (it
== m
->attributes
.end())
423 if (m
->name
.begins_with("$paramod"))
425 auto id
= it
->second
.as_int();
426 auto r
= box_lookup
.insert(std::make_pair(stringf("$__boxid%d", id
), m
->name
));
428 log_error("Module '%s' has the same abc9_box_id = %d value as '%s'.\n",
429 log_id(m
), id
, log_id(r
.first
->second
));
430 log_assert(r
.second
);
432 auto r2
= box_ports
.insert(m
->name
);
434 // Make carry in the last PI, and carry out the last PO
435 // since ABC requires it this way
436 IdString carry_in
, carry_out
;
437 for (const auto &port_name
: m
->ports
) {
438 auto w
= m
->wire(port_name
);
440 if (w
->get_bool_attribute("\\abc9_carry")) {
442 if (carry_in
!= IdString())
443 log_error("Module '%s' contains more than one 'abc9_carry' input port.\n", log_id(m
));
444 carry_in
= port_name
;
446 if (w
->port_output
) {
447 if (carry_out
!= IdString())
448 log_error("Module '%s' contains more than one 'abc9_carry' output port.\n", log_id(m
));
449 carry_out
= port_name
;
453 r2
.first
->second
.push_back(port_name
);
456 if (carry_in
!= IdString() && carry_out
== IdString())
457 log_error("Module '%s' contains an 'abc9_carry' input port but no output port.\n", log_id(m
));
458 if (carry_in
== IdString() && carry_out
!= IdString())
459 log_error("Module '%s' contains an 'abc9_carry' output port but no input port.\n", log_id(m
));
460 if (carry_in
!= IdString()) {
461 r2
.first
->second
.push_back(carry_in
);
462 r2
.first
->second
.push_back(carry_out
);
467 std::vector
<Cell
*> boxes
;
468 for (auto cell
: module
->cells().to_vector()) {
469 if (cell
->has_keep_attr())
471 if (cell
->type
.in(ID($_AND_
), ID($_NOT_
), ID($__ABC9_FF_
)))
472 module
->remove(cell
);
473 else if (cell
->attributes
.erase("\\abc9_box_seq"))
474 boxes
.emplace_back(cell
);
477 dict
<SigBit
, pool
<IdString
>> bit_drivers
, bit_users
;
478 TopoSort
<IdString
, RTLIL::sort_by_id_str
> toposort
;
479 dict
<RTLIL::Cell
*,RTLIL::Cell
*> not2drivers
;
480 dict
<SigBit
, std::vector
<RTLIL::Cell
*>> bit2sinks
;
482 std::map
<IdString
, int> cell_stats
;
483 for (auto mapped_cell
: mapped_mod
->cells())
485 toposort
.node(mapped_cell
->name
);
487 if (mapped_cell
->type
== ID($_NOT_
)) {
488 RTLIL::SigBit a_bit
= mapped_cell
->getPort(ID::A
);
489 RTLIL::SigBit y_bit
= mapped_cell
->getPort(ID::Y
);
490 bit_users
[a_bit
].insert(mapped_cell
->name
);
491 bit_drivers
[y_bit
].insert(mapped_cell
->name
);
494 mapped_cell
->setPort(ID::Y
, module
->addWire(NEW_ID
));
495 RTLIL::Wire
*wire
= module
->wire(remap_name(y_bit
.wire
->name
));
497 module
->connect(RTLIL::SigBit(wire
, y_bit
.offset
), State::S1
);
500 RTLIL::Cell
* driver_lut
= nullptr;
501 // ABC can return NOT gates that drive POs
502 if (!a_bit
.wire
->port_input
) {
503 // If it's not a NOT gate that that comes from a PI directly,
504 // find the driver LUT and clone that to guarantee that we won't
505 // increase the max logic depth
506 // (TODO: Optimise by not cloning unless will increase depth)
507 RTLIL::IdString driver_name
;
508 if (GetSize(a_bit
.wire
) == 1)
509 driver_name
= stringf("%s$lut", a_bit
.wire
->name
.c_str());
511 driver_name
= stringf("%s[%d]$lut", a_bit
.wire
->name
.c_str(), a_bit
.offset
);
512 driver_lut
= mapped_mod
->cell(driver_name
);
516 // If a driver couldn't be found (could be from PI or box CI)
517 // then implement using a LUT
518 RTLIL::Cell
*cell
= module
->addLut(remap_name(stringf("%s$lut", mapped_cell
->name
.c_str())),
519 RTLIL::SigBit(module
->wires_
.at(remap_name(a_bit
.wire
->name
)), a_bit
.offset
),
520 RTLIL::SigBit(module
->wires_
.at(remap_name(y_bit
.wire
->name
)), y_bit
.offset
),
521 RTLIL::Const::from_string("01"));
522 bit2sinks
[cell
->getPort(ID::A
)].push_back(cell
);
523 cell_stats
[ID($lut
)]++;
526 not2drivers
[mapped_cell
] = driver_lut
;
531 if (mapped_cell
->type
.in(ID($lut
), ID($__ABC9_FF_
))) {
532 // Convert buffer into direct connection
533 if (mapped_cell
->type
== ID($lut
) &&
534 GetSize(mapped_cell
->getPort(ID::A
)) == 1 &&
535 mapped_cell
->getParam(ID(LUT
)) == RTLIL::Const::from_string("01")) {
536 SigSpec my_a
= module
->wires_
.at(remap_name(mapped_cell
->getPort(ID::A
).as_wire()->name
));
537 SigSpec my_y
= module
->wires_
.at(remap_name(mapped_cell
->getPort(ID::Y
).as_wire()->name
));
538 module
->connect(my_y
, my_a
);
542 RTLIL::Cell
*cell
= module
->addCell(remap_name(mapped_cell
->name
), mapped_cell
->type
);
543 cell
->parameters
= mapped_cell
->parameters
;
544 cell
->attributes
= mapped_cell
->attributes
;
546 for (auto &mapped_conn
: mapped_cell
->connections()) {
547 RTLIL::SigSpec newsig
;
548 for (auto c
: mapped_conn
.second
.chunks()) {
551 //log_assert(c.width == 1);
553 c
.wire
= module
->wires_
.at(remap_name(c
.wire
->name
));
556 cell
->setPort(mapped_conn
.first
, newsig
);
558 if (cell
->input(mapped_conn
.first
)) {
559 for (auto i
: newsig
)
560 bit2sinks
[i
].push_back(cell
);
561 for (auto i
: mapped_conn
.second
)
562 bit_users
[i
].insert(mapped_cell
->name
);
564 if (cell
->output(mapped_conn
.first
))
565 for (auto i
: mapped_conn
.second
)
566 // Ignore inouts for topo ordering
567 if (i
.wire
&& !(i
.wire
->port_input
&& i
.wire
->port_output
))
568 bit_drivers
[i
].insert(mapped_cell
->name
);
572 RTLIL::Cell
*existing_cell
= module
->cell(mapped_cell
->name
);
573 log_assert(existing_cell
);
574 log_assert(mapped_cell
->type
.begins_with("$__boxid"));
576 auto type
= box_lookup
.at(mapped_cell
->type
, IdString());
577 if (type
== IdString())
578 log_error("No module with abc9_box_id = %s found.\n", mapped_cell
->type
.c_str() + strlen("$__boxid"));
579 mapped_cell
->type
= type
;
581 RTLIL::Cell
*cell
= module
->addCell(remap_name(mapped_cell
->name
), mapped_cell
->type
);
582 cell
->parameters
= existing_cell
->parameters
;
583 cell
->attributes
= existing_cell
->attributes
;
584 module
->swap_names(cell
, existing_cell
);
586 auto it
= mapped_cell
->connections_
.find("\\i");
587 log_assert(it
!= mapped_cell
->connections_
.end());
588 SigSpec inputs
= std::move(it
->second
);
589 mapped_cell
->connections_
.erase(it
);
590 it
= mapped_cell
->connections_
.find("\\o");
591 log_assert(it
!= mapped_cell
->connections_
.end());
592 SigSpec outputs
= std::move(it
->second
);
593 mapped_cell
->connections_
.erase(it
);
595 RTLIL::Module
* box_module
= design
->module(mapped_cell
->type
);
596 auto abc9_flop
= box_module
->attributes
.count("\\abc9_flop");
598 for (const auto &i
: inputs
)
599 bit_users
[i
].insert(mapped_cell
->name
);
600 for (const auto &i
: outputs
)
601 bit_drivers
[i
].insert(mapped_cell
->name
);
604 int input_count
= 0, output_count
= 0;
605 for (const auto &port_name
: box_ports
.at(cell
->type
)) {
606 RTLIL::Wire
*w
= box_module
->wire(port_name
);
611 sig
= inputs
.extract(input_count
, GetSize(w
));
612 input_count
+= GetSize(w
);
614 if (w
->port_output
) {
615 sig
= outputs
.extract(output_count
, GetSize(w
));
616 output_count
+= GetSize(w
);
620 for (auto c
: sig
.chunks()) {
623 //log_assert(c.width == 1);
625 c
.wire
= module
->wires_
.at(remap_name(c
.wire
->name
));
629 auto it
= existing_cell
->connections_
.find(port_name
);
630 if (it
== existing_cell
->connections_
.end())
632 if (GetSize(newsig
) > GetSize(it
->second
))
633 newsig
= newsig
.extract(0, GetSize(it
->second
));
635 log_assert(GetSize(newsig
) == GetSize(it
->second
));
637 cell
->setPort(port_name
, newsig
);
639 if (w
->port_input
&& !abc9_flop
)
640 for (const auto &i
: newsig
)
641 bit2sinks
[i
].push_back(cell
);
645 cell_stats
[mapped_cell
->type
]++;
648 for (auto cell
: boxes
)
649 module
->remove(cell
);
651 // Copy connections (and rename) from mapped_mod to module
652 for (auto conn
: mapped_mod
->connections()) {
653 if (!conn
.first
.is_fully_const()) {
654 auto chunks
= conn
.first
.chunks();
655 for (auto &c
: chunks
)
656 c
.wire
= module
->wires_
.at(remap_name(c
.wire
->name
));
657 conn
.first
= std::move(chunks
);
659 if (!conn
.second
.is_fully_const()) {
660 auto chunks
= conn
.second
.chunks();
661 for (auto &c
: chunks
)
663 c
.wire
= module
->wires_
.at(remap_name(c
.wire
->name
));
664 conn
.second
= std::move(chunks
);
666 module
->connect(conn
);
669 for (auto &it
: cell_stats
)
670 log("ABC RESULTS: %15s cells: %8d\n", it
.first
.c_str(), it
.second
);
671 int in_wires
= 0, out_wires
= 0;
673 // Stitch in mapped_mod's inputs/outputs into module
674 for (auto port
: mapped_mod
->ports
) {
675 RTLIL::Wire
*w
= mapped_mod
->wire(port
);
676 RTLIL::Wire
*wire
= module
->wire(port
);
678 RTLIL::Wire
*remap_wire
= module
->wire(remap_name(port
));
679 RTLIL::SigSpec
signal(wire
, 0, GetSize(remap_wire
));
680 log_assert(GetSize(signal
) >= GetSize(remap_wire
));
683 if (w
->port_output
) {
685 conn
.second
= remap_wire
;
687 module
->connect(conn
);
689 else if (w
->port_input
) {
690 conn
.first
= remap_wire
;
691 conn
.second
= signal
;
693 module
->connect(conn
);
697 for (auto &it
: bit_users
)
698 if (bit_drivers
.count(it
.first
))
699 for (auto driver_cell
: bit_drivers
.at(it
.first
))
700 for (auto user_cell
: it
.second
)
701 toposort
.edge(driver_cell
, user_cell
);
702 bool no_loops
YS_ATTRIBUTE(unused
) = toposort
.sort();
703 log_assert(no_loops
);
705 for (auto ii
= toposort
.sorted
.rbegin(); ii
!= toposort
.sorted
.rend(); ii
++) {
706 RTLIL::Cell
*not_cell
= mapped_mod
->cell(*ii
);
707 log_assert(not_cell
);
708 if (not_cell
->type
!= ID($_NOT_
))
710 auto it
= not2drivers
.find(not_cell
);
711 if (it
== not2drivers
.end())
713 RTLIL::Cell
*driver_lut
= it
->second
;
714 RTLIL::SigBit a_bit
= not_cell
->getPort(ID::A
);
715 RTLIL::SigBit y_bit
= not_cell
->getPort(ID::Y
);
716 RTLIL::Const driver_mask
;
718 a_bit
.wire
= module
->wires_
.at(remap_name(a_bit
.wire
->name
));
719 y_bit
.wire
= module
->wires_
.at(remap_name(y_bit
.wire
->name
));
721 auto jt
= bit2sinks
.find(a_bit
);
722 if (jt
== bit2sinks
.end())
725 for (auto sink_cell
: jt
->second
)
726 if (sink_cell
->type
!= ID($lut
))
729 // Push downstream LUTs past inverter
730 for (auto sink_cell
: jt
->second
) {
731 SigSpec A
= sink_cell
->getPort(ID::A
);
732 RTLIL::Const mask
= sink_cell
->getParam(ID(LUT
));
734 for (; index
< GetSize(A
); index
++)
735 if (A
[index
] == a_bit
)
737 log_assert(index
< GetSize(A
));
739 while (i
< GetSize(mask
)) {
740 for (int j
= 0; j
< (1 << index
); j
++)
741 std::swap(mask
[i
+j
], mask
[i
+j
+(1 << index
)]);
745 sink_cell
->setPort(ID::A
, A
);
746 sink_cell
->setParam(ID(LUT
), mask
);
749 // Since we have rewritten all sinks (which we know
750 // to be only LUTs) to be after the inverter, we can
751 // go ahead and clone the LUT with the expectation
752 // that the original driving LUT will become dangling
753 // and get cleaned away
755 driver_mask
= driver_lut
->getParam(ID(LUT
));
756 for (auto &b
: driver_mask
.bits
) {
757 if (b
== RTLIL::State::S0
) b
= RTLIL::State::S1
;
758 else if (b
== RTLIL::State::S1
) b
= RTLIL::State::S0
;
760 auto cell
= module
->addLut(NEW_ID
,
761 driver_lut
->getPort(ID::A
),
764 for (auto &bit
: cell
->connections_
.at(ID::A
)) {
765 bit
.wire
= module
->wires_
.at(remap_name(bit
.wire
->name
));
766 bit2sinks
[bit
].push_back(cell
);
770 //log("ABC RESULTS: internal signals: %8d\n", int(signal_list.size()) - in_wires - out_wires);
771 log("ABC RESULTS: input signals: %8d\n", in_wires
);
772 log("ABC RESULTS: output signals: %8d\n", out_wires
);
774 design
->remove(mapped_mod
);
777 struct Abc9OpsPass
: public Pass
{
778 Abc9OpsPass() : Pass("abc9_ops", "helper functions for ABC9") { }
779 void help() YS_OVERRIDE
781 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
783 log(" abc9_ops [options] [selection]\n");
786 void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) YS_OVERRIDE
788 log_header(design
, "Executing ABC9_OPS pass (helper functions for ABC9).\n");
790 bool break_scc_mode
= false;
791 bool unbreak_scc_mode
= false;
792 bool prep_dff_mode
= false;
793 bool prep_holes_mode
= false;
794 bool reintegrate_mode
= false;
795 bool dff_mode
= false;
798 for (argidx
= 1; argidx
< args
.size(); argidx
++) {
799 std::string arg
= args
[argidx
];
800 if (arg
== "-break_scc") {
801 break_scc_mode
= true;
804 if (arg
== "-unbreak_scc") {
805 unbreak_scc_mode
= true;
808 if (arg
== "-prep_dff") {
809 prep_dff_mode
= true;
812 if (arg
== "-prep_holes") {
813 prep_holes_mode
= true;
816 if (arg
== "-reintegrate") {
817 reintegrate_mode
= true;
826 extra_args(args
, argidx
, design
);
828 if (!(break_scc_mode
|| unbreak_scc_mode
|| prep_dff_mode
|| reintegrate_mode
))
829 log_cmd_error("At least one of -{,un}break_scc, -prep_{dff,holes}, -reintegrate must be specified.\n");
831 if (dff_mode
&& !prep_holes_mode
)
832 log_cmd_error("'-dff' option is only relevant for -prep_holes.\n");
834 for (auto mod
: design
->selected_modules()) {
835 if (mod
->get_bool_attribute("\\abc9_holes"))
838 if (mod
->processes
.size() > 0) {
839 log("Skipping module %s as it contains processes.\n", log_id(mod
));
843 if (!design
->selected_whole_module(mod
))
844 log_error("Can't handle partially selected module %s!\n", log_id(mod
));
848 if (unbreak_scc_mode
)
853 prep_holes(mod
, dff_mode
);
854 if (reintegrate_mode
)
860 PRIVATE_NAMESPACE_END