2013-01-05 04:13:26 -06:00
|
|
|
/*
|
|
|
|
* yosys -- Yosys Open SYnthesis Suite
|
|
|
|
*
|
2021-06-07 17:39:36 -05:00
|
|
|
* Copyright (C) 2012 Claire Xenia Wolf <claire@yosyshq.com>
|
2015-07-02 04:14:30 -05:00
|
|
|
*
|
2013-01-05 04:13:26 -06:00
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
2015-07-02 04:14:30 -05:00
|
|
|
*
|
2013-01-05 04:13:26 -06:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
* ---
|
|
|
|
*
|
2015-08-14 15:23:01 -05:00
|
|
|
* A simple and straightforward Verilog backend.
|
2013-01-05 04:13:26 -06:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "kernel/register.h"
|
|
|
|
#include "kernel/celltypes.h"
|
|
|
|
#include "kernel/log.h"
|
2015-05-08 14:29:51 -05:00
|
|
|
#include "kernel/sigtools.h"
|
2020-06-23 16:46:00 -05:00
|
|
|
#include "kernel/ff.h"
|
2020-10-17 14:48:38 -05:00
|
|
|
#include "kernel/mem.h"
|
2020-11-29 11:31:04 -06:00
|
|
|
#include "kernel/fmt.h"
|
2013-01-05 04:13:26 -06:00
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
|
|
|
#include <set>
|
|
|
|
#include <map>
|
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
USING_YOSYS_NAMESPACE
|
|
|
|
PRIVATE_NAMESPACE_BEGIN
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2022-11-10 09:16:37 -06:00
|
|
|
bool verbose, norename, noattr, attr2comment, noexpr, nodec, nohex, nostr, extmem, defparam, decimal, siminit, systemverilog, simple_lhs, noparallelcase;
|
2019-11-14 21:11:46 -06:00
|
|
|
int auto_name_counter, auto_name_offset, auto_name_digits, extmem_counter;
|
2023-07-19 09:56:58 -05:00
|
|
|
dict<RTLIL::IdString, int> auto_name_map;
|
2020-06-23 16:46:00 -05:00
|
|
|
std::set<RTLIL::IdString> reg_wires;
|
2019-11-14 21:11:46 -06:00
|
|
|
std::string auto_prefix, extmem_prefix;
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
RTLIL::Module *active_module;
|
2016-11-16 05:00:39 -06:00
|
|
|
dict<RTLIL::SigBit, RTLIL::State> active_initdata;
|
|
|
|
SigMap active_sigmap;
|
2021-11-17 05:19:06 -06:00
|
|
|
IdString initial_id;
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2014-08-02 06:11:01 -05:00
|
|
|
void reset_auto_counter_id(RTLIL::IdString id, bool may_rename)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
|
|
|
const char *str = id.c_str();
|
|
|
|
|
|
|
|
if (*str == '$' && may_rename && !norename)
|
|
|
|
auto_name_map[id] = auto_name_counter++;
|
|
|
|
|
2014-11-07 07:40:06 -06:00
|
|
|
if (str[0] != '\\' || str[1] != '_' || str[2] == 0)
|
2013-01-05 04:13:26 -06:00
|
|
|
return;
|
2014-11-07 07:40:06 -06:00
|
|
|
|
|
|
|
for (int i = 2; str[i] != 0; i++) {
|
|
|
|
if (str[i] == '_' && str[i+1] == 0)
|
2013-01-05 04:13:26 -06:00
|
|
|
continue;
|
|
|
|
if (str[i] < '0' || str[i] > '9')
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-07 07:40:06 -06:00
|
|
|
int num = atoi(str+2);
|
2013-01-05 04:13:26 -06:00
|
|
|
if (num >= auto_name_offset)
|
|
|
|
auto_name_offset = num + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset_auto_counter(RTLIL::Module *module)
|
|
|
|
{
|
|
|
|
auto_name_map.clear();
|
|
|
|
auto_name_counter = 0;
|
|
|
|
auto_name_offset = 0;
|
|
|
|
|
|
|
|
reset_auto_counter_id(module->name, false);
|
|
|
|
|
2020-04-01 00:25:10 -05:00
|
|
|
for (auto w : module->wires())
|
|
|
|
reset_auto_counter_id(w->name, true);
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2020-04-01 00:25:10 -05:00
|
|
|
for (auto cell : module->cells()) {
|
|
|
|
reset_auto_counter_id(cell->name, true);
|
|
|
|
reset_auto_counter_id(cell->type, false);
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
2014-12-26 14:35:22 -06:00
|
|
|
for (auto it = module->processes.begin(); it != module->processes.end(); ++it)
|
2013-01-05 04:13:26 -06:00
|
|
|
reset_auto_counter_id(it->second->name, false);
|
|
|
|
|
|
|
|
auto_name_digits = 1;
|
|
|
|
for (size_t i = 10; i < auto_name_offset + auto_name_map.size(); i = i*10)
|
|
|
|
auto_name_digits++;
|
|
|
|
|
2016-11-01 05:30:27 -05:00
|
|
|
if (verbose)
|
|
|
|
for (auto it = auto_name_map.begin(); it != auto_name_map.end(); ++it)
|
|
|
|
log(" renaming `%s' to `%s_%0*d_'.\n", it->first.c_str(), auto_prefix.c_str(), auto_name_digits, auto_name_offset + it->second);
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
2015-05-20 06:55:50 -05:00
|
|
|
std::string next_auto_id()
|
|
|
|
{
|
2016-11-01 05:30:27 -05:00
|
|
|
return stringf("%s_%0*d_", auto_prefix.c_str(), auto_name_digits, auto_name_offset + auto_name_counter++);
|
2015-05-20 06:55:50 -05:00
|
|
|
}
|
|
|
|
|
2014-08-02 06:11:01 -05:00
|
|
|
std::string id(RTLIL::IdString internal_id, bool may_rename = true)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
|
|
|
const char *str = internal_id.c_str();
|
|
|
|
bool do_escape = false;
|
|
|
|
|
2015-05-20 06:55:50 -05:00
|
|
|
if (may_rename && auto_name_map.count(internal_id) != 0)
|
2016-11-01 05:30:27 -05:00
|
|
|
return stringf("%s_%0*d_", auto_prefix.c_str(), auto_name_digits, auto_name_offset + auto_name_map[internal_id]);
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
if (*str == '\\')
|
|
|
|
str++;
|
|
|
|
|
|
|
|
if ('0' <= *str && *str <= '9')
|
|
|
|
do_escape = true;
|
|
|
|
|
|
|
|
for (int i = 0; str[i]; i++)
|
|
|
|
{
|
|
|
|
if ('0' <= str[i] && str[i] <= '9')
|
|
|
|
continue;
|
|
|
|
if ('a' <= str[i] && str[i] <= 'z')
|
|
|
|
continue;
|
|
|
|
if ('A' <= str[i] && str[i] <= 'Z')
|
|
|
|
continue;
|
|
|
|
if (str[i] == '_')
|
|
|
|
continue;
|
|
|
|
do_escape = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-07-19 09:55:30 -05:00
|
|
|
static const pool<string> keywords = {
|
2019-01-26 17:55:46 -06:00
|
|
|
// IEEE 1800-2017 Annex B
|
|
|
|
"accept_on", "alias", "always", "always_comb", "always_ff", "always_latch", "and", "assert", "assign", "assume", "automatic", "before",
|
|
|
|
"begin", "bind", "bins", "binsof", "bit", "break", "buf", "bufif0", "bufif1", "byte", "case", "casex", "casez", "cell", "chandle",
|
|
|
|
"checker", "class", "clocking", "cmos", "config", "const", "constraint", "context", "continue", "cover", "covergroup", "coverpoint",
|
|
|
|
"cross", "deassign", "default", "defparam", "design", "disable", "dist", "do", "edge", "else", "end", "endcase", "endchecker",
|
|
|
|
"endclass", "endclocking", "endconfig", "endfunction", "endgenerate", "endgroup", "endinterface", "endmodule", "endpackage",
|
|
|
|
"endprimitive", "endprogram", "endproperty", "endsequence", "endspecify", "endtable", "endtask", "enum", "event", "eventually",
|
|
|
|
"expect", "export", "extends", "extern", "final", "first_match", "for", "force", "foreach", "forever", "fork", "forkjoin", "function",
|
|
|
|
"generate", "genvar", "global", "highz0", "highz1", "if", "iff", "ifnone", "ignore_bins", "illegal_bins", "implements", "implies",
|
|
|
|
"import", "incdir", "include", "initial", "inout", "input", "inside", "instance", "int", "integer", "interconnect", "interface",
|
|
|
|
"intersect", "join", "join_any", "join_none", "large", "let", "liblist", "library", "local", "localparam", "logic", "longint",
|
|
|
|
"macromodule", "matches", "medium", "modport", "module", "nand", "negedge", "nettype", "new", "nexttime", "nmos", "nor",
|
|
|
|
"noshowcancelled", "not", "notif0", "notif1", "null", "or", "output", "package", "packed", "parameter", "pmos", "posedge", "primitive",
|
|
|
|
"priority", "program", "property", "protected", "pull0", "pull1", "pulldown", "pullup", "pulsestyle_ondetect", "pulsestyle_onevent",
|
|
|
|
"pure", "rand", "randc", "randcase", "randsequence", "rcmos", "real", "realtime", "ref", "reg", "reject_on", "release", "repeat",
|
|
|
|
"restrict", "return", "rnmos", "rpmos", "rtran", "rtranif0", "rtranif1", "s_always", "s_eventually", "s_nexttime", "s_until",
|
|
|
|
"s_until_with", "scalared", "sequence", "shortint", "shortreal", "showcancelled", "signed", "small", "soft", "solve", "specify",
|
|
|
|
"specparam", "static", "string", "strong", "strong0", "strong1", "struct", "super", "supply0", "supply1", "sync_accept_on",
|
|
|
|
"sync_reject_on", "table", "tagged", "task", "this", "throughout", "time", "timeprecision", "timeunit", "tran", "tranif0", "tranif1",
|
|
|
|
"tri", "tri0", "tri1", "triand", "trior", "trireg", "type", "typedef", "union", "unique", "unique0", "unsigned", "until", "until_with",
|
|
|
|
"untyped", "use", "uwire", "var", "vectored", "virtual", "void", "wait", "wait_order", "wand", "weak", "weak0", "weak1", "while",
|
|
|
|
"wildcard", "wire", "with", "within", "wor", "xnor", "xor",
|
|
|
|
};
|
|
|
|
if (keywords.count(str))
|
|
|
|
do_escape = true;
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
if (do_escape)
|
|
|
|
return "\\" + std::string(str) + " ";
|
|
|
|
return std::string(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_reg_wire(RTLIL::SigSpec sig, std::string ®_name)
|
|
|
|
{
|
2014-07-25 07:23:31 -05:00
|
|
|
if (!sig.is_chunk() || sig.as_chunk().wire == NULL)
|
2013-01-05 04:13:26 -06:00
|
|
|
return false;
|
2014-07-25 07:23:31 -05:00
|
|
|
|
|
|
|
RTLIL::SigChunk chunk = sig.as_chunk();
|
|
|
|
|
|
|
|
if (reg_wires.count(chunk.wire->name) == 0)
|
2013-01-05 04:13:26 -06:00
|
|
|
return false;
|
2014-07-25 07:23:31 -05:00
|
|
|
|
|
|
|
reg_name = id(chunk.wire->name);
|
|
|
|
if (sig.size() != chunk.wire->width) {
|
2014-07-22 13:15:14 -05:00
|
|
|
if (sig.size() == 1)
|
2014-07-25 07:23:31 -05:00
|
|
|
reg_name += stringf("[%d]", chunk.wire->start_offset + chunk.offset);
|
2016-08-15 01:26:20 -05:00
|
|
|
else if (chunk.wire->upto)
|
|
|
|
reg_name += stringf("[%d:%d]", (chunk.wire->width - (chunk.offset + chunk.width - 1) - 1) + chunk.wire->start_offset,
|
|
|
|
(chunk.wire->width - chunk.offset - 1) + chunk.wire->start_offset);
|
2013-01-05 04:13:26 -06:00
|
|
|
else
|
2014-07-25 07:23:31 -05:00
|
|
|
reg_name += stringf("[%d:%d]", chunk.wire->start_offset + chunk.offset + chunk.width - 1,
|
|
|
|
chunk.wire->start_offset + chunk.offset);
|
2013-03-03 15:45:06 -06:00
|
|
|
}
|
2014-07-25 07:23:31 -05:00
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-04 01:46:24 -05:00
|
|
|
void dump_const(std::ostream &f, const RTLIL::Const &data, int width = -1, int offset = 0, bool no_decimal = false, bool escape_comment = false)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2019-05-04 01:46:24 -05:00
|
|
|
bool set_signed = (data.flags & RTLIL::CONST_FLAG_SIGNED) != 0;
|
2013-01-05 04:13:26 -06:00
|
|
|
if (width < 0)
|
|
|
|
width = data.bits.size() - offset;
|
2019-04-22 12:44:10 -05:00
|
|
|
if (width == 0) {
|
2019-07-16 15:57:05 -05:00
|
|
|
// See IEEE 1364-2005 Clause 5.1.14.
|
|
|
|
f << "{0{1'b0}}";
|
2019-04-22 12:44:10 -05:00
|
|
|
return;
|
|
|
|
}
|
2016-07-30 05:38:40 -05:00
|
|
|
if (nostr)
|
2016-11-03 06:13:23 -05:00
|
|
|
goto dump_hex;
|
2013-12-04 07:14:05 -06:00
|
|
|
if ((data.flags & RTLIL::CONST_FLAG_STRING) == 0 || width != (int)data.bits.size()) {
|
2016-07-30 05:38:40 -05:00
|
|
|
if (width == 32 && !no_decimal && !nodec) {
|
2013-08-22 13:22:19 -05:00
|
|
|
int32_t val = 0;
|
2013-01-05 04:13:26 -06:00
|
|
|
for (int i = offset+width-1; i >= offset; i--) {
|
2014-07-28 04:08:55 -05:00
|
|
|
log_assert(i < (int)data.bits.size());
|
2019-08-07 13:12:38 -05:00
|
|
|
if (data.bits[i] != State::S0 && data.bits[i] != State::S1)
|
2016-11-03 06:13:23 -05:00
|
|
|
goto dump_hex;
|
2019-08-07 13:12:38 -05:00
|
|
|
if (data.bits[i] == State::S1)
|
2013-01-05 04:13:26 -06:00
|
|
|
val |= 1 << (i - offset);
|
|
|
|
}
|
2017-10-03 10:31:21 -05:00
|
|
|
if (decimal)
|
|
|
|
f << stringf("%d", val);
|
|
|
|
else if (set_signed && val < 0)
|
2016-07-30 05:38:40 -05:00
|
|
|
f << stringf("-32'sd%u", -val);
|
2017-10-03 10:31:21 -05:00
|
|
|
else
|
|
|
|
f << stringf("32'%sd%u", set_signed ? "s" : "", val);
|
2013-01-05 04:13:26 -06:00
|
|
|
} else {
|
2016-11-03 06:13:23 -05:00
|
|
|
dump_hex:
|
|
|
|
if (nohex)
|
|
|
|
goto dump_bin;
|
|
|
|
vector<char> bin_digits, hex_digits;
|
|
|
|
for (int i = offset; i < offset+width; i++) {
|
|
|
|
log_assert(i < (int)data.bits.size());
|
|
|
|
switch (data.bits[i]) {
|
2019-08-07 13:12:38 -05:00
|
|
|
case State::S0: bin_digits.push_back('0'); break;
|
|
|
|
case State::S1: bin_digits.push_back('1'); break;
|
2016-11-03 06:13:23 -05:00
|
|
|
case RTLIL::Sx: bin_digits.push_back('x'); break;
|
|
|
|
case RTLIL::Sz: bin_digits.push_back('z'); break;
|
2019-07-09 13:30:24 -05:00
|
|
|
case RTLIL::Sa: bin_digits.push_back('?'); break;
|
2016-11-03 06:13:23 -05:00
|
|
|
case RTLIL::Sm: log_error("Found marker state in final netlist.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (GetSize(bin_digits) == 0)
|
|
|
|
goto dump_bin;
|
|
|
|
while (GetSize(bin_digits) % 4 != 0)
|
|
|
|
if (bin_digits.back() == '1')
|
|
|
|
bin_digits.push_back('0');
|
|
|
|
else
|
|
|
|
bin_digits.push_back(bin_digits.back());
|
|
|
|
for (int i = 0; i < GetSize(bin_digits); i += 4)
|
|
|
|
{
|
|
|
|
char bit_3 = bin_digits[i+3];
|
|
|
|
char bit_2 = bin_digits[i+2];
|
|
|
|
char bit_1 = bin_digits[i+1];
|
|
|
|
char bit_0 = bin_digits[i+0];
|
|
|
|
if (bit_3 == 'x' || bit_2 == 'x' || bit_1 == 'x' || bit_0 == 'x') {
|
|
|
|
if (bit_3 != 'x' || bit_2 != 'x' || bit_1 != 'x' || bit_0 != 'x')
|
|
|
|
goto dump_bin;
|
|
|
|
hex_digits.push_back('x');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (bit_3 == 'z' || bit_2 == 'z' || bit_1 == 'z' || bit_0 == 'z') {
|
|
|
|
if (bit_3 != 'z' || bit_2 != 'z' || bit_1 != 'z' || bit_0 != 'z')
|
|
|
|
goto dump_bin;
|
|
|
|
hex_digits.push_back('z');
|
|
|
|
continue;
|
|
|
|
}
|
2019-07-09 13:30:24 -05:00
|
|
|
if (bit_3 == '?' || bit_2 == '?' || bit_1 == '?' || bit_0 == '?') {
|
|
|
|
if (bit_3 != '?' || bit_2 != '?' || bit_1 != '?' || bit_0 != '?')
|
|
|
|
goto dump_bin;
|
|
|
|
hex_digits.push_back('?');
|
|
|
|
continue;
|
|
|
|
}
|
2016-11-03 06:13:23 -05:00
|
|
|
int val = 8*(bit_3 - '0') + 4*(bit_2 - '0') + 2*(bit_1 - '0') + (bit_0 - '0');
|
|
|
|
hex_digits.push_back(val < 10 ? '0' + val : 'a' + val - 10);
|
|
|
|
}
|
|
|
|
f << stringf("%d'%sh", width, set_signed ? "s" : "");
|
|
|
|
for (int i = GetSize(hex_digits)-1; i >= 0; i--)
|
|
|
|
f << hex_digits[i];
|
|
|
|
}
|
|
|
|
if (0) {
|
|
|
|
dump_bin:
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%d'%sb", width, set_signed ? "s" : "");
|
2013-10-24 03:59:27 -05:00
|
|
|
if (width == 0)
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("0");
|
2013-01-05 04:13:26 -06:00
|
|
|
for (int i = offset+width-1; i >= offset; i--) {
|
2014-07-28 04:08:55 -05:00
|
|
|
log_assert(i < (int)data.bits.size());
|
2013-01-05 04:13:26 -06:00
|
|
|
switch (data.bits[i]) {
|
2019-08-07 13:12:38 -05:00
|
|
|
case State::S0: f << stringf("0"); break;
|
|
|
|
case State::S1: f << stringf("1"); break;
|
2014-08-23 06:54:21 -05:00
|
|
|
case RTLIL::Sx: f << stringf("x"); break;
|
|
|
|
case RTLIL::Sz: f << stringf("z"); break;
|
2019-07-09 13:30:24 -05:00
|
|
|
case RTLIL::Sa: f << stringf("?"); break;
|
2013-01-05 04:13:26 -06:00
|
|
|
case RTLIL::Sm: log_error("Found marker state in final netlist.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2019-05-04 01:46:24 -05:00
|
|
|
if ((data.flags & RTLIL::CONST_FLAG_REAL) == 0)
|
|
|
|
f << stringf("\"");
|
2013-12-04 07:14:05 -06:00
|
|
|
std::string str = data.decode_string();
|
|
|
|
for (size_t i = 0; i < str.size(); i++) {
|
|
|
|
if (str[i] == '\n')
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("\\n");
|
2013-12-04 07:14:05 -06:00
|
|
|
else if (str[i] == '\t')
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("\\t");
|
2013-12-04 07:14:05 -06:00
|
|
|
else if (str[i] < 32)
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("\\%03o", str[i]);
|
2013-12-04 07:14:05 -06:00
|
|
|
else if (str[i] == '"')
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("\\\"");
|
2013-12-04 07:14:05 -06:00
|
|
|
else if (str[i] == '\\')
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("\\\\");
|
2015-02-13 15:48:10 -06:00
|
|
|
else if (str[i] == '/' && escape_comment && i > 0 && str[i-1] == '*')
|
|
|
|
f << stringf("\\/");
|
2013-01-05 04:13:26 -06:00
|
|
|
else
|
2014-08-23 06:54:21 -05:00
|
|
|
f << str[i];
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
2019-05-04 01:46:24 -05:00
|
|
|
if ((data.flags & RTLIL::CONST_FLAG_REAL) == 0)
|
|
|
|
f << stringf("\"");
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-17 14:18:12 -06:00
|
|
|
void dump_reg_init(std::ostream &f, SigSpec sig)
|
2016-11-16 05:00:39 -06:00
|
|
|
{
|
|
|
|
Const initval;
|
|
|
|
bool gotinit = false;
|
|
|
|
|
|
|
|
for (auto bit : active_sigmap(sig)) {
|
|
|
|
if (active_initdata.count(bit)) {
|
|
|
|
initval.bits.push_back(active_initdata.at(bit));
|
|
|
|
gotinit = true;
|
|
|
|
} else {
|
|
|
|
initval.bits.push_back(State::Sx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gotinit) {
|
2019-02-17 14:18:12 -06:00
|
|
|
f << " = ";
|
2016-11-16 05:00:39 -06:00
|
|
|
dump_const(f, initval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-23 06:54:21 -05:00
|
|
|
void dump_sigchunk(std::ostream &f, const RTLIL::SigChunk &chunk, bool no_decimal = false)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
|
|
|
if (chunk.wire == NULL) {
|
|
|
|
dump_const(f, chunk.data, chunk.width, chunk.offset, no_decimal);
|
|
|
|
} else {
|
2014-07-28 07:25:03 -05:00
|
|
|
if (chunk.width == chunk.wire->width && chunk.offset == 0) {
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s", id(chunk.wire->name).c_str());
|
2014-07-28 07:25:03 -05:00
|
|
|
} else if (chunk.width == 1) {
|
|
|
|
if (chunk.wire->upto)
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s[%d]", id(chunk.wire->name).c_str(), (chunk.wire->width - chunk.offset - 1) + chunk.wire->start_offset);
|
2014-07-28 07:25:03 -05:00
|
|
|
else
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s[%d]", id(chunk.wire->name).c_str(), chunk.offset + chunk.wire->start_offset);
|
2014-07-28 07:25:03 -05:00
|
|
|
} else {
|
|
|
|
if (chunk.wire->upto)
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s[%d:%d]", id(chunk.wire->name).c_str(),
|
2014-07-28 07:25:03 -05:00
|
|
|
(chunk.wire->width - (chunk.offset + chunk.width - 1) - 1) + chunk.wire->start_offset,
|
|
|
|
(chunk.wire->width - chunk.offset - 1) + chunk.wire->start_offset);
|
|
|
|
else
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s[%d:%d]", id(chunk.wire->name).c_str(),
|
2014-07-28 07:25:03 -05:00
|
|
|
(chunk.offset + chunk.width - 1) + chunk.wire->start_offset,
|
|
|
|
chunk.offset + chunk.wire->start_offset);
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-23 06:54:21 -05:00
|
|
|
void dump_sigspec(std::ostream &f, const RTLIL::SigSpec &sig)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2019-04-22 12:44:10 -05:00
|
|
|
if (GetSize(sig) == 0) {
|
2021-12-11 06:01:52 -06:00
|
|
|
// See IEEE 1364-2005 Clause 5.1.14.
|
|
|
|
f << "{0{1'b0}}";
|
2019-04-22 12:44:10 -05:00
|
|
|
return;
|
|
|
|
}
|
2014-07-25 07:23:31 -05:00
|
|
|
if (sig.is_chunk()) {
|
|
|
|
dump_sigchunk(f, sig.as_chunk());
|
2013-01-05 04:13:26 -06:00
|
|
|
} else {
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("{ ");
|
2014-12-26 14:35:22 -06:00
|
|
|
for (auto it = sig.chunks().rbegin(); it != sig.chunks().rend(); ++it) {
|
2014-07-22 13:15:14 -05:00
|
|
|
if (it != sig.chunks().rbegin())
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(", ");
|
2013-01-05 04:13:26 -06:00
|
|
|
dump_sigchunk(f, *it, true);
|
|
|
|
}
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(" }");
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-20 20:02:39 -06:00
|
|
|
void dump_attributes(std::ostream &f, std::string indent, dict<RTLIL::IdString, RTLIL::Const> &attributes, std::string term = "\n", bool modattr = false, bool regattr = false, bool as_comment = false)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
|
|
|
if (noattr)
|
|
|
|
return;
|
2019-07-08 07:48:50 -05:00
|
|
|
if (attr2comment)
|
|
|
|
as_comment = true;
|
2014-12-26 14:35:22 -06:00
|
|
|
for (auto it = attributes.begin(); it != attributes.end(); ++it) {
|
2020-04-02 11:51:32 -05:00
|
|
|
if (it->first == ID::init && regattr) continue;
|
2019-07-08 07:48:50 -05:00
|
|
|
f << stringf("%s" "%s %s", indent.c_str(), as_comment ? "/*" : "(*", id(it->first).c_str());
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(" = ");
|
2019-08-06 18:22:47 -05:00
|
|
|
if (modattr && (it->second == State::S0 || it->second == Const(0)))
|
2015-01-02 15:46:20 -06:00
|
|
|
f << stringf(" 0 ");
|
2019-08-06 18:22:47 -05:00
|
|
|
else if (modattr && (it->second == State::S1 || it->second == Const(1)))
|
2015-01-02 15:46:20 -06:00
|
|
|
f << stringf(" 1 ");
|
|
|
|
else
|
2019-07-08 07:48:50 -05:00
|
|
|
dump_const(f, it->second, -1, 0, false, as_comment);
|
2023-12-20 20:02:39 -06:00
|
|
|
f << stringf(" %s%s", as_comment ? "*/" : "*)", term.c_str());
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-23 06:54:21 -05:00
|
|
|
void dump_wire(std::ostream &f, std::string indent, RTLIL::Wire *wire)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2023-12-20 20:02:39 -06:00
|
|
|
dump_attributes(f, indent, wire->attributes, "\n", /*modattr=*/false, /*regattr=*/reg_wires.count(wire->name));
|
2013-03-21 03:51:25 -05:00
|
|
|
#if 0
|
2013-01-05 04:13:26 -06:00
|
|
|
if (wire->port_input && !wire->port_output)
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s" "input %s", indent.c_str(), reg_wires.count(wire->name) ? "reg " : "");
|
2013-01-05 04:13:26 -06:00
|
|
|
else if (!wire->port_input && wire->port_output)
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s" "output %s", indent.c_str(), reg_wires.count(wire->name) ? "reg " : "");
|
2013-01-05 04:13:26 -06:00
|
|
|
else if (wire->port_input && wire->port_output)
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s" "inout %s", indent.c_str(), reg_wires.count(wire->name) ? "reg " : "");
|
2013-01-05 04:13:26 -06:00
|
|
|
else
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s" "%s ", indent.c_str(), reg_wires.count(wire->name) ? "reg" : "wire");
|
2013-01-05 04:13:26 -06:00
|
|
|
if (wire->width != 1)
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("[%d:%d] ", wire->width - 1 + wire->start_offset, wire->start_offset);
|
|
|
|
f << stringf("%s;\n", id(wire->name).c_str());
|
2013-03-21 03:51:25 -05:00
|
|
|
#else
|
2015-08-14 03:56:05 -05:00
|
|
|
// do not use Verilog-2k "output reg" syntax in Verilog export
|
2013-03-21 03:51:25 -05:00
|
|
|
std::string range = "";
|
2014-07-28 07:25:03 -05:00
|
|
|
if (wire->width != 1) {
|
|
|
|
if (wire->upto)
|
|
|
|
range = stringf(" [%d:%d]", wire->start_offset, wire->width - 1 + wire->start_offset);
|
|
|
|
else
|
|
|
|
range = stringf(" [%d:%d]", wire->width - 1 + wire->start_offset, wire->start_offset);
|
|
|
|
}
|
2013-03-21 03:51:25 -05:00
|
|
|
if (wire->port_input && !wire->port_output)
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s" "input%s %s;\n", indent.c_str(), range.c_str(), id(wire->name).c_str());
|
2013-03-21 03:51:25 -05:00
|
|
|
if (!wire->port_input && wire->port_output)
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s" "output%s %s;\n", indent.c_str(), range.c_str(), id(wire->name).c_str());
|
2013-03-21 03:51:25 -05:00
|
|
|
if (wire->port_input && wire->port_output)
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s" "inout%s %s;\n", indent.c_str(), range.c_str(), id(wire->name).c_str());
|
2015-04-09 08:12:26 -05:00
|
|
|
if (reg_wires.count(wire->name)) {
|
2016-11-16 05:00:39 -06:00
|
|
|
f << stringf("%s" "reg%s %s", indent.c_str(), range.c_str(), id(wire->name).c_str());
|
2020-04-02 11:51:32 -05:00
|
|
|
if (wire->attributes.count(ID::init)) {
|
2016-11-16 05:00:39 -06:00
|
|
|
f << stringf(" = ");
|
2020-04-02 11:51:32 -05:00
|
|
|
dump_const(f, wire->attributes.at(ID::init));
|
2015-04-09 08:12:26 -05:00
|
|
|
}
|
2016-11-16 05:00:39 -06:00
|
|
|
f << stringf(";\n");
|
2022-01-30 13:48:50 -06:00
|
|
|
} else
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s" "wire%s %s;\n", indent.c_str(), range.c_str(), id(wire->name).c_str());
|
2013-03-21 03:51:25 -05:00
|
|
|
#endif
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
2020-10-17 14:48:38 -05:00
|
|
|
void dump_memory(std::ostream &f, std::string indent, Mem &mem)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2020-10-17 14:48:38 -05:00
|
|
|
std::string mem_id = id(mem.memid);
|
|
|
|
|
|
|
|
dump_attributes(f, indent, mem.attributes);
|
|
|
|
f << stringf("%s" "reg [%d:0] %s [%d:%d];\n", indent.c_str(), mem.width-1, mem_id.c_str(), mem.size+mem.start_offset-1, mem.start_offset);
|
|
|
|
|
|
|
|
// for memory block make something like:
|
|
|
|
// reg [7:0] memid [3:0];
|
|
|
|
// initial begin
|
|
|
|
// memid[0] = ...
|
|
|
|
// end
|
|
|
|
if (!mem.inits.empty())
|
|
|
|
{
|
|
|
|
if (extmem)
|
|
|
|
{
|
|
|
|
std::string extmem_filename = stringf("%s-%d.mem", extmem_prefix.c_str(), extmem_counter++);
|
|
|
|
|
|
|
|
std::string extmem_filename_esc;
|
|
|
|
for (auto c : extmem_filename)
|
|
|
|
{
|
|
|
|
if (c == '\n')
|
|
|
|
extmem_filename_esc += "\\n";
|
|
|
|
else if (c == '\t')
|
|
|
|
extmem_filename_esc += "\\t";
|
|
|
|
else if (c < 32)
|
|
|
|
extmem_filename_esc += stringf("\\%03o", c);
|
|
|
|
else if (c == '"')
|
|
|
|
extmem_filename_esc += "\\\"";
|
|
|
|
else if (c == '\\')
|
|
|
|
extmem_filename_esc += "\\\\";
|
|
|
|
else
|
|
|
|
extmem_filename_esc += c;
|
|
|
|
}
|
|
|
|
f << stringf("%s" "initial $readmemb(\"%s\", %s);\n", indent.c_str(), extmem_filename_esc.c_str(), mem_id.c_str());
|
|
|
|
|
|
|
|
std::ofstream extmem_f(extmem_filename, std::ofstream::trunc);
|
|
|
|
if (extmem_f.fail())
|
|
|
|
log_error("Can't open file `%s' for writing: %s\n", extmem_filename.c_str(), strerror(errno));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Const data = mem.get_init_data();
|
|
|
|
for (int i=0; i<mem.size; i++)
|
|
|
|
{
|
|
|
|
RTLIL::Const element = data.extract(i*mem.width, mem.width);
|
|
|
|
for (int j=0; j<element.size(); j++)
|
|
|
|
{
|
|
|
|
switch (element[element.size()-j-1])
|
|
|
|
{
|
|
|
|
case State::S0: extmem_f << '0'; break;
|
|
|
|
case State::S1: extmem_f << '1'; break;
|
|
|
|
case State::Sx: extmem_f << 'x'; break;
|
|
|
|
case State::Sz: extmem_f << 'z'; break;
|
|
|
|
case State::Sa: extmem_f << '_'; break;
|
|
|
|
case State::Sm: log_error("Found marker state in final netlist.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
extmem_f << '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
f << stringf("%s" "initial begin\n", indent.c_str());
|
|
|
|
for (auto &init : mem.inits) {
|
|
|
|
int words = GetSize(init.data) / mem.width;
|
|
|
|
int start = init.addr.as_int();
|
|
|
|
for (int i=0; i<words; i++)
|
|
|
|
{
|
2021-07-12 13:43:09 -05:00
|
|
|
for (int j = 0; j < mem.width; j++)
|
|
|
|
{
|
|
|
|
if (init.en[j] != State::S1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int start_j = j, width = 1;
|
|
|
|
|
|
|
|
while (j+1 < mem.width && init.en[j+1] == State::S1)
|
|
|
|
j++, width++;
|
|
|
|
|
|
|
|
if (width == mem.width) {
|
|
|
|
f << stringf("%s" " %s[%d] = ", indent.c_str(), mem_id.c_str(), i + start);
|
|
|
|
} else {
|
|
|
|
f << stringf("%s" " %s[%d][%d:%d] = ", indent.c_str(), mem_id.c_str(), i + start, j, start_j);
|
|
|
|
}
|
|
|
|
dump_const(f, init.data.extract(i*mem.width+start_j, width));
|
|
|
|
f << stringf(";\n");
|
|
|
|
}
|
2020-10-17 14:48:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
f << stringf("%s" "end\n", indent.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a map : "edge clk" -> expressions within that clock domain
|
|
|
|
dict<std::string, std::vector<std::string>> clk_to_lof_body;
|
2021-05-27 10:50:59 -05:00
|
|
|
dict<std::string, std::string> clk_to_arst_cond;
|
|
|
|
dict<std::string, std::vector<std::string>> clk_to_arst_body;
|
2020-10-17 14:48:38 -05:00
|
|
|
clk_to_lof_body[""] = std::vector<std::string>();
|
|
|
|
std::string clk_domain_str;
|
|
|
|
// create a list of reg declarations
|
|
|
|
std::vector<std::string> lof_reg_declarations;
|
|
|
|
|
|
|
|
// read ports
|
|
|
|
for (auto &port : mem.rd_ports)
|
|
|
|
{
|
|
|
|
if (port.clk_enable)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::ostringstream os;
|
|
|
|
dump_sigspec(os, port.clk);
|
|
|
|
clk_domain_str = stringf("%sedge %s", port.clk_polarity ? "pos" : "neg", os.str().c_str());
|
2021-05-27 10:50:59 -05:00
|
|
|
if (port.arst != State::S0) {
|
|
|
|
std::ostringstream os2;
|
|
|
|
dump_sigspec(os2, port.arst);
|
|
|
|
clk_domain_str += stringf(", posedge %s", os2.str().c_str());
|
|
|
|
clk_to_arst_cond[clk_domain_str] = os2.str();
|
|
|
|
}
|
2020-10-17 14:48:38 -05:00
|
|
|
}
|
2021-05-25 16:42:58 -05:00
|
|
|
|
|
|
|
// Decide how to represent the transparency; same idea as Mem::extract_rdff.
|
2021-07-31 16:21:37 -05:00
|
|
|
bool trans_use_addr = true;
|
|
|
|
for (auto bit : port.transparency_mask)
|
|
|
|
if (!bit)
|
|
|
|
trans_use_addr = false;
|
2021-05-25 16:42:58 -05:00
|
|
|
|
|
|
|
if (GetSize(mem.wr_ports) == 0)
|
|
|
|
trans_use_addr = false;
|
|
|
|
|
|
|
|
if (port.en != State::S1 || port.srst != State::S0 || port.arst != State::S0 || !port.init_value.is_fully_undef())
|
|
|
|
trans_use_addr = false;
|
|
|
|
|
|
|
|
if (!trans_use_addr)
|
2020-10-17 14:48:38 -05:00
|
|
|
{
|
|
|
|
// for clocked read ports make something like:
|
|
|
|
// reg [..] temp_id;
|
|
|
|
// always @(posedge clk)
|
|
|
|
// if (rd_en) temp_id <= array_reg[r_addr];
|
|
|
|
// assign r_data = temp_id;
|
|
|
|
std::string temp_id = next_auto_id();
|
|
|
|
lof_reg_declarations.push_back( stringf("reg [%d:0] %s;\n", port.data.size() - 1, temp_id.c_str()) );
|
2021-05-25 18:18:29 -05:00
|
|
|
|
2021-05-27 10:50:59 -05:00
|
|
|
bool has_indent = false;
|
2021-05-25 18:18:29 -05:00
|
|
|
|
2021-05-27 10:50:59 -05:00
|
|
|
if (port.arst != State::S0) {
|
|
|
|
std::ostringstream os;
|
|
|
|
os << stringf("%s <= ", temp_id.c_str());
|
|
|
|
dump_sigspec(os, port.arst_value);
|
|
|
|
os << ";\n";
|
|
|
|
clk_to_arst_body[clk_domain_str].push_back(os.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (port.srst != State::S0 && !port.ce_over_srst) {
|
|
|
|
std::ostringstream os;
|
|
|
|
os << stringf("if (");
|
|
|
|
dump_sigspec(os, port.srst);
|
|
|
|
os << stringf(")\n");
|
|
|
|
clk_to_lof_body[clk_domain_str].push_back(os.str());
|
|
|
|
std::ostringstream os2;
|
|
|
|
os2 << stringf("%s" "%s <= ", indent.c_str(), temp_id.c_str());
|
|
|
|
dump_sigspec(os2, port.srst_value);
|
|
|
|
os2 << ";\n";
|
|
|
|
clk_to_lof_body[clk_domain_str].push_back(os2.str());
|
|
|
|
std::ostringstream os3;
|
|
|
|
if (port.en == State::S1) {
|
|
|
|
os3 << "else begin\n";
|
|
|
|
} else {
|
|
|
|
os3 << "else if (";
|
|
|
|
dump_sigspec(os3, port.en);
|
|
|
|
os3 << ") begin\n";
|
|
|
|
}
|
|
|
|
clk_to_lof_body[clk_domain_str].push_back(os3.str());
|
|
|
|
has_indent = true;
|
|
|
|
} else if (port.en != State::S1) {
|
2020-10-17 14:48:38 -05:00
|
|
|
std::ostringstream os;
|
2021-05-25 18:18:29 -05:00
|
|
|
os << stringf("if (");
|
|
|
|
dump_sigspec(os, port.en);
|
|
|
|
os << stringf(") begin\n");
|
|
|
|
clk_to_lof_body[clk_domain_str].push_back(os.str());
|
2021-05-27 10:50:59 -05:00
|
|
|
has_indent = true;
|
2021-05-25 18:18:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int sub = 0; sub < (1 << port.wide_log2); sub++)
|
|
|
|
{
|
|
|
|
SigSpec addr = port.sub_addr(sub);
|
|
|
|
std::ostringstream os;
|
2021-05-27 10:50:59 -05:00
|
|
|
if (has_indent)
|
2021-05-25 18:18:29 -05:00
|
|
|
os << indent;
|
|
|
|
os << temp_id;
|
|
|
|
if (port.wide_log2)
|
|
|
|
os << stringf("[%d:%d]", (sub + 1) * mem.width - 1, sub * mem.width);
|
|
|
|
os << stringf(" <= %s[", mem_id.c_str());
|
|
|
|
dump_sigspec(os, addr);
|
2020-10-17 14:48:38 -05:00
|
|
|
os << stringf("];\n");
|
|
|
|
clk_to_lof_body[clk_domain_str].push_back(os.str());
|
|
|
|
}
|
2021-05-25 18:18:29 -05:00
|
|
|
|
2021-05-25 16:42:58 -05:00
|
|
|
for (int i = 0; i < GetSize(mem.wr_ports); i++) {
|
|
|
|
auto &wport = mem.wr_ports[i];
|
2021-07-31 16:21:37 -05:00
|
|
|
if (!port.transparency_mask[i] && !port.collision_x_mask[i])
|
2021-05-25 16:42:58 -05:00
|
|
|
continue;
|
|
|
|
int min_wide_log2 = std::min(port.wide_log2, wport.wide_log2);
|
|
|
|
int max_wide_log2 = std::max(port.wide_log2, wport.wide_log2);
|
|
|
|
bool wide_write = wport.wide_log2 > port.wide_log2;
|
|
|
|
for (int sub = 0; sub < (1 << max_wide_log2); sub += (1 << min_wide_log2)) {
|
|
|
|
SigSpec raddr = port.addr;
|
|
|
|
SigSpec waddr = wport.addr;
|
|
|
|
if (wide_write)
|
|
|
|
waddr = wport.sub_addr(sub);
|
|
|
|
else
|
|
|
|
raddr = port.sub_addr(sub);
|
|
|
|
int pos = 0;
|
|
|
|
int ewidth = mem.width << min_wide_log2;
|
|
|
|
int wsub = wide_write ? sub : 0;
|
|
|
|
int rsub = wide_write ? 0 : sub;
|
|
|
|
while (pos < ewidth) {
|
|
|
|
int epos = pos;
|
|
|
|
while (epos < ewidth && wport.en[epos + wsub * mem.width] == wport.en[pos + wsub * mem.width])
|
|
|
|
epos++;
|
|
|
|
|
|
|
|
std::ostringstream os;
|
|
|
|
if (has_indent)
|
|
|
|
os << indent;
|
|
|
|
os << "if (";
|
|
|
|
dump_sigspec(os, wport.en[pos + wsub * mem.width]);
|
|
|
|
if (raddr != waddr) {
|
|
|
|
os << " && ";
|
|
|
|
dump_sigspec(os, raddr);
|
|
|
|
os << " == ";
|
|
|
|
dump_sigspec(os, waddr);
|
|
|
|
}
|
|
|
|
os << ")\n";
|
|
|
|
clk_to_lof_body[clk_domain_str].push_back(os.str());
|
|
|
|
|
|
|
|
std::ostringstream os2;
|
|
|
|
if (has_indent)
|
|
|
|
os2 << indent;
|
|
|
|
os2 << indent;
|
|
|
|
os2 << temp_id;
|
|
|
|
if (epos-pos != GetSize(port.data))
|
|
|
|
os2 << stringf("[%d:%d]", rsub * mem.width + epos-1, rsub * mem.width + pos);
|
|
|
|
os2 << " <= ";
|
2021-07-31 16:21:37 -05:00
|
|
|
if (port.transparency_mask[i])
|
|
|
|
dump_sigspec(os2, wport.data.extract(wsub * mem.width + pos, epos-pos));
|
|
|
|
else
|
|
|
|
dump_sigspec(os2, Const(State::Sx, epos - pos));
|
2021-05-25 16:42:58 -05:00
|
|
|
os2 << ";\n";
|
|
|
|
clk_to_lof_body[clk_domain_str].push_back(os2.str());
|
|
|
|
|
|
|
|
pos = epos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-27 10:50:59 -05:00
|
|
|
if (port.srst != State::S0 && port.ce_over_srst)
|
|
|
|
{
|
|
|
|
std::ostringstream os;
|
|
|
|
if (has_indent)
|
|
|
|
os << indent;
|
|
|
|
os << stringf("if (");
|
|
|
|
dump_sigspec(os, port.srst);
|
|
|
|
os << stringf(")\n");
|
|
|
|
clk_to_lof_body[clk_domain_str].push_back(os.str());
|
|
|
|
std::ostringstream os2;
|
|
|
|
if (has_indent)
|
|
|
|
os2 << indent;
|
|
|
|
os2 << stringf("%s" "%s <= ", indent.c_str(), temp_id.c_str());
|
|
|
|
dump_sigspec(os2, port.srst_value);
|
|
|
|
os2 << ";\n";
|
|
|
|
clk_to_lof_body[clk_domain_str].push_back(os2.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (has_indent)
|
2021-05-25 18:18:29 -05:00
|
|
|
clk_to_lof_body[clk_domain_str].push_back("end\n");
|
|
|
|
|
2021-05-27 10:50:59 -05:00
|
|
|
if (!port.init_value.is_fully_undef())
|
|
|
|
{
|
|
|
|
std::ostringstream os;
|
|
|
|
dump_sigspec(os, port.init_value);
|
|
|
|
std::string line = stringf("initial %s = %s;\n", temp_id.c_str(), os.str().c_str());
|
|
|
|
clk_to_lof_body[""].push_back(line);
|
|
|
|
}
|
|
|
|
|
2020-10-17 14:48:38 -05:00
|
|
|
{
|
|
|
|
std::ostringstream os;
|
|
|
|
dump_sigspec(os, port.data);
|
|
|
|
std::string line = stringf("assign %s = %s;\n", os.str().c_str(), temp_id.c_str());
|
|
|
|
clk_to_lof_body[""].push_back(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// for rd-transparent read-ports make something like:
|
|
|
|
// reg [..] temp_id;
|
|
|
|
// always @(posedge clk)
|
|
|
|
// temp_id <= r_addr;
|
|
|
|
// assign r_data = array_reg[temp_id];
|
|
|
|
std::string temp_id = next_auto_id();
|
2021-05-25 18:18:29 -05:00
|
|
|
lof_reg_declarations.push_back( stringf("reg [%d:0] %s;\n", port.addr.size() - 1 - port.wide_log2, temp_id.c_str()) );
|
2020-10-17 14:48:38 -05:00
|
|
|
{
|
|
|
|
std::ostringstream os;
|
2021-05-25 18:18:29 -05:00
|
|
|
dump_sigspec(os, port.addr.extract_end(port.wide_log2));
|
2020-10-17 14:48:38 -05:00
|
|
|
std::string line = stringf("%s <= %s;\n", temp_id.c_str(), os.str().c_str());
|
|
|
|
clk_to_lof_body[clk_domain_str].push_back(line);
|
|
|
|
}
|
2021-05-25 18:18:29 -05:00
|
|
|
for (int sub = 0; sub < (1 << port.wide_log2); sub++)
|
2020-10-17 14:48:38 -05:00
|
|
|
{
|
|
|
|
std::ostringstream os;
|
2021-05-25 18:18:29 -05:00
|
|
|
os << "assign ";
|
|
|
|
dump_sigspec(os, port.data.extract(sub * mem.width, mem.width));
|
|
|
|
os << stringf(" = %s[", mem_id.c_str());;
|
|
|
|
if (port.wide_log2) {
|
|
|
|
Const addr_lo;
|
|
|
|
for (int i = 0; i < port.wide_log2; i++)
|
|
|
|
addr_lo.bits.push_back(State(sub >> i & 1));
|
|
|
|
os << "{";
|
|
|
|
os << temp_id;
|
|
|
|
os << ", ";
|
|
|
|
dump_const(os, addr_lo);
|
|
|
|
os << "}";
|
|
|
|
} else {
|
|
|
|
os << temp_id;
|
|
|
|
}
|
|
|
|
os << "];\n";
|
|
|
|
clk_to_lof_body[""].push_back(os.str());
|
2020-10-17 14:48:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// for non-clocked read-ports make something like:
|
|
|
|
// assign r_data = array_reg[r_addr];
|
2021-05-25 18:18:29 -05:00
|
|
|
for (int sub = 0; sub < (1 << port.wide_log2); sub++)
|
|
|
|
{
|
|
|
|
SigSpec addr = port.sub_addr(sub);
|
|
|
|
|
|
|
|
std::ostringstream os, os2;
|
|
|
|
dump_sigspec(os, port.data.extract(sub * mem.width, mem.width));
|
|
|
|
dump_sigspec(os2, addr);
|
|
|
|
std::string line = stringf("assign %s = %s[%s];\n", os.str().c_str(), mem_id.c_str(), os2.str().c_str());
|
|
|
|
clk_to_lof_body[""].push_back(line);
|
|
|
|
}
|
2020-10-17 14:48:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-25 15:37:03 -05:00
|
|
|
// Write ports. Those are messy because we try to preserve priority, as much as we can:
|
|
|
|
//
|
|
|
|
// 1. We split all ports into several disjoint processes.
|
|
|
|
// 2. If a port has priority over another port, the two ports need to share
|
|
|
|
// a process, so that priority can be reconstructed on the other end.
|
|
|
|
// 3. We want each process to be as small as possible, to avoid extra
|
|
|
|
// priorities inferred on the other end.
|
|
|
|
pool<int> wr_ports_done;
|
|
|
|
for (int ridx = 0; ridx < GetSize(mem.wr_ports); ridx++)
|
2020-10-17 14:48:38 -05:00
|
|
|
{
|
2021-05-25 15:37:03 -05:00
|
|
|
if (wr_ports_done.count(ridx))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto &root = mem.wr_ports[ridx];
|
|
|
|
|
|
|
|
// Start from a root.
|
|
|
|
pool<int> wr_ports_now;
|
|
|
|
wr_ports_now.insert(ridx);
|
|
|
|
|
|
|
|
// Transitively fill list of ports in this process by following priority edges.
|
|
|
|
while (true)
|
2020-10-17 14:48:38 -05:00
|
|
|
{
|
2021-05-25 15:37:03 -05:00
|
|
|
bool changed = false;
|
|
|
|
|
|
|
|
for (int i = 0; i < GetSize(mem.wr_ports); i++)
|
|
|
|
for (int j = 0; j < i; j++)
|
|
|
|
if (mem.wr_ports[i].priority_mask[j])
|
|
|
|
{
|
|
|
|
if (wr_ports_now.count(i) && !wr_ports_now.count(j)) {
|
|
|
|
wr_ports_now.insert(j);
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
if (!wr_ports_now.count(i) && wr_ports_now.count(j)) {
|
|
|
|
wr_ports_now.insert(i);
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!changed)
|
|
|
|
break;
|
2020-10-17 14:48:38 -05:00
|
|
|
}
|
|
|
|
|
2021-05-25 18:18:29 -05:00
|
|
|
if (root.clk_enable) {
|
|
|
|
f << stringf("%s" "always%s @(%sedge ", indent.c_str(), systemverilog ? "_ff" : "", root.clk_polarity ? "pos" : "neg");
|
|
|
|
dump_sigspec(f, root.clk);
|
|
|
|
f << ") begin\n";
|
|
|
|
} else {
|
|
|
|
f << stringf("%s" "always%s begin\n", indent.c_str(), systemverilog ? "_latch" : " @*");
|
|
|
|
}
|
2020-10-17 14:48:38 -05:00
|
|
|
|
2021-05-25 15:37:03 -05:00
|
|
|
for (int pidx = 0; pidx < GetSize(mem.wr_ports); pidx++)
|
|
|
|
{
|
|
|
|
if (!wr_ports_now.count(pidx))
|
2020-10-17 14:48:38 -05:00
|
|
|
continue;
|
2021-05-25 15:37:03 -05:00
|
|
|
wr_ports_done.insert(pidx);
|
2020-10-17 14:48:38 -05:00
|
|
|
|
2021-05-25 15:37:03 -05:00
|
|
|
auto &port = mem.wr_ports[pidx];
|
|
|
|
log_assert(port.clk_enable == root.clk_enable);
|
|
|
|
if (port.clk_enable) {
|
|
|
|
log_assert(port.clk == root.clk);
|
|
|
|
log_assert(port.clk_polarity == root.clk_polarity);
|
|
|
|
}
|
|
|
|
|
|
|
|
// make something like:
|
|
|
|
// always @(posedge clk)
|
|
|
|
// if (wr_en_bit) memid[w_addr][??] <= w_data[??];
|
|
|
|
// ...
|
2021-05-25 18:18:29 -05:00
|
|
|
for (int sub = 0; sub < (1 << port.wide_log2); sub++)
|
2020-10-17 14:48:38 -05:00
|
|
|
{
|
2021-05-25 18:18:29 -05:00
|
|
|
SigSpec addr = port.sub_addr(sub);
|
|
|
|
for (int i = 0; i < mem.width; i++)
|
|
|
|
{
|
|
|
|
int start_i = i, width = 1;
|
|
|
|
SigBit wen_bit = port.en[sub * mem.width + i];
|
2021-05-25 15:37:03 -05:00
|
|
|
|
2021-05-25 18:18:29 -05:00
|
|
|
while (i+1 < mem.width && active_sigmap(port.en[sub * mem.width + i+1]) == active_sigmap(wen_bit))
|
|
|
|
i++, width++;
|
2021-05-25 15:37:03 -05:00
|
|
|
|
2021-05-25 18:18:29 -05:00
|
|
|
if (wen_bit == State::S0)
|
|
|
|
continue;
|
2021-05-25 15:37:03 -05:00
|
|
|
|
2021-05-25 18:18:29 -05:00
|
|
|
f << stringf("%s%s", indent.c_str(), indent.c_str());
|
|
|
|
if (wen_bit != State::S1)
|
|
|
|
{
|
|
|
|
f << stringf("if (");
|
|
|
|
dump_sigspec(f, wen_bit);
|
|
|
|
f << stringf(")\n");
|
|
|
|
f << stringf("%s%s%s", indent.c_str(), indent.c_str(), indent.c_str());
|
|
|
|
}
|
|
|
|
f << stringf("%s[", mem_id.c_str());
|
|
|
|
dump_sigspec(f, addr);
|
|
|
|
if (width == GetSize(port.en))
|
|
|
|
f << stringf("] <= ");
|
|
|
|
else
|
|
|
|
f << stringf("][%d:%d] <= ", i, start_i);
|
|
|
|
dump_sigspec(f, port.data.extract(sub * mem.width + start_i, width));
|
|
|
|
f << stringf(";\n");
|
2021-05-25 15:37:03 -05:00
|
|
|
}
|
2020-10-17 14:48:38 -05:00
|
|
|
}
|
|
|
|
}
|
2021-05-25 15:37:03 -05:00
|
|
|
|
|
|
|
f << stringf("%s" "end\n", indent.c_str());
|
2020-10-17 14:48:38 -05:00
|
|
|
}
|
|
|
|
// Output Verilog that looks something like this:
|
|
|
|
// reg [..] _3_;
|
|
|
|
// always @(posedge CLK2) begin
|
|
|
|
// _3_ <= memory[D1ADDR];
|
|
|
|
// if (A1EN)
|
|
|
|
// memory[A1ADDR] <= A1DATA;
|
|
|
|
// if (A2EN)
|
|
|
|
// memory[A2ADDR] <= A2DATA;
|
|
|
|
// ...
|
|
|
|
// end
|
|
|
|
// always @(negedge CLK1) begin
|
|
|
|
// if (C1EN)
|
|
|
|
// memory[C1ADDR] <= C1DATA;
|
|
|
|
// end
|
|
|
|
// ...
|
|
|
|
// assign D1DATA = _3_;
|
|
|
|
// assign D2DATA <= memory[D2ADDR];
|
|
|
|
|
|
|
|
// the reg ... definitions
|
|
|
|
for(auto ® : lof_reg_declarations)
|
|
|
|
{
|
|
|
|
f << stringf("%s" "%s", indent.c_str(), reg.c_str());
|
|
|
|
}
|
|
|
|
// the block of expressions by clock domain
|
|
|
|
for(auto &pair : clk_to_lof_body)
|
|
|
|
{
|
|
|
|
std::string clk_domain = pair.first;
|
|
|
|
std::vector<std::string> lof_lines = pair.second;
|
|
|
|
if( clk_domain != "")
|
|
|
|
{
|
|
|
|
f << stringf("%s" "always%s @(%s) begin\n", indent.c_str(), systemverilog ? "_ff" : "", clk_domain.c_str());
|
2021-05-27 10:50:59 -05:00
|
|
|
bool has_arst = clk_to_arst_cond.count(clk_domain) != 0;
|
|
|
|
if (has_arst) {
|
|
|
|
f << stringf("%s%s" "if (%s) begin\n", indent.c_str(), indent.c_str(), clk_to_arst_cond[clk_domain].c_str());
|
|
|
|
for(auto &line : clk_to_arst_body[clk_domain])
|
|
|
|
f << stringf("%s%s%s" "%s", indent.c_str(), indent.c_str(), indent.c_str(), line.c_str());
|
|
|
|
f << stringf("%s%s" "end else begin\n", indent.c_str(), indent.c_str());
|
|
|
|
for(auto &line : lof_lines)
|
|
|
|
f << stringf("%s%s%s" "%s", indent.c_str(), indent.c_str(), indent.c_str(), line.c_str());
|
|
|
|
f << stringf("%s%s" "end\n", indent.c_str(), indent.c_str());
|
|
|
|
} else {
|
|
|
|
for(auto &line : lof_lines)
|
|
|
|
f << stringf("%s%s" "%s", indent.c_str(), indent.c_str(), line.c_str());
|
|
|
|
}
|
2020-10-17 14:48:38 -05:00
|
|
|
f << stringf("%s" "end\n", indent.c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// the non-clocked assignments
|
|
|
|
for(auto &line : lof_lines)
|
|
|
|
f << stringf("%s" "%s", indent.c_str(), line.c_str());
|
|
|
|
}
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
2014-08-23 06:54:21 -05:00
|
|
|
void dump_cell_expr_port(std::ostream &f, RTLIL::Cell *cell, std::string port, bool gen_signed = true)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
|
|
|
if (gen_signed && cell->parameters.count("\\" + port + "_SIGNED") > 0 && cell->parameters["\\" + port + "_SIGNED"].as_bool()) {
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("$signed(");
|
2014-07-31 09:38:54 -05:00
|
|
|
dump_sigspec(f, cell->getPort("\\" + port));
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(")");
|
2013-01-05 04:13:26 -06:00
|
|
|
} else
|
2014-07-31 09:38:54 -05:00
|
|
|
dump_sigspec(f, cell->getPort("\\" + port));
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string cellname(RTLIL::Cell *cell)
|
|
|
|
{
|
2020-06-23 16:46:00 -05:00
|
|
|
if (!norename && cell->name[0] == '$' && RTLIL::builtin_ff_cell_types().count(cell->type) && cell->hasPort(ID::Q) && !cell->type.in(ID($ff), ID($_FF_)))
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2020-04-02 11:51:32 -05:00
|
|
|
RTLIL::SigSpec sig = cell->getPort(ID::Q);
|
2014-10-10 09:59:44 -05:00
|
|
|
if (GetSize(sig) != 1 || sig.is_fully_const())
|
2013-01-05 04:13:26 -06:00
|
|
|
goto no_special_reg_name;
|
|
|
|
|
2014-07-25 07:23:31 -05:00
|
|
|
RTLIL::Wire *wire = sig[0].wire;
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
if (wire->name[0] != '\\')
|
|
|
|
goto no_special_reg_name;
|
|
|
|
|
2014-08-02 06:11:01 -05:00
|
|
|
std::string cell_name = wire->name.str();
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
size_t pos = cell_name.find('[');
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
cell_name = cell_name.substr(0, pos) + "_reg" + cell_name.substr(pos);
|
|
|
|
else
|
|
|
|
cell_name = cell_name + "_reg";
|
|
|
|
|
|
|
|
if (wire->width != 1)
|
2014-07-25 07:23:31 -05:00
|
|
|
cell_name += stringf("[%d]", wire->start_offset + sig[0].offset);
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
if (active_module && active_module->count_id(cell_name) > 0)
|
|
|
|
goto no_special_reg_name;
|
|
|
|
|
|
|
|
return id(cell_name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
no_special_reg_name:
|
|
|
|
return id(cell->name).c_str();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-23 06:54:21 -05:00
|
|
|
void dump_cell_expr_uniop(std::ostream &f, std::string indent, RTLIL::Cell *cell, std::string op)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s" "assign ", indent.c_str());
|
2020-03-12 14:57:01 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::Y));
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(" = %s ", op.c_str());
|
2023-12-20 20:02:39 -06:00
|
|
|
dump_attributes(f, "", cell->attributes, " ");
|
2013-01-05 04:13:26 -06:00
|
|
|
dump_cell_expr_port(f, cell, "A", true);
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(";\n");
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
2014-08-23 06:54:21 -05:00
|
|
|
void dump_cell_expr_binop(std::ostream &f, std::string indent, RTLIL::Cell *cell, std::string op)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s" "assign ", indent.c_str());
|
2020-03-12 14:57:01 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::Y));
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(" = ");
|
2013-01-05 04:13:26 -06:00
|
|
|
dump_cell_expr_port(f, cell, "A", true);
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(" %s ", op.c_str());
|
2023-12-20 20:02:39 -06:00
|
|
|
dump_attributes(f, "", cell->attributes, " ");
|
2013-01-05 04:13:26 -06:00
|
|
|
dump_cell_expr_port(f, cell, "B", true);
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(";\n");
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
2023-06-27 20:51:33 -05:00
|
|
|
void dump_cell_expr_print(std::ostream &f, std::string indent, const RTLIL::Cell *cell)
|
2023-06-27 20:51:32 -05:00
|
|
|
{
|
2024-01-11 03:39:28 -06:00
|
|
|
Fmt fmt;
|
2023-06-27 20:51:32 -05:00
|
|
|
fmt.parse_rtlil(cell);
|
|
|
|
std::vector<VerilogFmtArg> args = fmt.emit_verilog();
|
|
|
|
|
|
|
|
f << stringf("%s" "$write(", indent.c_str());
|
|
|
|
bool first = true;
|
|
|
|
for (auto &arg : args) {
|
|
|
|
if (first) {
|
|
|
|
first = false;
|
|
|
|
} else {
|
|
|
|
f << ", ";
|
|
|
|
}
|
|
|
|
switch (arg.type) {
|
|
|
|
case VerilogFmtArg::STRING:
|
|
|
|
dump_const(f, RTLIL::Const(arg.str));
|
|
|
|
break;
|
|
|
|
case VerilogFmtArg::INTEGER:
|
|
|
|
f << (arg.signed_ ? "$signed(" : "$unsigned(");
|
|
|
|
dump_sigspec(f, arg.sig);
|
|
|
|
f << ")";
|
|
|
|
break;
|
|
|
|
case VerilogFmtArg::TIME:
|
|
|
|
if (arg.realtime)
|
|
|
|
f << "$realtime";
|
|
|
|
else
|
|
|
|
f << "$time";
|
|
|
|
break;
|
|
|
|
default: log_abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f << stringf(");\n");
|
|
|
|
}
|
|
|
|
|
2024-01-11 03:39:28 -06:00
|
|
|
void dump_cell_expr_check(std::ostream &f, std::string indent, const RTLIL::Cell *cell)
|
|
|
|
{
|
|
|
|
std::string flavor = cell->getParam(ID(FLAVOR)).decode_string();
|
|
|
|
if (flavor == "assert")
|
|
|
|
f << stringf("%s" "assert (", indent.c_str());
|
|
|
|
else if (flavor == "assume")
|
|
|
|
f << stringf("%s" "assume (", indent.c_str());
|
|
|
|
else if (flavor == "live")
|
|
|
|
f << stringf("%s" "assert (eventually ", indent.c_str());
|
|
|
|
else if (flavor == "fair")
|
|
|
|
f << stringf("%s" "assume (eventually ", indent.c_str());
|
|
|
|
else if (flavor == "cover")
|
|
|
|
f << stringf("%s" "cover (", indent.c_str());
|
|
|
|
dump_sigspec(f, cell->getPort(ID::A));
|
|
|
|
f << stringf(");\n");
|
|
|
|
}
|
|
|
|
|
2014-08-23 06:54:21 -05:00
|
|
|
bool dump_cell_expr(std::ostream &f, std::string indent, RTLIL::Cell *cell)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->type == ID($_NOT_)) {
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s" "assign ", indent.c_str());
|
2020-03-12 14:57:01 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::Y));
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(" = ");
|
|
|
|
f << stringf("~");
|
2023-12-20 20:02:39 -06:00
|
|
|
dump_attributes(f, "", cell->attributes, " ");
|
2013-01-05 04:13:26 -06:00
|
|
|
dump_cell_expr_port(f, cell, "A", false);
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(";\n");
|
2013-01-05 04:13:26 -06:00
|
|
|
return true;
|
|
|
|
}
|
2024-01-25 11:00:05 -06:00
|
|
|
|
|
|
|
if (cell->type == ID($_BUF_)) {
|
|
|
|
f << stringf("%s" "assign ", indent.c_str());
|
|
|
|
dump_sigspec(f, cell->getPort(ID::Y));
|
|
|
|
f << stringf(" = ");
|
|
|
|
dump_cell_expr_port(f, cell, "A", false);
|
|
|
|
f << stringf(";\n");
|
|
|
|
return true;
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->type.in(ID($_AND_), ID($_NAND_), ID($_OR_), ID($_NOR_), ID($_XOR_), ID($_XNOR_), ID($_ANDNOT_), ID($_ORNOT_))) {
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s" "assign ", indent.c_str());
|
2020-03-12 14:57:01 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::Y));
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(" = ");
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->type.in(ID($_NAND_), ID($_NOR_), ID($_XNOR_)))
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("~(");
|
2013-01-05 04:13:26 -06:00
|
|
|
dump_cell_expr_port(f, cell, "A", false);
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(" ");
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->type.in(ID($_AND_), ID($_NAND_), ID($_ANDNOT_)))
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("&");
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->type.in(ID($_OR_), ID($_NOR_), ID($_ORNOT_)))
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("|");
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->type.in(ID($_XOR_), ID($_XNOR_)))
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("^");
|
2023-12-20 20:02:39 -06:00
|
|
|
dump_attributes(f, "", cell->attributes, " ");
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(" ");
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->type.in(ID($_ANDNOT_), ID($_ORNOT_)))
|
2017-05-17 02:08:29 -05:00
|
|
|
f << stringf("~(");
|
2013-01-05 04:13:26 -06:00
|
|
|
dump_cell_expr_port(f, cell, "B", false);
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->type.in(ID($_NAND_), ID($_NOR_), ID($_XNOR_), ID($_ANDNOT_), ID($_ORNOT_)))
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(")");
|
|
|
|
f << stringf(";\n");
|
2013-01-05 04:13:26 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->type == ID($_MUX_)) {
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s" "assign ", indent.c_str());
|
2020-03-12 14:57:01 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::Y));
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(" = ");
|
2013-01-05 04:13:26 -06:00
|
|
|
dump_cell_expr_port(f, cell, "S", false);
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(" ? ");
|
2023-12-20 20:02:39 -06:00
|
|
|
dump_attributes(f, "", cell->attributes, " ");
|
2013-01-05 04:13:26 -06:00
|
|
|
dump_cell_expr_port(f, cell, "B", false);
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(" : ");
|
2013-01-05 04:13:26 -06:00
|
|
|
dump_cell_expr_port(f, cell, "A", false);
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(";\n");
|
2013-01-05 04:13:26 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->type == ID($_NMUX_)) {
|
2019-08-05 21:47:55 -05:00
|
|
|
f << stringf("%s" "assign ", indent.c_str());
|
2020-03-12 14:57:01 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::Y));
|
2019-08-05 21:47:55 -05:00
|
|
|
f << stringf(" = !(");
|
|
|
|
dump_cell_expr_port(f, cell, "S", false);
|
|
|
|
f << stringf(" ? ");
|
2023-12-20 20:02:39 -06:00
|
|
|
dump_attributes(f, "", cell->attributes, " ");
|
2019-08-05 21:47:55 -05:00
|
|
|
dump_cell_expr_port(f, cell, "B", false);
|
|
|
|
f << stringf(" : ");
|
|
|
|
dump_cell_expr_port(f, cell, "A", false);
|
|
|
|
f << stringf(");\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->type.in(ID($_AOI3_), ID($_OAI3_))) {
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s" "assign ", indent.c_str());
|
2020-03-12 14:57:01 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::Y));
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(" = ~((");
|
2014-08-16 11:18:30 -05:00
|
|
|
dump_cell_expr_port(f, cell, "A", false);
|
2020-04-02 11:51:32 -05:00
|
|
|
f << stringf(cell->type == ID($_AOI3_) ? " & " : " | ");
|
2014-08-16 11:18:30 -05:00
|
|
|
dump_cell_expr_port(f, cell, "B", false);
|
2020-04-02 11:51:32 -05:00
|
|
|
f << stringf(cell->type == ID($_AOI3_) ? ") |" : ") &");
|
2023-12-20 20:02:39 -06:00
|
|
|
dump_attributes(f, "", cell->attributes, " ");
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(" ");
|
2014-08-16 11:18:30 -05:00
|
|
|
dump_cell_expr_port(f, cell, "C", false);
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(");\n");
|
2014-08-16 11:18:30 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->type.in(ID($_AOI4_), ID($_OAI4_))) {
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s" "assign ", indent.c_str());
|
2020-03-12 14:57:01 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::Y));
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(" = ~((");
|
2014-08-16 11:18:30 -05:00
|
|
|
dump_cell_expr_port(f, cell, "A", false);
|
2020-04-02 11:51:32 -05:00
|
|
|
f << stringf(cell->type == ID($_AOI4_) ? " & " : " | ");
|
2014-08-16 11:18:30 -05:00
|
|
|
dump_cell_expr_port(f, cell, "B", false);
|
2020-04-02 11:51:32 -05:00
|
|
|
f << stringf(cell->type == ID($_AOI4_) ? ") |" : ") &");
|
2023-12-20 20:02:39 -06:00
|
|
|
dump_attributes(f, "", cell->attributes, " ");
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(" (");
|
2014-08-16 11:18:30 -05:00
|
|
|
dump_cell_expr_port(f, cell, "C", false);
|
2020-04-02 11:51:32 -05:00
|
|
|
f << stringf(cell->type == ID($_AOI4_) ? " & " : " | ");
|
2014-08-16 11:18:30 -05:00
|
|
|
dump_cell_expr_port(f, cell, "D", false);
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("));\n");
|
2014-08-16 11:18:30 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
#define HANDLE_UNIOP(_type, _operator) \
|
|
|
|
if (cell->type ==_type) { dump_cell_expr_uniop(f, indent, cell, _operator); return true; }
|
|
|
|
#define HANDLE_BINOP(_type, _operator) \
|
|
|
|
if (cell->type ==_type) { dump_cell_expr_binop(f, indent, cell, _operator); return true; }
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
HANDLE_UNIOP(ID($not), "~")
|
|
|
|
HANDLE_UNIOP(ID($pos), "+")
|
|
|
|
HANDLE_UNIOP(ID($neg), "-")
|
|
|
|
|
|
|
|
HANDLE_BINOP(ID($and), "&")
|
|
|
|
HANDLE_BINOP(ID($or), "|")
|
|
|
|
HANDLE_BINOP(ID($xor), "^")
|
|
|
|
HANDLE_BINOP(ID($xnor), "~^")
|
|
|
|
|
|
|
|
HANDLE_UNIOP(ID($reduce_and), "&")
|
|
|
|
HANDLE_UNIOP(ID($reduce_or), "|")
|
|
|
|
HANDLE_UNIOP(ID($reduce_xor), "^")
|
|
|
|
HANDLE_UNIOP(ID($reduce_xnor), "~^")
|
|
|
|
HANDLE_UNIOP(ID($reduce_bool), "|")
|
|
|
|
|
|
|
|
HANDLE_BINOP(ID($shl), "<<")
|
|
|
|
HANDLE_BINOP(ID($shr), ">>")
|
|
|
|
HANDLE_BINOP(ID($sshl), "<<<")
|
|
|
|
HANDLE_BINOP(ID($sshr), ">>>")
|
|
|
|
|
|
|
|
HANDLE_BINOP(ID($lt), "<")
|
|
|
|
HANDLE_BINOP(ID($le), "<=")
|
|
|
|
HANDLE_BINOP(ID($eq), "==")
|
|
|
|
HANDLE_BINOP(ID($ne), "!=")
|
|
|
|
HANDLE_BINOP(ID($eqx), "===")
|
|
|
|
HANDLE_BINOP(ID($nex), "!==")
|
|
|
|
HANDLE_BINOP(ID($ge), ">=")
|
|
|
|
HANDLE_BINOP(ID($gt), ">")
|
|
|
|
|
|
|
|
HANDLE_BINOP(ID($add), "+")
|
|
|
|
HANDLE_BINOP(ID($sub), "-")
|
|
|
|
HANDLE_BINOP(ID($mul), "*")
|
|
|
|
HANDLE_BINOP(ID($div), "/")
|
|
|
|
HANDLE_BINOP(ID($mod), "%")
|
|
|
|
HANDLE_BINOP(ID($pow), "**")
|
|
|
|
|
|
|
|
HANDLE_UNIOP(ID($logic_not), "!")
|
|
|
|
HANDLE_BINOP(ID($logic_and), "&&")
|
|
|
|
HANDLE_BINOP(ID($logic_or), "||")
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
#undef HANDLE_UNIOP
|
|
|
|
#undef HANDLE_BINOP
|
|
|
|
|
2020-04-21 05:51:58 -05:00
|
|
|
if (cell->type == ID($divfloor))
|
|
|
|
{
|
|
|
|
// wire [MAXLEN+1:0] _0_, _1_, _2_;
|
|
|
|
// assign _0_ = $signed(A);
|
|
|
|
// assign _1_ = $signed(B);
|
|
|
|
// assign _2_ = (A[-1] == B[-1]) || A == 0 ? _0_ : $signed(_0_ - (B[-1] ? _1_ + 1 : _1_ - 1));
|
|
|
|
// assign Y = $signed(_2_) / $signed(_1_);
|
|
|
|
|
|
|
|
if (cell->getParam(ID::A_SIGNED).as_bool() && cell->getParam(ID::B_SIGNED).as_bool()) {
|
|
|
|
SigSpec sig_a = cell->getPort(ID::A);
|
|
|
|
SigSpec sig_b = cell->getPort(ID::B);
|
|
|
|
|
|
|
|
std::string buf_a = next_auto_id();
|
|
|
|
std::string buf_b = next_auto_id();
|
|
|
|
std::string buf_num = next_auto_id();
|
|
|
|
int size_a = GetSize(sig_a);
|
|
|
|
int size_b = GetSize(sig_b);
|
|
|
|
int size_y = GetSize(cell->getPort(ID::Y));
|
|
|
|
int size_max = std::max(size_a, std::max(size_b, size_y));
|
|
|
|
|
|
|
|
// intentionally one wider than maximum width
|
|
|
|
f << stringf("%s" "wire [%d:0] %s, %s, %s;\n", indent.c_str(), size_max, buf_a.c_str(), buf_b.c_str(), buf_num.c_str());
|
|
|
|
f << stringf("%s" "assign %s = ", indent.c_str(), buf_a.c_str());
|
|
|
|
dump_cell_expr_port(f, cell, "A", true);
|
|
|
|
f << stringf(";\n");
|
|
|
|
f << stringf("%s" "assign %s = ", indent.c_str(), buf_b.c_str());
|
|
|
|
dump_cell_expr_port(f, cell, "B", true);
|
|
|
|
f << stringf(";\n");
|
|
|
|
|
|
|
|
f << stringf("%s" "assign %s = ", indent.c_str(), buf_num.c_str());
|
|
|
|
f << stringf("(");
|
|
|
|
dump_sigspec(f, sig_a.extract(sig_a.size()-1));
|
|
|
|
f << stringf(" == ");
|
|
|
|
dump_sigspec(f, sig_b.extract(sig_b.size()-1));
|
|
|
|
f << stringf(") || ");
|
|
|
|
dump_sigspec(f, sig_a);
|
|
|
|
f << stringf(" == 0 ? %s : ", buf_a.c_str());
|
|
|
|
f << stringf("$signed(%s - (", buf_a.c_str());
|
|
|
|
dump_sigspec(f, sig_b.extract(sig_b.size()-1));
|
|
|
|
f << stringf(" ? %s + 1 : %s - 1));\n", buf_b.c_str(), buf_b.c_str());
|
|
|
|
|
|
|
|
|
|
|
|
f << stringf("%s" "assign ", indent.c_str());
|
|
|
|
dump_sigspec(f, cell->getPort(ID::Y));
|
|
|
|
f << stringf(" = $signed(%s) / ", buf_num.c_str());
|
2023-12-20 20:02:39 -06:00
|
|
|
dump_attributes(f, "", cell->attributes, " ");
|
2020-04-21 05:51:58 -05:00
|
|
|
f << stringf("$signed(%s);\n", buf_b.c_str());
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// same as truncating division
|
|
|
|
dump_cell_expr_binop(f, indent, cell, "/");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-08 12:30:47 -05:00
|
|
|
if (cell->type == ID($modfloor))
|
|
|
|
{
|
|
|
|
// wire truncated = $signed(A) % $signed(B);
|
2022-11-10 09:27:13 -06:00
|
|
|
// assign Y = (A[-1] == B[-1]) || truncated == 0 ? $signed(truncated) : $signed(B) + $signed(truncated);
|
2020-04-08 12:30:47 -05:00
|
|
|
|
|
|
|
if (cell->getParam(ID::A_SIGNED).as_bool() && cell->getParam(ID::B_SIGNED).as_bool()) {
|
|
|
|
SigSpec sig_a = cell->getPort(ID::A);
|
|
|
|
SigSpec sig_b = cell->getPort(ID::B);
|
|
|
|
|
|
|
|
std::string temp_id = next_auto_id();
|
|
|
|
f << stringf("%s" "wire [%d:0] %s = ", indent.c_str(), GetSize(cell->getPort(ID::A))-1, temp_id.c_str());
|
|
|
|
dump_cell_expr_port(f, cell, "A", true);
|
|
|
|
f << stringf(" %% ");
|
2023-12-20 20:02:39 -06:00
|
|
|
dump_attributes(f, "", cell->attributes, " ");
|
2020-04-08 12:30:47 -05:00
|
|
|
dump_cell_expr_port(f, cell, "B", true);
|
|
|
|
f << stringf(";\n");
|
|
|
|
|
|
|
|
f << stringf("%s" "assign ", indent.c_str());
|
|
|
|
dump_sigspec(f, cell->getPort(ID::Y));
|
|
|
|
f << stringf(" = (");
|
|
|
|
dump_sigspec(f, sig_a.extract(sig_a.size()-1));
|
|
|
|
f << stringf(" == ");
|
|
|
|
dump_sigspec(f, sig_b.extract(sig_b.size()-1));
|
2022-11-10 09:27:13 -06:00
|
|
|
f << stringf(") || %s == 0 ? $signed(%s) : ", temp_id.c_str(), temp_id.c_str());
|
2020-04-08 12:30:47 -05:00
|
|
|
dump_cell_expr_port(f, cell, "B", true);
|
|
|
|
f << stringf(" + $signed(%s);\n", temp_id.c_str());
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// same as truncating modulo
|
|
|
|
dump_cell_expr_binop(f, indent, cell, "%");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->type == ID($shift))
|
2018-12-16 12:46:32 -06:00
|
|
|
{
|
|
|
|
f << stringf("%s" "assign ", indent.c_str());
|
2020-03-12 14:57:01 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::Y));
|
2018-12-16 12:46:32 -06:00
|
|
|
f << stringf(" = ");
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->getParam(ID::B_SIGNED).as_bool())
|
2018-12-16 12:46:32 -06:00
|
|
|
{
|
2020-07-03 06:13:21 -05:00
|
|
|
dump_cell_expr_port(f, cell, "B", true);
|
2018-12-16 12:46:32 -06:00
|
|
|
f << stringf(" < 0 ? ");
|
2020-07-03 06:13:21 -05:00
|
|
|
dump_cell_expr_port(f, cell, "A", true);
|
2018-12-16 12:46:32 -06:00
|
|
|
f << stringf(" << - ");
|
2020-03-12 14:57:01 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::B));
|
2018-12-16 12:46:32 -06:00
|
|
|
f << stringf(" : ");
|
2020-07-03 06:13:21 -05:00
|
|
|
dump_cell_expr_port(f, cell, "A", true);
|
2018-12-16 12:46:32 -06:00
|
|
|
f << stringf(" >> ");
|
2020-03-12 14:57:01 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::B));
|
2018-12-16 12:46:32 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-03 06:13:21 -05:00
|
|
|
dump_cell_expr_port(f, cell, "A", true);
|
2018-12-16 12:46:32 -06:00
|
|
|
f << stringf(" >> ");
|
2020-03-12 14:57:01 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::B));
|
2018-12-16 12:46:32 -06:00
|
|
|
}
|
|
|
|
f << stringf(";\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->type == ID($shiftx))
|
2017-10-07 06:40:54 -05:00
|
|
|
{
|
2019-01-15 03:55:27 -06:00
|
|
|
std::string temp_id = next_auto_id();
|
2020-03-12 14:57:01 -05:00
|
|
|
f << stringf("%s" "wire [%d:0] %s = ", indent.c_str(), GetSize(cell->getPort(ID::A))-1, temp_id.c_str());
|
|
|
|
dump_sigspec(f, cell->getPort(ID::A));
|
2019-01-15 03:55:27 -06:00
|
|
|
f << stringf(";\n");
|
|
|
|
|
2017-10-07 06:40:54 -05:00
|
|
|
f << stringf("%s" "assign ", indent.c_str());
|
2020-03-12 14:57:01 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::Y));
|
2019-01-15 03:55:27 -06:00
|
|
|
f << stringf(" = %s[", temp_id.c_str());
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->getParam(ID::B_SIGNED).as_bool())
|
2017-10-07 06:40:54 -05:00
|
|
|
f << stringf("$signed(");
|
2020-03-12 14:57:01 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::B));
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->getParam(ID::B_SIGNED).as_bool())
|
2017-10-07 06:40:54 -05:00
|
|
|
f << stringf(")");
|
2020-04-02 11:51:32 -05:00
|
|
|
f << stringf(" +: %d", cell->getParam(ID::Y_WIDTH).as_int());
|
2017-10-07 06:40:54 -05:00
|
|
|
f << stringf("];\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->type == ID($mux))
|
2014-08-02 14:10:08 -05:00
|
|
|
{
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s" "assign ", indent.c_str());
|
2020-03-12 14:57:01 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::Y));
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(" = ");
|
2020-03-12 14:57:01 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::S));
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(" ? ");
|
2023-12-20 20:02:39 -06:00
|
|
|
dump_attributes(f, "", cell->attributes, " ");
|
2020-03-12 14:57:01 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::B));
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(" : ");
|
2020-03-12 14:57:01 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::A));
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(";\n");
|
2014-08-02 14:10:08 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->type == ID($pmux))
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2020-04-02 11:51:32 -05:00
|
|
|
int width = cell->parameters[ID::WIDTH].as_int();
|
2020-03-12 14:57:01 -05:00
|
|
|
int s_width = cell->getPort(ID::S).size();
|
2014-07-19 19:16:30 -05:00
|
|
|
std::string func_name = cellname(cell);
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s" "function [%d:0] %s;\n", indent.c_str(), width-1, func_name.c_str());
|
|
|
|
f << stringf("%s" " input [%d:0] a;\n", indent.c_str(), width-1);
|
|
|
|
f << stringf("%s" " input [%d:0] b;\n", indent.c_str(), s_width*width-1);
|
|
|
|
f << stringf("%s" " input [%d:0] s;\n", indent.c_str(), s_width-1);
|
2014-07-19 19:16:30 -05:00
|
|
|
|
|
|
|
dump_attributes(f, indent + " ", cell->attributes);
|
2022-11-10 09:16:37 -06:00
|
|
|
if (noparallelcase)
|
|
|
|
f << stringf("%s" " case (s)\n", indent.c_str());
|
|
|
|
else {
|
|
|
|
if (!noattr)
|
|
|
|
f << stringf("%s" " (* parallel_case *)\n", indent.c_str());
|
|
|
|
f << stringf("%s" " casez (s)", indent.c_str());
|
|
|
|
f << stringf(noattr ? " // synopsys parallel_case\n" : "\n");
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
for (int i = 0; i < s_width; i++)
|
|
|
|
{
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s" " %d'b", indent.c_str(), s_width);
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
for (int j = s_width-1; j >= 0; j--)
|
2022-11-10 09:16:37 -06:00
|
|
|
f << stringf("%c", j == i ? '1' : noparallelcase ? '0' : '?');
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(":\n");
|
|
|
|
f << stringf("%s" " %s = b[%d:%d];\n", indent.c_str(), func_name.c_str(), (i+1)*width-1, i*width);
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
2022-11-10 09:16:37 -06:00
|
|
|
if (noparallelcase) {
|
|
|
|
f << stringf("%s" " %d'b", indent.c_str(), s_width);
|
|
|
|
for (int j = s_width-1; j >= 0; j--)
|
|
|
|
f << '0';
|
|
|
|
f << stringf(":\n");
|
|
|
|
} else
|
|
|
|
f << stringf("%s" " default:\n", indent.c_str());
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s" " %s = a;\n", indent.c_str(), func_name.c_str());
|
2022-11-10 09:16:37 -06:00
|
|
|
if (noparallelcase) {
|
|
|
|
f << stringf("%s" " default:\n", indent.c_str());
|
|
|
|
f << stringf("%s" " %s = %d'bx;\n", indent.c_str(), func_name.c_str(), width);
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s" " endcase\n", indent.c_str());
|
|
|
|
f << stringf("%s" "endfunction\n", indent.c_str());
|
2014-07-19 19:16:30 -05:00
|
|
|
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s" "assign ", indent.c_str());
|
2020-03-12 14:57:01 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::Y));
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(" = %s(", func_name.c_str());
|
2020-03-12 14:57:01 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::A));
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(", ");
|
2020-03-12 14:57:01 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::B));
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(", ");
|
2020-03-12 14:57:01 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::S));
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(");\n");
|
2013-01-05 04:13:26 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->type == ID($tribuf))
|
2019-01-26 18:21:31 -06:00
|
|
|
{
|
|
|
|
f << stringf("%s" "assign ", indent.c_str());
|
2020-03-12 14:57:01 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::Y));
|
2019-01-26 18:21:31 -06:00
|
|
|
f << stringf(" = ");
|
2020-04-02 11:51:32 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::EN));
|
2019-01-26 18:21:31 -06:00
|
|
|
f << stringf(" ? ");
|
2020-03-12 14:57:01 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::A));
|
2020-04-02 11:51:32 -05:00
|
|
|
f << stringf(" : %d'bz;\n", cell->parameters.at(ID::WIDTH).as_int());
|
2019-01-26 18:21:31 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->type == ID($slice))
|
2014-02-07 10:44:57 -06:00
|
|
|
{
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s" "assign ", indent.c_str());
|
2020-03-12 14:57:01 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::Y));
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(" = ");
|
2020-03-12 14:57:01 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::A));
|
2020-04-02 11:51:32 -05:00
|
|
|
f << stringf(" >> %d;\n", cell->parameters.at(ID::OFFSET).as_int());
|
2014-02-07 10:44:57 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->type == ID($concat))
|
2014-02-07 10:44:57 -06:00
|
|
|
{
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s" "assign ", indent.c_str());
|
2020-03-12 14:57:01 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::Y));
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(" = { ");
|
2020-03-12 14:57:01 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::B));
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(" , ");
|
2020-03-12 14:57:01 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::A));
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(" };\n");
|
2014-02-07 10:44:57 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->type == ID($lut))
|
2018-09-05 17:18:01 -05:00
|
|
|
{
|
|
|
|
f << stringf("%s" "assign ", indent.c_str());
|
2020-03-12 14:57:01 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::Y));
|
2018-09-05 17:18:01 -05:00
|
|
|
f << stringf(" = ");
|
2020-04-02 11:51:32 -05:00
|
|
|
dump_const(f, cell->parameters.at(ID::LUT));
|
2018-09-05 17:18:01 -05:00
|
|
|
f << stringf(" >> ");
|
2023-12-20 20:02:39 -06:00
|
|
|
dump_attributes(f, "", cell->attributes, " ");
|
2020-03-12 14:57:01 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::A));
|
2018-09-05 17:18:01 -05:00
|
|
|
f << stringf(";\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-23 16:46:00 -05:00
|
|
|
if (RTLIL::builtin_ff_cell_types().count(cell->type))
|
2015-03-18 02:01:37 -05:00
|
|
|
{
|
2020-06-23 16:46:00 -05:00
|
|
|
FfData ff(nullptr, cell);
|
2015-03-18 02:01:37 -05:00
|
|
|
|
2020-06-23 16:46:00 -05:00
|
|
|
// $ff / $_FF_ cell: not supported.
|
2021-10-01 16:50:48 -05:00
|
|
|
if (ff.has_gclk)
|
2020-06-23 16:46:00 -05:00
|
|
|
return false;
|
2015-03-18 02:01:37 -05:00
|
|
|
|
|
|
|
std::string reg_name = cellname(cell);
|
2020-06-23 16:46:00 -05:00
|
|
|
bool out_is_reg_wire = is_reg_wire(ff.sig_q, reg_name);
|
2015-03-18 02:01:37 -05:00
|
|
|
|
|
|
|
if (!out_is_reg_wire) {
|
2020-06-23 16:46:00 -05:00
|
|
|
if (ff.width == 1)
|
|
|
|
f << stringf("%s" "reg %s", indent.c_str(), reg_name.c_str());
|
|
|
|
else
|
|
|
|
f << stringf("%s" "reg [%d:0] %s", indent.c_str(), ff.width-1, reg_name.c_str());
|
|
|
|
dump_reg_init(f, ff.sig_q);
|
2016-11-16 05:00:39 -06:00
|
|
|
f << ";\n";
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2020-06-23 16:46:00 -05:00
|
|
|
// If the FF has CLR/SET inputs, emit every bit slice separately.
|
|
|
|
int chunks = ff.has_sr ? ff.width : 1;
|
|
|
|
bool chunky = ff.has_sr && ff.width != 1;
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2020-06-23 16:46:00 -05:00
|
|
|
for (int i = 0; i < chunks; i++)
|
|
|
|
{
|
2021-10-01 16:50:48 -05:00
|
|
|
SigSpec sig_d, sig_ad;
|
2020-06-23 16:46:00 -05:00
|
|
|
Const val_arst, val_srst;
|
2021-10-01 16:50:48 -05:00
|
|
|
std::string reg_bit_name, sig_set_name, sig_clr_name, sig_arst_name, sig_aload_name;
|
2020-06-23 16:46:00 -05:00
|
|
|
if (chunky) {
|
|
|
|
reg_bit_name = stringf("%s[%d]", reg_name.c_str(), i);
|
2021-10-01 16:50:48 -05:00
|
|
|
if (ff.has_gclk || ff.has_clk)
|
2020-06-23 16:46:00 -05:00
|
|
|
sig_d = ff.sig_d[i];
|
2021-10-01 16:50:48 -05:00
|
|
|
if (ff.has_aload)
|
|
|
|
sig_ad = ff.sig_ad[i];
|
2020-06-23 16:46:00 -05:00
|
|
|
} else {
|
|
|
|
reg_bit_name = reg_name;
|
2021-10-01 16:50:48 -05:00
|
|
|
sig_d = ff.sig_d;
|
|
|
|
sig_ad = ff.sig_ad;
|
2020-06-23 16:46:00 -05:00
|
|
|
}
|
|
|
|
if (ff.has_arst)
|
|
|
|
val_arst = chunky ? ff.val_arst[i] : ff.val_arst;
|
|
|
|
if (ff.has_srst)
|
|
|
|
val_srst = chunky ? ff.val_srst[i] : ff.val_srst;
|
2018-04-22 09:03:26 -05:00
|
|
|
|
2020-09-28 11:11:18 -05:00
|
|
|
// If there are constants in the sensitivity list, replace them with an intermediate wire
|
2021-10-01 16:50:48 -05:00
|
|
|
if (ff.has_clk) {
|
|
|
|
if (ff.has_sr) {
|
|
|
|
if (ff.sig_set[i].wire == NULL)
|
|
|
|
{
|
|
|
|
sig_set_name = next_auto_id();
|
|
|
|
f << stringf("%s" "wire %s = ", indent.c_str(), sig_set_name.c_str());
|
|
|
|
dump_const(f, ff.sig_set[i].data);
|
|
|
|
f << stringf(";\n");
|
|
|
|
}
|
|
|
|
if (ff.sig_clr[i].wire == NULL)
|
|
|
|
{
|
|
|
|
sig_clr_name = next_auto_id();
|
|
|
|
f << stringf("%s" "wire %s = ", indent.c_str(), sig_clr_name.c_str());
|
|
|
|
dump_const(f, ff.sig_clr[i].data);
|
|
|
|
f << stringf(";\n");
|
|
|
|
}
|
|
|
|
} else if (ff.has_arst) {
|
|
|
|
if (ff.sig_arst[0].wire == NULL)
|
|
|
|
{
|
|
|
|
sig_arst_name = next_auto_id();
|
|
|
|
f << stringf("%s" "wire %s = ", indent.c_str(), sig_arst_name.c_str());
|
|
|
|
dump_const(f, ff.sig_arst[0].data);
|
|
|
|
f << stringf(";\n");
|
|
|
|
}
|
|
|
|
} else if (ff.has_aload) {
|
|
|
|
if (ff.sig_aload[0].wire == NULL)
|
|
|
|
{
|
|
|
|
sig_aload_name = next_auto_id();
|
|
|
|
f << stringf("%s" "wire %s = ", indent.c_str(), sig_aload_name.c_str());
|
|
|
|
dump_const(f, ff.sig_aload[0].data);
|
|
|
|
f << stringf(";\n");
|
|
|
|
}
|
2020-09-28 11:11:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 16:46:00 -05:00
|
|
|
dump_attributes(f, indent, cell->attributes);
|
|
|
|
if (ff.has_clk)
|
|
|
|
{
|
|
|
|
// FFs.
|
|
|
|
f << stringf("%s" "always%s @(%sedge ", indent.c_str(), systemverilog ? "_ff" : "", ff.pol_clk ? "pos" : "neg");
|
|
|
|
dump_sigspec(f, ff.sig_clk);
|
|
|
|
if (ff.has_sr) {
|
|
|
|
f << stringf(", %sedge ", ff.pol_set ? "pos" : "neg");
|
2020-09-28 11:11:18 -05:00
|
|
|
if (ff.sig_set[i].wire == NULL)
|
|
|
|
f << stringf("%s", sig_set_name.c_str());
|
|
|
|
else
|
|
|
|
dump_sigspec(f, ff.sig_set[i]);
|
|
|
|
|
2020-06-23 16:46:00 -05:00
|
|
|
f << stringf(", %sedge ", ff.pol_clr ? "pos" : "neg");
|
2020-09-28 11:11:18 -05:00
|
|
|
if (ff.sig_clr[i].wire == NULL)
|
|
|
|
f << stringf("%s", sig_clr_name.c_str());
|
|
|
|
else
|
|
|
|
dump_sigspec(f, ff.sig_clr[i]);
|
2020-06-23 16:46:00 -05:00
|
|
|
} else if (ff.has_arst) {
|
|
|
|
f << stringf(", %sedge ", ff.pol_arst ? "pos" : "neg");
|
2021-10-01 16:50:48 -05:00
|
|
|
if (ff.sig_arst[0].wire == NULL)
|
2020-09-28 11:11:18 -05:00
|
|
|
f << stringf("%s", sig_arst_name.c_str());
|
|
|
|
else
|
|
|
|
dump_sigspec(f, ff.sig_arst);
|
2021-10-01 16:50:48 -05:00
|
|
|
} else if (ff.has_aload) {
|
|
|
|
f << stringf(", %sedge ", ff.pol_aload ? "pos" : "neg");
|
|
|
|
if (ff.sig_aload[0].wire == NULL)
|
|
|
|
f << stringf("%s", sig_aload_name.c_str());
|
|
|
|
else
|
|
|
|
dump_sigspec(f, ff.sig_aload);
|
2020-06-23 16:46:00 -05:00
|
|
|
}
|
|
|
|
f << stringf(")\n");
|
|
|
|
|
|
|
|
f << stringf("%s" " ", indent.c_str());
|
|
|
|
if (ff.has_sr) {
|
|
|
|
f << stringf("if (%s", ff.pol_clr ? "" : "!");
|
2020-09-28 11:11:18 -05:00
|
|
|
if (ff.sig_clr[i].wire == NULL)
|
|
|
|
f << stringf("%s", sig_clr_name.c_str());
|
|
|
|
else
|
|
|
|
dump_sigspec(f, ff.sig_clr[i]);
|
2020-06-23 16:46:00 -05:00
|
|
|
f << stringf(") %s <= 1'b0;\n", reg_bit_name.c_str());
|
|
|
|
f << stringf("%s" " else if (%s", indent.c_str(), ff.pol_set ? "" : "!");
|
2020-09-28 11:11:18 -05:00
|
|
|
if (ff.sig_set[i].wire == NULL)
|
|
|
|
f << stringf("%s", sig_set_name.c_str());
|
|
|
|
else
|
|
|
|
dump_sigspec(f, ff.sig_set[i]);
|
2020-06-23 16:46:00 -05:00
|
|
|
f << stringf(") %s <= 1'b1;\n", reg_bit_name.c_str());
|
|
|
|
f << stringf("%s" " else ", indent.c_str());
|
|
|
|
} else if (ff.has_arst) {
|
|
|
|
f << stringf("if (%s", ff.pol_arst ? "" : "!");
|
2021-10-01 16:50:48 -05:00
|
|
|
if (ff.sig_arst[0].wire == NULL)
|
2020-09-28 11:11:18 -05:00
|
|
|
f << stringf("%s", sig_arst_name.c_str());
|
|
|
|
else
|
|
|
|
dump_sigspec(f, ff.sig_arst);
|
2020-06-23 16:46:00 -05:00
|
|
|
f << stringf(") %s <= ", reg_bit_name.c_str());
|
|
|
|
dump_sigspec(f, val_arst);
|
|
|
|
f << stringf(";\n");
|
|
|
|
f << stringf("%s" " else ", indent.c_str());
|
2021-10-01 16:50:48 -05:00
|
|
|
} else if (ff.has_aload) {
|
|
|
|
f << stringf("if (%s", ff.pol_aload ? "" : "!");
|
|
|
|
if (ff.sig_aload[0].wire == NULL)
|
|
|
|
f << stringf("%s", sig_aload_name.c_str());
|
|
|
|
else
|
|
|
|
dump_sigspec(f, ff.sig_aload);
|
|
|
|
f << stringf(") %s <= ", reg_bit_name.c_str());
|
|
|
|
dump_sigspec(f, sig_ad);
|
|
|
|
f << stringf(";\n");
|
|
|
|
f << stringf("%s" " else ", indent.c_str());
|
2020-06-23 16:46:00 -05:00
|
|
|
}
|
2018-04-22 09:03:26 -05:00
|
|
|
|
2021-10-01 16:50:48 -05:00
|
|
|
if (ff.has_srst && ff.has_ce && ff.ce_over_srst) {
|
|
|
|
f << stringf("if (%s", ff.pol_ce ? "" : "!");
|
|
|
|
dump_sigspec(f, ff.sig_ce);
|
2020-06-23 16:46:00 -05:00
|
|
|
f << stringf(")\n");
|
|
|
|
f << stringf("%s" " if (%s", indent.c_str(), ff.pol_srst ? "" : "!");
|
|
|
|
dump_sigspec(f, ff.sig_srst);
|
|
|
|
f << stringf(") %s <= ", reg_bit_name.c_str());
|
|
|
|
dump_sigspec(f, val_srst);
|
|
|
|
f << stringf(";\n");
|
|
|
|
f << stringf("%s" " else ", indent.c_str());
|
|
|
|
} else {
|
|
|
|
if (ff.has_srst) {
|
|
|
|
f << stringf("if (%s", ff.pol_srst ? "" : "!");
|
|
|
|
dump_sigspec(f, ff.sig_srst);
|
|
|
|
f << stringf(") %s <= ", reg_bit_name.c_str());
|
|
|
|
dump_sigspec(f, val_srst);
|
|
|
|
f << stringf(";\n");
|
|
|
|
f << stringf("%s" " else ", indent.c_str());
|
|
|
|
}
|
2021-10-01 16:50:48 -05:00
|
|
|
if (ff.has_ce) {
|
|
|
|
f << stringf("if (%s", ff.pol_ce ? "" : "!");
|
|
|
|
dump_sigspec(f, ff.sig_ce);
|
2020-06-23 16:46:00 -05:00
|
|
|
f << stringf(") ");
|
|
|
|
}
|
|
|
|
}
|
2018-04-22 09:03:26 -05:00
|
|
|
|
2020-06-23 16:46:00 -05:00
|
|
|
f << stringf("%s <= ", reg_bit_name.c_str());
|
|
|
|
dump_sigspec(f, sig_d);
|
|
|
|
f << stringf(";\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Latches.
|
|
|
|
f << stringf("%s" "always%s\n", indent.c_str(), systemverilog ? "_latch" : " @*");
|
|
|
|
|
|
|
|
f << stringf("%s" " ", indent.c_str());
|
|
|
|
if (ff.has_sr) {
|
|
|
|
f << stringf("if (%s", ff.pol_clr ? "" : "!");
|
|
|
|
dump_sigspec(f, ff.sig_clr[i]);
|
|
|
|
f << stringf(") %s = 1'b0;\n", reg_bit_name.c_str());
|
|
|
|
f << stringf("%s" " else if (%s", indent.c_str(), ff.pol_set ? "" : "!");
|
|
|
|
dump_sigspec(f, ff.sig_set[i]);
|
|
|
|
f << stringf(") %s = 1'b1;\n", reg_bit_name.c_str());
|
2021-10-01 16:50:48 -05:00
|
|
|
if (ff.has_aload)
|
2020-06-23 16:46:00 -05:00
|
|
|
f << stringf("%s" " else ", indent.c_str());
|
|
|
|
} else if (ff.has_arst) {
|
|
|
|
f << stringf("if (%s", ff.pol_arst ? "" : "!");
|
|
|
|
dump_sigspec(f, ff.sig_arst);
|
|
|
|
f << stringf(") %s = ", reg_bit_name.c_str());
|
|
|
|
dump_sigspec(f, val_arst);
|
|
|
|
f << stringf(";\n");
|
2021-10-01 16:50:48 -05:00
|
|
|
if (ff.has_aload)
|
2020-06-23 16:46:00 -05:00
|
|
|
f << stringf("%s" " else ", indent.c_str());
|
|
|
|
}
|
2021-10-01 16:50:48 -05:00
|
|
|
if (ff.has_aload) {
|
|
|
|
f << stringf("if (%s", ff.pol_aload ? "" : "!");
|
|
|
|
dump_sigspec(f, ff.sig_aload);
|
2020-06-23 16:46:00 -05:00
|
|
|
f << stringf(") %s = ", reg_bit_name.c_str());
|
2021-10-01 16:50:48 -05:00
|
|
|
dump_sigspec(f, sig_ad);
|
2020-06-23 16:46:00 -05:00
|
|
|
f << stringf(";\n");
|
|
|
|
}
|
|
|
|
}
|
2018-04-22 09:03:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!out_is_reg_wire) {
|
|
|
|
f << stringf("%s" "assign ", indent.c_str());
|
2020-06-23 16:46:00 -05:00
|
|
|
dump_sigspec(f, ff.sig_q);
|
2018-04-22 09:03:26 -05:00
|
|
|
f << stringf(" = %s;\n", reg_name.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->type.in(ID($assert), ID($assume), ID($cover)))
|
2019-04-22 02:35:14 -05:00
|
|
|
{
|
2020-07-16 05:34:21 -05:00
|
|
|
f << stringf("%s" "always%s if (", indent.c_str(), systemverilog ? "_comb" : " @*");
|
2020-04-02 11:51:32 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::EN));
|
2019-04-22 02:35:14 -05:00
|
|
|
f << stringf(") %s(", cell->type.c_str()+1);
|
2020-03-12 14:57:01 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::A));
|
2019-04-22 02:35:14 -05:00
|
|
|
f << stringf(");\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->type.in(ID($specify2), ID($specify3)))
|
2019-04-22 02:49:55 -05:00
|
|
|
{
|
|
|
|
f << stringf("%s" "specify\n%s ", indent.c_str(), indent.c_str());
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
SigSpec en = cell->getPort(ID::EN);
|
2019-04-22 02:49:55 -05:00
|
|
|
if (en != State::S1) {
|
|
|
|
f << stringf("if (");
|
2020-04-02 11:51:32 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::EN));
|
2019-04-22 02:49:55 -05:00
|
|
|
f << stringf(") ");
|
|
|
|
}
|
|
|
|
|
|
|
|
f << "(";
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->type == ID($specify3) && cell->getParam(ID::EDGE_EN).as_bool())
|
|
|
|
f << (cell->getParam(ID::EDGE_POL).as_bool() ? "posedge ": "negedge ");
|
2019-04-22 02:49:55 -05:00
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::SRC));
|
2019-04-22 02:49:55 -05:00
|
|
|
|
|
|
|
f << " ";
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->getParam(ID::SRC_DST_PEN).as_bool())
|
|
|
|
f << (cell->getParam(ID::SRC_DST_POL).as_bool() ? "+": "-");
|
|
|
|
f << (cell->getParam(ID::FULL).as_bool() ? "*> ": "=> ");
|
2019-04-22 02:49:55 -05:00
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->type == ID($specify3)) {
|
2019-04-22 02:49:55 -05:00
|
|
|
f << "(";
|
2020-04-02 11:51:32 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::DST));
|
2019-04-22 02:49:55 -05:00
|
|
|
f << " ";
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->getParam(ID::DAT_DST_PEN).as_bool())
|
|
|
|
f << (cell->getParam(ID::DAT_DST_POL).as_bool() ? "+": "-");
|
2019-04-22 02:49:55 -05:00
|
|
|
f << ": ";
|
2020-04-02 11:51:32 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::DAT));
|
2019-04-22 02:49:55 -05:00
|
|
|
f << ")";
|
|
|
|
} else {
|
2020-04-02 11:51:32 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::DST));
|
2019-04-22 02:49:55 -05:00
|
|
|
}
|
|
|
|
|
2019-05-04 01:46:24 -05:00
|
|
|
bool bak_decimal = decimal;
|
|
|
|
decimal = 1;
|
|
|
|
|
|
|
|
f << ") = (";
|
2020-04-02 11:51:32 -05:00
|
|
|
dump_const(f, cell->getParam(ID::T_RISE_MIN));
|
2019-05-04 01:46:24 -05:00
|
|
|
f << ":";
|
2020-04-02 11:51:32 -05:00
|
|
|
dump_const(f, cell->getParam(ID::T_RISE_TYP));
|
2019-05-04 01:46:24 -05:00
|
|
|
f << ":";
|
2020-04-02 11:51:32 -05:00
|
|
|
dump_const(f, cell->getParam(ID::T_RISE_MAX));
|
2019-05-04 01:46:24 -05:00
|
|
|
f << ", ";
|
2020-04-02 11:51:32 -05:00
|
|
|
dump_const(f, cell->getParam(ID::T_FALL_MIN));
|
2019-05-04 01:46:24 -05:00
|
|
|
f << ":";
|
2020-04-02 11:51:32 -05:00
|
|
|
dump_const(f, cell->getParam(ID::T_FALL_TYP));
|
2019-05-04 01:46:24 -05:00
|
|
|
f << ":";
|
2020-04-02 11:51:32 -05:00
|
|
|
dump_const(f, cell->getParam(ID::T_FALL_MAX));
|
2019-05-04 01:46:24 -05:00
|
|
|
f << ");\n";
|
|
|
|
|
|
|
|
decimal = bak_decimal;
|
|
|
|
|
|
|
|
f << stringf("%s" "endspecify\n", indent.c_str());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->type == ID($specrule))
|
2019-05-04 01:46:24 -05:00
|
|
|
{
|
|
|
|
f << stringf("%s" "specify\n%s ", indent.c_str(), indent.c_str());
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
IdString spec_type = cell->getParam(ID::TYPE).decode_string();
|
2019-05-04 01:46:24 -05:00
|
|
|
f << stringf("%s(", spec_type.c_str());
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->getParam(ID::SRC_PEN).as_bool())
|
|
|
|
f << (cell->getParam(ID::SRC_POL).as_bool() ? "posedge ": "negedge ");
|
|
|
|
dump_sigspec(f, cell->getPort(ID::SRC));
|
2019-05-04 01:46:24 -05:00
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->getPort(ID::SRC_EN) != State::S1) {
|
2019-05-04 01:46:24 -05:00
|
|
|
f << " &&& ";
|
2020-04-02 11:51:32 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::SRC_EN));
|
2019-05-04 01:46:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
f << ", ";
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->getParam(ID::DST_PEN).as_bool())
|
|
|
|
f << (cell->getParam(ID::DST_POL).as_bool() ? "posedge ": "negedge ");
|
|
|
|
dump_sigspec(f, cell->getPort(ID::DST));
|
2019-05-04 01:46:24 -05:00
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->getPort(ID::DST_EN) != State::S1) {
|
2019-05-04 01:46:24 -05:00
|
|
|
f << " &&& ";
|
2020-04-02 11:51:32 -05:00
|
|
|
dump_sigspec(f, cell->getPort(ID::DST_EN));
|
2019-05-04 01:46:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool bak_decimal = decimal;
|
|
|
|
decimal = 1;
|
|
|
|
|
|
|
|
f << ", ";
|
2020-04-02 11:51:32 -05:00
|
|
|
dump_const(f, cell->getParam(ID::T_LIMIT_MIN));
|
2020-02-13 10:59:08 -06:00
|
|
|
f << ": ";
|
2020-04-02 11:51:32 -05:00
|
|
|
dump_const(f, cell->getParam(ID::T_LIMIT_TYP));
|
2020-02-13 10:59:08 -06:00
|
|
|
f << ": ";
|
2020-04-02 11:51:32 -05:00
|
|
|
dump_const(f, cell->getParam(ID::T_LIMIT_MAX));
|
2019-05-04 01:46:24 -05:00
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
if (spec_type.in(ID($setuphold), ID($recrem), ID($fullskew))) {
|
2019-05-04 01:46:24 -05:00
|
|
|
f << ", ";
|
2020-04-02 11:51:32 -05:00
|
|
|
dump_const(f, cell->getParam(ID::T_LIMIT2_MIN));
|
2020-02-13 10:59:08 -06:00
|
|
|
f << ": ";
|
2020-04-02 11:51:32 -05:00
|
|
|
dump_const(f, cell->getParam(ID::T_LIMIT2_TYP));
|
2020-02-13 10:59:08 -06:00
|
|
|
f << ": ";
|
2020-04-02 11:51:32 -05:00
|
|
|
dump_const(f, cell->getParam(ID::T_LIMIT2_MAX));
|
2019-05-04 01:46:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
f << ");\n";
|
|
|
|
decimal = bak_decimal;
|
2019-04-22 02:49:55 -05:00
|
|
|
|
|
|
|
f << stringf("%s" "endspecify\n", indent.c_str());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-29 11:31:04 -06:00
|
|
|
if (cell->type == ID($print))
|
|
|
|
{
|
2023-06-27 20:51:32 -05:00
|
|
|
// Sync $print cells are accumulated and handled in dump_module.
|
|
|
|
if (cell->getParam(ID::TRG_ENABLE).as_bool())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
f << stringf("%s" "always @*\n", indent.c_str());
|
2020-11-29 11:31:04 -06:00
|
|
|
|
|
|
|
f << stringf("%s" " if (", indent.c_str());
|
|
|
|
dump_sigspec(f, cell->getPort(ID::EN));
|
|
|
|
f << stringf(")\n");
|
|
|
|
|
2023-06-27 20:51:32 -05:00
|
|
|
dump_cell_expr_print(f, indent + " ", cell);
|
2020-11-29 11:31:04 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-01-11 03:39:28 -06:00
|
|
|
if (cell->type == ID($check))
|
|
|
|
{
|
|
|
|
// Sync $check cells are accumulated and handled in dump_module.
|
|
|
|
if (cell->getParam(ID::TRG_ENABLE).as_bool())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
f << stringf("%s" "always @*\n", indent.c_str());
|
|
|
|
|
|
|
|
f << stringf("%s" " if (", indent.c_str());
|
|
|
|
dump_sigspec(f, cell->getPort(ID::EN));
|
|
|
|
f << stringf(") begin\n");
|
|
|
|
|
|
|
|
std::string flavor = cell->getParam(ID::FLAVOR).decode_string();
|
|
|
|
if (flavor == "assert" || flavor == "assume") {
|
|
|
|
Fmt fmt;
|
|
|
|
fmt.parse_rtlil(cell);
|
|
|
|
if (!fmt.parts.empty()) {
|
|
|
|
f << stringf("%s" " if (!", indent.c_str());
|
|
|
|
dump_sigspec(f, cell->getPort(ID::A));
|
|
|
|
f << stringf(")\n");
|
|
|
|
dump_cell_expr_print(f, indent + " ", cell);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
f << stringf("%s" " /* message omitted */\n", indent.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
dump_cell_expr_check(f, indent + " ", cell);
|
|
|
|
|
|
|
|
f << stringf("%s" " end\n", indent.c_str());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-17 14:48:38 -05:00
|
|
|
// FIXME: $fsm
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-23 06:54:21 -05:00
|
|
|
void dump_cell(std::ostream &f, std::string indent, RTLIL::Cell *cell)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2024-01-11 08:18:37 -06:00
|
|
|
// To keep the output compatible with other tools we ignore $scopeinfo
|
|
|
|
// cells that exist only to hold metadata. If in the future that metadata
|
|
|
|
// should be exposed as part of the write_verilog output it should be
|
|
|
|
// opt-in and/or represented as something else than a $scopeinfo cell.
|
|
|
|
if (cell->type == ID($scopeinfo))
|
|
|
|
return;
|
|
|
|
|
2020-10-17 14:48:38 -05:00
|
|
|
// Handled by dump_memory
|
2021-05-22 12:14:13 -05:00
|
|
|
if (cell->is_mem_cell())
|
2020-10-17 14:48:38 -05:00
|
|
|
return;
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
if (cell->type[0] == '$' && !noexpr) {
|
|
|
|
if (dump_cell_expr(f, indent, cell))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dump_attributes(f, indent, cell->attributes);
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s" "%s", indent.c_str(), id(cell->type, false).c_str());
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2019-02-17 14:18:12 -06:00
|
|
|
if (!defparam && cell->parameters.size() > 0) {
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(" #(");
|
2014-12-26 14:35:22 -06:00
|
|
|
for (auto it = cell->parameters.begin(); it != cell->parameters.end(); ++it) {
|
2013-01-05 04:13:26 -06:00
|
|
|
if (it != cell->parameters.begin())
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(",");
|
|
|
|
f << stringf("\n%s .%s(", indent.c_str(), id(it->first).c_str());
|
2024-01-11 04:53:30 -06:00
|
|
|
if (it->second.size() > 0)
|
|
|
|
dump_const(f, it->second);
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(")");
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("\n%s" ")", indent.c_str());
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string cell_name = cellname(cell);
|
|
|
|
if (cell_name != id(cell->name))
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(" %s /* %s */ (", cell_name.c_str(), id(cell->name).c_str());
|
2013-01-05 04:13:26 -06:00
|
|
|
else
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(" %s (", cell_name.c_str());
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
bool first_arg = true;
|
2014-08-02 06:11:01 -05:00
|
|
|
std::set<RTLIL::IdString> numbered_ports;
|
2013-01-05 04:13:26 -06:00
|
|
|
for (int i = 1; true; i++) {
|
|
|
|
char str[16];
|
|
|
|
snprintf(str, 16, "$%d", i);
|
2014-12-26 14:35:22 -06:00
|
|
|
for (auto it = cell->connections().begin(); it != cell->connections().end(); ++it) {
|
2013-01-05 04:13:26 -06:00
|
|
|
if (it->first != str)
|
|
|
|
continue;
|
|
|
|
if (!first_arg)
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(",");
|
2013-01-05 04:13:26 -06:00
|
|
|
first_arg = false;
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("\n%s ", indent.c_str());
|
2013-01-05 04:13:26 -06:00
|
|
|
dump_sigspec(f, it->second);
|
|
|
|
numbered_ports.insert(it->first);
|
|
|
|
goto found_numbered_port;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
found_numbered_port:;
|
|
|
|
}
|
2014-12-26 14:35:22 -06:00
|
|
|
for (auto it = cell->connections().begin(); it != cell->connections().end(); ++it) {
|
2013-01-05 04:13:26 -06:00
|
|
|
if (numbered_ports.count(it->first))
|
|
|
|
continue;
|
|
|
|
if (!first_arg)
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(",");
|
2013-01-05 04:13:26 -06:00
|
|
|
first_arg = false;
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("\n%s .%s(", indent.c_str(), id(it->first).c_str());
|
2014-07-22 13:15:14 -05:00
|
|
|
if (it->second.size() > 0)
|
2013-01-05 04:13:26 -06:00
|
|
|
dump_sigspec(f, it->second);
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(")");
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("\n%s" ");\n", indent.c_str());
|
2016-07-30 05:46:06 -05:00
|
|
|
|
2019-02-17 14:18:12 -06:00
|
|
|
if (defparam && cell->parameters.size() > 0) {
|
2016-07-30 05:46:06 -05:00
|
|
|
for (auto it = cell->parameters.begin(); it != cell->parameters.end(); ++it) {
|
|
|
|
f << stringf("%sdefparam %s.%s = ", indent.c_str(), cell_name.c_str(), id(it->first).c_str());
|
2019-05-04 01:46:24 -05:00
|
|
|
dump_const(f, it->second);
|
2016-07-30 05:46:06 -05:00
|
|
|
f << stringf(";\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 16:46:00 -05:00
|
|
|
if (siminit && RTLIL::builtin_ff_cell_types().count(cell->type) && cell->hasPort(ID::Q) && !cell->type.in(ID($ff), ID($_FF_))) {
|
2019-02-17 14:18:12 -06:00
|
|
|
std::stringstream ss;
|
2020-04-02 11:51:32 -05:00
|
|
|
dump_reg_init(ss, cell->getPort(ID::Q));
|
2019-02-17 14:18:12 -06:00
|
|
|
if (!ss.str().empty()) {
|
|
|
|
f << stringf("%sinitial %s.Q", indent.c_str(), cell_name.c_str());
|
|
|
|
f << ss.str();
|
|
|
|
f << ";\n";
|
|
|
|
}
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
2024-01-11 03:39:28 -06:00
|
|
|
void dump_sync_effect(std::ostream &f, std::string indent, const RTLIL::SigSpec &trg, const RTLIL::Const &polarity, std::vector<const RTLIL::Cell*> &cells)
|
2023-06-27 20:51:32 -05:00
|
|
|
{
|
2024-01-11 05:47:55 -06:00
|
|
|
if (trg.size() == 0) {
|
|
|
|
f << stringf("%s" "initial begin\n", indent.c_str());
|
|
|
|
} else {
|
|
|
|
f << stringf("%s" "always @(", indent.c_str());
|
|
|
|
for (int i = 0; i < trg.size(); i++) {
|
|
|
|
if (i != 0)
|
|
|
|
f << " or ";
|
|
|
|
if (polarity[i])
|
|
|
|
f << "posedge ";
|
|
|
|
else
|
|
|
|
f << "negedge ";
|
|
|
|
dump_sigspec(f, trg[i]);
|
|
|
|
}
|
|
|
|
f << ") begin\n";
|
2023-06-27 20:51:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
std::sort(cells.begin(), cells.end(), [](const RTLIL::Cell *a, const RTLIL::Cell *b) {
|
|
|
|
return a->getParam(ID::PRIORITY).as_int() > b->getParam(ID::PRIORITY).as_int();
|
|
|
|
});
|
|
|
|
for (auto cell : cells) {
|
|
|
|
f << stringf("%s" " if (", indent.c_str());
|
|
|
|
dump_sigspec(f, cell->getPort(ID::EN));
|
2024-01-11 03:39:28 -06:00
|
|
|
f << stringf(") begin\n");
|
|
|
|
|
|
|
|
if (cell->type == ID($print)) {
|
|
|
|
dump_cell_expr_print(f, indent + " ", cell);
|
|
|
|
} else if (cell->type == ID($check)) {
|
|
|
|
std::string flavor = cell->getParam(ID::FLAVOR).decode_string();
|
|
|
|
if (flavor == "assert" || flavor == "assume") {
|
|
|
|
Fmt fmt;
|
|
|
|
fmt.parse_rtlil(cell);
|
|
|
|
if (!fmt.parts.empty()) {
|
|
|
|
f << stringf("%s" " if (!", indent.c_str());
|
|
|
|
dump_sigspec(f, cell->getPort(ID::A));
|
|
|
|
f << stringf(")\n");
|
|
|
|
dump_cell_expr_print(f, indent + " ", cell);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
f << stringf("%s" " /* message omitted */\n", indent.c_str());
|
|
|
|
}
|
2023-06-27 20:51:32 -05:00
|
|
|
|
2024-01-11 03:39:28 -06:00
|
|
|
dump_cell_expr_check(f, indent + " ", cell);
|
|
|
|
}
|
|
|
|
|
|
|
|
f << stringf("%s" " end\n", indent.c_str());
|
2023-06-27 20:51:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
f << stringf("%s" "end\n", indent.c_str());
|
|
|
|
}
|
|
|
|
|
2014-08-23 06:54:21 -05:00
|
|
|
void dump_conn(std::ostream &f, std::string indent, const RTLIL::SigSpec &left, const RTLIL::SigSpec &right)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2020-11-25 11:21:41 -06:00
|
|
|
if (simple_lhs) {
|
|
|
|
int offset = 0;
|
|
|
|
for (auto &chunk : left.chunks()) {
|
|
|
|
f << stringf("%s" "assign ", indent.c_str());
|
|
|
|
dump_sigspec(f, chunk);
|
|
|
|
f << stringf(" = ");
|
|
|
|
dump_sigspec(f, right.extract(offset, GetSize(chunk)));
|
|
|
|
f << stringf(";\n");
|
|
|
|
offset += GetSize(chunk);
|
|
|
|
}
|
|
|
|
} else {
|
2020-11-25 10:43:28 -06:00
|
|
|
f << stringf("%s" "assign ", indent.c_str());
|
2020-11-25 11:21:41 -06:00
|
|
|
dump_sigspec(f, left);
|
2020-11-25 10:43:28 -06:00
|
|
|
f << stringf(" = ");
|
2020-11-25 11:21:41 -06:00
|
|
|
dump_sigspec(f, right);
|
2020-11-25 10:43:28 -06:00
|
|
|
f << stringf(";\n");
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
2014-08-23 06:54:21 -05:00
|
|
|
void dump_proc_switch(std::ostream &f, std::string indent, RTLIL::SwitchRule *sw);
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2024-01-22 21:26:58 -06:00
|
|
|
void dump_case_actions(std::ostream &f, std::string indent, RTLIL::CaseRule *cs)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2014-12-26 14:35:22 -06:00
|
|
|
for (auto it = cs->actions.begin(); it != cs->actions.end(); ++it) {
|
2014-07-22 13:15:14 -05:00
|
|
|
if (it->first.size() == 0)
|
2013-01-05 04:13:26 -06:00
|
|
|
continue;
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s ", indent.c_str());
|
2013-01-05 04:13:26 -06:00
|
|
|
dump_sigspec(f, it->first);
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(" = ");
|
2013-01-05 04:13:26 -06:00
|
|
|
dump_sigspec(f, it->second);
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(";\n");
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-20 20:02:39 -06:00
|
|
|
bool dump_proc_switch_ifelse(std::ostream &f, std::string indent, RTLIL::SwitchRule *sw)
|
|
|
|
{
|
|
|
|
for (auto it = sw->cases.begin(); it != sw->cases.end(); ++it) {
|
|
|
|
if ((*it)->compare.size() == 0) {
|
|
|
|
break;
|
|
|
|
} else if ((*it)->compare.size() == 1) {
|
|
|
|
int case_index = it - sw->cases.begin();
|
|
|
|
SigSpec compare = (*it)->compare.at(0);
|
|
|
|
if (case_index >= compare.size())
|
|
|
|
return false;
|
|
|
|
if (compare[case_index] != State::S1)
|
|
|
|
return false;
|
|
|
|
for (int bit_index = 0; bit_index < compare.size(); bit_index++)
|
|
|
|
if (bit_index != case_index && compare[bit_index] != State::Sa)
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-22 21:26:58 -06:00
|
|
|
dump_attributes(f, indent, sw->attributes);
|
2023-12-20 20:02:39 -06:00
|
|
|
f << indent;
|
|
|
|
auto sig_it = sw->signal.begin();
|
|
|
|
for (auto it = sw->cases.begin(); it != sw->cases.end(); ++it, ++sig_it) {
|
|
|
|
if (it != sw->cases.begin()) {
|
2024-01-22 21:26:58 -06:00
|
|
|
if ((*it)->compare.empty())
|
|
|
|
f << " else begin\n";
|
|
|
|
else
|
|
|
|
f << " else ";
|
2023-12-20 20:02:39 -06:00
|
|
|
}
|
|
|
|
if (!(*it)->compare.empty()) {
|
|
|
|
f << stringf("if (");
|
|
|
|
dump_sigspec(f, *sig_it);
|
2024-01-22 21:26:58 -06:00
|
|
|
f << stringf(") begin\n");
|
2023-12-20 20:02:39 -06:00
|
|
|
}
|
2024-01-22 21:26:58 -06:00
|
|
|
|
|
|
|
dump_case_actions(f, indent, (*it));
|
|
|
|
for (auto it2 = (*it)->switches.begin(); it2 != (*it)->switches.end(); ++it2)
|
|
|
|
dump_proc_switch(f, indent + " ", *it2);
|
|
|
|
|
|
|
|
f << indent << "end";
|
2023-12-20 20:02:39 -06:00
|
|
|
if ((*it)->compare.empty())
|
|
|
|
break;
|
|
|
|
}
|
2024-01-22 21:26:58 -06:00
|
|
|
f << "\n";
|
2023-12-20 20:02:39 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-01-22 21:26:58 -06:00
|
|
|
void dump_case_body(std::ostream &f, std::string indent, RTLIL::CaseRule *cs, bool omit_trailing_begin = false)
|
|
|
|
{
|
|
|
|
int number_of_stmts = cs->switches.size() + cs->actions.size();
|
|
|
|
|
|
|
|
if (!omit_trailing_begin && number_of_stmts >= 2)
|
|
|
|
f << stringf("%s" "begin\n", indent.c_str());
|
|
|
|
|
|
|
|
dump_case_actions(f, indent, cs);
|
|
|
|
for (auto it = cs->switches.begin(); it != cs->switches.end(); ++it)
|
|
|
|
dump_proc_switch(f, indent + " ", *it);
|
|
|
|
|
|
|
|
if (!omit_trailing_begin && number_of_stmts == 0)
|
|
|
|
f << stringf("%s /* empty */;\n", indent.c_str());
|
|
|
|
|
|
|
|
if (omit_trailing_begin || number_of_stmts >= 2)
|
|
|
|
f << stringf("%s" "end\n", indent.c_str());
|
|
|
|
}
|
|
|
|
|
2014-08-23 06:54:21 -05:00
|
|
|
void dump_proc_switch(std::ostream &f, std::string indent, RTLIL::SwitchRule *sw)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2014-07-22 13:15:14 -05:00
|
|
|
if (sw->signal.size() == 0) {
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s" "begin\n", indent.c_str());
|
2014-12-26 14:35:22 -06:00
|
|
|
for (auto it = sw->cases.begin(); it != sw->cases.end(); ++it) {
|
2013-01-05 04:13:26 -06:00
|
|
|
if ((*it)->compare.size() == 0)
|
|
|
|
dump_case_body(f, indent + " ", *it);
|
|
|
|
}
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s" "end\n", indent.c_str());
|
2013-01-05 04:13:26 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-12-20 20:02:39 -06:00
|
|
|
if (dump_proc_switch_ifelse(f, indent, sw))
|
|
|
|
return;
|
|
|
|
|
2019-07-08 10:11:29 -05:00
|
|
|
dump_attributes(f, indent, sw->attributes);
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s" "casez (", indent.c_str());
|
2013-01-05 04:13:26 -06:00
|
|
|
dump_sigspec(f, sw->signal);
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(")\n");
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2014-12-26 14:35:22 -06:00
|
|
|
for (auto it = sw->cases.begin(); it != sw->cases.end(); ++it) {
|
2024-01-06 09:44:36 -06:00
|
|
|
bool got_default = false;
|
2023-12-20 20:02:39 -06:00
|
|
|
dump_attributes(f, indent + " ", (*it)->attributes, "\n", /*modattr=*/false, /*regattr=*/false, /*as_comment=*/true);
|
2016-03-14 07:03:28 -05:00
|
|
|
if ((*it)->compare.size() == 0) {
|
|
|
|
f << stringf("%s default", indent.c_str());
|
|
|
|
got_default = true;
|
|
|
|
} else {
|
|
|
|
f << stringf("%s ", indent.c_str());
|
2013-01-05 04:13:26 -06:00
|
|
|
for (size_t i = 0; i < (*it)->compare.size(); i++) {
|
|
|
|
if (i > 0)
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(", ");
|
2013-01-05 04:13:26 -06:00
|
|
|
dump_sigspec(f, (*it)->compare[i]);
|
|
|
|
}
|
|
|
|
}
|
2019-07-09 14:14:03 -05:00
|
|
|
f << stringf(":\n");
|
2013-01-05 04:13:26 -06:00
|
|
|
dump_case_body(f, indent + " ", *it);
|
2024-01-06 09:44:36 -06:00
|
|
|
|
|
|
|
if (got_default) {
|
|
|
|
// If we followed up the default with more cases the Verilog
|
|
|
|
// semantics would be to match those *before* the default, but
|
|
|
|
// the RTLIL semantics are to match those *after* the default
|
|
|
|
// (so they can never be selected). Exit now.
|
|
|
|
break;
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
2023-10-07 18:11:30 -05:00
|
|
|
if (sw->cases.empty()) {
|
|
|
|
// Verilog does not allow empty cases.
|
|
|
|
f << stringf("%s default: ;\n", indent.c_str());
|
|
|
|
}
|
|
|
|
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s" "endcase\n", indent.c_str());
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void case_body_find_regs(RTLIL::CaseRule *cs)
|
|
|
|
{
|
2014-12-26 14:35:22 -06:00
|
|
|
for (auto it = cs->switches.begin(); it != cs->switches.end(); ++it)
|
2013-01-05 04:13:26 -06:00
|
|
|
for (auto it2 = (*it)->cases.begin(); it2 != (*it)->cases.end(); it2++)
|
|
|
|
case_body_find_regs(*it2);
|
|
|
|
|
2014-12-26 14:35:22 -06:00
|
|
|
for (auto it = cs->actions.begin(); it != cs->actions.end(); ++it) {
|
2014-07-25 07:23:31 -05:00
|
|
|
for (auto &c : it->first.chunks())
|
|
|
|
if (c.wire != NULL)
|
|
|
|
reg_wires.insert(c.wire->name);
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-23 06:54:21 -05:00
|
|
|
void dump_process(std::ostream &f, std::string indent, RTLIL::Process *proc, bool find_regs = false)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
|
|
|
if (find_regs) {
|
|
|
|
case_body_find_regs(&proc->root_case);
|
2014-12-26 14:35:22 -06:00
|
|
|
for (auto it = proc->syncs.begin(); it != proc->syncs.end(); ++it)
|
2013-01-05 04:13:26 -06:00
|
|
|
for (auto it2 = (*it)->actions.begin(); it2 != (*it)->actions.end(); it2++) {
|
2014-07-25 07:23:31 -05:00
|
|
|
for (auto &c : it2->first.chunks())
|
|
|
|
if (c.wire != NULL)
|
|
|
|
reg_wires.insert(c.wire->name);
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-16 05:34:21 -05:00
|
|
|
f << stringf("%s" "always%s begin\n", indent.c_str(), systemverilog ? "_comb" : " @*");
|
2020-07-16 06:26:31 -05:00
|
|
|
if (!systemverilog)
|
2021-11-17 05:19:06 -06:00
|
|
|
f << indent + " " << "if (" << id(initial_id) << ") begin end\n";
|
2013-01-05 04:13:26 -06:00
|
|
|
dump_case_body(f, indent, &proc->root_case, true);
|
|
|
|
|
|
|
|
std::string backup_indent = indent;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < proc->syncs.size(); i++)
|
|
|
|
{
|
|
|
|
RTLIL::SyncRule *sync = proc->syncs[i];
|
|
|
|
indent = backup_indent;
|
|
|
|
|
|
|
|
if (sync->type == RTLIL::STa) {
|
2020-07-16 05:34:21 -05:00
|
|
|
f << stringf("%s" "always%s begin\n", indent.c_str(), systemverilog ? "_comb" : " @*");
|
2018-12-07 12:48:06 -06:00
|
|
|
} else if (sync->type == RTLIL::STi) {
|
|
|
|
f << stringf("%s" "initial begin\n", indent.c_str());
|
2013-01-05 04:13:26 -06:00
|
|
|
} else {
|
2020-07-16 05:34:21 -05:00
|
|
|
f << stringf("%s" "always%s @(", indent.c_str(), systemverilog ? "_ff" : "");
|
2013-01-05 04:13:26 -06:00
|
|
|
if (sync->type == RTLIL::STp || sync->type == RTLIL::ST1)
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("posedge ");
|
2013-01-05 04:13:26 -06:00
|
|
|
if (sync->type == RTLIL::STn || sync->type == RTLIL::ST0)
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("negedge ");
|
2013-01-05 04:13:26 -06:00
|
|
|
dump_sigspec(f, sync->signal);
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(") begin\n");
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
std::string ends = indent + "end\n";
|
|
|
|
indent += " ";
|
|
|
|
|
|
|
|
if (sync->type == RTLIL::ST0 || sync->type == RTLIL::ST1) {
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s" "if (%s", indent.c_str(), sync->type == RTLIL::ST0 ? "!" : "");
|
2013-01-05 04:13:26 -06:00
|
|
|
dump_sigspec(f, sync->signal);
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(") begin\n");
|
2013-01-05 04:13:26 -06:00
|
|
|
ends = indent + "end\n" + ends;
|
|
|
|
indent += " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sync->type == RTLIL::STp || sync->type == RTLIL::STn) {
|
|
|
|
for (size_t j = 0; j < proc->syncs.size(); j++) {
|
|
|
|
RTLIL::SyncRule *sync2 = proc->syncs[j];
|
|
|
|
if (sync2->type == RTLIL::ST0 || sync2->type == RTLIL::ST1) {
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s" "if (%s", indent.c_str(), sync2->type == RTLIL::ST1 ? "!" : "");
|
2013-01-05 04:13:26 -06:00
|
|
|
dump_sigspec(f, sync2->signal);
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(") begin\n");
|
2013-01-05 04:13:26 -06:00
|
|
|
ends = indent + "end\n" + ends;
|
|
|
|
indent += " ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-26 14:35:22 -06:00
|
|
|
for (auto it = sync->actions.begin(); it != sync->actions.end(); ++it) {
|
2014-07-22 13:15:14 -05:00
|
|
|
if (it->first.size() == 0)
|
2013-01-05 04:13:26 -06:00
|
|
|
continue;
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s ", indent.c_str());
|
2013-01-05 04:13:26 -06:00
|
|
|
dump_sigspec(f, it->first);
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(" <= ");
|
2013-01-05 04:13:26 -06:00
|
|
|
dump_sigspec(f, it->second);
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(";\n");
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s", ends.c_str());
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-23 06:54:21 -05:00
|
|
|
void dump_module(std::ostream &f, std::string indent, RTLIL::Module *module)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2024-01-11 03:39:28 -06:00
|
|
|
std::map<std::pair<RTLIL::SigSpec, RTLIL::Const>, std::vector<const RTLIL::Cell*>> sync_effect_cells;
|
2023-06-27 20:51:32 -05:00
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
reg_wires.clear();
|
|
|
|
reset_auto_counter(module);
|
|
|
|
active_module = module;
|
2016-11-16 05:00:39 -06:00
|
|
|
active_sigmap.set(module);
|
|
|
|
active_initdata.clear();
|
|
|
|
|
|
|
|
for (auto wire : module->wires())
|
2020-04-02 11:51:32 -05:00
|
|
|
if (wire->attributes.count(ID::init)) {
|
2016-11-16 05:00:39 -06:00
|
|
|
SigSpec sig = active_sigmap(wire);
|
2020-04-02 11:51:32 -05:00
|
|
|
Const val = wire->attributes.at(ID::init);
|
2016-11-16 05:00:39 -06:00
|
|
|
for (int i = 0; i < GetSize(sig) && i < GetSize(val); i++)
|
2019-05-07 12:55:36 -05:00
|
|
|
if (val[i] == State::S0 || val[i] == State::S1)
|
|
|
|
active_initdata[sig[i]] = val[i];
|
2016-11-16 05:00:39 -06:00
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2016-03-14 07:03:28 -05:00
|
|
|
if (!module->processes.empty())
|
2018-12-07 13:14:07 -06:00
|
|
|
log_warning("Module %s contains unmapped RTLIL processes. RTLIL processes\n"
|
2016-03-14 07:03:28 -05:00
|
|
|
"can't always be mapped directly to Verilog always blocks. Unintended\n"
|
|
|
|
"changes in simulation behavior are possible! Use \"proc\" to convert\n"
|
2018-12-12 22:36:02 -06:00
|
|
|
"processes to logic networks and registers.\n", log_id(module));
|
2016-03-14 07:03:28 -05:00
|
|
|
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("\n");
|
2014-12-26 14:35:22 -06:00
|
|
|
for (auto it = module->processes.begin(); it != module->processes.end(); ++it)
|
2013-01-05 04:13:26 -06:00
|
|
|
dump_process(f, indent + " ", it->second, true);
|
|
|
|
|
|
|
|
if (!noexpr)
|
|
|
|
{
|
|
|
|
std::set<std::pair<RTLIL::Wire*,int>> reg_bits;
|
2020-04-01 00:25:10 -05:00
|
|
|
for (auto cell : module->cells())
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2024-01-11 03:39:28 -06:00
|
|
|
if (cell->type.in(ID($print), ID($check)) && cell->getParam(ID::TRG_ENABLE).as_bool()) {
|
|
|
|
sync_effect_cells[make_pair(cell->getPort(ID::TRG), cell->getParam(ID::TRG_POLARITY))].push_back(cell);
|
2023-06-27 20:51:32 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-06-23 16:46:00 -05:00
|
|
|
if (!RTLIL::builtin_ff_cell_types().count(cell->type) || !cell->hasPort(ID::Q) || cell->type.in(ID($ff), ID($_FF_)))
|
2013-01-05 04:13:26 -06:00
|
|
|
continue;
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
RTLIL::SigSpec sig = cell->getPort(ID::Q);
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2014-07-25 07:23:31 -05:00
|
|
|
if (sig.is_chunk()) {
|
|
|
|
RTLIL::SigChunk chunk = sig.as_chunk();
|
|
|
|
if (chunk.wire != NULL)
|
|
|
|
for (int i = 0; i < chunk.width; i++)
|
|
|
|
reg_bits.insert(std::pair<RTLIL::Wire*,int>(chunk.wire, chunk.offset+i));
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
2020-04-01 00:25:10 -05:00
|
|
|
for (auto wire : module->wires())
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
|
|
|
for (int i = 0; i < wire->width; i++)
|
|
|
|
if (reg_bits.count(std::pair<RTLIL::Wire*,int>(wire, i)) == 0)
|
|
|
|
goto this_wire_aint_reg;
|
2014-09-06 13:30:46 -05:00
|
|
|
if (wire->width)
|
|
|
|
reg_wires.insert(wire->name);
|
2013-01-05 04:13:26 -06:00
|
|
|
this_wire_aint_reg:;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-20 20:02:39 -06:00
|
|
|
dump_attributes(f, indent, module->attributes, "\n", /*modattr=*/true);
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s" "module %s(", indent.c_str(), id(module->name, false).c_str());
|
2013-01-05 04:13:26 -06:00
|
|
|
bool keep_running = true;
|
2021-10-09 06:40:55 -05:00
|
|
|
int cnt = 0;
|
2013-01-05 04:13:26 -06:00
|
|
|
for (int port_id = 1; keep_running; port_id++) {
|
|
|
|
keep_running = false;
|
2020-04-01 00:25:10 -05:00
|
|
|
for (auto wire : module->wires()) {
|
2013-01-05 04:13:26 -06:00
|
|
|
if (wire->port_id == port_id) {
|
|
|
|
if (port_id != 1)
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(", ");
|
|
|
|
f << stringf("%s", id(wire->name).c_str());
|
2013-01-05 04:13:26 -06:00
|
|
|
keep_running = true;
|
2021-10-09 06:40:55 -05:00
|
|
|
if (cnt==20) { f << stringf("\n"); cnt = 0; } else cnt++;
|
2013-01-05 04:13:26 -06:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf(");\n");
|
2021-11-17 05:19:06 -06:00
|
|
|
if (!systemverilog && !module->processes.empty()) {
|
|
|
|
initial_id = NEW_ID;
|
|
|
|
f << indent + " " << "reg " << id(initial_id) << " = 0;\n";
|
|
|
|
}
|
2020-07-16 06:26:31 -05:00
|
|
|
|
2020-04-01 00:25:10 -05:00
|
|
|
for (auto w : module->wires())
|
|
|
|
dump_wire(f, indent + " ", w);
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2020-10-17 14:48:38 -05:00
|
|
|
for (auto &mem : Mem::get_all_memories(module))
|
|
|
|
dump_memory(f, indent + " ", mem);
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2020-04-01 00:25:10 -05:00
|
|
|
for (auto cell : module->cells())
|
|
|
|
dump_cell(f, indent + " ", cell);
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2024-01-11 03:39:28 -06:00
|
|
|
for (auto &it : sync_effect_cells)
|
|
|
|
dump_sync_effect(f, indent + " ", it.first.first, it.first.second, it.second);
|
2023-06-27 20:51:32 -05:00
|
|
|
|
2014-12-26 14:35:22 -06:00
|
|
|
for (auto it = module->processes.begin(); it != module->processes.end(); ++it)
|
2013-01-05 04:13:26 -06:00
|
|
|
dump_process(f, indent + " ", it->second);
|
|
|
|
|
2014-12-26 14:35:22 -06:00
|
|
|
for (auto it = module->connections().begin(); it != module->connections().end(); ++it)
|
2013-01-05 04:13:26 -06:00
|
|
|
dump_conn(f, indent + " ", it->first, it->second);
|
|
|
|
|
2014-08-23 06:54:21 -05:00
|
|
|
f << stringf("%s" "endmodule\n", indent.c_str());
|
2013-01-05 04:13:26 -06:00
|
|
|
active_module = NULL;
|
2016-11-16 05:00:39 -06:00
|
|
|
active_sigmap.clear();
|
|
|
|
active_initdata.clear();
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
struct VerilogBackend : public Backend {
|
2015-08-14 04:27:19 -05:00
|
|
|
VerilogBackend() : Backend("verilog", "write design to Verilog file") { }
|
2020-06-18 18:34:52 -05:00
|
|
|
void help() override
|
2013-02-28 17:36:19 -06:00
|
|
|
{
|
|
|
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
|
|
|
log("\n");
|
|
|
|
log(" write_verilog [options] [filename]\n");
|
|
|
|
log("\n");
|
2015-08-14 04:27:19 -05:00
|
|
|
log("Write the current design to a Verilog file.\n");
|
2013-02-28 17:36:19 -06:00
|
|
|
log("\n");
|
2020-07-16 05:34:21 -05:00
|
|
|
log(" -sv\n");
|
|
|
|
log(" with this option, SystemVerilog constructs like always_comb are used\n");
|
|
|
|
log("\n");
|
2013-02-28 17:36:19 -06:00
|
|
|
log(" -norename\n");
|
|
|
|
log(" without this option all internal object names (the ones with a dollar\n");
|
|
|
|
log(" instead of a backslash prefix) are changed to short names in the\n");
|
|
|
|
log(" format '_<number>_'.\n");
|
|
|
|
log("\n");
|
2016-11-01 05:30:27 -05:00
|
|
|
log(" -renameprefix <prefix>\n");
|
|
|
|
log(" insert this prefix in front of auto-generated instance names\n");
|
|
|
|
log("\n");
|
2013-02-28 17:36:19 -06:00
|
|
|
log(" -noattr\n");
|
|
|
|
log(" with this option no attributes are included in the output\n");
|
|
|
|
log("\n");
|
|
|
|
log(" -attr2comment\n");
|
|
|
|
log(" with this option attributes are included as comments in the output\n");
|
|
|
|
log("\n");
|
|
|
|
log(" -noexpr\n");
|
2015-08-14 04:27:19 -05:00
|
|
|
log(" without this option all internal cells are converted to Verilog\n");
|
2013-02-28 17:36:19 -06:00
|
|
|
log(" expressions.\n");
|
|
|
|
log("\n");
|
2022-11-10 09:16:37 -06:00
|
|
|
log(" -noparallelcase\n");
|
|
|
|
log(" With this option no parallel_case attributes are used. Instead, a case\n");
|
|
|
|
log(" statement that assigns don't-care values for priority dependent inputs\n");
|
|
|
|
log(" is generated.\n");
|
|
|
|
log("\n");
|
2019-02-28 16:56:55 -06:00
|
|
|
log(" -siminit\n");
|
|
|
|
log(" add initial statements with hierarchical refs to initialize FFs when\n");
|
|
|
|
log(" in -noexpr mode.\n");
|
|
|
|
log("\n");
|
2016-07-30 05:38:40 -05:00
|
|
|
log(" -nodec\n");
|
|
|
|
log(" 32-bit constant values are by default dumped as decimal numbers,\n");
|
2018-09-18 12:34:30 -05:00
|
|
|
log(" not bit pattern. This option deactivates this feature and instead\n");
|
2016-07-30 05:38:40 -05:00
|
|
|
log(" will write out all constants in binary.\n");
|
|
|
|
log("\n");
|
2017-10-03 10:31:21 -05:00
|
|
|
log(" -decimal\n");
|
|
|
|
log(" dump 32-bit constants in decimal and without size and radix\n");
|
|
|
|
log("\n");
|
2016-11-03 06:13:23 -05:00
|
|
|
log(" -nohex\n");
|
|
|
|
log(" constant values that are compatible with hex output are usually\n");
|
2018-09-18 12:34:30 -05:00
|
|
|
log(" dumped as hex values. This option deactivates this feature and\n");
|
2016-11-03 06:13:23 -05:00
|
|
|
log(" instead will write out all constants in binary.\n");
|
|
|
|
log("\n");
|
2016-07-30 05:38:40 -05:00
|
|
|
log(" -nostr\n");
|
|
|
|
log(" Parameters and attributes that are specified as strings in the\n");
|
|
|
|
log(" original input will be output as strings by this back-end. This\n");
|
2018-09-18 12:34:30 -05:00
|
|
|
log(" deactivates this feature and instead will write string constants\n");
|
2016-07-30 05:38:40 -05:00
|
|
|
log(" as binary numbers.\n");
|
|
|
|
log("\n");
|
2020-11-25 11:21:41 -06:00
|
|
|
log(" -simple-lhs\n");
|
2022-08-23 17:28:27 -05:00
|
|
|
log(" Connection assignments with simple left hand side without\n");
|
|
|
|
log(" concatenations.\n");
|
2020-11-25 11:21:41 -06:00
|
|
|
log("\n");
|
2019-11-14 21:11:46 -06:00
|
|
|
log(" -extmem\n");
|
|
|
|
log(" instead of initializing memories using assignments to individual\n");
|
|
|
|
log(" elements, use the '$readmemh' function to read initialization data\n");
|
|
|
|
log(" from a file. This data is written to a file named by appending\n");
|
|
|
|
log(" a sequential index to the Verilog filename and replacing the extension\n");
|
|
|
|
log(" with '.mem', e.g. 'write_verilog -extmem foo.v' writes 'foo-1.mem',\n");
|
|
|
|
log(" 'foo-2.mem' and so on.\n");
|
|
|
|
log("\n");
|
2016-07-30 05:46:06 -05:00
|
|
|
log(" -defparam\n");
|
2019-11-14 21:11:46 -06:00
|
|
|
log(" use 'defparam' statements instead of the Verilog-2001 syntax for\n");
|
2016-07-30 05:46:06 -05:00
|
|
|
log(" cell parameters.\n");
|
|
|
|
log("\n");
|
2013-11-22 08:01:12 -06:00
|
|
|
log(" -blackboxes\n");
|
|
|
|
log(" usually modules with the 'blackbox' attribute are ignored. with\n");
|
|
|
|
log(" this option set only the modules with the 'blackbox' attribute\n");
|
2013-03-28 03:20:10 -05:00
|
|
|
log(" are written to the output file.\n");
|
|
|
|
log("\n");
|
2013-09-03 12:10:11 -05:00
|
|
|
log(" -selected\n");
|
|
|
|
log(" only write selected modules. modules must be selected entirely or\n");
|
|
|
|
log(" not at all.\n");
|
|
|
|
log("\n");
|
2016-11-01 05:30:27 -05:00
|
|
|
log(" -v\n");
|
|
|
|
log(" verbose output (print new names of all renamed wires and cells)\n");
|
|
|
|
log("\n");
|
2016-03-14 07:03:28 -05:00
|
|
|
log("Note that RTLIL processes can't always be mapped directly to Verilog\n");
|
|
|
|
log("always blocks. This frontend should only be used to export an RTLIL\n");
|
|
|
|
log("netlist, i.e. after the \"proc\" pass has been used to convert all\n");
|
|
|
|
log("processes to logic networks and registers. A warning is generated when\n");
|
|
|
|
log("this command is called on a design with RTLIL processes.\n");
|
|
|
|
log("\n");
|
2013-02-28 17:36:19 -06:00
|
|
|
}
|
2020-06-18 18:34:52 -05:00
|
|
|
void execute(std::ostream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design) override
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2016-04-21 16:28:37 -05:00
|
|
|
log_header(design, "Executing Verilog backend.\n");
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2016-11-01 05:30:27 -05:00
|
|
|
verbose = false;
|
2013-01-05 04:13:26 -06:00
|
|
|
norename = false;
|
|
|
|
noattr = false;
|
|
|
|
attr2comment = false;
|
|
|
|
noexpr = false;
|
2016-07-30 05:38:40 -05:00
|
|
|
nodec = false;
|
2016-11-03 06:13:23 -05:00
|
|
|
nohex = false;
|
2016-07-30 05:38:40 -05:00
|
|
|
nostr = false;
|
2019-11-14 21:11:46 -06:00
|
|
|
extmem = false;
|
2016-07-30 05:46:06 -05:00
|
|
|
defparam = false;
|
2017-10-03 10:31:21 -05:00
|
|
|
decimal = false;
|
2019-02-28 16:56:55 -06:00
|
|
|
siminit = false;
|
2020-11-25 11:21:41 -06:00
|
|
|
simple_lhs = false;
|
2022-11-10 09:16:37 -06:00
|
|
|
noparallelcase = false;
|
2016-11-01 05:30:27 -05:00
|
|
|
auto_prefix = "";
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2013-11-22 08:01:12 -06:00
|
|
|
bool blackboxes = false;
|
2013-09-03 12:10:11 -05:00
|
|
|
bool selected = false;
|
2013-03-28 03:20:10 -05:00
|
|
|
|
2019-03-11 14:12:28 -05:00
|
|
|
auto_name_map.clear();
|
|
|
|
reg_wires.clear();
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
size_t argidx;
|
|
|
|
for (argidx = 1; argidx < args.size(); argidx++) {
|
|
|
|
std::string arg = args[argidx];
|
2020-07-16 05:34:21 -05:00
|
|
|
if (arg == "-sv") {
|
|
|
|
systemverilog = true;
|
|
|
|
continue;
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
if (arg == "-norename") {
|
|
|
|
norename = true;
|
|
|
|
continue;
|
|
|
|
}
|
2016-11-01 05:30:27 -05:00
|
|
|
if (arg == "-renameprefix" && argidx+1 < args.size()) {
|
|
|
|
auto_prefix = args[++argidx];
|
|
|
|
continue;
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
if (arg == "-noattr") {
|
|
|
|
noattr = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (arg == "-attr2comment") {
|
|
|
|
attr2comment = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (arg == "-noexpr") {
|
|
|
|
noexpr = true;
|
|
|
|
continue;
|
|
|
|
}
|
2022-11-10 09:16:37 -06:00
|
|
|
if (arg == "-noparallelcase") {
|
|
|
|
noparallelcase = true;
|
|
|
|
continue;
|
|
|
|
}
|
2016-07-30 05:38:40 -05:00
|
|
|
if (arg == "-nodec") {
|
|
|
|
nodec = true;
|
|
|
|
continue;
|
2016-11-03 06:13:23 -05:00
|
|
|
}
|
|
|
|
if (arg == "-nohex") {
|
|
|
|
nohex = true;
|
|
|
|
continue;
|
2016-07-30 05:38:40 -05:00
|
|
|
}
|
|
|
|
if (arg == "-nostr") {
|
|
|
|
nostr = true;
|
|
|
|
continue;
|
|
|
|
}
|
2019-11-14 21:11:46 -06:00
|
|
|
if (arg == "-extmem") {
|
|
|
|
extmem = true;
|
|
|
|
extmem_counter = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2016-07-30 05:46:06 -05:00
|
|
|
if (arg == "-defparam") {
|
|
|
|
defparam = true;
|
|
|
|
continue;
|
|
|
|
}
|
2017-10-03 10:31:21 -05:00
|
|
|
if (arg == "-decimal") {
|
|
|
|
decimal = true;
|
|
|
|
continue;
|
2017-10-01 11:04:17 -05:00
|
|
|
}
|
2019-02-28 16:56:55 -06:00
|
|
|
if (arg == "-siminit") {
|
|
|
|
siminit = true;
|
|
|
|
continue;
|
|
|
|
}
|
2013-11-22 08:01:12 -06:00
|
|
|
if (arg == "-blackboxes") {
|
|
|
|
blackboxes = true;
|
2013-03-28 03:20:10 -05:00
|
|
|
continue;
|
|
|
|
}
|
2013-09-03 12:10:11 -05:00
|
|
|
if (arg == "-selected") {
|
|
|
|
selected = true;
|
|
|
|
continue;
|
|
|
|
}
|
2020-11-25 11:21:41 -06:00
|
|
|
if (arg == "-simple-lhs") {
|
|
|
|
simple_lhs = true;
|
|
|
|
continue;
|
|
|
|
}
|
2016-11-01 05:30:27 -05:00
|
|
|
if (arg == "-v") {
|
|
|
|
verbose = true;
|
|
|
|
continue;
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
extra_args(f, filename, args, argidx);
|
2019-11-14 21:11:46 -06:00
|
|
|
if (extmem)
|
|
|
|
{
|
2020-04-14 07:12:50 -05:00
|
|
|
if (filename == "<stdout>")
|
2019-11-14 21:11:46 -06:00
|
|
|
log_cmd_error("Option -extmem must be used with a filename.\n");
|
|
|
|
extmem_prefix = filename.substr(0, filename.rfind('.'));
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2022-01-24 09:02:29 -06:00
|
|
|
log_push();
|
2022-11-10 09:28:47 -06:00
|
|
|
if (!noexpr) {
|
|
|
|
Pass::call(design, "bmuxmap");
|
|
|
|
Pass::call(design, "demuxmap");
|
|
|
|
}
|
2021-12-11 09:07:29 -06:00
|
|
|
Pass::call(design, "clean_zerowidth");
|
2022-01-24 09:02:29 -06:00
|
|
|
log_pop();
|
2021-12-11 09:07:29 -06:00
|
|
|
|
2015-01-23 17:13:27 -06:00
|
|
|
design->sort();
|
|
|
|
|
2014-08-23 06:54:21 -05:00
|
|
|
*f << stringf("/* Generated by %s */\n", yosys_version_str);
|
2020-04-01 00:25:10 -05:00
|
|
|
for (auto module : design->modules()) {
|
|
|
|
if (module->get_blackbox_attribute() != blackboxes)
|
2013-09-03 12:10:11 -05:00
|
|
|
continue;
|
2020-04-01 00:25:10 -05:00
|
|
|
if (selected && !design->selected_whole_module(module->name)) {
|
|
|
|
if (design->selected_module(module->name))
|
|
|
|
log_cmd_error("Can't handle partially selected module %s!\n", log_id(module->name));
|
2013-09-03 12:10:11 -05:00
|
|
|
continue;
|
2013-03-28 03:20:10 -05:00
|
|
|
}
|
2020-04-01 00:25:10 -05:00
|
|
|
log("Dumping module `%s'.\n", module->name.c_str());
|
|
|
|
dump_module(*f, "", module);
|
2013-09-03 12:10:11 -05:00
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2019-03-11 14:12:28 -05:00
|
|
|
auto_name_map.clear();
|
|
|
|
reg_wires.clear();
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
} VerilogBackend;
|
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
PRIVATE_NAMESPACE_END
|