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/rtlil.h"
21 #include "kernel/register.h"
22 #include "kernel/sigtools.h"
23 #include "kernel/celltypes.h"
24 #include "kernel/log.h"
28 PRIVATE_NAMESPACE_BEGIN
34 RTLIL::Module
*module
;
39 int initstate_nid
= -1;
42 dict
<int, int> sorts_bv
;
44 // (<address-width>, <data-width>) => <sid>
45 dict
<pair
<int, int>, int> sorts_mem
;
47 // SigBit => (<nid>, <bitidx>)
48 dict
<SigBit
, pair
<int, int>> bit_nid
;
51 dict
<int, int> nid_width
;
54 dict
<SigSpec
, int> sig_nid
;
56 // bit to driving cell
57 dict
<SigBit
, Cell
*> bit_cell
;
60 dict
<Const
, int> consts
;
62 // ff inputs that need to be evaluated (<nid>, <ff_cell>)
63 vector
<pair
<int, Cell
*>> ff_todo
;
65 pool
<Cell
*> cell_recursion_guard
;
66 vector
<int> bad_properties
;
67 dict
<SigBit
, bool> initbits
;
68 pool
<Wire
*> statewires
;
71 void btorf(const char *fmt
, ...)
75 f
<< indent
<< vstringf(fmt
, ap
);
79 void btorf_push(const string
&id
)
82 f
<< indent
<< stringf(" ; begin %s\n", id
.c_str());
87 void btorf_pop(const string
&id
)
90 indent
= indent
.substr(4);
91 f
<< indent
<< stringf(" ; end %s\n", id
.c_str());
95 int get_bv_sid(int width
)
97 if (sorts_bv
.count(width
) == 0) {
99 btorf("%d sort bitvec %d\n", nid
, width
);
100 sorts_bv
[width
] = nid
;
102 return sorts_bv
.at(width
);
105 int get_mem_sid(int abits
, int dbits
)
107 pair
<int, int> key(abits
, dbits
);
108 if (sorts_mem
.count(key
) == 0) {
109 int addr_sid
= get_bv_sid(abits
);
110 int data_sid
= get_bv_sid(dbits
);
111 int nid
= next_nid
++;
112 btorf("%d sort array %d %d\n", nid
, addr_sid
, data_sid
);
113 sorts_mem
[key
] = nid
;
115 return sorts_mem
.at(key
);
118 void add_nid_sig(int nid
, const SigSpec
&sig
)
121 f
<< indent
<< stringf("; %d %s\n", nid
, log_signal(sig
));
123 for (int i
= 0; i
< GetSize(sig
); i
++)
124 bit_nid
[sig
[i
]] = make_pair(nid
, i
);
127 nid_width
[nid
] = GetSize(sig
);
130 void export_cell(Cell
*cell
)
132 log_assert(cell_recursion_guard
.count(cell
) == 0);
133 cell_recursion_guard
.insert(cell
);
134 btorf_push(log_id(cell
));
136 if (cell
->type
.in("$add", "$sub", "$and", "$or", "$xor", "$xnor", "$shl", "$sshl", "$shr", "$sshr", "$shift", "$shiftx",
137 "$_AND_", "$_NAND_", "$_OR_", "$_NOR_", "$_XOR_", "$_XNOR_"))
140 if (cell
->type
== "$add") btor_op
= "add";
141 if (cell
->type
== "$sub") btor_op
= "sub";
142 if (cell
->type
.in("$shl", "$sshl")) btor_op
= "sll";
143 if (cell
->type
== "$shr") btor_op
= "srl";
144 if (cell
->type
== "$sshr") btor_op
= "sra";
145 if (cell
->type
.in("$shift", "$shiftx")) btor_op
= "shift";
146 if (cell
->type
.in("$and", "$_AND_")) btor_op
= "and";
147 if (cell
->type
.in("$or", "$_OR_")) btor_op
= "or";
148 if (cell
->type
.in("$xor", "$_XOR_")) btor_op
= "xor";
149 if (cell
->type
== "$_NAND_") btor_op
= "nand";
150 if (cell
->type
== "$_NOR_") btor_op
= "nor";
151 if (cell
->type
.in("$xnor", "$_XNOR_")) btor_op
= "xnor";
152 log_assert(!btor_op
.empty());
154 int width
= GetSize(cell
->getPort("\\Y"));
155 width
= std::max(width
, GetSize(cell
->getPort("\\A")));
156 width
= std::max(width
, GetSize(cell
->getPort("\\B")));
158 bool a_signed
= cell
->hasParam("\\A_SIGNED") ? cell
->getParam("\\A_SIGNED").as_bool() : false;
159 bool b_signed
= cell
->hasParam("\\B_SIGNED") ? cell
->getParam("\\B_SIGNED").as_bool() : false;
161 if (btor_op
== "shift" && !b_signed
)
164 if (cell
->type
.in("$shl", "$sshl", "$shr", "$sshr"))
167 if (cell
->type
== "$sshr" && !a_signed
)
170 int sid
= get_bv_sid(width
);
173 if (btor_op
== "shift")
175 int nid_a
= get_sig_nid(cell
->getPort("\\A"), width
, false);
176 int nid_b
= get_sig_nid(cell
->getPort("\\B"), width
, b_signed
);
178 int nid_r
= next_nid
++;
179 btorf("%d srl %d %d %d\n", nid_r
, sid
, nid_a
, nid_b
);
181 int nid_b_neg
= next_nid
++;
182 btorf("%d neg %d %d\n", nid_b_neg
, sid
, nid_b
);
184 int nid_l
= next_nid
++;
185 btorf("%d sll %d %d %d\n", nid_l
, sid
, nid_a
, nid_b_neg
);
187 int sid_bit
= get_bv_sid(1);
188 int nid_zero
= get_sig_nid(Const(0, width
));
189 int nid_b_ltz
= next_nid
++;
190 btorf("%d slt %d %d %d\n", nid_b_ltz
, sid_bit
, nid_b
, nid_zero
);
193 btorf("%d ite %d %d %d %d\n", nid
, sid
, nid_b_ltz
, nid_l
, nid_r
);
197 int nid_a
= get_sig_nid(cell
->getPort("\\A"), width
, a_signed
);
198 int nid_b
= get_sig_nid(cell
->getPort("\\B"), width
, b_signed
);
201 btorf("%d %s %d %d %d\n", nid
, btor_op
.c_str(), sid
, nid_a
, nid_b
);
204 SigSpec sig
= sigmap(cell
->getPort("\\Y"));
206 if (GetSize(sig
) < width
) {
207 int sid
= get_bv_sid(GetSize(sig
));
208 int nid2
= next_nid
++;
209 btorf("%d slice %d %d %d 0\n", nid2
, sid
, nid
, GetSize(sig
)-1);
213 add_nid_sig(nid
, sig
);
217 if (cell
->type
.in("$_ANDNOT_", "$_ORNOT_"))
219 int sid
= get_bv_sid(1);
220 int nid_a
= get_sig_nid(cell
->getPort("\\A"));
221 int nid_b
= get_sig_nid(cell
->getPort("\\B"));
223 int nid1
= next_nid
++;
224 int nid2
= next_nid
++;
226 if (cell
->type
== "$_ANDNOT_") {
227 btorf("%d not %d %d\n", nid1
, sid
, nid_b
);
228 btorf("%d and %d %d %d\n", nid2
, sid
, nid_a
, nid1
);
231 if (cell
->type
== "$_ORNOT_") {
232 btorf("%d not %d %d\n", nid1
, sid
, nid_b
);
233 btorf("%d or %d %d %d\n", nid2
, sid
, nid_a
, nid1
);
236 SigSpec sig
= sigmap(cell
->getPort("\\Y"));
237 add_nid_sig(nid2
, sig
);
241 if (cell
->type
.in("$_OAI3_", "$_AOI3_"))
243 int sid
= get_bv_sid(1);
244 int nid_a
= get_sig_nid(cell
->getPort("\\A"));
245 int nid_b
= get_sig_nid(cell
->getPort("\\B"));
246 int nid_c
= get_sig_nid(cell
->getPort("\\C"));
248 int nid1
= next_nid
++;
249 int nid2
= next_nid
++;
250 int nid3
= next_nid
++;
252 if (cell
->type
== "$_OAI3_") {
253 btorf("%d or %d %d %d\n", nid1
, sid
, nid_a
, nid_b
);
254 btorf("%d and %d %d %d\n", nid2
, sid
, nid1
, nid_c
);
255 btorf("%d not %d %d\n", nid3
, sid
, nid2
);
258 if (cell
->type
== "$_AOI3_") {
259 btorf("%d and %d %d %d\n", nid1
, sid
, nid_a
, nid_b
);
260 btorf("%d or %d %d %d\n", nid2
, sid
, nid1
, nid_c
);
261 btorf("%d not %d %d\n", nid3
, sid
, nid2
);
264 SigSpec sig
= sigmap(cell
->getPort("\\Y"));
265 add_nid_sig(nid3
, sig
);
269 if (cell
->type
.in("$_OAI4_", "$_AOI4_"))
271 int sid
= get_bv_sid(1);
272 int nid_a
= get_sig_nid(cell
->getPort("\\A"));
273 int nid_b
= get_sig_nid(cell
->getPort("\\B"));
274 int nid_c
= get_sig_nid(cell
->getPort("\\C"));
275 int nid_d
= get_sig_nid(cell
->getPort("\\D"));
277 int nid1
= next_nid
++;
278 int nid2
= next_nid
++;
279 int nid3
= next_nid
++;
280 int nid4
= next_nid
++;
282 if (cell
->type
== "$_OAI4_") {
283 btorf("%d or %d %d %d\n", nid1
, sid
, nid_a
, nid_b
);
284 btorf("%d or %d %d %d\n", nid2
, sid
, nid_c
, nid_d
);
285 btorf("%d and %d %d %d\n", nid3
, sid
, nid1
, nid2
);
286 btorf("%d not %d %d\n", nid4
, sid
, nid3
);
289 if (cell
->type
== "$_AOI4_") {
290 btorf("%d and %d %d %d\n", nid1
, sid
, nid_a
, nid_b
);
291 btorf("%d and %d %d %d\n", nid2
, sid
, nid_c
, nid_d
);
292 btorf("%d or %d %d %d\n", nid3
, sid
, nid1
, nid2
);
293 btorf("%d not %d %d\n", nid4
, sid
, nid3
);
296 SigSpec sig
= sigmap(cell
->getPort("\\Y"));
297 add_nid_sig(nid4
, sig
);
301 if (cell
->type
.in("$lt", "$le", "$eq", "$eqx", "$ne", "$nex", "$ge", "$gt"))
304 if (cell
->type
== "$lt") btor_op
= "lt";
305 if (cell
->type
== "$le") btor_op
= "lte";
306 if (cell
->type
.in("$eq", "$eqx")) btor_op
= "eq";
307 if (cell
->type
.in("$ne", "$nex")) btor_op
= "ne";
308 if (cell
->type
== "$ge") btor_op
= "gte";
309 if (cell
->type
== "$gt") btor_op
= "gt";
310 log_assert(!btor_op
.empty());
313 width
= std::max(width
, GetSize(cell
->getPort("\\A")));
314 width
= std::max(width
, GetSize(cell
->getPort("\\B")));
316 bool a_signed
= cell
->hasParam("\\A_SIGNED") ? cell
->getParam("\\A_SIGNED").as_bool() : false;
317 bool b_signed
= cell
->hasParam("\\B_SIGNED") ? cell
->getParam("\\B_SIGNED").as_bool() : false;
319 int sid
= get_bv_sid(1);
320 int nid_a
= get_sig_nid(cell
->getPort("\\A"), width
, a_signed
);
321 int nid_b
= get_sig_nid(cell
->getPort("\\B"), width
, b_signed
);
323 int nid
= next_nid
++;
324 if (cell
->type
.in("$lt", "$le", "$ge", "$gt")) {
325 btorf("%d %c%s %d %d %d\n", nid
, a_signed
|| b_signed
? 's' : 'u', btor_op
.c_str(), sid
, nid_a
, nid_b
);
327 btorf("%d %s %d %d %d\n", nid
, btor_op
.c_str(), sid
, nid_a
, nid_b
);
330 SigSpec sig
= sigmap(cell
->getPort("\\Y"));
332 if (GetSize(sig
) > 1) {
333 int sid
= get_bv_sid(GetSize(sig
));
334 int nid2
= next_nid
++;
335 btorf("%d uext %d %d %d\n", nid2
, sid
, nid
, GetSize(sig
) - 1);
339 add_nid_sig(nid
, sig
);
343 if (cell
->type
.in("$not", "$neg", "$_NOT_"))
346 if (cell
->type
.in("$not", "$_NOT_")) btor_op
= "not";
347 if (cell
->type
== "$neg") btor_op
= "neg";
348 log_assert(!btor_op
.empty());
350 int width
= GetSize(cell
->getPort("\\Y"));
351 width
= std::max(width
, GetSize(cell
->getPort("\\A")));
353 bool a_signed
= cell
->hasParam("\\A_SIGNED") ? cell
->getParam("\\A_SIGNED").as_bool() : false;
355 int sid
= get_bv_sid(width
);
356 int nid_a
= get_sig_nid(cell
->getPort("\\A"), width
, a_signed
);
358 int nid
= next_nid
++;
359 btorf("%d %s %d %d\n", nid
, btor_op
.c_str(), sid
, nid_a
);
361 SigSpec sig
= sigmap(cell
->getPort("\\Y"));
363 if (GetSize(sig
) < width
) {
364 int sid
= get_bv_sid(GetSize(sig
));
365 int nid2
= next_nid
++;
366 btorf("%d slice %d %d %d 0\n", nid2
, sid
, nid
, GetSize(sig
)-1);
370 add_nid_sig(nid
, sig
);
374 if (cell
->type
.in("$logic_and", "$logic_or", "$logic_not"))
377 if (cell
->type
== "$logic_and") btor_op
= "and";
378 if (cell
->type
== "$logic_or") btor_op
= "or";
379 if (cell
->type
== "$logic_not") btor_op
= "not";
380 log_assert(!btor_op
.empty());
382 int sid
= get_bv_sid(1);
383 int nid_a
= get_sig_nid(cell
->getPort("\\A"));
384 int nid_b
= btor_op
!= "not" ? get_sig_nid(cell
->getPort("\\B")) : 0;
386 if (GetSize(cell
->getPort("\\A")) > 1) {
387 int nid_red_a
= next_nid
++;
388 btorf("%d redor %d %d\n", nid_red_a
, sid
, nid_a
);
392 if (btor_op
!= "not" && GetSize(cell
->getPort("\\B")) > 1) {
393 int nid_red_b
= next_nid
++;
394 btorf("%d redor %d %d\n", nid_red_b
, sid
, nid_b
);
398 int nid
= next_nid
++;
399 if (btor_op
!= "not")
400 btorf("%d %s %d %d %d\n", nid
, btor_op
.c_str(), sid
, nid_a
, nid_b
);
402 btorf("%d %s %d %d\n", nid
, btor_op
.c_str(), sid
, nid_a
);
404 SigSpec sig
= sigmap(cell
->getPort("\\Y"));
406 if (GetSize(sig
) > 1) {
407 int sid
= get_bv_sid(GetSize(sig
));
408 int zeros_nid
= get_sig_nid(Const(0, GetSize(sig
)-1));
409 int nid2
= next_nid
++;
410 btorf("%d concat %d %d %d\n", nid2
, sid
, zeros_nid
, nid
);
414 add_nid_sig(nid
, sig
);
418 if (cell
->type
.in("$reduce_and", "$reduce_or", "$reduce_bool", "$reduce_xor", "$reduce_xnor"))
421 if (cell
->type
== "$reduce_and") btor_op
= "redand";
422 if (cell
->type
.in("$reduce_or", "$reduce_bool")) btor_op
= "redor";
423 if (cell
->type
.in("$reduce_xor", "$reduce_xnor")) btor_op
= "redxor";
424 log_assert(!btor_op
.empty());
426 int sid
= get_bv_sid(1);
427 int nid_a
= get_sig_nid(cell
->getPort("\\A"));
429 int nid
= next_nid
++;
430 btorf("%d %s %d %d\n", nid
, btor_op
.c_str(), sid
, nid_a
);
432 if (cell
->type
== "$reduce_xnor") {
433 int nid2
= next_nid
++;
434 btorf("%d not %d %d %d\n", nid2
, sid
, nid
);
438 SigSpec sig
= sigmap(cell
->getPort("\\Y"));
440 if (GetSize(sig
) > 1) {
441 int sid
= get_bv_sid(GetSize(sig
));
442 int zeros_nid
= get_sig_nid(Const(0, GetSize(sig
)-1));
443 int nid2
= next_nid
++;
444 btorf("%d concat %d %d %d\n", nid2
, sid
, zeros_nid
, nid
);
448 add_nid_sig(nid
, sig
);
452 if (cell
->type
.in("$mux", "$_MUX_"))
454 SigSpec sig_a
= sigmap(cell
->getPort("\\A"));
455 SigSpec sig_b
= sigmap(cell
->getPort("\\B"));
456 SigSpec sig_s
= sigmap(cell
->getPort("\\S"));
457 SigSpec sig_y
= sigmap(cell
->getPort("\\Y"));
459 int nid_a
= get_sig_nid(sig_a
);
460 int nid_b
= get_sig_nid(sig_b
);
461 int nid_s
= get_sig_nid(sig_s
);
463 int sid
= get_bv_sid(GetSize(sig_y
));
464 int nid
= next_nid
++;
465 btorf("%d ite %d %d %d %d\n", nid
, sid
, nid_s
, nid_b
, nid_a
);
467 add_nid_sig(nid
, sig_y
);
471 if (cell
->type
== "$pmux")
473 SigSpec sig_a
= sigmap(cell
->getPort("\\A"));
474 SigSpec sig_b
= sigmap(cell
->getPort("\\B"));
475 SigSpec sig_s
= sigmap(cell
->getPort("\\S"));
476 SigSpec sig_y
= sigmap(cell
->getPort("\\Y"));
478 int width
= GetSize(sig_a
);
479 int sid
= get_bv_sid(width
);
480 int nid
= get_sig_nid(sig_a
);
482 for (int i
= 0; i
< GetSize(sig_s
); i
++) {
483 int nid_b
= get_sig_nid(sig_b
.extract(i
*width
, width
));
484 int nid_s
= get_sig_nid(sig_s
.extract(i
));
485 int nid2
= next_nid
++;
486 btorf("%d ite %d %d %d %d\n", nid2
, sid
, nid_s
, nid_b
, nid
);
490 add_nid_sig(nid
, sig_y
);
494 if (cell
->type
.in("$dff", "$ff", "$_DFF_P_", "$_DFF_N", "$_FF_"))
496 SigSpec sig_d
= sigmap(cell
->getPort("\\D"));
497 SigSpec sig_q
= sigmap(cell
->getPort("\\Q"));
501 if (sig_q
.is_wire()) {
502 Wire
*w
= sig_q
.as_wire();
503 if (w
->port_id
== 0) {
504 statewires
.insert(w
);
509 int sid
= get_bv_sid(GetSize(sig_q
));
510 int nid
= next_nid
++;
513 btorf("%d state %d\n", nid
, sid
);
515 btorf("%d state %d %s\n", nid
, sid
, log_id(symbol
));
518 for (int i
= 0; i
< GetSize(sig_q
); i
++)
519 if (initbits
.count(sig_q
[i
]))
520 initval
.bits
.push_back(initbits
.at(sig_q
[i
]) ? State::S1
: State::S0
);
522 initval
.bits
.push_back(State::Sx
);
524 if (!initval
.is_fully_undef()) {
525 int nid_init_val
= get_sig_nid(initval
);
526 int nid_init
= next_nid
++;
528 btorf("; initval = %s\n", log_signal(initval
));
529 btorf("%d init %d %d %d\n", nid_init
, sid
, nid
, nid_init_val
);
532 ff_todo
.push_back(make_pair(nid
, cell
));
533 add_nid_sig(nid
, sig_q
);
537 if (cell
->type
.in("$anyconst", "$anyseq"))
539 SigSpec sig_y
= sigmap(cell
->getPort("\\Y"));
541 int sid
= get_bv_sid(GetSize(sig_y
));
542 int nid
= next_nid
++;
544 btorf("%d state %d\n", nid
, sid
);
546 if (cell
->type
== "$anyconst") {
547 int nid2
= next_nid
++;
548 btorf("%d next %d %d %d\n", nid2
, sid
, nid
, nid
);
551 add_nid_sig(nid
, sig_y
);
555 if (cell
->type
== "$initstate")
557 SigSpec sig_y
= sigmap(cell
->getPort("\\Y"));
559 if (initstate_nid
< 0)
561 int sid
= get_bv_sid(1);
562 int one_nid
= get_sig_nid(Const(1, 1));
563 int zero_nid
= get_sig_nid(Const(0, 1));
564 initstate_nid
= next_nid
++;
565 btorf("%d state %d\n", initstate_nid
, sid
);
566 btorf("%d init %d %d %d\n", next_nid
++, sid
, initstate_nid
, one_nid
);
567 btorf("%d next %d %d %d\n", next_nid
++, sid
, initstate_nid
, zero_nid
);
570 add_nid_sig(initstate_nid
, sig_y
);
574 if (cell
->type
== "$mem")
576 int abits
= cell
->getParam("\\ABITS").as_int();
577 int width
= cell
->getParam("\\WIDTH").as_int();
578 int rdports
= cell
->getParam("\\RD_PORTS").as_int();
579 int wrports
= cell
->getParam("\\WR_PORTS").as_int();
581 Const wr_clk_en
= cell
->getParam("\\WR_CLK_ENABLE");
582 Const rd_clk_en
= cell
->getParam("\\RD_CLK_ENABLE");
584 bool asyncwr
= wr_clk_en
.is_fully_zero();
586 if (!asyncwr
&& !wr_clk_en
.is_fully_ones())
587 log_error("Memory %s.%s has mixed async/sync write ports.\n",
588 log_id(module
), log_id(cell
));
590 if (!rd_clk_en
.is_fully_zero())
591 log_error("Memory %s.%s has sync read ports.\n",
592 log_id(module
), log_id(cell
));
594 SigSpec sig_rd_addr
= sigmap(cell
->getPort("\\RD_ADDR"));
595 SigSpec sig_rd_data
= sigmap(cell
->getPort("\\RD_DATA"));
597 SigSpec sig_wr_addr
= sigmap(cell
->getPort("\\WR_ADDR"));
598 SigSpec sig_wr_data
= sigmap(cell
->getPort("\\WR_DATA"));
599 SigSpec sig_wr_en
= sigmap(cell
->getPort("\\WR_EN"));
601 int data_sid
= get_bv_sid(width
);
602 int bool_sid
= get_bv_sid(1);
603 int sid
= get_mem_sid(abits
, width
);
604 int nid
= next_nid
++;
607 if (cell
->name
[0] == '$')
608 btorf("%d state %d\n", nid
, sid
);
610 btorf("%d state %d %s\n", nid
, sid
, log_id(cell
));
614 for (int port
= 0; port
< wrports
; port
++)
616 SigSpec wa
= sig_wr_addr
.extract(port
*abits
, abits
);
617 SigSpec wd
= sig_wr_data
.extract(port
*width
, width
);
618 SigSpec we
= sig_wr_en
.extract(port
*width
, width
);
620 int wa_nid
= get_sig_nid(wa
);
621 int wd_nid
= get_sig_nid(wd
);
622 int we_nid
= get_sig_nid(we
);
624 int nid2
= next_nid
++;
625 btorf("%d read %d %d %d\n", nid2
, data_sid
, nid_head
, wa_nid
);
627 int nid3
= next_nid
++;
628 btorf("%d not %d %d\n", nid3
, data_sid
, we_nid
);
630 int nid4
= next_nid
++;
631 btorf("%d and %d %d %d\n", nid4
, data_sid
, nid2
, nid3
);
633 int nid5
= next_nid
++;
634 btorf("%d and %d %d %d\n", nid5
, data_sid
, wd_nid
, we_nid
);
636 int nid6
= next_nid
++;
637 btorf("%d or %d %d %d\n", nid6
, data_sid
, nid5
, nid4
);
639 int nid7
= next_nid
++;
640 btorf("%d write %d %d %d %d\n", nid7
, sid
, nid_head
, wa_nid
, nid6
);
642 int nid8
= next_nid
++;
643 btorf("%d redor %d %d\n", nid8
, bool_sid
, we_nid
);
645 int nid9
= next_nid
++;
646 btorf("%d ite %d %d %d %d\n", nid9
, sid
, nid8
, nid7
, nid_head
);
652 for (int port
= 0; port
< rdports
; port
++)
654 SigSpec ra
= sig_rd_addr
.extract(port
*abits
, abits
);
655 SigSpec rd
= sig_rd_data
.extract(port
*width
, width
);
657 int ra_nid
= get_sig_nid(ra
);
658 int rd_nid
= next_nid
++;
660 btorf("%d read %d %d %d\n", rd_nid
, data_sid
, nid_head
, ra_nid
);
662 add_nid_sig(rd_nid
, rd
);
667 ff_todo
.push_back(make_pair(nid
, cell
));
671 int nid2
= next_nid
++;
672 btorf("%d next %d %d %d\n", nid2
, sid
, nid
, nid_head
);
678 log_error("Unsupported cell type: %s (%s)\n", log_id(cell
->type
), log_id(cell
));
681 btorf_pop(log_id(cell
));
682 cell_recursion_guard
.erase(cell
);
685 int get_sig_nid(SigSpec sig
, int to_width
= -1, bool is_signed
= false)
691 if (bit
== State::Sx
)
697 SigSpec sig_mask_undef
, sig_noundef
;
698 int first_undef
= -1;
700 for (int i
= 0; i
< GetSize(sig
); i
++)
701 if (sig
[i
] == State::Sx
) {
704 sig_mask_undef
.append(State::S1
);
705 sig_noundef
.append(State::S0
);
707 sig_mask_undef
.append(State::S0
);
708 sig_noundef
.append(sig
[i
]);
711 if (to_width
< 0 || first_undef
< to_width
)
713 int sid
= get_bv_sid(GetSize(sig
));
715 int nid_input
= next_nid
++;
716 btorf("%d input %d\n", nid_input
, sid
);
718 int nid_masked_input
;
719 if (sig_mask_undef
.is_fully_ones()) {
720 nid_masked_input
= nid_input
;
722 int nid_mask_undef
= get_sig_nid(sig_mask_undef
);
723 nid_masked_input
= next_nid
++;
724 btorf("%d and %d %d %d\n", nid_masked_input
, sid
, nid_input
, nid_mask_undef
);
727 if (sig_noundef
.is_fully_zero()) {
728 nid
= nid_masked_input
;
730 int nid_noundef
= get_sig_nid(sig_noundef
);
732 btorf("%d or %d %d %d\n", nid
, sid
, nid_masked_input
, nid_noundef
);
741 if (sig_nid
.count(sig
) == 0)
744 vector
<pair
<int, int>> nidbits
;
747 for (int i
= 0; i
< GetSize(sig
); i
++)
751 if (bit_nid
.count(bit
) == 0)
753 if (bit
.wire
== nullptr)
757 while (i
+GetSize(c
) < GetSize(sig
) && sig
[i
+GetSize(c
)].wire
== nullptr)
758 c
.bits
.push_back(sig
[i
+GetSize(c
)].data
);
760 if (consts
.count(c
) == 0) {
761 int sid
= get_bv_sid(GetSize(c
));
762 int nid
= next_nid
++;
763 btorf("%d const %d %s\n", nid
, sid
, c
.as_string().c_str());
765 nid_width
[nid
] = GetSize(c
);
768 int nid
= consts
.at(c
);
770 for (int j
= 0; j
< GetSize(c
); j
++)
771 nidbits
.push_back(make_pair(nid
, j
));
778 export_cell(bit_cell
.at(bit
));
779 log_assert(bit_nid
.count(bit
));
783 nidbits
.push_back(bit_nid
.at(bit
));
789 // group bits and emit slice-concat chain
790 for (int i
= 0; i
< GetSize(nidbits
); i
++)
792 int nid2
= nidbits
[i
].first
;
793 int lower
= nidbits
[i
].second
;
796 while (i
+1 < GetSize(nidbits
) && nidbits
[i
+1].first
== nidbits
[i
].first
&&
797 nidbits
[i
+1].second
== nidbits
[i
].second
+1)
802 if (lower
!= 0 || upper
+1 != nid_width
.at(nid2
)) {
803 int sid
= get_bv_sid(upper
-lower
+1);
805 btorf("%d slice %d %d %d %d\n", nid3
, sid
, nid2
, upper
, lower
);
811 int sid
= get_bv_sid(width
+upper
-lower
+1);
813 btorf("%d concat %d %d %d\n", nid4
, sid
, nid3
, nid
);
816 width
+= upper
-lower
+1;
821 nid_width
[nid
] = width
;
824 nid
= sig_nid
.at(sig
);
827 if (to_width
>= 0 && to_width
!= GetSize(sig
))
829 if (to_width
< GetSize(sig
))
831 int sid
= get_bv_sid(to_width
);
832 int nid2
= next_nid
++;
833 btorf("%d slice %d %d %d 0\n", nid2
, sid
, nid
, to_width
-1);
838 int sid
= get_bv_sid(to_width
);
839 int nid2
= next_nid
++;
840 btorf("%d %s %d %d %d\n", nid2
, is_signed
? "sext" : "uext",
841 sid
, nid
, to_width
- GetSize(sig
));
849 BtorWorker(std::ostream
&f
, RTLIL::Module
*module
, bool verbose
, bool single_bad
) :
850 f(f
), sigmap(module
), module(module
), verbose(verbose
), single_bad(single_bad
)
852 btorf_push("inputs");
854 for (auto wire
: module
->wires())
856 if (wire
->attributes
.count("\\init")) {
857 Const attrval
= wire
->attributes
.at("\\init");
858 for (int i
= 0; i
< GetSize(wire
) && i
< GetSize(attrval
); i
++)
859 if (attrval
[i
] == State::S0
|| attrval
[i
] == State::S1
)
860 initbits
[sigmap(SigBit(wire
, i
))] = (attrval
[i
] == State::S1
);
863 if (!wire
->port_id
|| !wire
->port_input
)
866 SigSpec sig
= sigmap(wire
);
867 int sid
= get_bv_sid(GetSize(sig
));
868 int nid
= next_nid
++;
870 btorf("%d input %d %s\n", nid
, sid
, log_id(wire
));
871 add_nid_sig(nid
, sig
);
876 for (auto cell
: module
->cells())
877 for (auto &conn
: cell
->connections())
879 if (!cell
->output(conn
.first
))
882 for (auto bit
: sigmap(conn
.second
))
883 bit_cell
[bit
] = cell
;
886 for (auto wire
: module
->wires())
888 if (!wire
->port_id
|| !wire
->port_output
)
891 btorf_push(stringf("output %s", log_id(wire
)));
893 int sid
= get_bv_sid(GetSize(wire
));
894 int nid
= get_sig_nid(wire
);
895 btorf("%d output %d %d %s\n", next_nid
++, sid
, nid
, log_id(wire
));
897 btorf_pop(stringf("output %s", log_id(wire
)));
900 for (auto cell
: module
->cells())
902 if (cell
->type
== "$assume")
904 btorf_push(log_id(cell
));
906 int sid
= get_bv_sid(1);
907 int nid_a
= get_sig_nid(cell
->getPort("\\A"));
908 int nid_en
= get_sig_nid(cell
->getPort("\\EN"));
909 int nid_not_en
= next_nid
++;
910 int nid_a_or_not_en
= next_nid
++;
911 int nid
= next_nid
++;
913 btorf("%d not %d %d\n", nid_not_en
, sid
, nid_en
);
914 btorf("%d or %d %d %d\n", nid_a_or_not_en
, sid
, nid_a
, nid_not_en
);
915 btorf("%d constraint %d\n", nid
, nid_a_or_not_en
);
917 btorf_pop(log_id(cell
));
920 if (cell
->type
== "$assert")
922 btorf_push(log_id(cell
));
924 int sid
= get_bv_sid(1);
925 int nid_a
= get_sig_nid(cell
->getPort("\\A"));
926 int nid_en
= get_sig_nid(cell
->getPort("\\EN"));
927 int nid_not_a
= next_nid
++;
928 int nid_en_and_not_a
= next_nid
++;
930 btorf("%d not %d %d\n", nid_not_a
, sid
, nid_a
);
931 btorf("%d and %d %d %d\n", nid_en_and_not_a
, sid
, nid_en
, nid_not_a
);
934 bad_properties
.push_back(nid_en_and_not_a
);
936 int nid
= next_nid
++;
937 btorf("%d bad %d\n", nid
, nid_en_and_not_a
);
940 btorf_pop(log_id(cell
));
944 for (auto wire
: module
->wires())
946 if (wire
->port_id
|| wire
->name
[0] == '$')
949 btorf_push(stringf("wire %s", log_id(wire
)));
951 int sid
= get_bv_sid(GetSize(wire
));
952 int nid
= get_sig_nid(sigmap(wire
));
954 if (statewires
.count(wire
))
957 int this_nid
= next_nid
++;
958 btorf("%d uext %d %d %d %s\n", this_nid
, sid
, nid
, 0, log_id(wire
));
960 btorf_pop(stringf("wire %s", log_id(wire
)));
964 while (!ff_todo
.empty())
966 vector
<pair
<int, Cell
*>> todo
;
969 for (auto &it
: todo
)
972 Cell
*cell
= it
.second
;
974 btorf_push(stringf("next %s", log_id(cell
)));
976 if (cell
->type
== "$mem")
978 int abits
= cell
->getParam("\\ABITS").as_int();
979 int width
= cell
->getParam("\\WIDTH").as_int();
980 int wrports
= cell
->getParam("\\WR_PORTS").as_int();
982 SigSpec sig_wr_addr
= sigmap(cell
->getPort("\\WR_ADDR"));
983 SigSpec sig_wr_data
= sigmap(cell
->getPort("\\WR_DATA"));
984 SigSpec sig_wr_en
= sigmap(cell
->getPort("\\WR_EN"));
986 int data_sid
= get_bv_sid(width
);
987 int bool_sid
= get_bv_sid(1);
988 int sid
= get_mem_sid(abits
, width
);
991 for (int port
= 0; port
< wrports
; port
++)
993 SigSpec wa
= sig_wr_addr
.extract(port
*abits
, abits
);
994 SigSpec wd
= sig_wr_data
.extract(port
*width
, width
);
995 SigSpec we
= sig_wr_en
.extract(port
*width
, width
);
997 int wa_nid
= get_sig_nid(wa
);
998 int wd_nid
= get_sig_nid(wd
);
999 int we_nid
= get_sig_nid(we
);
1001 int nid2
= next_nid
++;
1002 btorf("%d read %d %d %d\n", nid2
, data_sid
, nid_head
, wa_nid
);
1004 int nid3
= next_nid
++;
1005 btorf("%d not %d %d\n", nid3
, data_sid
, we_nid
);
1007 int nid4
= next_nid
++;
1008 btorf("%d and %d %d %d\n", nid4
, data_sid
, nid2
, nid3
);
1010 int nid5
= next_nid
++;
1011 btorf("%d and %d %d %d\n", nid5
, data_sid
, wd_nid
, we_nid
);
1013 int nid6
= next_nid
++;
1014 btorf("%d or %d %d %d\n", nid6
, data_sid
, nid5
, nid4
);
1016 int nid7
= next_nid
++;
1017 btorf("%d write %d %d %d %d\n", nid7
, sid
, nid_head
, wa_nid
, nid6
);
1019 int nid8
= next_nid
++;
1020 btorf("%d redor %d %d\n", nid8
, bool_sid
, we_nid
);
1022 int nid9
= next_nid
++;
1023 btorf("%d ite %d %d %d %d\n", nid9
, sid
, nid8
, nid7
, nid_head
);
1028 int nid2
= next_nid
++;
1029 btorf("%d next %d %d %d\n", nid2
, sid
, nid
, nid_head
);
1033 SigSpec sig
= sigmap(cell
->getPort("\\D"));
1034 int nid_q
= get_sig_nid(sig
);
1035 int sid
= get_bv_sid(GetSize(sig
));
1036 btorf("%d next %d %d %d\n", next_nid
++, sid
, nid
, nid_q
);
1039 btorf_pop(stringf("next %s", log_id(cell
)));
1043 while (!bad_properties
.empty())
1046 bad_properties
.swap(todo
);
1048 int sid
= get_bv_sid(1);
1051 while (cursor
+1 < GetSize(todo
))
1053 int nid_a
= todo
[cursor
++];
1054 int nid_b
= todo
[cursor
++];
1055 int nid
= next_nid
++;
1057 bad_properties
.push_back(nid
);
1058 btorf("%d or %d %d %d\n", nid
, sid
, nid_a
, nid_b
);
1061 if (!bad_properties
.empty()) {
1062 if (cursor
< GetSize(todo
))
1063 bad_properties
.push_back(todo
[cursor
++]);
1064 log_assert(cursor
== GetSize(todo
));
1066 int nid
= next_nid
++;
1067 log_assert(cursor
== 0);
1068 log_assert(GetSize(todo
) == 1);
1069 btorf("%d bad %d\n", nid
, todo
[cursor
]);
1075 struct BtorBackend
: public Backend
{
1076 BtorBackend() : Backend("btor", "write design to BTOR file") { }
1079 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
1081 log(" write_btor [options] [filename]\n");
1083 log("Write a BTOR description of the current design.\n");
1086 log(" Add comments and indentation to BTOR output file\n");
1089 log(" Output only a single bad property for all asserts\n");
1092 virtual void execute(std::ostream
*&f
, std::string filename
, std::vector
<std::string
> args
, RTLIL::Design
*design
)
1094 bool verbose
= false, single_bad
= false;
1096 log_header(design
, "Executing BTOR backend.\n");
1099 for (argidx
= 1; argidx
< args
.size(); argidx
++)
1101 if (args
[argidx
] == "-v") {
1105 if (args
[argidx
] == "-s") {
1111 extra_args(f
, filename
, args
, argidx
);
1113 RTLIL::Module
*topmod
= design
->top_module();
1115 if (topmod
== nullptr)
1116 log_cmd_error("No top module found.\n");
1118 *f
<< stringf("; BTOR description generated by %s for module %s.\n",
1119 yosys_version_str
, log_id(topmod
));
1121 BtorWorker(*f
, topmod
, verbose
, single_bad
);
1123 *f
<< stringf("; end of yosys output\n");
1127 PRIVATE_NAMESPACE_END