Disabled RTLIL::SigSpec::check() in release builds
[yosys.git] / kernel / rtlil.cc
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5 *
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.
9 *
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.
17 *
18 */
19
20 #include "kernel/compatibility.h"
21 #include "kernel/rtlil.h"
22 #include "kernel/log.h"
23 #include "frontends/verilog/verilog_frontend.h"
24 #include "backends/ilang/ilang_backend.h"
25
26 #include <assert.h>
27 #include <string.h>
28 #include <algorithm>
29
30 int RTLIL::autoidx = 1;
31
32 RTLIL::Const::Const()
33 {
34 flags = RTLIL::CONST_FLAG_NONE;
35 }
36
37 RTLIL::Const::Const(std::string str)
38 {
39 flags = RTLIL::CONST_FLAG_STRING;
40 for (int i = str.size()-1; i >= 0; i--) {
41 unsigned char ch = str[i];
42 for (int j = 0; j < 8; j++) {
43 bits.push_back((ch & 1) != 0 ? RTLIL::S1 : RTLIL::S0);
44 ch = ch >> 1;
45 }
46 }
47 }
48
49 RTLIL::Const::Const(int val, int width)
50 {
51 flags = RTLIL::CONST_FLAG_NONE;
52 for (int i = 0; i < width; i++) {
53 bits.push_back((val & 1) != 0 ? RTLIL::S1 : RTLIL::S0);
54 val = val >> 1;
55 }
56 }
57
58 RTLIL::Const::Const(RTLIL::State bit, int width)
59 {
60 flags = RTLIL::CONST_FLAG_NONE;
61 for (int i = 0; i < width; i++)
62 bits.push_back(bit);
63 }
64
65 bool RTLIL::Const::operator <(const RTLIL::Const &other) const
66 {
67 if (bits.size() != other.bits.size())
68 return bits.size() < other.bits.size();
69 for (size_t i = 0; i < bits.size(); i++)
70 if (bits[i] != other.bits[i])
71 return bits[i] < other.bits[i];
72 return false;
73 }
74
75 bool RTLIL::Const::operator ==(const RTLIL::Const &other) const
76 {
77 return bits == other.bits;
78 }
79
80 bool RTLIL::Const::operator !=(const RTLIL::Const &other) const
81 {
82 return bits != other.bits;
83 }
84
85 bool RTLIL::Const::as_bool() const
86 {
87 for (size_t i = 0; i < bits.size(); i++)
88 if (bits[i] == RTLIL::S1)
89 return true;
90 return false;
91 }
92
93 int RTLIL::Const::as_int() const
94 {
95 int ret = 0;
96 for (size_t i = 0; i < bits.size() && i < 32; i++)
97 if (bits[i] == RTLIL::S1)
98 ret |= 1 << i;
99 return ret;
100 }
101
102 std::string RTLIL::Const::as_string() const
103 {
104 std::string ret;
105 for (size_t i = bits.size(); i > 0; i--)
106 switch (bits[i-1]) {
107 case S0: ret += "0"; break;
108 case S1: ret += "1"; break;
109 case Sx: ret += "x"; break;
110 case Sz: ret += "z"; break;
111 case Sa: ret += "-"; break;
112 case Sm: ret += "m"; break;
113 }
114 return ret;
115 }
116
117 std::string RTLIL::Const::decode_string() const
118 {
119 std::string string;
120 std::vector <char> string_chars;
121 for (int i = 0; i < int (bits.size()); i += 8) {
122 char ch = 0;
123 for (int j = 0; j < 8 && i + j < int (bits.size()); j++)
124 if (bits[i + j] == RTLIL::State::S1)
125 ch |= 1 << j;
126 if (ch != 0)
127 string_chars.push_back(ch);
128 }
129 for (int i = int (string_chars.size()) - 1; i >= 0; i--)
130 string += string_chars[i];
131 return string;
132 }
133
134 bool RTLIL::Selection::selected_module(RTLIL::IdString mod_name) const
135 {
136 if (full_selection)
137 return true;
138 if (selected_modules.count(mod_name) > 0)
139 return true;
140 if (selected_members.count(mod_name) > 0)
141 return true;
142 return false;
143 }
144
145 bool RTLIL::Selection::selected_whole_module(RTLIL::IdString mod_name) const
146 {
147 if (full_selection)
148 return true;
149 if (selected_modules.count(mod_name) > 0)
150 return true;
151 return false;
152 }
153
154 bool RTLIL::Selection::selected_member(RTLIL::IdString mod_name, RTLIL::IdString memb_name) const
155 {
156 if (full_selection)
157 return true;
158 if (selected_modules.count(mod_name) > 0)
159 return true;
160 if (selected_members.count(mod_name) > 0)
161 if (selected_members.at(mod_name).count(memb_name) > 0)
162 return true;
163 return false;
164 }
165
166 void RTLIL::Selection::optimize(RTLIL::Design *design)
167 {
168 if (full_selection) {
169 selected_modules.clear();
170 selected_members.clear();
171 return;
172 }
173
174 std::vector<RTLIL::IdString> del_list, add_list;
175
176 del_list.clear();
177 for (auto mod_name : selected_modules) {
178 if (design->modules.count(mod_name) == 0)
179 del_list.push_back(mod_name);
180 selected_members.erase(mod_name);
181 }
182 for (auto mod_name : del_list)
183 selected_modules.erase(mod_name);
184
185 del_list.clear();
186 for (auto &it : selected_members)
187 if (design->modules.count(it.first) == 0)
188 del_list.push_back(it.first);
189 for (auto mod_name : del_list)
190 selected_members.erase(mod_name);
191
192 for (auto &it : selected_members) {
193 del_list.clear();
194 for (auto memb_name : it.second)
195 if (design->modules[it.first]->count_id(memb_name) == 0)
196 del_list.push_back(memb_name);
197 for (auto memb_name : del_list)
198 it.second.erase(memb_name);
199 }
200
201 del_list.clear();
202 add_list.clear();
203 for (auto &it : selected_members)
204 if (it.second.size() == 0)
205 del_list.push_back(it.first);
206 else if (it.second.size() == design->modules[it.first]->wires.size() + design->modules[it.first]->memories.size() +
207 design->modules[it.first]->cells.size() + design->modules[it.first]->processes.size())
208 add_list.push_back(it.first);
209 for (auto mod_name : del_list)
210 selected_members.erase(mod_name);
211 for (auto mod_name : add_list) {
212 selected_members.erase(mod_name);
213 selected_modules.insert(mod_name);
214 }
215
216 if (selected_modules.size() == design->modules.size()) {
217 full_selection = true;
218 selected_modules.clear();
219 selected_members.clear();
220 }
221 }
222
223 RTLIL::Design::~Design()
224 {
225 for (auto it = modules.begin(); it != modules.end(); it++)
226 delete it->second;
227 }
228
229 void RTLIL::Design::check()
230 {
231 #ifndef NDEBUG
232 for (auto &it : modules) {
233 assert(it.first == it.second->name);
234 assert(it.first.size() > 0 && (it.first[0] == '\\' || it.first[0] == '$'));
235 it.second->check();
236 }
237 #endif
238 }
239
240 void RTLIL::Design::optimize()
241 {
242 for (auto &it : modules)
243 it.second->optimize();
244 for (auto &it : selection_stack)
245 it.optimize(this);
246 for (auto &it : selection_vars)
247 it.second.optimize(this);
248 }
249
250 bool RTLIL::Design::selected_module(RTLIL::IdString mod_name) const
251 {
252 if (!selected_active_module.empty() && mod_name != selected_active_module)
253 return false;
254 if (selection_stack.size() == 0)
255 return true;
256 return selection_stack.back().selected_module(mod_name);
257 }
258
259 bool RTLIL::Design::selected_whole_module(RTLIL::IdString mod_name) const
260 {
261 if (!selected_active_module.empty() && mod_name != selected_active_module)
262 return false;
263 if (selection_stack.size() == 0)
264 return true;
265 return selection_stack.back().selected_whole_module(mod_name);
266 }
267
268 bool RTLIL::Design::selected_member(RTLIL::IdString mod_name, RTLIL::IdString memb_name) const
269 {
270 if (!selected_active_module.empty() && mod_name != selected_active_module)
271 return false;
272 if (selection_stack.size() == 0)
273 return true;
274 return selection_stack.back().selected_member(mod_name, memb_name);
275 }
276
277 RTLIL::Module::~Module()
278 {
279 for (auto it = wires.begin(); it != wires.end(); it++)
280 delete it->second;
281 for (auto it = memories.begin(); it != memories.end(); it++)
282 delete it->second;
283 for (auto it = cells.begin(); it != cells.end(); it++)
284 delete it->second;
285 for (auto it = processes.begin(); it != processes.end(); it++)
286 delete it->second;
287 }
288
289 RTLIL::IdString RTLIL::Module::derive(RTLIL::Design*, std::map<RTLIL::IdString, RTLIL::Const>)
290 {
291 log_error("Module `%s' is used with parameters but is not parametric!\n", id2cstr(name));
292 }
293
294 size_t RTLIL::Module::count_id(RTLIL::IdString id)
295 {
296 return wires.count(id) + memories.count(id) + cells.count(id) + processes.count(id);
297 }
298
299 #ifndef NDEBUG
300 namespace {
301 struct InternalCellChecker
302 {
303 RTLIL::Module *module;
304 RTLIL::Cell *cell;
305 std::set<RTLIL::IdString> expected_params, expected_ports;
306
307 InternalCellChecker(RTLIL::Module *module, RTLIL::Cell *cell) : module(module), cell(cell) { }
308
309 void error(int linenr)
310 {
311 char *ptr;
312 size_t size;
313
314 FILE *f = open_memstream(&ptr, &size);
315 ILANG_BACKEND::dump_cell(f, " ", cell);
316 fputc(0, f);
317 fclose(f);
318
319 log_error("Found error in internal cell %s%s%s (%s) at %s:%d:\n%s",
320 module ? module->name.c_str() : "", module ? "." : "",
321 cell->name.c_str(), cell->type.c_str(), __FILE__, linenr, ptr);
322 }
323
324 int param(const char *name)
325 {
326 if (cell->parameters.count(name) == 0)
327 error(__LINE__);
328 expected_params.insert(name);
329 return cell->parameters.at(name).as_int();
330 }
331
332 int param_bool(const char *name)
333 {
334 int v = param(name);
335 if (cell->parameters.at(name).bits.size() > 32)
336 error(__LINE__);
337 if (v != 0 && v != 1)
338 error(__LINE__);
339 return v;
340 }
341
342 void param_bits(const char *name, int width)
343 {
344 param(name);
345 if (int(cell->parameters.at(name).bits.size()) != width)
346 error(__LINE__);
347 }
348
349 void port(const char *name, int width)
350 {
351 if (cell->connections.count(name) == 0)
352 error(__LINE__);
353 if (cell->connections.at(name).size() != width)
354 error(__LINE__);
355 expected_ports.insert(name);
356 }
357
358 void check_expected(bool check_matched_sign = true)
359 {
360 for (auto &para : cell->parameters)
361 if (expected_params.count(para.first) == 0)
362 error(__LINE__);
363 for (auto &conn : cell->connections)
364 if (expected_ports.count(conn.first) == 0)
365 error(__LINE__);
366
367 if (expected_params.count("\\A_SIGNED") != 0 && expected_params.count("\\B_SIGNED") && check_matched_sign) {
368 bool a_is_signed = param("\\A_SIGNED") != 0;
369 bool b_is_signed = param("\\B_SIGNED") != 0;
370 if (a_is_signed != b_is_signed)
371 error(__LINE__);
372 }
373 }
374
375 void check_gate(const char *ports)
376 {
377 if (cell->parameters.size() != 0)
378 error(__LINE__);
379
380 for (const char *p = ports; *p; p++) {
381 char portname[3] = { '\\', *p, 0 };
382 if (cell->connections.count(portname) == 0)
383 error(__LINE__);
384 if (cell->connections.at(portname).size() != 1)
385 error(__LINE__);
386 }
387
388 for (auto &conn : cell->connections) {
389 if (conn.first.size() != 2 || conn.first.at(0) != '\\')
390 error(__LINE__);
391 if (strchr(ports, conn.first.at(1)) == NULL)
392 error(__LINE__);
393 }
394 }
395
396 void check()
397 {
398 if (cell->type[0] != '$' || cell->type.substr(0, 3) == "$__" || cell->type.substr(0, 8) == "$paramod" ||
399 cell->type.substr(0, 9) == "$verific$" || cell->type.substr(0, 7) == "$array:")
400 return;
401
402 if (cell->type == "$not" || cell->type == "$pos" || cell->type == "$bu0" || cell->type == "$neg") {
403 param_bool("\\A_SIGNED");
404 port("\\A", param("\\A_WIDTH"));
405 port("\\Y", param("\\Y_WIDTH"));
406 check_expected();
407 return;
408 }
409
410 if (cell->type == "$and" || cell->type == "$or" || cell->type == "$xor" || cell->type == "$xnor") {
411 param_bool("\\A_SIGNED");
412 param_bool("\\B_SIGNED");
413 port("\\A", param("\\A_WIDTH"));
414 port("\\B", param("\\B_WIDTH"));
415 port("\\Y", param("\\Y_WIDTH"));
416 check_expected();
417 return;
418 }
419
420 if (cell->type == "$reduce_and" || cell->type == "$reduce_or" || cell->type == "$reduce_xor" ||
421 cell->type == "$reduce_xnor" || cell->type == "$reduce_bool") {
422 param_bool("\\A_SIGNED");
423 port("\\A", param("\\A_WIDTH"));
424 port("\\Y", param("\\Y_WIDTH"));
425 check_expected();
426 return;
427 }
428
429 if (cell->type == "$shl" || cell->type == "$shr" || cell->type == "$sshl" || cell->type == "$sshr") {
430 param_bool("\\A_SIGNED");
431 param_bool("\\B_SIGNED");
432 port("\\A", param("\\A_WIDTH"));
433 port("\\B", param("\\B_WIDTH"));
434 port("\\Y", param("\\Y_WIDTH"));
435 check_expected(false);
436 return;
437 }
438
439 if (cell->type == "$lt" || cell->type == "$le" || cell->type == "$eq" || cell->type == "$ne" ||
440 cell->type == "$eqx" || cell->type == "$nex" || cell->type == "$ge" || cell->type == "$gt") {
441 param_bool("\\A_SIGNED");
442 param_bool("\\B_SIGNED");
443 port("\\A", param("\\A_WIDTH"));
444 port("\\B", param("\\B_WIDTH"));
445 port("\\Y", param("\\Y_WIDTH"));
446 check_expected();
447 return;
448 }
449
450 if (cell->type == "$add" || cell->type == "$sub" || cell->type == "$mul" || cell->type == "$div" ||
451 cell->type == "$mod" || cell->type == "$pow") {
452 param_bool("\\A_SIGNED");
453 param_bool("\\B_SIGNED");
454 port("\\A", param("\\A_WIDTH"));
455 port("\\B", param("\\B_WIDTH"));
456 port("\\Y", param("\\Y_WIDTH"));
457 check_expected(cell->type != "$pow");
458 return;
459 }
460
461 if (cell->type == "$logic_not") {
462 param_bool("\\A_SIGNED");
463 port("\\A", param("\\A_WIDTH"));
464 port("\\Y", param("\\Y_WIDTH"));
465 check_expected();
466 return;
467 }
468
469 if (cell->type == "$logic_and" || cell->type == "$logic_or") {
470 param_bool("\\A_SIGNED");
471 param_bool("\\B_SIGNED");
472 port("\\A", param("\\A_WIDTH"));
473 port("\\B", param("\\B_WIDTH"));
474 port("\\Y", param("\\Y_WIDTH"));
475 check_expected(false);
476 return;
477 }
478
479 if (cell->type == "$slice") {
480 param("\\OFFSET");
481 port("\\A", param("\\A_WIDTH"));
482 port("\\Y", param("\\Y_WIDTH"));
483 if (param("\\OFFSET") + param("\\Y_WIDTH") > param("\\A_WIDTH"))
484 error(__LINE__);
485 check_expected();
486 return;
487 }
488
489 if (cell->type == "$concat") {
490 port("\\A", param("\\A_WIDTH"));
491 port("\\B", param("\\B_WIDTH"));
492 port("\\Y", param("\\A_WIDTH") + param("\\B_WIDTH"));
493 check_expected();
494 return;
495 }
496
497 if (cell->type == "$mux") {
498 port("\\A", param("\\WIDTH"));
499 port("\\B", param("\\WIDTH"));
500 port("\\S", 1);
501 port("\\Y", param("\\WIDTH"));
502 check_expected();
503 return;
504 }
505
506 if (cell->type == "$pmux" || cell->type == "$safe_pmux") {
507 port("\\A", param("\\WIDTH"));
508 port("\\B", param("\\WIDTH") * param("\\S_WIDTH"));
509 port("\\S", param("\\S_WIDTH"));
510 port("\\Y", param("\\WIDTH"));
511 check_expected();
512 return;
513 }
514
515 if (cell->type == "$lut") {
516 param("\\LUT");
517 port("\\I", param("\\WIDTH"));
518 port("\\O", 1);
519 check_expected();
520 return;
521 }
522
523 if (cell->type == "$sr") {
524 param_bool("\\SET_POLARITY");
525 param_bool("\\CLR_POLARITY");
526 port("\\SET", param("\\WIDTH"));
527 port("\\CLR", param("\\WIDTH"));
528 port("\\Q", param("\\WIDTH"));
529 check_expected();
530 return;
531 }
532
533 if (cell->type == "$dff") {
534 param_bool("\\CLK_POLARITY");
535 port("\\CLK", 1);
536 port("\\D", param("\\WIDTH"));
537 port("\\Q", param("\\WIDTH"));
538 check_expected();
539 return;
540 }
541
542 if (cell->type == "$dffsr") {
543 param_bool("\\CLK_POLARITY");
544 param_bool("\\SET_POLARITY");
545 param_bool("\\CLR_POLARITY");
546 port("\\CLK", 1);
547 port("\\SET", param("\\WIDTH"));
548 port("\\CLR", param("\\WIDTH"));
549 port("\\D", param("\\WIDTH"));
550 port("\\Q", param("\\WIDTH"));
551 check_expected();
552 return;
553 }
554
555 if (cell->type == "$adff") {
556 param_bool("\\CLK_POLARITY");
557 param_bool("\\ARST_POLARITY");
558 param_bits("\\ARST_VALUE", param("\\WIDTH"));
559 port("\\CLK", 1);
560 port("\\ARST", 1);
561 port("\\D", param("\\WIDTH"));
562 port("\\Q", param("\\WIDTH"));
563 check_expected();
564 return;
565 }
566
567 if (cell->type == "$dlatch") {
568 param_bool("\\EN_POLARITY");
569 port("\\EN", 1);
570 port("\\D", param("\\WIDTH"));
571 port("\\Q", param("\\WIDTH"));
572 check_expected();
573 return;
574 }
575
576 if (cell->type == "$dlatchsr") {
577 param_bool("\\EN_POLARITY");
578 param_bool("\\SET_POLARITY");
579 param_bool("\\CLR_POLARITY");
580 port("\\EN", 1);
581 port("\\SET", param("\\WIDTH"));
582 port("\\CLR", param("\\WIDTH"));
583 port("\\D", param("\\WIDTH"));
584 port("\\Q", param("\\WIDTH"));
585 check_expected();
586 return;
587 }
588
589 if (cell->type == "$fsm") {
590 param("\\NAME");
591 param_bool("\\CLK_POLARITY");
592 param_bool("\\ARST_POLARITY");
593 param("\\STATE_BITS");
594 param("\\STATE_NUM");
595 param("\\STATE_NUM_LOG2");
596 param("\\STATE_RST");
597 param_bits("\\STATE_TABLE", param("\\STATE_BITS") * param("\\STATE_NUM"));
598 param("\\TRANS_NUM");
599 param_bits("\\TRANS_TABLE", param("\\TRANS_NUM") * (2*param("\\STATE_NUM_LOG2") + param("\\CTRL_IN_WIDTH") + param("\\CTRL_OUT_WIDTH")));
600 port("\\CLK", 1);
601 port("\\ARST", 1);
602 port("\\CTRL_IN", param("\\CTRL_IN_WIDTH"));
603 port("\\CTRL_OUT", param("\\CTRL_OUT_WIDTH"));
604 check_expected();
605 return;
606 }
607
608 if (cell->type == "$memrd") {
609 param("\\MEMID");
610 param_bool("\\CLK_ENABLE");
611 param_bool("\\CLK_POLARITY");
612 param_bool("\\TRANSPARENT");
613 port("\\CLK", 1);
614 port("\\ADDR", param("\\ABITS"));
615 port("\\DATA", param("\\WIDTH"));
616 check_expected();
617 return;
618 }
619
620 if (cell->type == "$memwr") {
621 param("\\MEMID");
622 param_bool("\\CLK_ENABLE");
623 param_bool("\\CLK_POLARITY");
624 param("\\PRIORITY");
625 port("\\CLK", 1);
626 port("\\EN", param("\\WIDTH"));
627 port("\\ADDR", param("\\ABITS"));
628 port("\\DATA", param("\\WIDTH"));
629 check_expected();
630 return;
631 }
632
633 if (cell->type == "$mem") {
634 param("\\MEMID");
635 param("\\SIZE");
636 param("\\OFFSET");
637 param_bits("\\RD_CLK_ENABLE", param("\\RD_PORTS"));
638 param_bits("\\RD_CLK_POLARITY", param("\\RD_PORTS"));
639 param_bits("\\RD_TRANSPARENT", param("\\RD_PORTS"));
640 param_bits("\\WR_CLK_ENABLE", param("\\WR_PORTS"));
641 param_bits("\\WR_CLK_POLARITY", param("\\WR_PORTS"));
642 port("\\RD_CLK", param("\\RD_PORTS"));
643 port("\\RD_ADDR", param("\\RD_PORTS") * param("\\ABITS"));
644 port("\\RD_DATA", param("\\RD_PORTS") * param("\\WIDTH"));
645 port("\\WR_CLK", param("\\WR_PORTS"));
646 port("\\WR_EN", param("\\WR_PORTS") * param("\\WIDTH"));
647 port("\\WR_ADDR", param("\\WR_PORTS") * param("\\ABITS"));
648 port("\\WR_DATA", param("\\WR_PORTS") * param("\\WIDTH"));
649 check_expected();
650 return;
651 }
652
653 if (cell->type == "$assert") {
654 port("\\A", 1);
655 port("\\EN", 1);
656 check_expected();
657 return;
658 }
659
660 if (cell->type == "$_INV_") { check_gate("AY"); return; }
661 if (cell->type == "$_AND_") { check_gate("ABY"); return; }
662 if (cell->type == "$_OR_") { check_gate("ABY"); return; }
663 if (cell->type == "$_XOR_") { check_gate("ABY"); return; }
664 if (cell->type == "$_MUX_") { check_gate("ABSY"); return; }
665
666 if (cell->type == "$_SR_NN_") { check_gate("SRQ"); return; }
667 if (cell->type == "$_SR_NP_") { check_gate("SRQ"); return; }
668 if (cell->type == "$_SR_PN_") { check_gate("SRQ"); return; }
669 if (cell->type == "$_SR_PP_") { check_gate("SRQ"); return; }
670
671 if (cell->type == "$_DFF_N_") { check_gate("DQC"); return; }
672 if (cell->type == "$_DFF_P_") { check_gate("DQC"); return; }
673
674 if (cell->type == "$_DFF_NN0_") { check_gate("DQCR"); return; }
675 if (cell->type == "$_DFF_NN1_") { check_gate("DQCR"); return; }
676 if (cell->type == "$_DFF_NP0_") { check_gate("DQCR"); return; }
677 if (cell->type == "$_DFF_NP1_") { check_gate("DQCR"); return; }
678 if (cell->type == "$_DFF_PN0_") { check_gate("DQCR"); return; }
679 if (cell->type == "$_DFF_PN1_") { check_gate("DQCR"); return; }
680 if (cell->type == "$_DFF_PP0_") { check_gate("DQCR"); return; }
681 if (cell->type == "$_DFF_PP1_") { check_gate("DQCR"); return; }
682
683 if (cell->type == "$_DFFSR_NNN_") { check_gate("CSRDQ"); return; }
684 if (cell->type == "$_DFFSR_NNP_") { check_gate("CSRDQ"); return; }
685 if (cell->type == "$_DFFSR_NPN_") { check_gate("CSRDQ"); return; }
686 if (cell->type == "$_DFFSR_NPP_") { check_gate("CSRDQ"); return; }
687 if (cell->type == "$_DFFSR_PNN_") { check_gate("CSRDQ"); return; }
688 if (cell->type == "$_DFFSR_PNP_") { check_gate("CSRDQ"); return; }
689 if (cell->type == "$_DFFSR_PPN_") { check_gate("CSRDQ"); return; }
690 if (cell->type == "$_DFFSR_PPP_") { check_gate("CSRDQ"); return; }
691
692 if (cell->type == "$_DLATCH_N_") { check_gate("EDQ"); return; }
693 if (cell->type == "$_DLATCH_P_") { check_gate("EDQ"); return; }
694
695 if (cell->type == "$_DLATCHSR_NNN_") { check_gate("ESRDQ"); return; }
696 if (cell->type == "$_DLATCHSR_NNP_") { check_gate("ESRDQ"); return; }
697 if (cell->type == "$_DLATCHSR_NPN_") { check_gate("ESRDQ"); return; }
698 if (cell->type == "$_DLATCHSR_NPP_") { check_gate("ESRDQ"); return; }
699 if (cell->type == "$_DLATCHSR_PNN_") { check_gate("ESRDQ"); return; }
700 if (cell->type == "$_DLATCHSR_PNP_") { check_gate("ESRDQ"); return; }
701 if (cell->type == "$_DLATCHSR_PPN_") { check_gate("ESRDQ"); return; }
702 if (cell->type == "$_DLATCHSR_PPP_") { check_gate("ESRDQ"); return; }
703
704 error(__LINE__);
705 }
706 };
707 }
708 #endif
709
710 void RTLIL::Module::check()
711 {
712 #ifndef NDEBUG
713 for (auto &it : wires) {
714 assert(it.first == it.second->name);
715 assert(it.first.size() > 0 && (it.first[0] == '\\' || it.first[0] == '$'));
716 assert(it.second->width >= 0);
717 assert(it.second->port_id >= 0);
718 for (auto &it2 : it.second->attributes) {
719 assert(it2.first.size() > 0 && (it2.first[0] == '\\' || it2.first[0] == '$'));
720 }
721 }
722
723 for (auto &it : memories) {
724 assert(it.first == it.second->name);
725 assert(it.first.size() > 0 && (it.first[0] == '\\' || it.first[0] == '$'));
726 assert(it.second->width >= 0);
727 assert(it.second->size >= 0);
728 for (auto &it2 : it.second->attributes) {
729 assert(it2.first.size() > 0 && (it2.first[0] == '\\' || it2.first[0] == '$'));
730 }
731 }
732
733 for (auto &it : cells) {
734 assert(it.first == it.second->name);
735 assert(it.first.size() > 0 && (it.first[0] == '\\' || it.first[0] == '$'));
736 assert(it.second->type.size() > 0 && (it.second->type[0] == '\\' || it.second->type[0] == '$'));
737 for (auto &it2 : it.second->connections) {
738 assert(it2.first.size() > 0 && (it2.first[0] == '\\' || it2.first[0] == '$'));
739 it2.second.check();
740 }
741 for (auto &it2 : it.second->attributes) {
742 assert(it2.first.size() > 0 && (it2.first[0] == '\\' || it2.first[0] == '$'));
743 }
744 for (auto &it2 : it.second->parameters) {
745 assert(it2.first.size() > 0 && (it2.first[0] == '\\' || it2.first[0] == '$'));
746 }
747 InternalCellChecker checker(this, it.second);
748 checker.check();
749 }
750
751 for (auto &it : processes) {
752 assert(it.first == it.second->name);
753 assert(it.first.size() > 0 && (it.first[0] == '\\' || it.first[0] == '$'));
754 // FIXME: More checks here..
755 }
756
757 for (auto &it : connections) {
758 assert(it.first.size() == it.second.size());
759 it.first.check();
760 it.second.check();
761 }
762
763 for (auto &it : attributes) {
764 assert(it.first.size() > 0 && (it.first[0] == '\\' || it.first[0] == '$'));
765 }
766 #endif
767 }
768
769 void RTLIL::Module::optimize()
770 {
771 }
772
773 void RTLIL::Module::cloneInto(RTLIL::Module *new_mod) const
774 {
775 new_mod->name = name;
776 new_mod->connections = connections;
777 new_mod->attributes = attributes;
778
779 for (auto &it : wires)
780 new_mod->wires[it.first] = new RTLIL::Wire(*it.second);
781
782 for (auto &it : memories)
783 new_mod->memories[it.first] = new RTLIL::Memory(*it.second);
784
785 for (auto &it : cells)
786 new_mod->cells[it.first] = new RTLIL::Cell(*it.second);
787
788 for (auto &it : processes)
789 new_mod->processes[it.first] = it.second->clone();
790
791 struct RewriteSigSpecWorker
792 {
793 RTLIL::Module *mod;
794 void operator()(RTLIL::SigSpec &sig)
795 {
796 std::vector<RTLIL::SigChunk> chunks = sig.chunks();
797 for (auto &c : chunks)
798 if (c.wire != NULL)
799 c.wire = mod->wires.at(c.wire->name);
800 sig = chunks;
801 }
802 };
803
804 RewriteSigSpecWorker rewriteSigSpecWorker;
805 rewriteSigSpecWorker.mod = new_mod;
806 new_mod->rewrite_sigspecs(rewriteSigSpecWorker);
807 }
808
809 RTLIL::Module *RTLIL::Module::clone() const
810 {
811 RTLIL::Module *new_mod = new RTLIL::Module;
812 cloneInto(new_mod);
813 return new_mod;
814 }
815
816 void RTLIL::Module::add(RTLIL::Wire *wire)
817 {
818 assert(!wire->name.empty());
819 assert(count_id(wire->name) == 0);
820 wires[wire->name] = wire;
821 }
822
823 void RTLIL::Module::add(RTLIL::Cell *cell)
824 {
825 assert(!cell->name.empty());
826 assert(count_id(cell->name) == 0);
827 cells[cell->name] = cell;
828 }
829
830 void RTLIL::Module::remove(RTLIL::Cell *cell)
831 {
832 assert(cells.count(cell->name) != 0);
833 cells.erase(cell->name);
834 delete cell;
835 }
836
837 static bool fixup_ports_compare(const RTLIL::Wire *a, const RTLIL::Wire *b)
838 {
839 if (a->port_id && !b->port_id)
840 return true;
841 if (!a->port_id && b->port_id)
842 return false;
843
844 if (a->port_id == b->port_id)
845 return a->name < b->name;
846 return a->port_id < b->port_id;
847 }
848
849 void RTLIL::Module::fixup_ports()
850 {
851 std::vector<RTLIL::Wire*> all_ports;
852
853 for (auto &w : wires)
854 if (w.second->port_input || w.second->port_output)
855 all_ports.push_back(w.second);
856 else
857 w.second->port_id = 0;
858
859 std::sort(all_ports.begin(), all_ports.end(), fixup_ports_compare);
860 for (size_t i = 0; i < all_ports.size(); i++)
861 all_ports[i]->port_id = i+1;
862 }
863
864 RTLIL::Wire *RTLIL::Module::addWire(RTLIL::IdString name, int width)
865 {
866 RTLIL::Wire *wire = new RTLIL::Wire;
867 wire->name = name;
868 wire->width = width;
869 add(wire);
870 return wire;
871 }
872
873 RTLIL::Cell *RTLIL::Module::addCell(RTLIL::IdString name, RTLIL::IdString type)
874 {
875 RTLIL::Cell *cell = new RTLIL::Cell;
876 cell->name = name;
877 cell->type = type;
878 add(cell);
879 return cell;
880 }
881
882 #define DEF_METHOD(_func, _y_size, _type) \
883 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_y, bool is_signed) { \
884 RTLIL::Cell *cell = new RTLIL::Cell; \
885 cell->name = name; \
886 cell->type = _type; \
887 cell->parameters["\\A_SIGNED"] = is_signed; \
888 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
889 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
890 cell->connections["\\A"] = sig_a; \
891 cell->connections["\\Y"] = sig_y; \
892 add(cell); \
893 return cell; \
894 } \
895 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, bool is_signed) { \
896 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
897 add ## _func(name, sig_a, sig_y, is_signed); \
898 return sig_y; \
899 }
900 DEF_METHOD(Not, sig_a.size(), "$not")
901 DEF_METHOD(Pos, sig_a.size(), "$pos")
902 DEF_METHOD(Bu0, sig_a.size(), "$bu0")
903 DEF_METHOD(Neg, sig_a.size(), "$neg")
904 DEF_METHOD(ReduceAnd, 1, "$reduce_and")
905 DEF_METHOD(ReduceOr, 1, "$reduce_or")
906 DEF_METHOD(ReduceXor, 1, "$reduce_xor")
907 DEF_METHOD(ReduceXnor, 1, "$reduce_xnor")
908 DEF_METHOD(ReduceBool, 1, "$reduce_bool")
909 DEF_METHOD(LogicNot, 1, "$logic_not")
910 #undef DEF_METHOD
911
912 #define DEF_METHOD(_func, _y_size, _type) \
913 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_y, bool is_signed) { \
914 RTLIL::Cell *cell = new RTLIL::Cell; \
915 cell->name = name; \
916 cell->type = _type; \
917 cell->parameters["\\A_SIGNED"] = is_signed; \
918 cell->parameters["\\B_SIGNED"] = is_signed; \
919 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
920 cell->parameters["\\B_WIDTH"] = sig_b.size(); \
921 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
922 cell->connections["\\A"] = sig_a; \
923 cell->connections["\\B"] = sig_b; \
924 cell->connections["\\Y"] = sig_y; \
925 add(cell); \
926 return cell; \
927 } \
928 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, bool is_signed) { \
929 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
930 add ## _func(name, sig_a, sig_b, sig_y, is_signed); \
931 return sig_y; \
932 }
933 DEF_METHOD(And, std::max(sig_a.size(), sig_b.size()), "$and")
934 DEF_METHOD(Or, std::max(sig_a.size(), sig_b.size()), "$or")
935 DEF_METHOD(Xor, std::max(sig_a.size(), sig_b.size()), "$xor")
936 DEF_METHOD(Xnor, std::max(sig_a.size(), sig_b.size()), "$xnor")
937 DEF_METHOD(Shl, sig_a.size(), "$shl")
938 DEF_METHOD(Shr, sig_a.size(), "$shr")
939 DEF_METHOD(Sshl, sig_a.size(), "$sshl")
940 DEF_METHOD(Sshr, sig_a.size(), "$sshr")
941 DEF_METHOD(Lt, 1, "$lt")
942 DEF_METHOD(Le, 1, "$le")
943 DEF_METHOD(Eq, 1, "$eq")
944 DEF_METHOD(Ne, 1, "$ne")
945 DEF_METHOD(Eqx, 1, "$eqx")
946 DEF_METHOD(Nex, 1, "$nex")
947 DEF_METHOD(Ge, 1, "$ge")
948 DEF_METHOD(Gt, 1, "$gt")
949 DEF_METHOD(Add, std::max(sig_a.size(), sig_b.size()), "$add")
950 DEF_METHOD(Sub, std::max(sig_a.size(), sig_b.size()), "$sub")
951 DEF_METHOD(Mul, std::max(sig_a.size(), sig_b.size()), "$mul")
952 DEF_METHOD(Div, std::max(sig_a.size(), sig_b.size()), "$div")
953 DEF_METHOD(Mod, std::max(sig_a.size(), sig_b.size()), "$mod")
954 DEF_METHOD(LogicAnd, 1, "$logic_and")
955 DEF_METHOD(LogicOr, 1, "$logic_or")
956 #undef DEF_METHOD
957
958 #define DEF_METHOD(_func, _type, _pmux) \
959 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_s, RTLIL::SigSpec sig_y) { \
960 RTLIL::Cell *cell = new RTLIL::Cell; \
961 cell->name = name; \
962 cell->type = _type; \
963 cell->parameters["\\WIDTH"] = sig_a.size(); \
964 cell->parameters["\\WIDTH"] = sig_b.size(); \
965 if (_pmux) cell->parameters["\\S_WIDTH"] = sig_s.size(); \
966 cell->connections["\\A"] = sig_a; \
967 cell->connections["\\B"] = sig_b; \
968 cell->connections["\\S"] = sig_s; \
969 cell->connections["\\Y"] = sig_y; \
970 add(cell); \
971 return cell; \
972 } \
973 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_s) { \
974 RTLIL::SigSpec sig_y = addWire(NEW_ID, sig_a.size()); \
975 add ## _func(name, sig_a, sig_b, sig_s, sig_y); \
976 return sig_y; \
977 }
978 DEF_METHOD(Mux, "$mux", 0)
979 DEF_METHOD(Pmux, "$pmux", 1)
980 DEF_METHOD(SafePmux, "$safe_pmux", 1)
981 #undef DEF_METHOD
982
983 #define DEF_METHOD_2(_func, _type, _P1, _P2) \
984 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig1, RTLIL::SigSpec sig2) { \
985 RTLIL::Cell *cell = new RTLIL::Cell; \
986 cell->name = name; \
987 cell->type = _type; \
988 cell->connections["\\" #_P1] = sig1; \
989 cell->connections["\\" #_P2] = sig2; \
990 add(cell); \
991 return cell; \
992 } \
993 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig1) { \
994 RTLIL::SigSpec sig2 = addWire(NEW_ID); \
995 add ## _func(name, sig1, sig2); \
996 return sig2; \
997 }
998 #define DEF_METHOD_3(_func, _type, _P1, _P2, _P3) \
999 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig1, RTLIL::SigSpec sig2, RTLIL::SigSpec sig3) { \
1000 RTLIL::Cell *cell = new RTLIL::Cell; \
1001 cell->name = name; \
1002 cell->type = _type; \
1003 cell->connections["\\" #_P1] = sig1; \
1004 cell->connections["\\" #_P2] = sig2; \
1005 cell->connections["\\" #_P3] = sig3; \
1006 add(cell); \
1007 return cell; \
1008 } \
1009 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig1, RTLIL::SigSpec sig2) { \
1010 RTLIL::SigSpec sig3 = addWire(NEW_ID); \
1011 add ## _func(name, sig1, sig2, sig3); \
1012 return sig3; \
1013 }
1014 #define DEF_METHOD_4(_func, _type, _P1, _P2, _P3, _P4) \
1015 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig1, RTLIL::SigSpec sig2, RTLIL::SigSpec sig3, RTLIL::SigSpec sig4) { \
1016 RTLIL::Cell *cell = new RTLIL::Cell; \
1017 cell->name = name; \
1018 cell->type = _type; \
1019 cell->connections["\\" #_P1] = sig1; \
1020 cell->connections["\\" #_P2] = sig2; \
1021 cell->connections["\\" #_P3] = sig3; \
1022 cell->connections["\\" #_P4] = sig4; \
1023 add(cell); \
1024 return cell; \
1025 } \
1026 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig1, RTLIL::SigSpec sig2, RTLIL::SigSpec sig3) { \
1027 RTLIL::SigSpec sig4 = addWire(NEW_ID); \
1028 add ## _func(name, sig1, sig2, sig3, sig4); \
1029 return sig4; \
1030 }
1031 DEF_METHOD_2(InvGate, "$_INV_", A, Y)
1032 DEF_METHOD_3(AndGate, "$_AND_", A, B, Y)
1033 DEF_METHOD_3(OrGate, "$_OR_", A, B, Y)
1034 DEF_METHOD_3(XorGate, "$_XOR_", A, B, Y)
1035 DEF_METHOD_4(MuxGate, "$_MUX_", A, B, S, Y)
1036 #undef DEF_METHOD_2
1037 #undef DEF_METHOD_3
1038 #undef DEF_METHOD_4
1039
1040 RTLIL::Cell* RTLIL::Module::addPow(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_y, bool a_signed, bool b_signed)
1041 {
1042 RTLIL::Cell *cell = new RTLIL::Cell;
1043 cell->name = name;
1044 cell->type = "$pow";
1045 cell->parameters["\\A_SIGNED"] = a_signed;
1046 cell->parameters["\\B_SIGNED"] = b_signed;
1047 cell->parameters["\\A_WIDTH"] = sig_a.size();
1048 cell->parameters["\\B_WIDTH"] = sig_b.size();
1049 cell->parameters["\\Y_WIDTH"] = sig_y.size();
1050 cell->connections["\\A"] = sig_a;
1051 cell->connections["\\B"] = sig_b;
1052 cell->connections["\\Y"] = sig_y;
1053 add(cell);
1054 return cell;
1055 }
1056
1057 RTLIL::Cell* RTLIL::Module::addSlice(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_y, RTLIL::Const offset)
1058 {
1059 RTLIL::Cell *cell = new RTLIL::Cell;
1060 cell->name = name;
1061 cell->type = "$slice";
1062 cell->parameters["\\A_WIDTH"] = sig_a.size();
1063 cell->parameters["\\Y_WIDTH"] = sig_y.size();
1064 cell->parameters["\\OFFSET"] = offset;
1065 cell->connections["\\A"] = sig_a;
1066 cell->connections["\\Y"] = sig_y;
1067 add(cell);
1068 return cell;
1069 }
1070
1071 RTLIL::Cell* RTLIL::Module::addConcat(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_y)
1072 {
1073 RTLIL::Cell *cell = new RTLIL::Cell;
1074 cell->name = name;
1075 cell->type = "$concat";
1076 cell->parameters["\\A_WIDTH"] = sig_a.size();
1077 cell->parameters["\\B_WIDTH"] = sig_b.size();
1078 cell->connections["\\A"] = sig_a;
1079 cell->connections["\\B"] = sig_b;
1080 cell->connections["\\Y"] = sig_y;
1081 add(cell);
1082 return cell;
1083 }
1084
1085 RTLIL::Cell* RTLIL::Module::addLut(RTLIL::IdString name, RTLIL::SigSpec sig_i, RTLIL::SigSpec sig_o, RTLIL::Const lut)
1086 {
1087 RTLIL::Cell *cell = new RTLIL::Cell;
1088 cell->name = name;
1089 cell->type = "$lut";
1090 cell->parameters["\\LUT"] = lut;
1091 cell->parameters["\\WIDTH"] = sig_i.size();
1092 cell->connections["\\I"] = sig_i;
1093 cell->connections["\\O"] = sig_o;
1094 add(cell);
1095 return cell;
1096 }
1097
1098 RTLIL::Cell* RTLIL::Module::addAssert(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_en)
1099 {
1100 RTLIL::Cell *cell = new RTLIL::Cell;
1101 cell->name = name;
1102 cell->type = "$assert";
1103 cell->connections["\\A"] = sig_a;
1104 cell->connections["\\EN"] = sig_en;
1105 add(cell);
1106 return cell;
1107 }
1108
1109 RTLIL::Cell* RTLIL::Module::addSr(RTLIL::IdString name, RTLIL::SigSpec sig_set, RTLIL::SigSpec sig_clr, RTLIL::SigSpec sig_q, bool set_polarity, bool clr_polarity)
1110 {
1111 RTLIL::Cell *cell = new RTLIL::Cell;
1112 cell->name = name;
1113 cell->type = "$sr";
1114 cell->parameters["\\SET_POLARITY"] = set_polarity;
1115 cell->parameters["\\CLR_POLARITY"] = clr_polarity;
1116 cell->parameters["\\WIDTH"] = sig_q.size();
1117 cell->connections["\\SET"] = sig_set;
1118 cell->connections["\\CLR"] = sig_clr;
1119 cell->connections["\\Q"] = sig_q;
1120 add(cell);
1121 return cell;
1122 }
1123
1124 RTLIL::Cell* RTLIL::Module::addDff(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool clk_polarity)
1125 {
1126 RTLIL::Cell *cell = new RTLIL::Cell;
1127 cell->name = name;
1128 cell->type = "$dff";
1129 cell->parameters["\\CLK_POLARITY"] = clk_polarity;
1130 cell->parameters["\\WIDTH"] = sig_q.size();
1131 cell->connections["\\CLK"] = sig_clk;
1132 cell->connections["\\D"] = sig_d;
1133 cell->connections["\\Q"] = sig_q;
1134 add(cell);
1135 return cell;
1136 }
1137
1138 RTLIL::Cell* RTLIL::Module::addDffsr(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_set, RTLIL::SigSpec sig_clr,
1139 RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool clk_polarity, bool set_polarity, bool clr_polarity)
1140 {
1141 RTLIL::Cell *cell = new RTLIL::Cell;
1142 cell->name = name;
1143 cell->type = "$dffsr";
1144 cell->parameters["\\CLK_POLARITY"] = clk_polarity;
1145 cell->parameters["\\SET_POLARITY"] = set_polarity;
1146 cell->parameters["\\CLR_POLARITY"] = clr_polarity;
1147 cell->parameters["\\WIDTH"] = sig_q.size();
1148 cell->connections["\\CLK"] = sig_clk;
1149 cell->connections["\\SET"] = sig_set;
1150 cell->connections["\\CLR"] = sig_clr;
1151 cell->connections["\\D"] = sig_d;
1152 cell->connections["\\Q"] = sig_q;
1153 add(cell);
1154 return cell;
1155 }
1156
1157 RTLIL::Cell* RTLIL::Module::addAdff(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_arst, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q,
1158 RTLIL::Const arst_value, bool clk_polarity, bool arst_polarity)
1159 {
1160 RTLIL::Cell *cell = new RTLIL::Cell;
1161 cell->name = name;
1162 cell->type = "$adff";
1163 cell->parameters["\\CLK_POLARITY"] = clk_polarity;
1164 cell->parameters["\\ARST_POLARITY"] = arst_polarity;
1165 cell->parameters["\\ARST_VALUE"] = arst_value;
1166 cell->parameters["\\WIDTH"] = sig_q.size();
1167 cell->connections["\\CLK"] = sig_clk;
1168 cell->connections["\\ARST"] = sig_arst;
1169 cell->connections["\\D"] = sig_d;
1170 cell->connections["\\Q"] = sig_q;
1171 add(cell);
1172 return cell;
1173 }
1174
1175 RTLIL::Cell* RTLIL::Module::addDlatch(RTLIL::IdString name, RTLIL::SigSpec sig_en, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool en_polarity)
1176 {
1177 RTLIL::Cell *cell = new RTLIL::Cell;
1178 cell->name = name;
1179 cell->type = "$dlatch";
1180 cell->parameters["\\EN_POLARITY"] = en_polarity;
1181 cell->parameters["\\WIDTH"] = sig_q.size();
1182 cell->connections["\\EN"] = sig_en;
1183 cell->connections["\\D"] = sig_d;
1184 cell->connections["\\Q"] = sig_q;
1185 add(cell);
1186 return cell;
1187 }
1188
1189 RTLIL::Cell* RTLIL::Module::addDlatchsr(RTLIL::IdString name, RTLIL::SigSpec sig_en, RTLIL::SigSpec sig_set, RTLIL::SigSpec sig_clr,
1190 RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool en_polarity, bool set_polarity, bool clr_polarity)
1191 {
1192 RTLIL::Cell *cell = new RTLIL::Cell;
1193 cell->name = name;
1194 cell->type = "$dlatchsr";
1195 cell->parameters["\\EN_POLARITY"] = en_polarity;
1196 cell->parameters["\\SET_POLARITY"] = set_polarity;
1197 cell->parameters["\\CLR_POLARITY"] = clr_polarity;
1198 cell->parameters["\\WIDTH"] = sig_q.size();
1199 cell->connections["\\EN"] = sig_en;
1200 cell->connections["\\SET"] = sig_set;
1201 cell->connections["\\CLR"] = sig_clr;
1202 cell->connections["\\D"] = sig_d;
1203 cell->connections["\\Q"] = sig_q;
1204 add(cell);
1205 return cell;
1206 }
1207
1208 RTLIL::Cell* RTLIL::Module::addDffGate(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool clk_polarity)
1209 {
1210 RTLIL::Cell *cell = new RTLIL::Cell;
1211 cell->name = name;
1212 cell->type = stringf("$_DFF_%c_", clk_polarity ? 'P' : 'N');
1213 cell->connections["\\C"] = sig_clk;
1214 cell->connections["\\D"] = sig_d;
1215 cell->connections["\\Q"] = sig_q;
1216 add(cell);
1217 return cell;
1218 }
1219
1220 RTLIL::Cell* RTLIL::Module::addDffsrGate(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_set, RTLIL::SigSpec sig_clr,
1221 RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool clk_polarity, bool set_polarity, bool clr_polarity)
1222 {
1223 RTLIL::Cell *cell = new RTLIL::Cell;
1224 cell->name = name;
1225 cell->type = stringf("$_DFFSR_%c%c%c_", clk_polarity ? 'P' : 'N', set_polarity ? 'P' : 'N', clr_polarity ? 'P' : 'N');
1226 cell->connections["\\C"] = sig_clk;
1227 cell->connections["\\S"] = sig_set;
1228 cell->connections["\\R"] = sig_clr;
1229 cell->connections["\\D"] = sig_d;
1230 cell->connections["\\Q"] = sig_q;
1231 add(cell);
1232 return cell;
1233 }
1234
1235 RTLIL::Cell* RTLIL::Module::addAdffGate(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_arst, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q,
1236 bool arst_value, bool clk_polarity, bool arst_polarity)
1237 {
1238 RTLIL::Cell *cell = new RTLIL::Cell;
1239 cell->name = name;
1240 cell->type = stringf("$_DFF_%c%c%c_", clk_polarity ? 'P' : 'N', arst_polarity ? 'P' : 'N', arst_value ? '1' : '0');
1241 cell->connections["\\C"] = sig_clk;
1242 cell->connections["\\R"] = sig_arst;
1243 cell->connections["\\D"] = sig_d;
1244 cell->connections["\\Q"] = sig_q;
1245 add(cell);
1246 return cell;
1247 }
1248
1249 RTLIL::Cell* RTLIL::Module::addDlatchGate(RTLIL::IdString name, RTLIL::SigSpec sig_en, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool en_polarity)
1250 {
1251 RTLIL::Cell *cell = new RTLIL::Cell;
1252 cell->name = name;
1253 cell->type = stringf("$_DLATCH_%c_", en_polarity ? 'P' : 'N');
1254 cell->connections["\\E"] = sig_en;
1255 cell->connections["\\D"] = sig_d;
1256 cell->connections["\\Q"] = sig_q;
1257 add(cell);
1258 return cell;
1259 }
1260
1261 RTLIL::Cell* RTLIL::Module::addDlatchsrGate(RTLIL::IdString name, RTLIL::SigSpec sig_en, RTLIL::SigSpec sig_set, RTLIL::SigSpec sig_clr,
1262 RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool en_polarity, bool set_polarity, bool clr_polarity)
1263 {
1264 RTLIL::Cell *cell = new RTLIL::Cell;
1265 cell->name = name;
1266 cell->type = stringf("$_DLATCHSR_%c%c%c_", en_polarity ? 'P' : 'N', set_polarity ? 'P' : 'N', clr_polarity ? 'P' : 'N');
1267 cell->connections["\\E"] = sig_en;
1268 cell->connections["\\S"] = sig_set;
1269 cell->connections["\\R"] = sig_clr;
1270 cell->connections["\\D"] = sig_d;
1271 cell->connections["\\Q"] = sig_q;
1272 add(cell);
1273 return cell;
1274 }
1275
1276
1277 RTLIL::Wire::Wire()
1278 {
1279 width = 1;
1280 start_offset = 0;
1281 port_id = 0;
1282 port_input = false;
1283 port_output = false;
1284 }
1285
1286 RTLIL::Memory::Memory()
1287 {
1288 width = 1;
1289 size = 0;
1290 }
1291
1292 void RTLIL::Cell::check()
1293 {
1294 #ifndef NDEBUG
1295 InternalCellChecker checker(NULL, this);
1296 checker.check();
1297 #endif
1298 }
1299
1300 RTLIL::SigChunk::SigChunk()
1301 {
1302 wire = NULL;
1303 width = 0;
1304 offset = 0;
1305 }
1306
1307 RTLIL::SigChunk::SigChunk(const RTLIL::Const &value)
1308 {
1309 wire = NULL;
1310 data = value;
1311 width = data.bits.size();
1312 offset = 0;
1313 }
1314
1315 RTLIL::SigChunk::SigChunk(RTLIL::Wire *wire)
1316 {
1317 this->wire = wire;
1318 this->width = wire->width;
1319 this->offset = 0;
1320 }
1321
1322 RTLIL::SigChunk::SigChunk(RTLIL::Wire *wire, int offset, int width)
1323 {
1324 this->wire = wire;
1325 this->width = width;
1326 this->offset = offset;
1327 }
1328
1329 RTLIL::SigChunk::SigChunk(const std::string &str)
1330 {
1331 wire = NULL;
1332 data = RTLIL::Const(str);
1333 width = data.bits.size();
1334 offset = 0;
1335 }
1336
1337 RTLIL::SigChunk::SigChunk(int val, int width)
1338 {
1339 wire = NULL;
1340 data = RTLIL::Const(val, width);
1341 this->width = data.bits.size();
1342 offset = 0;
1343 }
1344
1345 RTLIL::SigChunk::SigChunk(RTLIL::State bit, int width)
1346 {
1347 wire = NULL;
1348 data = RTLIL::Const(bit, width);
1349 this->width = data.bits.size();
1350 offset = 0;
1351 }
1352
1353 RTLIL::SigChunk::SigChunk(RTLIL::SigBit bit)
1354 {
1355 wire = bit.wire;
1356 if (wire == NULL)
1357 data = RTLIL::Const(bit.data);
1358 offset = bit.offset;
1359 width = 1;
1360 }
1361
1362 RTLIL::SigChunk RTLIL::SigChunk::extract(int offset, int length) const
1363 {
1364 RTLIL::SigChunk ret;
1365 if (wire) {
1366 ret.wire = wire;
1367 ret.offset = this->offset + offset;
1368 ret.width = length;
1369 } else {
1370 for (int i = 0; i < length; i++)
1371 ret.data.bits.push_back(data.bits[offset+i]);
1372 ret.width = length;
1373 }
1374 return ret;
1375 }
1376
1377 bool RTLIL::SigChunk::operator <(const RTLIL::SigChunk &other) const
1378 {
1379 if (wire && other.wire)
1380 if (wire->name != other.wire->name)
1381 return wire->name < other.wire->name;
1382
1383 if (wire != other.wire)
1384 return wire < other.wire;
1385
1386 if (offset != other.offset)
1387 return offset < other.offset;
1388
1389 if (width != other.width)
1390 return width < other.width;
1391
1392 return data.bits < other.data.bits;
1393 }
1394
1395 bool RTLIL::SigChunk::operator ==(const RTLIL::SigChunk &other) const
1396 {
1397 if (wire != other.wire || width != other.width || offset != other.offset)
1398 return false;
1399 if (data.bits != other.data.bits)
1400 return false;
1401 return true;
1402 }
1403
1404 bool RTLIL::SigChunk::operator !=(const RTLIL::SigChunk &other) const
1405 {
1406 if (*this == other)
1407 return false;
1408 return true;
1409 }
1410
1411 RTLIL::SigSpec::SigSpec()
1412 {
1413 width_ = 0;
1414 }
1415
1416 RTLIL::SigSpec::SigSpec(const RTLIL::Const &value)
1417 {
1418 chunks_.push_back(RTLIL::SigChunk(value));
1419 width_ = chunks_.back().width;
1420 check();
1421 }
1422
1423 RTLIL::SigSpec::SigSpec(const RTLIL::SigChunk &chunk)
1424 {
1425 chunks_.push_back(chunk);
1426 width_ = chunks_.back().width;
1427 check();
1428 }
1429
1430 RTLIL::SigSpec::SigSpec(RTLIL::Wire *wire)
1431 {
1432 chunks_.push_back(RTLIL::SigChunk(wire));
1433 width_ = chunks_.back().width;
1434 check();
1435 }
1436
1437 RTLIL::SigSpec::SigSpec(RTLIL::Wire *wire, int offset, int width)
1438 {
1439 chunks_.push_back(RTLIL::SigChunk(wire, offset, width));
1440 width_ = chunks_.back().width;
1441 check();
1442 }
1443
1444 RTLIL::SigSpec::SigSpec(const std::string &str)
1445 {
1446 chunks_.push_back(RTLIL::SigChunk(str));
1447 width_ = chunks_.back().width;
1448 check();
1449 }
1450
1451 RTLIL::SigSpec::SigSpec(int val, int width)
1452 {
1453 chunks_.push_back(RTLIL::SigChunk(val, width));
1454 width_ = width;
1455 check();
1456 }
1457
1458 RTLIL::SigSpec::SigSpec(RTLIL::State bit, int width)
1459 {
1460 chunks_.push_back(RTLIL::SigChunk(bit, width));
1461 width_ = width;
1462 check();
1463 }
1464
1465 RTLIL::SigSpec::SigSpec(RTLIL::SigBit bit, int width)
1466 {
1467 if (bit.wire == NULL)
1468 chunks_.push_back(RTLIL::SigChunk(bit.data, width));
1469 else
1470 for (int i = 0; i < width; i++)
1471 chunks_.push_back(bit);
1472 width_ = width;
1473 check();
1474 }
1475
1476 RTLIL::SigSpec::SigSpec(std::vector<RTLIL::SigChunk> chunks)
1477 {
1478 width_ = 0;
1479 for (auto &c : chunks)
1480 append(c);
1481 check();
1482 }
1483
1484 RTLIL::SigSpec::SigSpec(std::vector<RTLIL::SigBit> bits)
1485 {
1486 width_ = 0;
1487 for (auto &bit : bits)
1488 append_bit(bit);
1489 check();
1490 }
1491
1492 RTLIL::SigSpec::SigSpec(std::set<RTLIL::SigBit> bits)
1493 {
1494 width_ = 0;
1495 for (auto &bit : bits)
1496 append_bit(bit);
1497 check();
1498 }
1499
1500 void RTLIL::SigSpec::pack() const
1501 {
1502 RTLIL::SigSpec *that = (RTLIL::SigSpec*)this;
1503
1504 if (that->bits_.empty())
1505 return;
1506
1507 log_assert(that->chunks_.empty());
1508
1509 std::vector<RTLIL::SigBit> old_bits;
1510 old_bits.swap(that->bits_);
1511
1512 that->width_ = 0;
1513 for (auto &bit : old_bits)
1514 that->append_bit(bit);
1515 }
1516
1517 void RTLIL::SigSpec::unpack() const
1518 {
1519 RTLIL::SigSpec *that = (RTLIL::SigSpec*)this;
1520
1521 if (that->chunks_.empty())
1522 return;
1523
1524 log_assert(that->bits_.empty());
1525
1526 that->bits_.reserve(that->width_);
1527 for (auto &c : that->chunks_)
1528 for (int i = 0; i < c.width; i++)
1529 that->bits_.push_back(RTLIL::SigBit(c, i));
1530
1531 that->chunks_.clear();
1532 }
1533
1534 bool RTLIL::SigSpec::packed() const
1535 {
1536 return bits_.empty();
1537 }
1538
1539 void RTLIL::SigSpec::sort()
1540 {
1541 unpack();
1542 std::sort(bits_.begin(), bits_.end());
1543 }
1544
1545 void RTLIL::SigSpec::sort_and_unify()
1546 {
1547 *this = this->to_sigbit_set();
1548 }
1549
1550 void RTLIL::SigSpec::replace(const RTLIL::SigSpec &pattern, const RTLIL::SigSpec &with)
1551 {
1552 replace(pattern, with, this);
1553 }
1554
1555 void RTLIL::SigSpec::replace(const RTLIL::SigSpec &pattern, const RTLIL::SigSpec &with, RTLIL::SigSpec *other) const
1556 {
1557 unpack();
1558 pattern.unpack();
1559 with.unpack();
1560
1561 assert(other != NULL);
1562 assert(width_ == other->width_);
1563 other->unpack();
1564
1565 assert(pattern.width_ == with.width_);
1566
1567 std::map<RTLIL::SigBit, RTLIL::SigBit> pattern_map;
1568 for (int i = 0; i < SIZE(pattern.bits_); i++)
1569 if (pattern.bits_[i].wire != NULL)
1570 pattern_map[pattern.bits_[i]] = with.bits_[i];
1571
1572 for (int i = 0; i < SIZE(bits_); i++)
1573 if (pattern_map.count(bits_[i]))
1574 other->bits_[i] = pattern_map.at(bits_[i]);
1575
1576 other->check();
1577 }
1578
1579 void RTLIL::SigSpec::remove(const RTLIL::SigSpec &pattern)
1580 {
1581 remove2(pattern, NULL);
1582 }
1583
1584 void RTLIL::SigSpec::remove(const RTLIL::SigSpec &pattern, RTLIL::SigSpec *other) const
1585 {
1586 RTLIL::SigSpec tmp = *this;
1587 tmp.remove2(pattern, other);
1588 }
1589
1590 void RTLIL::SigSpec::remove2(const RTLIL::SigSpec &pattern, RTLIL::SigSpec *other)
1591 {
1592 unpack();
1593
1594 if (other != NULL) {
1595 assert(width_ == other->width_);
1596 other->unpack();
1597 }
1598
1599 std::set<RTLIL::SigBit> pattern_bits = pattern.to_sigbit_set();
1600 std::vector<RTLIL::SigBit> new_bits, new_other_bits;
1601
1602 for (int i = 0; i < SIZE(bits_); i++) {
1603 if (bits_[i].wire != NULL && pattern_bits.count(bits_[i]))
1604 continue;
1605 if (other != NULL)
1606 new_other_bits.push_back(other->bits_[i]);
1607 new_bits.push_back(bits_[i]);
1608 }
1609
1610 bits_.swap(new_bits);
1611 width_ = SIZE(bits_);
1612
1613 if (other != NULL) {
1614 other->bits_.swap(new_other_bits);
1615 other->width_ = SIZE(other->bits_);
1616 }
1617
1618 check();
1619 }
1620
1621 RTLIL::SigSpec RTLIL::SigSpec::extract(RTLIL::SigSpec pattern, RTLIL::SigSpec *other) const
1622 {
1623 pack();
1624 pattern.pack();
1625
1626 if (other != NULL)
1627 other->pack();
1628
1629 assert(other == NULL || width_ == other->width_);
1630
1631 std::set<RTLIL::SigBit> pat = pattern.to_sigbit_set();
1632 std::vector<RTLIL::SigBit> bits_match = to_sigbit_vector();
1633 RTLIL::SigSpec ret;
1634
1635 if (other) {
1636 std::vector<RTLIL::SigBit> bits_other = other ? other->to_sigbit_vector() : bits_match;
1637 for (int i = 0; i < width_; i++)
1638 if (bits_match[i].wire && pat.count(bits_match[i]))
1639 ret.append_bit(bits_other[i]);
1640 } else {
1641 for (int i = 0; i < width_; i++)
1642 if (bits_match[i].wire && pat.count(bits_match[i]))
1643 ret.append_bit(bits_match[i]);
1644 }
1645
1646 ret.check();
1647 return ret;
1648 }
1649
1650 void RTLIL::SigSpec::replace(int offset, const RTLIL::SigSpec &with)
1651 {
1652 unpack();
1653 with.unpack();
1654
1655 assert(offset >= 0);
1656 assert(with.width_ >= 0);
1657 assert(offset+with.width_ <= width_);
1658
1659 for (int i = 0; i < with.width_; i++)
1660 bits_.at(offset + i) = with.bits_.at(i);
1661
1662 check();
1663 }
1664
1665 void RTLIL::SigSpec::remove_const()
1666 {
1667 unpack();
1668
1669 std::vector<RTLIL::SigBit> new_bits;
1670 new_bits.reserve(width_);
1671
1672 for (auto &bit : bits_)
1673 if (bit.wire != NULL)
1674 new_bits.push_back(bit);
1675
1676 bits_.swap(new_bits);
1677 width_ = bits_.size();
1678
1679 check();
1680 }
1681
1682 void RTLIL::SigSpec::remove(int offset, int length)
1683 {
1684 unpack();
1685
1686 assert(offset >= 0);
1687 assert(length >= 0);
1688 assert(offset + length <= width_);
1689
1690 bits_.erase(bits_.begin() + offset, bits_.begin() + offset + length);
1691 width_ = bits_.size();
1692
1693 check();
1694 }
1695
1696 RTLIL::SigSpec RTLIL::SigSpec::extract(int offset, int length) const
1697 {
1698 unpack();
1699 return std::vector<RTLIL::SigBit>(bits_.begin() + offset, bits_.begin() + offset + length);
1700 }
1701
1702 void RTLIL::SigSpec::append(const RTLIL::SigSpec &signal)
1703 {
1704 if (signal.width_ == 0)
1705 return;
1706
1707 if (width_ == 0) {
1708 *this = signal;
1709 return;
1710 }
1711
1712 if (packed() != signal.packed()) {
1713 pack();
1714 signal.pack();
1715 }
1716
1717 if (packed())
1718 for (auto &other_c : signal.chunks_)
1719 {
1720 auto &my_last_c = chunks_.back();
1721 if (my_last_c.wire == NULL && other_c.wire == NULL) {
1722 auto &this_data = my_last_c.data.bits;
1723 auto &other_data = other_c.data.bits;
1724 this_data.insert(this_data.end(), other_data.begin(), other_data.end());
1725 my_last_c.width += other_c.width;
1726 } else
1727 if (my_last_c.wire == other_c.wire && my_last_c.offset + my_last_c.width == other_c.offset) {
1728 my_last_c.width += other_c.width;
1729 } else
1730 chunks_.push_back(other_c);
1731 }
1732 else
1733 bits_.insert(bits_.end(), signal.bits_.begin(), signal.bits_.end());
1734
1735 width_ += signal.width_;
1736
1737 check();
1738 }
1739
1740 void RTLIL::SigSpec::append_bit(const RTLIL::SigBit &bit)
1741 {
1742 if (packed())
1743 {
1744 if (chunks_.size() == 0)
1745 chunks_.push_back(bit);
1746 else
1747 if (bit.wire == NULL)
1748 if (chunks_.back().wire == NULL) {
1749 chunks_.back().data.bits.push_back(bit.data);
1750 chunks_.back().width++;
1751 } else
1752 chunks_.push_back(bit);
1753 else
1754 if (chunks_.back().wire == bit.wire && chunks_.back().offset + chunks_.back().width == bit.offset)
1755 chunks_.back().width++;
1756 else
1757 chunks_.push_back(bit);
1758 }
1759 else
1760 bits_.push_back(bit);
1761
1762 width_++;
1763
1764 check();
1765 }
1766
1767 void RTLIL::SigSpec::extend(int width, bool is_signed)
1768 {
1769 pack();
1770
1771 if (width_ > width)
1772 remove(width, width_ - width);
1773
1774 if (width_ < width) {
1775 RTLIL::SigSpec padding = width_ > 0 ? extract(width_ - 1, 1) : RTLIL::SigSpec(RTLIL::State::S0);
1776 if (!is_signed && padding != RTLIL::SigSpec(RTLIL::State::Sx) && padding != RTLIL::SigSpec(RTLIL::State::Sz) &&
1777 padding != RTLIL::SigSpec(RTLIL::State::Sa) && padding != RTLIL::SigSpec(RTLIL::State::Sm))
1778 padding = RTLIL::SigSpec(RTLIL::State::S0);
1779 while (width_ < width)
1780 append(padding);
1781 }
1782 }
1783
1784 void RTLIL::SigSpec::extend_u0(int width, bool is_signed)
1785 {
1786 pack();
1787
1788 if (width_ > width)
1789 remove(width, width_ - width);
1790
1791 if (width_ < width) {
1792 RTLIL::SigSpec padding = width_ > 0 ? extract(width_ - 1, 1) : RTLIL::SigSpec(RTLIL::State::S0);
1793 if (!is_signed)
1794 padding = RTLIL::SigSpec(RTLIL::State::S0);
1795 while (width_ < width)
1796 append(padding);
1797 }
1798
1799 }
1800
1801 RTLIL::SigSpec RTLIL::SigSpec::repeat(int num) const
1802 {
1803 RTLIL::SigSpec sig;
1804 for (int i = 0; i < num; i++)
1805 sig.append(*this);
1806 return sig;
1807 }
1808
1809 void RTLIL::SigSpec::check() const
1810 {
1811 #ifndef NDEBUG
1812 if (packed())
1813 {
1814 int w = 0;
1815 for (size_t i = 0; i < chunks_.size(); i++) {
1816 const RTLIL::SigChunk chunk = chunks_[i];
1817 if (chunk.wire == NULL) {
1818 if (i > 0)
1819 assert(chunks_[i-1].wire != NULL);
1820 assert(chunk.offset == 0);
1821 assert(chunk.data.bits.size() == (size_t)chunk.width);
1822 } else {
1823 if (i > 0 && chunks_[i-1].wire == chunk.wire)
1824 assert(chunk.offset != chunks_[i-1].offset + chunks_[i-1].width);
1825 assert(chunk.offset >= 0);
1826 assert(chunk.width >= 0);
1827 assert(chunk.offset + chunk.width <= chunk.wire->width);
1828 assert(chunk.data.bits.size() == 0);
1829 }
1830 w += chunk.width;
1831 }
1832 assert(w == width_);
1833 assert(bits_.empty());
1834 }
1835 else
1836 {
1837 assert(width_ == SIZE(bits_));
1838 assert(chunks_.empty());
1839 }
1840 #endif
1841 }
1842
1843 bool RTLIL::SigSpec::operator <(const RTLIL::SigSpec &other) const
1844 {
1845 pack();
1846 other.pack();
1847
1848 if (width_ != other.width_)
1849 return width_ < other.width_;
1850
1851 RTLIL::SigSpec a = *this, b = other;
1852
1853 if (a.chunks_.size() != b.chunks_.size())
1854 return a.chunks_.size() < b.chunks_.size();
1855
1856 for (size_t i = 0; i < a.chunks_.size(); i++)
1857 if (a.chunks_[i] != b.chunks_[i])
1858 return a.chunks_[i] < b.chunks_[i];
1859
1860 return false;
1861 }
1862
1863 bool RTLIL::SigSpec::operator ==(const RTLIL::SigSpec &other) const
1864 {
1865 pack();
1866 other.pack();
1867
1868 if (width_ != other.width_)
1869 return false;
1870
1871 RTLIL::SigSpec a = *this, b = other;
1872
1873 if (a.chunks_.size() != b.chunks_.size())
1874 return false;
1875
1876 for (size_t i = 0; i < a.chunks_.size(); i++)
1877 if (a.chunks_[i] != b.chunks_[i])
1878 return false;
1879
1880 return true;
1881 }
1882
1883 bool RTLIL::SigSpec::is_fully_const() const
1884 {
1885 pack();
1886 for (auto it = chunks_.begin(); it != chunks_.end(); it++)
1887 if (it->width > 0 && it->wire != NULL)
1888 return false;
1889 return true;
1890 }
1891
1892 bool RTLIL::SigSpec::is_fully_def() const
1893 {
1894 pack();
1895 for (auto it = chunks_.begin(); it != chunks_.end(); it++) {
1896 if (it->width > 0 && it->wire != NULL)
1897 return false;
1898 for (size_t i = 0; i < it->data.bits.size(); i++)
1899 if (it->data.bits[i] != RTLIL::State::S0 && it->data.bits[i] != RTLIL::State::S1)
1900 return false;
1901 }
1902 return true;
1903 }
1904
1905 bool RTLIL::SigSpec::is_fully_undef() const
1906 {
1907 pack();
1908 for (auto it = chunks_.begin(); it != chunks_.end(); it++) {
1909 if (it->width > 0 && it->wire != NULL)
1910 return false;
1911 for (size_t i = 0; i < it->data.bits.size(); i++)
1912 if (it->data.bits[i] != RTLIL::State::Sx && it->data.bits[i] != RTLIL::State::Sz)
1913 return false;
1914 }
1915 return true;
1916 }
1917
1918 bool RTLIL::SigSpec::has_marked_bits() const
1919 {
1920 pack();
1921 for (auto it = chunks_.begin(); it != chunks_.end(); it++)
1922 if (it->width > 0 && it->wire == NULL) {
1923 for (size_t i = 0; i < it->data.bits.size(); i++)
1924 if (it->data.bits[i] == RTLIL::State::Sm)
1925 return true;
1926 }
1927 return false;
1928 }
1929
1930 bool RTLIL::SigSpec::as_bool() const
1931 {
1932 pack();
1933 assert(is_fully_const() && SIZE(chunks_) <= 1);
1934 if (width_)
1935 return chunks_[0].data.as_bool();
1936 return false;
1937 }
1938
1939 int RTLIL::SigSpec::as_int() const
1940 {
1941 pack();
1942 assert(is_fully_const() && SIZE(chunks_) <= 1);
1943 if (width_)
1944 return chunks_[0].data.as_int();
1945 return 0;
1946 }
1947
1948 std::string RTLIL::SigSpec::as_string() const
1949 {
1950 pack();
1951 std::string str;
1952 for (size_t i = chunks_.size(); i > 0; i--) {
1953 const RTLIL::SigChunk &chunk = chunks_[i-1];
1954 if (chunk.wire != NULL)
1955 for (int j = 0; j < chunk.width; j++)
1956 str += "?";
1957 else
1958 str += chunk.data.as_string();
1959 }
1960 return str;
1961 }
1962
1963 RTLIL::Const RTLIL::SigSpec::as_const() const
1964 {
1965 pack();
1966 assert(is_fully_const() && SIZE(chunks_) <= 1);
1967 if (width_)
1968 return chunks_[0].data;
1969 return RTLIL::Const();
1970 }
1971
1972 bool RTLIL::SigSpec::match(std::string pattern) const
1973 {
1974 pack();
1975 std::string str = as_string();
1976 assert(pattern.size() == str.size());
1977
1978 for (size_t i = 0; i < pattern.size(); i++) {
1979 if (pattern[i] == ' ')
1980 continue;
1981 if (pattern[i] == '*') {
1982 if (str[i] != 'z' && str[i] != 'x')
1983 return false;
1984 continue;
1985 }
1986 if (pattern[i] != str[i])
1987 return false;
1988 }
1989
1990 return true;
1991 }
1992
1993 std::set<RTLIL::SigBit> RTLIL::SigSpec::to_sigbit_set() const
1994 {
1995 pack();
1996 std::set<RTLIL::SigBit> sigbits;
1997 for (auto &c : chunks_)
1998 for (int i = 0; i < c.width; i++)
1999 sigbits.insert(RTLIL::SigBit(c, i));
2000 return sigbits;
2001 }
2002
2003 std::vector<RTLIL::SigBit> RTLIL::SigSpec::to_sigbit_vector() const
2004 {
2005 unpack();
2006 return bits_;
2007 }
2008
2009 RTLIL::SigBit RTLIL::SigSpec::to_single_sigbit() const
2010 {
2011 pack();
2012 log_assert(width_ == 1);
2013 for (auto &c : chunks_)
2014 if (c.width)
2015 return RTLIL::SigBit(c);
2016 log_abort();
2017 }
2018
2019 static void sigspec_parse_split(std::vector<std::string> &tokens, const std::string &text, char sep)
2020 {
2021 size_t start = 0, end = 0;
2022 while ((end = text.find(sep, start)) != std::string::npos) {
2023 tokens.push_back(text.substr(start, end - start));
2024 start = end + 1;
2025 }
2026 tokens.push_back(text.substr(start));
2027 }
2028
2029 static int sigspec_parse_get_dummy_line_num()
2030 {
2031 return 0;
2032 }
2033
2034 bool RTLIL::SigSpec::parse(RTLIL::SigSpec &sig, RTLIL::Module *module, std::string str)
2035 {
2036 std::vector<std::string> tokens;
2037 sigspec_parse_split(tokens, str, ',');
2038
2039 sig = RTLIL::SigSpec();
2040 for (int tokidx = int(tokens.size())-1; tokidx >= 0; tokidx--)
2041 {
2042 std::string netname = tokens[tokidx];
2043 std::string indices;
2044
2045 if (netname.size() == 0)
2046 continue;
2047
2048 if ('0' <= netname[0] && netname[0] <= '9') {
2049 AST::get_line_num = sigspec_parse_get_dummy_line_num;
2050 AST::AstNode *ast = VERILOG_FRONTEND::const2ast(netname);
2051 if (ast == NULL)
2052 return false;
2053 sig.append(RTLIL::Const(ast->bits));
2054 delete ast;
2055 continue;
2056 }
2057
2058 if (module == NULL)
2059 return false;
2060
2061 if (netname[0] != '$' && netname[0] != '\\')
2062 netname = "\\" + netname;
2063
2064 if (module->wires.count(netname) == 0) {
2065 size_t indices_pos = netname.size()-1;
2066 if (indices_pos > 2 && netname[indices_pos] == ']')
2067 {
2068 indices_pos--;
2069 while (indices_pos > 0 && ('0' <= netname[indices_pos] && netname[indices_pos] <= '9')) indices_pos--;
2070 if (indices_pos > 0 && netname[indices_pos] == ':') {
2071 indices_pos--;
2072 while (indices_pos > 0 && ('0' <= netname[indices_pos] && netname[indices_pos] <= '9')) indices_pos--;
2073 }
2074 if (indices_pos > 0 && netname[indices_pos] == '[') {
2075 indices = netname.substr(indices_pos);
2076 netname = netname.substr(0, indices_pos);
2077 }
2078 }
2079 }
2080
2081 if (module->wires.count(netname) == 0)
2082 return false;
2083
2084 RTLIL::Wire *wire = module->wires.at(netname);
2085 if (!indices.empty()) {
2086 std::vector<std::string> index_tokens;
2087 sigspec_parse_split(index_tokens, indices.substr(1, indices.size()-2), ':');
2088 if (index_tokens.size() == 1)
2089 sig.append(RTLIL::SigSpec(wire, atoi(index_tokens.at(0).c_str())));
2090 else {
2091 int a = atoi(index_tokens.at(0).c_str());
2092 int b = atoi(index_tokens.at(1).c_str());
2093 if (a > b) {
2094 int tmp = a;
2095 a = b, b = tmp;
2096 }
2097 sig.append(RTLIL::SigSpec(wire, a, b-a+1));
2098 }
2099 } else
2100 sig.append(wire);
2101 }
2102
2103 return true;
2104 }
2105
2106 bool RTLIL::SigSpec::parse_sel(RTLIL::SigSpec &sig, RTLIL::Design *design, RTLIL::Module *module, std::string str)
2107 {
2108 if (str.empty() || str[0] != '@')
2109 return parse(sig, module, str);
2110
2111 str = RTLIL::escape_id(str.substr(1));
2112 if (design->selection_vars.count(str) == 0)
2113 return false;
2114
2115 sig = RTLIL::SigSpec();
2116 RTLIL::Selection &sel = design->selection_vars.at(str);
2117 for (auto &it : module->wires)
2118 if (sel.selected_member(module->name, it.first))
2119 sig.append(it.second);
2120
2121 return true;
2122 }
2123
2124 bool RTLIL::SigSpec::parse_rhs(const RTLIL::SigSpec &lhs, RTLIL::SigSpec &sig, RTLIL::Module *module, std::string str)
2125 {
2126 if (str == "0") {
2127 sig = RTLIL::SigSpec(RTLIL::State::S0, lhs.width_);
2128 return true;
2129 }
2130
2131 if (str == "~0") {
2132 sig = RTLIL::SigSpec(RTLIL::State::S1, lhs.width_);
2133 return true;
2134 }
2135
2136 if (lhs.chunks_.size() == 1) {
2137 char *p = (char*)str.c_str(), *endptr;
2138 long long int val = strtoll(p, &endptr, 10);
2139 if (endptr && endptr != p && *endptr == 0) {
2140 sig = RTLIL::SigSpec(val, lhs.width_);
2141 return true;
2142 }
2143 }
2144
2145 return parse(sig, module, str);
2146 }
2147
2148 RTLIL::CaseRule::~CaseRule()
2149 {
2150 for (auto it = switches.begin(); it != switches.end(); it++)
2151 delete *it;
2152 }
2153
2154 RTLIL::CaseRule *RTLIL::CaseRule::clone() const
2155 {
2156 RTLIL::CaseRule *new_caserule = new RTLIL::CaseRule;
2157 new_caserule->compare = compare;
2158 new_caserule->actions = actions;
2159 for (auto &it : switches)
2160 new_caserule->switches.push_back(it->clone());
2161 return new_caserule;
2162 }
2163
2164 RTLIL::SwitchRule::~SwitchRule()
2165 {
2166 for (auto it = cases.begin(); it != cases.end(); it++)
2167 delete *it;
2168 }
2169
2170 RTLIL::SwitchRule *RTLIL::SwitchRule::clone() const
2171 {
2172 RTLIL::SwitchRule *new_switchrule = new RTLIL::SwitchRule;
2173 new_switchrule->signal = signal;
2174 new_switchrule->attributes = attributes;
2175 for (auto &it : cases)
2176 new_switchrule->cases.push_back(it->clone());
2177 return new_switchrule;
2178
2179 }
2180
2181 RTLIL::SyncRule *RTLIL::SyncRule::clone() const
2182 {
2183 RTLIL::SyncRule *new_syncrule = new RTLIL::SyncRule;
2184 new_syncrule->type = type;
2185 new_syncrule->signal = signal;
2186 new_syncrule->actions = actions;
2187 return new_syncrule;
2188 }
2189
2190 RTLIL::Process::~Process()
2191 {
2192 for (auto it = syncs.begin(); it != syncs.end(); it++)
2193 delete *it;
2194 }
2195
2196 RTLIL::Process *RTLIL::Process::clone() const
2197 {
2198 RTLIL::Process *new_proc = new RTLIL::Process;
2199
2200 new_proc->name = name;
2201 new_proc->attributes = attributes;
2202
2203 RTLIL::CaseRule *rc_ptr = root_case.clone();
2204 new_proc->root_case = *rc_ptr;
2205 rc_ptr->switches.clear();
2206 delete rc_ptr;
2207
2208 for (auto &it : syncs)
2209 new_proc->syncs.push_back(it->clone());
2210
2211 return new_proc;
2212 }
2213