Merge pull request #2018 from boqwxp/qbfsat-timeout
[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 log_assert(cell->has_attribute(ID::hdlname));
310 instanceOf = cell->get_string_attribute(ID::hdlname);
311 }
312 else
313 {
314 instanceOf = cell_type;
315 }
316
317 std::string cell_name = cellname(cell);
318 std::string cell_name_comment;
319 if (cell_name != fid(cell->name))
320 cell_name_comment = " /* " + fid(cell->name) + " */ ";
321 else
322 cell_name_comment = "";
323 // Find the module corresponding to this instance.
324 auto instModule = design->module(cell->type);
325 // If there is no instance for this, just return.
326 if (instModule == NULL)
327 {
328 log_warning("No instance for %s.%s\n", cell_type.c_str(), cell_name.c_str());
329 return;
330 }
331 std::string cellFileinfo = getFileinfo(cell);
332 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()));
333
334 for (auto it = cell->connections().begin(); it != cell->connections().end(); ++it) {
335 if (it->second.size() > 0) {
336 const SigSpec &secondSig = it->second;
337 const std::string firstName = cell_name + "." + make_id(it->first);
338 const std::string secondExpr = make_expr(secondSig);
339 // Find the direction for this port.
340 FDirection dir = getPortFDirection(it->first, instModule);
341 std::string sourceExpr, sinkExpr;
342 const SigSpec *sinkSig = nullptr;
343 switch (dir) {
344 case FD_INOUT:
345 log_warning("Instance port connection %s.%s is INOUT; treating as OUT\n", cell_type.c_str(), log_signal(it->second));
346 YS_FALLTHROUGH
347 case FD_OUT:
348 sourceExpr = firstName;
349 sinkExpr = secondExpr;
350 sinkSig = &secondSig;
351 break;
352 case FD_NODIRECTION:
353 log_warning("Instance port connection %s.%s is NODIRECTION; treating as IN\n", cell_type.c_str(), log_signal(it->second));
354 YS_FALLTHROUGH
355 case FD_IN:
356 sourceExpr = secondExpr;
357 sinkExpr = firstName;
358 break;
359 default:
360 log_error("Instance port %s.%s unrecognized connection direction 0x%x !\n", cell_type.c_str(), log_signal(it->second), dir);
361 break;
362 }
363 // Check for subfield assignment.
364 std::string bitsString = "bits(";
365 if (sinkExpr.compare(0, bitsString.length(), bitsString) == 0) {
366 if (sinkSig == nullptr)
367 log_error("Unknown subfield %s.%s\n", cell_type.c_str(), sinkExpr.c_str());
368 // Don't generate the assignment here.
369 // Add the source and sink to the "reverse_wire_map" and we'll output the assignment
370 // as part of the coalesced subfield assignments for this wire.
371 register_reverse_wire_map(sourceExpr, *sinkSig);
372 } else {
373 wire_exprs.push_back(stringf("\n%s%s <= %s %s", indent.c_str(), sinkExpr.c_str(), sourceExpr.c_str(), cellFileinfo.c_str()));
374 }
375 }
376 }
377 wire_exprs.push_back(stringf("\n"));
378
379 }
380
381 // Given an expression for a shift amount, and a maximum width,
382 // generate the FIRRTL expression for equivalent dynamic shift taking into account FIRRTL shift semantics.
383 std::string gen_dshl(const string b_expr, const int b_width)
384 {
385 string result = b_expr;
386 if (b_width >= FIRRTL_MAX_DSH_WIDTH_ERROR) {
387 int max_shift_width_bits = FIRRTL_MAX_DSH_WIDTH_ERROR - 1;
388 string max_shift_string = stringf("UInt<%d>(%d)", max_shift_width_bits, (1<<max_shift_width_bits) - 1);
389 // Deal with the difference in semantics between FIRRTL and verilog
390 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);
391 }
392 return result;
393 }
394
395 void emit_extmodule()
396 {
397 std::string moduleFileinfo = getFileinfo(module);
398 f << stringf(" extmodule %s: %s\n", make_id(module->name), moduleFileinfo.c_str());
399 vector<std::string> port_decls;
400
401 for (auto wire : module->wires())
402 {
403 const auto wireName = make_id(wire->name);
404 std::string wireFileinfo = getFileinfo(wire);
405
406 if (wire->port_input && wire->port_output)
407 {
408 log_error("Module port %s.%s is inout!\n", log_id(module), log_id(wire));
409 }
410 port_decls.push_back(stringf(" %s %s: UInt<%d> %s\n", wire->port_input ? "input" : "output",
411 wireName, wire->width, wireFileinfo.c_str()));
412 }
413
414 for (auto &str : port_decls)
415 {
416 f << str;
417 }
418
419 f << stringf("\n");
420 }
421
422 void emit_module()
423 {
424 std::string moduleFileinfo = getFileinfo(module);
425 f << stringf(" module %s: %s\n", make_id(module->name), moduleFileinfo.c_str());
426 vector<string> port_decls, wire_decls, cell_exprs, wire_exprs;
427
428 for (auto wire : module->wires())
429 {
430 const auto wireName = make_id(wire->name);
431 std::string wireFileinfo = getFileinfo(wire);
432
433 // If a wire has initial data, issue a warning since FIRRTL doesn't currently support it.
434 if (wire->attributes.count(ID::init)) {
435 log_warning("Initial value (%s) for (%s.%s) not supported\n",
436 wire->attributes.at(ID::init).as_string().c_str(),
437 log_id(module), log_id(wire));
438 }
439 if (wire->port_id)
440 {
441 if (wire->port_input && wire->port_output)
442 log_error("Module port %s.%s is inout!\n", log_id(module), log_id(wire));
443 port_decls.push_back(stringf(" %s %s: UInt<%d> %s\n", wire->port_input ? "input" : "output",
444 wireName, wire->width, wireFileinfo.c_str()));
445 }
446 else
447 {
448 wire_decls.push_back(stringf(" wire %s: UInt<%d> %s\n", wireName, wire->width, wireFileinfo.c_str()));
449 }
450 }
451
452 for (auto cell : module->cells())
453 {
454 static Const ndef(0, 0);
455
456 // Is this cell is a module instance?
457 if (cell->type[0] != '$')
458 {
459 process_instance(cell, wire_exprs);
460 continue;
461 }
462 // Not a module instance. Set up cell properties
463 bool extract_y_bits = false; // Assume no extraction of final bits will be required.
464 int a_width = cell->parameters.at(ID::A_WIDTH, ndef).as_int(); // The width of "A"
465 int b_width = cell->parameters.at(ID::B_WIDTH, ndef).as_int(); // The width of "A"
466 const int y_width = cell->parameters.at(ID::Y_WIDTH, ndef).as_int(); // The width of the result
467 const bool a_signed = cell->parameters.at(ID::A_SIGNED, ndef).as_bool();
468 const bool b_signed = cell->parameters.at(ID::B_SIGNED, ndef).as_bool();
469 bool firrtl_is_signed = a_signed; // The result is signed (subsequent code may change this).
470 int firrtl_width = 0;
471 string primop;
472 bool always_uint = false;
473 string y_id = make_id(cell->name);
474 std::string cellFileinfo = getFileinfo(cell);
475
476 if (cell->type.in(ID($not), ID($logic_not), ID($_NOT_), ID($neg), ID($reduce_and), ID($reduce_or), ID($reduce_xor), ID($reduce_bool), ID($reduce_xnor)))
477 {
478 string a_expr = make_expr(cell->getPort(ID::A));
479 wire_decls.push_back(stringf(" wire %s: UInt<%d> %s\n", y_id.c_str(), y_width, cellFileinfo.c_str()));
480
481 if (a_signed) {
482 a_expr = "asSInt(" + a_expr + ")";
483 }
484
485 // Don't use the results of logical operations (a single bit) to control padding
486 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) ) {
487 a_expr = stringf("pad(%s, %d)", a_expr.c_str(), y_width);
488 }
489
490 // Assume the FIRRTL width is a single bit.
491 firrtl_width = 1;
492 if (cell->type.in(ID($not), ID($_NOT_))) primop = "not";
493 else if (cell->type == ID($neg)) {
494 primop = "neg";
495 firrtl_is_signed = true; // Result of "neg" is signed (an SInt).
496 firrtl_width = a_width;
497 } else if (cell->type == ID($logic_not)) {
498 primop = "eq";
499 a_expr = stringf("%s, UInt(0)", a_expr.c_str());
500 }
501 else if (cell->type == ID($reduce_and)) primop = "andr";
502 else if (cell->type == ID($reduce_or)) primop = "orr";
503 else if (cell->type == ID($reduce_xor)) primop = "xorr";
504 else if (cell->type == ID($reduce_xnor)) {
505 primop = "not";
506 a_expr = stringf("xorr(%s)", a_expr.c_str());
507 }
508 else if (cell->type == ID($reduce_bool)) {
509 primop = "neq";
510 // Use the sign of the a_expr and its width as the type (UInt/SInt) and width of the comparand.
511 a_expr = stringf("%s, %cInt<%d>(0)", a_expr.c_str(), a_signed ? 'S' : 'U', a_width);
512 }
513
514 string expr = stringf("%s(%s)", primop.c_str(), a_expr.c_str());
515
516 if ((firrtl_is_signed && !always_uint))
517 expr = stringf("asUInt(%s)", expr.c_str());
518
519 cell_exprs.push_back(stringf(" %s <= %s %s\n", y_id.c_str(), expr.c_str(), cellFileinfo.c_str()));
520 register_reverse_wire_map(y_id, cell->getPort(ID::Y));
521
522 continue;
523 }
524 if (cell->type.in(ID($add), ID($sub), ID($mul), ID($div), ID($mod), ID($xor), ID($_XOR_), ID($xnor), ID($and), ID($_AND_), ID($or), ID($_OR_), ID($eq), ID($eqx),
525 ID($gt), ID($ge), ID($lt), ID($le), ID($ne), ID($nex), ID($shr), ID($sshr), ID($sshl), ID($shl),
526 ID($logic_and), ID($logic_or), ID($pow)))
527 {
528 string a_expr = make_expr(cell->getPort(ID::A));
529 string b_expr = make_expr(cell->getPort(ID::B));
530 std::string cellFileinfo = getFileinfo(cell);
531 wire_decls.push_back(stringf(" wire %s: UInt<%d> %s\n", y_id.c_str(), y_width, cellFileinfo.c_str()));
532
533 if (a_signed) {
534 a_expr = "asSInt(" + a_expr + ")";
535 // Expand the "A" operand to the result width
536 if (a_width < y_width) {
537 a_expr = stringf("pad(%s, %d)", a_expr.c_str(), y_width);
538 a_width = y_width;
539 }
540 }
541 // Shift amount is always unsigned, and needn't be padded to result width,
542 // otherwise, we need to cast the b_expr appropriately
543 if (b_signed && !cell->type.in(ID($shr), ID($sshr), ID($shl), ID($sshl), ID($pow))) {
544 b_expr = "asSInt(" + b_expr + ")";
545 // Expand the "B" operand to the result width
546 if (b_width < y_width) {
547 b_expr = stringf("pad(%s, %d)", b_expr.c_str(), y_width);
548 b_width = y_width;
549 }
550 }
551
552 // For the arithmetic ops, expand operand widths to result widths befor performing the operation.
553 // This corresponds (according to iverilog) to what verilog compilers implement.
554 if (cell->type.in(ID($add), ID($sub), ID($mul), ID($div), ID($mod), ID($xor), ID($_XOR_), ID($xnor), ID($and), ID($_AND_), ID($or), ID($_OR_)))
555 {
556 if (a_width < y_width) {
557 a_expr = stringf("pad(%s, %d)", a_expr.c_str(), y_width);
558 a_width = y_width;
559 }
560 if (b_width < y_width) {
561 b_expr = stringf("pad(%s, %d)", b_expr.c_str(), y_width);
562 b_width = y_width;
563 }
564 }
565 // Assume the FIRRTL width is the width of "A"
566 firrtl_width = a_width;
567 auto a_sig = cell->getPort(ID::A);
568
569 if (cell->type == ID($add)) {
570 primop = "add";
571 firrtl_is_signed = a_signed | b_signed;
572 firrtl_width = max(a_width, b_width);
573 } else if (cell->type == ID($sub)) {
574 primop = "sub";
575 firrtl_is_signed = true;
576 int a_widthInc = (!a_signed && b_signed) ? 2 : (a_signed && !b_signed) ? 1 : 0;
577 int b_widthInc = (a_signed && !b_signed) ? 2 : (!a_signed && b_signed) ? 1 : 0;
578 firrtl_width = max(a_width + a_widthInc, b_width + b_widthInc);
579 } else if (cell->type == ID($mul)) {
580 primop = "mul";
581 firrtl_is_signed = a_signed | b_signed;
582 firrtl_width = a_width + b_width;
583 } else if (cell->type == ID($div)) {
584 primop = "div";
585 firrtl_is_signed = a_signed | b_signed;
586 firrtl_width = a_width;
587 } else if (cell->type == ID($mod)) {
588 // "rem" = truncating modulo
589 primop = "rem";
590 firrtl_width = min(a_width, b_width);
591 } else if (cell->type.in(ID($and), ID($_AND_))) {
592 primop = "and";
593 always_uint = true;
594 firrtl_width = max(a_width, b_width);
595 }
596 else if (cell->type.in(ID($or), ID($_OR_))) {
597 primop = "or";
598 always_uint = true;
599 firrtl_width = max(a_width, b_width);
600 }
601 else if (cell->type.in(ID($xor), ID($_XOR_))) {
602 primop = "xor";
603 always_uint = true;
604 firrtl_width = max(a_width, b_width);
605 }
606 else if (cell->type == ID($xnor)) {
607 primop = "xnor";
608 always_uint = true;
609 firrtl_width = max(a_width, b_width);
610 }
611 else if ((cell->type == ID($eq)) | (cell->type == ID($eqx))) {
612 primop = "eq";
613 always_uint = true;
614 firrtl_width = 1;
615 }
616 else if ((cell->type == ID($ne)) | (cell->type == ID($nex))) {
617 primop = "neq";
618 always_uint = true;
619 firrtl_width = 1;
620 }
621 else if (cell->type == ID($gt)) {
622 primop = "gt";
623 always_uint = true;
624 firrtl_width = 1;
625 }
626 else if (cell->type == ID($ge)) {
627 primop = "geq";
628 always_uint = true;
629 firrtl_width = 1;
630 }
631 else if (cell->type == ID($lt)) {
632 primop = "lt";
633 always_uint = true;
634 firrtl_width = 1;
635 }
636 else if (cell->type == ID($le)) {
637 primop = "leq";
638 always_uint = true;
639 firrtl_width = 1;
640 }
641 else if ((cell->type == ID($shl)) | (cell->type == ID($sshl))) {
642 // FIRRTL will widen the result (y) by the amount of the shift.
643 // We'll need to offset this by extracting the un-widened portion as Verilog would do.
644 extract_y_bits = true;
645 // Is the shift amount constant?
646 auto b_sig = cell->getPort(ID::B);
647 if (b_sig.is_fully_const()) {
648 primop = "shl";
649 int shift_amount = b_sig.as_int();
650 b_expr = std::to_string(shift_amount);
651 firrtl_width = a_width + shift_amount;
652 } else {
653 primop = "dshl";
654 // Convert from FIRRTL left shift semantics.
655 b_expr = gen_dshl(b_expr, b_width);
656 firrtl_width = a_width + (1 << b_width) - 1;
657 }
658 }
659 else if ((cell->type == ID($shr)) | (cell->type == ID($sshr))) {
660 // We don't need to extract a specific range of bits.
661 extract_y_bits = false;
662 // Is the shift amount constant?
663 auto b_sig = cell->getPort(ID::B);
664 if (b_sig.is_fully_const()) {
665 primop = "shr";
666 int shift_amount = b_sig.as_int();
667 b_expr = std::to_string(shift_amount);
668 firrtl_width = max(1, a_width - shift_amount);
669 } else {
670 primop = "dshr";
671 firrtl_width = a_width;
672 }
673 // We'll need to do some special fixups if the source (and thus result) is signed.
674 if (firrtl_is_signed) {
675 // If this is a "logical" shift right, pretend the source is unsigned.
676 if (cell->type == ID($shr)) {
677 a_expr = "asUInt(" + a_expr + ")";
678 }
679 }
680 }
681 else if ((cell->type == ID($logic_and))) {
682 primop = "and";
683 a_expr = "neq(" + a_expr + ", UInt(0))";
684 b_expr = "neq(" + b_expr + ", UInt(0))";
685 always_uint = true;
686 firrtl_width = 1;
687 }
688 else if ((cell->type == ID($logic_or))) {
689 primop = "or";
690 a_expr = "neq(" + a_expr + ", UInt(0))";
691 b_expr = "neq(" + b_expr + ", UInt(0))";
692 always_uint = true;
693 firrtl_width = 1;
694 }
695 else if ((cell->type == ID($pow))) {
696 if (a_sig.is_fully_const() && a_sig.as_int() == 2) {
697 // We'll convert this to a shift. To simplify things, change the a_expr to "1"
698 // so we can use b_expr directly as a shift amount.
699 // Only support 2 ** N (i.e., shift left)
700 // FIRRTL will widen the result (y) by the amount of the shift.
701 // We'll need to offset this by extracting the un-widened portion as Verilog would do.
702 a_expr = firrtl_is_signed ? "SInt(1)" : "UInt(1)";
703 extract_y_bits = true;
704 // Is the shift amount constant?
705 auto b_sig = cell->getPort(ID::B);
706 if (b_sig.is_fully_const()) {
707 primop = "shl";
708 int shiftAmount = b_sig.as_int();
709 if (shiftAmount < 0) {
710 log_error("Negative power exponent - %d: %s.%s\n", shiftAmount, log_id(module), log_id(cell));
711 }
712 b_expr = std::to_string(shiftAmount);
713 firrtl_width = a_width + shiftAmount;
714 } else {
715 primop = "dshl";
716 // Convert from FIRRTL left shift semantics.
717 b_expr = gen_dshl(b_expr, b_width);
718 firrtl_width = a_width + (1 << b_width) - 1;
719 }
720 } else {
721 log_error("Non power 2: %s.%s\n", log_id(module), log_id(cell));
722 }
723 }
724
725 auto it = cell->parameters.find(ID::B_SIGNED);
726 if (it == cell->parameters.end() || !it->second.as_bool()) {
727 b_expr = "asUInt(" + b_expr + ")";
728 }
729
730 string expr;
731 // Deal with $xnor == ~^ (not xor)
732 if (primop == "xnor") {
733 expr = stringf("not(xor(%s, %s))", a_expr.c_str(), b_expr.c_str());
734 } else {
735 expr = stringf("%s(%s, %s)", primop.c_str(), a_expr.c_str(), b_expr.c_str());
736 }
737
738 // Deal with FIRRTL's "shift widens" semantics, or the need to widen the FIRRTL result.
739 // If the operation is signed, the FIRRTL width will be 1 one bit larger.
740 if (extract_y_bits) {
741 expr = stringf("bits(%s, %d, 0)", expr.c_str(), y_width - 1);
742 } else if (firrtl_is_signed && (firrtl_width + 1) < y_width) {
743 expr = stringf("pad(%s, %d)", expr.c_str(), y_width);
744 }
745
746 if ((firrtl_is_signed && !always_uint))
747 expr = stringf("asUInt(%s)", expr.c_str());
748
749 cell_exprs.push_back(stringf(" %s <= %s %s\n", y_id.c_str(), expr.c_str(), cellFileinfo.c_str()));
750 register_reverse_wire_map(y_id, cell->getPort(ID::Y));
751
752 continue;
753 }
754
755 if (cell->type.in(ID($mux), ID($_MUX_)))
756 {
757 auto it = cell->parameters.find(ID::WIDTH);
758 int width = it == cell->parameters.end()? 1 : it->second.as_int();
759 string a_expr = make_expr(cell->getPort(ID::A));
760 string b_expr = make_expr(cell->getPort(ID::B));
761 string s_expr = make_expr(cell->getPort(ID::S));
762 wire_decls.push_back(stringf(" wire %s: UInt<%d> %s\n", y_id.c_str(), width, cellFileinfo.c_str()));
763
764 string expr = stringf("mux(%s, %s, %s)", s_expr.c_str(), b_expr.c_str(), a_expr.c_str());
765
766 cell_exprs.push_back(stringf(" %s <= %s %s\n", y_id.c_str(), expr.c_str(), cellFileinfo.c_str()));
767 register_reverse_wire_map(y_id, cell->getPort(ID::Y));
768
769 continue;
770 }
771
772 if (cell->type.in(ID($mem)))
773 {
774 string mem_id = make_id(cell->name);
775 int abits = cell->parameters.at(ID::ABITS).as_int();
776 int width = cell->parameters.at(ID::WIDTH).as_int();
777 int size = cell->parameters.at(ID::SIZE).as_int();
778 memory m(cell, mem_id, abits, size, width);
779 int rd_ports = cell->parameters.at(ID::RD_PORTS).as_int();
780 int wr_ports = cell->parameters.at(ID::WR_PORTS).as_int();
781
782 Const initdata = cell->parameters.at(ID::INIT);
783 for (State bit : initdata.bits)
784 if (bit != State::Sx)
785 log_error("Memory with initialization data: %s.%s\n", log_id(module), log_id(cell));
786
787 Const rd_clk_enable = cell->parameters.at(ID::RD_CLK_ENABLE);
788 Const wr_clk_enable = cell->parameters.at(ID::WR_CLK_ENABLE);
789 Const wr_clk_polarity = cell->parameters.at(ID::WR_CLK_POLARITY);
790
791 int offset = cell->parameters.at(ID::OFFSET).as_int();
792 if (offset != 0)
793 log_error("Memory with nonzero offset: %s.%s\n", log_id(module), log_id(cell));
794
795 for (int i = 0; i < rd_ports; i++)
796 {
797 if (rd_clk_enable[i] != State::S0)
798 log_error("Clocked read port %d on memory %s.%s.\n", i, log_id(module), log_id(cell));
799
800 SigSpec addr_sig = cell->getPort(ID::RD_ADDR).extract(i*abits, abits);
801 SigSpec data_sig = cell->getPort(ID::RD_DATA).extract(i*width, width);
802 string addr_expr = make_expr(addr_sig);
803 string name(stringf("%s.r%d", m.name.c_str(), i));
804 bool clk_enable = false;
805 bool clk_parity = true;
806 bool transparency = false;
807 SigSpec ena_sig = RTLIL::SigSpec(RTLIL::State::S1, 1);
808 SigSpec clk_sig = RTLIL::SigSpec(RTLIL::State::S0, 1);
809 read_port rp(name, clk_enable, clk_parity, transparency, clk_sig, ena_sig, addr_sig);
810 m.add_memory_read_port(rp);
811 cell_exprs.push_back(rp.gen_read(indent.c_str()));
812 register_reverse_wire_map(stringf("%s.data", name.c_str()), data_sig);
813 }
814
815 for (int i = 0; i < wr_ports; i++)
816 {
817 if (wr_clk_enable[i] != State::S1)
818 log_error("Unclocked write port %d on memory %s.%s.\n", i, log_id(module), log_id(cell));
819
820 if (wr_clk_polarity[i] != State::S1)
821 log_error("Negedge write port %d on memory %s.%s.\n", i, log_id(module), log_id(cell));
822
823 string name(stringf("%s.w%d", m.name.c_str(), i));
824 bool clk_enable = true;
825 bool clk_parity = true;
826 bool transparency = false;
827 SigSpec addr_sig =cell->getPort(ID::WR_ADDR).extract(i*abits, abits);
828 string addr_expr = make_expr(addr_sig);
829 SigSpec data_sig =cell->getPort(ID::WR_DATA).extract(i*width, width);
830 string data_expr = make_expr(data_sig);
831 SigSpec clk_sig = cell->getPort(ID::WR_CLK).extract(i);
832 string clk_expr = make_expr(clk_sig);
833
834 SigSpec wen_sig = cell->getPort(ID::WR_EN).extract(i*width, width);
835 string wen_expr = make_expr(wen_sig[0]);
836
837 for (int i = 1; i < GetSize(wen_sig); i++)
838 if (wen_sig[0] != wen_sig[i])
839 log_error("Complex write enable on port %d on memory %s.%s.\n", i, log_id(module), log_id(cell));
840
841 SigSpec mask_sig = RTLIL::SigSpec(RTLIL::State::S1, 1);
842 write_port wp(name, clk_enable, clk_parity, transparency, clk_sig, wen_sig[0], addr_sig, mask_sig);
843 m.add_memory_write_port(wp);
844 cell_exprs.push_back(stringf("%s%s.data <= %s\n", indent.c_str(), name.c_str(), data_expr.c_str()));
845 cell_exprs.push_back(wp.gen_write(indent.c_str()));
846 }
847 register_memory(m);
848 continue;
849 }
850
851 if (cell->type.in(ID($memwr), ID($memrd), ID($meminit)))
852 {
853 std::string cell_type = fid(cell->type);
854 std::string mem_id = make_id(cell->parameters[ID::MEMID].decode_string());
855 int abits = cell->parameters.at(ID::ABITS).as_int();
856 int width = cell->parameters.at(ID::WIDTH).as_int();
857 memory *mp = nullptr;
858 if (cell->type == ID($meminit) ) {
859 log_error("$meminit (%s.%s.%s) currently unsupported\n", log_id(module), log_id(cell), mem_id.c_str());
860 } else {
861 // It's a $memwr or $memrd. Remember the read/write port parameters for the eventual FIRRTL memory definition.
862 auto addrSig = cell->getPort(ID::ADDR);
863 auto dataSig = cell->getPort(ID::DATA);
864 auto enableSig = cell->getPort(ID::EN);
865 auto clockSig = cell->getPort(ID::CLK);
866 Const clk_enable = cell->parameters.at(ID::CLK_ENABLE);
867 Const clk_polarity = cell->parameters.at(ID::CLK_POLARITY);
868
869 // Do we already have an entry for this memory?
870 if (memories.count(mem_id) == 0) {
871 memory m(cell, mem_id, abits, 0, width);
872 register_memory(m);
873 }
874 mp = &memories.at(mem_id);
875 int portNum = 0;
876 bool transparency = false;
877 string data_expr = make_expr(dataSig);
878 if (cell->type.in(ID($memwr))) {
879 portNum = (int) mp->write_ports.size();
880 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);
881 mp->add_memory_write_port(wp);
882 cell_exprs.push_back(stringf("%s%s.data <= %s\n", indent.c_str(), wp.name.c_str(), data_expr.c_str()));
883 cell_exprs.push_back(wp.gen_write(indent.c_str()));
884 } else if (cell->type.in(ID($memrd))) {
885 portNum = (int) mp->read_ports.size();
886 read_port rp(stringf("%s.r%d", mem_id.c_str(), portNum), clk_enable.as_bool(), clk_polarity.as_bool(), transparency, clockSig, enableSig, addrSig);
887 mp->add_memory_read_port(rp);
888 cell_exprs.push_back(rp.gen_read(indent.c_str()));
889 register_reverse_wire_map(stringf("%s.data", rp.name.c_str()), dataSig);
890 }
891 }
892 continue;
893 }
894
895 if (cell->type.in(ID($dff)))
896 {
897 bool clkpol = cell->parameters.at(ID::CLK_POLARITY).as_bool();
898 if (clkpol == false)
899 log_error("Negative edge clock on FF %s.%s.\n", log_id(module), log_id(cell));
900
901 int width = cell->parameters.at(ID::WIDTH).as_int();
902 string expr = make_expr(cell->getPort(ID::D));
903 string clk_expr = "asClock(" + make_expr(cell->getPort(ID::CLK)) + ")";
904
905 wire_decls.push_back(stringf(" reg %s: UInt<%d>, %s %s\n", y_id.c_str(), width, clk_expr.c_str(), cellFileinfo.c_str()));
906
907 cell_exprs.push_back(stringf(" %s <= %s %s\n", y_id.c_str(), expr.c_str(), cellFileinfo.c_str()));
908 register_reverse_wire_map(y_id, cell->getPort(ID::Q));
909
910 continue;
911 }
912
913 // This may be a parameterized module - paramod.
914 if (cell->type.begins_with("$paramod"))
915 {
916 process_instance(cell, wire_exprs);
917 continue;
918 }
919 if (cell->type == ID($shiftx)) {
920 // assign y = a[b +: y_width];
921 // We'll extract the correct bits as part of the primop.
922
923 string a_expr = make_expr(cell->getPort(ID::A));
924 // Get the initial bit selector
925 string b_expr = make_expr(cell->getPort(ID::B));
926 wire_decls.push_back(stringf(" wire %s: UInt<%d>\n", y_id.c_str(), y_width));
927
928 if (cell->getParam(ID::B_SIGNED).as_bool()) {
929 // Use validif to constrain the selection (test the sign bit)
930 auto b_string = b_expr.c_str();
931 int b_sign = cell->parameters.at(ID::B_WIDTH).as_int() - 1;
932 b_expr = stringf("validif(not(bits(%s, %d, %d)), %s)", b_string, b_sign, b_sign, b_string);
933 }
934 string expr = stringf("dshr(%s, %s)", a_expr.c_str(), b_expr.c_str());
935
936 cell_exprs.push_back(stringf(" %s <= %s\n", y_id.c_str(), expr.c_str()));
937 register_reverse_wire_map(y_id, cell->getPort(ID::Y));
938 continue;
939 }
940 if (cell->type == ID($shift)) {
941 // assign y = a >> b;
942 // where b may be negative
943
944 string a_expr = make_expr(cell->getPort(ID::A));
945 string b_expr = make_expr(cell->getPort(ID::B));
946 auto b_string = b_expr.c_str();
947 string expr;
948 wire_decls.push_back(stringf(" wire %s: UInt<%d>\n", y_id.c_str(), y_width));
949
950 if (cell->getParam(ID::B_SIGNED).as_bool()) {
951 // We generate a left or right shift based on the sign of b.
952 std::string dshl = stringf("bits(dshl(%s, %s), 0, %d)", a_expr.c_str(), gen_dshl(b_expr, b_width).c_str(), y_width);
953 std::string dshr = stringf("dshr(%s, %s)", a_expr.c_str(), b_string);
954 expr = stringf("mux(%s < 0, %s, %s)",
955 b_string,
956 dshl.c_str(),
957 dshr.c_str()
958 );
959 } else {
960 expr = stringf("dshr(%s, %s)", a_expr.c_str(), b_string);
961 }
962 cell_exprs.push_back(stringf(" %s <= %s\n", y_id.c_str(), expr.c_str()));
963 register_reverse_wire_map(y_id, cell->getPort(ID::Y));
964 continue;
965 }
966 if (cell->type == ID($pos)) {
967 // assign y = a;
968 // printCell(cell);
969 string a_expr = make_expr(cell->getPort(ID::A));
970 // Verilog appears to treat the result as signed, so if the result is wider than "A",
971 // we need to pad.
972 if (a_width < y_width) {
973 a_expr = stringf("pad(%s, %d)", a_expr.c_str(), y_width);
974 }
975 wire_decls.push_back(stringf(" wire %s: UInt<%d>\n", y_id.c_str(), y_width));
976 cell_exprs.push_back(stringf(" %s <= %s\n", y_id.c_str(), a_expr.c_str()));
977 register_reverse_wire_map(y_id, cell->getPort(ID::Y));
978 continue;
979 }
980 log_error("Cell type not supported: %s (%s.%s)\n", log_id(cell->type), log_id(module), log_id(cell));
981 }
982
983 for (auto conn : module->connections())
984 {
985 string y_id = next_id();
986 int y_width = GetSize(conn.first);
987 string expr = make_expr(conn.second);
988
989 wire_decls.push_back(stringf(" wire %s: UInt<%d>\n", y_id.c_str(), y_width));
990 cell_exprs.push_back(stringf(" %s <= %s\n", y_id.c_str(), expr.c_str()));
991 register_reverse_wire_map(y_id, conn.first);
992 }
993
994 for (auto wire : module->wires())
995 {
996 string expr;
997 std::string wireFileinfo = getFileinfo(wire);
998
999 if (wire->port_input)
1000 continue;
1001
1002 int cursor = 0;
1003 bool is_valid = false;
1004 bool make_unconn_id = false;
1005
1006 while (cursor < wire->width)
1007 {
1008 int chunk_width = 1;
1009 string new_expr;
1010
1011 SigBit start_bit(wire, cursor);
1012
1013 if (reverse_wire_map.count(start_bit))
1014 {
1015 pair<string, int> start_map = reverse_wire_map.at(start_bit);
1016
1017 while (cursor+chunk_width < wire->width)
1018 {
1019 SigBit stop_bit(wire, cursor+chunk_width);
1020
1021 if (reverse_wire_map.count(stop_bit) == 0)
1022 break;
1023
1024 pair<string, int> stop_map = reverse_wire_map.at(stop_bit);
1025 stop_map.second -= chunk_width;
1026
1027 if (start_map != stop_map)
1028 break;
1029
1030 chunk_width++;
1031 }
1032
1033 new_expr = stringf("bits(%s, %d, %d)", start_map.first.c_str(),
1034 start_map.second + chunk_width - 1, start_map.second);
1035 is_valid = true;
1036 }
1037 else
1038 {
1039 if (unconn_id.empty()) {
1040 unconn_id = next_id();
1041 make_unconn_id = true;
1042 }
1043 new_expr = unconn_id;
1044 }
1045
1046 if (expr.empty())
1047 expr = new_expr;
1048 else
1049 expr = "cat(" + new_expr + ", " + expr + ")";
1050
1051 cursor += chunk_width;
1052 }
1053
1054 if (is_valid) {
1055 if (make_unconn_id) {
1056 wire_decls.push_back(stringf(" wire %s: UInt<1> %s\n", unconn_id.c_str(), wireFileinfo.c_str()));
1057 // `invalid` is a firrtl construction for simulation so we will not
1058 // tag it with a @[fileinfo] tag as it doesn't directly correspond to
1059 // a specific line of verilog code.
1060 wire_decls.push_back(stringf(" %s is invalid\n", unconn_id.c_str()));
1061 }
1062 wire_exprs.push_back(stringf(" %s <= %s %s\n", make_id(wire->name), expr.c_str(), wireFileinfo.c_str()));
1063 } else {
1064 if (make_unconn_id) {
1065 unconn_id.clear();
1066 }
1067 // `invalid` is a firrtl construction for simulation so we will not
1068 // tag it with a @[fileinfo] tag as it doesn't directly correspond to
1069 // a specific line of verilog code.
1070 wire_decls.push_back(stringf(" %s is invalid\n", make_id(wire->name)));
1071 }
1072 }
1073
1074 for (auto str : port_decls)
1075 f << str;
1076
1077 f << stringf("\n");
1078
1079 for (auto str : wire_decls)
1080 f << str;
1081
1082 f << stringf("\n");
1083
1084 // If we have any memory definitions, output them.
1085 for (auto kv : memories) {
1086 memory &m = kv.second;
1087 f << stringf(" mem %s:\n", m.name.c_str());
1088 f << stringf(" data-type => UInt<%d>\n", m.width);
1089 f << stringf(" depth => %d\n", m.size);
1090 for (int i = 0; i < (int) m.read_ports.size(); i += 1) {
1091 f << stringf(" reader => r%d\n", i);
1092 }
1093 for (int i = 0; i < (int) m.write_ports.size(); i += 1) {
1094 f << stringf(" writer => w%d\n", i);
1095 }
1096 f << stringf(" read-latency => %d\n", m.read_latency);
1097 f << stringf(" write-latency => %d\n", m.write_latency);
1098 f << stringf(" read-under-write => undefined\n");
1099 }
1100 f << stringf("\n");
1101
1102 for (auto str : cell_exprs)
1103 f << str;
1104
1105 f << stringf("\n");
1106
1107 for (auto str : wire_exprs)
1108 f << str;
1109
1110 f << stringf("\n");
1111 }
1112
1113 void run()
1114 {
1115 // Blackboxes should be emitted as `extmodule`s in firrtl. Only ports are
1116 // emitted in such a case.
1117 if (module->get_blackbox_attribute())
1118 emit_extmodule();
1119 else
1120 emit_module();
1121 }
1122 };
1123
1124 struct FirrtlBackend : public Backend {
1125 FirrtlBackend() : Backend("firrtl", "write design to a FIRRTL file") { }
1126 void help() YS_OVERRIDE
1127 {
1128 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
1129 log("\n");
1130 log(" write_firrtl [options] [filename]\n");
1131 log("\n");
1132 log("Write a FIRRTL netlist of the current design.\n");
1133 log("The following commands are executed by this command:\n");
1134 log(" pmuxtree\n");
1135 log("\n");
1136 }
1137 void execute(std::ostream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
1138 {
1139 size_t argidx = args.size(); // We aren't expecting any arguments.
1140
1141 // If we weren't explicitly passed a filename, use the last argument (if it isn't a flag).
1142 if (filename == "") {
1143 if (argidx > 0 && args[argidx - 1][0] != '-') {
1144 // extra_args and friends need to see this argument.
1145 argidx -= 1;
1146 filename = args[argidx];
1147 }
1148 }
1149 extra_args(f, filename, args, argidx);
1150
1151 if (!design->full_selection())
1152 log_cmd_error("This command only operates on fully selected designs!\n");
1153
1154 log_header(design, "Executing FIRRTL backend.\n");
1155 log_push();
1156
1157 Pass::call(design, stringf("pmuxtree"));
1158
1159 namecache.clear();
1160 autoid_counter = 0;
1161
1162 // Get the top module, or a reasonable facsimile - we need something for the circuit name.
1163 Module *top = design->top_module();
1164 Module *last = nullptr;
1165 // Generate module and wire names.
1166 for (auto module : design->modules()) {
1167 make_id(module->name);
1168 last = module;
1169 if (top == nullptr && module->get_bool_attribute(ID::top)) {
1170 top = module;
1171 }
1172 for (auto wire : module->wires())
1173 if (wire->port_id)
1174 make_id(wire->name);
1175 }
1176
1177 if (top == nullptr)
1178 top = last;
1179
1180 std::string circuitFileinfo = getFileinfo(top);
1181 *f << stringf("circuit %s: %s\n", make_id(top->name), circuitFileinfo.c_str());
1182
1183 for (auto module : design->modules())
1184 {
1185 FirrtlWorker worker(module, *f, design);
1186 worker.run();
1187 }
1188
1189 namecache.clear();
1190 autoid_counter = 0;
1191 }
1192 } FirrtlBackend;
1193
1194 PRIVATE_NAMESPACE_END