yosys/passes/silimate/annotate_ff_width.cc

80 lines
2.8 KiB
C++
Raw Normal View History

2026-06-02 01:35:30 +02:00
/*
* yosys -- Yosys Open SYnthesis Suite
*
* Copyright (C) 2026 Stan Lee <stan@silimate.com>
*
* 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.
*
* 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.
*
*/
#include "kernel/yosys.h"
USING_YOSYS_NAMESPACE
PRIVATE_NAMESPACE_BEGIN
2026-06-03 00:17:55 +02:00
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;
}
2026-06-02 01:35:30 +02:00
struct AnnotateFfWidthPass : public Pass {
AnnotateFfWidthPass() : Pass("annotate_ff_width", "annotate every flip-flop with its bit-width") { }
void help() override
{
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
log("\n");
log(" annotate_ff_width [selection]\n");
log("\n");
log("Annotate every flip-flop with its width.");
log("\n");
}
void execute(std::vector<std::string> args, RTLIL::Design *design) override
{
log_header(design, "Executing ANNOTATE_FF_WIDTH pass.\n");
size_t argidx;
for (argidx = 1; argidx < args.size(); argidx++)
break;
extra_args(args, argidx, design);
2026-06-03 00:45:52 +02:00
// Loop through all flip-flops in a module and annotate with their width
2026-06-02 01:35:30 +02:00
int annotated = 0;
2026-06-02 04:20:47 +02:00
for (auto module : design->selected_modules()) {
2026-06-03 00:45:52 +02:00
// First, count the number of flip-flops of the same base name.
2026-06-03 00:17:55 +02:00
dict<std::string, int> name_counts;
std::vector<std::pair<RTLIL::Cell *, std::string>> ff_cells;
2026-06-02 04:20:47 +02:00
for (auto cell : module->selected_cells()) {
2026-06-02 02:10:18 +02:00
if (!RTLIL::builtin_ff_cell_types().count(cell->type))
2026-06-02 01:35:30 +02:00
continue;
2026-06-03 00:17:55 +02:00
std::string base = ff_base_name(cell->name.str());
name_counts[base]++;
ff_cells.push_back({cell, base});
}
2026-06-03 00:45:52 +02:00
// Then, annotate each flip-flop with the count for its base name.
2026-06-03 00:17:55 +02:00
for (auto &it : ff_cells) {
int width = name_counts[it.second];
it.first->set_string_attribute(ID(ff_width), std::to_string(width));
2026-06-02 01:35:30 +02:00
annotated++;
}
}
2026-06-02 02:10:18 +02:00
log("Annotated %d flip-flops.\n", annotated);
2026-06-02 01:35:30 +02:00
}
} AnnotateFfWidthPass;
PRIVATE_NAMESPACE_END