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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-02-01 04:49:11 -06:00
|
|
|
#include "kernel/yosys.h"
|
|
|
|
#include "kernel/sigtools.h"
|
2013-01-05 04:13:26 -06:00
|
|
|
#include "libparse.h"
|
|
|
|
#include <string.h>
|
2014-03-11 08:24:24 -05:00
|
|
|
#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
|
|
|
|
|
|
|
struct cell_mapping {
|
2019-08-15 12:05:08 -05:00
|
|
|
IdString cell_name;
|
2019-08-16 03:22:04 -05:00
|
|
|
std::map<std::string, char> ports;
|
2013-01-05 04:13:26 -06:00
|
|
|
};
|
2014-08-02 06:11:01 -05:00
|
|
|
static std::map<RTLIL::IdString, cell_mapping> cell_mappings;
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2019-08-15 12:05:08 -05:00
|
|
|
static void logmap(IdString dff)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
|
|
|
if (cell_mappings.count(dff) == 0) {
|
|
|
|
log(" unmapped dff cell: %s\n", dff.c_str());
|
|
|
|
} else {
|
2013-10-24 11:20:06 -05:00
|
|
|
log(" %s %s (", cell_mappings[dff].cell_name.c_str(), dff.substr(1).c_str());
|
2013-01-05 04:13:26 -06:00
|
|
|
bool first = true;
|
|
|
|
for (auto &port : cell_mappings[dff].ports) {
|
|
|
|
char arg[3] = { port.second, 0, 0 };
|
|
|
|
if ('a' <= arg[0] && arg[0] <= 'z')
|
|
|
|
arg[1] = arg[0] - ('a' - 'A'), arg[0] = '~';
|
2013-10-24 11:20:06 -05:00
|
|
|
else
|
|
|
|
arg[1] = arg[0], arg[0] = ' ';
|
2013-01-05 04:13:26 -06:00
|
|
|
log("%s.%s(%s)", first ? "" : ", ", port.first.c_str(), arg);
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
log(");\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void logmap_all()
|
|
|
|
{
|
2019-08-15 12:05:08 -05:00
|
|
|
logmap(ID($_DFF_N_));
|
|
|
|
logmap(ID($_DFF_P_));
|
|
|
|
|
|
|
|
logmap(ID($_DFF_NN0_));
|
|
|
|
logmap(ID($_DFF_NN1_));
|
|
|
|
logmap(ID($_DFF_NP0_));
|
|
|
|
logmap(ID($_DFF_NP1_));
|
|
|
|
logmap(ID($_DFF_PN0_));
|
|
|
|
logmap(ID($_DFF_PN1_));
|
|
|
|
logmap(ID($_DFF_PP0_));
|
|
|
|
logmap(ID($_DFF_PP1_));
|
|
|
|
|
|
|
|
logmap(ID($_DFFSR_NNN_));
|
|
|
|
logmap(ID($_DFFSR_NNP_));
|
|
|
|
logmap(ID($_DFFSR_NPN_));
|
|
|
|
logmap(ID($_DFFSR_NPP_));
|
|
|
|
logmap(ID($_DFFSR_PNN_));
|
|
|
|
logmap(ID($_DFFSR_PNP_));
|
|
|
|
logmap(ID($_DFFSR_PPN_));
|
|
|
|
logmap(ID($_DFFSR_PPP_));
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool parse_pin(LibertyAst *cell, LibertyAst *attr, std::string &pin_name, bool &pin_pol)
|
|
|
|
{
|
2020-04-05 22:02:40 -05:00
|
|
|
if (cell == nullptr || attr == nullptr || attr->value.empty())
|
2013-01-05 04:13:26 -06:00
|
|
|
return false;
|
2015-07-02 04:14:30 -05:00
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
std::string value = attr->value;
|
|
|
|
|
2013-10-15 23:32:35 -05:00
|
|
|
for (size_t pos = value.find_first_of("\" \t()"); pos != std::string::npos; pos = value.find_first_of("\" \t()"))
|
2013-01-05 04:13:26 -06:00
|
|
|
value.erase(pos, 1);
|
|
|
|
|
|
|
|
if (value[value.size()-1] == '\'') {
|
|
|
|
pin_name = value.substr(0, value.size()-1);
|
|
|
|
pin_pol = false;
|
2013-10-15 23:32:35 -05:00
|
|
|
} else if (value[0] == '!') {
|
|
|
|
pin_name = value.substr(1, value.size()-1);
|
|
|
|
pin_pol = false;
|
2013-01-05 04:13:26 -06:00
|
|
|
} else {
|
|
|
|
pin_name = value;
|
|
|
|
pin_pol = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto child : cell->children)
|
|
|
|
if (child->id == "pin" && child->args.size() == 1 && child->args[0] == pin_name)
|
|
|
|
return true;
|
2018-11-03 12:07:51 -05:00
|
|
|
|
2018-11-06 05:11:52 -06:00
|
|
|
/* If we end up here, the pin specified in the attribute does not exist, which is an error,
|
|
|
|
or, the attribute contains an expression which we do not yet support.
|
|
|
|
For now, we'll simply produce a warning to let the user know something is up.
|
|
|
|
*/
|
|
|
|
if (pin_name.find_first_of("^*|&") == std::string::npos) {
|
|
|
|
log_warning("Malformed liberty file - cannot find pin '%s' in cell '%s' - skipping.\n", pin_name.c_str(), cell->args[0].c_str());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
log_warning("Found unsupported expression '%s' in pin attribute of cell '%s' - skipping.\n", pin_name.c_str(), cell->args[0].c_str());
|
|
|
|
}
|
2018-11-03 12:07:51 -05:00
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-15 12:05:08 -05:00
|
|
|
static void find_cell(LibertyAst *ast, IdString cell_type, bool clkpol, bool has_reset, bool rstpol, bool rstval, bool prepare_mode)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2020-04-05 22:02:40 -05:00
|
|
|
LibertyAst *best_cell = nullptr;
|
2019-08-16 03:22:04 -05:00
|
|
|
std::map<std::string, char> best_cell_ports;
|
2013-01-05 04:13:26 -06:00
|
|
|
int best_cell_pins = 0;
|
2016-04-05 05:51:04 -05:00
|
|
|
bool best_cell_noninv = false;
|
2014-10-18 08:20:38 -05:00
|
|
|
double best_cell_area = 0;
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
if (ast->id != "library")
|
|
|
|
log_error("Format error in liberty file.\n");
|
|
|
|
|
|
|
|
for (auto cell : ast->children)
|
|
|
|
{
|
|
|
|
if (cell->id != "cell" || cell->args.size() != 1)
|
|
|
|
continue;
|
|
|
|
|
2015-05-31 00:51:12 -05:00
|
|
|
LibertyAst *dn = cell->find("dont_use");
|
2020-04-05 22:02:40 -05:00
|
|
|
if (dn != nullptr && dn->value == "true")
|
2015-05-31 00:51:12 -05:00
|
|
|
continue;
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
LibertyAst *ff = cell->find("ff");
|
2020-04-05 22:02:40 -05:00
|
|
|
if (ff == nullptr)
|
2013-01-05 04:13:26 -06:00
|
|
|
continue;
|
|
|
|
|
|
|
|
std::string cell_clk_pin, cell_rst_pin, cell_next_pin;
|
|
|
|
bool cell_clk_pol, cell_rst_pol, cell_next_pol;
|
|
|
|
|
|
|
|
if (!parse_pin(cell, ff->find("clocked_on"), cell_clk_pin, cell_clk_pol) || cell_clk_pol != clkpol)
|
|
|
|
continue;
|
|
|
|
if (!parse_pin(cell, ff->find("next_state"), cell_next_pin, cell_next_pol))
|
|
|
|
continue;
|
|
|
|
if (has_reset && rstval == false) {
|
|
|
|
if (!parse_pin(cell, ff->find("clear"), cell_rst_pin, cell_rst_pol) || cell_rst_pol != rstpol)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (has_reset && rstval == true) {
|
|
|
|
if (!parse_pin(cell, ff->find("preset"), cell_rst_pin, cell_rst_pol) || cell_rst_pol != rstpol)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-08-16 03:22:04 -05:00
|
|
|
std::map<std::string, char> this_cell_ports;
|
2013-01-05 04:13:26 -06:00
|
|
|
this_cell_ports[cell_clk_pin] = 'C';
|
|
|
|
if (has_reset)
|
|
|
|
this_cell_ports[cell_rst_pin] = 'R';
|
|
|
|
this_cell_ports[cell_next_pin] = 'D';
|
|
|
|
|
2014-10-18 08:20:38 -05:00
|
|
|
double area = 0;
|
2013-12-21 01:42:37 -06:00
|
|
|
LibertyAst *ar = cell->find("area");
|
2020-04-05 22:02:40 -05:00
|
|
|
if (ar != nullptr && !ar->value.empty())
|
2013-12-21 01:42:37 -06:00
|
|
|
area = atof(ar->value.c_str());
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
int num_pins = 0;
|
|
|
|
bool found_output = false;
|
2016-04-05 05:51:04 -05:00
|
|
|
bool found_noninv_output = false;
|
2013-01-05 04:13:26 -06:00
|
|
|
for (auto pin : cell->children)
|
|
|
|
{
|
|
|
|
if (pin->id != "pin" || pin->args.size() != 1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
LibertyAst *dir = pin->find("direction");
|
2020-04-05 22:02:40 -05:00
|
|
|
if (dir == nullptr || dir->value == "internal")
|
2013-01-05 04:13:26 -06:00
|
|
|
continue;
|
|
|
|
num_pins++;
|
|
|
|
|
|
|
|
if (dir->value == "input" && this_cell_ports.count(pin->args[0]) == 0)
|
|
|
|
goto continue_cell_loop;
|
|
|
|
|
|
|
|
LibertyAst *func = pin->find("function");
|
2020-04-05 22:02:40 -05:00
|
|
|
if (dir->value == "output" && func != nullptr) {
|
2013-01-05 04:13:26 -06:00
|
|
|
std::string value = func->value;
|
|
|
|
for (size_t pos = value.find_first_of("\" \t"); pos != std::string::npos; pos = value.find_first_of("\" \t"))
|
|
|
|
value.erase(pos, 1);
|
2016-02-01 04:49:11 -06:00
|
|
|
if (value == ff->args[0]) {
|
|
|
|
this_cell_ports[pin->args[0]] = cell_next_pol ? 'Q' : 'q';
|
2016-04-05 05:51:04 -05:00
|
|
|
if (cell_next_pol)
|
|
|
|
found_noninv_output = true;
|
2016-02-01 04:49:11 -06:00
|
|
|
found_output = true;
|
|
|
|
} else
|
|
|
|
if (value == ff->args[1]) {
|
|
|
|
this_cell_ports[pin->args[0]] = cell_next_pol ? 'q' : 'Q';
|
2016-04-05 05:51:04 -05:00
|
|
|
if (!cell_next_pol)
|
|
|
|
found_noninv_output = true;
|
2013-01-05 04:13:26 -06:00
|
|
|
found_output = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this_cell_ports.count(pin->args[0]) == 0)
|
|
|
|
this_cell_ports[pin->args[0]] = 0;
|
|
|
|
}
|
|
|
|
|
2020-04-05 22:02:40 -05:00
|
|
|
if (!found_output || (best_cell != nullptr && (num_pins > best_cell_pins || (best_cell_noninv && !found_noninv_output))))
|
2013-01-05 04:13:26 -06:00
|
|
|
continue;
|
|
|
|
|
2020-04-05 22:02:40 -05:00
|
|
|
if (best_cell != nullptr && num_pins == best_cell_pins && area > best_cell_area)
|
2013-12-21 01:42:37 -06:00
|
|
|
continue;
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
best_cell = cell;
|
|
|
|
best_cell_pins = num_pins;
|
2013-12-21 01:42:37 -06:00
|
|
|
best_cell_area = area;
|
2016-04-05 05:51:04 -05:00
|
|
|
best_cell_noninv = found_noninv_output;
|
2013-01-05 04:13:26 -06:00
|
|
|
best_cell_ports.swap(this_cell_ports);
|
|
|
|
continue_cell_loop:;
|
|
|
|
}
|
|
|
|
|
2020-04-05 22:02:40 -05:00
|
|
|
if (best_cell != nullptr) {
|
2016-04-05 05:51:04 -05:00
|
|
|
log(" cell %s (%sinv, pins=%d, area=%.2f) is a direct match for cell type %s.\n",
|
|
|
|
best_cell->args[0].c_str(), best_cell_noninv ? "non" : "", best_cell_pins, best_cell_area, cell_type.c_str());
|
2014-12-24 05:19:20 -06:00
|
|
|
if (prepare_mode) {
|
|
|
|
cell_mappings[cell_type].cell_name = cell_type;
|
|
|
|
cell_mappings[cell_type].ports["C"] = 'C';
|
|
|
|
if (has_reset)
|
|
|
|
cell_mappings[cell_type].ports["R"] = 'R';
|
|
|
|
cell_mappings[cell_type].ports["D"] = 'D';
|
|
|
|
cell_mappings[cell_type].ports["Q"] = 'Q';
|
|
|
|
} else {
|
2019-08-16 03:22:04 -05:00
|
|
|
cell_mappings[cell_type].cell_name = RTLIL::escape_id(best_cell->args[0]);
|
2014-12-24 05:19:20 -06:00
|
|
|
cell_mappings[cell_type].ports = best_cell_ports;
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-15 12:05:08 -05:00
|
|
|
static void find_cell_sr(LibertyAst *ast, IdString cell_type, bool clkpol, bool setpol, bool clrpol, bool prepare_mode)
|
2013-10-24 11:20:06 -05:00
|
|
|
{
|
2020-04-05 22:02:40 -05:00
|
|
|
LibertyAst *best_cell = nullptr;
|
2019-08-16 03:22:04 -05:00
|
|
|
std::map<std::string, char> best_cell_ports;
|
2013-10-24 11:20:06 -05:00
|
|
|
int best_cell_pins = 0;
|
2016-04-05 05:51:04 -05:00
|
|
|
bool best_cell_noninv = false;
|
2014-10-18 08:20:38 -05:00
|
|
|
double best_cell_area = 0;
|
2013-10-24 11:20:06 -05:00
|
|
|
|
|
|
|
if (ast->id != "library")
|
|
|
|
log_error("Format error in liberty file.\n");
|
|
|
|
|
|
|
|
for (auto cell : ast->children)
|
|
|
|
{
|
|
|
|
if (cell->id != "cell" || cell->args.size() != 1)
|
|
|
|
continue;
|
|
|
|
|
2018-04-05 04:01:32 -05:00
|
|
|
LibertyAst *dn = cell->find("dont_use");
|
2020-04-05 22:02:40 -05:00
|
|
|
if (dn != nullptr && dn->value == "true")
|
2018-04-05 04:01:32 -05:00
|
|
|
continue;
|
|
|
|
|
2013-10-24 11:20:06 -05:00
|
|
|
LibertyAst *ff = cell->find("ff");
|
2020-04-05 22:02:40 -05:00
|
|
|
if (ff == nullptr)
|
2013-10-24 11:20:06 -05:00
|
|
|
continue;
|
|
|
|
|
|
|
|
std::string cell_clk_pin, cell_set_pin, cell_clr_pin, cell_next_pin;
|
|
|
|
bool cell_clk_pol, cell_set_pol, cell_clr_pol, cell_next_pol;
|
|
|
|
|
|
|
|
if (!parse_pin(cell, ff->find("clocked_on"), cell_clk_pin, cell_clk_pol) || cell_clk_pol != clkpol)
|
|
|
|
continue;
|
|
|
|
if (!parse_pin(cell, ff->find("next_state"), cell_next_pin, cell_next_pol))
|
|
|
|
continue;
|
|
|
|
if (!parse_pin(cell, ff->find("preset"), cell_set_pin, cell_set_pol) || cell_set_pol != setpol)
|
|
|
|
continue;
|
|
|
|
if (!parse_pin(cell, ff->find("clear"), cell_clr_pin, cell_clr_pol) || cell_clr_pol != clrpol)
|
|
|
|
continue;
|
|
|
|
|
2019-08-16 03:22:04 -05:00
|
|
|
std::map<std::string, char> this_cell_ports;
|
2013-10-24 11:20:06 -05:00
|
|
|
this_cell_ports[cell_clk_pin] = 'C';
|
|
|
|
this_cell_ports[cell_set_pin] = 'S';
|
|
|
|
this_cell_ports[cell_clr_pin] = 'R';
|
|
|
|
this_cell_ports[cell_next_pin] = 'D';
|
|
|
|
|
2014-10-18 08:20:38 -05:00
|
|
|
double area = 0;
|
2013-12-21 01:42:37 -06:00
|
|
|
LibertyAst *ar = cell->find("area");
|
2020-04-05 22:02:40 -05:00
|
|
|
if (ar != nullptr && !ar->value.empty())
|
2013-12-21 01:42:37 -06:00
|
|
|
area = atof(ar->value.c_str());
|
|
|
|
|
2013-10-24 11:20:06 -05:00
|
|
|
int num_pins = 0;
|
|
|
|
bool found_output = false;
|
2016-04-05 05:51:04 -05:00
|
|
|
bool found_noninv_output = false;
|
2013-10-24 11:20:06 -05:00
|
|
|
for (auto pin : cell->children)
|
|
|
|
{
|
|
|
|
if (pin->id != "pin" || pin->args.size() != 1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
LibertyAst *dir = pin->find("direction");
|
2020-04-05 22:02:40 -05:00
|
|
|
if (dir == nullptr || dir->value == "internal")
|
2013-10-24 11:20:06 -05:00
|
|
|
continue;
|
|
|
|
num_pins++;
|
|
|
|
|
|
|
|
if (dir->value == "input" && this_cell_ports.count(pin->args[0]) == 0)
|
|
|
|
goto continue_cell_loop;
|
|
|
|
|
|
|
|
LibertyAst *func = pin->find("function");
|
2020-04-05 22:02:40 -05:00
|
|
|
if (dir->value == "output" && func != nullptr) {
|
2013-10-24 11:20:06 -05:00
|
|
|
std::string value = func->value;
|
|
|
|
for (size_t pos = value.find_first_of("\" \t"); pos != std::string::npos; pos = value.find_first_of("\" \t"))
|
|
|
|
value.erase(pos, 1);
|
2016-02-01 04:49:11 -06:00
|
|
|
if (value == ff->args[0]) {
|
|
|
|
this_cell_ports[pin->args[0]] = cell_next_pol ? 'Q' : 'q';
|
2016-04-05 05:51:04 -05:00
|
|
|
if (cell_next_pol)
|
|
|
|
found_noninv_output = true;
|
2016-02-01 04:49:11 -06:00
|
|
|
found_output = true;
|
|
|
|
} else
|
|
|
|
if (value == ff->args[1]) {
|
|
|
|
this_cell_ports[pin->args[0]] = cell_next_pol ? 'q' : 'Q';
|
2016-04-05 05:51:04 -05:00
|
|
|
if (!cell_next_pol)
|
|
|
|
found_noninv_output = true;
|
2013-10-24 11:20:06 -05:00
|
|
|
found_output = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this_cell_ports.count(pin->args[0]) == 0)
|
|
|
|
this_cell_ports[pin->args[0]] = 0;
|
|
|
|
}
|
|
|
|
|
2020-04-05 22:02:40 -05:00
|
|
|
if (!found_output || (best_cell != nullptr && (num_pins > best_cell_pins || (best_cell_noninv && !found_noninv_output))))
|
2013-10-24 11:20:06 -05:00
|
|
|
continue;
|
|
|
|
|
2020-04-05 22:02:40 -05:00
|
|
|
if (best_cell != nullptr && num_pins == best_cell_pins && area > best_cell_area)
|
2013-12-21 01:42:37 -06:00
|
|
|
continue;
|
|
|
|
|
2013-10-24 11:20:06 -05:00
|
|
|
best_cell = cell;
|
|
|
|
best_cell_pins = num_pins;
|
2013-12-21 01:42:37 -06:00
|
|
|
best_cell_area = area;
|
2016-04-05 05:51:04 -05:00
|
|
|
best_cell_noninv = found_noninv_output;
|
2013-10-24 11:20:06 -05:00
|
|
|
best_cell_ports.swap(this_cell_ports);
|
|
|
|
continue_cell_loop:;
|
|
|
|
}
|
|
|
|
|
2020-04-05 22:02:40 -05:00
|
|
|
if (best_cell != nullptr) {
|
2016-04-05 05:51:04 -05:00
|
|
|
log(" cell %s (%sinv, pins=%d, area=%.2f) is a direct match for cell type %s.\n",
|
|
|
|
best_cell->args[0].c_str(), best_cell_noninv ? "non" : "", best_cell_pins, best_cell_area, cell_type.c_str());
|
2014-12-24 05:19:20 -06:00
|
|
|
if (prepare_mode) {
|
|
|
|
cell_mappings[cell_type].cell_name = cell_type;
|
|
|
|
cell_mappings[cell_type].ports["C"] = 'C';
|
|
|
|
cell_mappings[cell_type].ports["S"] = 'S';
|
|
|
|
cell_mappings[cell_type].ports["R"] = 'R';
|
|
|
|
cell_mappings[cell_type].ports["D"] = 'D';
|
|
|
|
cell_mappings[cell_type].ports["Q"] = 'Q';
|
|
|
|
} else {
|
2019-08-16 03:22:04 -05:00
|
|
|
cell_mappings[cell_type].cell_name = RTLIL::escape_id(best_cell->args[0]);
|
2014-12-24 05:19:20 -06:00
|
|
|
cell_mappings[cell_type].ports = best_cell_ports;
|
|
|
|
}
|
2013-10-24 11:20:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
static bool expand_cellmap_worker(std::string from, std::string to, std::string inv)
|
|
|
|
{
|
|
|
|
if (cell_mappings.count(to) > 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
log(" create mapping for %s from mapping for %s.\n", to.c_str(), from.c_str());
|
|
|
|
cell_mappings[to].cell_name = cell_mappings[from].cell_name;
|
|
|
|
cell_mappings[to].ports = cell_mappings[from].ports;
|
|
|
|
|
|
|
|
for (auto &it : cell_mappings[to].ports) {
|
2013-10-24 11:20:06 -05:00
|
|
|
char cmp_ch = it.second;
|
|
|
|
if ('a' <= cmp_ch && cmp_ch <= 'z')
|
|
|
|
cmp_ch -= 'a' - 'A';
|
|
|
|
if (inv.find(cmp_ch) == std::string::npos)
|
2013-01-05 04:13:26 -06:00
|
|
|
continue;
|
|
|
|
if ('a' <= it.second && it.second <= 'z')
|
|
|
|
it.second -= 'a' - 'A';
|
|
|
|
else if ('A' <= it.second && it.second <= 'Z')
|
|
|
|
it.second += 'a' - 'A';
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool expand_cellmap(std::string pattern, std::string inv)
|
|
|
|
{
|
|
|
|
std::vector<std::pair<std::string, std::string>> from_to_list;
|
|
|
|
bool return_status = false;
|
|
|
|
|
|
|
|
for (auto &it : cell_mappings) {
|
2014-08-02 06:11:01 -05:00
|
|
|
std::string from = it.first.str(), to = it.first.str();
|
2013-01-05 04:13:26 -06:00
|
|
|
if (from.size() != pattern.size())
|
|
|
|
continue;
|
|
|
|
for (size_t i = 0; i < from.size(); i++) {
|
|
|
|
if (pattern[i] == '*') {
|
|
|
|
to[i] = from[i] == 'P' ? 'N' :
|
|
|
|
from[i] == 'N' ? 'P' :
|
|
|
|
from[i] == '1' ? '0' :
|
|
|
|
from[i] == '0' ? '1' : '*';
|
|
|
|
} else
|
|
|
|
if (pattern[i] != '?' && pattern[i] != from[i])
|
|
|
|
goto pattern_failed;
|
|
|
|
}
|
|
|
|
from_to_list.push_back(std::pair<std::string, std::string>(from, to));
|
|
|
|
pattern_failed:;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &it : from_to_list)
|
|
|
|
return_status = return_status || expand_cellmap_worker(it.first, it.second, inv);
|
|
|
|
return return_status;
|
|
|
|
}
|
|
|
|
|
2019-08-15 12:05:08 -05:00
|
|
|
static void map_sr_to_arst(IdString from, IdString to)
|
2013-10-24 11:20:06 -05:00
|
|
|
{
|
2014-02-15 09:34:12 -06:00
|
|
|
if (!cell_mappings.count(from) || cell_mappings.count(to) > 0)
|
2013-10-24 11:20:06 -05:00
|
|
|
return;
|
|
|
|
|
2020-06-18 20:32:48 -05:00
|
|
|
char from_clk_pol = from[8];
|
2015-01-24 05:16:46 -06:00
|
|
|
char from_set_pol = from[9];
|
|
|
|
char from_clr_pol = from[10];
|
2020-06-18 20:32:48 -05:00
|
|
|
char to_clk_pol = to[6];
|
|
|
|
char to_rst_pol = to[7];
|
2015-01-24 05:16:46 -06:00
|
|
|
char to_rst_val = to[8];
|
2013-10-24 11:20:06 -05:00
|
|
|
|
|
|
|
log_assert(from_clk_pol == to_clk_pol);
|
|
|
|
log_assert(to_rst_pol == from_set_pol && to_rst_pol == from_clr_pol);
|
|
|
|
|
2019-08-15 12:05:08 -05:00
|
|
|
log(" create mapping for %s from mapping for %s.\n", to.c_str(), from.c_str());
|
2013-10-24 11:20:06 -05:00
|
|
|
cell_mappings[to].cell_name = cell_mappings[from].cell_name;
|
|
|
|
cell_mappings[to].ports = cell_mappings[from].ports;
|
|
|
|
|
|
|
|
for (auto &it : cell_mappings[to].ports)
|
|
|
|
{
|
|
|
|
bool is_set_pin = it.second == 'S' || it.second == 's';
|
|
|
|
bool is_clr_pin = it.second == 'R' || it.second == 'r';
|
|
|
|
|
|
|
|
if (!is_set_pin && !is_clr_pin)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ((to_rst_val == '0' && is_set_pin) || (to_rst_val == '1' && is_clr_pin))
|
|
|
|
{
|
|
|
|
// this is the unused set/clr pin -- deactivate it
|
|
|
|
if (is_set_pin)
|
|
|
|
it.second = (from_set_pol == 'P') == (it.second == 'S') ? '0' : '1';
|
|
|
|
else
|
|
|
|
it.second = (from_clr_pol == 'P') == (it.second == 'R') ? '0' : '1';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// this is the used set/clr pin -- rename it to 'reset'
|
|
|
|
if (it.second == 'S')
|
|
|
|
it.second = 'R';
|
|
|
|
if (it.second == 's')
|
|
|
|
it.second = 'r';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-15 12:05:08 -05:00
|
|
|
static void map_adff_to_dff(IdString from, IdString to)
|
2016-02-01 04:49:11 -06:00
|
|
|
{
|
|
|
|
if (!cell_mappings.count(from) || cell_mappings.count(to) > 0)
|
|
|
|
return;
|
|
|
|
|
2020-06-18 20:32:48 -05:00
|
|
|
char from_clk_pol = from[6];
|
2016-02-01 04:49:11 -06:00
|
|
|
char from_rst_pol = from[7];
|
2020-06-18 20:32:48 -05:00
|
|
|
char to_clk_pol = to[6];
|
2016-02-01 04:49:11 -06:00
|
|
|
|
|
|
|
log_assert(from_clk_pol == to_clk_pol);
|
|
|
|
|
2019-08-15 12:05:08 -05:00
|
|
|
log(" create mapping for %s from mapping for %s.\n", to.c_str(), from.c_str());
|
2016-02-01 04:49:11 -06:00
|
|
|
cell_mappings[to].cell_name = cell_mappings[from].cell_name;
|
|
|
|
cell_mappings[to].ports = cell_mappings[from].ports;
|
|
|
|
|
|
|
|
for (auto &it : cell_mappings[to].ports) {
|
|
|
|
if (it.second == 'S' || it.second == 'R')
|
|
|
|
it.second = from_rst_pol == 'P' ? '0' : '1';
|
|
|
|
if (it.second == 's' || it.second == 'r')
|
|
|
|
it.second = from_rst_pol == 'P' ? '1' : '0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-24 05:19:20 -06:00
|
|
|
static void dfflibmap(RTLIL::Design *design, RTLIL::Module *module, bool prepare_mode)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
|
|
|
log("Mapping DFF cells in module `%s':\n", module->name.c_str());
|
|
|
|
|
2016-02-01 04:49:11 -06:00
|
|
|
dict<SigBit, pool<Cell*>> notmap;
|
|
|
|
SigMap sigmap(module);
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
std::vector<RTLIL::Cell*> cell_list;
|
2020-04-05 22:02:40 -05:00
|
|
|
for (auto cell : module->cells()) {
|
|
|
|
if (design->selected(module, cell) && cell_mappings.count(cell->type) > 0)
|
|
|
|
cell_list.push_back(cell);
|
|
|
|
if (cell->type == ID($_NOT_))
|
|
|
|
notmap[sigmap(cell->getPort(ID::A))].insert(cell);
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
std::map<std::string, int> stats;
|
2014-07-25 08:05:18 -05:00
|
|
|
for (auto cell : cell_list)
|
|
|
|
{
|
|
|
|
auto cell_type = cell->type;
|
|
|
|
auto cell_name = cell->name;
|
2014-07-26 07:32:50 -05:00
|
|
|
auto cell_connections = cell->connections();
|
2017-08-31 15:51:56 -05:00
|
|
|
std::string src = cell->get_src_attribute();
|
2017-08-29 15:46:35 -05:00
|
|
|
|
2014-07-25 08:05:18 -05:00
|
|
|
module->remove(cell);
|
|
|
|
|
|
|
|
cell_mapping &cm = cell_mappings[cell_type];
|
2019-08-16 03:22:04 -05:00
|
|
|
RTLIL::Cell *new_cell = module->addCell(cell_name, prepare_mode ? cm.cell_name : cm.cell_name);
|
2014-07-25 08:05:18 -05:00
|
|
|
|
2017-08-31 15:51:56 -05:00
|
|
|
new_cell->set_src_attribute(src);
|
2017-08-29 15:46:35 -05:00
|
|
|
|
2016-02-01 04:49:11 -06:00
|
|
|
bool has_q = false, has_qn = false;
|
|
|
|
for (auto &port : cm.ports) {
|
|
|
|
if (port.second == 'Q') has_q = true;
|
|
|
|
if (port.second == 'q') has_qn = true;
|
|
|
|
}
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
for (auto &port : cm.ports) {
|
|
|
|
RTLIL::SigSpec sig;
|
|
|
|
if ('A' <= port.second && port.second <= 'Z') {
|
2014-07-25 08:05:18 -05:00
|
|
|
sig = cell_connections[std::string("\\") + port.second];
|
2013-01-05 04:13:26 -06:00
|
|
|
} else
|
2014-08-27 12:44:12 -05:00
|
|
|
if (port.second == 'q') {
|
|
|
|
RTLIL::SigSpec old_sig = cell_connections[std::string("\\") + char(port.second - ('a' - 'A'))];
|
2014-10-10 09:59:44 -05:00
|
|
|
sig = module->addWire(NEW_ID, GetSize(old_sig));
|
2016-02-01 04:49:11 -06:00
|
|
|
if (has_q && has_qn) {
|
|
|
|
for (auto &it : notmap[sigmap(old_sig)]) {
|
2019-08-15 16:50:10 -05:00
|
|
|
module->connect(it->getPort(ID::Y), sig);
|
|
|
|
it->setPort(ID::Y, module->addWire(NEW_ID, GetSize(old_sig)));
|
2016-02-01 04:49:11 -06:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
module->addNotGate(NEW_ID, sig, old_sig);
|
|
|
|
}
|
2014-08-27 12:44:12 -05:00
|
|
|
} else
|
2013-01-05 04:13:26 -06:00
|
|
|
if ('a' <= port.second && port.second <= 'z') {
|
2014-07-25 08:05:18 -05:00
|
|
|
sig = cell_connections[std::string("\\") + char(port.second - ('a' - 'A'))];
|
2014-08-15 07:11:40 -05:00
|
|
|
sig = module->NotGate(NEW_ID, sig);
|
2013-10-24 11:20:06 -05:00
|
|
|
} else
|
|
|
|
if (port.second == '0' || port.second == '1') {
|
|
|
|
sig = RTLIL::SigSpec(port.second == '0' ? 0 : 1, 1);
|
|
|
|
} else
|
2016-02-01 04:49:11 -06:00
|
|
|
if (port.second == 0) {
|
|
|
|
sig = module->addWire(NEW_ID);
|
|
|
|
} else
|
2013-10-24 11:20:06 -05:00
|
|
|
log_abort();
|
2019-08-16 03:22:04 -05:00
|
|
|
new_cell->setPort("\\" + port.first, sig);
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
2014-07-25 08:05:18 -05:00
|
|
|
|
|
|
|
stats[stringf(" mapped %%d %s cells to %s cells.\n", cell_type.c_str(), new_cell->type.c_str())]++;
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &stat: stats)
|
|
|
|
log(stat.first.c_str(), stat.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct DfflibmapPass : public Pass {
|
2013-02-28 04:14:59 -06:00
|
|
|
DfflibmapPass() : Pass("dfflibmap", "technology mapping of flip-flops") { }
|
2020-06-18 18:34:52 -05:00
|
|
|
void help() override
|
2013-02-28 04:14:59 -06:00
|
|
|
{
|
|
|
|
log("\n");
|
2014-12-24 05:56:05 -06:00
|
|
|
log(" dfflibmap [-prepare] -liberty <file> [selection]\n");
|
2013-02-28 04:14:59 -06:00
|
|
|
log("\n");
|
|
|
|
log("Map internal flip-flop cells to the flip-flop cells in the technology\n");
|
|
|
|
log("library specified in the given liberty file.\n");
|
|
|
|
log("\n");
|
|
|
|
log("This pass may add inverters as needed. Therefore it is recommended to\n");
|
|
|
|
log("first run this pass and then map the logic paths to the target technology.\n");
|
|
|
|
log("\n");
|
2014-12-24 05:56:05 -06:00
|
|
|
log("When called with -prepare, this command will convert the internal FF cells\n");
|
|
|
|
log("to the internal cell types that best match the cells found in the given\n");
|
|
|
|
log("liberty file.\n");
|
|
|
|
log("\n");
|
2013-02-28 04:14:59 -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-04-21 16:28:37 -05:00
|
|
|
log_header(design, "Executing DFFLIBMAP pass (mapping DFF cells to sequential cells from liberty file).\n");
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
std::string liberty_file;
|
2014-12-24 05:19:20 -06:00
|
|
|
bool prepare_mode = false;
|
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 == "-liberty" && argidx+1 < args.size()) {
|
|
|
|
liberty_file = args[++argidx];
|
2015-09-18 04:55:57 -05:00
|
|
|
rewrite_filename(liberty_file);
|
2013-01-05 04:13:26 -06:00
|
|
|
continue;
|
|
|
|
}
|
2014-12-24 05:19:20 -06:00
|
|
|
if (arg == "-prepare") {
|
|
|
|
prepare_mode = true;
|
|
|
|
continue;
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
extra_args(args, argidx, design);
|
|
|
|
|
|
|
|
if (liberty_file.empty())
|
|
|
|
log_cmd_error("Missing `-liberty liberty_file' option!\n");
|
|
|
|
|
2014-08-23 08:03:55 -05:00
|
|
|
std::ifstream f;
|
|
|
|
f.open(liberty_file.c_str());
|
|
|
|
if (f.fail())
|
2013-01-05 04:13:26 -06:00
|
|
|
log_cmd_error("Can't open liberty file `%s': %s\n", liberty_file.c_str(), strerror(errno));
|
2014-01-14 11:57:47 -06:00
|
|
|
LibertyParser libparser(f);
|
2014-08-23 08:03:55 -05:00
|
|
|
f.close();
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2019-08-15 12:05:08 -05:00
|
|
|
find_cell(libparser.ast, ID($_DFF_N_), false, false, false, false, prepare_mode);
|
|
|
|
find_cell(libparser.ast, ID($_DFF_P_), true, false, false, false, prepare_mode);
|
|
|
|
|
|
|
|
find_cell(libparser.ast, ID($_DFF_NN0_), false, true, false, false, prepare_mode);
|
|
|
|
find_cell(libparser.ast, ID($_DFF_NN1_), false, true, false, true, prepare_mode);
|
|
|
|
find_cell(libparser.ast, ID($_DFF_NP0_), false, true, true, false, prepare_mode);
|
|
|
|
find_cell(libparser.ast, ID($_DFF_NP1_), false, true, true, true, prepare_mode);
|
|
|
|
find_cell(libparser.ast, ID($_DFF_PN0_), true, true, false, false, prepare_mode);
|
|
|
|
find_cell(libparser.ast, ID($_DFF_PN1_), true, true, false, true, prepare_mode);
|
|
|
|
find_cell(libparser.ast, ID($_DFF_PP0_), true, true, true, false, prepare_mode);
|
|
|
|
find_cell(libparser.ast, ID($_DFF_PP1_), true, true, true, true, prepare_mode);
|
|
|
|
|
|
|
|
find_cell_sr(libparser.ast, ID($_DFFSR_NNN_), false, false, false, prepare_mode);
|
|
|
|
find_cell_sr(libparser.ast, ID($_DFFSR_NNP_), false, false, true, prepare_mode);
|
|
|
|
find_cell_sr(libparser.ast, ID($_DFFSR_NPN_), false, true, false, prepare_mode);
|
|
|
|
find_cell_sr(libparser.ast, ID($_DFFSR_NPP_), false, true, true, prepare_mode);
|
|
|
|
find_cell_sr(libparser.ast, ID($_DFFSR_PNN_), true, false, false, prepare_mode);
|
|
|
|
find_cell_sr(libparser.ast, ID($_DFFSR_PNP_), true, false, true, prepare_mode);
|
|
|
|
find_cell_sr(libparser.ast, ID($_DFFSR_PPN_), true, true, false, prepare_mode);
|
|
|
|
find_cell_sr(libparser.ast, ID($_DFFSR_PPP_), true, true, true, prepare_mode);
|
2013-10-24 11:20:06 -05:00
|
|
|
|
2013-12-20 07:21:18 -06:00
|
|
|
// try to implement as many cells as possible just by inverting
|
|
|
|
// the SET and RESET pins. If necessary, implement cell types
|
|
|
|
// by inverting both D and Q. Only invert clock pins if there
|
|
|
|
// is no other way of implementing the cell.
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (expand_cellmap("$_DFF_?*?_", "R") ||
|
|
|
|
expand_cellmap("$_DFFSR_?*?_", "S") ||
|
|
|
|
expand_cellmap("$_DFFSR_??*_", "R"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (expand_cellmap("$_DFF_??*_", "DQ"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (expand_cellmap("$_DFF_*_", "C") ||
|
|
|
|
expand_cellmap("$_DFF_*??_", "C") ||
|
|
|
|
expand_cellmap("$_DFFSR_*??_", "C"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
break;
|
2013-12-20 05:34:34 -06:00
|
|
|
}
|
2013-10-24 11:20:06 -05:00
|
|
|
|
2019-08-15 12:05:08 -05:00
|
|
|
map_sr_to_arst(ID($_DFFSR_NNN_), ID($_DFF_NN0_));
|
|
|
|
map_sr_to_arst(ID($_DFFSR_NNN_), ID($_DFF_NN1_));
|
|
|
|
map_sr_to_arst(ID($_DFFSR_NPP_), ID($_DFF_NP0_));
|
|
|
|
map_sr_to_arst(ID($_DFFSR_NPP_), ID($_DFF_NP1_));
|
|
|
|
map_sr_to_arst(ID($_DFFSR_PNN_), ID($_DFF_PN0_));
|
|
|
|
map_sr_to_arst(ID($_DFFSR_PNN_), ID($_DFF_PN1_));
|
|
|
|
map_sr_to_arst(ID($_DFFSR_PPP_), ID($_DFF_PP0_));
|
|
|
|
map_sr_to_arst(ID($_DFFSR_PPP_), ID($_DFF_PP1_));
|
|
|
|
|
|
|
|
map_adff_to_dff(ID($_DFF_NN0_), ID($_DFF_N_));
|
|
|
|
map_adff_to_dff(ID($_DFF_NN1_), ID($_DFF_N_));
|
|
|
|
map_adff_to_dff(ID($_DFF_NP0_), ID($_DFF_N_));
|
|
|
|
map_adff_to_dff(ID($_DFF_NP1_), ID($_DFF_N_));
|
|
|
|
map_adff_to_dff(ID($_DFF_PN0_), ID($_DFF_P_));
|
|
|
|
map_adff_to_dff(ID($_DFF_PN1_), ID($_DFF_P_));
|
|
|
|
map_adff_to_dff(ID($_DFF_PP0_), ID($_DFF_P_));
|
|
|
|
map_adff_to_dff(ID($_DFF_PP1_), ID($_DFF_P_));
|
2016-02-01 04:49:11 -06:00
|
|
|
|
2019-02-25 00:08:52 -06:00
|
|
|
log(" final dff cell mappings:\n");
|
|
|
|
logmap_all();
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2020-04-05 22:02:40 -05:00
|
|
|
for (auto module : design->selected_modules())
|
|
|
|
if (!module->get_blackbox_attribute())
|
|
|
|
dfflibmap(design, module, prepare_mode);
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
cell_mappings.clear();
|
|
|
|
}
|
|
|
|
} DfflibmapPass;
|
2015-07-02 04:14:30 -05:00
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
PRIVATE_NAMESPACE_END
|