update annotate_ff_width pass

This commit is contained in:
Stan Lee 2026-06-02 15:17:55 -07:00
parent 3d8f4999d6
commit 6b7e5e2685
1 changed files with 24 additions and 8 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
@ -43,17 +56,20 @@ struct AnnotateFfWidthPass : public Pass {
// Loop through all flip-flops and annotate with their width
int annotated = 0;
for (auto module : design->selected_modules()) {
// Pass 1: count flip-flop cells per 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});
}
// Pass 2: 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++;
}
}