31 std::map<std::string, char>
ports;
38 log(
" unmapped dff cell: %s\n", dff.c_str());
40 log(
" %s %s (",
cell_mappings[dff].cell_name.c_str(), dff.substr(1).c_str());
43 char arg[3] = { port.second, 0, 0 };
44 if (
'a' <= arg[0] && arg[0] <=
'z')
45 arg[1] = arg[0] - (
'a' -
'A'), arg[0] =
'~';
47 arg[1] = arg[0], arg[0] =
' ';
48 log(
"%s.%s(%s)", first ?
"" :
", ", port.first.c_str(), arg);
79 static bool parse_pin(LibertyAst *cell, LibertyAst *attr, std::string &pin_name,
bool &pin_pol)
81 if (cell ==
NULL || attr ==
NULL || attr->value.empty())
84 std::string value = attr->value;
86 for (
size_t pos = value.find_first_of(
"\" \t()"); pos != std::string::npos; pos = value.find_first_of(
"\" \t()"))
89 if (value[value.size()-1] ==
'\'') {
90 pin_name = value.substr(0, value.size()-1);
92 }
else if (value[0] ==
'!') {
93 pin_name = value.substr(1, value.size()-1);
100 for (
auto child : cell->children)
101 if (child->id ==
"pin" && child->args.size() == 1 && child->args[0] == pin_name)
106 static void find_cell(LibertyAst *ast, std::string cell_type,
bool clkpol,
bool has_reset,
bool rstpol,
bool rstval)
108 LibertyAst *best_cell =
NULL;
109 std::map<std::string, char> best_cell_ports;
110 int best_cell_pins = 0;
111 double best_cell_area = 0;
113 if (ast->id !=
"library")
114 log_error(
"Format error in liberty file.\n");
116 for (
auto cell : ast->children)
118 if (cell->id !=
"cell" || cell->args.size() != 1)
121 LibertyAst *ff = cell->find(
"ff");
125 std::string cell_clk_pin, cell_rst_pin, cell_next_pin;
126 bool cell_clk_pol, cell_rst_pol, cell_next_pol;
128 if (!
parse_pin(cell, ff->find(
"clocked_on"), cell_clk_pin, cell_clk_pol) || cell_clk_pol != clkpol)
130 if (!
parse_pin(cell, ff->find(
"next_state"), cell_next_pin, cell_next_pol))
132 if (has_reset && rstval ==
false) {
133 if (!
parse_pin(cell, ff->find(
"clear"), cell_rst_pin, cell_rst_pol) || cell_rst_pol != rstpol)
136 if (has_reset && rstval ==
true) {
137 if (!
parse_pin(cell, ff->find(
"preset"), cell_rst_pin, cell_rst_pol) || cell_rst_pol != rstpol)
141 std::map<std::string, char> this_cell_ports;
142 this_cell_ports[cell_clk_pin] =
'C';
144 this_cell_ports[cell_rst_pin] =
'R';
145 this_cell_ports[cell_next_pin] =
'D';
148 LibertyAst *ar = cell->find(
"area");
149 if (ar !=
NULL && !ar->value.empty())
150 area = atof(ar->value.c_str());
153 bool found_output =
false;
154 for (
auto pin : cell->children)
156 if (pin->id !=
"pin" || pin->args.size() != 1)
159 LibertyAst *dir = pin->find(
"direction");
160 if (dir ==
NULL || dir->value ==
"internal")
164 if (dir->value ==
"input" && this_cell_ports.count(pin->args[0]) == 0)
165 goto continue_cell_loop;
167 LibertyAst *func = pin->find(
"function");
168 if (dir->value ==
"output" && func !=
NULL) {
169 std::string value = func->value;
170 for (
size_t pos = value.find_first_of(
"\" \t"); pos != std::string::npos; pos = value.find_first_of(
"\" \t"))
172 if ((cell_next_pol ==
true && value == ff->args[0]) || (cell_next_pol ==
false && value == ff->args[1])) {
173 this_cell_ports[pin->args[0]] =
'Q';
178 if (this_cell_ports.count(pin->args[0]) == 0)
179 this_cell_ports[pin->args[0]] = 0;
182 if (!found_output || (best_cell !=
NULL && num_pins > best_cell_pins))
185 if (best_cell !=
NULL && num_pins == best_cell_pins && area > best_cell_area)
189 best_cell_pins = num_pins;
190 best_cell_area = area;
191 best_cell_ports.swap(this_cell_ports);
195 if (best_cell !=
NULL) {
196 log(
" cell %s (pins=%d, area=%.2f) is a direct match for cell type %s.\n", best_cell->args[0].c_str(), best_cell_pins, best_cell_area, cell_type.substr(1).c_str());
202 static void find_cell_sr(LibertyAst *ast, std::string cell_type,
bool clkpol,
bool setpol,
bool clrpol)
204 LibertyAst *best_cell =
NULL;
205 std::map<std::string, char> best_cell_ports;
206 int best_cell_pins = 0;
207 double best_cell_area = 0;
209 if (ast->id !=
"library")
210 log_error(
"Format error in liberty file.\n");
212 for (
auto cell : ast->children)
214 if (cell->id !=
"cell" || cell->args.size() != 1)
217 LibertyAst *ff = cell->find(
"ff");
221 std::string cell_clk_pin, cell_set_pin, cell_clr_pin, cell_next_pin;
222 bool cell_clk_pol, cell_set_pol, cell_clr_pol, cell_next_pol;
224 if (!
parse_pin(cell, ff->find(
"clocked_on"), cell_clk_pin, cell_clk_pol) || cell_clk_pol != clkpol)
226 if (!
parse_pin(cell, ff->find(
"next_state"), cell_next_pin, cell_next_pol))
228 if (!
parse_pin(cell, ff->find(
"preset"), cell_set_pin, cell_set_pol) || cell_set_pol != setpol)
230 if (!
parse_pin(cell, ff->find(
"clear"), cell_clr_pin, cell_clr_pol) || cell_clr_pol != clrpol)
233 std::map<std::string, char> this_cell_ports;
234 this_cell_ports[cell_clk_pin] =
'C';
235 this_cell_ports[cell_set_pin] =
'S';
236 this_cell_ports[cell_clr_pin] =
'R';
237 this_cell_ports[cell_next_pin] =
'D';
240 LibertyAst *ar = cell->find(
"area");
241 if (ar !=
NULL && !ar->value.empty())
242 area = atof(ar->value.c_str());
245 bool found_output =
false;
246 for (
auto pin : cell->children)
248 if (pin->id !=
"pin" || pin->args.size() != 1)
251 LibertyAst *dir = pin->find(
"direction");
252 if (dir ==
NULL || dir->value ==
"internal")
256 if (dir->value ==
"input" && this_cell_ports.count(pin->args[0]) == 0)
257 goto continue_cell_loop;
259 LibertyAst *func = pin->find(
"function");
260 if (dir->value ==
"output" && func !=
NULL) {
261 std::string value = func->value;
262 for (
size_t pos = value.find_first_of(
"\" \t"); pos != std::string::npos; pos = value.find_first_of(
"\" \t"))
264 if ((cell_next_pol ==
true && value == ff->args[0]) || (cell_next_pol ==
false && value == ff->args[1])) {
265 this_cell_ports[pin->args[0]] =
'Q';
270 if (this_cell_ports.count(pin->args[0]) == 0)
271 this_cell_ports[pin->args[0]] = 0;
274 if (!found_output || (best_cell !=
NULL && num_pins > best_cell_pins))
277 if (best_cell !=
NULL && num_pins == best_cell_pins && area > best_cell_area)
281 best_cell_pins = num_pins;
282 best_cell_area = area;
283 best_cell_ports.swap(this_cell_ports);
287 if (best_cell !=
NULL) {
288 log(
" cell %s (pins=%d, area=%.2f) is a direct match for cell type %s.\n", best_cell->args[0].c_str(), best_cell_pins, best_cell_area, cell_type.substr(1).c_str());
299 log(
" create mapping for %s from mapping for %s.\n", to.c_str(), from.c_str());
304 char cmp_ch = it.second;
305 if (
'a' <= cmp_ch && cmp_ch <=
'z')
307 if (inv.find(cmp_ch) == std::string::npos)
309 if (
'a' <= it.second && it.second <=
'z')
310 it.second -=
'a' -
'A';
311 else if (
'A' <= it.second && it.second <=
'Z')
312 it.second +=
'a' -
'A';
319 std::vector<std::pair<std::string, std::string>> from_to_list;
320 bool return_status =
false;
323 std::string from = it.first.str(), to = it.first.str();
324 if (from.size() != pattern.size())
326 for (
size_t i = 0; i < from.size(); i++) {
327 if (pattern[i] ==
'*') {
328 to[i] = from[i] ==
'P' ?
'N' :
329 from[i] ==
'N' ?
'P' :
330 from[i] ==
'1' ?
'0' :
331 from[i] ==
'0' ?
'1' :
'*';
333 if (pattern[i] !=
'?' && pattern[i] != from[i])
336 from_to_list.push_back(std::pair<std::string, std::string>(from, to));
340 for (
auto &it : from_to_list)
342 return return_status;
350 char from_clk_pol = from[8], from_set_pol = from[9], from_clr_pol = from[10];
351 char to_clk_pol = to[6], to_rst_pol = to[7], to_rst_val = to[8];
354 log_assert(to_rst_pol == from_set_pol && to_rst_pol == from_clr_pol);
356 log(
" create mapping for %s from mapping for %s.\n", to, from);
362 bool is_set_pin = it.second ==
'S' || it.second ==
's';
363 bool is_clr_pin = it.second ==
'R' || it.second ==
'r';
365 if (!is_set_pin && !is_clr_pin)
368 if ((to_rst_val ==
'0' && is_set_pin) || (to_rst_val ==
'1' && is_clr_pin))
372 it.second = (from_set_pol ==
'P') == (it.second ==
'S') ?
'0' :
'1';
374 it.second = (from_clr_pol ==
'P') == (it.second ==
'R') ?
'0' :
'1';
379 if (it.second ==
'S')
381 if (it.second ==
's')
389 log(
"Mapping DFF cells in module `%s':\n", module->
name.
c_str());
391 std::vector<RTLIL::Cell*> cell_list;
392 for (
auto &it : module->
cells_) {
394 cell_list.push_back(it.second);
397 std::map<std::string, int> stats;
398 for (
auto cell : cell_list)
400 auto cell_type = cell->type;
401 auto cell_name = cell->name;
402 auto cell_connections = cell->connections();
408 for (
auto &port : cm.
ports) {
410 if (
'A' <= port.second && port.second <=
'Z') {
411 sig = cell_connections[std::string(
"\\") + port.second];
413 if (port.second ==
'q') {
414 RTLIL::SigSpec old_sig = cell_connections[std::string(
"\\") + char(port.second - (
'a' -
'A'))];
418 if (
'a' <= port.second && port.second <=
'z') {
419 sig = cell_connections[std::string(
"\\") + char(port.second - (
'a' -
'A'))];
422 if (port.second ==
'0' || port.second ==
'1') {
425 if (port.second != 0)
427 new_cell->
setPort(
"\\" + port.first, sig);
430 stats[
stringf(
" mapped %%d %s cells to %s cells.\n", cell_type.c_str(), new_cell->
type.
c_str())]++;
433 for (
auto &stat: stats)
434 log(stat.first.c_str(), stat.second);
442 log(
" dfflibmap -liberty <file> [selection]\n");
444 log(
"Map internal flip-flop cells to the flip-flop cells in the technology\n");
445 log(
"library specified in the given liberty file.\n");
447 log(
"This pass may add inverters as needed. Therefore it is recommended to\n");
448 log(
"first run this pass and then map the logic paths to the target technology.\n");
453 log_header(
"Executing DFFLIBMAP pass (mapping DFF cells to sequential cells from liberty file).\n");
455 std::string liberty_file;
458 for (argidx = 1; argidx < args.size(); argidx++)
460 std::string arg = args[argidx];
461 if (arg ==
"-liberty" && argidx+1 < args.size()) {
462 liberty_file = args[++argidx];
469 if (liberty_file.empty())
473 f.open(liberty_file.c_str());
475 log_cmd_error(
"Can't open liberty file `%s': %s\n", liberty_file.c_str(), strerror(errno));
476 LibertyParser libparser(f);
479 find_cell(libparser.ast,
"$_DFF_N_",
false,
false,
false,
false);
480 find_cell(libparser.ast,
"$_DFF_P_",
true,
false,
false,
false);
482 find_cell(libparser.ast,
"$_DFF_NN0_",
false,
true,
false,
false);
483 find_cell(libparser.ast,
"$_DFF_NN1_",
false,
true,
false,
true);
484 find_cell(libparser.ast,
"$_DFF_NP0_",
false,
true,
true,
false);
485 find_cell(libparser.ast,
"$_DFF_NP1_",
false,
true,
true,
true);
486 find_cell(libparser.ast,
"$_DFF_PN0_",
true,
true,
false,
false);
487 find_cell(libparser.ast,
"$_DFF_PN1_",
true,
true,
false,
true);
488 find_cell(libparser.ast,
"$_DFF_PP0_",
true,
true,
true,
false);
489 find_cell(libparser.ast,
"$_DFF_PP1_",
true,
true,
true,
true);
491 find_cell_sr(libparser.ast,
"$_DFFSR_NNN_",
false,
false,
false);
492 find_cell_sr(libparser.ast,
"$_DFFSR_NNP_",
false,
false,
true);
493 find_cell_sr(libparser.ast,
"$_DFFSR_NPN_",
false,
true,
false);
494 find_cell_sr(libparser.ast,
"$_DFFSR_NPP_",
false,
true,
true);
495 find_cell_sr(libparser.ast,
"$_DFFSR_PNN_",
true,
false,
false);
496 find_cell_sr(libparser.ast,
"$_DFFSR_PNP_",
true,
false,
true);
497 find_cell_sr(libparser.ast,
"$_DFFSR_PPN_",
true,
true,
false);
498 find_cell_sr(libparser.ast,
"$_DFFSR_PPP_",
true,
true,
true);
531 log(
" final dff cell mappings:\n");
535 if (design->
selected(it.second) && !it.second->get_bool_attribute(
"\\blackbox"))
const char * c_str() const
bool selected(T1 *module) const
static std::map< RTLIL::IdString, cell_mapping > cell_mappings
std::map< std::string, char > ports
std::string stringf(const char *fmt,...)
RTLIL::Cell * addCell(RTLIL::IdString name, RTLIL::IdString type)
void log_header(const char *format,...)
void setPort(RTLIL::IdString portname, RTLIL::SigSpec signal)
void log_error(const char *format,...)
RTLIL::Cell * addNotGate(RTLIL::IdString name, RTLIL::SigBit sig_a, RTLIL::SigBit sig_y)
static void find_cell(LibertyAst *ast, std::string cell_type, bool clkpol, bool has_reset, bool rstpol, bool rstval)
static void find_cell_sr(LibertyAst *ast, std::string cell_type, bool clkpol, bool setpol, bool clrpol)
#define PRIVATE_NAMESPACE_BEGIN
int GetSize(RTLIL::Wire *wire)
#define log_assert(_assert_expr_)
RTLIL::Wire * addWire(RTLIL::IdString name, int width=1)
static void logmap(std::string dff)
#define PRIVATE_NAMESPACE_END
RTLIL::SigBit NotGate(RTLIL::IdString name, RTLIL::SigBit sig_a)
void log_cmd_error(const char *format,...)
virtual void execute(std::vector< std::string > args, RTLIL::Design *design)
static bool expand_cellmap(std::string pattern, std::string inv)
#define USING_YOSYS_NAMESPACE
static bool expand_cellmap_worker(std::string from, std::string to, std::string inv)
std::map< RTLIL::IdString, RTLIL::Module * > modules_
static void map_sr_to_arst(const char *from, const char *to)
DfflibmapPass DfflibmapPass
std::map< RTLIL::IdString, RTLIL::Cell * > cells_
void remove(const std::set< RTLIL::Wire * > &wires)
void log(const char *format,...)
static void dfflibmap(RTLIL::Design *design, RTLIL::Module *module)
void extra_args(std::vector< std::string > args, size_t argidx, RTLIL::Design *design, bool select=true)
static bool parse_pin(LibertyAst *cell, LibertyAst *attr, std::string &pin_name, bool &pin_pol)