35 std::string name = cell->
parameters[
"\\NAME"].decode_string();
37 fprintf(f,
"set_fsm_state_vector {");
38 for (
int i = fsm_data.
state_bits-1; i >= 0; i--)
39 fprintf(f,
" %s_reg[%d]", name[0] ==
'\\' ? name.substr(1).c_str() : name.c_str(), i);
40 fprintf(f,
" } -name {%s_%s} {%s:/WORK/%s}\n", prefix,
RTLIL::unescape_id(name).c_str(),
43 fprintf(f,
"set_fsm_encoding {");
45 fprintf(f,
" s%d=2#", i);
49 fprintf(f,
" } -name {%s_%s} {%s:/WORK/%s}\n",
56 std::string encoding = cell->attributes.count(
"\\fsm_encoding") ? cell->attributes.at(
"\\fsm_encoding").decode_string() :
"auto";
58 log(
"Recoding FSM `%s' from module `%s' using `%s' encoding:\n", cell->
name.
c_str(), module->
name.
c_str(), encoding.c_str());
60 if (encoding !=
"none" && encoding !=
"one-hot" && encoding !=
"binary" && encoding !=
"auto") {
61 log(
" unknown encoding `%s': using auto instead.\n", encoding.c_str());
65 if (encoding ==
"none") {
66 log(
" nothing to do for encoding `none'.\n");
73 if (fm_set_fsm_file !=
NULL)
76 if (encoding ==
"auto") {
77 if (!default_encoding.empty())
78 encoding = default_encoding;
81 log(
" mapping auto encoding to `%s` for this FSM.\n", encoding.c_str());
84 if (encoding ==
"one-hot") {
87 if (encoding ==
"binary") {
88 int new_num_state_bits = ceil(log2(fsm_data.
state_table.size()));
89 if (fsm_data.
state_bits == new_num_state_bits) {
90 log(
" existing encoding is already a packed binary encoding.\n");
95 log_error(
"FSM encoding `%s' is not supported!\n", encoding.c_str());
97 int state_idx_counter = fsm_data.
reset_state >= 0 ? 1 : 0;
98 for (
int i = 0; i < int(fsm_data.
state_table.size()); i++)
100 int state_idx = fsm_data.
reset_state == i ? 0 : state_idx_counter++;
103 if (encoding ==
"one-hot") {
107 if (encoding ==
"binary") {
116 if (fm_set_fsm_file !=
NULL)
128 log(
" fsm_recode [-encoding type] [-fm_set_fsm_file file] [selection]\n");
130 log(
"This pass reassign the state encodings for FSM cells. At the moment only\n");
131 log(
"one-hot encoding and binary encoding is supported. The option -encoding\n");
132 log(
"can be used to specify the encoding scheme used for FSMs without the\n");
133 log(
"`fsm_encoding' attribute (or with the attribute set to `auto'.\n");
135 log(
"The option -fm_set_fsm_file can be used to generate a file containing the\n");
136 log(
"mapping from old to new FSM encoding in form of Synopsys Formality set_fsm_*\n");
142 FILE *fm_set_fsm_file =
NULL;
143 std::string default_encoding;
145 log_header(
"Executing FSM_RECODE pass (re-assigning FSM state encoding).\n");
147 for (argidx = 1; argidx < args.size(); argidx++) {
148 std::string arg = args[argidx];
149 if (arg ==
"-fm_set_fsm_file" && argidx+1 < args.size() && fm_set_fsm_file ==
NULL) {
150 fm_set_fsm_file = fopen(args[++argidx].c_str(),
"w");
151 if (fm_set_fsm_file ==
NULL)
152 log_error(
"Can't open fm_set_fsm_file `%s' for writing: %s\n", args[argidx].c_str(), strerror(errno));
155 if (arg ==
"-encoding" && argidx+1 < args.size() && fm_set_fsm_file ==
NULL) {
156 default_encoding = args[++argidx];
163 for (
auto &mod_it : design->
modules_)
164 if (design->
selected(mod_it.second))
165 for (
auto &cell_it : mod_it.second->cells_)
166 if (cell_it.second->type ==
"$fsm" && design->
selected(mod_it.second, cell_it.second))
167 fsm_recode(cell_it.second, mod_it.second, fm_set_fsm_file, default_encoding);
169 if (fm_set_fsm_file !=
NULL)
170 fclose(fm_set_fsm_file);
const char * c_str() const
bool selected(T1 *module) const
void log_header(const char *format,...)
static std::string unescape_id(std::string str)
void copy_to_cell(RTLIL::Cell *cell)
void log_error(const char *format,...)
std::map< RTLIL::IdString, RTLIL::Const > parameters
FsmRecodePass FsmRecodePass
std::string as_string() const
#define PRIVATE_NAMESPACE_BEGIN
int GetSize(RTLIL::Wire *wire)
static void fsm_recode(RTLIL::Cell *cell, RTLIL::Module *module, FILE *fm_set_fsm_file, std::string default_encoding)
#define PRIVATE_NAMESPACE_END
#define USING_YOSYS_NAMESPACE
std::map< RTLIL::IdString, RTLIL::Module * > modules_
std::vector< RTLIL::Const > state_table
void log(const char *format,...)
void copy_from_cell(RTLIL::Cell *cell)
std::vector< RTLIL::State > bits
USING_YOSYS_NAMESPACE static PRIVATE_NAMESPACE_BEGIN void fm_set_fsm_print(RTLIL::Cell *cell, RTLIL::Module *module, FsmData &fsm_data, const char *prefix, FILE *f)
virtual void execute(std::vector< std::string > args, RTLIL::Design *design)
void extra_args(std::vector< std::string > args, size_t argidx, RTLIL::Design *design, bool select=true)