From 4c000d3aba7ab080a95ed0a4b3de5c756f69646b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Povi=C5=A1er?= Date: Wed, 10 Apr 2024 15:20:01 +0200 Subject: [PATCH] Add new `bbox_derive` command for blackbox derivation --- passes/cmds/Makefile.inc | 1 + passes/cmds/bbox_derive.cc | 90 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 passes/cmds/bbox_derive.cc diff --git a/passes/cmds/Makefile.inc b/passes/cmds/Makefile.inc index d7e572462..b1c9c67f0 100644 --- a/passes/cmds/Makefile.inc +++ b/passes/cmds/Makefile.inc @@ -48,3 +48,4 @@ OBJS += passes/cmds/clean_zerowidth.o OBJS += passes/cmds/xprop.o OBJS += passes/cmds/dft_tag.o OBJS += passes/cmds/future.o +OBJS += passes/cmds/bbox_derive.o diff --git a/passes/cmds/bbox_derive.cc b/passes/cmds/bbox_derive.cc new file mode 100644 index 000000000..de0869cb1 --- /dev/null +++ b/passes/cmds/bbox_derive.cc @@ -0,0 +1,90 @@ +/* + * yosys -- Yosys Open SYnthesis Suite + * + * Copyright (C) 2024 Martin PoviĊĦer + * + * 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 + +struct BboxDerivePass : Pass { + BboxDerivePass() : Pass("bbox_derive", "derive blackbox modules") {} + void help() override + { + // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---| + log("\n"); + log(" bbox_derive [-base ] [-naming_attr ] [selection]\n"); + log("\n"); + } + void execute(std::vector args, RTLIL::Design *d) override + { + log_header(d, "Executing BBOX_DERIVE pass. (derive modules for blackboxes)\n"); + + size_t argidx; + IdString naming_attr; + IdString base_name; + for (argidx = 1; argidx < args.size(); argidx++) { + if (args[argidx] == "-naming_attr" && argidx + 1 < args.size()) + naming_attr = RTLIL::escape_id(args[++argidx]); + else if (args[argidx] == "-base" && argidx + 1 < args.size()) + base_name = RTLIL::escape_id(args[++argidx]); + else + break; + } + extra_args(args, argidx, d); + + Module *base_override; + if (!base_name.empty()) { + base_override = d->module(base_name); + if (!base_override) + log_cmd_error("Base module %s not found.\n", log_id(base_name)); + } + + dict, Module*> done; + + for (auto module : d->selected_modules()) { + for (auto cell : module->selected_cells()) { + Module *inst_module = d->module(cell->type); + if (!inst_module || !inst_module->get_blackbox_attribute()) + continue; + + if (cell->parameters.empty() || done.count(cell->parameters)) + continue; + + Module *base = inst_module; + if (base_override) + base = base_override; + + IdString derived_type = base->derive(d, cell->parameters); + Module *derived = d->module(derived_type); + + if (!naming_attr.empty() && derived->has_attribute(naming_attr)) { + IdString new_name = RTLIL::escape_id(derived->get_string_attribute(naming_attr)); + if (!new_name.isPublic()) + log_error("Derived module %s cannot be renamed to private name %s.\n", + log_id(derived), log_id(new_name)); + derived->attributes.erase(naming_attr); + d->rename(derived, new_name); + } + + done[cell->parameters] = derived; + } + } + } +} BboxDerivePass; + +PRIVATE_NAMESPACE_END