Merge pull request #181 from Silimate/annotate_ff_width

[ENG-2097] Small fix for annotate_ff_width
This commit is contained in:
Akash Levy 2026-06-02 17:45:33 -07:00 committed by GitHub
commit 3cfe3ead82
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 25 additions and 9 deletions

View File

@ -21,6 +21,19 @@
USING_YOSYS_NAMESPACE
PRIVATE_NAMESPACE_BEGIN
static std::string ff_base_name(const std::string &name)
{
if (!name.empty() && name.back() == ']') {
size_t open = name.rfind('[');
if (open != std::string::npos && open + 1 < name.size() - 1) {
std::string inner = name.substr(open + 1, name.size() - open - 2);
if (inner.find_first_not_of("0123456789") == std::string::npos)
return name.substr(0, open);
}
}
return name;
}
struct AnnotateFfWidthPass : public Pass {
AnnotateFfWidthPass() : Pass("annotate_ff_width", "annotate every flip-flop with its bit-width") { }
void help() override
@ -40,20 +53,23 @@ struct AnnotateFfWidthPass : public Pass {
break;
extra_args(args, argidx, design);
// Loop through all flip-flops and annotate with their width
// Loop through all flip-flops in a module and annotate with their width
int annotated = 0;
for (auto module : design->selected_modules()) {
// First, count the number of flip-flops of the same base name.
dict<std::string, int> name_counts;
std::vector<std::pair<RTLIL::Cell *, std::string>> ff_cells;
for (auto cell : module->selected_cells()) {
if (!RTLIL::builtin_ff_cell_types().count(cell->type))
continue;
int width;
if (cell->hasParam(ID::WIDTH))
width = cell->getParam(ID::WIDTH).as_int();
else if (cell->hasPort(ID::Q))
width = GetSize(cell->getPort(ID::Q));
else
width = 1;
cell->set_string_attribute(ID(ff_width), std::to_string(width));
std::string base = ff_base_name(cell->name.str());
name_counts[base]++;
ff_cells.push_back({cell, base});
}
// Then, annotate each flip-flop with the count for its base name.
for (auto &it : ff_cells) {
int width = name_counts[it.second];
it.first->set_string_attribute(ID(ff_width), std::to_string(width));
annotated++;
}
}