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 check(RTLIL::Design
*design
)
38 dict
<IdString
,IdString
> box_lookup
;
39 for (auto m
: design
->modules()) {
40 if (m
->name
.begins_with("$paramod"))
43 auto flop
= m
->get_bool_attribute(ID(abc9_flop
));
44 auto it
= m
->attributes
.find(ID(abc9_box_id
));
46 if (it
== m
->attributes
.end())
48 auto id
= it
->second
.as_int();
49 auto r
= box_lookup
.insert(std::make_pair(stringf("$__boxid%d", id
), m
->name
));
51 log_error("Module '%s' has the same abc9_box_id = %d value as '%s'.\n",
52 log_id(m
), id
, log_id(r
.first
->second
));
55 // Make carry in the last PI, and carry out the last PO
56 // since ABC requires it this way
57 IdString carry_in
, carry_out
;
58 for (const auto &port_name
: m
->ports
) {
59 auto w
= m
->wire(port_name
);
61 if (w
->get_bool_attribute("\\abc9_carry")) {
63 if (carry_in
!= IdString())
64 log_error("Module '%s' contains more than one (* abc9_carry *) input port.\n", log_id(m
));
68 if (carry_out
!= IdString())
69 log_error("Module '%s' contains more than one (* abc9_carry *) output port.\n", log_id(m
));
70 carry_out
= port_name
;
74 auto it
= w
->attributes
.find("\\abc9_arrival");
75 if (it
!= w
->attributes
.end()) {
77 if (it
->second
.flags
== 0) {
78 if (it
->second
.as_int() < 0)
79 log_error("%s.%s has negative arrival value %d!\n", log_id(m
), log_id(port_name
),
84 for (const auto &tok
: split_tokens(it
->second
.decode_string())) {
85 if (tok
.find_first_not_of("0123456789") != std::string::npos
)
86 log_error("%s.%s has non-integer arrival value '%s'!\n", log_id(m
), log_id(port_name
),
88 if (atoi(tok
.c_str()) < 0)
89 log_error("%s.%s has negative arrival value %s!\n", log_id(m
), log_id(port_name
),
93 if (count
> 1 && count
!= GetSize(w
))
94 log_error("%s.%s is %d bits wide but abc9_arrival = %s has %d value(s)!\n", log_id(m
), log_id(port_name
),
95 GetSize(w
), log_signal(it
->second
), count
);
98 it
= w
->attributes
.find("\\abc9_required");
99 if (it
!= w
->attributes
.end()) {
101 if (it
->second
.flags
== 0) {
102 if (it
->second
.as_int() < 0)
103 log_error("%s.%s has negative required value %d!\n", log_id(m
), log_id(port_name
),
104 it
->second
.as_int());
108 for (const auto &tok
: split_tokens(it
->second
.decode_string())) {
109 if (tok
.find_first_not_of("0123456789") != std::string::npos
)
110 log_error("%s.%s has non-integer required value '%s'!\n", log_id(m
), log_id(port_name
),
112 if (atoi(tok
.c_str()) < 0)
113 log_error("%s.%s has negative required value %s!\n", log_id(m
), log_id(port_name
),
117 if (count
> 1 && count
!= GetSize(w
))
118 log_error("%s.%s is %d bits wide but abc9_required = %s has %d value(s)!\n", log_id(m
), log_id(port_name
),
119 GetSize(w
), log_signal(it
->second
), count
);
123 if (carry_in
!= IdString() && carry_out
== IdString())
124 log_error("Module '%s' contains an (* abc9_carry *) input port but no output port.\n", log_id(m
));
125 if (carry_in
== IdString() && carry_out
!= IdString())
126 log_error("Module '%s' contains an (* abc9_carry *) output port but no input port.\n", log_id(m
));
130 for (auto port_name
: m
->ports
) {
131 auto wire
= m
->wire(port_name
);
132 if (wire
->port_output
) num_outputs
++;
134 if (num_outputs
!= 1)
135 log_error("Module '%s' with (* abc9_flop *) has %d outputs (expect 1).\n", log_id(m
), num_outputs
);
140 void mark_scc(RTLIL::Module
*module
)
142 // For every unique SCC found, (arbitrarily) find the first
143 // cell in the component, and convert all wires driven by
144 // its output ports into a new PO, and drive its previous
145 // sinks with a new PI
146 pool
<RTLIL::Const
> ids_seen
;
147 for (auto cell
: module
->cells()) {
148 auto it
= cell
->attributes
.find(ID(abc9_scc_id
));
149 if (it
== cell
->attributes
.end())
151 auto id
= it
->second
;
152 auto r
= ids_seen
.insert(id
);
153 cell
->attributes
.erase(it
);
156 for (auto &c
: cell
->connections_
) {
157 if (c
.second
.is_fully_const()) continue;
158 if (cell
->output(c
.first
)) {
159 SigBit b
= c
.second
.as_bit();
161 w
->set_bool_attribute(ID::keep
);
162 w
->attributes
[ID(abc9_scc_id
)] = id
.as_int();
167 module
->fixup_ports();
170 void prep_dff(RTLIL::Module
*module
)
172 auto design
= module
->design
;
175 SigMap
assign_map(module
);
177 typedef SigSpec clkdomain_t
;
178 dict
<clkdomain_t
, int> clk_to_mergeability
;
180 for (auto cell
: module
->cells()) {
181 if (cell
->type
!= "$__ABC9_FF_")
184 Wire
*abc9_clock_wire
= module
->wire(stringf("%s.clock", cell
->name
.c_str()));
185 if (abc9_clock_wire
== NULL
)
186 log_error("'%s.clock' is not a wire present in module '%s'.\n", cell
->name
.c_str(), log_id(module
));
187 SigSpec abc9_clock
= assign_map(abc9_clock_wire
);
189 clkdomain_t
key(abc9_clock
);
191 auto r
= clk_to_mergeability
.insert(std::make_pair(abc9_clock
, clk_to_mergeability
.size() + 1));
192 auto r2
= cell
->attributes
.insert(ID(abc9_mergeability
));;
193 log_assert(r2
.second
);
194 r2
.first
->second
= r
.first
->second
;
197 RTLIL::Module
*holes_module
= design
->module(stringf("%s$holes", module
->name
.c_str()));
199 SigMap
sigmap(holes_module
);
201 dict
<SigSpec
, SigSpec
> replace
;
202 for (auto cell
: holes_module
->cells().to_vector()) {
203 if (!cell
->type
.in("$_DFF_N_", "$_DFF_NN0_", "$_DFF_NN1_", "$_DFF_NP0_", "$_DFF_NP1_",
204 "$_DFF_P_", "$_DFF_PN0_", "$_DFF_PN1", "$_DFF_PP0_", "$_DFF_PP1_"))
206 SigBit D
= cell
->getPort("\\D");
207 SigBit Q
= cell
->getPort("\\Q");
208 // Emulate async control embedded inside $_DFF_* cell with mux in front of D
209 if (cell
->type
.in("$_DFF_NN0_", "$_DFF_PN0_"))
210 D
= holes_module
->MuxGate(NEW_ID
, State::S0
, D
, cell
->getPort("\\R"));
211 else if (cell
->type
.in("$_DFF_NN1_", "$_DFF_PN1_"))
212 D
= holes_module
->MuxGate(NEW_ID
, State::S1
, D
, cell
->getPort("\\R"));
213 else if (cell
->type
.in("$_DFF_NP0_", "$_DFF_PP0_"))
214 D
= holes_module
->MuxGate(NEW_ID
, D
, State::S0
, cell
->getPort("\\R"));
215 else if (cell
->type
.in("$_DFF_NP1_", "$_DFF_PP1_"))
216 D
= holes_module
->MuxGate(NEW_ID
, D
, State::S1
, cell
->getPort("\\R"));
217 // Remove the $_DFF_* cell from what needs to be a combinatorial box
218 holes_module
->remove(cell
);
220 if (GetSize(Q
.wire
) == 1)
221 port
= holes_module
->wire(stringf("$abc%s", Q
.wire
->name
.c_str()));
223 port
= holes_module
->wire(stringf("$abc%s[%d]", Q
.wire
->name
.c_str(), Q
.offset
));
225 // Prepare to replace "assign <port> = $_DFF_*.Q;" with "assign <port> = $_DFF_*.D;"
226 // in order to extract just the combinatorial control logic that feeds the box
227 // (i.e. clock enable, synchronous reset, etc.)
228 replace
.insert(std::make_pair(Q
,D
));
229 // Since `flatten` above would have created wires named "<cell>.Q",
230 // extract the pre-techmap cell name
231 auto pos
= Q
.wire
->name
.str().rfind(".");
232 log_assert(pos
!= std::string::npos
);
233 IdString driver
= Q
.wire
->name
.substr(0, pos
);
234 // And drive the signal that was previously driven by "DFF.Q" (typically
235 // used to implement clock-enable functionality) with the "<cell>.$abc9_currQ"
236 // wire (which itself is driven an by input port) we inserted above
237 Wire
*currQ
= holes_module
->wire(stringf("%s.abc9_ff.Q", driver
.c_str()));
239 holes_module
->connect(Q
, currQ
);
242 for (auto &conn
: holes_module
->connections_
)
243 conn
.second
= replace
.at(sigmap(conn
.second
), conn
.second
);
247 void prep_xaiger(RTLIL::Module
*module
, bool dff
)
249 auto design
= module
->design
;
252 SigMap
sigmap(module
);
254 dict
<SigBit
, pool
<IdString
>> bit_drivers
, bit_users
;
255 TopoSort
<IdString
, RTLIL::sort_by_id_str
> toposort
;
256 dict
<IdString
, std::vector
<IdString
>> box_ports
;
258 for (auto cell
: module
->cells()) {
259 if (cell
->type
== "$__ABC9_FF_")
261 if (cell
->has_keep_attr())
264 auto inst_module
= module
->design
->module(cell
->type
);
265 bool abc9_flop
= inst_module
&& inst_module
->get_bool_attribute("\\abc9_flop");
266 if (abc9_flop
&& !dff
)
269 if ((inst_module
&& inst_module
->get_bool_attribute("\\abc9_box")) || abc9_flop
) {
270 auto r
= box_ports
.insert(cell
->type
);
272 // Make carry in the last PI, and carry out the last PO
273 // since ABC requires it this way
274 IdString carry_in
, carry_out
;
275 for (const auto &port_name
: inst_module
->ports
) {
276 auto w
= inst_module
->wire(port_name
);
278 if (w
->get_bool_attribute("\\abc9_carry")) {
279 log_assert(w
->port_input
!= w
->port_output
);
281 carry_in
= port_name
;
282 else if (w
->port_output
)
283 carry_out
= port_name
;
286 r
.first
->second
.push_back(port_name
);
288 if (carry_in
!= IdString()) {
289 r
.first
->second
.push_back(carry_in
);
290 r
.first
->second
.push_back(carry_out
);
294 else if (!yosys_celltypes
.cell_known(cell
->type
))
297 // TODO: Speed up toposort -- we care about box ordering only
298 for (auto conn
: cell
->connections()) {
299 if (cell
->input(conn
.first
))
300 for (auto bit
: sigmap(conn
.second
))
301 bit_users
[bit
].insert(cell
->name
);
303 if (cell
->output(conn
.first
) && !abc9_flop
)
304 for (auto bit
: sigmap(conn
.second
))
305 bit_drivers
[bit
].insert(cell
->name
);
307 toposort
.node(cell
->name
);
310 if (box_ports
.empty())
313 for (auto &it
: bit_users
)
314 if (bit_drivers
.count(it
.first
))
315 for (auto driver_cell
: bit_drivers
.at(it
.first
))
316 for (auto user_cell
: it
.second
)
317 toposort
.edge(driver_cell
, user_cell
);
320 toposort
.analyze_loops
= true;
322 bool no_loops
YS_ATTRIBUTE(unused
) = toposort
.sort();
326 for (auto &it
: toposort
.loops
) {
327 log(" loop %d\n", i
++);
328 for (auto cell_name
: it
) {
329 auto cell
= module
->cell(cell_name
);
331 log("\t%s (%s @ %s)\n", log_id(cell
), log_id(cell
->type
), cell
->get_src_attribute().c_str());
336 log_assert(no_loops
);
338 RTLIL::Module
*holes_module
= design
->addModule(stringf("%s$holes", module
->name
.c_str()));
339 log_assert(holes_module
);
340 holes_module
->set_bool_attribute("\\abc9_holes");
342 dict
<IdString
, Cell
*> cell_cache
;
344 int port_id
= 1, box_count
= 0;
345 for (auto cell_name
: toposort
.sorted
) {
346 RTLIL::Cell
*cell
= module
->cell(cell_name
);
349 RTLIL::Module
* box_module
= design
->module(cell
->type
);
350 if (!box_module
|| (!box_module
->get_bool_attribute("\\abc9_box") && !box_module
->get_bool_attribute("\\abc9_flop")))
353 cell
->attributes
["\\abc9_box_seq"] = box_count
++;
355 IdString derived_type
= box_module
->derive(design
, cell
->parameters
);
356 box_module
= design
->module(derived_type
);
358 auto r
= cell_cache
.insert(derived_type
);
359 auto &holes_cell
= r
.first
->second
;
361 if (box_module
->has_processes())
362 Pass::call_on_module(design
, box_module
, "proc");
364 if (box_module
->get_bool_attribute("\\whitebox")) {
365 holes_cell
= holes_module
->addCell(cell
->name
, derived_type
);
367 if (box_module
->has_processes())
368 Pass::call_on_module(design
, box_module
, "proc");
371 for (auto port_name
: box_ports
.at(cell
->type
)) {
372 RTLIL::Wire
*w
= box_module
->wire(port_name
);
374 log_assert(!w
->port_input
|| !w
->port_output
);
375 auto &conn
= holes_cell
->connections_
[port_name
];
377 for (int i
= 0; i
< GetSize(w
); i
++) {
379 RTLIL::Wire
*holes_wire
= holes_module
->wire(stringf("\\i%d", box_inputs
));
381 holes_wire
= holes_module
->addWire(stringf("\\i%d", box_inputs
));
382 holes_wire
->port_input
= true;
383 holes_wire
->port_id
= port_id
++;
384 holes_module
->ports
.push_back(holes_wire
->name
);
386 conn
.append(holes_wire
);
389 else if (w
->port_output
)
390 conn
= holes_module
->addWire(stringf("%s.%s", derived_type
.c_str(), log_id(port_name
)), GetSize(w
));
393 // For flops only, create an extra 1-bit input that drives a new wire
394 // called "<cell>.abc9_ff.Q" that is used below
395 if (box_module
->get_bool_attribute("\\abc9_flop")) {
397 Wire
*holes_wire
= holes_module
->wire(stringf("\\i%d", box_inputs
));
399 holes_wire
= holes_module
->addWire(stringf("\\i%d", box_inputs
));
400 holes_wire
->port_input
= true;
401 holes_wire
->port_id
= port_id
++;
402 holes_module
->ports
.push_back(holes_wire
->name
);
404 Wire
*Q
= holes_module
->addWire(stringf("%s.abc9_ff.Q", cell
->name
.c_str()));
405 holes_module
->connect(Q
, holes_wire
);
408 else // box_module is a blackbox
409 log_assert(holes_cell
== nullptr);
412 for (auto port_name
: box_ports
.at(cell
->type
)) {
413 RTLIL::Wire
*w
= box_module
->wire(port_name
);
417 Wire
*holes_wire
= holes_module
->addWire(stringf("$abc%s.%s", cell
->name
.c_str(), log_id(port_name
)), GetSize(w
));
418 holes_wire
->port_output
= true;
419 holes_wire
->port_id
= port_id
++;
420 holes_module
->ports
.push_back(holes_wire
->name
);
421 if (holes_cell
) // whitebox
422 holes_module
->connect(holes_wire
, holes_cell
->getPort(port_name
));
424 holes_module
->connect(holes_wire
, Const(State::S0
, GetSize(w
)));
429 void prep_delays(RTLIL::Design
*design
)
432 std::vector
<Cell
*> cells
;
433 dict
<IdString
,dict
<IdString
,std::vector
<int>>> requireds_cache
;
434 for (auto module
: design
->selected_modules()) {
435 if (module
->processes
.size() > 0) {
436 log("Skipping module %s as it contains processes.\n", log_id(module
));
441 for (auto cell
: module
->cells()) {
442 if (cell
->type
.in(ID($_AND_
), ID($_NOT_
), ID($__ABC9_FF_
), ID($__ABC9_DELAY
)))
445 RTLIL::Module
* inst_module
= module
->design
->module(cell
->type
);
448 if (!inst_module
->get_blackbox_attribute())
450 if (inst_module
->get_bool_attribute(ID(abc9_flop
))) {
451 IdString derived_type
= inst_module
->derive(design
, cell
->parameters
);
452 inst_module
= design
->module(derived_type
);
453 log_assert(inst_module
);
454 flops
.insert(inst_module
);
455 continue; // because all flop required times
456 // will be captured in the flop box
458 if (inst_module
->attributes
.count(ID(abc9_box
)))
460 cells
.emplace_back(cell
);
463 for (auto cell
: cells
) {
464 RTLIL::Module
* inst_module
= module
->design
->module(cell
->type
);
465 log_assert(inst_module
);
466 auto &cell_requireds
= requireds_cache
[cell
->type
];
467 for (auto &conn
: cell
->connections_
) {
468 auto port_wire
= inst_module
->wire(conn
.first
);
469 if (!port_wire
->port_input
)
472 auto r
= cell_requireds
.insert(conn
.first
);
473 auto &requireds
= r
.first
->second
;
475 auto it
= port_wire
->attributes
.find("\\abc9_required");
476 if (it
== port_wire
->attributes
.end())
478 if (it
->second
.flags
== 0) {
479 int delay
= it
->second
.as_int();
480 requireds
.emplace_back(delay
);
483 for (const auto &tok
: split_tokens(it
->second
.decode_string())) {
484 int delay
= atoi(tok
.c_str());
485 requireds
.push_back(delay
);
489 if (requireds
.empty())
492 SigSpec O
= module
->addWire(NEW_ID
, GetSize(conn
.second
));
493 auto it
= requireds
.begin();
494 for (int i
= 0; i
< GetSize(conn
.second
); ++i
) {
497 static std::set
<std::pair
<IdString
,IdString
>> seen
;
498 if (seen
.emplace(cell
->type
, conn
.first
).second
) log("%s.%s abc9_required = %d\n", log_id(cell
->type
), log_id(conn
.first
), requireds
[i
]);
501 auto box
= module
->addCell(NEW_ID
, ID($__ABC9_DELAY
));
502 box
->setPort(ID(I
), conn
.second
[i
]);
503 box
->setPort(ID(O
), O
[i
]);
504 box
->setParam(ID(DELAY
), *it
);
505 if (requireds
.size() > 1)
507 conn
.second
[i
] = O
[i
];
513 int abc9_box_id
= design
->scratchpad_get_int("abc9_ops.box_id");
514 std::stringstream ss
;
515 for (auto flop_module
: flops
) {
516 int num_inputs
= 0, num_outputs
= 0;
517 for (auto port_name
: flop_module
->ports
) {
518 auto wire
= flop_module
->wire(port_name
);
519 log_assert(GetSize(wire
) == 1);
520 if (wire
->port_input
) num_inputs
++;
521 if (wire
->port_output
) num_outputs
++;
523 log_assert(num_outputs
== 1);
525 auto r
= flop_module
->attributes
.insert(ID(abc9_box_id
));
527 r
.first
->second
= ++abc9_box_id
;
529 ss
<< log_id(flop_module
) << " " << r
.first
->second
.as_int();
530 ss
<< " " << (flop_module
->get_bool_attribute(ID::whitebox
) ? "1" : "0");
531 ss
<< " " << num_inputs
+1 << " " << num_outputs
<< std::endl
;
535 for (auto port_name
: flop_module
->ports
) {
536 auto wire
= flop_module
->wire(port_name
);
537 if (!wire
->port_input
)
545 ss
<< " abc9_ff.Q" << std::endl
;
548 for (auto port_name
: flop_module
->ports
) {
549 auto wire
= flop_module
->wire(port_name
);
550 if (!wire
->port_input
)
556 ss
<< wire
->attributes
.at("\\abc9_required", 0).as_int();
558 // Last input is 'abc9_ff.Q'
559 ss
<< " 0" << std::endl
<< std::endl
;
561 design
->scratchpad_set_string("abc9_ops.box_library.flops", ss
.str());
562 design
->scratchpad_set_int("abc9_ops.box_id", abc9_box_id
);
565 void prep_lut(RTLIL::Design
*design
, int maxlut
)
567 std::stringstream ss
;
568 std::vector
<std::pair
<int, std::string
>> table
;
569 for (auto module
: design
->modules()) {
570 auto it
= module
->attributes
.find(ID(abc9_lut
));
571 if (it
== module
->attributes
.end())
574 std::vector
<int> specify
;
575 for (auto cell
: module
->cells()) {
576 if (cell
->type
!= ID($specify2
))
578 log_assert(cell
->getParam(ID(SRC_WIDTH
)) == 1);
579 log_assert(cell
->getParam(ID(DST_WIDTH
)) == 1);
580 SigBit s
= cell
->getPort(ID(SRC
));
581 SigBit d
= cell
->getPort(ID(DST
));
582 log_assert(s
.wire
->port_input
);
583 log_assert(d
.wire
->port_output
);
588 int rise_max
= cell
->getParam(ID(T_RISE_MAX
)).as_int();
589 int fall_max
= cell
->getParam(ID(T_FALL_MAX
)).as_int();
590 specify
.push_back(std::max(rise_max
,fall_max
));
592 if (maxlut
&& GetSize(specify
) > maxlut
)
594 std::sort(specify
.begin(), specify
.end());
596 ss
<< "# " << module
->name
.str() << std::endl
;
597 ss
<< GetSize(specify
) << " " << it
->second
.as_int();
598 for (auto i
: specify
)
601 table
.emplace_back(GetSize(specify
), ss
.str());
603 // ABC expects ascending size
604 std::sort(table
.begin(), table
.end());
606 for (auto &i
: table
)
608 design
->scratchpad_set_string("abc9_ops.lut_library", ss
.str());
611 void write_lut(RTLIL::Module
*module
, const std::string
&dst
) {
612 std::ofstream
ofs(dst
);
613 log_assert(ofs
.is_open());
614 ofs
<< module
->design
->scratchpad_get_string("abc9_ops.lut_library");
618 void prep_box(RTLIL::Design
*design
)
620 std::stringstream ss
;
621 ss
<< design
->scratchpad_get_string("abc9_ops.box_library.flops", ss
.str());
623 int abc9_box_id
= design
->scratchpad_get_int("abc9_ops.box_id");
624 for (auto module
: design
->modules()) {
625 auto it
= module
->attributes
.find(ID(abc9_box
));
626 if (it
== module
->attributes
.end())
628 module
->attributes
.erase(it
);
629 log_assert(!module
->attributes
.count(ID(abc9_box_id
)));
631 dict
<std::pair
<SigBit
,SigBit
>, std::string
> table
;
632 std::vector
<SigBit
> inputs
;
633 std::vector
<SigBit
> outputs
;
634 for (auto port_name
: module
->ports
) {
635 auto wire
= module
->wire(port_name
);
636 if (wire
->port_input
)
637 for (int i
= 0; i
< GetSize(wire
); i
++)
638 inputs
.emplace_back(wire
, i
);
639 if (wire
->port_output
)
640 for (int i
= 0; i
< GetSize(wire
); i
++)
641 outputs
.emplace_back(wire
, i
);
643 for (auto cell
: module
->cells()) {
644 if (cell
->type
!= ID($specify2
))
646 auto src
= cell
->getPort(ID(SRC
));
647 auto dst
= cell
->getPort(ID(DST
));
648 for (const auto &c
: src
.chunks())
649 if (!c
.wire
->port_input
)
650 log_error("Module '%s' contains specify cell '%s' where SRC '%s' is not a module input.\n", log_id(module
), log_id(cell
), log_signal(src
));
651 for (const auto &c
: dst
.chunks())
652 if (!c
.wire
->port_output
)
653 log_error("Module '%s' contains specify cell '%s' where DST '%s' is not a module output.\n", log_id(module
), log_id(cell
), log_signal(dst
));
654 int rise_max
= cell
->getParam(ID(T_RISE_MAX
)).as_int();
655 int fall_max
= cell
->getParam(ID(T_FALL_MAX
)).as_int();
656 int max
= std::max(rise_max
,fall_max
);
659 auto r
= table
.insert(std::make_pair(s
,d
));
660 log_assert(r
.second
);
661 r
.first
->second
= std::to_string(max
);
664 auto r
= module
->attributes
.insert(ID(abc9_box_id
));
665 log_assert(r
.second
);
666 r
.first
->second
= ++abc9_box_id
;
667 ss
<< log_id(module
) << " " << abc9_box_id
;
668 ss
<< " " << (module
->get_bool_attribute(ID::whitebox
) ? "1" : "0");
669 ss
<< " " << GetSize(inputs
) << " " << GetSize(outputs
) << std::endl
;
672 for (const auto &i
: inputs
) {
677 if (GetSize(i
.wire
) == 1)
678 ss
<< log_id(i
.wire
);
680 ss
<< log_id(i
.wire
) << "[" << i
.offset
<< "]";
683 for (const auto &o
: outputs
) {
685 for (const auto &i
: inputs
) {
690 ss
<< table
.at(std::make_pair(i
,o
), "-");
693 if (GetSize(o
.wire
) == 1)
694 ss
<< log_id(o
.wire
);
696 ss
<< log_id(o
.wire
) << "[" << o
.offset
<< "]";
703 design
->scratchpad_set_string("abc9_ops.box_library", ss
.str());
704 design
->scratchpad_set_int("abc9_ops.box_id", abc9_box_id
);
707 void write_box(RTLIL::Module
*module
, const std::string
&dst
) {
708 std::ofstream
ofs(dst
);
709 log_assert(ofs
.is_open());
710 ofs
<< module
->design
->scratchpad_get_string("abc9_ops.box_library");
711 // ABC expects at least one box
712 if (ofs
.tellp() == 0)
713 ofs
<< "(dummy) 1 0 0 0";
717 void reintegrate(RTLIL::Module
*module
)
719 auto design
= module
->design
;
722 map_autoidx
= autoidx
++;
724 RTLIL::Module
*mapped_mod
= design
->module(stringf("%s$abc9", module
->name
.c_str()));
725 if (mapped_mod
== NULL
)
726 log_error("ABC output file does not contain a module `%s$abc'.\n", log_id(module
));
728 for (auto w
: mapped_mod
->wires())
729 module
->addWire(remap_name(w
->name
), GetSize(w
));
731 dict
<IdString
,std::vector
<IdString
>> box_ports
;
733 for (auto m
: design
->modules()) {
734 if (!m
->attributes
.count(ID(abc9_box_id
)))
737 auto r
= box_ports
.insert(m
->name
);
741 // Make carry in the last PI, and carry out the last PO
742 // since ABC requires it this way
743 IdString carry_in
, carry_out
;
744 for (const auto &port_name
: m
->ports
) {
745 auto w
= m
->wire(port_name
);
747 if (w
->get_bool_attribute("\\abc9_carry")) {
748 log_assert(w
->port_input
!= w
->port_output
);
750 carry_in
= port_name
;
751 else if (w
->port_output
)
752 carry_out
= port_name
;
755 r
.first
->second
.push_back(port_name
);
758 if (carry_in
!= IdString()) {
759 r
.first
->second
.push_back(carry_in
);
760 r
.first
->second
.push_back(carry_out
);
764 std::vector
<Cell
*> boxes
;
765 for (auto cell
: module
->cells().to_vector()) {
766 if (cell
->has_keep_attr())
768 if (cell
->type
.in(ID($_AND_
), ID($_NOT_
), ID($__ABC9_FF_
)))
769 module
->remove(cell
);
770 else if (cell
->attributes
.erase("\\abc9_box_seq"))
771 boxes
.emplace_back(cell
);
774 dict
<SigBit
, pool
<IdString
>> bit_drivers
, bit_users
;
775 TopoSort
<IdString
, RTLIL::sort_by_id_str
> toposort
;
776 dict
<RTLIL::Cell
*,RTLIL::Cell
*> not2drivers
;
777 dict
<SigBit
, std::vector
<RTLIL::Cell
*>> bit2sinks
;
779 std::map
<IdString
, int> cell_stats
;
780 for (auto mapped_cell
: mapped_mod
->cells())
782 // TODO: Speed up toposort -- we care about NOT ordering only
783 toposort
.node(mapped_cell
->name
);
785 if (mapped_cell
->type
== ID($_NOT_
)) {
786 RTLIL::SigBit a_bit
= mapped_cell
->getPort(ID::A
);
787 RTLIL::SigBit y_bit
= mapped_cell
->getPort(ID::Y
);
788 bit_users
[a_bit
].insert(mapped_cell
->name
);
789 // Ignore inouts for topo ordering
790 if (y_bit
.wire
&& !(y_bit
.wire
->port_input
&& y_bit
.wire
->port_output
))
791 bit_drivers
[y_bit
].insert(mapped_cell
->name
);
794 mapped_cell
->setPort(ID::Y
, module
->addWire(NEW_ID
));
795 RTLIL::Wire
*wire
= module
->wire(remap_name(y_bit
.wire
->name
));
797 module
->connect(RTLIL::SigBit(wire
, y_bit
.offset
), State::S1
);
800 RTLIL::Cell
* driver_lut
= nullptr;
801 // ABC can return NOT gates that drive POs
802 if (!a_bit
.wire
->port_input
) {
803 // If it's not a NOT gate that that comes from a PI directly,
804 // find the driver LUT and clone that to guarantee that we won't
805 // increase the max logic depth
806 // (TODO: Optimise by not cloning unless will increase depth)
807 RTLIL::IdString driver_name
;
808 if (GetSize(a_bit
.wire
) == 1)
809 driver_name
= stringf("$lut%s", a_bit
.wire
->name
.c_str());
811 driver_name
= stringf("$lut%s[%d]", a_bit
.wire
->name
.c_str(), a_bit
.offset
);
812 driver_lut
= mapped_mod
->cell(driver_name
);
816 // If a driver couldn't be found (could be from PI or box CI)
817 // then implement using a LUT
818 RTLIL::Cell
*cell
= module
->addLut(remap_name(stringf("$lut%s", mapped_cell
->name
.c_str())),
819 RTLIL::SigBit(module
->wires_
.at(remap_name(a_bit
.wire
->name
)), a_bit
.offset
),
820 RTLIL::SigBit(module
->wires_
.at(remap_name(y_bit
.wire
->name
)), y_bit
.offset
),
821 RTLIL::Const::from_string("01"));
822 bit2sinks
[cell
->getPort(ID::A
)].push_back(cell
);
823 cell_stats
[ID($lut
)]++;
826 not2drivers
[mapped_cell
] = driver_lut
;
831 if (mapped_cell
->type
.in(ID($lut
), ID($__ABC9_FF_
))) {
832 RTLIL::Cell
*cell
= module
->addCell(remap_name(mapped_cell
->name
), mapped_cell
->type
);
833 cell
->parameters
= mapped_cell
->parameters
;
834 cell
->attributes
= mapped_cell
->attributes
;
836 for (auto &mapped_conn
: mapped_cell
->connections()) {
837 RTLIL::SigSpec newsig
;
838 for (auto c
: mapped_conn
.second
.chunks()) {
841 //log_assert(c.width == 1);
843 c
.wire
= module
->wires_
.at(remap_name(c
.wire
->name
));
846 cell
->setPort(mapped_conn
.first
, newsig
);
848 if (cell
->input(mapped_conn
.first
)) {
849 for (auto i
: newsig
)
850 bit2sinks
[i
].push_back(cell
);
851 for (auto i
: mapped_conn
.second
)
852 bit_users
[i
].insert(mapped_cell
->name
);
854 if (cell
->output(mapped_conn
.first
))
855 for (auto i
: mapped_conn
.second
)
856 // Ignore inouts for topo ordering
857 if (i
.wire
&& !(i
.wire
->port_input
&& i
.wire
->port_output
))
858 bit_drivers
[i
].insert(mapped_cell
->name
);
862 RTLIL::Cell
*existing_cell
= module
->cell(mapped_cell
->name
);
864 log_error("Cannot find existing box cell with name '%s' in original design.\n", log_id(mapped_cell
));
866 if (existing_cell
->type
== ID($__ABC9_DELAY
)) {
867 SigBit I
= mapped_cell
->getPort(ID(i
));
868 SigBit O
= mapped_cell
->getPort(ID(o
));
870 I
.wire
= module
->wires_
.at(remap_name(I
.wire
->name
));
872 O
.wire
= module
->wires_
.at(remap_name(O
.wire
->name
));
873 module
->connect(O
, I
);
877 RTLIL::Module
* box_module
= design
->module(existing_cell
->type
);
878 IdString derived_type
= box_module
->derive(design
, existing_cell
->parameters
);
879 RTLIL::Module
* derived_module
= design
->module(derived_type
);
880 log_assert(derived_module
);
881 log_assert(mapped_cell
->type
== stringf("$__boxid%d", derived_module
->attributes
.at("\\abc9_box_id").as_int()));
882 mapped_cell
->type
= existing_cell
->type
;
884 RTLIL::Cell
*cell
= module
->addCell(remap_name(mapped_cell
->name
), mapped_cell
->type
);
885 cell
->parameters
= existing_cell
->parameters
;
886 cell
->attributes
= existing_cell
->attributes
;
887 module
->swap_names(cell
, existing_cell
);
889 auto jt
= mapped_cell
->connections_
.find("\\i");
890 log_assert(jt
!= mapped_cell
->connections_
.end());
891 SigSpec inputs
= std::move(jt
->second
);
892 mapped_cell
->connections_
.erase(jt
);
893 jt
= mapped_cell
->connections_
.find("\\o");
894 log_assert(jt
!= mapped_cell
->connections_
.end());
895 SigSpec outputs
= std::move(jt
->second
);
896 mapped_cell
->connections_
.erase(jt
);
898 auto abc9_flop
= box_module
->attributes
.count("\\abc9_flop");
900 for (const auto &i
: inputs
)
901 bit_users
[i
].insert(mapped_cell
->name
);
902 for (const auto &i
: outputs
)
903 // Ignore inouts for topo ordering
904 if (i
.wire
&& !(i
.wire
->port_input
&& i
.wire
->port_output
))
905 bit_drivers
[i
].insert(mapped_cell
->name
);
908 int input_count
= 0, output_count
= 0;
909 for (const auto &port_name
: box_ports
.at(derived_type
)) {
910 RTLIL::Wire
*w
= box_module
->wire(port_name
);
915 sig
= inputs
.extract(input_count
, GetSize(w
));
916 input_count
+= GetSize(w
);
918 if (w
->port_output
) {
919 sig
= outputs
.extract(output_count
, GetSize(w
));
920 output_count
+= GetSize(w
);
924 for (auto c
: sig
.chunks()) {
927 //log_assert(c.width == 1);
929 c
.wire
= module
->wires_
.at(remap_name(c
.wire
->name
));
932 cell
->setPort(port_name
, newsig
);
934 if (w
->port_input
&& !abc9_flop
)
935 for (const auto &i
: newsig
)
936 bit2sinks
[i
].push_back(cell
);
940 cell_stats
[mapped_cell
->type
]++;
943 for (auto cell
: boxes
)
944 module
->remove(cell
);
946 // Copy connections (and rename) from mapped_mod to module
947 for (auto conn
: mapped_mod
->connections()) {
948 if (!conn
.first
.is_fully_const()) {
949 auto chunks
= conn
.first
.chunks();
950 for (auto &c
: chunks
)
951 c
.wire
= module
->wires_
.at(remap_name(c
.wire
->name
));
952 conn
.first
= std::move(chunks
);
954 if (!conn
.second
.is_fully_const()) {
955 auto chunks
= conn
.second
.chunks();
956 for (auto &c
: chunks
)
958 c
.wire
= module
->wires_
.at(remap_name(c
.wire
->name
));
959 conn
.second
= std::move(chunks
);
961 module
->connect(conn
);
964 for (auto &it
: cell_stats
)
965 log("ABC RESULTS: %15s cells: %8d\n", it
.first
.c_str(), it
.second
);
966 int in_wires
= 0, out_wires
= 0;
968 // Stitch in mapped_mod's inputs/outputs into module
969 for (auto port
: mapped_mod
->ports
) {
970 RTLIL::Wire
*mapped_wire
= mapped_mod
->wire(port
);
971 RTLIL::Wire
*wire
= module
->wire(port
);
973 if (wire
->attributes
.erase(ID(abc9_scc_id
))) {
974 auto r
YS_ATTRIBUTE(unused
) = wire
->attributes
.erase(ID::keep
);
977 RTLIL::Wire
*remap_wire
= module
->wire(remap_name(port
));
978 RTLIL::SigSpec
signal(wire
, 0, GetSize(remap_wire
));
979 log_assert(GetSize(signal
) >= GetSize(remap_wire
));
982 if (mapped_wire
->port_output
) {
984 conn
.second
= remap_wire
;
986 module
->connect(conn
);
988 else if (mapped_wire
->port_input
) {
989 conn
.first
= remap_wire
;
990 conn
.second
= signal
;
992 module
->connect(conn
);
996 // ABC9 will return $_NOT_ gates in its mapping (since they are
997 // treated as being "free"), in particular driving primary
998 // outputs (real primary outputs, or cells treated as blackboxes)
999 // or driving box inputs.
1000 // Instead of just mapping those $_NOT_ gates into 2-input $lut-s
1001 // at an area and delay cost, see if it is possible to push
1002 // this $_NOT_ into the driving LUT, or into all sink LUTs.
1003 // When this is not possible, (i.e. this signal drives two primary
1004 // outputs, only one of which is complemented) and when the driver
1005 // is a LUT, then clone the LUT so that it can be inverted without
1006 // increasing depth/delay.
1007 for (auto &it
: bit_users
)
1008 if (bit_drivers
.count(it
.first
))
1009 for (auto driver_cell
: bit_drivers
.at(it
.first
))
1010 for (auto user_cell
: it
.second
)
1011 toposort
.edge(driver_cell
, user_cell
);
1012 bool no_loops
YS_ATTRIBUTE(unused
) = toposort
.sort();
1013 log_assert(no_loops
);
1015 for (auto ii
= toposort
.sorted
.rbegin(); ii
!= toposort
.sorted
.rend(); ii
++) {
1016 RTLIL::Cell
*not_cell
= mapped_mod
->cell(*ii
);
1017 log_assert(not_cell
);
1018 if (not_cell
->type
!= ID($_NOT_
))
1020 auto it
= not2drivers
.find(not_cell
);
1021 if (it
== not2drivers
.end())
1023 RTLIL::Cell
*driver_lut
= it
->second
;
1024 RTLIL::SigBit a_bit
= not_cell
->getPort(ID::A
);
1025 RTLIL::SigBit y_bit
= not_cell
->getPort(ID::Y
);
1026 RTLIL::Const driver_mask
;
1028 a_bit
.wire
= module
->wires_
.at(remap_name(a_bit
.wire
->name
));
1029 y_bit
.wire
= module
->wires_
.at(remap_name(y_bit
.wire
->name
));
1031 auto jt
= bit2sinks
.find(a_bit
);
1032 if (jt
== bit2sinks
.end())
1035 for (auto sink_cell
: jt
->second
)
1036 if (sink_cell
->type
!= ID($lut
))
1039 // Push downstream LUTs past inverter
1040 for (auto sink_cell
: jt
->second
) {
1041 SigSpec A
= sink_cell
->getPort(ID::A
);
1042 RTLIL::Const mask
= sink_cell
->getParam(ID(LUT
));
1044 for (; index
< GetSize(A
); index
++)
1045 if (A
[index
] == a_bit
)
1047 log_assert(index
< GetSize(A
));
1049 while (i
< GetSize(mask
)) {
1050 for (int j
= 0; j
< (1 << index
); j
++)
1051 std::swap(mask
[i
+j
], mask
[i
+j
+(1 << index
)]);
1052 i
+= 1 << (index
+1);
1055 sink_cell
->setPort(ID::A
, A
);
1056 sink_cell
->setParam(ID(LUT
), mask
);
1059 // Since we have rewritten all sinks (which we know
1060 // to be only LUTs) to be after the inverter, we can
1061 // go ahead and clone the LUT with the expectation
1062 // that the original driving LUT will become dangling
1063 // and get cleaned away
1065 driver_mask
= driver_lut
->getParam(ID(LUT
));
1066 for (auto &b
: driver_mask
.bits
) {
1067 if (b
== RTLIL::State::S0
) b
= RTLIL::State::S1
;
1068 else if (b
== RTLIL::State::S1
) b
= RTLIL::State::S0
;
1070 auto cell
= module
->addLut(NEW_ID
,
1071 driver_lut
->getPort(ID::A
),
1074 for (auto &bit
: cell
->connections_
.at(ID::A
)) {
1075 bit
.wire
= module
->wires_
.at(remap_name(bit
.wire
->name
));
1076 bit2sinks
[bit
].push_back(cell
);
1080 //log("ABC RESULTS: internal signals: %8d\n", int(signal_list.size()) - in_wires - out_wires);
1081 log("ABC RESULTS: input signals: %8d\n", in_wires
);
1082 log("ABC RESULTS: output signals: %8d\n", out_wires
);
1084 design
->remove(mapped_mod
);
1087 struct Abc9OpsPass
: public Pass
{
1088 Abc9OpsPass() : Pass("abc9_ops", "helper functions for ABC9") { }
1089 void help() YS_OVERRIDE
1091 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
1093 log(" abc9_ops [options] [selection]\n");
1095 log("This pass contains a set of supporting operations for use during ABC technology\n");
1096 log("mapping, and is expected to be called in conjunction with other operations from\n");
1097 log("the `abc9' script pass. Only fully-selected modules are supported.\n");
1100 log(" check that the design is valid, e.g. (* abc9_box_id *) values are unique,\n");
1101 log(" (* abc9_carry *) is only given for one input/output port, etc.\n");
1103 log(" -prep_delays\n");
1104 log(" insert `$__ABC9_DELAY' blackbox cells into the design to account for\n");
1105 log(" certain delays, e.g. (* abc9_required *) values.\n");
1107 log(" -mark_scc\n");
1108 log(" for an arbitrarily chosen cell in each unique SCC of each selected module\n");
1109 log(" (tagged with an (* abc9_scc_id = <int> *) attribute), temporarily mark all\n");
1110 log(" wires driven by this cell's outputs with a (* keep *) attribute in order\n");
1111 log(" to break the SCC. this temporary attribute will be removed on -reintegrate.\n");
1113 log(" -prep_xaiger\n");
1114 log(" prepare the design for XAIGER output. this includes computing the\n");
1115 log(" topological ordering of ABC9 boxes, as well as preparing the\n");
1116 log(" '<module-name>$holes' module that contains the logic behaviour of ABC9\n");
1117 log(" whiteboxes.\n");
1120 log(" consider flop cells (those instantiating modules marked with (* abc9_flop *)\n");
1121 log(" during -prep_xaiger.\n");
1123 log(" -prep_dff\n");
1124 log(" compute the clock domain and initial value of each flop in the design.\n");
1125 log(" process the '$holes' module to support clock-enable functionality.\n");
1127 log(" -prep_lut <maxlut>\n");
1128 log(" pre-compute the lut library by analysing all modules marked with\n");
1129 log(" (* abc9_lut=<area> *).\n");
1131 log(" -write_lut <dst>\n");
1132 log(" write the pre-computed lut library to <dst>.\n");
1134 log(" -prep_box\n");
1135 log(" pre-compute the box library by analysing all modules marked with\n");
1136 log(" (* abc9_box *)\n");
1138 log(" -write_box <dst>\n");
1139 log(" write the pre-computed box library to <dst>.\n");
1141 log(" -reintegrate\n");
1142 log(" for each selected module, re-intergrate the module '<module-name>$abc9'\n");
1143 log(" by first recovering ABC9 boxes, and then stitching in the remaining primary\n");
1144 log(" inputs and outputs.\n");
1147 void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) YS_OVERRIDE
1149 log_header(design
, "Executing ABC9_OPS pass (helper functions for ABC9).\n");
1151 bool check_mode
= false;
1152 bool prep_delays_mode
= false;
1153 bool mark_scc_mode
= false;
1154 bool prep_dff_mode
= false;
1155 bool prep_xaiger_mode
= false;
1156 bool prep_lut_mode
= false;
1157 bool prep_box_mode
= false;
1158 bool reintegrate_mode
= false;
1159 bool dff_mode
= false;
1160 std::string write_lut_dst
;
1162 std::string write_box_dst
;
1165 for (argidx
= 1; argidx
< args
.size(); argidx
++) {
1166 std::string arg
= args
[argidx
];
1167 if (arg
== "-check") {
1171 if (arg
== "-mark_scc") {
1172 mark_scc_mode
= true;
1175 if (arg
== "-prep_dff") {
1176 prep_dff_mode
= true;
1179 if (arg
== "-prep_xaiger") {
1180 prep_xaiger_mode
= true;
1183 if (arg
== "-prep_delays") {
1184 prep_delays_mode
= true;
1187 if (arg
== "-prep_lut" && argidx
+1 < args
.size()) {
1188 prep_lut_mode
= true;
1189 maxlut
= atoi(args
[++argidx
].c_str());
1192 if (arg
== "-maxlut" && argidx
+1 < args
.size()) {
1195 if (arg
== "-write_lut" && argidx
+1 < args
.size()) {
1196 write_lut_dst
= args
[++argidx
];
1197 rewrite_filename(write_lut_dst
);
1200 if (arg
== "-prep_box") {
1201 prep_box_mode
= true;
1204 if (arg
== "-write_box" && argidx
+1 < args
.size()) {
1205 write_box_dst
= args
[++argidx
];
1206 rewrite_filename(write_box_dst
);
1209 if (arg
== "-reintegrate") {
1210 reintegrate_mode
= true;
1213 if (arg
== "-dff") {
1219 extra_args(args
, argidx
, design
);
1221 if (!(check_mode
|| mark_scc_mode
|| prep_delays_mode
|| prep_xaiger_mode
|| prep_dff_mode
|| prep_lut_mode
|| prep_box_mode
|| !write_lut_dst
.empty() || !write_box_dst
.empty() || reintegrate_mode
))
1222 log_cmd_error("At least one of -check, -mark_scc, -prep_{delays,xaiger,dff,lut,box}, -write_{lut,box}, -reintegrate must be specified.\n");
1224 if (dff_mode
&& !prep_xaiger_mode
)
1225 log_cmd_error("'-dff' option is only relevant for -prep_xaiger.\n");
1229 if (prep_delays_mode
)
1230 prep_delays(design
);
1232 prep_lut(design
, maxlut
);
1236 for (auto mod
: design
->selected_modules()) {
1237 if (mod
->get_bool_attribute("\\abc9_holes"))
1240 if (mod
->processes
.size() > 0) {
1241 log("Skipping module %s as it contains processes.\n", log_id(mod
));
1245 if (!design
->selected_whole_module(mod
))
1246 log_error("Can't handle partially selected module %s!\n", log_id(mod
));
1248 if (!write_lut_dst
.empty())
1249 write_lut(mod
, write_lut_dst
);
1250 if (!write_box_dst
.empty())
1251 write_box(mod
, write_box_dst
);
1256 if (prep_xaiger_mode
)
1257 prep_xaiger(mod
, dff_mode
);
1258 if (reintegrate_mode
)
1264 PRIVATE_NAMESPACE_END