yosys/passes/fsm/fsm_recode.cc

198 lines
7.3 KiB
C++
Raw Normal View History

2013-01-05 04:13:26 -06:00
/*
* yosys -- Yosys Open SYnthesis Suite
*
* Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
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.
*
*/
#include "kernel/log.h"
#include "kernel/register.h"
#include "kernel/sigtools.h"
#include "kernel/consteval.h"
#include "kernel/celltypes.h"
#include "fsmdata.h"
#include <math.h>
2013-01-05 04:13:26 -06:00
#include <string.h>
#include <errno.h>
2013-01-05 04:13:26 -06:00
2014-09-27 09:17:53 -05:00
USING_YOSYS_NAMESPACE
PRIVATE_NAMESPACE_BEGIN
2013-01-05 04:13:26 -06:00
static void fm_set_fsm_print(RTLIL::Cell *cell, RTLIL::Module *module, FsmData &fsm_data, const char *prefix, FILE *f)
{
std::string name = cell->parameters["\\NAME"].decode_string();
2013-01-05 04:13:26 -06:00
fprintf(f, "set_fsm_state_vector {");
for (int i = fsm_data.state_bits-1; i >= 0; i--)
fprintf(f, " %s_reg[%d]", name[0] == '\\' ? name.substr(1).c_str() : name.c_str(), i);
fprintf(f, " } -name {%s_%s} {%s:/WORK/%s}\n", prefix, RTLIL::unescape_id(name).c_str(),
2013-01-05 04:13:26 -06:00
prefix, RTLIL::unescape_id(module->name).c_str());
fprintf(f, "set_fsm_encoding {");
for (int i = 0; i < GetSize(fsm_data.state_table); i++) {
fprintf(f, " s%d=2#", i);
for (int j = GetSize(fsm_data.state_table[i].bits)-1; j >= 0; j--)
2013-01-05 04:13:26 -06:00
fprintf(f, "%c", fsm_data.state_table[i].bits[j] == RTLIL::State::S1 ? '1' : '0');
}
fprintf(f, " } -name {%s_%s} {%s:/WORK/%s}\n",
prefix, RTLIL::unescape_id(name).c_str(),
2013-01-05 04:13:26 -06:00
prefix, RTLIL::unescape_id(module->name).c_str());
}
2015-01-30 15:46:53 -06:00
static void fsm_recode(RTLIL::Cell *cell, RTLIL::Module *module, FILE *fm_set_fsm_file, FILE *encfile, std::string default_encoding)
2013-01-05 04:13:26 -06:00
{
std::string encoding = cell->attributes.count("\\fsm_encoding") ? cell->attributes.at("\\fsm_encoding").decode_string() : "auto";
log("Recoding FSM `%s' from module `%s' using `%s' encoding:\n", cell->name.c_str(), module->name.c_str(), encoding.c_str());
2016-11-02 07:15:49 -05:00
if (encoding != "none" && encoding != "user" && encoding != "one-hot" && encoding != "binary" && encoding != "auto") {
log(" unknown encoding `%s': using auto instead.\n", encoding.c_str());
encoding = "auto";
}
2016-11-02 07:15:49 -05:00
if (encoding == "none" || encoding == "user") {
log(" nothing to do for encoding `%s'.\n", encoding.c_str());
return;
}
2013-01-05 04:13:26 -06:00
FsmData fsm_data;
fsm_data.copy_from_cell(cell);
if (fm_set_fsm_file != NULL)
fm_set_fsm_print(cell, module, fsm_data, "r", fm_set_fsm_file);
if (encoding == "auto") {
if (!default_encoding.empty())
encoding = default_encoding;
else
encoding = GetSize(fsm_data.state_table) < 32 ? "one-hot" : "binary";
log(" mapping auto encoding to `%s` for this FSM.\n", encoding.c_str());
}
if (encoding == "one-hot") {
fsm_data.state_bits = fsm_data.state_table.size();
} else
if (encoding == "binary") {
2016-02-13 09:52:16 -06:00
int new_num_state_bits = ceil_log2(fsm_data.state_table.size());
if (fsm_data.state_bits == new_num_state_bits) {
log(" existing encoding is already a packed binary encoding.\n");
return;
}
fsm_data.state_bits = new_num_state_bits;
} else
log_error("FSM encoding `%s' is not supported!\n", encoding.c_str());
2015-07-02 04:14:30 -05:00
2015-01-30 15:46:53 -06:00
if (encfile)
fprintf(encfile, ".fsm %s %s\n", log_id(module), RTLIL::unescape_id(cell->parameters["\\NAME"].decode_string()).c_str());
int state_idx_counter = fsm_data.reset_state >= 0 ? 1 : 0;
for (int i = 0; i < int(fsm_data.state_table.size()); i++)
2013-01-05 04:13:26 -06:00
{
int state_idx = fsm_data.reset_state == i ? 0 : state_idx_counter++;
2013-01-05 04:13:26 -06:00
RTLIL::Const new_code;
if (encoding == "one-hot") {
new_code = RTLIL::Const(RTLIL::State::Sa, fsm_data.state_bits);
new_code.bits[state_idx] = RTLIL::State::S1;
} else
if (encoding == "binary") {
new_code = RTLIL::Const(state_idx, fsm_data.state_bits);
} else
log_abort();
2013-01-05 04:13:26 -06:00
log(" %s -> %s\n", fsm_data.state_table[i].as_string().c_str(), new_code.as_string().c_str());
2015-01-30 15:46:53 -06:00
if (encfile)
fprintf(encfile, ".map %s %s\n", fsm_data.state_table[i].as_string().c_str(), new_code.as_string().c_str());
2013-01-05 04:13:26 -06:00
fsm_data.state_table[i] = new_code;
}
if (fm_set_fsm_file != NULL)
fm_set_fsm_print(cell, module, fsm_data, "i", fm_set_fsm_file);
fsm_data.copy_to_cell(cell);
}
struct FsmRecodePass : public Pass {
2013-03-01 05:35:12 -06:00
FsmRecodePass() : Pass("fsm_recode", "recoding finite state machines") { }
void help() YS_OVERRIDE
2013-03-01 05:35:12 -06:00
{
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
log("\n");
2015-01-30 15:46:53 -06:00
log(" fsm_recode [options] [selection]\n");
2013-03-01 05:35:12 -06:00
log("\n");
log("This pass reassign the state encodings for FSM cells. At the moment only\n");
2015-01-30 15:46:53 -06:00
log("one-hot encoding and binary encoding is supported.\n");
2015-07-02 04:14:30 -05:00
2015-01-30 15:46:53 -06:00
log(" -encoding <type>\n");
log(" specify the encoding scheme used for FSMs without the\n");
log(" 'fsm_encoding' attribute or with the attribute set to `auto'.\n");
log("\n");
log(" -fm_set_fsm_file <file>\n");
log(" generate a file containing the mapping from old to new FSM encoding\n");
log(" in form of Synopsys Formality set_fsm_* commands.\n");
log("\n");
log(" -encfile <file>\n");
log(" write the mappings from old to new FSM encoding to a file in the\n");
log(" following format:\n");
2013-03-01 05:35:12 -06:00
log("\n");
2015-01-30 15:46:53 -06:00
log(" .fsm <module_name> <state_signal>\n");
log(" .map <old_bitpattern> <new_bitpattern>\n");
2013-03-01 05:35:12 -06:00
log("\n");
}
void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
2013-01-05 04:13:26 -06:00
{
FILE *fm_set_fsm_file = NULL;
2015-01-30 15:46:53 -06:00
FILE *encfile = NULL;
std::string default_encoding;
2013-01-05 04:13:26 -06:00
2016-04-21 16:28:37 -05:00
log_header(design, "Executing FSM_RECODE pass (re-assigning FSM state encoding).\n");
2013-01-05 04:13:26 -06:00
size_t argidx;
for (argidx = 1; argidx < args.size(); argidx++) {
std::string arg = args[argidx];
if (arg == "-fm_set_fsm_file" && argidx+1 < args.size() && fm_set_fsm_file == NULL) {
fm_set_fsm_file = fopen(args[++argidx].c_str(), "w");
if (fm_set_fsm_file == NULL)
log_error("Can't open fm_set_fsm_file `%s' for writing: %s\n", args[argidx].c_str(), strerror(errno));
continue;
}
2015-01-30 15:46:53 -06:00
if (arg == "-encfile" && argidx+1 < args.size() && encfile == NULL) {
encfile = fopen(args[++argidx].c_str(), "w");
if (encfile == NULL)
log_error("Can't open encfile `%s' for writing: %s\n", args[argidx].c_str(), strerror(errno));
continue;
}
if (arg == "-encoding" && argidx+1 < args.size() && default_encoding.empty()) {
default_encoding = args[++argidx];
continue;
}
2013-01-05 04:13:26 -06:00
break;
}
extra_args(args, argidx, design);
for (auto &mod_it : design->modules_)
2013-03-01 05:35:12 -06:00
if (design->selected(mod_it.second))
for (auto &cell_it : mod_it.second->cells_)
2013-03-01 05:35:12 -06:00
if (cell_it.second->type == "$fsm" && design->selected(mod_it.second, cell_it.second))
2015-01-30 15:46:53 -06:00
fsm_recode(cell_it.second, mod_it.second, fm_set_fsm_file, encfile, default_encoding);
2013-01-05 04:13:26 -06:00
if (fm_set_fsm_file != NULL)
fclose(fm_set_fsm_file);
2015-01-30 15:46:53 -06:00
if (encfile != NULL)
fclose(encfile);
2013-01-05 04:13:26 -06:00
}
} FsmRecodePass;
2015-07-02 04:14:30 -05:00
2014-09-27 09:17:53 -05:00
PRIVATE_NAMESPACE_END