diff --git a/passes/silimate/annotate_ff_width.cc b/passes/silimate/annotate_ff_width.cc index 1782fa5ad..5e9488ded 100644 --- a/passes/silimate/annotate_ff_width.cc +++ b/passes/silimate/annotate_ff_width.cc @@ -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 name_counts; + std::vector> 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++; } }