dcb8a8a78901190466a2a0a3c0f9092a3975fb9e
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"
26 PRIVATE_NAMESPACE_BEGIN
28 void break_scc(RTLIL::Module
*module
)
30 // For every unique SCC found, (arbitrarily) find the first
31 // cell in the component, and convert all wires driven by
32 // its output ports into a new PO, and drive its previous
33 // sinks with a new PI
34 pool
<RTLIL::Const
> ids_seen
;
35 for (auto cell
: module
->selected_cells()) {
36 auto it
= cell
->attributes
.find(ID(abc9_scc_id
));
37 if (it
== cell
->attributes
.end())
39 auto r
= ids_seen
.insert(it
->second
);
40 cell
->attributes
.erase(it
);
43 for (auto &c
: cell
->connections_
) {
44 if (c
.second
.is_fully_const()) continue;
45 if (cell
->output(c
.first
)) {
46 SigBit b
= c
.second
.as_bit();
49 // In this case, hopefully the loop break has been already created
50 // Get the non-prefixed wire
51 Wire
*wo
= module
->wire(stringf("%s.abco", b
.wire
->name
.c_str()));
52 log_assert(wo
!= nullptr);
53 log_assert(wo
->port_output
);
54 log_assert(b
.offset
< GetSize(wo
));
55 c
.second
= RTLIL::SigBit(wo
, b
.offset
);
58 // Create a new output/input loop break
60 w
= module
->wire(stringf("%s.abco", w
->name
.c_str()));
62 w
= module
->addWire(stringf("%s.abco", b
.wire
->name
.c_str()), GetSize(b
.wire
));
63 w
->port_output
= true;
66 log_assert(w
->port_input
);
67 log_assert(b
.offset
< GetSize(w
));
69 w
->set_bool_attribute(ID(abc9_scc_break
));
70 c
.second
= RTLIL::SigBit(w
, b
.offset
);
76 module
->fixup_ports();
79 void unbreak_scc(RTLIL::Module
*module
)
81 // Now 'unexpose' those wires by undoing
82 // the expose operation -- remove them from PO/PI
83 // and re-connecting them back together
84 for (auto wire
: module
->wires()) {
85 auto it
= wire
->attributes
.find(ID(abc9_scc_break
));
86 if (it
!= wire
->attributes
.end()) {
87 wire
->attributes
.erase(it
);
88 log_assert(wire
->port_output
);
89 wire
->port_output
= false;
90 std::string name
= wire
->name
.str();
91 RTLIL::Wire
*i_wire
= module
->wire(name
.substr(0, GetSize(name
) - 5));
93 log_assert(i_wire
->port_input
);
94 i_wire
->port_input
= false;
95 module
->connect(i_wire
, wire
);
98 module
->fixup_ports();
101 void prep_dff(RTLIL::Module
*module
)
103 auto design
= module
->design
;
106 SigMap
assign_map(module
);
108 typedef SigSpec clkdomain_t
;
109 dict
<clkdomain_t
, int> clk_to_mergeability
;
112 for (auto cell
: module
->selected_cells()) {
113 if (cell
->type
!= "$__ABC9_FF_")
116 Wire
*abc9_clock_wire
= module
->wire(stringf("%s.clock", cell
->name
.c_str()));
117 if (abc9_clock_wire
== NULL
)
118 log_error("'%s.clock' is not a wire present in module '%s'.\n", cell
->name
.c_str(), log_id(module
));
119 SigSpec abc9_clock
= assign_map(abc9_clock_wire
);
121 clkdomain_t
key(abc9_clock
);
123 auto r
= clk_to_mergeability
.insert(std::make_pair(abc9_clock
, clk_to_mergeability
.size() + 1));
124 auto r2
YS_ATTRIBUTE(unused
) = cell
->attributes
.insert(std::make_pair(ID(abc9_mergeability
), r
.first
->second
));
125 log_assert(r2
.second
);
127 Wire
*abc9_init_wire
= module
->wire(stringf("%s.init", cell
->name
.c_str()));
128 if (abc9_init_wire
== NULL
)
129 log_error("'%s.init' is not a wire present in module '%s'.\n", cell
->name
.c_str(), log_id(module
));
130 log_assert(GetSize(abc9_init_wire
) == 1);
131 SigSpec abc9_init
= assign_map(abc9_init_wire
);
132 if (!abc9_init
.is_fully_const())
133 log_error("'%s.init' is not a constant wire present in module '%s'.\n", cell
->name
.c_str(), log_id(module
));
134 r2
= cell
->attributes
.insert(std::make_pair(ID(abc9_init
), abc9_init
.as_const()));
135 log_assert(r2
.second
);
138 // for (auto cell : module->selected_cells()) {
139 // auto inst_module = design->module(cell->type);
140 // if (!inst_module || !inst_module->get_bool_attribute("\\abc9_flop"))
142 // cell->set_bool_attribute("\\abc9_keep");
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 auto it
= replace
.find(conn
);
184 if (it
!= replace
.end())
190 void prep_holes(RTLIL::Module
*module
)
192 auto design
= module
->design
;
195 SigMap
sigmap(module
);
197 // TODO: Speed up toposort -- ultimately we care about
198 // box ordering, but not individual AIG cells
199 dict
<SigBit
, pool
<IdString
>> bit_drivers
, bit_users
;
200 TopoSort
<IdString
, RTLIL::sort_by_id_str
> toposort
;
201 bool abc9_box_seen
= false;
203 for (auto cell
: module
->selected_cells()) {
204 if (cell
->type
== "$_NOT_")
206 SigBit A
= sigmap(cell
->getPort("\\A").as_bit());
207 SigBit Y
= sigmap(cell
->getPort("\\Y").as_bit());
208 toposort
.node(cell
->name
);
209 bit_users
[A
].insert(cell
->name
);
210 bit_drivers
[Y
].insert(cell
->name
);
214 if (cell
->type
== "$_AND_")
216 SigBit A
= sigmap(cell
->getPort("\\A").as_bit());
217 SigBit B
= sigmap(cell
->getPort("\\B").as_bit());
218 SigBit Y
= sigmap(cell
->getPort("\\Y").as_bit());
219 toposort
.node(cell
->name
);
220 bit_users
[A
].insert(cell
->name
);
221 bit_users
[B
].insert(cell
->name
);
222 bit_drivers
[Y
].insert(cell
->name
);
226 if (cell
->type
== "$__ABC9_FF_")
229 RTLIL::Module
* inst_module
= design
->module(cell
->type
);
231 if (!inst_module
->attributes
.count("\\abc9_box_id") || cell
->get_bool_attribute("\\abc9_keep"))
234 for (const auto &conn
: cell
->connections()) {
235 auto port_wire
= inst_module
->wire(conn
.first
);
236 // Ignore inout for the sake of topographical ordering
237 if (port_wire
->port_input
&& !port_wire
->port_output
)
238 for (auto bit
: sigmap(conn
.second
))
239 bit_users
[bit
].insert(cell
->name
);
240 if (port_wire
->port_output
)
241 for (auto bit
: sigmap(conn
.second
))
242 bit_drivers
[bit
].insert(cell
->name
);
245 abc9_box_seen
= true;
247 toposort
.node(cell
->name
);
254 for (auto &it
: bit_users
)
255 if (bit_drivers
.count(it
.first
))
256 for (auto driver_cell
: bit_drivers
.at(it
.first
))
257 for (auto user_cell
: it
.second
)
258 toposort
.edge(driver_cell
, user_cell
);
261 toposort
.analyze_loops
= true;
263 bool no_loops
YS_ATTRIBUTE(unused
) = toposort
.sort();
266 for (auto &it
: toposort
.loops
) {
267 log(" loop %d\n", i
++);
268 for (auto cell_name
: it
) {
269 auto cell
= module
->cell(cell_name
);
271 log("\t%s (%s @ %s)\n", log_id(cell
), log_id(cell
->type
), cell
->get_src_attribute().c_str());
275 log_assert(no_loops
);
277 vector
<Cell
*> box_list
;
278 for (auto cell_name
: toposort
.sorted
) {
279 RTLIL::Cell
*cell
= module
->cell(cell_name
);
282 RTLIL::Module
* box_module
= design
->module(cell
->type
);
283 if (!box_module
|| !box_module
->attributes
.count("\\abc9_box_id")
284 || cell
->get_bool_attribute("\\abc9_keep"))
287 bool blackbox
= box_module
->get_blackbox_attribute(true /* ignore_wb */);
289 // Fully pad all unused input connections of this box cell with S0
290 // Fully pad all undriven output connections of this box cell with anonymous wires
291 // NB: Assume box_module->ports are sorted alphabetically
292 // (as RTLIL::Module::fixup_ports() would do)
293 for (const auto &port_name
: box_module
->ports
) {
294 RTLIL::Wire
* w
= box_module
->wire(port_name
);
296 auto it
= cell
->connections_
.find(port_name
);
299 if (it
!= cell
->connections_
.end()) {
300 if (GetSize(it
->second
) < GetSize(w
))
301 it
->second
.append(RTLIL::SigSpec(State::S0
, GetSize(w
)-GetSize(it
->second
)));
305 rhs
= RTLIL::SigSpec(State::S0
, GetSize(w
));
306 cell
->setPort(port_name
, rhs
);
309 if (w
->port_output
) {
311 auto it
= cell
->connections_
.find(w
->name
);
312 if (it
!= cell
->connections_
.end()) {
313 if (GetSize(it
->second
) < GetSize(w
))
314 it
->second
.append(module
->addWire(NEW_ID
, GetSize(w
)-GetSize(it
->second
)));
318 Wire
*wire
= module
->addWire(NEW_ID
, GetSize(w
));
320 wire
->set_bool_attribute(ID(abc9_padding
));
322 cell
->setPort(port_name
, rhs
);
327 box_list
.emplace_back(cell
);
329 log_assert(!box_list
.empty());
331 RTLIL::Module
*holes_module
= design
->addModule(stringf("%s$holes", module
->name
.c_str()));
332 log_assert(holes_module
);
333 holes_module
->set_bool_attribute("\\abc9_holes");
335 dict
<IdString
, Cell
*> cell_cache
;
338 for (auto cell
: box_list
) {
339 RTLIL::Module
* orig_box_module
= design
->module(cell
->type
);
340 log_assert(orig_box_module
);
341 IdString derived_name
= orig_box_module
->derive(design
, cell
->parameters
);
342 RTLIL::Module
* box_module
= design
->module(derived_name
);
343 if (box_module
->has_processes())
344 Pass::call_on_module(design
, box_module
, "proc");
347 auto r
= cell_cache
.insert(std::make_pair(derived_name
, nullptr));
348 Cell
*holes_cell
= r
.first
->second
;
349 if (r
.second
&& box_module
->get_bool_attribute("\\whitebox")) {
350 holes_cell
= holes_module
->addCell(cell
->name
, cell
->type
);
351 holes_cell
->parameters
= cell
->parameters
;
352 r
.first
->second
= holes_cell
;
354 // Since Module::derive() will create a new module, there
355 // is a chance that the ports will be alphabetically ordered
356 // again, which is a problem when carry-chains are involved.
357 // Inherit the port ordering from the original module here...
358 // (and set the port_id below, when iterating through those)
359 log_assert(GetSize(box_module
->ports
) == GetSize(orig_box_module
->ports
));
360 box_module
->ports
= orig_box_module
->ports
;
363 // NB: Assume box_module->ports are sorted alphabetically
364 // (as RTLIL::Module::fixup_ports() would do)
366 for (const auto &port_name
: box_module
->ports
) {
367 RTLIL::Wire
*w
= box_module
->wire(port_name
);
370 w
->port_id
= box_port_id
++;
371 RTLIL::Wire
*holes_wire
;
372 RTLIL::SigSpec port_sig
;
374 for (int i
= 0; i
< GetSize(w
); i
++) {
376 holes_wire
= holes_module
->wire(stringf("\\i%d", box_inputs
));
378 holes_wire
= holes_module
->addWire(stringf("\\i%d", box_inputs
));
379 holes_wire
->port_input
= true;
380 holes_wire
->port_id
= port_id
++;
381 holes_module
->ports
.push_back(holes_wire
->name
);
384 port_sig
.append(holes_wire
);
387 for (int i
= 0; i
< GetSize(w
); i
++) {
389 holes_wire
= holes_module
->addWire(stringf("$abc%s.%s", cell
->name
.c_str(), log_id(w
->name
)));
391 holes_wire
= holes_module
->addWire(stringf("$abc%s.%s[%d]", cell
->name
.c_str(), log_id(w
->name
), i
));
392 holes_wire
->port_output
= true;
393 holes_wire
->port_id
= port_id
++;
394 holes_module
->ports
.push_back(holes_wire
->name
);
396 port_sig
.append(holes_wire
);
398 holes_module
->connect(holes_wire
, State::S0
);
400 if (!port_sig
.empty()) {
402 holes_cell
->setPort(w
->name
, port_sig
);
404 holes_module
->connect(holes_cell
->getPort(w
->name
), port_sig
);
408 // For flops only, create an extra 1-bit input that drives a new wire
409 // called "<cell>.$abc9_currQ" that is used below
410 if (box_module
->get_bool_attribute("\\abc9_flop")) {
411 log_assert(holes_cell
);
414 Wire
*holes_wire
= holes_module
->wire(stringf("\\i%d", box_inputs
));
416 holes_wire
= holes_module
->addWire(stringf("\\i%d", box_inputs
));
417 holes_wire
->port_input
= true;
418 holes_wire
->port_id
= port_id
++;
419 holes_module
->ports
.push_back(holes_wire
->name
);
421 Wire
*w
= holes_module
->addWire(stringf("%s.abc9_ff.Q", cell
->name
.c_str()));
422 holes_module
->connect(w
, holes_wire
);
427 struct Abc9OpsPass
: public Pass
{
428 Abc9OpsPass() : Pass("abc9_ops", "helper functions for ABC9") { }
429 void help() YS_OVERRIDE
431 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
433 log(" abc9_ops [options] [selection]\n");
436 void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) YS_OVERRIDE
438 log_header(design
, "Executing ABC9_OPS pass (helper functions for ABC9).\n");
441 bool break_scc_mode
= false;
442 bool unbreak_scc_mode
= false;
443 bool prep_dff_mode
= false;
444 bool prep_holes_mode
= false;
447 for (argidx
= 1; argidx
< args
.size(); argidx
++) {
448 std::string arg
= args
[argidx
];
449 if (arg
== "-break_scc") {
450 break_scc_mode
= true;
453 if (arg
== "-unbreak_scc") {
454 unbreak_scc_mode
= true;
457 if (arg
== "-prep_dff") {
458 prep_dff_mode
= true;
461 if (arg
== "-prep_holes") {
462 prep_holes_mode
= true;
467 extra_args(args
, argidx
, design
);
469 for (auto mod
: design
->selected_modules()) {
470 if (mod
->get_blackbox_attribute())
472 if (mod
->get_bool_attribute("\\abc9_holes"))
475 if (mod
->processes
.size() > 0) {
476 log("Skipping module %s as it contains processes.\n", log_id(mod
));
482 if (unbreak_scc_mode
)
492 PRIVATE_NAMESPACE_END