e8a355671751f8b44adf7c23facebe64cc9afec9
2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5 * Copyright (C) 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 // [[CITE]] The AIGER And-Inverter Graph (AIG) Format Version 20071012
22 // Armin Biere. The AIGER And-Inverter Graph (AIG) Format Version 20071012. Technical Report 07/1, October 2011, FMV Reports Series, Institute for Formal Models and Verification, Johannes Kepler University, Altenbergerstr. 69, 4040 Linz, Austria.
23 // http://fmv.jku.at/papers/Biere-FMV-TR-07-1.pdf
31 #include "kernel/yosys.h"
32 #include "kernel/sigtools.h"
33 #include "kernel/consteval.h"
34 #include "aigerparse.h"
38 AigerReader::AigerReader(RTLIL::Design
*design
, std::istream
&f
, RTLIL::IdString module_name
, RTLIL::IdString clk_name
, std::string map_filename
, bool wideports
)
39 : design(design
), f(f
), clk_name(clk_name
), map_filename(map_filename
), wideports(wideports
)
41 module
= new RTLIL::Module
;
42 module
->name
= module_name
;
43 if (design
->module(module
->name
))
44 log_error("Duplicate definition of module %s!\n", log_id(module
->name
));
47 void AigerReader::parse_aiger()
51 if (header
!= "aag" && header
!= "aig")
52 log_error("Unsupported AIGER file!\n");
54 // Parse rest of header
55 if (!(f
>> M
>> I
>> L
>> O
>> A
))
56 log_error("Invalid AIGER header\n");
60 if (f
.peek() != ' ') goto end_of_header
;
61 if (!(f
>> B
)) log_error("Invalid AIGER header\n");
62 if (f
.peek() != ' ') goto end_of_header
;
63 if (!(f
>> C
)) log_error("Invalid AIGER header\n");
64 if (f
.peek() != ' ') goto end_of_header
;
65 if (!(f
>> J
)) log_error("Invalid AIGER header\n");
66 if (f
.peek() != ' ') goto end_of_header
;
67 if (!(f
>> F
)) log_error("Invalid AIGER header\n");
71 std::getline(f
, line
); // Ignore up to start of next line, as standard
72 // says anything that follows could be used for
75 log_debug("M=%u I=%u L=%u O=%u A=%u B=%u C=%u J=%u F=%u\n", M
, I
, L
, O
, A
, B
, C
, J
, F
);
81 else if (header
== "aig")
86 // Parse footer (symbol table, comments, etc.)
89 for (int c
= f
.peek(); c
!= EOF
; c
= f
.peek(), ++line_count
) {
90 if (c
== 'i' || c
== 'l' || c
== 'o') {
93 log_error("Line %u cannot be interpreted as a symbol entry!\n", line_count
);
95 if ((c
== 'i' && l1
> inputs
.size()) || (c
== 'l' && l1
> latches
.size()) || (c
== 'o' && l1
> outputs
.size()))
96 log_error("Line %u has invalid symbol position!\n", line_count
);
99 if (c
== 'i') wire
= inputs
[l1
];
100 else if (c
== 'l') wire
= latches
[l1
];
101 else if (c
== 'o') wire
= outputs
[l1
];
104 module
->rename(wire
, stringf("\\%s", s
.c_str()));
106 else if (c
== 'b' || c
== 'j' || c
== 'f') {
111 if (f
.peek() == '\n')
113 // Else constraint (TODO)
116 log_error("Line %u: cannot interpret first character '%c'!\n", line_count
, c
);
117 std::getline(f
, line
); // Ignore up to start of next line
123 static uint32_t parse_xaiger_literal(std::istream
&f
)
126 f
.read(reinterpret_cast<char*>(&l
), sizeof(l
));
127 if (f
.gcount() != sizeof(l
))
128 log_error("Offset %ld: unable to read literal!\n", static_cast<int64_t>(f
.tellg()));
129 // TODO: Don't assume we're on little endian
131 return _byteswap_ulong(l
);
133 return __builtin_bswap32(l
);
137 static RTLIL::Wire
* createWireIfNotExists(RTLIL::Module
*module
, unsigned literal
)
139 const unsigned variable
= literal
>> 1;
140 const bool invert
= literal
& 1;
141 RTLIL::IdString
wire_name(stringf("\\__%d%s__", variable
, invert
? "b" : "")); // FIXME: is "b" the right suffix?
142 RTLIL::Wire
*wire
= module
->wire(wire_name
);
143 if (wire
) return wire
;
144 log_debug("Creating %s\n", wire_name
.c_str());
145 wire
= module
->addWire(wire_name
);
146 wire
->port_input
= wire
->port_output
= false;
147 if (!invert
) return wire
;
148 RTLIL::IdString
wire_inv_name(stringf("\\__%d__", variable
));
149 RTLIL::Wire
*wire_inv
= module
->wire(wire_inv_name
);
151 if (module
->cell(wire_inv_name
)) return wire
;
154 log_debug("Creating %s\n", wire_inv_name
.c_str());
155 wire_inv
= module
->addWire(wire_inv_name
);
156 wire_inv
->port_input
= wire_inv
->port_output
= false;
159 log_debug("Creating %s = ~%s\n", wire_name
.c_str(), wire_inv_name
.c_str());
160 module
->addNotGate(stringf("\\__%d__$not", variable
), wire_inv
, wire
); // FIXME: is "$not" the right suffix?
165 void AigerReader::parse_xaiger()
169 if (header
!= "aag" && header
!= "aig")
170 log_error("Unsupported AIGER file!\n");
172 // Parse rest of header
173 if (!(f
>> M
>> I
>> L
>> O
>> A
))
174 log_error("Invalid AIGER header\n");
180 std::getline(f
, line
); // Ignore up to start of next line, as standard
181 // says anything that follows could be used for
184 log_debug("M=%u I=%u L=%u O=%u A=%u\n", M
, I
, L
, O
, A
);
190 else if (header
== "aig")
191 parse_aiger_binary();
195 dict
<int,IdString
> box_lookup
;
196 for (auto m
: design
->modules()) {
197 auto it
= m
->attributes
.find("\\abc_box_id");
198 if (it
== m
->attributes
.end())
200 box_lookup
[it
->second
.as_int()] = m
->name
;
203 // Parse footer (symbol table, comments, etc.)
205 bool comment_seen
= false;
206 for (int c
= f
.peek(); c
!= EOF
; c
= f
.peek()) {
207 if (comment_seen
|| c
== 'c') {
218 uint32_t dataSize
= parse_xaiger_literal(f
);
219 uint32_t lutNum
= parse_xaiger_literal(f
);
220 uint32_t lutSize
= parse_xaiger_literal(f
);
221 log_debug("m: dataSize=%u lutNum=%u lutSize=%u\n", dataSize
, lutNum
, lutSize
);
222 ConstEval
ce(module
);
223 for (unsigned i
= 0; i
< lutNum
; ++i
) {
224 uint32_t rootNodeID
= parse_xaiger_literal(f
);
225 uint32_t cutLeavesM
= parse_xaiger_literal(f
);
226 log_debug("rootNodeID=%d cutLeavesM=%d\n", rootNodeID
, cutLeavesM
);
227 RTLIL::Wire
*output_sig
= module
->wire(stringf("\\__%d__", rootNodeID
));
229 RTLIL::SigSpec input_sig
;
230 for (unsigned j
= 0; j
< cutLeavesM
; ++j
) {
231 nodeID
= parse_xaiger_literal(f
);
232 log_debug("\t%u\n", nodeID
);
233 RTLIL::Wire
*wire
= module
->wire(stringf("\\__%d__", nodeID
));
235 input_sig
.append(wire
);
237 RTLIL::Const
lut_mask(RTLIL::State::Sx
, 1 << input_sig
.size());
238 for (int j
= 0; j
< (1 << cutLeavesM
); ++j
) {
240 ce
.set(input_sig
, RTLIL::Const
{j
, static_cast<int>(cutLeavesM
)});
241 RTLIL::SigSpec
o(output_sig
);
243 lut_mask
[j
] = o
.as_const()[0];
246 RTLIL::Cell
*output_cell
= module
->cell(stringf("\\__%d__$and", rootNodeID
));
247 log_assert(output_cell
);
248 module
->remove(output_cell
);
249 module
->addLut(stringf("\\__%d__$lut", rootNodeID
), input_sig
, output_sig
, std::move(lut_mask
));
253 parse_xaiger_literal(f
);
255 log_debug("n: '%s'\n", s
.c_str());
258 f
.ignore(sizeof(uint32_t));
259 uint32_t version
= parse_xaiger_literal(f
);
260 log_assert(version
== 1);
261 f
.ignore(4*sizeof(uint32_t));
262 uint32_t boxNum
= parse_xaiger_literal(f
);
263 for (unsigned i
= 0; i
< boxNum
; i
++) {
264 f
.ignore(2*sizeof(uint32_t));
265 uint32_t boxUniqueId
= parse_xaiger_literal(f
);
266 log_assert(boxUniqueId
> 0);
267 uint32_t oldBoxNum
= parse_xaiger_literal(f
);
268 module
->addCell(stringf("$__box%u__", oldBoxNum
), box_lookup
.at(boxUniqueId
));
271 else if (c
== 'a' || c
== 'i' || c
== 'o') {
272 uint32_t dataSize
= parse_xaiger_literal(f
);
280 log_error("Line %u: cannot interpret first character '%c'!\n", line_count
, c
);
286 void AigerReader::parse_aiger_ascii()
289 std::stringstream ss
;
294 for (unsigned i
= 0; i
< I
; ++i
, ++line_count
) {
296 log_error("Line %u cannot be interpreted as an input!\n", line_count
);
297 log_debug("%d is an input\n", l1
);
298 log_assert(!(l1
& 1)); // Inputs can't be inverted
299 RTLIL::Wire
*wire
= createWireIfNotExists(module
, l1
);
300 wire
->port_input
= true;
301 inputs
.push_back(wire
);
305 RTLIL::Wire
*clk_wire
= nullptr;
307 log_assert(clk_name
!= "");
308 clk_wire
= module
->wire(clk_name
);
309 log_assert(!clk_wire
);
310 log_debug("Creating %s\n", clk_name
.c_str());
311 clk_wire
= module
->addWire(clk_name
);
312 clk_wire
->port_input
= true;
313 clk_wire
->port_output
= false;
315 for (unsigned i
= 0; i
< L
; ++i
, ++line_count
) {
316 if (!(f
>> l1
>> l2
))
317 log_error("Line %u cannot be interpreted as a latch!\n", line_count
);
318 log_debug("%d %d is a latch\n", l1
, l2
);
319 log_assert(!(l1
& 1)); // TODO: Latch outputs can't be inverted?
320 RTLIL::Wire
*q_wire
= createWireIfNotExists(module
, l1
);
321 RTLIL::Wire
*d_wire
= createWireIfNotExists(module
, l2
);
323 module
->addDffGate(NEW_ID
, clk_wire
, d_wire
, q_wire
);
325 // Reset logic is optional in AIGER 1.9
326 if (f
.peek() == ' ') {
328 log_error("Line %u cannot be interpreted as a latch!\n", line_count
);
330 if (l3
== 0 || l3
== 1)
331 q_wire
->attributes
["\\init"] = RTLIL::Const(l3
);
333 //q_wire->attributes["\\init"] = RTLIL::Const(RTLIL::State::Sx);
336 log_error("Line %u has invalid reset literal for latch!\n", line_count
);
339 // AIGER latches are assumed to be initialized to zero
340 q_wire
->attributes
["\\init"] = RTLIL::Const(0);
342 latches
.push_back(q_wire
);
346 for (unsigned i
= 0; i
< O
; ++i
, ++line_count
) {
348 log_error("Line %u cannot be interpreted as an output!\n", line_count
);
351 if (l1
== 0 || l1
== 1) {
352 wire
= module
->addWire(NEW_ID
);
354 module
->connect(wire
, RTLIL::State::S0
);
356 module
->connect(wire
, RTLIL::State::S1
);
361 log_debug("%d is an output\n", l1
);
362 const unsigned variable
= l1
>> 1;
363 const bool invert
= l1
& 1;
364 RTLIL::IdString
wire_name(stringf("\\__%d%s__", variable
, invert
? "b" : "")); // FIXME: is "b" the right suffix?
365 wire
= module
->wire(wire_name
);
367 wire
= createWireIfNotExists(module
, l1
);
369 if (wire
->port_input
|| wire
->port_output
) {
370 RTLIL::Wire
*new_wire
= module
->addWire(NEW_ID
);
371 module
->connect(new_wire
, wire
);
376 wire
->port_output
= true;
377 outputs
.push_back(wire
);
379 std::getline(f
, line
); // Ignore up to start of next line
381 // TODO: Parse bad state properties
382 for (unsigned i
= 0; i
< B
; ++i
, ++line_count
)
383 std::getline(f
, line
); // Ignore up to start of next line
385 // TODO: Parse invariant constraints
386 for (unsigned i
= 0; i
< C
; ++i
, ++line_count
)
387 std::getline(f
, line
); // Ignore up to start of next line
389 // TODO: Parse justice properties
390 for (unsigned i
= 0; i
< J
; ++i
, ++line_count
)
391 std::getline(f
, line
); // Ignore up to start of next line
393 // TODO: Parse fairness constraints
394 for (unsigned i
= 0; i
< F
; ++i
, ++line_count
)
395 std::getline(f
, line
); // Ignore up to start of next line
398 for (unsigned i
= 0; i
< A
; ++i
) {
399 if (!(f
>> l1
>> l2
>> l3
))
400 log_error("Line %u cannot be interpreted as an AND!\n", line_count
);
402 log_debug("%d %d %d is an AND\n", l1
, l2
, l3
);
403 log_assert(!(l1
& 1));
404 RTLIL::Wire
*o_wire
= createWireIfNotExists(module
, l1
);
405 RTLIL::Wire
*i1_wire
= createWireIfNotExists(module
, l2
);
406 RTLIL::Wire
*i2_wire
= createWireIfNotExists(module
, l3
);
407 module
->addAndGate(o_wire
->name
.str() + "$and", i1_wire
, i2_wire
, o_wire
);
409 std::getline(f
, line
); // Ignore up to start of next line
412 static unsigned parse_next_delta_literal(std::istream
&f
, unsigned ref
)
414 unsigned x
= 0, i
= 0;
416 while ((ch
= f
.get()) & 0x80)
417 x
|= (ch
& 0x7f) << (7 * i
++);
418 return ref
- (x
| (ch
<< (7 * i
)));
421 void AigerReader::parse_aiger_binary()
427 for (unsigned i
= 1; i
<= I
; ++i
) {
428 log_debug("%d is an input\n", i
);
429 RTLIL::Wire
*wire
= createWireIfNotExists(module
, i
<< 1);
430 wire
->port_input
= true;
431 log_assert(!wire
->port_output
);
432 inputs
.push_back(wire
);
436 RTLIL::Wire
*clk_wire
= nullptr;
438 log_assert(clk_name
!= "");
439 clk_wire
= module
->wire(clk_name
);
440 log_assert(!clk_wire
);
441 log_debug("Creating %s\n", clk_name
.c_str());
442 clk_wire
= module
->addWire(clk_name
);
443 clk_wire
->port_input
= true;
444 clk_wire
->port_output
= false;
447 for (unsigned i
= 0; i
< L
; ++i
, ++line_count
, l1
+= 2) {
449 log_error("Line %u cannot be interpreted as a latch!\n", line_count
);
450 log_debug("%d %d is a latch\n", l1
, l2
);
451 RTLIL::Wire
*q_wire
= createWireIfNotExists(module
, l1
);
452 RTLIL::Wire
*d_wire
= createWireIfNotExists(module
, l2
);
454 module
->addDff(NEW_ID
, clk_wire
, d_wire
, q_wire
);
456 // Reset logic is optional in AIGER 1.9
457 if (f
.peek() == ' ') {
459 log_error("Line %u cannot be interpreted as a latch!\n", line_count
);
461 if (l3
== 0 || l3
== 1)
462 q_wire
->attributes
["\\init"] = RTLIL::Const(l3
);
464 //q_wire->attributes["\\init"] = RTLIL::Const(RTLIL::State::Sx);
467 log_error("Line %u has invalid reset literal for latch!\n", line_count
);
470 // AIGER latches are assumed to be initialized to zero
471 q_wire
->attributes
["\\init"] = RTLIL::Const(0);
473 latches
.push_back(q_wire
);
477 for (unsigned i
= 0; i
< O
; ++i
, ++line_count
) {
479 log_error("Line %u cannot be interpreted as an output!\n", line_count
);
482 if (l1
== 0 || l1
== 1) {
483 wire
= module
->addWire(NEW_ID
);
485 module
->connect(wire
, RTLIL::State::S0
);
487 module
->connect(wire
, RTLIL::State::S1
);
492 log_debug("%d is an output\n", l1
);
493 const unsigned variable
= l1
>> 1;
494 const bool invert
= l1
& 1;
495 RTLIL::IdString
wire_name(stringf("\\__%d%s__", variable
, invert
? "b" : "")); // FIXME: is "_b" the right suffix?
496 wire
= module
->wire(wire_name
);
498 wire
= createWireIfNotExists(module
, l1
);
500 if (wire
->port_input
|| wire
->port_output
) {
501 RTLIL::Wire
*new_wire
= module
->addWire(NEW_ID
);
502 module
->connect(new_wire
, wire
);
507 wire
->port_output
= true;
508 outputs
.push_back(wire
);
510 std::getline(f
, line
); // Ignore up to start of next line
512 // TODO: Parse bad state properties
513 for (unsigned i
= 0; i
< B
; ++i
, ++line_count
)
514 std::getline(f
, line
); // Ignore up to start of next line
516 // TODO: Parse invariant constraints
517 for (unsigned i
= 0; i
< C
; ++i
, ++line_count
)
518 std::getline(f
, line
); // Ignore up to start of next line
520 // TODO: Parse justice properties
521 for (unsigned i
= 0; i
< J
; ++i
, ++line_count
)
522 std::getline(f
, line
); // Ignore up to start of next line
524 // TODO: Parse fairness constraints
525 for (unsigned i
= 0; i
< F
; ++i
, ++line_count
)
526 std::getline(f
, line
); // Ignore up to start of next line
530 for (unsigned i
= 0; i
< A
; ++i
, ++line_count
, l1
+= 2) {
531 l2
= parse_next_delta_literal(f
, l1
);
532 l3
= parse_next_delta_literal(f
, l2
);
534 log_debug("%d %d %d is an AND\n", l1
, l2
, l3
);
535 log_assert(!(l1
& 1));
536 RTLIL::Wire
*o_wire
= createWireIfNotExists(module
, l1
);
537 RTLIL::Wire
*i1_wire
= createWireIfNotExists(module
, l2
);
538 RTLIL::Wire
*i2_wire
= createWireIfNotExists(module
, l3
);
539 module
->addAndGate(o_wire
->name
.str() + "$and", i1_wire
, i2_wire
, o_wire
);
543 void AigerReader::post_process()
545 dict
<RTLIL::IdString
, int> wideports_cache
;
547 if (!map_filename
.empty()) {
548 std::ifstream
mf(map_filename
);
549 std::string type
, symbol
;
551 int pi_count
= 0, ci_count
= 0, co_count
= 0;
552 while (mf
>> type
>> variable
>> index
>> symbol
) {
553 RTLIL::IdString escaped_s
= RTLIL::escape_id(symbol
);
554 if (type
== "input") {
555 log_assert(static_cast<unsigned>(variable
) < inputs
.size());
556 RTLIL::Wire
* wire
= inputs
[variable
];
558 log_assert(wire
->port_input
);
562 // Cope with the fact that a CI might be identical
563 // to a PI (necessary due to ABC); in those cases
564 // simply connect the latter to the former
565 RTLIL::Wire
* existing
= module
->wire(escaped_s
);
567 module
->rename(wire
, escaped_s
);
569 wire
->port_input
= false;
570 module
->connect(wire
, existing
);
573 else if (index
> 0) {
574 std::string indexed_name
= stringf("%s[%d]", escaped_s
.c_str(), index
);
575 RTLIL::Wire
* existing
= module
->wire(indexed_name
);
577 module
->rename(wire
, indexed_name
);
579 wideports_cache
[escaped_s
] = std::max(wideports_cache
[escaped_s
], index
);
582 module
->connect(wire
, existing
);
583 wire
->port_input
= false;
587 else if (type
== "output") {
588 log_assert(static_cast<unsigned>(variable
+ co_count
) < outputs
.size());
589 RTLIL::Wire
* wire
= outputs
[variable
+ co_count
];
591 log_assert(wire
->port_output
);
592 if (escaped_s
.in("\\__dummy_o__", "\\__const0__", "\\__const1__")) {
593 wire
->port_output
= false;
598 // Cope with the fact that a CO might be identical
599 // to a PO (necessary due to ABC); in those cases
600 // simply connect the latter to the former
601 RTLIL::Wire
* existing
= module
->wire(escaped_s
);
603 if (escaped_s
.ends_with("$inout.out")) {
604 wire
->port_output
= false;
605 RTLIL::Wire
*in_wire
= module
->wire(escaped_s
.substr(0, escaped_s
.size()-10));
607 log_assert(in_wire
->port_input
&& !in_wire
->port_output
);
608 in_wire
->port_output
= true;
609 module
->connect(in_wire
, wire
);
612 module
->rename(wire
, escaped_s
);
615 wire
->port_output
= false;
616 module
->connect(wire
, existing
);
619 else if (index
> 0) {
620 std::string indexed_name
= stringf("%s[%d]", escaped_s
.c_str(), index
);
621 RTLIL::Wire
* existing
= module
->wire(indexed_name
);
623 if (escaped_s
.ends_with("$inout.out")) {
624 wire
->port_output
= false;
625 RTLIL::Wire
*in_wire
= module
->wire(stringf("%s[%d]", escaped_s
.substr(0, escaped_s
.size()-10).c_str(), index
));
627 log_assert(in_wire
->port_input
&& !in_wire
->port_output
);
628 in_wire
->port_output
= true;
629 module
->connect(in_wire
, wire
);
632 module
->rename(wire
, indexed_name
);
634 wideports_cache
[escaped_s
] = std::max(wideports_cache
[escaped_s
], index
);
638 module
->connect(wire
, existing
);
639 wire
->port_output
= false;
643 else if (type
== "box") {
644 RTLIL::Cell
* cell
= module
->cell(stringf("$__box%d__", variable
));
646 module
->rename(cell
, escaped_s
);
647 RTLIL::Module
* box_module
= design
->module(cell
->type
);
648 log_assert(box_module
);
649 // NB: Assume box_module->ports are sorted alphabetically
650 // (as RTLIL::Module::fixup_ports() would do)
651 for (auto port_name
: box_module
->ports
) {
652 RTLIL::Wire
* w
= box_module
->wire(port_name
);
655 for (int i
= 0; i
< GetSize(w
); i
++) {
657 log_assert(static_cast<unsigned>(co_count
) < outputs
.size());
658 RTLIL::Wire
* wire
= outputs
[co_count
++];
660 log_assert(wire
->port_output
);
661 wire
->port_output
= false;
664 if (w
->port_output
) {
665 log_assert(static_cast<unsigned>(pi_count
+ ci_count
) < inputs
.size());
666 RTLIL::Wire
* wire
= inputs
[pi_count
+ ci_count
++];
668 log_assert(wire
->port_input
);
669 wire
->port_input
= false;
673 cell
->setPort(port_name
, rhs
);
678 log_error("Symbol type '%s' not recognised.\n", type
.c_str());
682 for (auto &wp
: wideports_cache
) {
683 auto name
= wp
.first
;
684 int width
= wp
.second
+ 1;
686 RTLIL::Wire
*wire
= module
->wire(name
);
688 module
->rename(wire
, RTLIL::escape_id(stringf("%s[%d]", name
.c_str(), 0)));
690 // Do not make ports with a mix of input/output into
692 bool port_input
= false, port_output
= false;
693 for (int i
= 0; i
< width
; i
++) {
694 RTLIL::IdString other_name
= name
.str() + stringf("[%d]", i
);
695 RTLIL::Wire
*other_wire
= module
->wire(other_name
);
697 port_input
= port_input
|| other_wire
->port_input
;
698 port_output
= port_output
|| other_wire
->port_output
;
701 if ((port_input
&& port_output
) || (!port_input
&& !port_output
))
704 wire
= module
->addWire(name
, width
);
705 wire
->port_input
= port_input
;
706 wire
->port_output
= port_output
;
708 for (int i
= 0; i
< width
; i
++) {
709 RTLIL::IdString other_name
= name
.str() + stringf("[%d]", i
);
710 RTLIL::Wire
*other_wire
= module
->wire(other_name
);
712 other_wire
->port_input
= false;
713 other_wire
->port_output
= false;
714 if (wire
->port_input
)
715 module
->connect(other_wire
, SigSpec(wire
, i
));
717 module
->connect(SigSpec(wire
, i
), other_wire
);
722 module
->fixup_ports();
725 Pass::call(design
, "clean");
727 for (auto cell
: module
->cells().to_vector()) {
728 if (cell
->type
!= "$lut") continue;
729 auto y_port
= cell
->getPort("\\Y").as_bit();
730 if (y_port
.wire
->width
== 1)
731 module
->rename(cell
, stringf("%s$lut", y_port
.wire
->name
.c_str()));
733 module
->rename(cell
, stringf("%s[%d]$lut", y_port
.wire
->name
.c_str(), y_port
.offset
));
737 struct AigerFrontend
: public Frontend
{
738 AigerFrontend() : Frontend("aiger", "read AIGER file") { }
739 void help() YS_OVERRIDE
741 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
743 log(" read_aiger [options] [filename]\n");
745 log("Load module from an AIGER file into the current design.\n");
747 log(" -module_name <module_name>\n");
748 log(" Name of module to be created (default: <filename>)\n");
750 log(" -clk_name <wire_name>\n");
751 log(" AIGER latches to be transformed into posedge DFFs clocked by wire of");
752 log(" this name (default: clk)\n");
754 log(" -map <filename>\n");
755 log(" read file with port and latch symbols\n");
757 log(" -wideports\n");
758 log(" Merge ports that match the pattern 'name[int]' into a single\n");
759 log(" multi-bit port 'name'.\n");
762 void execute(std::istream
*&f
, std::string filename
, std::vector
<std::string
> args
, RTLIL::Design
*design
) YS_OVERRIDE
764 log_header(design
, "Executing AIGER frontend.\n");
766 RTLIL::IdString clk_name
= "\\clk";
767 RTLIL::IdString module_name
;
768 std::string map_filename
;
769 bool wideports
= false;
772 for (argidx
= 1; argidx
< args
.size(); argidx
++) {
773 std::string arg
= args
[argidx
];
774 if (arg
== "-module_name" && argidx
+1 < args
.size()) {
775 module_name
= RTLIL::escape_id(args
[++argidx
]);
778 if (arg
== "-clk_name" && argidx
+1 < args
.size()) {
779 clk_name
= RTLIL::escape_id(args
[++argidx
]);
782 if (map_filename
.empty() && arg
== "-map" && argidx
+1 < args
.size()) {
783 map_filename
= args
[++argidx
];
786 if (arg
== "-wideports") {
792 extra_args(f
, filename
, args
, argidx
);
794 if (module_name
.empty()) {
796 char fname
[_MAX_FNAME
];
797 _splitpath(filename
.c_str(), NULL
/* drive */, NULL
/* dir */, fname
, NULL
/* ext */)
800 char* bn
= strdup(filename
.c_str());
801 module_name
= RTLIL::escape_id(bn
);
806 AigerReader
reader(design
, *f
, module_name
, clk_name
, map_filename
, wideports
);
807 reader
.parse_aiger();