2013-07-27 07:27:51 -05:00
|
|
|
/*
|
|
|
|
* yosys -- Yosys Open SYnthesis Suite
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
|
2015-07-02 04:14:30 -05:00
|
|
|
*
|
2013-07-27 07:27:51 -05:00
|
|
|
* 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.
|
2015-07-02 04:14:30 -05:00
|
|
|
*
|
2013-07-27 07:27:51 -05:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-11-30 04:25:55 -06:00
|
|
|
#include "kernel/yosys.h"
|
|
|
|
#include "frontends/ast/ast.h"
|
2013-07-27 07:27:51 -05:00
|
|
|
|
2014-07-31 06:19:47 -05:00
|
|
|
YOSYS_NAMESPACE_BEGIN
|
|
|
|
|
2014-02-20 16:28:59 -06:00
|
|
|
std::map<std::string, RTLIL::Design*> saved_designs;
|
|
|
|
std::vector<RTLIL::Design*> pushed_designs;
|
|
|
|
|
2013-07-27 07:27:51 -05:00
|
|
|
struct DesignPass : public Pass {
|
|
|
|
DesignPass() : Pass("design", "save, restore and reset current design") { }
|
2018-07-21 01:41:18 -05:00
|
|
|
~DesignPass() YS_OVERRIDE {
|
2013-07-27 07:27:51 -05:00
|
|
|
for (auto &it : saved_designs)
|
|
|
|
delete it.second;
|
|
|
|
saved_designs.clear();
|
2014-02-20 16:28:59 -06:00
|
|
|
for (auto &it : pushed_designs)
|
|
|
|
delete it;
|
|
|
|
pushed_designs.clear();
|
2013-07-27 07:27:51 -05:00
|
|
|
}
|
2018-07-21 01:41:18 -05:00
|
|
|
void help() YS_OVERRIDE
|
2013-07-27 07:27:51 -05:00
|
|
|
{
|
|
|
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
|
|
|
log("\n");
|
|
|
|
log(" design -reset\n");
|
|
|
|
log("\n");
|
|
|
|
log("Clear the current design.\n");
|
|
|
|
log("\n");
|
|
|
|
log("\n");
|
|
|
|
log(" design -save <name>\n");
|
|
|
|
log("\n");
|
|
|
|
log("Save the current design under the given name.\n");
|
|
|
|
log("\n");
|
|
|
|
log("\n");
|
2014-02-06 14:52:07 -06:00
|
|
|
log(" design -stash <name>\n");
|
|
|
|
log("\n");
|
|
|
|
log("Save the current design under the given name and then clear the current design.\n");
|
|
|
|
log("\n");
|
|
|
|
log("\n");
|
2014-02-20 16:28:59 -06:00
|
|
|
log(" design -push\n");
|
|
|
|
log("\n");
|
|
|
|
log("Push the current design to the stack and then clear the current design.\n");
|
|
|
|
log("\n");
|
|
|
|
log("\n");
|
|
|
|
log(" design -pop\n");
|
|
|
|
log("\n");
|
|
|
|
log("Reset the current design and pop the last design from the stack.\n");
|
|
|
|
log("\n");
|
|
|
|
log("\n");
|
2013-07-27 07:27:51 -05:00
|
|
|
log(" design -load <name>\n");
|
|
|
|
log("\n");
|
|
|
|
log("Reset the current design and load the design previously saved under the given\n");
|
|
|
|
log("name.\n");
|
|
|
|
log("\n");
|
2014-02-06 14:52:07 -06:00
|
|
|
log("\n");
|
|
|
|
log(" design -copy-from <name> [-as <new_mod_name>] <selection>\n");
|
|
|
|
log("\n");
|
|
|
|
log("Copy modules from the specified design into the current one. The selection is\n");
|
|
|
|
log("evaluated in the other design.\n");
|
|
|
|
log("\n");
|
|
|
|
log("\n");
|
|
|
|
log(" design -copy-to <name> [-as <new_mod_name>] [selection]\n");
|
|
|
|
log("\n");
|
2015-08-14 03:56:05 -05:00
|
|
|
log("Copy modules from the current design into the specified one.\n");
|
2014-02-06 14:52:07 -06:00
|
|
|
log("\n");
|
2016-11-30 04:25:55 -06:00
|
|
|
log("\n");
|
2017-06-30 11:52:52 -05:00
|
|
|
log(" design -import <name> [-as <new_top_name>] [selection]\n");
|
|
|
|
log("\n");
|
|
|
|
log("Import the specified design into the current design. The source design must\n");
|
|
|
|
log("either have a selected top module or the selection must contain exactly one\n");
|
|
|
|
log("module that is then used as top module for this command.\n");
|
|
|
|
log("\n");
|
|
|
|
log("\n");
|
2016-11-30 04:25:55 -06:00
|
|
|
log(" design -reset-vlog\n");
|
|
|
|
log("\n");
|
|
|
|
log("The Verilog front-end remembers defined macros and top-level declarations\n");
|
|
|
|
log("between calls to 'read_verilog'. This command resets this memory.\n");
|
|
|
|
log("\n");
|
2013-07-27 07:27:51 -05:00
|
|
|
}
|
2018-07-21 01:41:18 -05:00
|
|
|
void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
|
2013-07-27 07:27:51 -05:00
|
|
|
{
|
|
|
|
bool got_mode = false;
|
|
|
|
bool reset_mode = false;
|
2016-11-30 04:25:55 -06:00
|
|
|
bool reset_vlog_mode = false;
|
2014-02-20 16:28:59 -06:00
|
|
|
bool push_mode = false;
|
|
|
|
bool pop_mode = false;
|
2017-06-30 11:52:52 -05:00
|
|
|
bool import_mode = false;
|
2014-02-06 14:52:07 -06:00
|
|
|
RTLIL::Design *copy_from_design = NULL, *copy_to_design = NULL;
|
|
|
|
std::string save_name, load_name, as_name;
|
|
|
|
std::vector<RTLIL::Module*> copy_src_modules;
|
2013-07-27 07:27:51 -05:00
|
|
|
|
|
|
|
size_t argidx;
|
|
|
|
for (argidx = 1; argidx < args.size(); argidx++)
|
|
|
|
{
|
|
|
|
std::string arg = args[argidx];
|
2014-02-06 14:52:07 -06:00
|
|
|
if (!got_mode && args[argidx] == "-reset") {
|
2013-07-27 07:27:51 -05:00
|
|
|
got_mode = true;
|
|
|
|
reset_mode = true;
|
|
|
|
continue;
|
|
|
|
}
|
2016-11-30 04:25:55 -06:00
|
|
|
if (!got_mode && args[argidx] == "-reset-vlog") {
|
|
|
|
got_mode = true;
|
|
|
|
reset_vlog_mode = true;
|
|
|
|
continue;
|
|
|
|
}
|
2014-02-20 16:28:59 -06:00
|
|
|
if (!got_mode && args[argidx] == "-push") {
|
|
|
|
got_mode = true;
|
|
|
|
push_mode = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!got_mode && args[argidx] == "-pop") {
|
|
|
|
got_mode = true;
|
|
|
|
pop_mode = true;
|
|
|
|
continue;
|
|
|
|
}
|
2014-02-06 14:52:07 -06:00
|
|
|
if (!got_mode && args[argidx] == "-save" && argidx+1 < args.size()) {
|
2013-07-27 07:27:51 -05:00
|
|
|
got_mode = true;
|
|
|
|
save_name = args[++argidx];
|
|
|
|
continue;
|
|
|
|
}
|
2014-02-06 14:52:07 -06:00
|
|
|
if (!got_mode && args[argidx] == "-stash" && argidx+1 < args.size()) {
|
|
|
|
got_mode = true;
|
|
|
|
save_name = args[++argidx];
|
|
|
|
reset_mode = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!got_mode && args[argidx] == "-load" && argidx+1 < args.size()) {
|
2013-07-27 07:27:51 -05:00
|
|
|
got_mode = true;
|
|
|
|
load_name = args[++argidx];
|
|
|
|
if (saved_designs.count(load_name) == 0)
|
|
|
|
log_cmd_error("No saved design '%s' found!\n", load_name.c_str());
|
|
|
|
continue;
|
|
|
|
}
|
2014-02-06 14:52:07 -06:00
|
|
|
if (!got_mode && args[argidx] == "-copy-from" && argidx+1 < args.size()) {
|
|
|
|
got_mode = true;
|
|
|
|
if (saved_designs.count(args[++argidx]) == 0)
|
|
|
|
log_cmd_error("No saved design '%s' found!\n", args[argidx].c_str());
|
|
|
|
copy_from_design = saved_designs.at(args[argidx]);
|
|
|
|
copy_to_design = design;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!got_mode && args[argidx] == "-copy-to" && argidx+1 < args.size()) {
|
|
|
|
got_mode = true;
|
|
|
|
if (saved_designs.count(args[++argidx]) == 0)
|
|
|
|
saved_designs[args[argidx]] = new RTLIL::Design;
|
|
|
|
copy_to_design = saved_designs.at(args[argidx]);
|
|
|
|
copy_from_design = design;
|
|
|
|
continue;
|
|
|
|
}
|
2017-06-30 11:52:52 -05:00
|
|
|
if (!got_mode && args[argidx] == "-import" && argidx+1 < args.size()) {
|
2014-02-06 14:52:07 -06:00
|
|
|
got_mode = true;
|
2017-06-30 11:52:52 -05:00
|
|
|
import_mode = true;
|
|
|
|
if (saved_designs.count(args[++argidx]) == 0)
|
|
|
|
log_cmd_error("No saved design '%s' found!\n", args[argidx].c_str());
|
|
|
|
copy_from_design = saved_designs.at(args[argidx]);
|
|
|
|
copy_to_design = design;
|
|
|
|
as_name = args[argidx];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (copy_from_design != NULL && args[argidx] == "-as" && argidx+1 < args.size()) {
|
2014-02-06 14:52:07 -06:00
|
|
|
as_name = args[++argidx];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
2013-07-27 07:27:51 -05:00
|
|
|
}
|
2014-02-06 14:52:07 -06:00
|
|
|
|
|
|
|
if (copy_from_design != NULL)
|
|
|
|
{
|
2017-06-30 11:52:52 -05:00
|
|
|
if (copy_from_design != design && argidx == args.size() && !import_mode)
|
2014-02-07 07:16:42 -06:00
|
|
|
cmd_error(args, argidx, "Missing selection.");
|
2014-02-06 14:52:07 -06:00
|
|
|
|
2017-06-30 11:52:52 -05:00
|
|
|
RTLIL::Selection sel;
|
2014-02-06 14:52:07 -06:00
|
|
|
if (argidx != args.size()) {
|
|
|
|
handle_extra_select_args(this, args, argidx, args.size(), copy_from_design);
|
|
|
|
sel = copy_from_design->selection_stack.back();
|
|
|
|
copy_from_design->selection_stack.pop_back();
|
|
|
|
argidx = args.size();
|
|
|
|
}
|
|
|
|
|
2014-07-27 03:18:00 -05:00
|
|
|
for (auto &it : copy_from_design->modules_) {
|
2014-02-06 14:52:07 -06:00
|
|
|
if (sel.selected_whole_module(it.first)) {
|
|
|
|
copy_src_modules.push_back(it.second);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (sel.selected_module(it.first))
|
|
|
|
log_cmd_error("Module %s is only partly selected.\n", RTLIL::id2cstr(it.first));
|
|
|
|
}
|
2017-06-30 11:52:52 -05:00
|
|
|
|
|
|
|
if (import_mode) {
|
|
|
|
for (auto module : copy_src_modules)
|
|
|
|
{
|
|
|
|
if (module->get_bool_attribute("\\top")) {
|
|
|
|
copy_src_modules.clear();
|
|
|
|
copy_src_modules.push_back(module);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-06 14:52:07 -06:00
|
|
|
}
|
|
|
|
|
2013-07-27 07:27:51 -05:00
|
|
|
extra_args(args, argidx, design, false);
|
|
|
|
|
|
|
|
if (!got_mode)
|
2014-02-20 16:28:59 -06:00
|
|
|
cmd_error(args, argidx, "Missing mode argument.");
|
|
|
|
|
|
|
|
if (pop_mode && pushed_designs.empty())
|
|
|
|
log_cmd_error("No pushed designs.\n");
|
2013-07-27 07:27:51 -05:00
|
|
|
|
2017-06-30 11:52:52 -05:00
|
|
|
if (import_mode)
|
|
|
|
{
|
|
|
|
std::string prefix = RTLIL::escape_id(as_name);
|
|
|
|
|
|
|
|
pool<Module*> queue;
|
|
|
|
dict<IdString, IdString> done;
|
|
|
|
|
|
|
|
if (copy_to_design->modules_.count(prefix))
|
|
|
|
delete copy_to_design->modules_.at(prefix);
|
|
|
|
|
|
|
|
if (GetSize(copy_src_modules) != 1)
|
|
|
|
log_cmd_error("No top module found in source design.\n");
|
|
|
|
|
|
|
|
for (auto mod : copy_src_modules)
|
|
|
|
{
|
|
|
|
log("Importing %s as %s.\n", log_id(mod), log_id(prefix));
|
|
|
|
|
|
|
|
copy_to_design->modules_[prefix] = mod->clone();
|
|
|
|
copy_to_design->modules_[prefix]->name = prefix;
|
|
|
|
copy_to_design->modules_[prefix]->design = copy_to_design;
|
|
|
|
copy_to_design->modules_[prefix]->attributes.erase("\\top");
|
|
|
|
|
|
|
|
queue.insert(copy_to_design->modules_[prefix]);
|
|
|
|
done[mod->name] = prefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!queue.empty())
|
|
|
|
{
|
|
|
|
pool<Module*> old_queue;
|
|
|
|
old_queue.swap(queue);
|
|
|
|
|
|
|
|
for (auto mod : old_queue)
|
|
|
|
for (auto cell : mod->cells())
|
|
|
|
{
|
|
|
|
Module *fmod = copy_from_design->module(cell->type);
|
|
|
|
|
|
|
|
if (fmod == nullptr)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (done.count(cell->type) == 0)
|
|
|
|
{
|
|
|
|
std::string trg_name = prefix + "." + (cell->type.c_str() + (*cell->type.c_str() == '\\'));
|
|
|
|
|
|
|
|
log("Importing %s as %s.\n", log_id(fmod), log_id(trg_name));
|
|
|
|
|
|
|
|
if (copy_to_design->modules_.count(trg_name))
|
|
|
|
delete copy_to_design->modules_.at(trg_name);
|
|
|
|
|
|
|
|
copy_to_design->modules_[trg_name] = fmod->clone();
|
|
|
|
copy_to_design->modules_[trg_name]->name = trg_name;
|
|
|
|
copy_to_design->modules_[trg_name]->design = copy_to_design;
|
|
|
|
copy_to_design->modules_[trg_name]->attributes.erase("\\top");
|
|
|
|
|
|
|
|
queue.insert(copy_to_design->modules_[trg_name]);
|
|
|
|
done[cell->type] = trg_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
cell->type = done.at(cell->type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2014-02-06 14:52:07 -06:00
|
|
|
if (copy_to_design != NULL)
|
2013-07-27 07:27:51 -05:00
|
|
|
{
|
2014-02-06 14:52:07 -06:00
|
|
|
if (!as_name.empty() && copy_src_modules.size() > 1)
|
|
|
|
log_cmd_error("Only one module can be selected in combination with -as.\n");
|
2013-07-27 07:27:51 -05:00
|
|
|
|
2014-02-06 14:52:07 -06:00
|
|
|
for (auto mod : copy_src_modules)
|
|
|
|
{
|
2014-08-02 11:58:40 -05:00
|
|
|
std::string trg_name = as_name.empty() ? mod->name.str() : RTLIL::escape_id(as_name);
|
2013-07-27 07:27:51 -05:00
|
|
|
|
2014-07-27 03:18:00 -05:00
|
|
|
if (copy_to_design->modules_.count(trg_name))
|
|
|
|
delete copy_to_design->modules_.at(trg_name);
|
2017-06-30 11:52:52 -05:00
|
|
|
|
2014-07-27 03:18:00 -05:00
|
|
|
copy_to_design->modules_[trg_name] = mod->clone();
|
|
|
|
copy_to_design->modules_[trg_name]->name = trg_name;
|
2014-07-31 07:11:39 -05:00
|
|
|
copy_to_design->modules_[trg_name]->design = copy_to_design;
|
2014-02-06 14:52:07 -06:00
|
|
|
}
|
2013-07-27 07:27:51 -05:00
|
|
|
}
|
|
|
|
|
2014-02-20 16:28:59 -06:00
|
|
|
if (!save_name.empty() || push_mode)
|
2013-07-27 07:27:51 -05:00
|
|
|
{
|
|
|
|
RTLIL::Design *design_copy = new RTLIL::Design;
|
|
|
|
|
2014-07-27 03:18:00 -05:00
|
|
|
for (auto &it : design->modules_)
|
2014-07-31 07:11:39 -05:00
|
|
|
design_copy->add(it.second->clone());
|
2013-07-27 07:27:51 -05:00
|
|
|
|
|
|
|
design_copy->selection_stack = design->selection_stack;
|
|
|
|
design_copy->selection_vars = design->selection_vars;
|
|
|
|
design_copy->selected_active_module = design->selected_active_module;
|
|
|
|
|
|
|
|
if (saved_designs.count(save_name))
|
|
|
|
delete saved_designs.at(save_name);
|
2014-02-20 16:28:59 -06:00
|
|
|
|
|
|
|
if (push_mode)
|
|
|
|
pushed_designs.push_back(design_copy);
|
|
|
|
else
|
|
|
|
saved_designs[save_name] = design_copy;
|
2013-07-27 07:27:51 -05:00
|
|
|
}
|
|
|
|
|
2014-02-20 16:28:59 -06:00
|
|
|
if (reset_mode || !load_name.empty() || push_mode || pop_mode)
|
2014-02-06 14:52:07 -06:00
|
|
|
{
|
2014-07-27 03:18:00 -05:00
|
|
|
for (auto &it : design->modules_)
|
2014-02-06 14:52:07 -06:00
|
|
|
delete it.second;
|
2014-07-27 03:18:00 -05:00
|
|
|
design->modules_.clear();
|
2014-02-06 14:52:07 -06:00
|
|
|
|
|
|
|
design->selection_stack.clear();
|
|
|
|
design->selection_vars.clear();
|
|
|
|
design->selected_active_module.clear();
|
|
|
|
|
|
|
|
design->selection_stack.push_back(RTLIL::Selection());
|
|
|
|
}
|
|
|
|
|
2016-11-30 04:25:55 -06:00
|
|
|
if (reset_mode || reset_vlog_mode || !load_name.empty() || push_mode || pop_mode)
|
|
|
|
{
|
|
|
|
for (auto node : design->verilog_packages)
|
|
|
|
delete node;
|
|
|
|
design->verilog_packages.clear();
|
|
|
|
|
|
|
|
for (auto node : design->verilog_globals)
|
|
|
|
delete node;
|
|
|
|
design->verilog_globals.clear();
|
|
|
|
|
|
|
|
design->verilog_defines.clear();
|
|
|
|
}
|
|
|
|
|
2014-02-20 16:28:59 -06:00
|
|
|
if (!load_name.empty() || pop_mode)
|
2013-07-27 07:27:51 -05:00
|
|
|
{
|
2014-02-20 16:28:59 -06:00
|
|
|
RTLIL::Design *saved_design = pop_mode ? pushed_designs.back() : saved_designs.at(load_name);
|
|
|
|
|
2014-07-27 03:18:00 -05:00
|
|
|
for (auto &it : saved_design->modules_)
|
2014-07-31 07:11:39 -05:00
|
|
|
design->add(it.second->clone());
|
2013-07-27 07:27:51 -05:00
|
|
|
|
|
|
|
design->selection_stack = saved_design->selection_stack;
|
|
|
|
design->selection_vars = saved_design->selection_vars;
|
|
|
|
design->selected_active_module = saved_design->selected_active_module;
|
2016-11-30 04:25:55 -06:00
|
|
|
|
|
|
|
if (pop_mode) {
|
|
|
|
delete saved_design;
|
|
|
|
pushed_designs.pop_back();
|
|
|
|
}
|
2013-07-27 07:27:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} DesignPass;
|
2014-02-20 16:28:59 -06:00
|
|
|
|
2014-07-31 06:19:47 -05:00
|
|
|
YOSYS_NAMESPACE_END
|
|
|
|
|