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"
|
|
|
|
#include <string.h>
|
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
USING_YOSYS_NAMESPACE
|
|
|
|
PRIVATE_NAMESPACE_BEGIN
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
struct FsmExpand
|
|
|
|
{
|
|
|
|
RTLIL::Module *module;
|
|
|
|
RTLIL::Cell *fsm_cell;
|
2016-11-02 03:31:39 -05:00
|
|
|
bool full_mode;
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
SigMap assign_map;
|
2014-08-14 19:40:46 -05:00
|
|
|
SigSet<RTLIL::Cell*, RTLIL::sort_by_name_id<RTLIL::Cell>> sig2driver, sig2user;
|
2013-01-05 04:13:26 -06:00
|
|
|
CellTypes ct;
|
|
|
|
|
2014-08-14 19:40:46 -05:00
|
|
|
std::set<RTLIL::Cell*, RTLIL::sort_by_name_id<RTLIL::Cell>> merged_set;
|
|
|
|
std::set<RTLIL::Cell*, RTLIL::sort_by_name_id<RTLIL::Cell>> current_set;
|
|
|
|
std::set<RTLIL::Cell*, RTLIL::sort_by_name_id<RTLIL::Cell>> no_candidate_set;
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
bool already_optimized;
|
|
|
|
int limit_transitions;
|
|
|
|
|
|
|
|
bool is_cell_merge_candidate(RTLIL::Cell *cell)
|
|
|
|
{
|
2020-04-02 11:51:32 -05:00
|
|
|
if (full_mode || cell->type == ID($_MUX_))
|
2016-11-02 03:31:39 -05:00
|
|
|
return true;
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->type.in(ID($mux), ID($pmux)))
|
2020-03-12 14:57:01 -05:00
|
|
|
if (cell->getPort(ID::A).size() < 2)
|
2013-03-24 20:24:11 -05:00
|
|
|
return true;
|
2013-03-24 11:59:44 -05:00
|
|
|
|
2017-01-26 02:19:28 -06:00
|
|
|
int in_bits = 0;
|
2013-01-05 04:13:26 -06:00
|
|
|
RTLIL::SigSpec new_signals;
|
2017-01-26 02:19:28 -06:00
|
|
|
|
2020-03-12 14:57:01 -05:00
|
|
|
if (cell->hasPort(ID::A)) {
|
|
|
|
in_bits += GetSize(cell->getPort(ID::A));
|
|
|
|
new_signals.append(assign_map(cell->getPort(ID::A)));
|
2017-01-26 02:19:28 -06:00
|
|
|
}
|
|
|
|
|
2020-03-12 14:57:01 -05:00
|
|
|
if (cell->hasPort(ID::B)) {
|
|
|
|
in_bits += GetSize(cell->getPort(ID::B));
|
|
|
|
new_signals.append(assign_map(cell->getPort(ID::B)));
|
2017-01-26 02:19:28 -06:00
|
|
|
}
|
|
|
|
|
2020-03-12 14:57:01 -05:00
|
|
|
if (cell->hasPort(ID::S)) {
|
|
|
|
in_bits += GetSize(cell->getPort(ID::S));
|
|
|
|
new_signals.append(assign_map(cell->getPort(ID::S)));
|
2017-01-26 02:19:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (in_bits > 8)
|
|
|
|
return false;
|
|
|
|
|
2020-03-12 14:57:01 -05:00
|
|
|
if (cell->hasPort(ID::Y))
|
|
|
|
new_signals.append(assign_map(cell->getPort(ID::Y)));
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
new_signals.sort_and_unify();
|
|
|
|
new_signals.remove_const();
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
new_signals.remove(assign_map(fsm_cell->getPort(ID::CTRL_IN)));
|
|
|
|
new_signals.remove(assign_map(fsm_cell->getPort(ID::CTRL_OUT)));
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2014-07-22 13:15:14 -05:00
|
|
|
if (new_signals.size() > 3)
|
2013-03-24 20:24:11 -05:00
|
|
|
return false;
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void create_current_set()
|
|
|
|
{
|
|
|
|
std::vector<RTLIL::Cell*> cell_list;
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
for (auto c : sig2driver.find(assign_map(fsm_cell->getPort(ID::CTRL_IN))))
|
2013-01-05 04:13:26 -06:00
|
|
|
cell_list.push_back(c);
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
for (auto c : sig2user.find(assign_map(fsm_cell->getPort(ID::CTRL_OUT))))
|
2013-01-05 04:13:26 -06:00
|
|
|
cell_list.push_back(c);
|
|
|
|
|
|
|
|
current_set.clear();
|
|
|
|
for (auto c : cell_list)
|
|
|
|
{
|
|
|
|
if (merged_set.count(c) > 0 || current_set.count(c) > 0 || no_candidate_set.count(c) > 0)
|
|
|
|
continue;
|
2014-07-26 07:32:50 -05:00
|
|
|
for (auto &p : c->connections()) {
|
2020-03-12 14:57:01 -05:00
|
|
|
if (p.first != ID::A && p.first != ID::B && p.first != ID::S && p.first != ID::Y)
|
2013-01-05 04:13:26 -06:00
|
|
|
goto next_cell;
|
|
|
|
}
|
|
|
|
if (!is_cell_merge_candidate(c)) {
|
|
|
|
no_candidate_set.insert(c);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
current_set.insert(c);
|
|
|
|
next_cell:;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void optimze_as_needed()
|
|
|
|
{
|
|
|
|
if (already_optimized)
|
|
|
|
return;
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
int trans_num = fsm_cell->parameters[ID::TRANS_NUM].as_int();
|
2013-01-05 04:13:26 -06:00
|
|
|
if (trans_num > limit_transitions)
|
|
|
|
{
|
|
|
|
log(" grown transition table to %d entries -> optimize.\n", trans_num);
|
|
|
|
FsmData::optimize_fsm(fsm_cell, module);
|
|
|
|
already_optimized = true;
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
trans_num = fsm_cell->parameters[ID::TRANS_NUM].as_int();
|
2013-01-05 04:13:26 -06:00
|
|
|
log(" transition table size after optimizaton: %d\n", trans_num);
|
|
|
|
limit_transitions = 16 * trans_num;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void merge_cell_into_fsm(RTLIL::Cell *cell)
|
|
|
|
{
|
|
|
|
optimze_as_needed();
|
|
|
|
|
|
|
|
log(" merging %s cell %s.\n", cell->type.c_str(), cell->name.c_str());
|
|
|
|
merged_set.insert(cell);
|
|
|
|
already_optimized = false;
|
|
|
|
|
|
|
|
RTLIL::SigSpec input_sig, output_sig;
|
|
|
|
|
2014-07-26 07:32:50 -05:00
|
|
|
for (auto &p : cell->connections())
|
2013-01-05 04:13:26 -06:00
|
|
|
if (ct.cell_output(cell->type, p.first))
|
|
|
|
output_sig.append(assign_map(p.second));
|
|
|
|
else
|
|
|
|
input_sig.append(assign_map(p.second));
|
|
|
|
input_sig.sort_and_unify();
|
|
|
|
input_sig.remove_const();
|
|
|
|
|
|
|
|
std::vector<RTLIL::Const> truth_tab;
|
|
|
|
|
2014-07-22 13:15:14 -05:00
|
|
|
for (int i = 0; i < (1 << input_sig.size()); i++) {
|
|
|
|
RTLIL::Const in_val(i, input_sig.size());
|
2013-01-05 04:13:26 -06:00
|
|
|
RTLIL::SigSpec A, B, S;
|
2020-03-12 14:57:01 -05:00
|
|
|
if (cell->hasPort(ID::A))
|
|
|
|
A = assign_map(cell->getPort(ID::A));
|
|
|
|
if (cell->hasPort(ID::B))
|
|
|
|
B = assign_map(cell->getPort(ID::B));
|
|
|
|
if (cell->hasPort(ID::S))
|
|
|
|
S = assign_map(cell->getPort(ID::S));
|
2013-01-05 04:13:26 -06:00
|
|
|
A.replace(input_sig, RTLIL::SigSpec(in_val));
|
|
|
|
B.replace(input_sig, RTLIL::SigSpec(in_val));
|
|
|
|
S.replace(input_sig, RTLIL::SigSpec(in_val));
|
2014-07-28 04:08:55 -05:00
|
|
|
log_assert(A.is_fully_const());
|
|
|
|
log_assert(B.is_fully_const());
|
|
|
|
log_assert(S.is_fully_const());
|
2013-01-05 04:13:26 -06:00
|
|
|
truth_tab.push_back(ct.eval(cell, A.as_const(), B.as_const(), S.as_const()));
|
|
|
|
}
|
|
|
|
|
|
|
|
FsmData fsm_data;
|
|
|
|
fsm_data.copy_from_cell(fsm_cell);
|
|
|
|
|
2014-07-22 13:15:14 -05:00
|
|
|
fsm_data.num_inputs += input_sig.size();
|
2020-04-02 11:51:32 -05:00
|
|
|
RTLIL::SigSpec new_ctrl_in = fsm_cell->getPort(ID::CTRL_IN);
|
2014-07-26 08:57:57 -05:00
|
|
|
new_ctrl_in.append(input_sig);
|
2020-04-02 11:51:32 -05:00
|
|
|
fsm_cell->setPort(ID::CTRL_IN, new_ctrl_in);
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2014-07-22 13:15:14 -05:00
|
|
|
fsm_data.num_outputs += output_sig.size();
|
2020-04-02 11:51:32 -05:00
|
|
|
RTLIL::SigSpec new_ctrl_out = fsm_cell->getPort(ID::CTRL_OUT);
|
2014-07-26 08:57:57 -05:00
|
|
|
new_ctrl_out.append(output_sig);
|
2020-04-02 11:51:32 -05:00
|
|
|
fsm_cell->setPort(ID::CTRL_OUT, new_ctrl_out);
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2017-01-26 02:01:26 -06:00
|
|
|
if (GetSize(input_sig) > 10)
|
|
|
|
log_warning("Cell %s.%s (%s) has %d input bits, merging into FSM %s.%s might be problematic.\n",
|
|
|
|
log_id(cell->module), log_id(cell), log_id(cell->type),
|
|
|
|
GetSize(input_sig), log_id(fsm_cell->module), log_id(fsm_cell));
|
|
|
|
|
|
|
|
if (GetSize(fsm_data.transition_table) > 10000)
|
|
|
|
log_warning("Transition table for FSM %s.%s already has %d rows, merging more cells "
|
|
|
|
"into this FSM might be problematic.\n", log_id(fsm_cell->module), log_id(fsm_cell),
|
|
|
|
GetSize(fsm_data.transition_table));
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
std::vector<FsmData::transition_t> new_transition_table;
|
|
|
|
for (auto &tr : fsm_data.transition_table) {
|
2014-07-22 13:15:14 -05:00
|
|
|
for (int i = 0; i < (1 << input_sig.size()); i++) {
|
2013-01-05 04:13:26 -06:00
|
|
|
FsmData::transition_t new_tr = tr;
|
2014-07-22 13:15:14 -05:00
|
|
|
RTLIL::Const in_val(i, input_sig.size());
|
2013-01-05 04:13:26 -06:00
|
|
|
RTLIL::Const out_val = truth_tab[i];
|
|
|
|
RTLIL::SigSpec ctrl_in = new_tr.ctrl_in;
|
|
|
|
RTLIL::SigSpec ctrl_out = new_tr.ctrl_out;
|
|
|
|
ctrl_in.append(in_val);
|
|
|
|
ctrl_out.append(out_val);
|
|
|
|
new_tr.ctrl_in = ctrl_in.as_const();
|
|
|
|
new_tr.ctrl_out = ctrl_out.as_const();
|
|
|
|
new_transition_table.push_back(new_tr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fsm_data.transition_table.swap(new_transition_table);
|
|
|
|
new_transition_table.clear();
|
|
|
|
|
|
|
|
fsm_data.copy_to_cell(fsm_cell);
|
|
|
|
}
|
|
|
|
|
2016-11-02 03:31:39 -05:00
|
|
|
FsmExpand(RTLIL::Cell *cell, RTLIL::Design *design, RTLIL::Module *mod, bool full)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
|
|
|
module = mod;
|
|
|
|
fsm_cell = cell;
|
2016-11-02 03:31:39 -05:00
|
|
|
full_mode = full;
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
assign_map.set(module);
|
|
|
|
ct.setup_internals();
|
2016-11-02 03:31:39 -05:00
|
|
|
ct.setup_stdcells();
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2014-07-26 18:51:45 -05:00
|
|
|
for (auto &cell_it : module->cells_) {
|
2013-01-05 04:13:26 -06:00
|
|
|
RTLIL::Cell *c = cell_it.second;
|
2013-03-01 05:35:12 -06:00
|
|
|
if (ct.cell_known(c->type) && design->selected(mod, c))
|
2014-07-26 07:32:50 -05:00
|
|
|
for (auto &p : c->connections()) {
|
2013-01-05 04:13:26 -06:00
|
|
|
if (ct.cell_output(c->type, p.first))
|
|
|
|
sig2driver.insert(assign_map(p.second), c);
|
|
|
|
else
|
|
|
|
sig2user.insert(assign_map(p.second), c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void execute()
|
|
|
|
{
|
|
|
|
log("\n");
|
|
|
|
log("Expanding FSM `%s' from module `%s':\n", fsm_cell->name.c_str(), module->name.c_str());
|
|
|
|
|
|
|
|
already_optimized = false;
|
2020-04-02 11:51:32 -05:00
|
|
|
limit_transitions = 16 * fsm_cell->parameters[ID::TRANS_NUM].as_int();
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
for (create_current_set(); current_set.size() > 0; create_current_set()) {
|
|
|
|
for (auto c : current_set)
|
|
|
|
merge_cell_into_fsm(c);
|
|
|
|
}
|
|
|
|
|
2014-07-25 08:05:18 -05:00
|
|
|
for (auto c : merged_set)
|
|
|
|
module->remove(c);
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
if (merged_set.size() > 0 && !already_optimized)
|
|
|
|
FsmData::optimize_fsm(fsm_cell, module);
|
|
|
|
|
2014-10-11 04:42:08 -05:00
|
|
|
log(" merged %d cells into FSM.\n", GetSize(merged_set));
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct FsmExpandPass : public Pass {
|
2013-03-01 05:35:12 -06:00
|
|
|
FsmExpandPass() : Pass("fsm_expand", "expand FSM cells by merging logic into it") { }
|
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");
|
2016-11-02 03:31:39 -05:00
|
|
|
log(" fsm_expand [-full] [selection]\n");
|
2013-03-01 05:35:12 -06:00
|
|
|
log("\n");
|
2013-03-17 16:02:30 -05:00
|
|
|
log("The fsm_extract pass is conservative about the cells that belong to a finite\n");
|
2013-03-01 05:35:12 -06:00
|
|
|
log("state machine. This pass can be used to merge additional auxiliary gates into\n");
|
2015-08-14 03:56:05 -05:00
|
|
|
log("the finite state machine.\n");
|
2013-03-01 05:35:12 -06:00
|
|
|
log("\n");
|
2016-11-02 03:31:39 -05:00
|
|
|
log("By default, fsm_expand is still a bit conservative regarding merging larger\n");
|
|
|
|
log("word-wide cells. Call with -full to consider all cells for merging.\n");
|
|
|
|
log("\n");
|
2013-03-01 05:35:12 -06:00
|
|
|
}
|
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-11-02 03:31:39 -05:00
|
|
|
bool full_mode = false;
|
|
|
|
|
2016-04-21 16:28:37 -05:00
|
|
|
log_header(design, "Executing FSM_EXPAND pass (merging auxiliary logic into FSMs).\n");
|
2016-11-02 03:31:39 -05:00
|
|
|
|
|
|
|
size_t argidx;
|
|
|
|
for (argidx = 1; argidx < args.size(); argidx++) {
|
|
|
|
if (args[argidx] == "-full") {
|
|
|
|
full_mode = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
extra_args(args, argidx, design);
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
for (auto mod : design->selected_modules()) {
|
2013-01-05 04:13:26 -06:00
|
|
|
std::vector<RTLIL::Cell*> fsm_cells;
|
2020-04-02 11:51:32 -05:00
|
|
|
for (auto cell : mod->selected_cells())
|
|
|
|
if (cell->type == ID($fsm))
|
|
|
|
fsm_cells.push_back(cell);
|
2013-01-05 04:13:26 -06:00
|
|
|
for (auto c : fsm_cells) {
|
2020-04-02 11:51:32 -05:00
|
|
|
FsmExpand fsm_expand(c, design, mod, full_mode);
|
2013-01-05 04:13:26 -06:00
|
|
|
fsm_expand.execute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} FsmExpandPass;
|
2015-07-02 04:14:30 -05:00
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
PRIVATE_NAMESPACE_END
|