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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "kernel/log.h"
|
|
|
|
#include "kernel/register.h"
|
|
|
|
#include "kernel/sigtools.h"
|
|
|
|
#include "kernel/consteval.h"
|
|
|
|
#include "kernel/celltypes.h"
|
|
|
|
#include "fsmdata.h"
|
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
USING_YOSYS_NAMESPACE
|
|
|
|
PRIVATE_NAMESPACE_BEGIN
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
static RTLIL::Module *module;
|
|
|
|
static SigMap assign_map;
|
2014-08-02 06:11:01 -05:00
|
|
|
typedef std::pair<RTLIL::Cell*, RTLIL::IdString> sig2driver_entry_t;
|
2013-01-05 04:13:26 -06:00
|
|
|
static SigSet<sig2driver_entry_t> sig2driver, sig2user;
|
|
|
|
static std::set<RTLIL::Cell*> muxtree_cells;
|
|
|
|
static SigPool sig_at_port;
|
|
|
|
|
2020-01-14 15:48:40 -06:00
|
|
|
static bool check_state_mux_tree(RTLIL::SigSpec old_sig, RTLIL::SigSpec sig, pool<Cell*> &recursion_monitor, dict<RTLIL::SigSpec, bool> &mux_tree_cache)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2020-01-14 15:48:40 -06:00
|
|
|
if (mux_tree_cache.find(sig) != mux_tree_cache.end())
|
|
|
|
return mux_tree_cache.at(sig);
|
|
|
|
|
2016-07-09 06:23:06 -05:00
|
|
|
if (sig.is_fully_const() || old_sig == sig) {
|
2020-01-14 15:48:40 -06:00
|
|
|
ret_true:
|
|
|
|
mux_tree_cache[sig] = true;
|
2013-01-05 04:13:26 -06:00
|
|
|
return true;
|
2016-07-09 06:23:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sig_at_port.check_any(assign_map(sig))) {
|
2020-01-14 15:48:40 -06:00
|
|
|
ret_false:
|
|
|
|
mux_tree_cache[sig] = false;
|
2016-07-09 06:23:06 -05:00
|
|
|
return false;
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
std::set<sig2driver_entry_t> cellport_list;
|
|
|
|
sig2driver.find(sig, cellport_list);
|
2015-08-18 07:17:50 -05:00
|
|
|
for (auto &cellport : cellport_list)
|
|
|
|
{
|
2020-04-02 11:51:32 -05:00
|
|
|
if ((cellport.first->type != ID($mux) && cellport.first->type != ID($pmux)) || cellport.second != ID::Y) {
|
2020-01-14 15:48:40 -06:00
|
|
|
goto ret_false;
|
2016-07-09 06:23:06 -05:00
|
|
|
}
|
2015-08-18 07:17:50 -05:00
|
|
|
|
|
|
|
if (recursion_monitor.count(cellport.first)) {
|
|
|
|
log_warning("logic loop in mux tree at signal %s in module %s.\n",
|
|
|
|
log_signal(sig), RTLIL::id2cstr(module->name));
|
2020-01-14 15:48:40 -06:00
|
|
|
goto ret_false;
|
2015-08-18 07:17:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
recursion_monitor.insert(cellport.first);
|
|
|
|
|
2020-03-12 14:57:01 -05:00
|
|
|
RTLIL::SigSpec sig_a = assign_map(cellport.first->getPort(ID::A));
|
|
|
|
RTLIL::SigSpec sig_b = assign_map(cellport.first->getPort(ID::B));
|
2015-08-18 07:17:50 -05:00
|
|
|
|
2020-01-14 15:48:40 -06:00
|
|
|
if (!check_state_mux_tree(old_sig, sig_a, recursion_monitor, mux_tree_cache)) {
|
2015-08-18 07:17:50 -05:00
|
|
|
recursion_monitor.erase(cellport.first);
|
2020-01-14 15:48:40 -06:00
|
|
|
goto ret_false;
|
2015-08-18 07:17:50 -05:00
|
|
|
}
|
|
|
|
|
2014-07-22 13:15:14 -05:00
|
|
|
for (int i = 0; i < sig_b.size(); i += sig_a.size())
|
2020-01-14 15:48:40 -06:00
|
|
|
if (!check_state_mux_tree(old_sig, sig_b.extract(i, sig_a.size()), recursion_monitor, mux_tree_cache)) {
|
2015-08-18 07:17:50 -05:00
|
|
|
recursion_monitor.erase(cellport.first);
|
2020-01-14 15:48:40 -06:00
|
|
|
goto ret_false;
|
2015-08-18 07:17:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
recursion_monitor.erase(cellport.first);
|
2013-01-05 04:13:26 -06:00
|
|
|
muxtree_cells.insert(cellport.first);
|
|
|
|
}
|
|
|
|
|
2020-01-14 15:48:40 -06:00
|
|
|
goto ret_true;
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool check_state_users(RTLIL::SigSpec sig)
|
|
|
|
{
|
|
|
|
if (sig_at_port.check_any(assign_map(sig)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
std::set<sig2driver_entry_t> cellport_list;
|
|
|
|
sig2user.find(sig, cellport_list);
|
|
|
|
for (auto &cellport : cellport_list) {
|
|
|
|
RTLIL::Cell *cell = cellport.first;
|
|
|
|
if (muxtree_cells.count(cell) > 0)
|
|
|
|
continue;
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->type == ID($logic_not) && assign_map(cell->getPort(ID::A)) == sig)
|
2015-09-18 03:46:50 -05:00
|
|
|
continue;
|
2020-03-12 14:57:01 -05:00
|
|
|
if (cellport.second != ID::A && cellport.second != ID::B)
|
2013-01-05 04:13:26 -06:00
|
|
|
return false;
|
2020-03-12 14:57:01 -05:00
|
|
|
if (!cell->hasPort(ID::A) || !cell->hasPort(ID::B) || !cell->hasPort(ID::Y))
|
2013-01-05 04:13:26 -06:00
|
|
|
return false;
|
2014-07-26 07:32:50 -05:00
|
|
|
for (auto &port_it : cell->connections())
|
2020-03-12 14:57:01 -05:00
|
|
|
if (port_it.first != ID::A && port_it.first != ID::B && port_it.first != ID::Y)
|
2013-01-05 04:13:26 -06:00
|
|
|
return false;
|
2020-03-12 14:57:01 -05:00
|
|
|
if (assign_map(cell->getPort(ID::A)) == sig && cell->getPort(ID::B).is_fully_const())
|
2013-01-05 04:13:26 -06:00
|
|
|
continue;
|
2020-03-12 14:57:01 -05:00
|
|
|
if (assign_map(cell->getPort(ID::B)) == sig && cell->getPort(ID::A).is_fully_const())
|
2013-01-05 04:13:26 -06:00
|
|
|
continue;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void detect_fsm(RTLIL::Wire *wire)
|
|
|
|
{
|
2020-03-12 14:57:01 -05:00
|
|
|
bool has_fsm_encoding_attr = wire->attributes.count(ID::fsm_encoding) > 0 && wire->attributes.at(ID::fsm_encoding).decode_string() != "none";
|
|
|
|
bool has_fsm_encoding_none = wire->attributes.count(ID::fsm_encoding) > 0 && wire->attributes.at(ID::fsm_encoding).decode_string() == "none";
|
2020-04-02 11:51:32 -05:00
|
|
|
bool has_init_attr = wire->attributes.count(ID::init) > 0;
|
2016-07-09 06:23:06 -05:00
|
|
|
bool is_module_port = sig_at_port.check_any(assign_map(RTLIL::SigSpec(wire)));
|
|
|
|
bool looks_like_state_reg = false, looks_like_good_state_reg = false;
|
2016-07-09 07:02:49 -05:00
|
|
|
bool is_self_resetting = false;
|
2016-07-09 06:23:06 -05:00
|
|
|
|
|
|
|
if (has_fsm_encoding_none)
|
2013-01-05 04:13:26 -06:00
|
|
|
return;
|
2016-07-09 06:23:06 -05:00
|
|
|
|
|
|
|
if (wire->width <= 1) {
|
|
|
|
if (has_fsm_encoding_attr) {
|
|
|
|
log_warning("Removing fsm_encoding attribute from 1-bit net: %s.%s\n", log_id(wire->module), log_id(wire));
|
2020-03-12 14:57:01 -05:00
|
|
|
wire->attributes.erase(ID::fsm_encoding);
|
2016-07-09 06:23:06 -05:00
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
return;
|
2016-07-09 06:23:06 -05:00
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
std::set<sig2driver_entry_t> cellport_list;
|
|
|
|
sig2driver.find(RTLIL::SigSpec(wire), cellport_list);
|
2016-07-09 06:23:06 -05:00
|
|
|
|
|
|
|
for (auto &cellport : cellport_list)
|
|
|
|
{
|
2020-04-02 11:51:32 -05:00
|
|
|
if ((cellport.first->type != ID($dff) && cellport.first->type != ID($adff)) || cellport.second != ID::Q)
|
2013-01-05 04:13:26 -06:00
|
|
|
continue;
|
2016-07-09 06:23:06 -05:00
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
muxtree_cells.clear();
|
2015-08-18 07:17:50 -05:00
|
|
|
pool<Cell*> recursion_monitor;
|
2020-04-02 11:51:32 -05:00
|
|
|
RTLIL::SigSpec sig_q = assign_map(cellport.first->getPort(ID::Q));
|
|
|
|
RTLIL::SigSpec sig_d = assign_map(cellport.first->getPort(ID::D));
|
2020-01-14 15:48:40 -06:00
|
|
|
dict<RTLIL::SigSpec, bool> mux_tree_cache;
|
2016-07-09 07:02:49 -05:00
|
|
|
|
|
|
|
if (sig_q != assign_map(wire))
|
|
|
|
continue;
|
|
|
|
|
2020-01-14 15:48:40 -06:00
|
|
|
looks_like_state_reg = check_state_mux_tree(sig_q, sig_d, recursion_monitor, mux_tree_cache);
|
2016-07-09 07:02:49 -05:00
|
|
|
looks_like_good_state_reg = check_state_users(sig_q);
|
|
|
|
|
|
|
|
if (!looks_like_state_reg)
|
2016-07-09 06:23:06 -05:00
|
|
|
break;
|
2016-07-09 07:02:49 -05:00
|
|
|
|
|
|
|
ConstEval ce(wire->module);
|
|
|
|
|
|
|
|
std::set<sig2driver_entry_t> cellport_list;
|
|
|
|
sig2user.find(sig_q, cellport_list);
|
|
|
|
|
2019-11-12 07:26:02 -06:00
|
|
|
auto sig_q_bits = sig_q.to_sigbit_pool();
|
|
|
|
|
2016-07-09 07:02:49 -05:00
|
|
|
for (auto &cellport : cellport_list)
|
|
|
|
{
|
|
|
|
RTLIL::Cell *cell = cellport.first;
|
|
|
|
bool set_output = false, clr_output = false;
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->type.in(ID($ne), ID($reduce_or), ID($reduce_bool)))
|
2016-07-09 07:02:49 -05:00
|
|
|
set_output = true;
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->type.in(ID($eq), ID($logic_not), ID($reduce_and)))
|
2016-07-09 07:02:49 -05:00
|
|
|
clr_output = true;
|
|
|
|
|
2019-11-12 07:26:02 -06:00
|
|
|
if (set_output || clr_output) {
|
2016-07-09 07:02:49 -05:00
|
|
|
for (auto &port_it : cell->connections())
|
2019-11-12 10:31:30 -06:00
|
|
|
if (cell->input(port_it.first))
|
|
|
|
for (auto bit : assign_map(port_it.second))
|
|
|
|
if (bit.wire != nullptr && !sig_q_bits.count(bit))
|
|
|
|
goto next_cellport;
|
2016-07-09 07:02:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (set_output || clr_output) {
|
|
|
|
for (auto &port_it : cell->connections())
|
|
|
|
if (cell->output(port_it.first)) {
|
|
|
|
SigSpec sig = assign_map(port_it.second);
|
2017-04-08 22:54:31 -05:00
|
|
|
Const val(set_output ? State::S1 : State::S0, GetSize(sig));
|
2016-07-09 07:02:49 -05:00
|
|
|
ce.set(sig, val);
|
|
|
|
}
|
|
|
|
}
|
2019-11-12 07:26:02 -06:00
|
|
|
next_cellport:;
|
2016-07-09 06:23:06 -05:00
|
|
|
}
|
2016-07-09 07:02:49 -05:00
|
|
|
|
|
|
|
SigSpec sig_y = sig_d, sig_undef;
|
|
|
|
if (ce.eval(sig_y, sig_undef))
|
|
|
|
is_self_resetting = true;
|
2016-07-09 06:23:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (has_fsm_encoding_attr)
|
|
|
|
{
|
|
|
|
vector<string> warnings;
|
|
|
|
|
|
|
|
if (is_module_port)
|
2018-12-07 13:14:07 -06:00
|
|
|
warnings.push_back("Forcing FSM recoding on module port might result in larger circuit.\n");
|
2016-07-09 06:23:06 -05:00
|
|
|
|
|
|
|
if (!looks_like_good_state_reg)
|
2018-12-07 13:14:07 -06:00
|
|
|
warnings.push_back("Users of state reg look like FSM recoding might result in larger circuit.\n");
|
2016-07-09 06:23:06 -05:00
|
|
|
|
|
|
|
if (has_init_attr)
|
2018-12-07 13:14:07 -06:00
|
|
|
warnings.push_back("Initialization value on FSM state register is ignored. Possible simulation-synthesis mismatch!\n");
|
2016-07-09 06:23:06 -05:00
|
|
|
|
|
|
|
if (!looks_like_state_reg)
|
|
|
|
warnings.push_back("Doesn't look like a proper FSM. Possible simulation-synthesis mismatch!\n");
|
|
|
|
|
2016-07-09 07:02:49 -05:00
|
|
|
if (is_self_resetting)
|
|
|
|
warnings.push_back("FSM seems to be self-resetting. Possible simulation-synthesis mismatch!\n");
|
|
|
|
|
2016-07-09 06:23:06 -05:00
|
|
|
if (!warnings.empty()) {
|
|
|
|
string warnmsg = stringf("Regarding the user-specified fsm_encoding attribute on %s.%s:\n", log_id(wire->module), log_id(wire));
|
|
|
|
for (auto w : warnings) warnmsg += " " + w;
|
|
|
|
log_warning("%s", warnmsg.c_str());
|
|
|
|
} else {
|
2017-04-08 22:54:31 -05:00
|
|
|
log("FSM state register %s.%s already has fsm_encoding attribute.\n", log_id(wire->module), log_id(wire));
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
}
|
2016-07-09 06:23:06 -05:00
|
|
|
else
|
2016-07-09 07:02:49 -05:00
|
|
|
if (looks_like_state_reg && looks_like_good_state_reg && !has_init_attr && !is_module_port && !is_self_resetting)
|
2016-07-09 06:23:06 -05:00
|
|
|
{
|
2016-07-09 07:02:49 -05:00
|
|
|
log("Found FSM state register %s.%s.\n", log_id(wire->module), log_id(wire));
|
2020-03-12 14:57:01 -05:00
|
|
|
wire->attributes[ID::fsm_encoding] = RTLIL::Const("auto");
|
2016-07-09 06:23:06 -05:00
|
|
|
}
|
2016-07-09 07:02:49 -05:00
|
|
|
else
|
|
|
|
if (looks_like_state_reg)
|
|
|
|
{
|
|
|
|
log("Not marking %s.%s as FSM state register:\n", log_id(wire->module), log_id(wire));
|
|
|
|
|
|
|
|
if (is_module_port)
|
|
|
|
log(" Register is connected to module port.\n");
|
|
|
|
|
|
|
|
if (!looks_like_good_state_reg)
|
|
|
|
log(" Users of register don't seem to benefit from recoding.\n");
|
|
|
|
|
|
|
|
if (has_init_attr)
|
2018-12-07 13:14:07 -06:00
|
|
|
log(" Register has an initialization value.\n");
|
2016-07-09 07:02:49 -05:00
|
|
|
|
|
|
|
if (is_self_resetting)
|
|
|
|
log(" Circuit seems to be self-resetting.\n");
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
struct FsmDetectPass : public Pass {
|
2013-03-01 05:35:12 -06:00
|
|
|
FsmDetectPass() : Pass("fsm_detect", "finding FSMs in design") { }
|
2020-06-18 18:34:52 -05:00
|
|
|
void help() override
|
2013-03-01 05:35:12 -06:00
|
|
|
{
|
|
|
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
|
|
|
log("\n");
|
|
|
|
log(" fsm_detect [selection]\n");
|
|
|
|
log("\n");
|
2013-03-17 16:02:30 -05:00
|
|
|
log("This pass detects finite state machines by identifying the state signal.\n");
|
2013-03-01 05:35:12 -06:00
|
|
|
log("The state signal is then marked by setting the attribute 'fsm_encoding'\n");
|
|
|
|
log("on the state signal to \"auto\".\n");
|
|
|
|
log("\n");
|
|
|
|
log("Existing 'fsm_encoding' attributes are not changed by this pass.\n");
|
|
|
|
log("\n");
|
2013-03-17 16:02:30 -05:00
|
|
|
log("Signals can be protected from being detected by this pass by setting the\n");
|
|
|
|
log("'fsm_encoding' attribute to \"none\".\n");
|
2013-03-01 05:35:12 -06:00
|
|
|
log("\n");
|
|
|
|
}
|
2020-06-18 18:34:52 -05:00
|
|
|
void execute(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 FSM_DETECT pass (finding FSMs in design).\n");
|
2013-01-05 04:13:26 -06:00
|
|
|
extra_args(args, 1, design);
|
|
|
|
|
|
|
|
CellTypes ct;
|
|
|
|
ct.setup_internals();
|
2022-07-21 07:22:15 -05:00
|
|
|
ct.setup_internals_anyinit();
|
2013-01-05 04:13:26 -06:00
|
|
|
ct.setup_internals_mem();
|
|
|
|
ct.setup_stdcells();
|
|
|
|
ct.setup_stdcells_mem();
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
for (auto mod : design->selected_modules())
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2020-04-02 11:51:32 -05:00
|
|
|
module = mod;
|
2013-01-05 04:13:26 -06:00
|
|
|
assign_map.set(module);
|
|
|
|
|
|
|
|
sig2driver.clear();
|
|
|
|
sig2user.clear();
|
|
|
|
sig_at_port.clear();
|
2020-04-02 11:51:32 -05:00
|
|
|
for (auto cell : module->cells())
|
|
|
|
for (auto &conn_it : cell->connections()) {
|
|
|
|
if (ct.cell_output(cell->type, conn_it.first) || !ct.cell_known(cell->type)) {
|
2013-01-05 04:13:26 -06:00
|
|
|
RTLIL::SigSpec sig = conn_it.second;
|
|
|
|
assign_map.apply(sig);
|
2020-04-02 11:51:32 -05:00
|
|
|
sig2driver.insert(sig, sig2driver_entry_t(cell, conn_it.first));
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
2020-04-02 11:51:32 -05:00
|
|
|
if (!ct.cell_known(cell->type) || ct.cell_input(cell->type, conn_it.first)) {
|
2013-01-05 04:13:26 -06:00
|
|
|
RTLIL::SigSpec sig = conn_it.second;
|
|
|
|
assign_map.apply(sig);
|
2020-04-02 11:51:32 -05:00
|
|
|
sig2user.insert(sig, sig2driver_entry_t(cell, conn_it.first));
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
for (auto wire : module->wires())
|
|
|
|
if (wire->port_id != 0)
|
|
|
|
sig_at_port.add(assign_map(wire));
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
for (auto wire : module->selected_wires())
|
|
|
|
detect_fsm(wire);
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
assign_map.clear();
|
|
|
|
sig2driver.clear();
|
|
|
|
sig2user.clear();
|
|
|
|
muxtree_cells.clear();
|
|
|
|
}
|
|
|
|
} FsmDetectPass;
|
2015-07-02 04:14:30 -05:00
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
PRIVATE_NAMESPACE_END
|