yosys/passes/fsm/fsm_expand.cc

276 lines
8.2 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>
*
* 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.
*
* 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>
struct FsmExpand
{
RTLIL::Module *module;
RTLIL::Cell *fsm_cell;
SigMap assign_map;
2013-08-09 05:42:32 -05:00
SigSet<RTLIL::Cell*, RTLIL::sort_by_name<RTLIL::Cell>> sig2driver, sig2user;
2013-01-05 04:13:26 -06:00
CellTypes ct;
2013-08-09 05:42:32 -05:00
std::set<RTLIL::Cell*, RTLIL::sort_by_name<RTLIL::Cell>> merged_set;
std::set<RTLIL::Cell*, RTLIL::sort_by_name<RTLIL::Cell>> current_set;
std::set<RTLIL::Cell*, RTLIL::sort_by_name<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)
{
if (cell->type == "$mux" || cell->type == "$pmux" || cell->type == "$safe_pmux")
if (cell->get("\\A").size() < 2)
return true;
2013-01-05 04:13:26 -06:00
RTLIL::SigSpec new_signals;
2014-07-26 09:11:28 -05:00
if (cell->has("\\A"))
new_signals.append(assign_map(cell->get("\\A")));
2014-07-26 09:11:28 -05:00
if (cell->has("\\B"))
new_signals.append(assign_map(cell->get("\\B")));
2014-07-26 09:11:28 -05:00
if (cell->has("\\S"))
new_signals.append(assign_map(cell->get("\\S")));
2014-07-26 09:11:28 -05:00
if (cell->has("\\Y"))
new_signals.append(assign_map(cell->get("\\Y")));
2013-01-05 04:13:26 -06:00
new_signals.sort_and_unify();
new_signals.remove_const();
new_signals.remove(assign_map(fsm_cell->get("\\CTRL_IN")));
new_signals.remove(assign_map(fsm_cell->get("\\CTRL_OUT")));
2013-01-05 04:13:26 -06:00
if (new_signals.size() > 3)
return false;
2014-07-26 09:11:28 -05:00
if (cell->has("\\Y")) {
new_signals.append(assign_map(cell->get("\\Y")));
2013-01-05 04:13:26 -06:00
new_signals.sort_and_unify();
new_signals.remove_const();
new_signals.remove(assign_map(fsm_cell->get("\\CTRL_IN")));
new_signals.remove(assign_map(fsm_cell->get("\\CTRL_OUT")));
2013-01-05 04:13:26 -06:00
}
if (new_signals.size() > 2)
2013-01-05 04:13:26 -06:00
return false;
return true;
}
void create_current_set()
{
std::vector<RTLIL::Cell*> cell_list;
for (auto c : sig2driver.find(assign_map(fsm_cell->get("\\CTRL_IN"))))
2013-01-05 04:13:26 -06:00
cell_list.push_back(c);
for (auto c : sig2user.find(assign_map(fsm_cell->get("\\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;
for (auto &p : c->connections()) {
2013-01-05 04:13:26 -06:00
if (p.first != "\\A" && p.first != "\\B" && p.first != "\\S" && p.first != "\\Y")
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;
int trans_num = fsm_cell->parameters["\\TRANS_NUM"].as_int();
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;
trans_num = fsm_cell->parameters["\\TRANS_NUM"].as_int();
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;
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;
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;
2014-07-26 09:11:28 -05:00
if (cell->has("\\A"))
A = assign_map(cell->get("\\A"));
2014-07-26 09:11:28 -05:00
if (cell->has("\\B"))
B = assign_map(cell->get("\\B"));
2014-07-26 09:11:28 -05:00
if (cell->has("\\S"))
S = assign_map(cell->get("\\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));
assert(A.is_fully_const());
assert(B.is_fully_const());
assert(S.is_fully_const());
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);
fsm_data.num_inputs += input_sig.size();
RTLIL::SigSpec new_ctrl_in = fsm_cell->get("\\CTRL_IN");
new_ctrl_in.append(input_sig);
fsm_cell->set("\\CTRL_IN", new_ctrl_in);
2013-01-05 04:13:26 -06:00
fsm_data.num_outputs += output_sig.size();
RTLIL::SigSpec new_ctrl_out = fsm_cell->get("\\CTRL_OUT");
new_ctrl_out.append(output_sig);
fsm_cell->set("\\CTRL_OUT", new_ctrl_out);
2013-01-05 04:13:26 -06:00
std::vector<FsmData::transition_t> new_transition_table;
for (auto &tr : fsm_data.transition_table) {
for (int i = 0; i < (1 << input_sig.size()); i++) {
2013-01-05 04:13:26 -06:00
FsmData::transition_t new_tr = tr;
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);
}
2013-03-01 05:35:12 -06:00
FsmExpand(RTLIL::Cell *cell, RTLIL::Design *design, RTLIL::Module *mod)
2013-01-05 04:13:26 -06:00
{
module = mod;
fsm_cell = cell;
assign_map.set(module);
ct.setup_internals();
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))
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;
limit_transitions = 16 * fsm_cell->parameters["\\TRANS_NUM"].as_int();
for (create_current_set(); current_set.size() > 0; create_current_set()) {
for (auto c : current_set)
merge_cell_into_fsm(c);
}
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);
log(" merged %zd cells into FSM.\n", merged_set.size());
}
};
struct FsmExpandPass : public Pass {
2013-03-01 05:35:12 -06:00
FsmExpandPass() : Pass("fsm_expand", "expand FSM cells by merging logic into it") { }
virtual void help()
{
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
log("\n");
log(" fsm_expand [selection]\n");
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");
log("the finate state machine.\n");
log("\n");
}
2013-01-05 04:13:26 -06:00
virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
{
2013-03-01 05:35:12 -06:00
log_header("Executing FSM_EXPAND pass (merging auxiliary logic into FSMs).\n");
2013-01-05 04:13:26 -06:00
extra_args(args, 1, design);
for (auto &mod_it : design->modules) {
2013-03-01 05:35:12 -06:00
if (!design->selected(mod_it.second))
continue;
2013-01-05 04:13:26 -06:00
std::vector<RTLIL::Cell*> fsm_cells;
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))
2013-01-05 04:13:26 -06:00
fsm_cells.push_back(cell_it.second);
for (auto c : fsm_cells) {
2013-03-01 05:35:12 -06:00
FsmExpand fsm_expand(c, design, mod_it.second);
2013-01-05 04:13:26 -06:00
fsm_expand.execute();
}
}
}
} FsmExpandPass;