2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5 * 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
30 #include "kernel/yosys.h"
31 #include "kernel/sigtools.h"
32 #include "aigerparse.h"
38 AigerReader::AigerReader(RTLIL::Design
*design
, std::istream
&f
, RTLIL::IdString module_name
, RTLIL::IdString clk_name
)
39 : design(design
), f(f
), clk_name(clk_name
)
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 for (auto &i
: std::array
<std::reference_wrapper
<unsigned>,4>{B
, C
, J
, F
}) {
61 if (f
.peek() != ' ') break;
63 log_error("Invalid AIGER header\n");
67 std::getline(f
, line
); // Ignore up to start of next line, as standard
68 // says anything that follows could be used for
71 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
);
77 else if (header
== "aig")
82 // Parse footer (symbol table, comments, etc.)
85 for (int c
= f
.peek(); c
!= EOF
; c
= f
.peek(), ++line_count
) {
86 if (c
== 'i' || c
== 'l' || c
== 'o') {
89 log_error("Line %u cannot be interpreted as a symbol entry!\n", line_count
);
91 if ((c
== 'i' && l1
> inputs
.size()) || (c
== 'l' && l1
> latches
.size()) || (c
== 'o' && l1
> outputs
.size()))
92 log_error("Line %u has invalid symbol position!\n", line_count
);
95 if (c
== 'i') wire
= inputs
[l1
];
96 else if (c
== 'l') wire
= latches
[l1
];
97 else if (c
== 'o') wire
= outputs
[l1
];
100 module
->rename(wire
, stringf("\\%s", s
.c_str()));
102 else if (c
== 'b' || c
== 'j' || c
== 'f') {
107 if (f
.peek() == '\n')
109 // Else constraint (TODO)
112 log_error("Line %u: cannot interpret first character '%c'!\n", line_count
, c
);
113 std::getline(f
, line
); // Ignore up to start of next line
116 module
->fixup_ports();
120 static RTLIL::Wire
* createWireIfNotExists(RTLIL::Module
*module
, unsigned literal
)
122 const unsigned variable
= literal
>> 1;
123 const bool invert
= literal
& 1;
124 RTLIL::IdString
wire_name(stringf("\\n%d%s", variable
, invert
? "_inv" : "")); // FIXME: is "_inv" the right suffix?
125 RTLIL::Wire
*wire
= module
->wire(wire_name
);
126 if (wire
) return wire
;
127 log_debug("Creating %s\n", wire_name
.c_str());
128 wire
= module
->addWire(wire_name
);
129 if (!invert
) return wire
;
130 RTLIL::IdString
wire_inv_name(stringf("\\n%d", variable
));
131 RTLIL::Wire
*wire_inv
= module
->wire(wire_inv_name
);
133 if (module
->cell(wire_inv_name
)) return wire
;
136 log_debug("Creating %s\n", wire_inv_name
.c_str());
137 wire_inv
= module
->addWire(wire_inv_name
);
140 log_debug("Creating %s = ~%s\n", wire_name
.c_str(), wire_inv_name
.c_str());
141 module
->addNotGate(stringf("\\n%d_not", variable
), wire_inv
, wire
); // FIXME: is "_not" the right suffix?
146 void AigerReader::parse_aiger_ascii()
149 std::stringstream ss
;
154 for (unsigned i
= 0; i
< I
; ++i
, ++line_count
) {
156 log_error("Line %u cannot be interpreted as an input!\n", line_count
);
157 log_debug("%d is an input\n", l1
);
158 log_assert(!(l1
& 1)); // TODO: Inputs can't be inverted?
159 RTLIL::Wire
*wire
= createWireIfNotExists(module
, l1
);
160 wire
->port_input
= true;
161 inputs
.push_back(wire
);
165 RTLIL::Wire
*clk_wire
= nullptr;
167 clk_wire
= module
->wire(clk_name
);
168 log_assert(!clk_wire
);
169 log_debug("Creating %s\n", clk_name
.c_str());
170 clk_wire
= module
->addWire(clk_name
);
171 clk_wire
->port_input
= true;
173 for (unsigned i
= 0; i
< L
; ++i
, ++line_count
) {
174 if (!(f
>> l1
>> l2
))
175 log_error("Line %u cannot be interpreted as a latch!\n", line_count
);
176 log_debug("%d %d is a latch\n", l1
, l2
);
177 log_assert(!(l1
& 1)); // TODO: Latch outputs can't be inverted?
178 RTLIL::Wire
*q_wire
= createWireIfNotExists(module
, l1
);
179 RTLIL::Wire
*d_wire
= createWireIfNotExists(module
, l2
);
181 module
->addDffGate(NEW_ID
, clk_wire
, d_wire
, q_wire
);
183 // Reset logic is optional in AIGER 1.9
184 if (f
.peek() == ' ') {
186 log_error("Line %u cannot be interpreted as a latch!\n", line_count
);
188 if (l3
== 0 || l3
== 1)
189 q_wire
->attributes
["\\init"] = RTLIL::Const(l3
);
191 //q_wire->attributes["\\init"] = RTLIL::Const(RTLIL::State::Sx);
194 log_error("Line %u has invalid reset literal for latch!\n", line_count
);
197 // AIGER latches are assumed to be initialized to zero
198 q_wire
->attributes
["\\init"] = RTLIL::Const(0);
200 latches
.push_back(q_wire
);
204 for (unsigned i
= 0; i
< O
; ++i
, ++line_count
) {
206 log_error("Line %u cannot be interpreted as an output!\n", line_count
);
208 log_debug("%d is an output\n", l1
);
209 RTLIL::Wire
*wire
= createWireIfNotExists(module
, l1
);
210 wire
->port_output
= true;
211 outputs
.push_back(wire
);
213 std::getline(f
, line
); // Ignore up to start of next line
215 // TODO: Parse bad state properties
216 for (unsigned i
= 0; i
< B
; ++i
, ++line_count
)
217 std::getline(f
, line
); // Ignore up to start of next line
219 // TODO: Parse invariant constraints
220 for (unsigned i
= 0; i
< C
; ++i
, ++line_count
)
221 std::getline(f
, line
); // Ignore up to start of next line
223 // TODO: Parse justice properties
224 for (unsigned i
= 0; i
< J
; ++i
, ++line_count
)
225 std::getline(f
, line
); // Ignore up to start of next line
227 // TODO: Parse fairness constraints
228 for (unsigned i
= 0; i
< F
; ++i
, ++line_count
)
229 std::getline(f
, line
); // Ignore up to start of next line
232 for (unsigned i
= 0; i
< A
; ++i
) {
233 if (!(f
>> l1
>> l2
>> l3
))
234 log_error("Line %u cannot be interpreted as an AND!\n", line_count
);
236 log_debug("%d %d %d is an AND\n", l1
, l2
, l3
);
237 log_assert(!(l1
& 1)); // TODO: Output of ANDs can't be inverted?
238 RTLIL::Wire
*o_wire
= createWireIfNotExists(module
, l1
);
239 RTLIL::Wire
*i1_wire
= createWireIfNotExists(module
, l2
);
240 RTLIL::Wire
*i2_wire
= createWireIfNotExists(module
, l3
);
241 module
->addAndGate(NEW_ID
, i1_wire
, i2_wire
, o_wire
);
243 std::getline(f
, line
); // Ignore up to start of next line
246 static unsigned parse_next_delta_literal(std::istream
&f
, unsigned ref
)
248 unsigned x
= 0, i
= 0;
250 while ((ch
= f
.get()) & 0x80)
251 x
|= (ch
& 0x7f) << (7 * i
++);
252 return ref
- (x
| (ch
<< (7 * i
)));
255 void AigerReader::parse_aiger_binary()
261 for (unsigned i
= 1; i
<= I
; ++i
) {
262 RTLIL::Wire
*wire
= createWireIfNotExists(module
, i
<< 1);
263 wire
->port_input
= true;
264 inputs
.push_back(wire
);
268 RTLIL::Wire
*clk_wire
= nullptr;
270 clk_wire
= module
->wire(clk_name
);
271 log_assert(!clk_wire
);
272 log_debug("Creating %s\n", clk_name
.c_str());
273 clk_wire
= module
->addWire(clk_name
);
274 clk_wire
->port_input
= true;
277 for (unsigned i
= 0; i
< L
; ++i
, ++line_count
, l1
+= 2) {
279 log_error("Line %u cannot be interpreted as a latch!\n", line_count
);
280 log_debug("%d %d is a latch\n", l1
, l2
);
281 RTLIL::Wire
*q_wire
= createWireIfNotExists(module
, l1
);
282 RTLIL::Wire
*d_wire
= createWireIfNotExists(module
, l2
);
284 module
->addDff(NEW_ID
, clk_wire
, d_wire
, q_wire
);
286 // Reset logic is optional in AIGER 1.9
287 if (f
.peek() == ' ') {
289 log_error("Line %u cannot be interpreted as a latch!\n", line_count
);
291 if (l3
== 0 || l3
== 1)
292 q_wire
->attributes
["\\init"] = RTLIL::Const(l3
);
294 //q_wire->attributes["\\init"] = RTLIL::Const(RTLIL::State::Sx);
297 log_error("Line %u has invalid reset literal for latch!\n", line_count
);
300 // AIGER latches are assumed to be initialized to zero
301 q_wire
->attributes
["\\init"] = RTLIL::Const(0);
303 latches
.push_back(q_wire
);
307 for (unsigned i
= 0; i
< O
; ++i
, ++line_count
) {
309 log_error("Line %u cannot be interpreted as an output!\n", line_count
);
311 log_debug("%d is an output\n", l1
);
312 RTLIL::Wire
*wire
= createWireIfNotExists(module
, l1
);
313 wire
->port_output
= true;
314 outputs
.push_back(wire
);
316 std::getline(f
, line
); // Ignore up to start of next line
318 // TODO: Parse bad state properties
319 for (unsigned i
= 0; i
< B
; ++i
, ++line_count
)
320 std::getline(f
, line
); // Ignore up to start of next line
322 // TODO: Parse invariant constraints
323 for (unsigned i
= 0; i
< C
; ++i
, ++line_count
)
324 std::getline(f
, line
); // Ignore up to start of next line
326 // TODO: Parse justice properties
327 for (unsigned i
= 0; i
< J
; ++i
, ++line_count
)
328 std::getline(f
, line
); // Ignore up to start of next line
330 // TODO: Parse fairness constraints
331 for (unsigned i
= 0; i
< F
; ++i
, ++line_count
)
332 std::getline(f
, line
); // Ignore up to start of next line
336 for (unsigned i
= 0; i
< A
; ++i
, ++line_count
, l1
+= 2) {
337 l2
= parse_next_delta_literal(f
, l1
);
338 l3
= parse_next_delta_literal(f
, l2
);
340 log_debug("%d %d %d is an AND\n", l1
, l2
, l3
);
341 log_assert(!(l1
& 1)); // TODO: Output of ANDs can't be inverted?
342 RTLIL::Wire
*o_wire
= createWireIfNotExists(module
, l1
);
343 RTLIL::Wire
*i1_wire
= createWireIfNotExists(module
, l2
);
344 RTLIL::Wire
*i2_wire
= createWireIfNotExists(module
, l3
);
346 RTLIL::Cell
*and_cell
= module
->addCell(NEW_ID
, "$_AND_");
347 and_cell
->setPort("\\A", i1_wire
);
348 and_cell
->setPort("\\B", i2_wire
);
349 and_cell
->setPort("\\Y", o_wire
);
353 struct AigerFrontend
: public Frontend
{
354 AigerFrontend() : Frontend("aiger", "read AIGER file") { }
355 void help() YS_OVERRIDE
357 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
359 log(" read_aiger [options] [filename]\n");
361 log("Load module from an AIGER file into the current design.\n");
363 log(" -module_name <module_name>\n");
364 log(" Name of module to be created (default: <filename>)"
372 log(" -clk_name <wire_name>\n");
373 log(" AIGER latches to be transformed into posedge DFFs clocked by wire of");
374 log(" this name (default: clk)\n");
377 void execute(std::istream
*&f
, std::string filename
, std::vector
<std::string
> args
, RTLIL::Design
*design
) YS_OVERRIDE
379 log_header(design
, "Executing AIGER frontend.\n");
381 RTLIL::IdString clk_name
= "\\clk";
382 RTLIL::IdString module_name
;
385 for (argidx
= 1; argidx
< args
.size(); argidx
++) {
386 std::string arg
= args
[argidx
];
387 if (arg
== "-module_name" && argidx
+1 < args
.size()) {
388 module_name
= RTLIL::escape_id(args
[++argidx
]);
391 if (arg
== "-clk_name" && argidx
+1 < args
.size()) {
392 clk_name
= RTLIL::escape_id(args
[++argidx
]);
397 extra_args(f
, filename
, args
, argidx
);
399 if (module_name
.empty()) {
401 module_name
= "top"; // FIXME: basename equivalent on Win32?
403 char* bn
= strdup(filename
.c_str());
404 module_name
= RTLIL::escape_id(bn
);
409 AigerReader
reader(design
, *f
, module_name
, clk_name
);
410 reader
.parse_aiger();