Merge pull request #1896 from boqwxp/read_stdin_repl
[yosys.git] / backends / firrtl / firrtl.cc
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 */
19
20 #include "kernel/rtlil.h"
21 #include "kernel/register.h"
22 #include "kernel/sigtools.h"
23 #include "kernel/celltypes.h"
24 #include "kernel/cellaigs.h"
25 #include "kernel/log.h"
26 #include <algorithm>
27 #include <string>
28 #include <vector>
29 #include <cmath>
30
31 USING_YOSYS_NAMESPACE
32 PRIVATE_NAMESPACE_BEGIN
33
34 pool<string> used_names;
35 dict<IdString, string> namecache;
36 int autoid_counter;
37
38 typedef unsigned FDirection;
39 static const FDirection FD_NODIRECTION = 0x0;
40 static const FDirection FD_IN = 0x1;
41 static const FDirection FD_OUT = 0x2;
42 static const FDirection FD_INOUT = 0x3;
43 static const int FIRRTL_MAX_DSH_WIDTH_ERROR = 20; // For historic reasons, this is actually one greater than the maximum allowed shift width
44
45 std::string getFileinfo(const RTLIL::AttrObject *design_entity)
46 {
47 std::string src(design_entity->get_src_attribute());
48 std::string fileinfo_str = src.empty() ? "" : "@[" + src + "]";
49 return fileinfo_str;
50 }
51
52 // Get a port direction with respect to a specific module.
53 FDirection getPortFDirection(IdString id, Module *module)
54 {
55 Wire *wire = module->wires_.at(id);
56 FDirection direction = FD_NODIRECTION;
57 if (wire && wire->port_id)
58 {
59 if (wire->port_input)
60 direction |= FD_IN;
61 if (wire->port_output)
62 direction |= FD_OUT;
63 }
64 return direction;
65 }
66
67 string next_id()
68 {
69 string new_id;
70
71 while (1) {
72 new_id = stringf("_%d", autoid_counter++);
73 if (used_names.count(new_id) == 0) break;
74 }
75
76 used_names.insert(new_id);
77 return new_id;
78 }
79
80 const char *make_id(IdString id)
81 {
82 if (namecache.count(id) != 0)
83 return namecache.at(id).c_str();
84
85 string new_id = log_id(id);
86
87 for (int i = 0; i < GetSize(new_id); i++)
88 {
89 char &ch = new_id[i];
90 if ('a' <= ch && ch <= 'z') continue;
91 if ('A' <= ch && ch <= 'Z') continue;
92 if ('0' <= ch && ch <= '9' && i != 0) continue;
93 if ('_' == ch) continue;
94 ch = '_';
95 }
96
97 while (used_names.count(new_id) != 0)
98 new_id += '_';
99
100 namecache[id] = new_id;
101 used_names.insert(new_id);
102 return namecache.at(id).c_str();
103 }
104
105 struct FirrtlWorker
106 {
107 Module *module;
108 std::ostream &f;
109
110 dict<SigBit, pair<string, int>> reverse_wire_map;
111 string unconn_id;
112 RTLIL::Design *design;
113 std::string indent;
114
115 // Define read/write ports and memories.
116 // We'll collect their definitions and emit the corresponding FIRRTL definitions at the appropriate point in module construction.
117 // For the moment, we don't handle $readmemh or $readmemb.
118 // These will be part of a subsequent PR.
119 struct read_port {
120 string name;
121 bool clk_enable;
122 bool clk_parity;
123 bool transparent;
124 RTLIL::SigSpec clk;
125 RTLIL::SigSpec ena;
126 RTLIL::SigSpec addr;
127 read_port(string name, bool clk_enable, bool clk_parity, bool transparent, RTLIL::SigSpec clk, RTLIL::SigSpec ena, RTLIL::SigSpec addr) : name(name), clk_enable(clk_enable), clk_parity(clk_parity), transparent(transparent), clk(clk), ena(ena), addr(addr) {
128 // Current (3/13/2019) conventions:
129 // generate a constant 0 for clock and a constant 1 for enable if they are undefined.
130 if (!clk.is_fully_def())
131 this->clk = SigSpec(State::S0);
132 if (!ena.is_fully_def())
133 this->ena = SigSpec(State::S1);
134 }
135 string gen_read(const char * indent) {
136 string addr_expr = make_expr(addr);
137 string ena_expr = make_expr(ena);
138 string clk_expr = make_expr(clk);
139 string addr_str = stringf("%s%s.addr <= %s\n", indent, name.c_str(), addr_expr.c_str());
140 string ena_str = stringf("%s%s.en <= %s\n", indent, name.c_str(), ena_expr.c_str());
141 string clk_str = stringf("%s%s.clk <= asClock(%s)\n", indent, name.c_str(), clk_expr.c_str());
142 return addr_str + ena_str + clk_str;
143 }
144 };
145 struct write_port : read_port {
146 RTLIL::SigSpec mask;
147 write_port(string name, bool clk_enable, bool clk_parity, bool transparent, RTLIL::SigSpec clk, RTLIL::SigSpec ena, RTLIL::SigSpec addr, RTLIL::SigSpec mask) : read_port(name, clk_enable, clk_parity, transparent, clk, ena, addr), mask(mask) {
148 if (!clk.is_fully_def())
149 this->clk = SigSpec(RTLIL::Const(0));
150 if (!ena.is_fully_def())
151 this->ena = SigSpec(RTLIL::Const(0));
152 if (!mask.is_fully_def())
153 this->ena = SigSpec(RTLIL::Const(1));
154 }
155 string gen_read(const char * /* indent */) {
156 log_error("gen_read called on write_port: %s\n", name.c_str());
157 return stringf("gen_read called on write_port: %s\n", name.c_str());
158 }
159 string gen_write(const char * indent) {
160 string addr_expr = make_expr(addr);
161 string ena_expr = make_expr(ena);
162 string clk_expr = make_expr(clk);
163 string mask_expr = make_expr(mask);
164 string mask_str = stringf("%s%s.mask <= %s\n", indent, name.c_str(), mask_expr.c_str());
165 string addr_str = stringf("%s%s.addr <= %s\n", indent, name.c_str(), addr_expr.c_str());
166 string ena_str = stringf("%s%s.en <= %s\n", indent, name.c_str(), ena_expr.c_str());
167 string clk_str = stringf("%s%s.clk <= asClock(%s)\n", indent, name.c_str(), clk_expr.c_str());
168 return addr_str + ena_str + clk_str + mask_str;
169 }
170 };
171 /* Memories defined within this module. */
172 struct memory {
173 Cell *pCell; // for error reporting
174 string name; // memory name
175 int abits; // number of address bits
176 int size; // size (in units) of the memory
177 int width; // size (in bits) of each element
178 int read_latency;
179 int write_latency;
180 vector<read_port> read_ports;
181 vector<write_port> write_ports;
182 std::string init_file;
183 std::string init_file_srcFileSpec;
184 string srcLine;
185 memory(Cell *pCell, string name, int abits, int size, int width) : pCell(pCell), name(name), abits(abits), size(size), width(width), read_latency(0), write_latency(1), init_file(""), init_file_srcFileSpec("") {
186 // Provide defaults for abits or size if one (but not the other) is specified.
187 if (this->abits == 0 && this->size != 0) {
188 this->abits = ceil_log2(this->size);
189 } else if (this->abits != 0 && this->size == 0) {
190 this->size = 1 << this->abits;
191 }
192 // Sanity-check this construction.
193 if (this->name == "") {
194 log_error("Nameless memory%s\n", this->atLine());
195 }
196 if (this->abits == 0 && this->size == 0) {
197 log_error("Memory %s has zero address bits and size%s\n", this->name.c_str(), this->atLine());
198 }
199 if (this->width == 0) {
200 log_error("Memory %s has zero width%s\n", this->name.c_str(), this->atLine());
201 }
202 }
203
204 // We need a default constructor for the dict insert.
205 memory() : pCell(0), read_latency(0), write_latency(1), init_file(""), init_file_srcFileSpec(""){}
206
207 const char *atLine() {
208 if (srcLine == "") {
209 if (pCell) {
210 auto p = pCell->attributes.find(ID::src);
211 srcLine = " at " + p->second.decode_string();
212 }
213 }
214 return srcLine.c_str();
215 }
216 void add_memory_read_port(read_port &rp) {
217 read_ports.push_back(rp);
218 }
219 void add_memory_write_port(write_port &wp) {
220 write_ports.push_back(wp);
221 }
222 void add_memory_file(std::string init_file, std::string init_file_srcFileSpec) {
223 this->init_file = init_file;
224 this->init_file_srcFileSpec = init_file_srcFileSpec;
225 }
226
227 };
228 dict<string, memory> memories;
229
230 void register_memory(memory &m)
231 {
232 memories[m.name] = m;
233 }
234
235 void register_reverse_wire_map(string id, SigSpec sig)
236 {
237 for (int i = 0; i < GetSize(sig); i++)
238 reverse_wire_map[sig[i]] = make_pair(id, i);
239 }
240
241 FirrtlWorker(Module *module, std::ostream &f, RTLIL::Design *theDesign) : module(module), f(f), design(theDesign), indent(" ")
242 {
243 }
244
245 static string make_expr(const SigSpec &sig)
246 {
247 string expr;
248
249 for (auto chunk : sig.chunks())
250 {
251 string new_expr;
252
253 if (chunk.wire == nullptr)
254 {
255 std::vector<RTLIL::State> bits = chunk.data;
256 new_expr = stringf("UInt<%d>(\"h", GetSize(bits));
257
258 while (GetSize(bits) % 4 != 0)
259 bits.push_back(State::S0);
260
261 for (int i = GetSize(bits)-4; i >= 0; i -= 4)
262 {
263 int val = 0;
264 if (bits[i+0] == State::S1) val += 1;
265 if (bits[i+1] == State::S1) val += 2;
266 if (bits[i+2] == State::S1) val += 4;
267 if (bits[i+3] == State::S1) val += 8;
268 new_expr.push_back(val < 10 ? '0' + val : 'a' + val - 10);
269 }
270
271 new_expr += "\")";
272 }
273 else if (chunk.offset == 0 && chunk.width == chunk.wire->width)
274 {
275 new_expr = make_id(chunk.wire->name);
276 }
277 else
278 {
279 string wire_id = make_id(chunk.wire->name);
280 new_expr = stringf("bits(%s, %d, %d)", wire_id.c_str(), chunk.offset + chunk.width - 1, chunk.offset);
281 }
282
283 if (expr.empty())
284 expr = new_expr;
285 else
286 expr = "cat(" + new_expr + ", " + expr + ")";
287 }
288
289 return expr;
290 }
291
292 std::string fid(RTLIL::IdString internal_id)
293 {
294 return make_id(internal_id);
295 }
296
297 std::string cellname(RTLIL::Cell *cell)
298 {
299 return fid(cell->name).c_str();
300 }
301
302 void process_instance(RTLIL::Cell *cell, vector<string> &wire_exprs)
303 {
304 std::string cell_type = fid(cell->type);
305 std::string instanceOf;
306 // If this is a parameterized module, its parent module is encoded in the cell type
307 if (cell->type.begins_with("$paramod"))
308 {
309 std::string::iterator it;
310 for (it = cell_type.begin(); it < cell_type.end(); it++)
311 {
312 switch (*it) {
313 case '\\': /* FALL_THROUGH */
314 case '=': /* FALL_THROUGH */
315 case '\'': /* FALL_THROUGH */
316 case '$': instanceOf.append("_"); break;
317 default: instanceOf.append(1, *it); break;
318 }
319 }
320 }
321 else
322 {
323 instanceOf = cell_type;
324 }
325
326 std::string cell_name = cellname(cell);
327 std::string cell_name_comment;
328 if (cell_name != fid(cell->name))
329 cell_name_comment = " /* " + fid(cell->name) + " */ ";
330 else
331 cell_name_comment = "";
332 // Find the module corresponding to this instance.
333 auto instModule = design->module(cell->type);
334 // If there is no instance for this, just return.
335 if (instModule == NULL)
336 {
337 log_warning("No instance for %s.%s\n", cell_type.c_str(), cell_name.c_str());
338 return;
339 }
340 std::string cellFileinfo = getFileinfo(cell);
341 wire_exprs.push_back(stringf("%s" "inst %s%s of %s %s", indent.c_str(), cell_name.c_str(), cell_name_comment.c_str(), instanceOf.c_str(), cellFileinfo.c_str()));
342
343 for (auto it = cell->connections().begin(); it != cell->connections().end(); ++it) {
344 if (it->second.size() > 0) {
345 const SigSpec &secondSig = it->second;
346 const std::string firstName = cell_name + "." + make_id(it->first);
347 const std::string secondExpr = make_expr(secondSig);
348 // Find the direction for this port.
349 FDirection dir = getPortFDirection(it->first, instModule);
350 std::string sourceExpr, sinkExpr;
351 const SigSpec *sinkSig = nullptr;
352 switch (dir) {
353 case FD_INOUT:
354 log_warning("Instance port connection %s.%s is INOUT; treating as OUT\n", cell_type.c_str(), log_signal(it->second));
355 /* FALLTHRU */
356 case FD_OUT:
357 sourceExpr = firstName;
358 sinkExpr = secondExpr;
359 sinkSig = &secondSig;
360 break;
361 case FD_NODIRECTION:
362 log_warning("Instance port connection %s.%s is NODIRECTION; treating as IN\n", cell_type.c_str(), log_signal(it->second));
363 /* FALLTHRU */
364 case FD_IN:
365 sourceExpr = secondExpr;
366 sinkExpr = firstName;
367 break;
368 default:
369 log_error("Instance port %s.%s unrecognized connection direction 0x%x !\n", cell_type.c_str(), log_signal(it->second), dir);
370 break;
371 }
372 // Check for subfield assignment.
373 std::string bitsString = "bits(";
374 if (sinkExpr.compare(0, bitsString.length(), bitsString) == 0) {
375 if (sinkSig == nullptr)
376 log_error("Unknown subfield %s.%s\n", cell_type.c_str(), sinkExpr.c_str());
377 // Don't generate the assignment here.
378 // Add the source and sink to the "reverse_wire_map" and we'll output the assignment
379 // as part of the coalesced subfield assignments for this wire.
380 register_reverse_wire_map(sourceExpr, *sinkSig);
381 } else {
382 wire_exprs.push_back(stringf("\n%s%s <= %s %s", indent.c_str(), sinkExpr.c_str(), sourceExpr.c_str(), cellFileinfo.c_str()));
383 }
384 }
385 }
386 wire_exprs.push_back(stringf("\n"));
387
388 }
389
390 // Given an expression for a shift amount, and a maximum width,
391 // generate the FIRRTL expression for equivalent dynamic shift taking into account FIRRTL shift semantics.
392 std::string gen_dshl(const string b_expr, const int b_width)
393 {
394 string result = b_expr;
395 if (b_width >= FIRRTL_MAX_DSH_WIDTH_ERROR) {
396 int max_shift_width_bits = FIRRTL_MAX_DSH_WIDTH_ERROR - 1;
397 string max_shift_string = stringf("UInt<%d>(%d)", max_shift_width_bits, (1<<max_shift_width_bits) - 1);
398 // Deal with the difference in semantics between FIRRTL and verilog
399 result = stringf("mux(gt(%s, %s), %s, bits(%s, %d, 0))", b_expr.c_str(), max_shift_string.c_str(), max_shift_string.c_str(), b_expr.c_str(), max_shift_width_bits - 1);
400 }
401 return result;
402 }
403
404 void run()
405 {
406 std::string moduleFileinfo = getFileinfo(module);
407 f << stringf(" module %s: %s\n", make_id(module->name), moduleFileinfo.c_str());
408 vector<string> port_decls, wire_decls, cell_exprs, wire_exprs;
409
410 for (auto wire : module->wires())
411 {
412 const auto wireName = make_id(wire->name);
413 std::string wireFileinfo = getFileinfo(wire);
414
415 // If a wire has initial data, issue a warning since FIRRTL doesn't currently support it.
416 if (wire->attributes.count(ID::init)) {
417 log_warning("Initial value (%s) for (%s.%s) not supported\n",
418 wire->attributes.at(ID::init).as_string().c_str(),
419 log_id(module), log_id(wire));
420 }
421 if (wire->port_id)
422 {
423 if (wire->port_input && wire->port_output)
424 log_error("Module port %s.%s is inout!\n", log_id(module), log_id(wire));
425 port_decls.push_back(stringf(" %s %s: UInt<%d> %s\n", wire->port_input ? "input" : "output",
426 wireName, wire->width, wireFileinfo.c_str()));
427 }
428 else
429 {
430 wire_decls.push_back(stringf(" wire %s: UInt<%d> %s\n", wireName, wire->width, wireFileinfo.c_str()));
431 }
432 }
433
434 for (auto cell : module->cells())
435 {
436 static Const ndef(0, 0);
437
438 // Is this cell is a module instance?
439 if (cell->type[0] != '$')
440 {
441 process_instance(cell, wire_exprs);
442 continue;
443 }
444 // Not a module instance. Set up cell properties
445 bool extract_y_bits = false; // Assume no extraction of final bits will be required.
446 int a_width = cell->parameters.at(ID::A_WIDTH, ndef).as_int(); // The width of "A"
447 int b_width = cell->parameters.at(ID::B_WIDTH, ndef).as_int(); // The width of "A"
448 const int y_width = cell->parameters.at(ID::Y_WIDTH, ndef).as_int(); // The width of the result
449 const bool a_signed = cell->parameters.at(ID::A_SIGNED, ndef).as_bool();
450 const bool b_signed = cell->parameters.at(ID::B_SIGNED, ndef).as_bool();
451 bool firrtl_is_signed = a_signed; // The result is signed (subsequent code may change this).
452 int firrtl_width = 0;
453 string primop;
454 bool always_uint = false;
455 string y_id = make_id(cell->name);
456 std::string cellFileinfo = getFileinfo(cell);
457
458 if (cell->type.in(ID($not), ID($logic_not), ID($neg), ID($reduce_and), ID($reduce_or), ID($reduce_xor), ID($reduce_bool), ID($reduce_xnor)))
459 {
460 string a_expr = make_expr(cell->getPort(ID::A));
461 wire_decls.push_back(stringf(" wire %s: UInt<%d> %s\n", y_id.c_str(), y_width, cellFileinfo.c_str()));
462
463 if (a_signed) {
464 a_expr = "asSInt(" + a_expr + ")";
465 }
466
467 // Don't use the results of logical operations (a single bit) to control padding
468 if (!(cell->type.in(ID($eq), ID($eqx), ID($gt), ID($ge), ID($lt), ID($le), ID($ne), ID($nex), ID($reduce_bool), ID($logic_not)) && y_width == 1) ) {
469 a_expr = stringf("pad(%s, %d)", a_expr.c_str(), y_width);
470 }
471
472 // Assume the FIRRTL width is a single bit.
473 firrtl_width = 1;
474 if (cell->type == ID($not)) primop = "not";
475 else if (cell->type == ID($neg)) {
476 primop = "neg";
477 firrtl_is_signed = true; // Result of "neg" is signed (an SInt).
478 firrtl_width = a_width;
479 } else if (cell->type == ID($logic_not)) {
480 primop = "eq";
481 a_expr = stringf("%s, UInt(0)", a_expr.c_str());
482 }
483 else if (cell->type == ID($reduce_and)) primop = "andr";
484 else if (cell->type == ID($reduce_or)) primop = "orr";
485 else if (cell->type == ID($reduce_xor)) primop = "xorr";
486 else if (cell->type == ID($reduce_xnor)) {
487 primop = "not";
488 a_expr = stringf("xorr(%s)", a_expr.c_str());
489 }
490 else if (cell->type == ID($reduce_bool)) {
491 primop = "neq";
492 // Use the sign of the a_expr and its width as the type (UInt/SInt) and width of the comparand.
493 a_expr = stringf("%s, %cInt<%d>(0)", a_expr.c_str(), a_signed ? 'S' : 'U', a_width);
494 }
495
496 string expr = stringf("%s(%s)", primop.c_str(), a_expr.c_str());
497
498 if ((firrtl_is_signed && !always_uint))
499 expr = stringf("asUInt(%s)", expr.c_str());
500
501 cell_exprs.push_back(stringf(" %s <= %s %s\n", y_id.c_str(), expr.c_str(), cellFileinfo.c_str()));
502 register_reverse_wire_map(y_id, cell->getPort(ID::Y));
503
504 continue;
505 }
506 if (cell->type.in(ID($add), ID($sub), ID($mul), ID($div), ID($mod), ID($xor), ID($xnor), ID($and), ID($or), ID($eq), ID($eqx),
507 ID($gt), ID($ge), ID($lt), ID($le), ID($ne), ID($nex), ID($shr), ID($sshr), ID($sshl), ID($shl),
508 ID($logic_and), ID($logic_or), ID($pow)))
509 {
510 string a_expr = make_expr(cell->getPort(ID::A));
511 string b_expr = make_expr(cell->getPort(ID::B));
512 std::string cellFileinfo = getFileinfo(cell);
513 wire_decls.push_back(stringf(" wire %s: UInt<%d> %s\n", y_id.c_str(), y_width, cellFileinfo.c_str()));
514
515 if (a_signed) {
516 a_expr = "asSInt(" + a_expr + ")";
517 // Expand the "A" operand to the result width
518 if (a_width < y_width) {
519 a_expr = stringf("pad(%s, %d)", a_expr.c_str(), y_width);
520 a_width = y_width;
521 }
522 }
523 // Shift amount is always unsigned, and needn't be padded to result width,
524 // otherwise, we need to cast the b_expr appropriately
525 if (b_signed && !cell->type.in(ID($shr), ID($sshr), ID($shl), ID($sshl), ID($pow))) {
526 b_expr = "asSInt(" + b_expr + ")";
527 // Expand the "B" operand to the result width
528 if (b_width < y_width) {
529 b_expr = stringf("pad(%s, %d)", b_expr.c_str(), y_width);
530 b_width = y_width;
531 }
532 }
533
534 // For the arithmetic ops, expand operand widths to result widths befor performing the operation.
535 // This corresponds (according to iverilog) to what verilog compilers implement.
536 if (cell->type.in(ID($add), ID($sub), ID($mul), ID($div), ID($mod), ID($xor), ID($xnor), ID($and), ID($or)))
537 {
538 if (a_width < y_width) {
539 a_expr = stringf("pad(%s, %d)", a_expr.c_str(), y_width);
540 a_width = y_width;
541 }
542 if (b_width < y_width) {
543 b_expr = stringf("pad(%s, %d)", b_expr.c_str(), y_width);
544 b_width = y_width;
545 }
546 }
547 // Assume the FIRRTL width is the width of "A"
548 firrtl_width = a_width;
549 auto a_sig = cell->getPort(ID::A);
550
551 if (cell->type == ID($add)) {
552 primop = "add";
553 firrtl_is_signed = a_signed | b_signed;
554 firrtl_width = max(a_width, b_width);
555 } else if (cell->type == ID($sub)) {
556 primop = "sub";
557 firrtl_is_signed = true;
558 int a_widthInc = (!a_signed && b_signed) ? 2 : (a_signed && !b_signed) ? 1 : 0;
559 int b_widthInc = (a_signed && !b_signed) ? 2 : (!a_signed && b_signed) ? 1 : 0;
560 firrtl_width = max(a_width + a_widthInc, b_width + b_widthInc);
561 } else if (cell->type == ID($mul)) {
562 primop = "mul";
563 firrtl_is_signed = a_signed | b_signed;
564 firrtl_width = a_width + b_width;
565 } else if (cell->type == ID($div)) {
566 primop = "div";
567 firrtl_is_signed = a_signed | b_signed;
568 firrtl_width = a_width;
569 } else if (cell->type == ID($mod)) {
570 primop = "rem";
571 firrtl_width = min(a_width, b_width);
572 } else if (cell->type == ID($and)) {
573 primop = "and";
574 always_uint = true;
575 firrtl_width = max(a_width, b_width);
576 }
577 else if (cell->type == ID($or) ) {
578 primop = "or";
579 always_uint = true;
580 firrtl_width = max(a_width, b_width);
581 }
582 else if (cell->type == ID($xor)) {
583 primop = "xor";
584 always_uint = true;
585 firrtl_width = max(a_width, b_width);
586 }
587 else if (cell->type == ID($xnor)) {
588 primop = "xnor";
589 always_uint = true;
590 firrtl_width = max(a_width, b_width);
591 }
592 else if ((cell->type == ID($eq)) | (cell->type == ID($eqx))) {
593 primop = "eq";
594 always_uint = true;
595 firrtl_width = 1;
596 }
597 else if ((cell->type == ID($ne)) | (cell->type == ID($nex))) {
598 primop = "neq";
599 always_uint = true;
600 firrtl_width = 1;
601 }
602 else if (cell->type == ID($gt)) {
603 primop = "gt";
604 always_uint = true;
605 firrtl_width = 1;
606 }
607 else if (cell->type == ID($ge)) {
608 primop = "geq";
609 always_uint = true;
610 firrtl_width = 1;
611 }
612 else if (cell->type == ID($lt)) {
613 primop = "lt";
614 always_uint = true;
615 firrtl_width = 1;
616 }
617 else if (cell->type == ID($le)) {
618 primop = "leq";
619 always_uint = true;
620 firrtl_width = 1;
621 }
622 else if ((cell->type == ID($shl)) | (cell->type == ID($sshl))) {
623 // FIRRTL will widen the result (y) by the amount of the shift.
624 // We'll need to offset this by extracting the un-widened portion as Verilog would do.
625 extract_y_bits = true;
626 // Is the shift amount constant?
627 auto b_sig = cell->getPort(ID::B);
628 if (b_sig.is_fully_const()) {
629 primop = "shl";
630 int shift_amount = b_sig.as_int();
631 b_expr = std::to_string(shift_amount);
632 firrtl_width = a_width + shift_amount;
633 } else {
634 primop = "dshl";
635 // Convert from FIRRTL left shift semantics.
636 b_expr = gen_dshl(b_expr, b_width);
637 firrtl_width = a_width + (1 << b_width) - 1;
638 }
639 }
640 else if ((cell->type == ID($shr)) | (cell->type == ID($sshr))) {
641 // We don't need to extract a specific range of bits.
642 extract_y_bits = false;
643 // Is the shift amount constant?
644 auto b_sig = cell->getPort(ID::B);
645 if (b_sig.is_fully_const()) {
646 primop = "shr";
647 int shift_amount = b_sig.as_int();
648 b_expr = std::to_string(shift_amount);
649 firrtl_width = max(1, a_width - shift_amount);
650 } else {
651 primop = "dshr";
652 firrtl_width = a_width;
653 }
654 // We'll need to do some special fixups if the source (and thus result) is signed.
655 if (firrtl_is_signed) {
656 // If this is a "logical" shift right, pretend the source is unsigned.
657 if (cell->type == ID($shr)) {
658 a_expr = "asUInt(" + a_expr + ")";
659 }
660 }
661 }
662 else if ((cell->type == ID($logic_and))) {
663 primop = "and";
664 a_expr = "neq(" + a_expr + ", UInt(0))";
665 b_expr = "neq(" + b_expr + ", UInt(0))";
666 always_uint = true;
667 firrtl_width = 1;
668 }
669 else if ((cell->type == ID($logic_or))) {
670 primop = "or";
671 a_expr = "neq(" + a_expr + ", UInt(0))";
672 b_expr = "neq(" + b_expr + ", UInt(0))";
673 always_uint = true;
674 firrtl_width = 1;
675 }
676 else if ((cell->type == ID($pow))) {
677 if (a_sig.is_fully_const() && a_sig.as_int() == 2) {
678 // We'll convert this to a shift. To simplify things, change the a_expr to "1"
679 // so we can use b_expr directly as a shift amount.
680 // Only support 2 ** N (i.e., shift left)
681 // FIRRTL will widen the result (y) by the amount of the shift.
682 // We'll need to offset this by extracting the un-widened portion as Verilog would do.
683 a_expr = firrtl_is_signed ? "SInt(1)" : "UInt(1)";
684 extract_y_bits = true;
685 // Is the shift amount constant?
686 auto b_sig = cell->getPort(ID::B);
687 if (b_sig.is_fully_const()) {
688 primop = "shl";
689 int shiftAmount = b_sig.as_int();
690 if (shiftAmount < 0) {
691 log_error("Negative power exponent - %d: %s.%s\n", shiftAmount, log_id(module), log_id(cell));
692 }
693 b_expr = std::to_string(shiftAmount);
694 firrtl_width = a_width + shiftAmount;
695 } else {
696 primop = "dshl";
697 // Convert from FIRRTL left shift semantics.
698 b_expr = gen_dshl(b_expr, b_width);
699 firrtl_width = a_width + (1 << b_width) - 1;
700 }
701 } else {
702 log_error("Non power 2: %s.%s\n", log_id(module), log_id(cell));
703 }
704 }
705
706 if (!cell->parameters.at(ID::B_SIGNED).as_bool()) {
707 b_expr = "asUInt(" + b_expr + ")";
708 }
709
710 string expr;
711 // Deal with $xnor == ~^ (not xor)
712 if (primop == "xnor") {
713 expr = stringf("not(xor(%s, %s))", a_expr.c_str(), b_expr.c_str());
714 } else {
715 expr = stringf("%s(%s, %s)", primop.c_str(), a_expr.c_str(), b_expr.c_str());
716 }
717
718 // Deal with FIRRTL's "shift widens" semantics, or the need to widen the FIRRTL result.
719 // If the operation is signed, the FIRRTL width will be 1 one bit larger.
720 if (extract_y_bits) {
721 expr = stringf("bits(%s, %d, 0)", expr.c_str(), y_width - 1);
722 } else if (firrtl_is_signed && (firrtl_width + 1) < y_width) {
723 expr = stringf("pad(%s, %d)", expr.c_str(), y_width);
724 }
725
726 if ((firrtl_is_signed && !always_uint))
727 expr = stringf("asUInt(%s)", expr.c_str());
728
729 cell_exprs.push_back(stringf(" %s <= %s %s\n", y_id.c_str(), expr.c_str(), cellFileinfo.c_str()));
730 register_reverse_wire_map(y_id, cell->getPort(ID::Y));
731
732 continue;
733 }
734
735 if (cell->type.in(ID($mux)))
736 {
737 int width = cell->parameters.at(ID::WIDTH).as_int();
738 string a_expr = make_expr(cell->getPort(ID::A));
739 string b_expr = make_expr(cell->getPort(ID::B));
740 string s_expr = make_expr(cell->getPort(ID::S));
741 wire_decls.push_back(stringf(" wire %s: UInt<%d> %s\n", y_id.c_str(), width, cellFileinfo.c_str()));
742
743 string expr = stringf("mux(%s, %s, %s)", s_expr.c_str(), b_expr.c_str(), a_expr.c_str());
744
745 cell_exprs.push_back(stringf(" %s <= %s %s\n", y_id.c_str(), expr.c_str(), cellFileinfo.c_str()));
746 register_reverse_wire_map(y_id, cell->getPort(ID::Y));
747
748 continue;
749 }
750
751 if (cell->type.in(ID($mem)))
752 {
753 string mem_id = make_id(cell->name);
754 int abits = cell->parameters.at(ID::ABITS).as_int();
755 int width = cell->parameters.at(ID::WIDTH).as_int();
756 int size = cell->parameters.at(ID::SIZE).as_int();
757 memory m(cell, mem_id, abits, size, width);
758 int rd_ports = cell->parameters.at(ID::RD_PORTS).as_int();
759 int wr_ports = cell->parameters.at(ID::WR_PORTS).as_int();
760
761 Const initdata = cell->parameters.at(ID::INIT);
762 for (State bit : initdata.bits)
763 if (bit != State::Sx)
764 log_error("Memory with initialization data: %s.%s\n", log_id(module), log_id(cell));
765
766 Const rd_clk_enable = cell->parameters.at(ID::RD_CLK_ENABLE);
767 Const wr_clk_enable = cell->parameters.at(ID::WR_CLK_ENABLE);
768 Const wr_clk_polarity = cell->parameters.at(ID::WR_CLK_POLARITY);
769
770 int offset = cell->parameters.at(ID::OFFSET).as_int();
771 if (offset != 0)
772 log_error("Memory with nonzero offset: %s.%s\n", log_id(module), log_id(cell));
773
774 for (int i = 0; i < rd_ports; i++)
775 {
776 if (rd_clk_enable[i] != State::S0)
777 log_error("Clocked read port %d on memory %s.%s.\n", i, log_id(module), log_id(cell));
778
779 SigSpec addr_sig = cell->getPort(ID::RD_ADDR).extract(i*abits, abits);
780 SigSpec data_sig = cell->getPort(ID::RD_DATA).extract(i*width, width);
781 string addr_expr = make_expr(addr_sig);
782 string name(stringf("%s.r%d", m.name.c_str(), i));
783 bool clk_enable = false;
784 bool clk_parity = true;
785 bool transparency = false;
786 SigSpec ena_sig = RTLIL::SigSpec(RTLIL::State::S1, 1);
787 SigSpec clk_sig = RTLIL::SigSpec(RTLIL::State::S0, 1);
788 read_port rp(name, clk_enable, clk_parity, transparency, clk_sig, ena_sig, addr_sig);
789 m.add_memory_read_port(rp);
790 cell_exprs.push_back(rp.gen_read(indent.c_str()));
791 register_reverse_wire_map(stringf("%s.data", name.c_str()), data_sig);
792 }
793
794 for (int i = 0; i < wr_ports; i++)
795 {
796 if (wr_clk_enable[i] != State::S1)
797 log_error("Unclocked write port %d on memory %s.%s.\n", i, log_id(module), log_id(cell));
798
799 if (wr_clk_polarity[i] != State::S1)
800 log_error("Negedge write port %d on memory %s.%s.\n", i, log_id(module), log_id(cell));
801
802 string name(stringf("%s.w%d", m.name.c_str(), i));
803 bool clk_enable = true;
804 bool clk_parity = true;
805 bool transparency = false;
806 SigSpec addr_sig =cell->getPort(ID::WR_ADDR).extract(i*abits, abits);
807 string addr_expr = make_expr(addr_sig);
808 SigSpec data_sig =cell->getPort(ID::WR_DATA).extract(i*width, width);
809 string data_expr = make_expr(data_sig);
810 SigSpec clk_sig = cell->getPort(ID::WR_CLK).extract(i);
811 string clk_expr = make_expr(clk_sig);
812
813 SigSpec wen_sig = cell->getPort(ID::WR_EN).extract(i*width, width);
814 string wen_expr = make_expr(wen_sig[0]);
815
816 for (int i = 1; i < GetSize(wen_sig); i++)
817 if (wen_sig[0] != wen_sig[i])
818 log_error("Complex write enable on port %d on memory %s.%s.\n", i, log_id(module), log_id(cell));
819
820 SigSpec mask_sig = RTLIL::SigSpec(RTLIL::State::S1, 1);
821 write_port wp(name, clk_enable, clk_parity, transparency, clk_sig, wen_sig[0], addr_sig, mask_sig);
822 m.add_memory_write_port(wp);
823 cell_exprs.push_back(stringf("%s%s.data <= %s\n", indent.c_str(), name.c_str(), data_expr.c_str()));
824 cell_exprs.push_back(wp.gen_write(indent.c_str()));
825 }
826 register_memory(m);
827 continue;
828 }
829
830 if (cell->type.in(ID($memwr), ID($memrd), ID($meminit)))
831 {
832 std::string cell_type = fid(cell->type);
833 std::string mem_id = make_id(cell->parameters[ID::MEMID].decode_string());
834 int abits = cell->parameters.at(ID::ABITS).as_int();
835 int width = cell->parameters.at(ID::WIDTH).as_int();
836 memory *mp = nullptr;
837 if (cell->type == ID($meminit) ) {
838 log_error("$meminit (%s.%s.%s) currently unsupported\n", log_id(module), log_id(cell), mem_id.c_str());
839 } else {
840 // It's a $memwr or $memrd. Remember the read/write port parameters for the eventual FIRRTL memory definition.
841 auto addrSig = cell->getPort(ID::ADDR);
842 auto dataSig = cell->getPort(ID::DATA);
843 auto enableSig = cell->getPort(ID::EN);
844 auto clockSig = cell->getPort(ID::CLK);
845 Const clk_enable = cell->parameters.at(ID::CLK_ENABLE);
846 Const clk_polarity = cell->parameters.at(ID::CLK_POLARITY);
847
848 // Do we already have an entry for this memory?
849 if (memories.count(mem_id) == 0) {
850 memory m(cell, mem_id, abits, 0, width);
851 register_memory(m);
852 }
853 mp = &memories.at(mem_id);
854 int portNum = 0;
855 bool transparency = false;
856 string data_expr = make_expr(dataSig);
857 if (cell->type.in(ID($memwr))) {
858 portNum = (int) mp->write_ports.size();
859 write_port wp(stringf("%s.w%d", mem_id.c_str(), portNum), clk_enable.as_bool(), clk_polarity.as_bool(), transparency, clockSig, enableSig, addrSig, dataSig);
860 mp->add_memory_write_port(wp);
861 cell_exprs.push_back(stringf("%s%s.data <= %s\n", indent.c_str(), wp.name.c_str(), data_expr.c_str()));
862 cell_exprs.push_back(wp.gen_write(indent.c_str()));
863 } else if (cell->type.in(ID($memrd))) {
864 portNum = (int) mp->read_ports.size();
865 read_port rp(stringf("%s.r%d", mem_id.c_str(), portNum), clk_enable.as_bool(), clk_polarity.as_bool(), transparency, clockSig, enableSig, addrSig);
866 mp->add_memory_read_port(rp);
867 cell_exprs.push_back(rp.gen_read(indent.c_str()));
868 register_reverse_wire_map(stringf("%s.data", rp.name.c_str()), dataSig);
869 }
870 }
871 continue;
872 }
873
874 if (cell->type.in(ID($dff)))
875 {
876 bool clkpol = cell->parameters.at(ID::CLK_POLARITY).as_bool();
877 if (clkpol == false)
878 log_error("Negative edge clock on FF %s.%s.\n", log_id(module), log_id(cell));
879
880 int width = cell->parameters.at(ID::WIDTH).as_int();
881 string expr = make_expr(cell->getPort(ID::D));
882 string clk_expr = "asClock(" + make_expr(cell->getPort(ID::CLK)) + ")";
883
884 wire_decls.push_back(stringf(" reg %s: UInt<%d>, %s %s\n", y_id.c_str(), width, clk_expr.c_str(), cellFileinfo.c_str()));
885
886 cell_exprs.push_back(stringf(" %s <= %s %s\n", y_id.c_str(), expr.c_str(), cellFileinfo.c_str()));
887 register_reverse_wire_map(y_id, cell->getPort(ID::Q));
888
889 continue;
890 }
891
892 // This may be a parameterized module - paramod.
893 if (cell->type.begins_with("$paramod"))
894 {
895 process_instance(cell, wire_exprs);
896 continue;
897 }
898 if (cell->type == ID($shiftx)) {
899 // assign y = a[b +: y_width];
900 // We'll extract the correct bits as part of the primop.
901
902 string a_expr = make_expr(cell->getPort(ID::A));
903 // Get the initial bit selector
904 string b_expr = make_expr(cell->getPort(ID::B));
905 wire_decls.push_back(stringf(" wire %s: UInt<%d>\n", y_id.c_str(), y_width));
906
907 if (cell->getParam(ID::B_SIGNED).as_bool()) {
908 // Use validif to constrain the selection (test the sign bit)
909 auto b_string = b_expr.c_str();
910 int b_sign = cell->parameters.at(ID::B_WIDTH).as_int() - 1;
911 b_expr = stringf("validif(not(bits(%s, %d, %d)), %s)", b_string, b_sign, b_sign, b_string);
912 }
913 string expr = stringf("dshr(%s, %s)", a_expr.c_str(), b_expr.c_str());
914
915 cell_exprs.push_back(stringf(" %s <= %s\n", y_id.c_str(), expr.c_str()));
916 register_reverse_wire_map(y_id, cell->getPort(ID::Y));
917 continue;
918 }
919 if (cell->type == ID($shift)) {
920 // assign y = a >> b;
921 // where b may be negative
922
923 string a_expr = make_expr(cell->getPort(ID::A));
924 string b_expr = make_expr(cell->getPort(ID::B));
925 auto b_string = b_expr.c_str();
926 string expr;
927 wire_decls.push_back(stringf(" wire %s: UInt<%d>\n", y_id.c_str(), y_width));
928
929 if (cell->getParam(ID::B_SIGNED).as_bool()) {
930 // We generate a left or right shift based on the sign of b.
931 std::string dshl = stringf("bits(dshl(%s, %s), 0, %d)", a_expr.c_str(), gen_dshl(b_expr, b_width).c_str(), y_width);
932 std::string dshr = stringf("dshr(%s, %s)", a_expr.c_str(), b_string);
933 expr = stringf("mux(%s < 0, %s, %s)",
934 b_string,
935 dshl.c_str(),
936 dshr.c_str()
937 );
938 } else {
939 expr = stringf("dshr(%s, %s)", a_expr.c_str(), b_string);
940 }
941 cell_exprs.push_back(stringf(" %s <= %s\n", y_id.c_str(), expr.c_str()));
942 register_reverse_wire_map(y_id, cell->getPort(ID::Y));
943 continue;
944 }
945 if (cell->type == ID($pos)) {
946 // assign y = a;
947 // printCell(cell);
948 string a_expr = make_expr(cell->getPort(ID::A));
949 // Verilog appears to treat the result as signed, so if the result is wider than "A",
950 // we need to pad.
951 if (a_width < y_width) {
952 a_expr = stringf("pad(%s, %d)", a_expr.c_str(), y_width);
953 }
954 wire_decls.push_back(stringf(" wire %s: UInt<%d>\n", y_id.c_str(), y_width));
955 cell_exprs.push_back(stringf(" %s <= %s\n", y_id.c_str(), a_expr.c_str()));
956 register_reverse_wire_map(y_id, cell->getPort(ID::Y));
957 continue;
958 }
959 log_error("Cell type not supported: %s (%s.%s)\n", log_id(cell->type), log_id(module), log_id(cell));
960 }
961
962 for (auto conn : module->connections())
963 {
964 string y_id = next_id();
965 int y_width = GetSize(conn.first);
966 string expr = make_expr(conn.second);
967
968 wire_decls.push_back(stringf(" wire %s: UInt<%d>\n", y_id.c_str(), y_width));
969 cell_exprs.push_back(stringf(" %s <= %s\n", y_id.c_str(), expr.c_str()));
970 register_reverse_wire_map(y_id, conn.first);
971 }
972
973 for (auto wire : module->wires())
974 {
975 string expr;
976 std::string wireFileinfo = getFileinfo(wire);
977
978 if (wire->port_input)
979 continue;
980
981 int cursor = 0;
982 bool is_valid = false;
983 bool make_unconn_id = false;
984
985 while (cursor < wire->width)
986 {
987 int chunk_width = 1;
988 string new_expr;
989
990 SigBit start_bit(wire, cursor);
991
992 if (reverse_wire_map.count(start_bit))
993 {
994 pair<string, int> start_map = reverse_wire_map.at(start_bit);
995
996 while (cursor+chunk_width < wire->width)
997 {
998 SigBit stop_bit(wire, cursor+chunk_width);
999
1000 if (reverse_wire_map.count(stop_bit) == 0)
1001 break;
1002
1003 pair<string, int> stop_map = reverse_wire_map.at(stop_bit);
1004 stop_map.second -= chunk_width;
1005
1006 if (start_map != stop_map)
1007 break;
1008
1009 chunk_width++;
1010 }
1011
1012 new_expr = stringf("bits(%s, %d, %d)", start_map.first.c_str(),
1013 start_map.second + chunk_width - 1, start_map.second);
1014 is_valid = true;
1015 }
1016 else
1017 {
1018 if (unconn_id.empty()) {
1019 unconn_id = next_id();
1020 make_unconn_id = true;
1021 }
1022 new_expr = unconn_id;
1023 }
1024
1025 if (expr.empty())
1026 expr = new_expr;
1027 else
1028 expr = "cat(" + new_expr + ", " + expr + ")";
1029
1030 cursor += chunk_width;
1031 }
1032
1033 if (is_valid) {
1034 if (make_unconn_id) {
1035 wire_decls.push_back(stringf(" wire %s: UInt<1> %s\n", unconn_id.c_str(), wireFileinfo.c_str()));
1036 // `invalid` is a firrtl construction for simulation so we will not
1037 // tag it with a @[fileinfo] tag as it doesn't directly correspond to
1038 // a specific line of verilog code.
1039 wire_decls.push_back(stringf(" %s is invalid\n", unconn_id.c_str()));
1040 }
1041 wire_exprs.push_back(stringf(" %s <= %s %s\n", make_id(wire->name), expr.c_str(), wireFileinfo.c_str()));
1042 } else {
1043 if (make_unconn_id) {
1044 unconn_id.clear();
1045 }
1046 // `invalid` is a firrtl construction for simulation so we will not
1047 // tag it with a @[fileinfo] tag as it doesn't directly correspond to
1048 // a specific line of verilog code.
1049 wire_decls.push_back(stringf(" %s is invalid\n", make_id(wire->name)));
1050 }
1051 }
1052
1053 for (auto str : port_decls)
1054 f << str;
1055
1056 f << stringf("\n");
1057
1058 for (auto str : wire_decls)
1059 f << str;
1060
1061 f << stringf("\n");
1062
1063 // If we have any memory definitions, output them.
1064 for (auto kv : memories) {
1065 memory &m = kv.second;
1066 f << stringf(" mem %s:\n", m.name.c_str());
1067 f << stringf(" data-type => UInt<%d>\n", m.width);
1068 f << stringf(" depth => %d\n", m.size);
1069 for (int i = 0; i < (int) m.read_ports.size(); i += 1) {
1070 f << stringf(" reader => r%d\n", i);
1071 }
1072 for (int i = 0; i < (int) m.write_ports.size(); i += 1) {
1073 f << stringf(" writer => w%d\n", i);
1074 }
1075 f << stringf(" read-latency => %d\n", m.read_latency);
1076 f << stringf(" write-latency => %d\n", m.write_latency);
1077 f << stringf(" read-under-write => undefined\n");
1078 }
1079 f << stringf("\n");
1080
1081 for (auto str : cell_exprs)
1082 f << str;
1083
1084 f << stringf("\n");
1085
1086 for (auto str : wire_exprs)
1087 f << str;
1088 }
1089 };
1090
1091 struct FirrtlBackend : public Backend {
1092 FirrtlBackend() : Backend("firrtl", "write design to a FIRRTL file") { }
1093 void help() YS_OVERRIDE
1094 {
1095 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
1096 log("\n");
1097 log(" write_firrtl [options] [filename]\n");
1098 log("\n");
1099 log("Write a FIRRTL netlist of the current design.\n");
1100 log("The following commands are executed by this command:\n");
1101 log(" pmuxtree\n");
1102 log("\n");
1103 }
1104 void execute(std::ostream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
1105 {
1106 size_t argidx = args.size(); // We aren't expecting any arguments.
1107
1108 // If we weren't explicitly passed a filename, use the last argument (if it isn't a flag).
1109 if (filename == "") {
1110 if (argidx > 0 && args[argidx - 1][0] != '-') {
1111 // extra_args and friends need to see this argument.
1112 argidx -= 1;
1113 filename = args[argidx];
1114 }
1115 }
1116 extra_args(f, filename, args, argidx);
1117
1118 if (!design->full_selection())
1119 log_cmd_error("This command only operates on fully selected designs!\n");
1120
1121 log_header(design, "Executing FIRRTL backend.\n");
1122 log_push();
1123
1124 Pass::call(design, stringf("pmuxtree"));
1125
1126 namecache.clear();
1127 autoid_counter = 0;
1128
1129 // Get the top module, or a reasonable facsimile - we need something for the circuit name.
1130 Module *top = design->top_module();
1131 Module *last = nullptr;
1132 // Generate module and wire names.
1133 for (auto module : design->modules()) {
1134 make_id(module->name);
1135 last = module;
1136 if (top == nullptr && module->get_bool_attribute(ID::top)) {
1137 top = module;
1138 }
1139 for (auto wire : module->wires())
1140 if (wire->port_id)
1141 make_id(wire->name);
1142 }
1143
1144 if (top == nullptr)
1145 top = last;
1146
1147 std::string circuitFileinfo = getFileinfo(top);
1148 *f << stringf("circuit %s: %s\n", make_id(top->name), circuitFileinfo.c_str());
1149
1150 for (auto module : design->modules())
1151 {
1152 FirrtlWorker worker(module, *f, design);
1153 worker.run();
1154 }
1155
1156 namecache.clear();
1157 autoid_counter = 0;
1158 }
1159 } FirrtlBackend;
1160
1161 PRIVATE_NAMESPACE_END