93e0ebc8ccd16a1383e76fb5464ba60cfa1498dc
[yosys.git] / backends / aiger / xaiger.cc
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5 * 2019 Eddie Hung <eddie@fpgeh.com>
6 *
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.
10 *
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.
18 *
19 */
20
21 // https://stackoverflow.com/a/46137633
22 #ifdef _MSC_VER
23 #include <stdlib.h>
24 #define bswap32 _byteswap_ulong
25 #elif defined(__APPLE__)
26 #include <libkern/OSByteOrder.h>
27 #define bswap32 OSSwapInt32
28 #elif defined(__GNUC__)
29 #define bswap32 __builtin_bswap32
30 #else
31 #include <cstdint>
32 inline static uint32_t bswap32(uint32_t x)
33 {
34 // https://stackoverflow.com/a/27796212
35 register uint32_t value = number_to_be_reversed;
36 uint8_t lolo = (value >> 0) & 0xFF;
37 uint8_t lohi = (value >> 8) & 0xFF;
38 uint8_t hilo = (value >> 16) & 0xFF;
39 uint8_t hihi = (value >> 24) & 0xFF;
40 return (hihi << 24)
41 | (hilo << 16)
42 | (lohi << 8)
43 | (lolo << 0);
44 }
45 #endif
46
47 #include "kernel/yosys.h"
48 #include "kernel/sigtools.h"
49 #include "kernel/utils.h"
50
51 USING_YOSYS_NAMESPACE
52 PRIVATE_NAMESPACE_BEGIN
53
54 inline int32_t to_big_endian(int32_t i32) {
55 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
56 return bswap32(i32);
57 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
58 return i32;
59 #else
60 #error "Unknown endianness"
61 #endif
62 }
63
64 void aiger_encode(std::ostream &f, int x)
65 {
66 log_assert(x >= 0);
67
68 while (x & ~0x7f) {
69 f.put((x & 0x7f) | 0x80);
70 x = x >> 7;
71 }
72
73 f.put(x);
74 }
75
76 struct XAigerWriter
77 {
78 Module *module;
79 SigMap sigmap;
80
81 pool<SigBit> input_bits, output_bits;
82 dict<SigBit, SigBit> not_map, alias_map;
83 dict<SigBit, pair<SigBit, SigBit>> and_map;
84 vector<SigBit> ci_bits, co_bits;
85 dict<SigBit, Cell*> ff_bits;
86 dict<SigBit, float> arrival_times;
87
88 vector<pair<int, int>> aig_gates;
89 vector<int> aig_outputs;
90 int aig_m = 0, aig_i = 0, aig_l = 0, aig_o = 0, aig_a = 0;
91
92 dict<SigBit, int> aig_map;
93 dict<SigBit, int> ordered_outputs;
94
95 vector<Cell*> box_list;
96
97 int mkgate(int a0, int a1)
98 {
99 aig_m++, aig_a++;
100 aig_gates.push_back(a0 > a1 ? make_pair(a0, a1) : make_pair(a1, a0));
101 return 2*aig_m;
102 }
103
104 int bit2aig(SigBit bit)
105 {
106 auto it = aig_map.find(bit);
107 if (it != aig_map.end()) {
108 log_assert(it->second >= 0);
109 return it->second;
110 }
111
112 // NB: Cannot use iterator returned from aig_map.insert()
113 // since this function is called recursively
114
115 int a = -1;
116 if (not_map.count(bit)) {
117 a = bit2aig(not_map.at(bit)) ^ 1;
118 } else
119 if (and_map.count(bit)) {
120 auto args = and_map.at(bit);
121 int a0 = bit2aig(args.first);
122 int a1 = bit2aig(args.second);
123 a = mkgate(a0, a1);
124 } else
125 if (alias_map.count(bit)) {
126 a = bit2aig(alias_map.at(bit));
127 }
128
129 if (bit == State::Sx || bit == State::Sz) {
130 log_debug("Design contains 'x' or 'z' bits. Treating as 1'b0.\n");
131 a = aig_map.at(State::S0);
132 }
133
134 log_assert(a >= 0);
135 aig_map[bit] = a;
136 return a;
137 }
138
139 XAigerWriter(Module *module, bool holes_mode=false) : module(module), sigmap(module)
140 {
141 pool<SigBit> undriven_bits;
142 pool<SigBit> unused_bits;
143
144 // promote public wires
145 for (auto wire : module->wires())
146 if (wire->name[0] == '\\')
147 sigmap.add(wire);
148
149 // promote input wires
150 for (auto wire : module->wires())
151 if (wire->port_input)
152 sigmap.add(wire);
153
154 // promote keep wires
155 for (auto wire : module->wires())
156 if (wire->get_bool_attribute(ID::keep))
157 sigmap.add(wire);
158
159
160 for (auto wire : module->wires())
161 for (int i = 0; i < GetSize(wire); i++)
162 {
163 SigBit wirebit(wire, i);
164 SigBit bit = sigmap(wirebit);
165
166 if (bit.wire == nullptr) {
167 if (wire->port_output) {
168 aig_map[wirebit] = (bit == State::S1) ? 1 : 0;
169 output_bits.insert(wirebit);
170 }
171 continue;
172 }
173
174 undriven_bits.insert(bit);
175 unused_bits.insert(bit);
176
177 if (wire->port_input)
178 input_bits.insert(bit);
179
180 if (wire->port_output) {
181 if (bit != wirebit)
182 alias_map[wirebit] = bit;
183 output_bits.insert(wirebit);
184 }
185 }
186
187 for (auto cell : module->cells()) {
188 if (cell->type == "$_NOT_")
189 {
190 SigBit A = sigmap(cell->getPort("\\A").as_bit());
191 SigBit Y = sigmap(cell->getPort("\\Y").as_bit());
192 unused_bits.erase(A);
193 undriven_bits.erase(Y);
194 not_map[Y] = A;
195 continue;
196 }
197
198 if (cell->type == "$_AND_")
199 {
200 SigBit A = sigmap(cell->getPort("\\A").as_bit());
201 SigBit B = sigmap(cell->getPort("\\B").as_bit());
202 SigBit Y = sigmap(cell->getPort("\\Y").as_bit());
203 unused_bits.erase(A);
204 unused_bits.erase(B);
205 undriven_bits.erase(Y);
206 and_map[Y] = make_pair(A, B);
207 continue;
208 }
209
210 if (cell->type == "$__ABC9_FF_" &&
211 // The presence of an abc9_mergeability attribute indicates
212 // that we do want to pass this flop to ABC
213 cell->attributes.count("\\abc9_mergeability"))
214 {
215 SigBit D = sigmap(cell->getPort("\\D").as_bit());
216 SigBit Q = sigmap(cell->getPort("\\Q").as_bit());
217 unused_bits.erase(D);
218 undriven_bits.erase(Q);
219 alias_map[Q] = D;
220 auto r YS_ATTRIBUTE(unused) = ff_bits.insert(std::make_pair(D, cell));
221 log_assert(r.second);
222 continue;
223 }
224
225 RTLIL::Module* inst_module = module->design->module(cell->type);
226 if (inst_module) {
227 auto it = cell->attributes.find("\\abc9_box_seq");
228 if (it != cell->attributes.end()) {
229 int abc9_box_seq = it->second.as_int();
230 if (GetSize(box_list) <= abc9_box_seq)
231 box_list.resize(abc9_box_seq+1);
232 box_list[abc9_box_seq] = cell;
233 if (!inst_module->get_bool_attribute("\\abc9_flop"))
234 continue;
235 }
236
237 for (const auto &conn : cell->connections()) {
238 auto port_wire = inst_module->wire(conn.first);
239 if (port_wire->port_output) {
240 int arrival = 0;
241 auto it = port_wire->attributes.find("\\abc9_arrival");
242 if (it != port_wire->attributes.end()) {
243 if (it->second.flags != 0)
244 log_error("Attribute 'abc9_arrival' on port '%s' of module '%s' is not an integer.\n", log_id(port_wire), log_id(cell->type));
245 arrival = it->second.as_int();
246 }
247 if (arrival)
248 for (auto bit : sigmap(conn.second))
249 arrival_times[bit] = arrival;
250 }
251 }
252 }
253
254 bool cell_known = inst_module || cell->known();
255 for (const auto &c : cell->connections()) {
256 if (c.second.is_fully_const()) continue;
257 auto port_wire = inst_module ? inst_module->wire(c.first) : nullptr;
258 auto is_input = (port_wire && port_wire->port_input) || !cell_known || cell->input(c.first);
259 auto is_output = (port_wire && port_wire->port_output) || !cell_known || cell->output(c.first);
260 if (!is_input && !is_output)
261 log_error("Connection '%s' on cell '%s' (type '%s') not recognised!\n", log_id(c.first), log_id(cell), log_id(cell->type));
262
263 if (is_input)
264 for (auto b : c.second) {
265 Wire *w = b.wire;
266 if (!w) continue;
267 if (!w->port_output || !cell_known) {
268 SigBit I = sigmap(b);
269 if (I != b)
270 alias_map[b] = I;
271 output_bits.insert(b);
272 }
273 }
274 }
275
276 //log_warning("Unsupported cell type: %s (%s)\n", log_id(cell->type), log_id(cell));
277 }
278
279 dict<IdString, std::vector<IdString>> box_ports;
280 for (auto cell : box_list) {
281 log_assert(cell);
282
283 RTLIL::Module* box_module = module->design->module(cell->type);
284 log_assert(box_module);
285 log_assert(box_module->attributes.count("\\abc9_box_id"));
286
287 auto r = box_ports.insert(cell->type);
288 if (r.second) {
289 // Make carry in the last PI, and carry out the last PO
290 // since ABC requires it this way
291 IdString carry_in, carry_out;
292 for (const auto &port_name : box_module->ports) {
293 auto w = box_module->wire(port_name);
294 log_assert(w);
295 if (w->get_bool_attribute("\\abc9_carry")) {
296 if (w->port_input) {
297 if (carry_in != IdString())
298 log_error("Module '%s' contains more than one 'abc9_carry' input port.\n", log_id(box_module));
299 carry_in = port_name;
300 }
301 if (w->port_output) {
302 if (carry_out != IdString())
303 log_error("Module '%s' contains more than one 'abc9_carry' output port.\n", log_id(box_module));
304 carry_out = port_name;
305 }
306 }
307 else
308 r.first->second.push_back(port_name);
309 }
310
311 if (carry_in != IdString() && carry_out == IdString())
312 log_error("Module '%s' contains an 'abc9_carry' input port but no output port.\n", log_id(box_module));
313 if (carry_in == IdString() && carry_out != IdString())
314 log_error("Module '%s' contains an 'abc9_carry' output port but no input port.\n", log_id(box_module));
315 if (carry_in != IdString()) {
316 r.first->second.push_back(carry_in);
317 r.first->second.push_back(carry_out);
318 }
319 }
320
321 // Fully pad all unused input connections of this box cell with S0
322 // Fully pad all undriven output connections of this box cell with anonymous wires
323 for (auto port_name : r.first->second) {
324 auto w = box_module->wire(port_name);
325 log_assert(w);
326 auto rhs = cell->getPort(port_name);
327 if (w->port_input)
328 for (auto b : rhs) {
329 SigBit I = sigmap(b);
330 if (b == RTLIL::Sx)
331 b = State::S0;
332 else if (I != b) {
333 if (I == RTLIL::Sx)
334 alias_map[b] = State::S0;
335 else
336 alias_map[b] = I;
337 }
338 co_bits.emplace_back(b);
339 unused_bits.erase(I);
340 }
341 if (w->port_output)
342 for (const auto &b : rhs.bits()) {
343 SigBit O = sigmap(b);
344 if (O != b)
345 alias_map[O] = b;
346 ci_bits.emplace_back(b);
347 undriven_bits.erase(O);
348 }
349 }
350
351 // Connect <cell>.abc9_ff.Q (inserted by abc9_map.v) as the last input to the flop box
352 if (box_module->get_bool_attribute("\\abc9_flop")) {
353 SigSpec rhs = module->wire(stringf("%s.abc9_ff.Q", cell->name.c_str()));
354 if (rhs.empty())
355 log_error("'%s.abc9_ff.Q' is not a wire present in module '%s'.\n", log_id(cell), log_id(module));
356
357 for (auto b : rhs) {
358 SigBit I = sigmap(b);
359 if (b == RTLIL::Sx)
360 b = State::S0;
361 else if (I != b) {
362 if (I == RTLIL::Sx)
363 alias_map[b] = State::S0;
364 else
365 alias_map[b] = I;
366 }
367 co_bits.emplace_back(b);
368 unused_bits.erase(I);
369 }
370 }
371 }
372
373 for (auto bit : input_bits)
374 undriven_bits.erase(bit);
375 for (auto bit : output_bits)
376 unused_bits.erase(sigmap(bit));
377 for (auto bit : unused_bits)
378 undriven_bits.erase(bit);
379
380 // Make all undriven bits a primary input
381 for (auto bit : undriven_bits) {
382 input_bits.insert(bit);
383 undriven_bits.erase(bit);
384 }
385
386 if (holes_mode) {
387 struct sort_by_port_id {
388 bool operator()(const RTLIL::SigBit& a, const RTLIL::SigBit& b) const {
389 return a.wire->port_id < b.wire->port_id ||
390 (a.wire->port_id == b.wire->port_id && a.offset < b.offset);
391 }
392 };
393 input_bits.sort(sort_by_port_id());
394 output_bits.sort(sort_by_port_id());
395 }
396
397 aig_map[State::S0] = 0;
398 aig_map[State::S1] = 1;
399
400 for (const auto &bit : input_bits) {
401 aig_m++, aig_i++;
402 log_assert(!aig_map.count(bit));
403 aig_map[bit] = 2*aig_m;
404 }
405
406 for (const auto &i : ff_bits) {
407 const Cell *cell = i.second;
408 const SigBit &q = sigmap(cell->getPort("\\Q"));
409 aig_m++, aig_i++;
410 log_assert(!aig_map.count(q));
411 aig_map[q] = 2*aig_m;
412 }
413
414 for (auto &bit : ci_bits) {
415 aig_m++, aig_i++;
416 log_assert(!aig_map.count(bit));
417 aig_map[bit] = 2*aig_m;
418 }
419
420 for (auto bit : co_bits) {
421 ordered_outputs[bit] = aig_o++;
422 aig_outputs.push_back(bit2aig(bit));
423 }
424
425 for (const auto &bit : output_bits) {
426 ordered_outputs[bit] = aig_o++;
427 aig_outputs.push_back(bit2aig(bit));
428 }
429
430 for (auto &i : ff_bits) {
431 const SigBit &d = i.first;
432 aig_o++;
433 aig_outputs.push_back(aig_map.at(d));
434 }
435 }
436
437 void write_aiger(std::ostream &f, bool ascii_mode)
438 {
439 int aig_obc = aig_o;
440 int aig_obcj = aig_obc;
441 int aig_obcjf = aig_obcj;
442
443 log_assert(aig_m == aig_i + aig_l + aig_a);
444 log_assert(aig_obcjf == GetSize(aig_outputs));
445
446 f << stringf("%s %d %d %d %d %d", ascii_mode ? "aag" : "aig", aig_m, aig_i, aig_l, aig_o, aig_a);
447 f << stringf("\n");
448
449 if (ascii_mode)
450 {
451 for (int i = 0; i < aig_i; i++)
452 f << stringf("%d\n", 2*i+2);
453
454 for (int i = 0; i < aig_obc; i++)
455 f << stringf("%d\n", aig_outputs.at(i));
456
457 for (int i = aig_obc; i < aig_obcj; i++)
458 f << stringf("1\n");
459
460 for (int i = aig_obc; i < aig_obcj; i++)
461 f << stringf("%d\n", aig_outputs.at(i));
462
463 for (int i = aig_obcj; i < aig_obcjf; i++)
464 f << stringf("%d\n", aig_outputs.at(i));
465
466 for (int i = 0; i < aig_a; i++)
467 f << stringf("%d %d %d\n", 2*(aig_i+aig_l+i)+2, aig_gates.at(i).first, aig_gates.at(i).second);
468 }
469 else
470 {
471 for (int i = 0; i < aig_obc; i++)
472 f << stringf("%d\n", aig_outputs.at(i));
473
474 for (int i = aig_obc; i < aig_obcj; i++)
475 f << stringf("1\n");
476
477 for (int i = aig_obc; i < aig_obcj; i++)
478 f << stringf("%d\n", aig_outputs.at(i));
479
480 for (int i = aig_obcj; i < aig_obcjf; i++)
481 f << stringf("%d\n", aig_outputs.at(i));
482
483 for (int i = 0; i < aig_a; i++) {
484 int lhs = 2*(aig_i+aig_l+i)+2;
485 int rhs0 = aig_gates.at(i).first;
486 int rhs1 = aig_gates.at(i).second;
487 int delta0 = lhs - rhs0;
488 int delta1 = rhs0 - rhs1;
489 aiger_encode(f, delta0);
490 aiger_encode(f, delta1);
491 }
492 }
493
494 f << "c";
495
496 auto write_buffer = [](std::stringstream &buffer, int i32) {
497 int32_t i32_be = to_big_endian(i32);
498 buffer.write(reinterpret_cast<const char*>(&i32_be), sizeof(i32_be));
499 };
500 std::stringstream h_buffer;
501 auto write_h_buffer = std::bind(write_buffer, std::ref(h_buffer), std::placeholders::_1);
502 write_h_buffer(1);
503 log_debug("ciNum = %d\n", GetSize(input_bits) + GetSize(ff_bits) + GetSize(ci_bits));
504 write_h_buffer(input_bits.size() + ff_bits.size() + ci_bits.size());
505 log_debug("coNum = %d\n", GetSize(output_bits) + GetSize(ff_bits) + GetSize(co_bits));
506 write_h_buffer(output_bits.size() + GetSize(ff_bits) + GetSize(co_bits));
507 log_debug("piNum = %d\n", GetSize(input_bits) + GetSize(ff_bits));
508 write_h_buffer(input_bits.size() + ff_bits.size());
509 log_debug("poNum = %d\n", GetSize(output_bits) + GetSize(ff_bits));
510 write_h_buffer(output_bits.size() + ff_bits.size());
511 log_debug("boxNum = %d\n", GetSize(box_list));
512 write_h_buffer(box_list.size());
513
514 auto write_buffer_float = [](std::stringstream &buffer, float f32) {
515 buffer.write(reinterpret_cast<const char*>(&f32), sizeof(f32));
516 };
517 std::stringstream i_buffer;
518 auto write_i_buffer = std::bind(write_buffer_float, std::ref(i_buffer), std::placeholders::_1);
519 for (auto bit : input_bits)
520 write_i_buffer(arrival_times.at(bit, 0));
521 //std::stringstream o_buffer;
522 //auto write_o_buffer = std::bind(write_buffer_float, std::ref(o_buffer), std::placeholders::_1);
523 //for (auto bit : output_bits)
524 // write_o_buffer(0);
525
526 if (!box_list.empty() || !ff_bits.empty()) {
527 RTLIL::Module *holes_module = module->design->module(stringf("%s$holes", module->name.c_str()));
528 log_assert(holes_module);
529
530 dict<IdString, std::tuple<int,int,int>> cell_cache;
531
532 int box_count = 0;
533 for (auto cell : box_list) {
534 log_assert(cell);
535
536 RTLIL::Module* box_module = module->design->module(cell->type);
537 log_assert(box_module);
538
539 auto r = cell_cache.insert(cell->type);
540 auto &v = r.first->second;
541 if (r.second) {
542 int box_inputs = 0, box_outputs = 0;
543 for (auto port_name : box_module->ports) {
544 RTLIL::Wire *w = box_module->wire(port_name);
545 log_assert(w);
546 if (w->port_input)
547 box_inputs += GetSize(w);
548 if (w->port_output)
549 box_outputs += GetSize(w);
550 }
551
552 // For flops only, create an extra 1-bit input that drives a new wire
553 // called "<cell>.abc9_ff.Q" that is used below
554 if (box_module->get_bool_attribute("\\abc9_flop"))
555 box_inputs++;
556
557 std::get<0>(v) = box_inputs;
558 std::get<1>(v) = box_outputs;
559 std::get<2>(v) = box_module->attributes.at("\\abc9_box_id").as_int();
560 }
561
562 write_h_buffer(std::get<0>(v));
563 write_h_buffer(std::get<1>(v));
564 write_h_buffer(std::get<2>(v));
565 write_h_buffer(box_count++);
566 }
567
568 std::stringstream r_buffer;
569 auto write_r_buffer = std::bind(write_buffer, std::ref(r_buffer), std::placeholders::_1);
570 log_debug("flopNum = %d\n", GetSize(ff_bits));
571 write_r_buffer(ff_bits.size());
572
573 std::stringstream s_buffer;
574 auto write_s_buffer = std::bind(write_buffer, std::ref(s_buffer), std::placeholders::_1);
575 write_s_buffer(ff_bits.size());
576
577 for (const auto &i : ff_bits) {
578 const SigBit &d = i.first;
579 const Cell *cell = i.second;
580
581 int mergeability = cell->attributes.at(ID(abc9_mergeability)).as_int();
582 log_assert(mergeability > 0);
583 write_r_buffer(mergeability);
584
585 Const init = cell->attributes.at(ID(abc9_init));
586 log_assert(GetSize(init) == 1);
587 if (init == State::S1)
588 write_s_buffer(1);
589 else if (init == State::S0)
590 write_s_buffer(0);
591 else {
592 log_assert(init == State::Sx);
593 write_s_buffer(0);
594 }
595
596 write_i_buffer(arrival_times.at(d, 0));
597 //write_o_buffer(0);
598 }
599
600 f << "r";
601 std::string buffer_str = r_buffer.str();
602 int32_t buffer_size_be = to_big_endian(buffer_str.size());
603 f.write(reinterpret_cast<const char*>(&buffer_size_be), sizeof(buffer_size_be));
604 f.write(buffer_str.data(), buffer_str.size());
605
606 f << "s";
607 buffer_str = s_buffer.str();
608 buffer_size_be = to_big_endian(buffer_str.size());
609 f.write(reinterpret_cast<const char*>(&buffer_size_be), sizeof(buffer_size_be));
610 f.write(buffer_str.data(), buffer_str.size());
611
612 if (holes_module) {
613 std::stringstream a_buffer;
614 XAigerWriter writer(holes_module, true /* holes_mode */);
615 writer.write_aiger(a_buffer, false /*ascii_mode*/);
616
617 f << "a";
618 std::string buffer_str = a_buffer.str();
619 int32_t buffer_size_be = to_big_endian(buffer_str.size());
620 f.write(reinterpret_cast<const char*>(&buffer_size_be), sizeof(buffer_size_be));
621 f.write(buffer_str.data(), buffer_str.size());
622 }
623 }
624
625 f << "h";
626 std::string buffer_str = h_buffer.str();
627 int32_t buffer_size_be = to_big_endian(buffer_str.size());
628 f.write(reinterpret_cast<const char*>(&buffer_size_be), sizeof(buffer_size_be));
629 f.write(buffer_str.data(), buffer_str.size());
630
631 f << "i";
632 buffer_str = i_buffer.str();
633 buffer_size_be = to_big_endian(buffer_str.size());
634 f.write(reinterpret_cast<const char*>(&buffer_size_be), sizeof(buffer_size_be));
635 f.write(buffer_str.data(), buffer_str.size());
636 //f << "o";
637 //buffer_str = o_buffer.str();
638 //buffer_size_be = to_big_endian(buffer_str.size());
639 //f.write(reinterpret_cast<const char*>(&buffer_size_be), sizeof(buffer_size_be));
640 //f.write(buffer_str.data(), buffer_str.size());
641
642 f << stringf("Generated by %s\n", yosys_version_str);
643
644 module->design->scratchpad_set_int("write_xaiger.num_ands", and_map.size());
645 module->design->scratchpad_set_int("write_xaiger.num_wires", aig_map.size());
646 module->design->scratchpad_set_int("write_xaiger.num_inputs", input_bits.size());
647 module->design->scratchpad_set_int("write_xaiger.num_outputs", output_bits.size());
648 }
649
650 void write_map(std::ostream &f)
651 {
652 dict<int, string> input_lines;
653 dict<int, string> output_lines;
654
655 for (auto wire : module->wires())
656 {
657 SigSpec sig = sigmap(wire);
658
659 for (int i = 0; i < GetSize(wire); i++)
660 {
661 RTLIL::SigBit b(wire, i);
662 if (input_bits.count(b)) {
663 int a = aig_map.at(b);
664 log_assert((a & 1) == 0);
665 input_lines[a] += stringf("input %d %d %s\n", (a >> 1)-1, i, log_id(wire));
666 }
667
668 if (output_bits.count(b)) {
669 int o = ordered_outputs.at(b);
670 int init = 2;
671 output_lines[o] += stringf("output %d %d %s %d\n", o - GetSize(co_bits), i, log_id(wire), init);
672 continue;
673 }
674 }
675 }
676
677 input_lines.sort();
678 for (auto &it : input_lines)
679 f << it.second;
680 log_assert(input_lines.size() == input_bits.size());
681
682 int box_count = 0;
683 for (auto cell : box_list)
684 f << stringf("box %d %d %s\n", box_count++, 0, log_id(cell->name));
685
686 output_lines.sort();
687 for (auto &it : output_lines)
688 f << it.second;
689 log_assert(output_lines.size() == output_bits.size());
690 }
691 };
692
693 struct XAigerBackend : public Backend {
694 XAigerBackend() : Backend("xaiger", "write design to XAIGER file") { }
695 void help() YS_OVERRIDE
696 {
697 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
698 log("\n");
699 log(" write_xaiger [options] [filename]\n");
700 log("\n");
701 log("Write the top module (according to the (* top *) attribute or if only one module\n");
702 log("is currently selected) to an XAIGER file. Any non $_NOT_, $_AND_, $_ABC9_FF_, or");
703 log("non (* abc9_box_id *) cells will be converted into psuedo-inputs and\n");
704 log("pseudo-outputs.\n");
705 log("\n");
706 log(" -ascii\n");
707 log(" write ASCII version of AIGER format\n");
708 log("\n");
709 log(" -map <filename>\n");
710 log(" write an extra file with port and box symbols\n");
711 log("\n");
712 }
713 void execute(std::ostream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
714 {
715 bool ascii_mode = false;
716 std::string map_filename;
717
718 log_header(design, "Executing XAIGER backend.\n");
719
720 size_t argidx;
721 for (argidx = 1; argidx < args.size(); argidx++)
722 {
723 if (args[argidx] == "-ascii") {
724 ascii_mode = true;
725 continue;
726 }
727 if (map_filename.empty() && args[argidx] == "-map" && argidx+1 < args.size()) {
728 map_filename = args[++argidx];
729 continue;
730 }
731 break;
732 }
733 extra_args(f, filename, args, argidx, !ascii_mode);
734
735 Module *top_module = design->top_module();
736
737 if (top_module == nullptr)
738 log_error("Can't find top module in current design!\n");
739
740 if (!design->selected_whole_module(top_module))
741 log_cmd_error("Can't handle partially selected module %s!\n", log_id(top_module));
742
743 if (!top_module->processes.empty())
744 log_error("Found unmapped processes in module %s: unmapped processes are not supported in XAIGER backend!\n", log_id(top_module));
745 if (!top_module->memories.empty())
746 log_error("Found unmapped memories in module %s: unmapped memories are not supported in XAIGER backend!\n", log_id(top_module));
747
748 XAigerWriter writer(top_module);
749 writer.write_aiger(*f, ascii_mode);
750
751 if (!map_filename.empty()) {
752 std::ofstream mapf;
753 mapf.open(map_filename.c_str(), std::ofstream::trunc);
754 if (mapf.fail())
755 log_error("Can't open file `%s' for writing: %s\n", map_filename.c_str(), strerror(errno));
756 writer.write_map(mapf);
757 }
758 }
759 } XAigerBackend;
760
761 PRIVATE_NAMESPACE_END