Merge pull request #2203 from antmicro/fix-grammar
[yosys.git] / backends / cxxrtl / cxxrtl_backend.cc
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2019-2020 whitequark <whitequark@whitequark.org>
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/rtlil.h"
21 #include "kernel/register.h"
22 #include "kernel/sigtools.h"
23 #include "kernel/utils.h"
24 #include "kernel/celltypes.h"
25 #include "kernel/log.h"
26
27 USING_YOSYS_NAMESPACE
28 PRIVATE_NAMESPACE_BEGIN
29
30 // [[CITE]]
31 // Peter Eades; Xuemin Lin; W. F. Smyth, "A Fast Effective Heuristic For The Feedback Arc Set Problem"
32 // Information Processing Letters, Vol. 47, pp 319-323, 1993
33 // https://pdfs.semanticscholar.org/c7ed/d9acce96ca357876540e19664eb9d976637f.pdf
34
35 // A topological sort (on a cell/wire graph) is always possible in a fully flattened RTLIL design without
36 // processes or logic loops where every wire has a single driver. Logic loops are illegal in RTLIL and wires
37 // with multiple drivers can be split by the `splitnets` pass; however, interdependencies between processes
38 // or module instances can create strongly connected components without introducing evaluation nondeterminism.
39 // We wish to support designs with such benign SCCs (as well as designs with multiple drivers per wire), so
40 // we sort the graph in a way that minimizes feedback arcs. If there are no feedback arcs in the sorted graph,
41 // then a more efficient evaluation method is possible, since eval() will always immediately converge.
42 template<class T>
43 struct Scheduler {
44 struct Vertex {
45 T *data;
46 Vertex *prev, *next;
47 pool<Vertex*, hash_ptr_ops> preds, succs;
48
49 Vertex() : data(NULL), prev(this), next(this) {}
50 Vertex(T *data) : data(data), prev(NULL), next(NULL) {}
51
52 bool empty() const
53 {
54 log_assert(data == NULL);
55 if (next == this) {
56 log_assert(prev == next);
57 return true;
58 }
59 return false;
60 }
61
62 void link(Vertex *list)
63 {
64 log_assert(prev == NULL && next == NULL);
65 next = list;
66 prev = list->prev;
67 list->prev->next = this;
68 list->prev = this;
69 }
70
71 void unlink()
72 {
73 log_assert(prev->next == this && next->prev == this);
74 prev->next = next;
75 next->prev = prev;
76 next = prev = NULL;
77 }
78
79 int delta() const
80 {
81 return succs.size() - preds.size();
82 }
83 };
84
85 std::vector<Vertex*> vertices;
86 Vertex *sources = new Vertex;
87 Vertex *sinks = new Vertex;
88 dict<int, Vertex*> bins;
89
90 ~Scheduler()
91 {
92 delete sources;
93 delete sinks;
94 for (auto bin : bins)
95 delete bin.second;
96 for (auto vertex : vertices)
97 delete vertex;
98 }
99
100 Vertex *add(T *data)
101 {
102 Vertex *vertex = new Vertex(data);
103 vertices.push_back(vertex);
104 return vertex;
105 }
106
107 void relink(Vertex *vertex)
108 {
109 if (vertex->succs.empty())
110 vertex->link(sinks);
111 else if (vertex->preds.empty())
112 vertex->link(sources);
113 else {
114 int delta = vertex->delta();
115 if (!bins.count(delta))
116 bins[delta] = new Vertex;
117 vertex->link(bins[delta]);
118 }
119 }
120
121 Vertex *remove(Vertex *vertex)
122 {
123 vertex->unlink();
124 for (auto pred : vertex->preds) {
125 if (pred == vertex)
126 continue;
127 log_assert(pred->succs[vertex]);
128 pred->unlink();
129 pred->succs.erase(vertex);
130 relink(pred);
131 }
132 for (auto succ : vertex->succs) {
133 if (succ == vertex)
134 continue;
135 log_assert(succ->preds[vertex]);
136 succ->unlink();
137 succ->preds.erase(vertex);
138 relink(succ);
139 }
140 vertex->preds.clear();
141 vertex->succs.clear();
142 return vertex;
143 }
144
145 std::vector<Vertex*> schedule()
146 {
147 std::vector<Vertex*> s1, s2r;
148 for (auto vertex : vertices)
149 relink(vertex);
150 bool bins_empty = false;
151 while (!(sinks->empty() && sources->empty() && bins_empty)) {
152 while (!sinks->empty())
153 s2r.push_back(remove(sinks->next));
154 while (!sources->empty())
155 s1.push_back(remove(sources->next));
156 // Choosing u in this implementation isn't O(1), but the paper handwaves which data structure they suggest
157 // using to get O(1) relinking *and* find-max-key ("it is clear"... no it isn't), so this code uses a very
158 // naive implementation of find-max-key.
159 bins_empty = true;
160 bins.template sort<std::greater<int>>();
161 for (auto bin : bins) {
162 if (!bin.second->empty()) {
163 bins_empty = false;
164 s1.push_back(remove(bin.second->next));
165 break;
166 }
167 }
168 }
169 s1.insert(s1.end(), s2r.rbegin(), s2r.rend());
170 return s1;
171 }
172 };
173
174 bool is_unary_cell(RTLIL::IdString type)
175 {
176 return type.in(
177 ID($not), ID($logic_not), ID($reduce_and), ID($reduce_or), ID($reduce_xor), ID($reduce_xnor), ID($reduce_bool),
178 ID($pos), ID($neg));
179 }
180
181 bool is_binary_cell(RTLIL::IdString type)
182 {
183 return type.in(
184 ID($and), ID($or), ID($xor), ID($xnor), ID($logic_and), ID($logic_or),
185 ID($shl), ID($sshl), ID($shr), ID($sshr), ID($shift), ID($shiftx),
186 ID($eq), ID($ne), ID($eqx), ID($nex), ID($gt), ID($ge), ID($lt), ID($le),
187 ID($add), ID($sub), ID($mul), ID($div), ID($mod));
188 }
189
190 bool is_extending_cell(RTLIL::IdString type)
191 {
192 return !type.in(
193 ID($logic_not), ID($logic_and), ID($logic_or),
194 ID($reduce_and), ID($reduce_or), ID($reduce_xor), ID($reduce_xnor), ID($reduce_bool));
195 }
196
197 bool is_elidable_cell(RTLIL::IdString type)
198 {
199 return is_unary_cell(type) || is_binary_cell(type) || type.in(
200 ID($mux), ID($concat), ID($slice), ID($pmux));
201 }
202
203 bool is_sync_ff_cell(RTLIL::IdString type)
204 {
205 return type.in(
206 ID($dff), ID($dffe), ID($sdff), ID($sdffe), ID($sdffce));
207 }
208
209 bool is_ff_cell(RTLIL::IdString type)
210 {
211 return is_sync_ff_cell(type) || type.in(
212 ID($adff), ID($adffe), ID($dffsr), ID($dffsre), ID($dlatch), ID($adlatch), ID($dlatchsr), ID($sr));
213 }
214
215 bool is_internal_cell(RTLIL::IdString type)
216 {
217 return type[0] == '$' && !type.begins_with("$paramod");
218 }
219
220 bool is_cxxrtl_blackbox_cell(const RTLIL::Cell *cell)
221 {
222 RTLIL::Module *cell_module = cell->module->design->module(cell->type);
223 log_assert(cell_module != nullptr);
224 return cell_module->get_bool_attribute(ID(cxxrtl_blackbox));
225 }
226
227 enum class CxxrtlPortType {
228 UNKNOWN = 0, // or mixed comb/sync
229 COMB = 1,
230 SYNC = 2,
231 };
232
233 CxxrtlPortType cxxrtl_port_type(const RTLIL::Cell *cell, RTLIL::IdString port)
234 {
235 RTLIL::Module *cell_module = cell->module->design->module(cell->type);
236 if (cell_module == nullptr || !cell_module->get_bool_attribute(ID(cxxrtl_blackbox)))
237 return CxxrtlPortType::UNKNOWN;
238 RTLIL::Wire *cell_output_wire = cell_module->wire(port);
239 log_assert(cell_output_wire != nullptr);
240 bool is_comb = cell_output_wire->get_bool_attribute(ID(cxxrtl_comb));
241 bool is_sync = cell_output_wire->get_bool_attribute(ID(cxxrtl_sync));
242 if (is_comb && is_sync)
243 log_cmd_error("Port `%s.%s' is marked as both `cxxrtl_comb` and `cxxrtl_sync`.\n",
244 log_id(cell_module), log_signal(cell_output_wire));
245 else if (is_comb)
246 return CxxrtlPortType::COMB;
247 else if (is_sync)
248 return CxxrtlPortType::SYNC;
249 return CxxrtlPortType::UNKNOWN;
250 }
251
252 bool is_cxxrtl_comb_port(const RTLIL::Cell *cell, RTLIL::IdString port)
253 {
254 return cxxrtl_port_type(cell, port) == CxxrtlPortType::COMB;
255 }
256
257 bool is_cxxrtl_sync_port(const RTLIL::Cell *cell, RTLIL::IdString port)
258 {
259 return cxxrtl_port_type(cell, port) == CxxrtlPortType::SYNC;
260 }
261
262 struct FlowGraph {
263 struct Node {
264 enum class Type {
265 CONNECT,
266 CELL_SYNC,
267 CELL_EVAL,
268 PROCESS
269 };
270
271 Type type;
272 RTLIL::SigSig connect = {};
273 const RTLIL::Cell *cell = NULL;
274 const RTLIL::Process *process = NULL;
275 };
276
277 std::vector<Node*> nodes;
278 dict<const RTLIL::Wire*, pool<Node*, hash_ptr_ops>> wire_comb_defs, wire_sync_defs, wire_uses;
279 dict<const RTLIL::Wire*, bool> wire_def_elidable, wire_use_elidable;
280
281 ~FlowGraph()
282 {
283 for (auto node : nodes)
284 delete node;
285 }
286
287 void add_defs(Node *node, const RTLIL::SigSpec &sig, bool fully_sync, bool elidable)
288 {
289 for (auto chunk : sig.chunks())
290 if (chunk.wire) {
291 if (fully_sync)
292 wire_sync_defs[chunk.wire].insert(node);
293 else
294 wire_comb_defs[chunk.wire].insert(node);
295 }
296 // Only comb defs of an entire wire in the right order can be elided.
297 if (!fully_sync && sig.is_wire())
298 wire_def_elidable[sig.as_wire()] = elidable;
299 }
300
301 void add_uses(Node *node, const RTLIL::SigSpec &sig)
302 {
303 for (auto chunk : sig.chunks())
304 if (chunk.wire) {
305 wire_uses[chunk.wire].insert(node);
306 // Only a single use of an entire wire in the right order can be elided.
307 // (But the use can include other chunks.)
308 if (!wire_use_elidable.count(chunk.wire))
309 wire_use_elidable[chunk.wire] = true;
310 else
311 wire_use_elidable[chunk.wire] = false;
312 }
313 }
314
315 bool is_elidable(const RTLIL::Wire *wire) const
316 {
317 if (wire_def_elidable.count(wire) && wire_use_elidable.count(wire))
318 return wire_def_elidable.at(wire) && wire_use_elidable.at(wire);
319 return false;
320 }
321
322 // Connections
323 void add_connect_defs_uses(Node *node, const RTLIL::SigSig &conn)
324 {
325 add_defs(node, conn.first, /*fully_sync=*/false, /*elidable=*/true);
326 add_uses(node, conn.second);
327 }
328
329 Node *add_node(const RTLIL::SigSig &conn)
330 {
331 Node *node = new Node;
332 node->type = Node::Type::CONNECT;
333 node->connect = conn;
334 nodes.push_back(node);
335 add_connect_defs_uses(node, conn);
336 return node;
337 }
338
339 // Cells
340 void add_cell_sync_defs(Node *node, const RTLIL::Cell *cell)
341 {
342 // To understand why this node type is necessary and why it produces comb defs, consider a cell
343 // with input \i and sync output \o, used in a design such that \i is connected to \o. This does
344 // not result in a feedback arc because the output is synchronous. However, a naive implementation
345 // of code generation for cells that assigns to inputs, evaluates cells, assigns from outputs
346 // would not be able to immediately converge...
347 //
348 // wire<1> i_tmp;
349 // cell->p_i = i_tmp.curr;
350 // cell->eval();
351 // i_tmp.next = cell->p_o.curr;
352 //
353 // ... since the wire connecting the input and output ports would not be localizable. To solve
354 // this, the cell is split into two scheduling nodes; one exclusively for sync outputs, and
355 // another for inputs and all non-sync outputs. This way the generated code can be rearranged...
356 //
357 // value<1> i_tmp;
358 // i_tmp = cell->p_o.curr;
359 // cell->p_i = i_tmp;
360 // cell->eval();
361 //
362 // eliminating the unnecessary delta cycle. Conceptually, the CELL_SYNC node type is a series of
363 // connections of the form `connect \lhs \cell.\sync_output`; the right-hand side of these is not
364 // expressible as a wire in RTLIL. If it was expressible, then `\cell.\sync_output` would have
365 // a sync def, and this node would be an ordinary CONNECT node, with `\lhs` having a comb def.
366 // Because it isn't, a special node type is used, the right-hand side does not appear anywhere,
367 // and the left-hand side has a comb def.
368 for (auto conn : cell->connections())
369 if (cell->output(conn.first))
370 if (is_cxxrtl_sync_port(cell, conn.first)) {
371 // See note regarding elidability below.
372 add_defs(node, conn.second, /*fully_sync=*/false, /*elidable=*/false);
373 }
374 }
375
376 void add_cell_eval_defs_uses(Node *node, const RTLIL::Cell *cell)
377 {
378 for (auto conn : cell->connections()) {
379 if (cell->output(conn.first)) {
380 if (is_elidable_cell(cell->type))
381 add_defs(node, conn.second, /*fully_sync=*/false, /*elidable=*/true);
382 else if (is_sync_ff_cell(cell->type) || (cell->type == ID($memrd) && cell->getParam(ID::CLK_ENABLE).as_bool()))
383 add_defs(node, conn.second, /*fully_sync=*/true, /*elidable=*/false);
384 else if (is_internal_cell(cell->type))
385 add_defs(node, conn.second, /*fully_sync=*/false, /*elidable=*/false);
386 else if (!is_cxxrtl_sync_port(cell, conn.first)) {
387 // Although at first it looks like outputs of user-defined cells may always be elided, the reality is
388 // more complex. Fully sync outputs produce no defs and so don't participate in elision. Fully comb
389 // outputs are assigned in a different way depending on whether the cell's eval() immediately converged.
390 // Unknown/mixed outputs could be elided, but should be rare in practical designs and don't justify
391 // the infrastructure required to elide outputs of cells with many of them.
392 add_defs(node, conn.second, /*fully_sync=*/false, /*elidable=*/false);
393 }
394 }
395 if (cell->input(conn.first))
396 add_uses(node, conn.second);
397 }
398 }
399
400 Node *add_node(const RTLIL::Cell *cell)
401 {
402 log_assert(cell->known());
403
404 bool has_fully_sync_outputs = false;
405 for (auto conn : cell->connections())
406 if (cell->output(conn.first) && is_cxxrtl_sync_port(cell, conn.first)) {
407 has_fully_sync_outputs = true;
408 break;
409 }
410 if (has_fully_sync_outputs) {
411 Node *node = new Node;
412 node->type = Node::Type::CELL_SYNC;
413 node->cell = cell;
414 nodes.push_back(node);
415 add_cell_sync_defs(node, cell);
416 }
417
418 Node *node = new Node;
419 node->type = Node::Type::CELL_EVAL;
420 node->cell = cell;
421 nodes.push_back(node);
422 add_cell_eval_defs_uses(node, cell);
423 return node;
424 }
425
426 // Processes
427 void add_case_defs_uses(Node *node, const RTLIL::CaseRule *case_)
428 {
429 for (auto &action : case_->actions) {
430 add_defs(node, action.first, /*is_sync=*/false, /*elidable=*/false);
431 add_uses(node, action.second);
432 }
433 for (auto sub_switch : case_->switches) {
434 add_uses(node, sub_switch->signal);
435 for (auto sub_case : sub_switch->cases) {
436 for (auto &compare : sub_case->compare)
437 add_uses(node, compare);
438 add_case_defs_uses(node, sub_case);
439 }
440 }
441 }
442
443 void add_process_defs_uses(Node *node, const RTLIL::Process *process)
444 {
445 add_case_defs_uses(node, &process->root_case);
446 for (auto sync : process->syncs)
447 for (auto action : sync->actions) {
448 if (sync->type == RTLIL::STp || sync->type == RTLIL::STn || sync->type == RTLIL::STe)
449 add_defs(node, action.first, /*is_sync=*/true, /*elidable=*/false);
450 else
451 add_defs(node, action.first, /*is_sync=*/false, /*elidable=*/false);
452 add_uses(node, action.second);
453 }
454 }
455
456 Node *add_node(const RTLIL::Process *process)
457 {
458 Node *node = new Node;
459 node->type = Node::Type::PROCESS;
460 node->process = process;
461 nodes.push_back(node);
462 add_process_defs_uses(node, process);
463 return node;
464 }
465 };
466
467 std::vector<std::string> split_by(const std::string &str, const std::string &sep)
468 {
469 std::vector<std::string> result;
470 size_t prev = 0;
471 while (true) {
472 size_t curr = str.find_first_of(sep, prev);
473 if (curr == std::string::npos) {
474 std::string part = str.substr(prev);
475 if (!part.empty()) result.push_back(part);
476 break;
477 } else {
478 std::string part = str.substr(prev, curr - prev);
479 if (!part.empty()) result.push_back(part);
480 prev = curr + 1;
481 }
482 }
483 return result;
484 }
485
486 std::string escape_cxx_string(const std::string &input)
487 {
488 std::string output = "\"";
489 for (auto c : input) {
490 if (::isprint(c)) {
491 if (c == '\\')
492 output.push_back('\\');
493 output.push_back(c);
494 } else {
495 char l = c & 0xf, h = (c >> 4) & 0xf;
496 output.append("\\x");
497 output.push_back((h < 10 ? '0' + h : 'a' + h - 10));
498 output.push_back((l < 10 ? '0' + l : 'a' + l - 10));
499 }
500 }
501 output.push_back('"');
502 if (output.find('\0') != std::string::npos) {
503 output.insert(0, "std::string {");
504 output.append(stringf(", %zu}", input.size()));
505 }
506 return output;
507 }
508
509 template<class T>
510 std::string get_hdl_name(T *object)
511 {
512 if (object->has_attribute(ID::hdlname))
513 return object->get_string_attribute(ID::hdlname);
514 else
515 return object->name.str().substr(1);
516 }
517
518 struct CxxrtlWorker {
519 bool split_intf = false;
520 std::string intf_filename;
521 std::string design_ns = "cxxrtl_design";
522 std::ostream *impl_f = nullptr;
523 std::ostream *intf_f = nullptr;
524
525 bool run_flatten = false;
526 bool run_proc = false;
527
528 bool unbuffer_internal = false;
529 bool unbuffer_public = false;
530 bool localize_internal = false;
531 bool localize_public = false;
532 bool elide_internal = false;
533 bool elide_public = false;
534
535 bool debug_info = false;
536
537 std::ostringstream f;
538 std::string indent;
539 int temporary = 0;
540
541 dict<const RTLIL::Module*, SigMap> sigmaps;
542 pool<const RTLIL::Wire*> edge_wires;
543 dict<RTLIL::SigBit, RTLIL::SyncType> edge_types;
544 pool<const RTLIL::Memory*> writable_memories;
545 dict<const RTLIL::Cell*, pool<const RTLIL::Cell*>> transparent_for;
546 dict<const RTLIL::Wire*, FlowGraph::Node> elided_wires;
547 dict<const RTLIL::Module*, std::vector<FlowGraph::Node>> schedule;
548 pool<const RTLIL::Wire*> unbuffered_wires;
549 pool<const RTLIL::Wire*> localized_wires;
550 dict<const RTLIL::Wire*, const RTLIL::Wire*> debug_alias_wires;
551 dict<const RTLIL::Wire*, RTLIL::Const> debug_const_wires;
552 dict<const RTLIL::Module*, pool<std::string>> blackbox_specializations;
553 dict<const RTLIL::Module*, bool> eval_converges;
554
555 void inc_indent() {
556 indent += "\t";
557 }
558 void dec_indent() {
559 indent.resize(indent.size() - 1);
560 }
561
562 // RTLIL allows any characters in names other than whitespace. This presents an issue for generating C++ code
563 // because C++ identifiers may be only alphanumeric, cannot clash with C++ keywords, and cannot clash with cxxrtl
564 // identifiers. This issue can be solved with a name mangling scheme. We choose a name mangling scheme that results
565 // in readable identifiers, does not depend on an up-to-date list of C++ keywords, and is easy to apply. Its rules:
566 // 1. All generated identifiers start with `_`.
567 // 1a. Generated identifiers for public names (beginning with `\`) start with `p_`.
568 // 1b. Generated identifiers for internal names (beginning with `$`) start with `i_`.
569 // 2. An underscore is escaped with another underscore, i.e. `__`.
570 // 3. Any other non-alnum character is escaped with underscores around its lowercase hex code, e.g. `@` as `_40_`.
571 std::string mangle_name(const RTLIL::IdString &name)
572 {
573 std::string mangled;
574 bool first = true;
575 for (char c : name.str()) {
576 if (first) {
577 first = false;
578 if (c == '\\')
579 mangled += "p_";
580 else if (c == '$')
581 mangled += "i_";
582 else
583 log_assert(false);
584 } else {
585 if (isalnum(c)) {
586 mangled += c;
587 } else if (c == '_') {
588 mangled += "__";
589 } else {
590 char l = c & 0xf, h = (c >> 4) & 0xf;
591 mangled += '_';
592 mangled += (h < 10 ? '0' + h : 'a' + h - 10);
593 mangled += (l < 10 ? '0' + l : 'a' + l - 10);
594 mangled += '_';
595 }
596 }
597 }
598 return mangled;
599 }
600
601 std::string mangle_module_name(const RTLIL::IdString &name, bool is_blackbox = false)
602 {
603 // Class namespace.
604 if (is_blackbox)
605 return "bb_" + mangle_name(name);
606 return mangle_name(name);
607 }
608
609 std::string mangle_memory_name(const RTLIL::IdString &name)
610 {
611 // Class member namespace.
612 return "memory_" + mangle_name(name);
613 }
614
615 std::string mangle_cell_name(const RTLIL::IdString &name)
616 {
617 // Class member namespace.
618 return "cell_" + mangle_name(name);
619 }
620
621 std::string mangle_wire_name(const RTLIL::IdString &name)
622 {
623 // Class member namespace.
624 return mangle_name(name);
625 }
626
627 std::string mangle(const RTLIL::Module *module)
628 {
629 return mangle_module_name(module->name, /*is_blackbox=*/module->get_bool_attribute(ID(cxxrtl_blackbox)));
630 }
631
632 std::string mangle(const RTLIL::Memory *memory)
633 {
634 return mangle_memory_name(memory->name);
635 }
636
637 std::string mangle(const RTLIL::Cell *cell)
638 {
639 return mangle_cell_name(cell->name);
640 }
641
642 std::string mangle(const RTLIL::Wire *wire)
643 {
644 return mangle_wire_name(wire->name);
645 }
646
647 std::string mangle(RTLIL::SigBit sigbit)
648 {
649 log_assert(sigbit.wire != NULL);
650 if (sigbit.wire->width == 1)
651 return mangle(sigbit.wire);
652 return mangle(sigbit.wire) + "_" + std::to_string(sigbit.offset);
653 }
654
655 std::vector<std::string> template_param_names(const RTLIL::Module *module)
656 {
657 if (!module->has_attribute(ID(cxxrtl_template)))
658 return {};
659
660 if (module->attributes.at(ID(cxxrtl_template)).flags != RTLIL::CONST_FLAG_STRING)
661 log_cmd_error("Attribute `cxxrtl_template' of module `%s' is not a string.\n", log_id(module));
662
663 std::vector<std::string> param_names = split_by(module->get_string_attribute(ID(cxxrtl_template)), " \t");
664 for (const auto &param_name : param_names) {
665 // Various lowercase prefixes (p_, i_, cell_, ...) are used for member variables, so require
666 // parameters to start with an uppercase letter to avoid name conflicts. (This is the convention
667 // in both Verilog and C++, anyway.)
668 if (!isupper(param_name[0]))
669 log_cmd_error("Attribute `cxxrtl_template' of module `%s' includes a parameter `%s', "
670 "which does not start with an uppercase letter.\n",
671 log_id(module), param_name.c_str());
672 }
673 return param_names;
674 }
675
676 std::string template_params(const RTLIL::Module *module, bool is_decl)
677 {
678 std::vector<std::string> param_names = template_param_names(module);
679 if (param_names.empty())
680 return "";
681
682 std::string params = "<";
683 bool first = true;
684 for (const auto &param_name : param_names) {
685 if (!first)
686 params += ", ";
687 first = false;
688 if (is_decl)
689 params += "size_t ";
690 params += param_name;
691 }
692 params += ">";
693 return params;
694 }
695
696 std::string template_args(const RTLIL::Cell *cell)
697 {
698 RTLIL::Module *cell_module = cell->module->design->module(cell->type);
699 log_assert(cell_module != nullptr);
700 if (!cell_module->get_bool_attribute(ID(cxxrtl_blackbox)))
701 return "";
702
703 std::vector<std::string> param_names = template_param_names(cell_module);
704 if (param_names.empty())
705 return "";
706
707 std::string params = "<";
708 bool first = true;
709 for (const auto &param_name : param_names) {
710 if (!first)
711 params += ", ";
712 first = false;
713 params += "/*" + param_name + "=*/";
714 RTLIL::IdString id_param_name = '\\' + param_name;
715 if (!cell->hasParam(id_param_name))
716 log_cmd_error("Cell `%s.%s' does not have a parameter `%s', which is required by the templated module `%s'.\n",
717 log_id(cell->module), log_id(cell), param_name.c_str(), log_id(cell_module));
718 RTLIL::Const param_value = cell->getParam(id_param_name);
719 if (((param_value.flags & ~RTLIL::CONST_FLAG_SIGNED) != 0) || param_value.as_int() < 0)
720 log_cmd_error("Parameter `%s' of cell `%s.%s', which is required by the templated module `%s', "
721 "is not a positive integer.\n",
722 param_name.c_str(), log_id(cell->module), log_id(cell), log_id(cell_module));
723 params += std::to_string(cell->getParam(id_param_name).as_int());
724 }
725 params += ">";
726 return params;
727 }
728
729 std::string fresh_temporary()
730 {
731 return stringf("tmp_%d", temporary++);
732 }
733
734 void dump_attrs(const RTLIL::AttrObject *object)
735 {
736 for (auto attr : object->attributes) {
737 f << indent << "// " << attr.first.str() << ": ";
738 if (attr.second.flags & RTLIL::CONST_FLAG_STRING) {
739 f << attr.second.decode_string();
740 } else {
741 f << attr.second.as_int(/*is_signed=*/attr.second.flags & RTLIL::CONST_FLAG_SIGNED);
742 }
743 f << "\n";
744 }
745 }
746
747 void dump_const_init(const RTLIL::Const &data, int width, int offset = 0, bool fixed_width = false)
748 {
749 const int CHUNK_SIZE = 32;
750 f << "{";
751 while (width > 0) {
752 int chunk_width = min(width, CHUNK_SIZE);
753 uint32_t chunk = data.extract(offset, chunk_width).as_int();
754 if (fixed_width)
755 f << stringf("0x%.*xu", (3 + chunk_width) / 4, chunk);
756 else
757 f << stringf("%#xu", chunk);
758 if (width > CHUNK_SIZE)
759 f << ',';
760 offset += CHUNK_SIZE;
761 width -= CHUNK_SIZE;
762 }
763 f << "}";
764 }
765
766 void dump_const_init(const RTLIL::Const &data)
767 {
768 dump_const_init(data, data.size());
769 }
770
771 void dump_const(const RTLIL::Const &data, int width, int offset = 0, bool fixed_width = false)
772 {
773 f << "value<" << width << ">";
774 dump_const_init(data, width, offset, fixed_width);
775 }
776
777 void dump_const(const RTLIL::Const &data)
778 {
779 dump_const(data, data.size());
780 }
781
782 bool dump_sigchunk(const RTLIL::SigChunk &chunk, bool is_lhs)
783 {
784 if (chunk.wire == NULL) {
785 dump_const(chunk.data, chunk.width, chunk.offset);
786 return false;
787 } else {
788 if (elided_wires.count(chunk.wire)) {
789 log_assert(!is_lhs);
790 const FlowGraph::Node &node = elided_wires[chunk.wire];
791 switch (node.type) {
792 case FlowGraph::Node::Type::CONNECT:
793 dump_connect_elided(node.connect);
794 break;
795 case FlowGraph::Node::Type::CELL_EVAL:
796 log_assert(is_elidable_cell(node.cell->type));
797 dump_cell_elided(node.cell);
798 break;
799 default:
800 log_assert(false);
801 }
802 } else if (unbuffered_wires[chunk.wire]) {
803 f << mangle(chunk.wire);
804 } else {
805 f << mangle(chunk.wire) << (is_lhs ? ".next" : ".curr");
806 }
807 if (chunk.width == chunk.wire->width && chunk.offset == 0)
808 return false;
809 else if (chunk.width == 1)
810 f << ".slice<" << chunk.offset << ">()";
811 else
812 f << ".slice<" << chunk.offset+chunk.width-1 << "," << chunk.offset << ">()";
813 return true;
814 }
815 }
816
817 bool dump_sigspec(const RTLIL::SigSpec &sig, bool is_lhs)
818 {
819 if (sig.empty()) {
820 f << "value<0>()";
821 return false;
822 } else if (sig.is_chunk()) {
823 return dump_sigchunk(sig.as_chunk(), is_lhs);
824 } else {
825 dump_sigchunk(*sig.chunks().rbegin(), is_lhs);
826 for (auto it = sig.chunks().rbegin() + 1; it != sig.chunks().rend(); ++it) {
827 f << ".concat(";
828 dump_sigchunk(*it, is_lhs);
829 f << ")";
830 }
831 return true;
832 }
833 }
834
835 void dump_sigspec_lhs(const RTLIL::SigSpec &sig)
836 {
837 dump_sigspec(sig, /*is_lhs=*/true);
838 }
839
840 void dump_sigspec_rhs(const RTLIL::SigSpec &sig)
841 {
842 // In the contexts where we want template argument deduction to occur for `template<size_t Bits> ... value<Bits>`,
843 // it is necessary to have the argument to already be a `value<N>`, since template argument deduction and implicit
844 // type conversion are mutually exclusive. In these contexts, we use dump_sigspec_rhs() to emit an explicit
845 // type conversion, but only if the expression needs it.
846 bool is_complex = dump_sigspec(sig, /*is_lhs=*/false);
847 if (is_complex)
848 f << ".val()";
849 }
850
851 void collect_sigspec_rhs(const RTLIL::SigSpec &sig, std::vector<RTLIL::IdString> &cells)
852 {
853 for (auto chunk : sig.chunks()) {
854 if (!chunk.wire || !elided_wires.count(chunk.wire))
855 continue;
856
857 const FlowGraph::Node &node = elided_wires[chunk.wire];
858 switch (node.type) {
859 case FlowGraph::Node::Type::CONNECT:
860 collect_connect(node.connect, cells);
861 break;
862 case FlowGraph::Node::Type::CELL_EVAL:
863 collect_cell_eval(node.cell, cells);
864 break;
865 default:
866 log_assert(false);
867 }
868 }
869 }
870
871 void dump_connect_elided(const RTLIL::SigSig &conn)
872 {
873 dump_sigspec_rhs(conn.second);
874 }
875
876 bool is_connect_elided(const RTLIL::SigSig &conn)
877 {
878 return conn.first.is_wire() && elided_wires.count(conn.first.as_wire());
879 }
880
881 void collect_connect(const RTLIL::SigSig &conn, std::vector<RTLIL::IdString> &cells)
882 {
883 if (!is_connect_elided(conn))
884 return;
885
886 collect_sigspec_rhs(conn.second, cells);
887 }
888
889 void dump_connect(const RTLIL::SigSig &conn)
890 {
891 if (is_connect_elided(conn))
892 return;
893
894 f << indent << "// connection\n";
895 f << indent;
896 dump_sigspec_lhs(conn.first);
897 f << " = ";
898 dump_connect_elided(conn);
899 f << ";\n";
900 }
901
902 void dump_cell_sync(const RTLIL::Cell *cell)
903 {
904 const char *access = is_cxxrtl_blackbox_cell(cell) ? "->" : ".";
905 f << indent << "// cell " << cell->name.str() << " syncs\n";
906 for (auto conn : cell->connections())
907 if (cell->output(conn.first))
908 if (is_cxxrtl_sync_port(cell, conn.first)) {
909 f << indent;
910 dump_sigspec_lhs(conn.second);
911 f << " = " << mangle(cell) << access << mangle_wire_name(conn.first) << ".curr;\n";
912 }
913 }
914
915 void dump_cell_elided(const RTLIL::Cell *cell)
916 {
917 // Unary cells
918 if (is_unary_cell(cell->type)) {
919 f << cell->type.substr(1);
920 if (is_extending_cell(cell->type))
921 f << '_' << (cell->getParam(ID::A_SIGNED).as_bool() ? 's' : 'u');
922 f << "<" << cell->getParam(ID::Y_WIDTH).as_int() << ">(";
923 dump_sigspec_rhs(cell->getPort(ID::A));
924 f << ")";
925 // Binary cells
926 } else if (is_binary_cell(cell->type)) {
927 f << cell->type.substr(1);
928 if (is_extending_cell(cell->type))
929 f << '_' << (cell->getParam(ID::A_SIGNED).as_bool() ? 's' : 'u') <<
930 (cell->getParam(ID::B_SIGNED).as_bool() ? 's' : 'u');
931 f << "<" << cell->getParam(ID::Y_WIDTH).as_int() << ">(";
932 dump_sigspec_rhs(cell->getPort(ID::A));
933 f << ", ";
934 dump_sigspec_rhs(cell->getPort(ID::B));
935 f << ")";
936 // Muxes
937 } else if (cell->type == ID($mux)) {
938 f << "(";
939 dump_sigspec_rhs(cell->getPort(ID::S));
940 f << " ? ";
941 dump_sigspec_rhs(cell->getPort(ID::B));
942 f << " : ";
943 dump_sigspec_rhs(cell->getPort(ID::A));
944 f << ")";
945 // Parallel (one-hot) muxes
946 } else if (cell->type == ID($pmux)) {
947 int width = cell->getParam(ID::WIDTH).as_int();
948 int s_width = cell->getParam(ID::S_WIDTH).as_int();
949 for (int part = 0; part < s_width; part++) {
950 f << "(";
951 dump_sigspec_rhs(cell->getPort(ID::S).extract(part));
952 f << " ? ";
953 dump_sigspec_rhs(cell->getPort(ID::B).extract(part * width, width));
954 f << " : ";
955 }
956 dump_sigspec_rhs(cell->getPort(ID::A));
957 for (int part = 0; part < s_width; part++) {
958 f << ")";
959 }
960 // Concats
961 } else if (cell->type == ID($concat)) {
962 dump_sigspec_rhs(cell->getPort(ID::B));
963 f << ".concat(";
964 dump_sigspec_rhs(cell->getPort(ID::A));
965 f << ").val()";
966 // Slices
967 } else if (cell->type == ID($slice)) {
968 dump_sigspec_rhs(cell->getPort(ID::A));
969 f << ".slice<";
970 f << cell->getParam(ID::OFFSET).as_int() + cell->getParam(ID::Y_WIDTH).as_int() - 1;
971 f << ",";
972 f << cell->getParam(ID::OFFSET).as_int();
973 f << ">().val()";
974 } else {
975 log_assert(false);
976 }
977 }
978
979 bool is_cell_elided(const RTLIL::Cell *cell)
980 {
981 return is_elidable_cell(cell->type) && cell->hasPort(ID::Y) && cell->getPort(ID::Y).is_wire() &&
982 elided_wires.count(cell->getPort(ID::Y).as_wire());
983 }
984
985 void collect_cell_eval(const RTLIL::Cell *cell, std::vector<RTLIL::IdString> &cells)
986 {
987 if (!is_cell_elided(cell))
988 return;
989
990 cells.push_back(cell->name);
991 for (auto port : cell->connections())
992 if (port.first != ID::Y)
993 collect_sigspec_rhs(port.second, cells);
994 }
995
996 void dump_cell_eval(const RTLIL::Cell *cell)
997 {
998 if (is_cell_elided(cell))
999 return;
1000 if (cell->type == ID($meminit))
1001 return; // Handled elsewhere.
1002
1003 std::vector<RTLIL::IdString> elided_cells;
1004 if (is_elidable_cell(cell->type)) {
1005 for (auto port : cell->connections())
1006 if (port.first != ID::Y)
1007 collect_sigspec_rhs(port.second, elided_cells);
1008 }
1009 if (elided_cells.empty()) {
1010 dump_attrs(cell);
1011 f << indent << "// cell " << cell->name.str() << "\n";
1012 } else {
1013 f << indent << "// cells";
1014 for (auto elided_cell : elided_cells)
1015 f << " " << elided_cell.str();
1016 f << "\n";
1017 }
1018
1019 // Elidable cells
1020 if (is_elidable_cell(cell->type)) {
1021 f << indent;
1022 dump_sigspec_lhs(cell->getPort(ID::Y));
1023 f << " = ";
1024 dump_cell_elided(cell);
1025 f << ";\n";
1026 // Flip-flops
1027 } else if (is_ff_cell(cell->type)) {
1028 if (cell->hasPort(ID::CLK) && cell->getPort(ID::CLK).is_wire()) {
1029 // Edge-sensitive logic
1030 RTLIL::SigBit clk_bit = cell->getPort(ID::CLK)[0];
1031 clk_bit = sigmaps[clk_bit.wire->module](clk_bit);
1032 f << indent << "if (" << (cell->getParam(ID::CLK_POLARITY).as_bool() ? "posedge_" : "negedge_")
1033 << mangle(clk_bit) << ") {\n";
1034 inc_indent();
1035 if (cell->hasPort(ID::EN)) {
1036 f << indent << "if (";
1037 dump_sigspec_rhs(cell->getPort(ID::EN));
1038 f << " == value<1> {" << cell->getParam(ID::EN_POLARITY).as_bool() << "u}) {\n";
1039 inc_indent();
1040 }
1041 f << indent;
1042 dump_sigspec_lhs(cell->getPort(ID::Q));
1043 f << " = ";
1044 dump_sigspec_rhs(cell->getPort(ID::D));
1045 f << ";\n";
1046 if (cell->hasPort(ID::EN) && cell->type != ID($sdffce)) {
1047 dec_indent();
1048 f << indent << "}\n";
1049 }
1050 if (cell->hasPort(ID::SRST)) {
1051 f << indent << "if (";
1052 dump_sigspec_rhs(cell->getPort(ID::SRST));
1053 f << " == value<1> {" << cell->getParam(ID::SRST_POLARITY).as_bool() << "u}) {\n";
1054 inc_indent();
1055 f << indent;
1056 dump_sigspec_lhs(cell->getPort(ID::Q));
1057 f << " = ";
1058 dump_const(cell->getParam(ID::SRST_VALUE));
1059 f << ";\n";
1060 dec_indent();
1061 f << indent << "}\n";
1062 }
1063 if (cell->hasPort(ID::EN) && cell->type == ID($sdffce)) {
1064 dec_indent();
1065 f << indent << "}\n";
1066 }
1067 dec_indent();
1068 f << indent << "}\n";
1069 } else if (cell->hasPort(ID::EN)) {
1070 // Level-sensitive logic
1071 f << indent << "if (";
1072 dump_sigspec_rhs(cell->getPort(ID::EN));
1073 f << " == value<1> {" << cell->getParam(ID::EN_POLARITY).as_bool() << "u}) {\n";
1074 inc_indent();
1075 f << indent;
1076 dump_sigspec_lhs(cell->getPort(ID::Q));
1077 f << " = ";
1078 dump_sigspec_rhs(cell->getPort(ID::D));
1079 f << ";\n";
1080 dec_indent();
1081 f << indent << "}\n";
1082 }
1083 if (cell->hasPort(ID::ARST)) {
1084 // Asynchronous reset (entire coarse cell at once)
1085 f << indent << "if (";
1086 dump_sigspec_rhs(cell->getPort(ID::ARST));
1087 f << " == value<1> {" << cell->getParam(ID::ARST_POLARITY).as_bool() << "u}) {\n";
1088 inc_indent();
1089 f << indent;
1090 dump_sigspec_lhs(cell->getPort(ID::Q));
1091 f << " = ";
1092 dump_const(cell->getParam(ID::ARST_VALUE));
1093 f << ";\n";
1094 dec_indent();
1095 f << indent << "}\n";
1096 }
1097 if (cell->hasPort(ID::SET)) {
1098 // Asynchronous set (for individual bits)
1099 f << indent;
1100 dump_sigspec_lhs(cell->getPort(ID::Q));
1101 f << " = ";
1102 dump_sigspec_lhs(cell->getPort(ID::Q));
1103 f << ".update(";
1104 dump_const(RTLIL::Const(RTLIL::S1, cell->getParam(ID::WIDTH).as_int()));
1105 f << ", ";
1106 dump_sigspec_rhs(cell->getPort(ID::SET));
1107 f << (cell->getParam(ID::SET_POLARITY).as_bool() ? "" : ".bit_not()") << ");\n";
1108 }
1109 if (cell->hasPort(ID::CLR)) {
1110 // Asynchronous clear (for individual bits; priority over set)
1111 f << indent;
1112 dump_sigspec_lhs(cell->getPort(ID::Q));
1113 f << " = ";
1114 dump_sigspec_lhs(cell->getPort(ID::Q));
1115 f << ".update(";
1116 dump_const(RTLIL::Const(RTLIL::S0, cell->getParam(ID::WIDTH).as_int()));
1117 f << ", ";
1118 dump_sigspec_rhs(cell->getPort(ID::CLR));
1119 f << (cell->getParam(ID::CLR_POLARITY).as_bool() ? "" : ".bit_not()") << ");\n";
1120 }
1121 // Memory ports
1122 } else if (cell->type.in(ID($memrd), ID($memwr))) {
1123 if (cell->getParam(ID::CLK_ENABLE).as_bool()) {
1124 RTLIL::SigBit clk_bit = cell->getPort(ID::CLK)[0];
1125 clk_bit = sigmaps[clk_bit.wire->module](clk_bit);
1126 f << indent << "if (" << (cell->getParam(ID::CLK_POLARITY).as_bool() ? "posedge_" : "negedge_")
1127 << mangle(clk_bit) << ") {\n";
1128 inc_indent();
1129 }
1130 RTLIL::Memory *memory = cell->module->memories[cell->getParam(ID::MEMID).decode_string()];
1131 std::string valid_index_temp = fresh_temporary();
1132 f << indent << "auto " << valid_index_temp << " = memory_index(";
1133 dump_sigspec_rhs(cell->getPort(ID::ADDR));
1134 f << ", " << memory->start_offset << ", " << memory->size << ");\n";
1135 if (cell->type == ID($memrd)) {
1136 bool has_enable = cell->getParam(ID::CLK_ENABLE).as_bool() && !cell->getPort(ID::EN).is_fully_ones();
1137 if (has_enable) {
1138 f << indent << "if (";
1139 dump_sigspec_rhs(cell->getPort(ID::EN));
1140 f << ") {\n";
1141 inc_indent();
1142 }
1143 // The generated code has two bounds checks; one in an assertion, and another that guards the read.
1144 // This is done so that the code does not invoke undefined behavior under any conditions, but nevertheless
1145 // loudly crashes if an illegal condition is encountered. The assert may be turned off with -NDEBUG not
1146 // just for release builds, but also to make sure the simulator (which is presumably embedded in some
1147 // larger program) will never crash the code that calls into it.
1148 //
1149 // If assertions are disabled, out of bounds reads are defined to return zero.
1150 f << indent << "assert(" << valid_index_temp << ".valid && \"out of bounds read\");\n";
1151 f << indent << "if(" << valid_index_temp << ".valid) {\n";
1152 inc_indent();
1153 if (writable_memories[memory]) {
1154 std::string lhs_temp = fresh_temporary();
1155 f << indent << "value<" << memory->width << "> " << lhs_temp << " = "
1156 << mangle(memory) << "[" << valid_index_temp << ".index];\n";
1157 std::vector<const RTLIL::Cell*> memwr_cells(transparent_for[cell].begin(), transparent_for[cell].end());
1158 if (!memwr_cells.empty()) {
1159 std::string addr_temp = fresh_temporary();
1160 f << indent << "const value<" << cell->getPort(ID::ADDR).size() << "> &" << addr_temp << " = ";
1161 dump_sigspec_rhs(cell->getPort(ID::ADDR));
1162 f << ";\n";
1163 std::sort(memwr_cells.begin(), memwr_cells.end(),
1164 [](const RTLIL::Cell *a, const RTLIL::Cell *b) {
1165 return a->getParam(ID::PRIORITY).as_int() < b->getParam(ID::PRIORITY).as_int();
1166 });
1167 for (auto memwr_cell : memwr_cells) {
1168 f << indent << "if (" << addr_temp << " == ";
1169 dump_sigspec_rhs(memwr_cell->getPort(ID::ADDR));
1170 f << ") {\n";
1171 inc_indent();
1172 f << indent << lhs_temp << " = " << lhs_temp;
1173 f << ".update(";
1174 dump_sigspec_rhs(memwr_cell->getPort(ID::DATA));
1175 f << ", ";
1176 dump_sigspec_rhs(memwr_cell->getPort(ID::EN));
1177 f << ");\n";
1178 dec_indent();
1179 f << indent << "}\n";
1180 }
1181 }
1182 f << indent;
1183 dump_sigspec_lhs(cell->getPort(ID::DATA));
1184 f << " = " << lhs_temp << ";\n";
1185 } else {
1186 f << indent;
1187 dump_sigspec_lhs(cell->getPort(ID::DATA));
1188 f << " = " << mangle(memory) << "[" << valid_index_temp << ".index];\n";
1189 }
1190 dec_indent();
1191 f << indent << "} else {\n";
1192 inc_indent();
1193 f << indent;
1194 dump_sigspec_lhs(cell->getPort(ID::DATA));
1195 f << " = value<" << memory->width << "> {};\n";
1196 dec_indent();
1197 f << indent << "}\n";
1198 if (has_enable) {
1199 dec_indent();
1200 f << indent << "}\n";
1201 }
1202 } else /*if (cell->type == ID($memwr))*/ {
1203 log_assert(writable_memories[memory]);
1204 // See above for rationale of having both the assert and the condition.
1205 //
1206 // If assertions are disabled, out of bounds writes are defined to do nothing.
1207 f << indent << "assert(" << valid_index_temp << ".valid && \"out of bounds write\");\n";
1208 f << indent << "if (" << valid_index_temp << ".valid) {\n";
1209 inc_indent();
1210 f << indent << mangle(memory) << ".update(" << valid_index_temp << ".index, ";
1211 dump_sigspec_rhs(cell->getPort(ID::DATA));
1212 f << ", ";
1213 dump_sigspec_rhs(cell->getPort(ID::EN));
1214 f << ", " << cell->getParam(ID::PRIORITY).as_int() << ");\n";
1215 dec_indent();
1216 f << indent << "}\n";
1217 }
1218 if (cell->getParam(ID::CLK_ENABLE).as_bool()) {
1219 dec_indent();
1220 f << indent << "}\n";
1221 }
1222 // Internal cells
1223 } else if (is_internal_cell(cell->type)) {
1224 log_cmd_error("Unsupported internal cell `%s'.\n", cell->type.c_str());
1225 // User cells
1226 } else {
1227 log_assert(cell->known());
1228 const char *access = is_cxxrtl_blackbox_cell(cell) ? "->" : ".";
1229 for (auto conn : cell->connections())
1230 if (cell->input(conn.first) && !cell->output(conn.first)) {
1231 f << indent << mangle(cell) << access << mangle_wire_name(conn.first) << " = ";
1232 dump_sigspec_rhs(conn.second);
1233 f << ";\n";
1234 if (getenv("CXXRTL_VOID_MY_WARRANTY")) {
1235 // Until we have proper clock tree detection, this really awful hack that opportunistically
1236 // propagates prev_* values for clocks can be used to estimate how much faster a design could
1237 // be if only one clock edge was simulated by replacing:
1238 // top.p_clk = value<1>{0u}; top.step();
1239 // top.p_clk = value<1>{1u}; top.step();
1240 // with:
1241 // top.prev_p_clk = value<1>{0u}; top.p_clk = value<1>{1u}; top.step();
1242 // Don't rely on this; it will be removed without warning.
1243 RTLIL::Module *cell_module = cell->module->design->module(cell->type);
1244 if (cell_module != nullptr && cell_module->wire(conn.first) && conn.second.is_wire()) {
1245 RTLIL::Wire *cell_module_wire = cell_module->wire(conn.first);
1246 if (edge_wires[conn.second.as_wire()] && edge_wires[cell_module_wire]) {
1247 f << indent << mangle(cell) << access << "prev_" << mangle(cell_module_wire) << " = ";
1248 f << "prev_" << mangle(conn.second.as_wire()) << ";\n";
1249 }
1250 }
1251 }
1252 } else if (cell->input(conn.first)) {
1253 f << indent << mangle(cell) << access << mangle_wire_name(conn.first) << ".next = ";
1254 dump_sigspec_rhs(conn.second);
1255 f << ";\n";
1256 }
1257 auto assign_from_outputs = [&](bool cell_converged) {
1258 for (auto conn : cell->connections()) {
1259 if (cell->output(conn.first)) {
1260 if (conn.second.empty())
1261 continue; // ignore disconnected ports
1262 if (is_cxxrtl_sync_port(cell, conn.first))
1263 continue; // fully sync ports are handled in CELL_SYNC nodes
1264 f << indent;
1265 dump_sigspec_lhs(conn.second);
1266 f << " = " << mangle(cell) << access << mangle_wire_name(conn.first);
1267 // Similarly to how there is no purpose to buffering cell inputs, there is also no purpose to buffering
1268 // combinatorial cell outputs in case the cell converges within one cycle. (To convince yourself that
1269 // this optimization is valid, consider that, since the cell converged within one cycle, it would not
1270 // have any buffered wires if they were not output ports. Imagine inlining the cell's eval() function,
1271 // and consider the fate of the localized wires that used to be output ports.)
1272 //
1273 // Unlike cell inputs (which are never buffered), it is not possible to know apriori whether the cell
1274 // (which may be late bound) will converge immediately. Because of this, the choice between using .curr
1275 // (appropriate for buffered outputs) and .next (appropriate for unbuffered outputs) is made at runtime.
1276 if (cell_converged && is_cxxrtl_comb_port(cell, conn.first))
1277 f << ".next;\n";
1278 else
1279 f << ".curr;\n";
1280 }
1281 }
1282 };
1283 f << indent << "if (" << mangle(cell) << access << "eval()) {\n";
1284 inc_indent();
1285 assign_from_outputs(/*cell_converged=*/true);
1286 dec_indent();
1287 f << indent << "} else {\n";
1288 inc_indent();
1289 f << indent << "converged = false;\n";
1290 assign_from_outputs(/*cell_converged=*/false);
1291 dec_indent();
1292 f << indent << "}\n";
1293 }
1294 }
1295
1296 void dump_assign(const RTLIL::SigSig &sigsig)
1297 {
1298 f << indent;
1299 dump_sigspec_lhs(sigsig.first);
1300 f << " = ";
1301 dump_sigspec_rhs(sigsig.second);
1302 f << ";\n";
1303 }
1304
1305 void dump_case_rule(const RTLIL::CaseRule *rule)
1306 {
1307 for (auto action : rule->actions)
1308 dump_assign(action);
1309 for (auto switch_ : rule->switches)
1310 dump_switch_rule(switch_);
1311 }
1312
1313 void dump_switch_rule(const RTLIL::SwitchRule *rule)
1314 {
1315 // The switch attributes are printed before the switch condition is captured.
1316 dump_attrs(rule);
1317 std::string signal_temp = fresh_temporary();
1318 f << indent << "const value<" << rule->signal.size() << "> &" << signal_temp << " = ";
1319 dump_sigspec(rule->signal, /*is_lhs=*/false);
1320 f << ";\n";
1321
1322 bool first = true;
1323 for (auto case_ : rule->cases) {
1324 // The case attributes (for nested cases) are printed before the if/else if/else statement.
1325 dump_attrs(rule);
1326 f << indent;
1327 if (!first)
1328 f << "} else ";
1329 first = false;
1330 if (!case_->compare.empty()) {
1331 f << "if (";
1332 bool first = true;
1333 for (auto &compare : case_->compare) {
1334 if (!first)
1335 f << " || ";
1336 first = false;
1337 if (compare.is_fully_def()) {
1338 f << signal_temp << " == ";
1339 dump_sigspec(compare, /*is_lhs=*/false);
1340 } else if (compare.is_fully_const()) {
1341 RTLIL::Const compare_mask, compare_value;
1342 for (auto bit : compare.as_const()) {
1343 switch (bit) {
1344 case RTLIL::S0:
1345 case RTLIL::S1:
1346 compare_mask.bits.push_back(RTLIL::S1);
1347 compare_value.bits.push_back(bit);
1348 break;
1349
1350 case RTLIL::Sx:
1351 case RTLIL::Sz:
1352 case RTLIL::Sa:
1353 compare_mask.bits.push_back(RTLIL::S0);
1354 compare_value.bits.push_back(RTLIL::S0);
1355 break;
1356
1357 default:
1358 log_assert(false);
1359 }
1360 }
1361 f << "and_uu<" << compare.size() << ">(" << signal_temp << ", ";
1362 dump_const(compare_mask);
1363 f << ") == ";
1364 dump_const(compare_value);
1365 } else {
1366 log_assert(false);
1367 }
1368 }
1369 f << ") ";
1370 }
1371 f << "{\n";
1372 inc_indent();
1373 dump_case_rule(case_);
1374 dec_indent();
1375 }
1376 f << indent << "}\n";
1377 }
1378
1379 void dump_process(const RTLIL::Process *proc)
1380 {
1381 dump_attrs(proc);
1382 f << indent << "// process " << proc->name.str() << "\n";
1383 // The case attributes (for root case) are always empty.
1384 log_assert(proc->root_case.attributes.empty());
1385 dump_case_rule(&proc->root_case);
1386 for (auto sync : proc->syncs) {
1387 RTLIL::SigBit sync_bit;
1388 if (!sync->signal.empty()) {
1389 sync_bit = sync->signal[0];
1390 sync_bit = sigmaps[sync_bit.wire->module](sync_bit);
1391 }
1392
1393 pool<std::string> events;
1394 switch (sync->type) {
1395 case RTLIL::STp:
1396 log_assert(sync_bit.wire != nullptr);
1397 events.insert("posedge_" + mangle(sync_bit));
1398 break;
1399 case RTLIL::STn:
1400 log_assert(sync_bit.wire != nullptr);
1401 events.insert("negedge_" + mangle(sync_bit));
1402 break;
1403 case RTLIL::STe:
1404 log_assert(sync_bit.wire != nullptr);
1405 events.insert("posedge_" + mangle(sync_bit));
1406 events.insert("negedge_" + mangle(sync_bit));
1407 break;
1408
1409 case RTLIL::STa:
1410 events.insert("true");
1411 break;
1412
1413 case RTLIL::ST0:
1414 case RTLIL::ST1:
1415 case RTLIL::STg:
1416 case RTLIL::STi:
1417 log_assert(false);
1418 }
1419 if (!events.empty()) {
1420 f << indent << "if (";
1421 bool first = true;
1422 for (auto &event : events) {
1423 if (!first)
1424 f << " || ";
1425 first = false;
1426 f << event;
1427 }
1428 f << ") {\n";
1429 inc_indent();
1430 for (auto action : sync->actions)
1431 dump_assign(action);
1432 dec_indent();
1433 f << indent << "}\n";
1434 }
1435 }
1436 }
1437
1438 void dump_wire(const RTLIL::Wire *wire, bool is_local_context)
1439 {
1440 if (elided_wires.count(wire))
1441 return;
1442
1443 if (localized_wires[wire] && is_local_context) {
1444 dump_attrs(wire);
1445 f << indent << "value<" << wire->width << "> " << mangle(wire) << ";\n";
1446 }
1447 if (!localized_wires[wire] && !is_local_context) {
1448 std::string width;
1449 if (wire->module->has_attribute(ID(cxxrtl_blackbox)) && wire->has_attribute(ID(cxxrtl_width))) {
1450 width = wire->get_string_attribute(ID(cxxrtl_width));
1451 } else {
1452 width = std::to_string(wire->width);
1453 }
1454
1455 dump_attrs(wire);
1456 f << indent;
1457 if (wire->port_input && wire->port_output)
1458 f << "/*inout*/ ";
1459 else if (wire->port_input)
1460 f << "/*input*/ ";
1461 else if (wire->port_output)
1462 f << "/*output*/ ";
1463 f << (unbuffered_wires[wire] ? "value" : "wire") << "<" << width << "> " << mangle(wire);
1464 if (wire->has_attribute(ID::init)) {
1465 f << " ";
1466 dump_const_init(wire->attributes.at(ID::init));
1467 }
1468 f << ";\n";
1469 if (edge_wires[wire]) {
1470 if (unbuffered_wires[wire]) {
1471 f << indent << "value<" << width << "> prev_" << mangle(wire);
1472 if (wire->has_attribute(ID::init)) {
1473 f << " ";
1474 dump_const_init(wire->attributes.at(ID::init));
1475 }
1476 f << ";\n";
1477 }
1478 for (auto edge_type : edge_types) {
1479 if (edge_type.first.wire == wire) {
1480 std::string prev, next;
1481 if (unbuffered_wires[wire]) {
1482 prev = "prev_" + mangle(edge_type.first.wire);
1483 next = mangle(edge_type.first.wire);
1484 } else {
1485 prev = mangle(edge_type.first.wire) + ".curr";
1486 next = mangle(edge_type.first.wire) + ".next";
1487 }
1488 prev += ".slice<" + std::to_string(edge_type.first.offset) + ">().val()";
1489 next += ".slice<" + std::to_string(edge_type.first.offset) + ">().val()";
1490 if (edge_type.second != RTLIL::STn) {
1491 f << indent << "bool posedge_" << mangle(edge_type.first) << "() const {\n";
1492 inc_indent();
1493 f << indent << "return !" << prev << " && " << next << ";\n";
1494 dec_indent();
1495 f << indent << "}\n";
1496 }
1497 if (edge_type.second != RTLIL::STp) {
1498 f << indent << "bool negedge_" << mangle(edge_type.first) << "() const {\n";
1499 inc_indent();
1500 f << indent << "return " << prev << " && !" << next << ";\n";
1501 dec_indent();
1502 f << indent << "}\n";
1503 }
1504 }
1505 }
1506 }
1507 }
1508 }
1509
1510 void dump_memory(RTLIL::Module *module, const RTLIL::Memory *memory)
1511 {
1512 vector<const RTLIL::Cell*> init_cells;
1513 for (auto cell : module->cells())
1514 if (cell->type == ID($meminit) && cell->getParam(ID::MEMID).decode_string() == memory->name.str())
1515 init_cells.push_back(cell);
1516
1517 std::sort(init_cells.begin(), init_cells.end(), [](const RTLIL::Cell *a, const RTLIL::Cell *b) {
1518 int a_addr = a->getPort(ID::ADDR).as_int(), b_addr = b->getPort(ID::ADDR).as_int();
1519 int a_prio = a->getParam(ID::PRIORITY).as_int(), b_prio = b->getParam(ID::PRIORITY).as_int();
1520 return a_prio > b_prio || (a_prio == b_prio && a_addr < b_addr);
1521 });
1522
1523 dump_attrs(memory);
1524 f << indent << "memory<" << memory->width << "> " << mangle(memory)
1525 << " { " << memory->size << "u";
1526 if (init_cells.empty()) {
1527 f << " };\n";
1528 } else {
1529 f << ",\n";
1530 inc_indent();
1531 for (auto cell : init_cells) {
1532 dump_attrs(cell);
1533 RTLIL::Const data = cell->getPort(ID::DATA).as_const();
1534 size_t width = cell->getParam(ID::WIDTH).as_int();
1535 size_t words = cell->getParam(ID::WORDS).as_int();
1536 f << indent << "memory<" << memory->width << ">::init<" << words << "> { "
1537 << stringf("%#x", cell->getPort(ID::ADDR).as_int()) << ", {";
1538 inc_indent();
1539 for (size_t n = 0; n < words; n++) {
1540 if (n % 4 == 0)
1541 f << "\n" << indent;
1542 else
1543 f << " ";
1544 dump_const(data, width, n * width, /*fixed_width=*/true);
1545 f << ",";
1546 }
1547 dec_indent();
1548 f << "\n" << indent << "}},\n";
1549 }
1550 dec_indent();
1551 f << indent << "};\n";
1552 }
1553 }
1554
1555 void dump_eval_method(RTLIL::Module *module)
1556 {
1557 inc_indent();
1558 f << indent << "bool converged = " << (eval_converges.at(module) ? "true" : "false") << ";\n";
1559 if (!module->get_bool_attribute(ID(cxxrtl_blackbox))) {
1560 for (auto wire : module->wires()) {
1561 if (edge_wires[wire]) {
1562 for (auto edge_type : edge_types) {
1563 if (edge_type.first.wire == wire) {
1564 if (edge_type.second != RTLIL::STn) {
1565 f << indent << "bool posedge_" << mangle(edge_type.first) << " = ";
1566 f << "this->posedge_" << mangle(edge_type.first) << "();\n";
1567 }
1568 if (edge_type.second != RTLIL::STp) {
1569 f << indent << "bool negedge_" << mangle(edge_type.first) << " = ";
1570 f << "this->negedge_" << mangle(edge_type.first) << "();\n";
1571 }
1572 }
1573 }
1574 }
1575 }
1576 for (auto wire : module->wires())
1577 dump_wire(wire, /*is_local_context=*/true);
1578 for (auto node : schedule[module]) {
1579 switch (node.type) {
1580 case FlowGraph::Node::Type::CONNECT:
1581 dump_connect(node.connect);
1582 break;
1583 case FlowGraph::Node::Type::CELL_SYNC:
1584 dump_cell_sync(node.cell);
1585 break;
1586 case FlowGraph::Node::Type::CELL_EVAL:
1587 dump_cell_eval(node.cell);
1588 break;
1589 case FlowGraph::Node::Type::PROCESS:
1590 dump_process(node.process);
1591 break;
1592 }
1593 }
1594 }
1595 f << indent << "return converged;\n";
1596 dec_indent();
1597 }
1598
1599 void dump_commit_method(RTLIL::Module *module)
1600 {
1601 inc_indent();
1602 f << indent << "bool changed = false;\n";
1603 for (auto wire : module->wires()) {
1604 if (elided_wires.count(wire))
1605 continue;
1606 if (unbuffered_wires[wire]) {
1607 if (edge_wires[wire])
1608 f << indent << "prev_" << mangle(wire) << " = " << mangle(wire) << ";\n";
1609 continue;
1610 }
1611 if (!module->get_bool_attribute(ID(cxxrtl_blackbox)) || wire->port_id != 0)
1612 f << indent << "changed |= " << mangle(wire) << ".commit();\n";
1613 }
1614 if (!module->get_bool_attribute(ID(cxxrtl_blackbox))) {
1615 for (auto memory : module->memories) {
1616 if (!writable_memories[memory.second])
1617 continue;
1618 f << indent << "changed |= " << mangle(memory.second) << ".commit();\n";
1619 }
1620 for (auto cell : module->cells()) {
1621 if (is_internal_cell(cell->type))
1622 continue;
1623 const char *access = is_cxxrtl_blackbox_cell(cell) ? "->" : ".";
1624 f << indent << "changed |= " << mangle(cell) << access << "commit();\n";
1625 }
1626 }
1627 f << indent << "return changed;\n";
1628 dec_indent();
1629 }
1630
1631 void dump_debug_info_method(RTLIL::Module *module)
1632 {
1633 size_t count_public_wires = 0;
1634 size_t count_const_wires = 0;
1635 size_t count_alias_wires = 0;
1636 size_t count_member_wires = 0;
1637 size_t count_skipped_wires = 0;
1638 inc_indent();
1639 f << indent << "assert(path.empty() || path[path.size() - 1] == ' ');\n";
1640 for (auto wire : module->wires()) {
1641 if (wire->name[0] != '\\')
1642 continue;
1643 if (module->get_bool_attribute(ID(cxxrtl_blackbox)) && (wire->port_id == 0))
1644 continue;
1645 count_public_wires++;
1646 if (debug_const_wires.count(wire)) {
1647 // Wire tied to a constant
1648 f << indent << "static const value<" << wire->width << "> const_" << mangle(wire) << " = ";
1649 dump_const(debug_const_wires[wire]);
1650 f << ";\n";
1651 f << indent << "items.add(path + " << escape_cxx_string(get_hdl_name(wire));
1652 f << ", debug_item(const_" << mangle(wire) << ", ";
1653 f << wire->start_offset << "));\n";
1654 count_const_wires++;
1655 } else if (debug_alias_wires.count(wire)) {
1656 // Alias of a member wire
1657 f << indent << "items.add(path + " << escape_cxx_string(get_hdl_name(wire));
1658 f << ", debug_item(debug_alias(), " << mangle(debug_alias_wires[wire]) << ", ";
1659 f << wire->start_offset << "));\n";
1660 count_alias_wires++;
1661 } else if (!localized_wires.count(wire)) {
1662 // Member wire
1663 f << indent << "items.add(path + " << escape_cxx_string(get_hdl_name(wire));
1664 f << ", debug_item(" << mangle(wire) << ", ";
1665 f << wire->start_offset << "));\n";
1666 count_member_wires++;
1667 } else {
1668 count_skipped_wires++;
1669 }
1670 }
1671 if (!module->get_bool_attribute(ID(cxxrtl_blackbox))) {
1672 for (auto &memory_it : module->memories) {
1673 if (memory_it.first[0] != '\\')
1674 continue;
1675 f << indent << "items.add(path + " << escape_cxx_string(get_hdl_name(memory_it.second));
1676 f << ", debug_item(" << mangle(memory_it.second) << ", ";
1677 f << memory_it.second->start_offset << "));\n";
1678 }
1679 for (auto cell : module->cells()) {
1680 if (is_internal_cell(cell->type))
1681 continue;
1682 const char *access = is_cxxrtl_blackbox_cell(cell) ? "->" : ".";
1683 f << indent << mangle(cell) << access << "debug_info(items, ";
1684 f << "path + " << escape_cxx_string(get_hdl_name(cell) + ' ') << ");\n";
1685 }
1686 }
1687 dec_indent();
1688
1689 log_debug("Debug information statistics for module `%s':\n", log_id(module));
1690 log_debug(" Public wires: %zu, of which:\n", count_public_wires);
1691 log_debug(" Const wires: %zu\n", count_const_wires);
1692 log_debug(" Alias wires: %zu\n", count_alias_wires);
1693 log_debug(" Member wires: %zu\n", count_member_wires);
1694 log_debug(" Other wires: %zu (no debug information)\n", count_skipped_wires);
1695 }
1696
1697 void dump_metadata_map(const dict<RTLIL::IdString, RTLIL::Const> &metadata_map)
1698 {
1699 if (metadata_map.empty()) {
1700 f << "metadata_map()";
1701 return;
1702 }
1703 f << "metadata_map({\n";
1704 inc_indent();
1705 for (auto metadata_item : metadata_map) {
1706 if (!metadata_item.first.begins_with("\\"))
1707 continue;
1708 f << indent << "{ " << escape_cxx_string(metadata_item.first.str().substr(1)) << ", ";
1709 if (metadata_item.second.flags & RTLIL::CONST_FLAG_REAL) {
1710 f << std::showpoint << std::stod(metadata_item.second.decode_string()) << std::noshowpoint;
1711 } else if (metadata_item.second.flags & RTLIL::CONST_FLAG_STRING) {
1712 f << escape_cxx_string(metadata_item.second.decode_string());
1713 } else {
1714 f << metadata_item.second.as_int(/*is_signed=*/metadata_item.second.flags & RTLIL::CONST_FLAG_SIGNED);
1715 if (!(metadata_item.second.flags & RTLIL::CONST_FLAG_SIGNED))
1716 f << "u";
1717 }
1718 f << " },\n";
1719 }
1720 dec_indent();
1721 f << indent << "})";
1722 }
1723
1724 void dump_module_intf(RTLIL::Module *module)
1725 {
1726 dump_attrs(module);
1727 if (module->get_bool_attribute(ID(cxxrtl_blackbox))) {
1728 if (module->has_attribute(ID(cxxrtl_template)))
1729 f << indent << "template" << template_params(module, /*is_decl=*/true) << "\n";
1730 f << indent << "struct " << mangle(module) << " : public module {\n";
1731 inc_indent();
1732 for (auto wire : module->wires()) {
1733 if (wire->port_id != 0)
1734 dump_wire(wire, /*is_local_context=*/false);
1735 }
1736 f << "\n";
1737 f << indent << "bool eval() override {\n";
1738 dump_eval_method(module);
1739 f << indent << "}\n";
1740 f << "\n";
1741 f << indent << "bool commit() override {\n";
1742 dump_commit_method(module);
1743 f << indent << "}\n";
1744 f << "\n";
1745 if (debug_info) {
1746 f << indent << "void debug_info(debug_items &items, std::string path = \"\") override {\n";
1747 dump_debug_info_method(module);
1748 f << indent << "}\n";
1749 f << "\n";
1750 }
1751 f << indent << "static std::unique_ptr<" << mangle(module);
1752 f << template_params(module, /*is_decl=*/false) << "> ";
1753 f << "create(std::string name, metadata_map parameters, metadata_map attributes);\n";
1754 dec_indent();
1755 f << indent << "}; // struct " << mangle(module) << "\n";
1756 f << "\n";
1757 if (blackbox_specializations.count(module)) {
1758 // If templated black boxes are used, the constructor of any module which includes the black box cell
1759 // (which calls the declared but not defined in the generated code `create` function) may only be used
1760 // if (a) the create function is defined in the same translation unit, or (b) the create function has
1761 // a forward-declared explicit specialization.
1762 //
1763 // Option (b) makes it possible to have the generated code and the black box implementation in different
1764 // translation units, which is convenient. Of course, its downside is that black boxes must predefine
1765 // a specialization for every combination of parameters the generated code may use; but since the main
1766 // purpose of templated black boxes is abstracting over datapath width, it is expected that there would
1767 // be very few such combinations anyway.
1768 for (auto specialization : blackbox_specializations[module]) {
1769 f << indent << "template<>\n";
1770 f << indent << "std::unique_ptr<" << mangle(module) << specialization << "> ";
1771 f << mangle(module) << specialization << "::";
1772 f << "create(std::string name, metadata_map parameters, metadata_map attributes);\n";
1773 f << "\n";
1774 }
1775 }
1776 } else {
1777 f << indent << "struct " << mangle(module) << " : public module {\n";
1778 inc_indent();
1779 for (auto wire : module->wires())
1780 dump_wire(wire, /*is_local_context=*/false);
1781 f << "\n";
1782 bool has_memories = false;
1783 for (auto memory : module->memories) {
1784 dump_memory(module, memory.second);
1785 has_memories = true;
1786 }
1787 if (has_memories)
1788 f << "\n";
1789 bool has_cells = false;
1790 for (auto cell : module->cells()) {
1791 if (is_internal_cell(cell->type))
1792 continue;
1793 dump_attrs(cell);
1794 RTLIL::Module *cell_module = module->design->module(cell->type);
1795 log_assert(cell_module != nullptr);
1796 if (cell_module->get_bool_attribute(ID(cxxrtl_blackbox))) {
1797 f << indent << "std::unique_ptr<" << mangle(cell_module) << template_args(cell) << "> ";
1798 f << mangle(cell) << " = " << mangle(cell_module) << template_args(cell);
1799 f << "::create(" << escape_cxx_string(get_hdl_name(cell)) << ", ";
1800 dump_metadata_map(cell->parameters);
1801 f << ", ";
1802 dump_metadata_map(cell->attributes);
1803 f << ");\n";
1804 } else {
1805 f << indent << mangle(cell_module) << " " << mangle(cell) << ";\n";
1806 }
1807 has_cells = true;
1808 }
1809 if (has_cells)
1810 f << "\n";
1811 f << indent << "bool eval() override;\n";
1812 f << indent << "bool commit() override;\n";
1813 if (debug_info)
1814 f << indent << "void debug_info(debug_items &items, std::string path = \"\") override;\n";
1815 dec_indent();
1816 f << indent << "}; // struct " << mangle(module) << "\n";
1817 f << "\n";
1818 }
1819 }
1820
1821 void dump_module_impl(RTLIL::Module *module)
1822 {
1823 if (module->get_bool_attribute(ID(cxxrtl_blackbox)))
1824 return;
1825 f << indent << "bool " << mangle(module) << "::eval() {\n";
1826 dump_eval_method(module);
1827 f << indent << "}\n";
1828 f << "\n";
1829 f << indent << "bool " << mangle(module) << "::commit() {\n";
1830 dump_commit_method(module);
1831 f << indent << "}\n";
1832 f << "\n";
1833 if (debug_info) {
1834 f << indent << "void " << mangle(module) << "::debug_info(debug_items &items, std::string path) {\n";
1835 dump_debug_info_method(module);
1836 f << indent << "}\n";
1837 f << "\n";
1838 }
1839 }
1840
1841 void dump_design(RTLIL::Design *design)
1842 {
1843 RTLIL::Module *top_module = nullptr;
1844 std::vector<RTLIL::Module*> modules;
1845 TopoSort<RTLIL::Module*> topo_design;
1846 for (auto module : design->modules()) {
1847 if (!design->selected_module(module))
1848 continue;
1849 if (module->get_bool_attribute(ID(cxxrtl_blackbox)))
1850 modules.push_back(module); // cxxrtl blackboxes first
1851 if (module->get_blackbox_attribute() || module->get_bool_attribute(ID(cxxrtl_blackbox)))
1852 continue;
1853 if (module->get_bool_attribute(ID::top))
1854 top_module = module;
1855
1856 topo_design.node(module);
1857 for (auto cell : module->cells()) {
1858 if (is_internal_cell(cell->type) || is_cxxrtl_blackbox_cell(cell))
1859 continue;
1860 RTLIL::Module *cell_module = design->module(cell->type);
1861 log_assert(cell_module != nullptr);
1862 topo_design.edge(cell_module, module);
1863 }
1864 }
1865 bool no_loops = topo_design.sort();
1866 log_assert(no_loops);
1867 modules.insert(modules.end(), topo_design.sorted.begin(), topo_design.sorted.end());
1868
1869 if (split_intf) {
1870 // The only thing more depraved than include guards, is mangling filenames to turn them into include guards.
1871 std::string include_guard = design_ns + "_header";
1872 std::transform(include_guard.begin(), include_guard.end(), include_guard.begin(), ::toupper);
1873
1874 f << "#ifndef " << include_guard << "\n";
1875 f << "#define " << include_guard << "\n";
1876 f << "\n";
1877 if (top_module != nullptr && debug_info) {
1878 f << "#include <backends/cxxrtl/cxxrtl_capi.h>\n";
1879 f << "\n";
1880 f << "#ifdef __cplusplus\n";
1881 f << "extern \"C\" {\n";
1882 f << "#endif\n";
1883 f << "\n";
1884 f << "cxxrtl_toplevel " << design_ns << "_create();\n";
1885 f << "\n";
1886 f << "#ifdef __cplusplus\n";
1887 f << "}\n";
1888 f << "#endif\n";
1889 f << "\n";
1890 } else {
1891 f << "// The CXXRTL C API is not available because the design is built without debug information.\n";
1892 f << "\n";
1893 }
1894 f << "#ifdef __cplusplus\n";
1895 f << "\n";
1896 f << "#include <backends/cxxrtl/cxxrtl.h>\n";
1897 f << "\n";
1898 f << "using namespace cxxrtl;\n";
1899 f << "\n";
1900 f << "namespace " << design_ns << " {\n";
1901 f << "\n";
1902 for (auto module : modules)
1903 dump_module_intf(module);
1904 f << "} // namespace " << design_ns << "\n";
1905 f << "\n";
1906 f << "#endif // __cplusplus\n";
1907 f << "\n";
1908 f << "#endif\n";
1909 *intf_f << f.str(); f.str("");
1910 }
1911
1912 if (split_intf)
1913 f << "#include \"" << intf_filename << "\"\n";
1914 else
1915 f << "#include <backends/cxxrtl/cxxrtl.h>\n";
1916 f << "\n";
1917 f << "#if defined(CXXRTL_INCLUDE_CAPI_IMPL) || \\\n";
1918 f << " defined(CXXRTL_INCLUDE_VCD_CAPI_IMPL)\n";
1919 f << "#include <backends/cxxrtl/cxxrtl_capi.cc>\n";
1920 f << "#endif\n";
1921 f << "\n";
1922 f << "#if defined(CXXRTL_INCLUDE_VCD_CAPI_IMPL)\n";
1923 f << "#include <backends/cxxrtl/cxxrtl_vcd_capi.cc>\n";
1924 f << "#endif\n";
1925 f << "\n";
1926 f << "using namespace cxxrtl_yosys;\n";
1927 f << "\n";
1928 f << "namespace " << design_ns << " {\n";
1929 f << "\n";
1930 for (auto module : modules) {
1931 if (!split_intf)
1932 dump_module_intf(module);
1933 dump_module_impl(module);
1934 }
1935 f << "} // namespace " << design_ns << "\n";
1936 f << "\n";
1937 if (top_module != nullptr && debug_info) {
1938 f << "cxxrtl_toplevel " << design_ns << "_create() {\n";
1939 inc_indent();
1940 std::string top_type = design_ns + "::" + mangle(top_module);
1941 f << indent << "return new _cxxrtl_toplevel { ";
1942 f << "std::unique_ptr<" << top_type << ">(new " + top_type + ")";
1943 f << " };\n";
1944 dec_indent();
1945 f << "}\n";
1946 }
1947
1948 *impl_f << f.str(); f.str("");
1949 }
1950
1951 // Edge-type sync rules require us to emit edge detectors, which require coordination between
1952 // eval and commit phases. To do this we need to collect them upfront.
1953 //
1954 // Note that the simulator commit phase operates at wire granularity but edge-type sync rules
1955 // operate at wire bit granularity; it is possible to have code similar to:
1956 // wire [3:0] clocks;
1957 // always @(posedge clocks[0]) ...
1958 // To handle this we track edge sensitivity both for wires and wire bits.
1959 void register_edge_signal(SigMap &sigmap, RTLIL::SigSpec signal, RTLIL::SyncType type)
1960 {
1961 signal = sigmap(signal);
1962 log_assert(signal.is_wire() && signal.is_bit());
1963 log_assert(type == RTLIL::STp || type == RTLIL::STn || type == RTLIL::STe);
1964
1965 RTLIL::SigBit sigbit = signal[0];
1966 if (!edge_types.count(sigbit))
1967 edge_types[sigbit] = type;
1968 else if (edge_types[sigbit] != type)
1969 edge_types[sigbit] = RTLIL::STe;
1970 edge_wires.insert(signal.as_wire());
1971 }
1972
1973 void analyze_design(RTLIL::Design *design)
1974 {
1975 bool has_feedback_arcs = false;
1976 bool has_buffered_comb_wires = false;
1977
1978 for (auto module : design->modules()) {
1979 if (!design->selected_module(module))
1980 continue;
1981
1982 SigMap &sigmap = sigmaps[module];
1983 sigmap.set(module);
1984
1985 if (module->get_bool_attribute(ID(cxxrtl_blackbox))) {
1986 for (auto port : module->ports) {
1987 RTLIL::Wire *wire = module->wire(port);
1988 if (wire->port_input && !wire->port_output)
1989 unbuffered_wires.insert(wire);
1990 if (wire->has_attribute(ID(cxxrtl_edge))) {
1991 RTLIL::Const edge_attr = wire->attributes[ID(cxxrtl_edge)];
1992 if (!(edge_attr.flags & RTLIL::CONST_FLAG_STRING) || (int)edge_attr.decode_string().size() != GetSize(wire))
1993 log_cmd_error("Attribute `cxxrtl_edge' of port `%s.%s' is not a string with one character per bit.\n",
1994 log_id(module), log_signal(wire));
1995
1996 std::string edges = wire->get_string_attribute(ID(cxxrtl_edge));
1997 for (int i = 0; i < GetSize(wire); i++) {
1998 RTLIL::SigSpec wire_sig = wire;
1999 switch (edges[i]) {
2000 case '-': break;
2001 case 'p': register_edge_signal(sigmap, wire_sig[i], RTLIL::STp); break;
2002 case 'n': register_edge_signal(sigmap, wire_sig[i], RTLIL::STn); break;
2003 case 'a': register_edge_signal(sigmap, wire_sig[i], RTLIL::STe); break;
2004 default:
2005 log_cmd_error("Attribute `cxxrtl_edge' of port `%s.%s' contains specifiers "
2006 "other than '-', 'p', 'n', or 'a'.\n",
2007 log_id(module), log_signal(wire));
2008 }
2009 }
2010 }
2011 }
2012
2013 // Black boxes converge by default, since their implementations are quite unlikely to require
2014 // internal propagation of comb signals.
2015 eval_converges[module] = true;
2016 continue;
2017 }
2018
2019 FlowGraph flow;
2020
2021 for (auto conn : module->connections())
2022 flow.add_node(conn);
2023
2024 dict<const RTLIL::Cell*, FlowGraph::Node*> memrw_cell_nodes;
2025 dict<std::pair<RTLIL::SigBit, const RTLIL::Memory*>,
2026 pool<const RTLIL::Cell*>> memwr_per_domain;
2027 for (auto cell : module->cells()) {
2028 if (!cell->known())
2029 log_cmd_error("Unknown cell `%s'.\n", log_id(cell->type));
2030
2031 RTLIL::Module *cell_module = design->module(cell->type);
2032 if (cell_module &&
2033 cell_module->get_blackbox_attribute() &&
2034 !cell_module->get_bool_attribute(ID(cxxrtl_blackbox)))
2035 log_cmd_error("External blackbox cell `%s' is not marked as a CXXRTL blackbox.\n", log_id(cell->type));
2036
2037 if (cell_module &&
2038 cell_module->get_bool_attribute(ID(cxxrtl_blackbox)) &&
2039 cell_module->get_bool_attribute(ID(cxxrtl_template)))
2040 blackbox_specializations[cell_module].insert(template_args(cell));
2041
2042 FlowGraph::Node *node = flow.add_node(cell);
2043
2044 // Various DFF cells are treated like posedge/negedge processes, see above for details.
2045 if (cell->type.in(ID($dff), ID($dffe), ID($adff), ID($adffe), ID($dffsr), ID($dffsre), ID($sdff), ID($sdffe), ID($sdffce))) {
2046 if (cell->getPort(ID::CLK).is_wire())
2047 register_edge_signal(sigmap, cell->getPort(ID::CLK),
2048 cell->parameters[ID::CLK_POLARITY].as_bool() ? RTLIL::STp : RTLIL::STn);
2049 }
2050 // Similar for memory port cells.
2051 if (cell->type.in(ID($memrd), ID($memwr))) {
2052 if (cell->getParam(ID::CLK_ENABLE).as_bool()) {
2053 if (cell->getPort(ID::CLK).is_wire())
2054 register_edge_signal(sigmap, cell->getPort(ID::CLK),
2055 cell->parameters[ID::CLK_POLARITY].as_bool() ? RTLIL::STp : RTLIL::STn);
2056 }
2057 memrw_cell_nodes[cell] = node;
2058 }
2059 // Optimize access to read-only memories.
2060 if (cell->type == ID($memwr))
2061 writable_memories.insert(module->memories[cell->getParam(ID::MEMID).decode_string()]);
2062 // Collect groups of memory write ports in the same domain.
2063 if (cell->type == ID($memwr) && cell->getParam(ID::CLK_ENABLE).as_bool() && cell->getPort(ID::CLK).is_wire()) {
2064 RTLIL::SigBit clk_bit = sigmap(cell->getPort(ID::CLK))[0];
2065 const RTLIL::Memory *memory = module->memories[cell->getParam(ID::MEMID).decode_string()];
2066 memwr_per_domain[{clk_bit, memory}].insert(cell);
2067 }
2068 // Handling of packed memories is delegated to the `memory_unpack` pass, so we can rely on the presence
2069 // of RTLIL memory objects and $memrd/$memwr/$meminit cells.
2070 if (cell->type.in(ID($mem)))
2071 log_assert(false);
2072 }
2073 for (auto cell : module->cells()) {
2074 // Collect groups of memory write ports read by every transparent read port.
2075 if (cell->type == ID($memrd) && cell->getParam(ID::CLK_ENABLE).as_bool() && cell->getPort(ID::CLK).is_wire() &&
2076 cell->getParam(ID::TRANSPARENT).as_bool()) {
2077 RTLIL::SigBit clk_bit = sigmap(cell->getPort(ID::CLK))[0];
2078 const RTLIL::Memory *memory = module->memories[cell->getParam(ID::MEMID).decode_string()];
2079 for (auto memwr_cell : memwr_per_domain[{clk_bit, memory}]) {
2080 transparent_for[cell].insert(memwr_cell);
2081 // Our implementation of transparent $memrd cells reads \EN, \ADDR and \DATA from every $memwr cell
2082 // in the same domain, which isn't directly visible in the netlist. Add these uses explicitly.
2083 flow.add_uses(memrw_cell_nodes[cell], memwr_cell->getPort(ID::EN));
2084 flow.add_uses(memrw_cell_nodes[cell], memwr_cell->getPort(ID::ADDR));
2085 flow.add_uses(memrw_cell_nodes[cell], memwr_cell->getPort(ID::DATA));
2086 }
2087 }
2088 }
2089
2090 for (auto proc : module->processes) {
2091 flow.add_node(proc.second);
2092
2093 for (auto sync : proc.second->syncs)
2094 switch (sync->type) {
2095 // Edge-type sync rules require pre-registration.
2096 case RTLIL::STp:
2097 case RTLIL::STn:
2098 case RTLIL::STe:
2099 register_edge_signal(sigmap, sync->signal, sync->type);
2100 break;
2101
2102 // Level-type sync rules require no special handling.
2103 case RTLIL::ST0:
2104 case RTLIL::ST1:
2105 case RTLIL::STa:
2106 break;
2107
2108 case RTLIL::STg:
2109 log_cmd_error("Global clock is not supported.\n");
2110
2111 // Handling of init-type sync rules is delegated to the `proc_init` pass, so we can use the wire
2112 // attribute regardless of input.
2113 case RTLIL::STi:
2114 log_assert(false);
2115 }
2116 }
2117
2118 for (auto wire : module->wires()) {
2119 if (!flow.is_elidable(wire)) continue;
2120 if (wire->port_id != 0) continue;
2121 if (wire->get_bool_attribute(ID::keep)) continue;
2122 if (wire->name.begins_with("$") && !elide_internal) continue;
2123 if (wire->name.begins_with("\\") && !elide_public) continue;
2124 if (edge_wires[wire]) continue;
2125 log_assert(flow.wire_comb_defs[wire].size() == 1);
2126 elided_wires[wire] = **flow.wire_comb_defs[wire].begin();
2127 }
2128
2129 dict<FlowGraph::Node*, pool<const RTLIL::Wire*>, hash_ptr_ops> node_defs;
2130 for (auto wire_comb_def : flow.wire_comb_defs)
2131 for (auto node : wire_comb_def.second)
2132 node_defs[node].insert(wire_comb_def.first);
2133
2134 Scheduler<FlowGraph::Node> scheduler;
2135 dict<FlowGraph::Node*, Scheduler<FlowGraph::Node>::Vertex*, hash_ptr_ops> node_map;
2136 for (auto node : flow.nodes)
2137 node_map[node] = scheduler.add(node);
2138 for (auto node_def : node_defs) {
2139 auto vertex = node_map[node_def.first];
2140 for (auto wire : node_def.second)
2141 for (auto succ_node : flow.wire_uses[wire]) {
2142 auto succ_vertex = node_map[succ_node];
2143 vertex->succs.insert(succ_vertex);
2144 succ_vertex->preds.insert(vertex);
2145 }
2146 }
2147
2148 auto eval_order = scheduler.schedule();
2149 pool<FlowGraph::Node*, hash_ptr_ops> evaluated;
2150 pool<const RTLIL::Wire*> feedback_wires;
2151 for (auto vertex : eval_order) {
2152 auto node = vertex->data;
2153 schedule[module].push_back(*node);
2154 // Any wire that is an output of node vo and input of node vi where vo is scheduled later than vi
2155 // is a feedback wire. Feedback wires indicate apparent logic loops in the design, which may be
2156 // caused by a true logic loop, but usually are a benign result of dependency tracking that works
2157 // on wire, not bit, level. Nevertheless, feedback wires cannot be localized.
2158 evaluated.insert(node);
2159 for (auto wire : node_defs[node])
2160 for (auto succ_node : flow.wire_uses[wire])
2161 if (evaluated[succ_node]) {
2162 feedback_wires.insert(wire);
2163 // Feedback wires may never be elided because feedback requires state, but the point of elision
2164 // (and localization) is to eliminate state.
2165 elided_wires.erase(wire);
2166 }
2167 }
2168
2169 if (!feedback_wires.empty()) {
2170 has_feedback_arcs = true;
2171 log("Module `%s' contains feedback arcs through wires:\n", log_id(module));
2172 for (auto wire : feedback_wires)
2173 log(" %s\n", log_id(wire));
2174 }
2175
2176 for (auto wire : module->wires()) {
2177 if (feedback_wires[wire]) continue;
2178 if (wire->port_output && !module->get_bool_attribute(ID::top)) continue;
2179 if (wire->name.begins_with("$") && !unbuffer_internal) continue;
2180 if (wire->name.begins_with("\\") && !unbuffer_public) continue;
2181 if (flow.wire_sync_defs.count(wire) > 0) continue;
2182 unbuffered_wires.insert(wire);
2183 if (edge_wires[wire]) continue;
2184 if (wire->get_bool_attribute(ID::keep)) continue;
2185 if (wire->port_input || wire->port_output) continue;
2186 if (wire->name.begins_with("$") && !localize_internal) continue;
2187 if (wire->name.begins_with("\\") && !localize_public) continue;
2188 localized_wires.insert(wire);
2189 }
2190
2191 // For maximum performance, the state of the simulation (which is the same as the set of its double buffered
2192 // wires, since using a singly buffered wire for any kind of state introduces a race condition) should contain
2193 // no wires attached to combinatorial outputs. Feedback wires, by definition, make that impossible. However,
2194 // it is possible that a design with no feedback arcs would end up with doubly buffered wires in such cases
2195 // as a wire with multiple drivers where one of them is combinatorial and the other is synchronous. Such designs
2196 // also require more than one delta cycle to converge.
2197 pool<const RTLIL::Wire*> buffered_comb_wires;
2198 for (auto wire : module->wires()) {
2199 if (flow.wire_comb_defs[wire].size() > 0 && !unbuffered_wires[wire] && !feedback_wires[wire])
2200 buffered_comb_wires.insert(wire);
2201 }
2202 if (!buffered_comb_wires.empty()) {
2203 has_buffered_comb_wires = true;
2204 log("Module `%s' contains buffered combinatorial wires:\n", log_id(module));
2205 for (auto wire : buffered_comb_wires)
2206 log(" %s\n", log_id(wire));
2207 }
2208
2209 eval_converges[module] = feedback_wires.empty() && buffered_comb_wires.empty();
2210
2211 if (debug_info) {
2212 // Find wires that alias other wires or are tied to a constant; debug information can be enriched with these
2213 // at essentially zero additional cost.
2214 //
2215 // Note that the information collected here can't be used for optimizing the netlist: debug information queries
2216 // are pure and run on a design in a stable state, which allows assumptions that do not otherwise hold.
2217 for (auto wire : module->wires()) {
2218 if (wire->name[0] != '\\')
2219 continue;
2220 if (!unbuffered_wires[wire])
2221 continue;
2222 const RTLIL::Wire *wire_it = wire;
2223 while (1) {
2224 if (!(flow.wire_def_elidable.count(wire_it) && flow.wire_def_elidable[wire_it]))
2225 break; // not an alias: complex def
2226 log_assert(flow.wire_comb_defs[wire_it].size() == 1);
2227 FlowGraph::Node *node = *flow.wire_comb_defs[wire_it].begin();
2228 if (node->type != FlowGraph::Node::Type::CONNECT)
2229 break; // not an alias: def by cell
2230 RTLIL::SigSpec rhs_sig = node->connect.second;
2231 if (rhs_sig.is_wire()) {
2232 RTLIL::Wire *rhs_wire = rhs_sig.as_wire();
2233 if (unbuffered_wires[rhs_wire]) {
2234 wire_it = rhs_wire; // maybe an alias
2235 } else {
2236 debug_alias_wires[wire] = rhs_wire; // is an alias
2237 break;
2238 }
2239 } else if (rhs_sig.is_fully_const()) {
2240 debug_const_wires[wire] = rhs_sig.as_const(); // is a const
2241 break;
2242 } else {
2243 break; // not an alias: complex rhs
2244 }
2245 }
2246 }
2247 }
2248 }
2249 if (has_feedback_arcs || has_buffered_comb_wires) {
2250 // Although both non-feedback buffered combinatorial wires and apparent feedback wires may be eliminated
2251 // by optimizing the design, if after `proc; flatten` there are any feedback wires remaining, it is very
2252 // likely that these feedback wires are indicative of a true logic loop, so they get emphasized in the message.
2253 const char *why_pessimistic = nullptr;
2254 if (has_feedback_arcs)
2255 why_pessimistic = "feedback wires";
2256 else if (has_buffered_comb_wires)
2257 why_pessimistic = "buffered combinatorial wires";
2258 log_warning("Design contains %s, which require delta cycles during evaluation.\n", why_pessimistic);
2259 if (!run_flatten)
2260 log("Flattening may eliminate %s from the design.\n", why_pessimistic);
2261 if (!run_proc)
2262 log("Converting processes to netlists may eliminate %s from the design.\n", why_pessimistic);
2263 }
2264 }
2265
2266 void check_design(RTLIL::Design *design, bool &has_sync_init, bool &has_packed_mem)
2267 {
2268 has_sync_init = has_packed_mem = false;
2269
2270 for (auto module : design->modules()) {
2271 if (module->get_blackbox_attribute() && !module->has_attribute(ID(cxxrtl_blackbox)))
2272 continue;
2273
2274 if (!design->selected_whole_module(module))
2275 if (design->selected_module(module))
2276 log_cmd_error("Can't handle partially selected module `%s'!\n", id2cstr(module->name));
2277 if (!design->selected_module(module))
2278 continue;
2279
2280 for (auto proc : module->processes)
2281 for (auto sync : proc.second->syncs)
2282 if (sync->type == RTLIL::STi)
2283 has_sync_init = true;
2284
2285 for (auto cell : module->cells())
2286 if (cell->type == ID($mem))
2287 has_packed_mem = true;
2288 }
2289 }
2290
2291 void prepare_design(RTLIL::Design *design)
2292 {
2293 bool did_anything = false;
2294 bool has_sync_init, has_packed_mem;
2295 log_push();
2296 check_design(design, has_sync_init, has_packed_mem);
2297 if (run_flatten) {
2298 Pass::call(design, "flatten");
2299 did_anything = true;
2300 }
2301 if (run_proc) {
2302 Pass::call(design, "proc");
2303 did_anything = true;
2304 } else if (has_sync_init) {
2305 // We're only interested in proc_init, but it depends on proc_prune and proc_clean, so call those
2306 // in case they weren't already. (This allows `yosys foo.v -o foo.cc` to work.)
2307 Pass::call(design, "proc_prune");
2308 Pass::call(design, "proc_clean");
2309 Pass::call(design, "proc_init");
2310 did_anything = true;
2311 }
2312 if (has_packed_mem) {
2313 Pass::call(design, "memory_unpack");
2314 did_anything = true;
2315 }
2316 // Recheck the design if it was modified.
2317 if (has_sync_init || has_packed_mem)
2318 check_design(design, has_sync_init, has_packed_mem);
2319 log_assert(!(has_sync_init || has_packed_mem));
2320 log_pop();
2321 if (did_anything)
2322 log_spacer();
2323 analyze_design(design);
2324 }
2325 };
2326
2327 struct CxxrtlBackend : public Backend {
2328 static const int DEFAULT_OPT_LEVEL = 6;
2329 static const int OPT_LEVEL_DEBUG = 4;
2330 static const int DEFAULT_DEBUG_LEVEL = 1;
2331
2332 CxxrtlBackend() : Backend("cxxrtl", "convert design to C++ RTL simulation") { }
2333 void help() override
2334 {
2335 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
2336 log("\n");
2337 log(" write_cxxrtl [options] [filename]\n");
2338 log("\n");
2339 log("Write C++ code that simulates the design. The generated code requires a driver\n");
2340 log("that instantiates the design, toggles its clock, and interacts with its ports.\n");
2341 log("\n");
2342 log("The following driver may be used as an example for a design with a single clock\n");
2343 log("driving rising edge triggered flip-flops:\n");
2344 log("\n");
2345 log(" #include \"top.cc\"\n");
2346 log("\n");
2347 log(" int main() {\n");
2348 log(" cxxrtl_design::p_top top;\n");
2349 log(" top.step();\n");
2350 log(" while (1) {\n");
2351 log(" /* user logic */\n");
2352 log(" top.p_clk.set(false);\n");
2353 log(" top.step();\n");
2354 log(" top.p_clk.set(true);\n");
2355 log(" top.step();\n");
2356 log(" }\n");
2357 log(" }\n");
2358 log("\n");
2359 log("Note that CXXRTL simulations, just like the hardware they are simulating, are\n");
2360 log("subject to race conditions. If, in the example above, the user logic would run\n");
2361 log("simultaneously with the rising edge of the clock, the design would malfunction.\n");
2362 log("\n");
2363 log("This backend supports replacing parts of the design with black boxes implemented\n");
2364 log("in C++. If a module marked as a CXXRTL black box, its implementation is ignored,\n");
2365 log("and the generated code consists only of an interface and a factory function.\n");
2366 log("The driver must implement the factory function that creates an implementation of\n");
2367 log("the black box, taking into account the parameters it is instantiated with.\n");
2368 log("\n");
2369 log("For example, the following Verilog code defines a CXXRTL black box interface for\n");
2370 log("a synchronous debug sink:\n");
2371 log("\n");
2372 log(" (* cxxrtl_blackbox *)\n");
2373 log(" module debug(...);\n");
2374 log(" (* cxxrtl_edge = \"p\" *) input clk;\n");
2375 log(" input en;\n");
2376 log(" input [7:0] i_data;\n");
2377 log(" (* cxxrtl_sync *) output [7:0] o_data;\n");
2378 log(" endmodule\n");
2379 log("\n");
2380 log("For this HDL interface, this backend will generate the following C++ interface:\n");
2381 log("\n");
2382 log(" struct bb_p_debug : public module {\n");
2383 log(" value<1> p_clk;\n");
2384 log(" bool posedge_p_clk() const { /* ... */ }\n");
2385 log(" value<1> p_en;\n");
2386 log(" value<8> p_i_data;\n");
2387 log(" wire<8> p_o_data;\n");
2388 log("\n");
2389 log(" bool eval() override;\n");
2390 log(" bool commit() override;\n");
2391 log("\n");
2392 log(" static std::unique_ptr<bb_p_debug>\n");
2393 log(" create(std::string name, metadata_map parameters, metadata_map attributes);\n");
2394 log(" };\n");
2395 log("\n");
2396 log("The `create' function must be implemented by the driver. For example, it could\n");
2397 log("always provide an implementation logging the values to standard error stream:\n");
2398 log("\n");
2399 log(" namespace cxxrtl_design {\n");
2400 log("\n");
2401 log(" struct stderr_debug : public bb_p_debug {\n");
2402 log(" bool eval() override {\n");
2403 log(" if (posedge_p_clk() && p_en)\n");
2404 log(" fprintf(stderr, \"debug: %%02x\\n\", p_i_data.data[0]);\n");
2405 log(" p_o_data.next = p_i_data;\n");
2406 log(" return bb_p_debug::eval();\n");
2407 log(" }\n");
2408 log(" };\n");
2409 log("\n");
2410 log(" std::unique_ptr<bb_p_debug>\n");
2411 log(" bb_p_debug::create(std::string name, cxxrtl::metadata_map parameters,\n");
2412 log(" cxxrtl::metadata_map attributes) {\n");
2413 log(" return std::make_unique<stderr_debug>();\n");
2414 log(" }\n");
2415 log("\n");
2416 log(" }\n");
2417 log("\n");
2418 log("For complex applications of black boxes, it is possible to parameterize their\n");
2419 log("port widths. For example, the following Verilog code defines a CXXRTL black box\n");
2420 log("interface for a configurable width debug sink:\n");
2421 log("\n");
2422 log(" (* cxxrtl_blackbox, cxxrtl_template = \"WIDTH\" *)\n");
2423 log(" module debug(...);\n");
2424 log(" parameter WIDTH = 8;\n");
2425 log(" (* cxxrtl_edge = \"p\" *) input clk;\n");
2426 log(" input en;\n");
2427 log(" (* cxxrtl_width = \"WIDTH\" *) input [WIDTH - 1:0] i_data;\n");
2428 log(" (* cxxrtl_width = \"WIDTH\" *) output [WIDTH - 1:0] o_data;\n");
2429 log(" endmodule\n");
2430 log("\n");
2431 log("For this parametric HDL interface, this backend will generate the following C++\n");
2432 log("interface (only the differences are shown):\n");
2433 log("\n");
2434 log(" template<size_t WIDTH>\n");
2435 log(" struct bb_p_debug : public module {\n");
2436 log(" // ...\n");
2437 log(" value<WIDTH> p_i_data;\n");
2438 log(" wire<WIDTH> p_o_data;\n");
2439 log(" // ...\n");
2440 log(" static std::unique_ptr<bb_p_debug<WIDTH>>\n");
2441 log(" create(std::string name, metadata_map parameters, metadata_map attributes);\n");
2442 log(" };\n");
2443 log("\n");
2444 log("The `create' function must be implemented by the driver, specialized for every\n");
2445 log("possible combination of template parameters. (Specialization is necessary to\n");
2446 log("enable separate compilation of generated code and black box implementations.)\n");
2447 log("\n");
2448 log(" template<size_t SIZE>\n");
2449 log(" struct stderr_debug : public bb_p_debug<SIZE> {\n");
2450 log(" // ...\n");
2451 log(" };\n");
2452 log("\n");
2453 log(" template<>\n");
2454 log(" std::unique_ptr<bb_p_debug<8>>\n");
2455 log(" bb_p_debug<8>::create(std::string name, cxxrtl::metadata_map parameters,\n");
2456 log(" cxxrtl::metadata_map attributes) {\n");
2457 log(" return std::make_unique<stderr_debug<8>>();\n");
2458 log(" }\n");
2459 log("\n");
2460 log("The following attributes are recognized by this backend:\n");
2461 log("\n");
2462 log(" cxxrtl_blackbox\n");
2463 log(" only valid on modules. if specified, the module contents are ignored,\n");
2464 log(" and the generated code includes only the module interface and a factory\n");
2465 log(" function, which will be called to instantiate the module.\n");
2466 log("\n");
2467 log(" cxxrtl_edge\n");
2468 log(" only valid on inputs of black boxes. must be one of \"p\", \"n\", \"a\".\n");
2469 log(" if specified on signal `clk`, the generated code includes edge detectors\n");
2470 log(" `posedge_p_clk()` (if \"p\"), `negedge_p_clk()` (if \"n\"), or both (if\n");
2471 log(" \"a\"), simplifying implementation of clocked black boxes.\n");
2472 log("\n");
2473 log(" cxxrtl_template\n");
2474 log(" only valid on black boxes. must contain a space separated sequence of\n");
2475 log(" identifiers that have a corresponding black box parameters. for each\n");
2476 log(" of them, the generated code includes a `size_t` template parameter.\n");
2477 log("\n");
2478 log(" cxxrtl_width\n");
2479 log(" only valid on ports of black boxes. must be a constant expression, which\n");
2480 log(" is directly inserted into generated code.\n");
2481 log("\n");
2482 log(" cxxrtl_comb, cxxrtl_sync\n");
2483 log(" only valid on outputs of black boxes. if specified, indicates that every\n");
2484 log(" bit of the output port is driven, correspondingly, by combinatorial or\n");
2485 log(" synchronous logic. this knowledge is used for scheduling optimizations.\n");
2486 log(" if neither is specified, the output will be pessimistically treated as\n");
2487 log(" driven by both combinatorial and synchronous logic.\n");
2488 log("\n");
2489 log("The following options are supported by this backend:\n");
2490 log("\n");
2491 log(" -header\n");
2492 log(" generate separate interface (.h) and implementation (.cc) files.\n");
2493 log(" if specified, the backend must be called with a filename, and filename\n");
2494 log(" of the interface is derived from filename of the implementation.\n");
2495 log(" otherwise, interface and implementation are generated together.\n");
2496 log("\n");
2497 log(" -namespace <ns-name>\n");
2498 log(" place the generated code into namespace <ns-name>. if not specified,\n");
2499 log(" \"cxxrtl_design\" is used.\n");
2500 log("\n");
2501 log(" -noflatten\n");
2502 log(" don't flatten the design. fully flattened designs can evaluate within\n");
2503 log(" one delta cycle if they have no combinatorial feedback.\n");
2504 log(" note that the debug interface and waveform dumps use full hierarchical\n");
2505 log(" names for all wires even in flattened designs.\n");
2506 log("\n");
2507 log(" -noproc\n");
2508 log(" don't convert processes to netlists. in most designs, converting\n");
2509 log(" processes significantly improves evaluation performance at the cost of\n");
2510 log(" slight increase in compilation time.\n");
2511 log("\n");
2512 log(" -O <level>\n");
2513 log(" set the optimization level. the default is -O%d. higher optimization\n", DEFAULT_OPT_LEVEL);
2514 log(" levels dramatically decrease compile and run time, and highest level\n");
2515 log(" possible for a design should be used.\n");
2516 log("\n");
2517 log(" -O0\n");
2518 log(" no optimization.\n");
2519 log("\n");
2520 log(" -O1\n");
2521 log(" localize internal wires if possible.\n");
2522 log("\n");
2523 log(" -O2\n");
2524 log(" like -O1, and unbuffer internal wires if possible.\n");
2525 log("\n");
2526 log(" -O3\n");
2527 log(" like -O2, and elide internal wires if possible.\n");
2528 log("\n");
2529 log(" -O4\n");
2530 log(" like -O3, and unbuffer public wires not marked (*keep*) if possible.\n");
2531 log("\n");
2532 log(" -O5\n");
2533 log(" like -O4, and localize public wires not marked (*keep*) if possible.\n");
2534 log("\n");
2535 log(" -O6\n");
2536 log(" like -O5, and elide public wires not marked (*keep*) if possible.\n");
2537 log("\n");
2538 log(" -Og\n");
2539 log(" highest optimization level that provides debug information for all\n");
2540 log(" public wires. currently, alias for -O%d.\n", OPT_LEVEL_DEBUG);
2541 log("\n");
2542 log(" -g <level>\n");
2543 log(" set the debug level. the default is -g%d. higher debug levels provide\n", DEFAULT_DEBUG_LEVEL);
2544 log(" more visibility and generate more code, but do not pessimize evaluation.\n");
2545 log("\n");
2546 log(" -g0\n");
2547 log(" no debug information.\n");
2548 log("\n");
2549 log(" -g1\n");
2550 log(" debug information for non-optimized public wires. this also makes it\n");
2551 log(" possible to use the C API.\n");
2552 log("\n");
2553 }
2554
2555 void execute(std::ostream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design) override
2556 {
2557 bool noflatten = false;
2558 bool noproc = false;
2559 int opt_level = DEFAULT_OPT_LEVEL;
2560 int debug_level = DEFAULT_DEBUG_LEVEL;
2561 CxxrtlWorker worker;
2562
2563 log_header(design, "Executing CXXRTL backend.\n");
2564
2565 size_t argidx;
2566 for (argidx = 1; argidx < args.size(); argidx++)
2567 {
2568 if (args[argidx] == "-noflatten") {
2569 noflatten = true;
2570 continue;
2571 }
2572 if (args[argidx] == "-noproc") {
2573 noproc = true;
2574 continue;
2575 }
2576 if (args[argidx] == "-Og") {
2577 opt_level = OPT_LEVEL_DEBUG;
2578 continue;
2579 }
2580 if (args[argidx] == "-O" && argidx+1 < args.size() && args[argidx+1] == "g") {
2581 argidx++;
2582 opt_level = OPT_LEVEL_DEBUG;
2583 continue;
2584 }
2585 if (args[argidx] == "-O" && argidx+1 < args.size()) {
2586 opt_level = std::stoi(args[++argidx]);
2587 continue;
2588 }
2589 if (args[argidx].substr(0, 2) == "-O" && args[argidx].size() == 3 && isdigit(args[argidx][2])) {
2590 opt_level = std::stoi(args[argidx].substr(2));
2591 continue;
2592 }
2593 if (args[argidx] == "-g" && argidx+1 < args.size()) {
2594 debug_level = std::stoi(args[++argidx]);
2595 continue;
2596 }
2597 if (args[argidx].substr(0, 2) == "-g" && args[argidx].size() == 3 && isdigit(args[argidx][2])) {
2598 debug_level = std::stoi(args[argidx].substr(2));
2599 continue;
2600 }
2601 if (args[argidx] == "-header") {
2602 worker.split_intf = true;
2603 continue;
2604 }
2605 if (args[argidx] == "-namespace" && argidx+1 < args.size()) {
2606 worker.design_ns = args[++argidx];
2607 continue;
2608 }
2609 break;
2610 }
2611 extra_args(f, filename, args, argidx);
2612
2613 worker.run_flatten = !noflatten;
2614 worker.run_proc = !noproc;
2615 switch (opt_level) {
2616 // the highest level here must match DEFAULT_OPT_LEVEL
2617 case 6:
2618 worker.elide_public = true;
2619 YS_FALLTHROUGH
2620 case 5:
2621 worker.localize_public = true;
2622 YS_FALLTHROUGH
2623 case 4:
2624 worker.unbuffer_public = true;
2625 YS_FALLTHROUGH
2626 case 3:
2627 worker.elide_internal = true;
2628 YS_FALLTHROUGH
2629 case 2:
2630 worker.localize_internal = true;
2631 YS_FALLTHROUGH
2632 case 1:
2633 worker.unbuffer_internal = true;
2634 YS_FALLTHROUGH
2635 case 0:
2636 break;
2637 default:
2638 log_cmd_error("Invalid optimization level %d.\n", opt_level);
2639 }
2640 switch (debug_level) {
2641 // the highest level here must match DEFAULT_DEBUG_LEVEL
2642 case 1:
2643 worker.debug_info = true;
2644 YS_FALLTHROUGH
2645 case 0:
2646 break;
2647 default:
2648 log_cmd_error("Invalid debug information level %d.\n", debug_level);
2649 }
2650
2651 std::ofstream intf_f;
2652 if (worker.split_intf) {
2653 if (filename == "<stdout>")
2654 log_cmd_error("Option -header must be used with a filename.\n");
2655
2656 worker.intf_filename = filename.substr(0, filename.rfind('.')) + ".h";
2657 intf_f.open(worker.intf_filename, std::ofstream::trunc);
2658 if (intf_f.fail())
2659 log_cmd_error("Can't open file `%s' for writing: %s\n",
2660 worker.intf_filename.c_str(), strerror(errno));
2661
2662 worker.intf_f = &intf_f;
2663 }
2664 worker.impl_f = f;
2665
2666 worker.prepare_design(design);
2667 worker.dump_design(design);
2668 }
2669 } CxxrtlBackend;
2670
2671 PRIVATE_NAMESPACE_END