2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include "kernel/yosys.h"
21 #include "kernel/sigtools.h"
24 PRIVATE_NAMESPACE_BEGIN
26 void aiger_encode(std::ostream
&f
, int x
)
31 f
.put((x
& 0x7f) | 0x80);
44 dict
<SigBit
, bool> init_map
;
45 pool
<SigBit
> input_bits
, output_bits
;
46 dict
<SigBit
, SigBit
> not_map
, ff_map
, alias_map
;
47 dict
<SigBit
, pair
<SigBit
, SigBit
>> and_map
;
48 pool
<SigBit
> initstate_bits
;
49 pool
<SigBit
> ci_bits
, co_bits
;
50 dict
<IdString
, unsigned> type_map
;
52 vector
<pair
<int, int>> aig_gates
;
53 vector
<int> aig_latchin
, aig_latchinit
, aig_outputs
;
54 int aig_m
= 0, aig_i
= 0, aig_l
= 0, aig_o
= 0, aig_a
= 0;
56 dict
<SigBit
, int> aig_map
;
57 dict
<SigBit
, int> ordered_outputs
;
58 dict
<SigBit
, int> ordered_latches
;
60 dict
<SigBit
, int> init_inputs
;
63 int mkgate(int a0
, int a1
)
66 aig_gates
.push_back(a0
> a1
? make_pair(a0
, a1
) : make_pair(a1
, a0
));
70 int bit2aig(SigBit bit
)
72 if (aig_map
.count(bit
) == 0)
76 if (initstate_bits
.count(bit
)) {
77 log_assert(initstate_ff
> 0);
78 aig_map
[bit
] = initstate_ff
;
80 if (not_map
.count(bit
)) {
81 int a
= bit2aig(not_map
.at(bit
)) ^ 1;
84 if (and_map
.count(bit
)) {
85 auto args
= and_map
.at(bit
);
86 int a0
= bit2aig(args
.first
);
87 int a1
= bit2aig(args
.second
);
88 aig_map
[bit
] = mkgate(a0
, a1
);
90 if (alias_map
.count(bit
)) {
91 aig_map
[bit
] = bit2aig(alias_map
.at(bit
));
94 if (bit
== State::Sx
|| bit
== State::Sz
)
95 log_error("Design contains 'x' or 'z' bits. Use 'setundef' to replace those constants.\n");
98 log_assert(aig_map
.at(bit
) >= 0);
99 return aig_map
.at(bit
);
102 XAigerWriter(Module
*module
, bool zinit_mode
, bool imode
, bool omode
, bool bmode
) : module(module
), zinit_mode(zinit_mode
), sigmap(module
)
104 pool
<SigBit
> undriven_bits
;
105 pool
<SigBit
> unused_bits
;
107 // promote public wires
108 for (auto wire
: module
->wires())
109 if (wire
->name
[0] == '\\')
112 // promote input wires
113 for (auto wire
: module
->wires())
114 if (wire
->port_input
)
117 // promote output wires
118 for (auto wire
: module
->wires())
119 if (wire
->port_output
)
122 for (auto wire
: module
->wires())
124 if (wire
->attributes
.count("\\init")) {
125 SigSpec initsig
= sigmap(wire
);
126 Const initval
= wire
->attributes
.at("\\init");
127 for (int i
= 0; i
< GetSize(wire
) && i
< GetSize(initval
); i
++)
128 if (initval
[i
] == State::S0
|| initval
[i
] == State::S1
)
129 init_map
[initsig
[i
]] = initval
[i
] == State::S1
;
132 for (int i
= 0; i
< GetSize(wire
); i
++)
134 SigBit
wirebit(wire
, i
);
135 SigBit bit
= sigmap(wirebit
);
137 if (bit
.wire
== nullptr) {
138 if (wire
->port_output
) {
139 aig_map
[wirebit
] = (bit
== State::S1
) ? 1 : 0;
140 output_bits
.insert(wirebit
);
145 undriven_bits
.insert(bit
);
146 unused_bits
.insert(bit
);
148 if (wire
->port_input
)
149 input_bits
.insert(bit
);
151 if (wire
->port_output
) {
153 alias_map
[wirebit
] = bit
;
154 //output_bits.insert(wirebit);
159 for (auto bit
: input_bits
)
160 undriven_bits
.erase(bit
);
162 for (auto bit
: output_bits
)
163 unused_bits
.erase(bit
);
165 for (auto cell
: module
->cells())
167 if (cell
->type
== "$_NOT_")
169 SigBit A
= sigmap(cell
->getPort("\\A").as_bit());
170 SigBit Y
= sigmap(cell
->getPort("\\Y").as_bit());
171 if (Y
.wire
->port_output
)
172 output_bits
.insert(Y
);
173 unused_bits
.erase(A
);
174 undriven_bits
.erase(Y
);
179 if (cell
->type
.in("$_FF_", "$_DFF_N_", "$_DFF_P_"))
181 SigBit D
= sigmap(cell
->getPort("\\D").as_bit());
182 SigBit Q
= sigmap(cell
->getPort("\\Q").as_bit());
183 unused_bits
.erase(D
);
184 undriven_bits
.erase(Q
);
189 if (cell
->type
== "$_AND_")
191 SigBit A
= sigmap(cell
->getPort("\\A").as_bit());
192 SigBit B
= sigmap(cell
->getPort("\\B").as_bit());
193 SigBit Y
= sigmap(cell
->getPort("\\Y").as_bit());
194 if (Y
.wire
->port_output
)
195 output_bits
.insert(Y
);
196 unused_bits
.erase(A
);
197 unused_bits
.erase(B
);
198 undriven_bits
.erase(Y
);
199 and_map
[Y
] = make_pair(A
, B
);
203 if (cell
->type
== "$initstate")
205 SigBit Y
= sigmap(cell
->getPort("\\Y").as_bit());
206 undriven_bits
.erase(Y
);
207 initstate_bits
.insert(Y
);
211 for (const auto &c
: cell
->connections()) {
212 if (c
.second
.is_fully_const()) continue;
213 SigBit b
= c
.second
.as_bit();
215 if (cell
->input(c
.first
)) {
216 SigBit I
= sigmap(b
);
219 unused_bits
.erase(I
);
221 else if (cell
->output(c
.first
)) {
222 SigBit O
= sigmap(b
);
225 undriven_bits
.erase(O
);
228 if (!type_map
.count(cell
->type
))
229 type_map
[cell
->type
] = type_map
.size()+1;
231 //log_error("Unsupported cell type: %s (%s)\n", log_id(cell->type), log_id(cell));
234 for (auto bit
: unused_bits
)
235 undriven_bits
.erase(bit
);
237 if (!undriven_bits
.empty()) {
238 undriven_bits
.sort();
239 for (auto bit
: undriven_bits
) {
240 log_warning("Treating undriven bit %s.%s like $anyseq.\n", log_id(module
), log_signal(bit
));
241 input_bits
.insert(bit
);
243 log_warning("Treating a total of %d undriven bits in %s like $anyseq.\n", GetSize(undriven_bits
), log_id(module
));
253 aig_map
[State::S0
] = 0;
254 aig_map
[State::S1
] = 1;
256 for (auto bit
: ci_bits
) {
258 aig_map
[bit
] = 2*aig_m
;
262 for (auto bit
: input_bits
) {
264 aig_map
[bit
] = 2*aig_m
;
267 if (imode
&& input_bits
.empty()) {
273 for (auto it
: ff_map
) {
274 if (init_map
.count(it
.first
))
277 init_inputs
[it
.first
] = 2*aig_m
;
281 for (auto it
: ff_map
) {
283 aig_map
[it
.first
] = 2*aig_m
;
284 ordered_latches
[it
.first
] = aig_l
-1;
285 if (init_map
.count(it
.first
) == 0)
286 aig_latchinit
.push_back(2);
288 aig_latchinit
.push_back(init_map
.at(it
.first
) ? 1 : 0);
291 if (!initstate_bits
.empty() || !init_inputs
.empty()) {
293 initstate_ff
= 2*aig_m
+1;
294 aig_latchinit
.push_back(0);
299 for (auto it
: ff_map
)
301 int l
= ordered_latches
[it
.first
];
303 if (aig_latchinit
.at(l
) == 1)
304 aig_map
[it
.first
] ^= 1;
306 if (aig_latchinit
.at(l
) == 2)
308 int gated_ffout
= mkgate(aig_map
[it
.first
], initstate_ff
^1);
309 int gated_initin
= mkgate(init_inputs
[it
.first
], initstate_ff
);
310 aig_map
[it
.first
] = mkgate(gated_ffout
^1, gated_initin
^1)^1;
315 for (auto it
: ff_map
) {
316 int a
= bit2aig(it
.second
);
317 int l
= ordered_latches
[it
.first
];
318 if (zinit_mode
&& aig_latchinit
.at(l
) == 1)
319 aig_latchin
.push_back(a
^ 1);
321 aig_latchin
.push_back(a
);
324 if (!initstate_bits
.empty() || !init_inputs
.empty())
325 aig_latchin
.push_back(1);
327 for (auto bit
: co_bits
) {
329 ordered_outputs
[bit
] = aig_o
-1;
330 aig_outputs
.push_back(bit2aig(bit
));
333 for (auto bit
: output_bits
) {
335 ordered_outputs
[bit
] = aig_o
-1;
336 aig_outputs
.push_back(bit2aig(bit
));
339 if (omode
&& output_bits
.empty()) {
341 aig_outputs
.push_back(0);
346 aig_outputs
.push_back(0);
350 void write_aiger(std::ostream
&f
, bool ascii_mode
, bool miter_mode
, bool symbols_mode
)
353 int aig_obcj
= aig_obc
;
354 int aig_obcjf
= aig_obcj
;
356 log_assert(aig_m
== aig_i
+ aig_l
+ aig_a
);
357 log_assert(aig_l
== GetSize(aig_latchin
));
358 log_assert(aig_l
== GetSize(aig_latchinit
));
359 log_assert(aig_obcjf
== GetSize(aig_outputs
));
361 f
<< stringf("%s %d %d %d %d %d", ascii_mode
? "aag" : "aig", aig_m
, aig_i
, aig_l
, aig_o
, aig_a
);
366 for (int i
= 0; i
< aig_i
; i
++)
367 f
<< stringf("%d\n", 2*i
+2);
369 for (int i
= 0; i
< aig_l
; i
++) {
370 if (zinit_mode
|| aig_latchinit
.at(i
) == 0)
371 f
<< stringf("%d %d\n", 2*(aig_i
+i
)+2, aig_latchin
.at(i
));
372 else if (aig_latchinit
.at(i
) == 1)
373 f
<< stringf("%d %d 1\n", 2*(aig_i
+i
)+2, aig_latchin
.at(i
));
374 else if (aig_latchinit
.at(i
) == 2)
375 f
<< stringf("%d %d %d\n", 2*(aig_i
+i
)+2, aig_latchin
.at(i
), 2*(aig_i
+i
)+2);
378 for (int i
= 0; i
< aig_obc
; i
++)
379 f
<< stringf("%d\n", aig_outputs
.at(i
));
381 for (int i
= aig_obc
; i
< aig_obcj
; i
++)
384 for (int i
= aig_obc
; i
< aig_obcj
; i
++)
385 f
<< stringf("%d\n", aig_outputs
.at(i
));
387 for (int i
= aig_obcj
; i
< aig_obcjf
; i
++)
388 f
<< stringf("%d\n", aig_outputs
.at(i
));
390 for (int i
= 0; i
< aig_a
; i
++)
391 f
<< stringf("%d %d %d\n", 2*(aig_i
+aig_l
+i
)+2, aig_gates
.at(i
).first
, aig_gates
.at(i
).second
);
395 for (int i
= 0; i
< aig_l
; i
++) {
396 if (zinit_mode
|| aig_latchinit
.at(i
) == 0)
397 f
<< stringf("%d\n", aig_latchin
.at(i
));
398 else if (aig_latchinit
.at(i
) == 1)
399 f
<< stringf("%d 1\n", aig_latchin
.at(i
));
400 else if (aig_latchinit
.at(i
) == 2)
401 f
<< stringf("%d %d\n", aig_latchin
.at(i
), 2*(aig_i
+i
)+2);
404 for (int i
= 0; i
< aig_obc
; i
++)
405 f
<< stringf("%d\n", aig_outputs
.at(i
));
407 for (int i
= aig_obc
; i
< aig_obcj
; i
++)
410 for (int i
= aig_obc
; i
< aig_obcj
; i
++)
411 f
<< stringf("%d\n", aig_outputs
.at(i
));
413 for (int i
= aig_obcj
; i
< aig_obcjf
; i
++)
414 f
<< stringf("%d\n", aig_outputs
.at(i
));
416 for (int i
= 0; i
< aig_a
; i
++) {
417 int lhs
= 2*(aig_i
+aig_l
+i
)+2;
418 int rhs0
= aig_gates
.at(i
).first
;
419 int rhs1
= aig_gates
.at(i
).second
;
420 int delta0
= lhs
- rhs0
;
421 int delta1
= rhs0
- rhs1
;
422 aiger_encode(f
, delta0
);
423 aiger_encode(f
, delta1
);
429 dict
<string
, vector
<string
>> symbols
;
431 for (auto wire
: module
->wires())
433 if (wire
->name
[0] == '$')
436 SigSpec sig
= sigmap(wire
);
438 for (int i
= 0; i
< GetSize(wire
); i
++)
440 if (sig
[i
].wire
== nullptr) {
441 if (wire
->port_output
)
442 sig
[i
] = SigBit(wire
, i
);
447 if (wire
->port_input
) {
448 int a
= aig_map
.at(sig
[i
]);
449 log_assert((a
& 1) == 0);
450 if (GetSize(wire
) != 1)
451 symbols
[stringf("i%d", (a
>> 1)-1)].push_back(stringf("%s[%d]", log_id(wire
), i
));
453 symbols
[stringf("i%d", (a
>> 1)-1)].push_back(stringf("%s", log_id(wire
)));
456 if (wire
->port_output
) {
457 int o
= ordered_outputs
.at(SigSpec(wire
, i
));
458 if (GetSize(wire
) != 1)
459 symbols
[stringf("%c%d", miter_mode
? 'b' : 'o', o
)].push_back(stringf("%s[%d]", log_id(wire
), i
));
461 symbols
[stringf("%c%d", miter_mode
? 'b' : 'o', o
)].push_back(stringf("%s", log_id(wire
)));
464 if (init_inputs
.count(sig
[i
])) {
465 int a
= init_inputs
.at(sig
[i
]);
466 log_assert((a
& 1) == 0);
467 if (GetSize(wire
) != 1)
468 symbols
[stringf("i%d", (a
>> 1)-1)].push_back(stringf("init:%s[%d]", log_id(wire
), i
));
470 symbols
[stringf("i%d", (a
>> 1)-1)].push_back(stringf("init:%s", log_id(wire
)));
473 if (ordered_latches
.count(sig
[i
])) {
474 int l
= ordered_latches
.at(sig
[i
]);
475 const char *p
= (zinit_mode
&& (aig_latchinit
.at(l
) == 1)) ? "!" : "";
476 if (GetSize(wire
) != 1)
477 symbols
[stringf("l%d", l
)].push_back(stringf("%s%s[%d]", p
, log_id(wire
), i
));
479 symbols
[stringf("l%d", l
)].push_back(stringf("%s%s", p
, log_id(wire
)));
486 for (auto &sym
: symbols
) {
488 std::sort(sym
.second
.begin(), sym
.second
.end());
489 for (auto &s
: sym
.second
)
495 f
<< stringf("c\nGenerated by %s\n", yosys_version_str
);
498 void write_map(std::ostream
&f
, bool verbose_map
)
500 dict
<int, string
> input_lines
;
501 dict
<int, string
> init_lines
;
502 dict
<int, string
> output_lines
;
503 dict
<int, string
> latch_lines
;
504 dict
<int, string
> wire_lines
;
506 for (auto wire
: module
->wires())
508 //if (!verbose_map && wire->name[0] == '$')
511 SigSpec sig
= sigmap(wire
);
513 for (int i
= 0; i
< GetSize(wire
); i
++)
515 if (aig_map
.count(sig
[i
]) == 0 || sig
[i
].wire
== nullptr)
518 int a
= aig_map
.at(sig
[i
]);
521 wire_lines
[a
] += stringf("wire %d %d %s\n", a
, i
, log_id(wire
));
523 if (wire
->port_input
|| ci_bits
.count(RTLIL::SigBit
{wire
, i
})) {
524 log_assert((a
& 1) == 0);
525 input_lines
[a
] += stringf("input %d %d %s\n", (a
>> 1)-1, i
, log_id(wire
));
528 if (wire
->port_output
|| co_bits
.count(RTLIL::SigBit
{wire
, i
})) {
529 int o
= ordered_outputs
.at(sig
[i
]);
530 output_lines
[o
] += stringf("output %d %d %s\n", o
, i
, log_id(wire
));
533 if (init_inputs
.count(sig
[i
])) {
534 int a
= init_inputs
.at(sig
[i
]);
535 log_assert((a
& 1) == 0);
536 init_lines
[a
] += stringf("init %d %d %s\n", (a
>> 1)-1, i
, log_id(wire
));
539 if (ordered_latches
.count(sig
[i
])) {
540 int l
= ordered_latches
.at(sig
[i
]);
541 if (zinit_mode
&& (aig_latchinit
.at(l
) == 1))
542 latch_lines
[l
] += stringf("invlatch %d %d %s\n", l
, i
, log_id(wire
));
544 latch_lines
[l
] += stringf("latch %d %d %s\n", l
, i
, log_id(wire
));
550 for (auto &it
: input_lines
)
554 for (auto &it
: init_lines
)
558 for (auto &it
: output_lines
)
562 for (auto &it
: latch_lines
)
566 for (auto &it
: wire_lines
)
571 struct XAigerBackend
: public Backend
{
572 XAigerBackend() : Backend("xaiger", "write design to XAIGER file") { }
573 void help() YS_OVERRIDE
575 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
577 log(" write_xaiger [options] [filename]\n");
579 log("Write the current design to an XAIGER file. The design must be flattened and\n");
580 log("all unsupported cells will be converted into psuedo-inputs and pseudo-outputs.\n");
583 log(" write ASCII version of AGIER format\n");
586 log(" convert FFs to zero-initialized FFs, adding additional inputs for\n");
587 log(" uninitialized FFs.\n");
590 log(" include a symbol table in the generated AIGER file\n");
592 log(" -map <filename>\n");
593 log(" write an extra file with port and latch symbols\n");
595 log(" -vmap <filename>\n");
596 log(" like -map, but more verbose\n");
598 log(" -I, -O, -B\n");
599 log(" If the design contains no input/output/assert then create one\n");
600 log(" dummy input/output/bad_state pin to make the tools reading the\n");
601 log(" AIGER file happy.\n");
604 void execute(std::ostream
*&f
, std::string filename
, std::vector
<std::string
> args
, RTLIL::Design
*design
) YS_OVERRIDE
606 bool ascii_mode
= false;
607 bool zinit_mode
= false;
608 bool miter_mode
= false;
609 bool symbols_mode
= false;
610 bool verbose_map
= false;
614 std::string map_filename
;
616 log_header(design
, "Executing XAIGER backend.\n");
619 for (argidx
= 1; argidx
< args
.size(); argidx
++)
621 if (args
[argidx
] == "-ascii") {
625 if (args
[argidx
] == "-zinit") {
629 if (args
[argidx
] == "-symbols") {
633 if (map_filename
.empty() && args
[argidx
] == "-map" && argidx
+1 < args
.size()) {
634 map_filename
= args
[++argidx
];
637 if (map_filename
.empty() && args
[argidx
] == "-vmap" && argidx
+1 < args
.size()) {
638 map_filename
= args
[++argidx
];
642 if (args
[argidx
] == "-I") {
646 if (args
[argidx
] == "-O") {
650 if (args
[argidx
] == "-B") {
656 extra_args(f
, filename
, args
, argidx
);
658 Module
*top_module
= design
->top_module();
660 if (top_module
== nullptr)
661 log_error("Can't find top module in current design!\n");
663 XAigerWriter
writer(top_module
, zinit_mode
, imode
, omode
, bmode
);
664 writer
.write_aiger(*f
, ascii_mode
, miter_mode
, symbols_mode
);
666 if (!map_filename
.empty()) {
668 mapf
.open(map_filename
.c_str(), std::ofstream::trunc
);
670 log_error("Can't open file `%s' for writing: %s\n", map_filename
.c_str(), strerror(errno
));
671 writer
.write_map(mapf
, verbose_map
);
676 PRIVATE_NAMESPACE_END