diff options
author | Miodrag Milanović <mmicko@gmail.com> | 2018-08-08 09:18:03 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-08-08 09:18:03 -0700 |
commit | 330bb86bfcfd0e1727c6e786f41b1f4c2440cac4 (patch) | |
tree | ea6a0c6531aed19796cfa6ab50a24fb1408ad72f /common | |
parent | 8553573d2485ac2ec60d1c49949c254e02d35490 (diff) | |
parent | fc5cee6fb896bc4d7a8b79dcde789e03c787bd89 (diff) | |
download | nextpnr-330bb86bfcfd0e1727c6e786f41b1f4c2440cac4.tar.gz nextpnr-330bb86bfcfd0e1727c6e786f41b1f4c2440cac4.tar.bz2 nextpnr-330bb86bfcfd0e1727c6e786f41b1f4c2440cac4.zip |
Merge pull request #43 from YosysHQ/common_main
Common main and project
Diffstat (limited to 'common')
-rw-r--r-- | common/command.cc | 262 | ||||
-rw-r--r-- | common/command.h | 70 | ||||
-rw-r--r-- | common/project.cc | 86 | ||||
-rw-r--r-- | common/project.h | 42 |
4 files changed, 460 insertions, 0 deletions
diff --git a/common/command.cc b/common/command.cc new file mode 100644 index 00000000..cf12df3a --- /dev/null +++ b/common/command.cc @@ -0,0 +1,262 @@ +/* + * nextpnr -- Next Generation Place and Route + * + * Copyright (C) 2018 Clifford Wolf <clifford@symbioticeda.com> + * Copyright (C) 2018 Miodrag Milanovic <miodrag@symbioticeda.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. + * + */ + +#ifndef NO_GUI +#include <QApplication> +#include "application.h" +#include "mainwindow.h" +#endif +#ifndef NO_PYTHON +#include "pybindings.h" +#endif + +#include <boost/filesystem/convenience.hpp> +#include <boost/program_options.hpp> +#include <fstream> +#include <iostream> +#include "command.h" +#include "design_utils.h" +#include "jsonparse.h" +#include "log.h" +#include "timing.h" +#include "version.h" + +NEXTPNR_NAMESPACE_BEGIN + +CommandHandler::CommandHandler(int argc, char **argv) : argc(argc), argv(argv) { log_files.push_back(stdout); } + +bool CommandHandler::parseOptions() +{ + options.add(getGeneralOptions()).add(getArchOptions()); + try { + po::parsed_options parsed = + po::command_line_parser(argc, argv) + .style(po::command_line_style::default_style ^ po::command_line_style::allow_guessing) + .options(options) + .positional(pos) + .run(); + po::store(parsed, vm); + po::notify(vm); + return true; + } catch (std::exception &e) { + std::cout << e.what() << "\n"; + return false; + } +} + +bool CommandHandler::executeBeforeContext() +{ + if (vm.count("help") || argc == 1) { + std::cout << boost::filesystem::basename(argv[0]) + << " -- Next Generation Place and Route (git sha1 " GIT_COMMIT_HASH_STR ")\n"; + std::cout << options << "\n"; + return argc != 1; + } + + if (vm.count("version")) { + std::cout << boost::filesystem::basename(argv[0]) + << " -- Next Generation Place and Route (git sha1 " GIT_COMMIT_HASH_STR ")\n"; + return true; + } + validate(); + return false; +} + +po::options_description CommandHandler::getGeneralOptions() +{ + po::options_description general("General options"); + general.add_options()("help,h", "show help"); + general.add_options()("verbose,v", "verbose output"); + general.add_options()("debug", "debug output"); + general.add_options()("force,f", "keep running after errors"); +#ifndef NO_GUI + general.add_options()("gui", "start gui"); +#endif +#ifndef NO_PYTHON + general.add_options()("run", po::value<std::vector<std::string>>(), "python file to execute"); + pos.add("run", -1); +#endif + general.add_options()("json", po::value<std::string>(), "JSON design file to ingest"); + general.add_options()("seed", po::value<int>(), "seed value for random number generator"); + general.add_options()("slack_redist_iter", po::value<int>(), "number of iterations between slack redistribution"); + general.add_options()("cstrweight", po::value<float>(), "placer weighting for relative constraint satisfaction"); + + general.add_options()("version,V", "show version"); + general.add_options()("test", "check architecture database integrity"); + general.add_options()("freq", po::value<double>(), "set target frequency for design in MHz"); + general.add_options()("no-tmdriv", "disable timing-driven placement"); + general.add_options()("save", po::value<std::string>(), "project file to write"); + general.add_options()("load", po::value<std::string>(), "project file to read"); + return general; +} + +void CommandHandler::setupContext(Context *ctx) +{ + if (vm.count("verbose")) { + ctx->verbose = true; + } + + if (vm.count("debug")) { + ctx->verbose = true; + ctx->debug = true; + } + + if (vm.count("force")) { + ctx->force = true; + } + + if (vm.count("seed")) { + ctx->rngseed(vm["seed"].as<int>()); + } + + if (vm.count("slack_redist_iter")) { + ctx->slack_redist_iter = vm["slack_redist_iter"].as<int>(); + if (vm.count("freq") && vm["freq"].as<double>() == 0) { + ctx->auto_freq = true; +#ifndef NO_GUI + if (!vm.count("gui")) +#endif + log_warning("Target frequency not specified. Will optimise for max frequency.\n"); + } + } + + if (vm.count("cstrweight")) { + // ctx->placer_constraintWeight = vm["cstrweight"].as<float>(); + } + + if (vm.count("freq")) { + auto freq = vm["freq"].as<double>(); + if (freq > 0) + ctx->target_freq = freq * 1e6; + } + + ctx->timing_driven = true; + if (vm.count("no-tmdriv")) + ctx->timing_driven = false; +} + +int CommandHandler::executeMain(std::unique_ptr<Context> ctx) +{ + if (vm.count("test")) { + ctx->archcheck(); + return 0; + } + +#ifndef NO_GUI + if (vm.count("gui")) { + Application a(argc, argv); + MainWindow w(std::move(ctx), chipArgs); + try { + if (vm.count("json")) { + std::string filename = vm["json"].as<std::string>(); + std::ifstream f(filename); + if (!parse_json_file(f, filename, w.getContext())) + log_error("Loading design failed.\n"); + + customAfterLoad(w.getContext()); + w.updateJsonLoaded(); + } + } catch (log_execution_error_exception) { + // show error is handled by gui itself + } + w.show(); + + return a.exec(); + } +#endif + if (vm.count("json")) { + std::string filename = vm["json"].as<std::string>(); + std::ifstream f(filename); + if (!parse_json_file(f, filename, ctx.get())) + log_error("Loading design failed.\n"); + + customAfterLoad(ctx.get()); + } + + if (vm.count("json") || vm.count("load")) { + if (!ctx->pack() && !ctx->force) + log_error("Packing design failed.\n"); + assign_budget(ctx.get()); + ctx->check(); + print_utilisation(ctx.get()); + if (!vm.count("pack-only")) { + if (!ctx->place() && !ctx->force) + log_error("Placing design failed.\n"); + ctx->check(); + if (!ctx->route() && !ctx->force) + log_error("Routing design failed.\n"); + } + + customBitstream(ctx.get()); + } + +#ifndef NO_PYTHON + if (vm.count("run")) { + init_python(argv[0], true); + python_export_global("ctx", *ctx); + + std::vector<std::string> files = vm["run"].as<std::vector<std::string>>(); + for (auto filename : files) + execute_python_file(filename.c_str()); + + deinit_python(); + } +#endif + + if (vm.count("save")) { + project.save(ctx.get(), vm["save"].as<std::string>()); + } + + return 0; +} + +void CommandHandler::conflicting_options(const boost::program_options::variables_map &vm, const char *opt1, + const char *opt2) +{ + if (vm.count(opt1) && !vm[opt1].defaulted() && vm.count(opt2) && !vm[opt2].defaulted()) { + std::string msg = "Conflicting options '" + std::string(opt1) + "' and '" + std::string(opt2) + "'."; + log_error("%s\n", msg.c_str()); + } +} + +int CommandHandler::exec() +{ + try { + if (!parseOptions()) + return -1; + + if (executeBeforeContext()) + return 0; + + std::unique_ptr<Context> ctx; + if (vm.count("load")) { + ctx = project.load(vm["load"].as<std::string>()); + } else { + ctx = createContext(); + } + setupContext(ctx.get()); + setupArchContext(ctx.get()); + return executeMain(std::move(ctx)); + } catch (log_execution_error_exception) { + return -1; + } +} + +NEXTPNR_NAMESPACE_END diff --git a/common/command.h b/common/command.h new file mode 100644 index 00000000..13d5a250 --- /dev/null +++ b/common/command.h @@ -0,0 +1,70 @@ +/* + * nextpnr -- Next Generation Place and Route + * + * Copyright (C) 2018 Clifford Wolf <clifford@symbioticeda.com> + * Copyright (C) 2018 Miodrag Milanovic <miodrag@symbioticeda.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. + * + */ + +#ifndef COMMAND_H +#define COMMAND_H + +#include <boost/program_options.hpp> +#include "nextpnr.h" +#include "project.h" + +NEXTPNR_NAMESPACE_BEGIN + +namespace po = boost::program_options; + +class CommandHandler +{ + public: + CommandHandler(int argc, char **argv); + virtual ~CommandHandler(){}; + + int exec(); + + protected: + virtual void setupArchContext(Context *ctx) = 0; + virtual std::unique_ptr<Context> createContext() = 0; + virtual po::options_description getArchOptions() = 0; + virtual void validate(){}; + virtual void customAfterLoad(Context *ctx){}; + virtual void customBitstream(Context *ctx){}; + void conflicting_options(const boost::program_options::variables_map &vm, const char *opt1, const char *opt2); + + private: + bool parseOptions(); + bool executeBeforeContext(); + void setupContext(Context *ctx); + int executeMain(std::unique_ptr<Context> ctx); + po::options_description getGeneralOptions(); + + protected: + po::variables_map vm; + ArchArgs chipArgs; + + private: + po::options_description options; + po::positional_options_description pos; + int argc; + char **argv; + ProjectHandler project; +}; + +NEXTPNR_NAMESPACE_END + +#endif // COMMAND_H diff --git a/common/project.cc b/common/project.cc new file mode 100644 index 00000000..244ca761 --- /dev/null +++ b/common/project.cc @@ -0,0 +1,86 @@ +/* + * nextpnr -- Next Generation Place and Route + * + * Copyright (C) 2018 Miodrag Milanovic <miodrag@symbioticeda.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 "project.h" +#include <boost/filesystem/convenience.hpp> +#include <boost/property_tree/json_parser.hpp> +#include <fstream> +#include "jsonparse.h" +#include "log.h" + +NEXTPNR_NAMESPACE_BEGIN + +void ProjectHandler::save(Context *ctx, std::string filename) +{ + std::ofstream f(filename); + pt::ptree root; + root.put("project.version", 1); + root.put("project.name", boost::filesystem::basename(filename)); + root.put("project.arch.name", ctx->archId().c_str(ctx)); + root.put("project.arch.type", ctx->archArgsToId(ctx->archArgs()).c_str(ctx)); + /* root.put("project.input.json", );*/ + root.put("project.params.freq", int(ctx->target_freq / 1e6)); + root.put("project.params.seed", ctx->rngstate); + saveArch(ctx, root); + pt::write_json(f, root); +} + +std::unique_ptr<Context> ProjectHandler::load(std::string filename) +{ + std::unique_ptr<Context> ctx; + try { + pt::ptree root; + boost::filesystem::path proj(filename); + pt::read_json(filename, root); + log_info("Loading project %s...\n", filename.c_str()); + log_break(); + + int version = root.get<int>("project.version"); + if (version != 1) + log_error("Wrong project format version.\n"); + + ctx = createContext(root); + + std::string arch_name = root.get<std::string>("project.arch.name"); + if (arch_name != ctx->archId().c_str(ctx.get())) + log_error("Unsuported project architecture.\n"); + + auto project = root.get_child("project"); + auto input = project.get_child("input"); + std::string filename = input.get<std::string>("json"); + boost::filesystem::path json = proj.parent_path() / filename; + std::ifstream f(json.string()); + if (!parse_json_file(f, filename, ctx.get())) + log_error("Loading design failed.\n"); + + if (project.count("params")) { + auto params = project.get_child("params"); + if (params.count("freq")) + ctx->target_freq = params.get<double>("freq") * 1e6; + if (params.count("seed")) + ctx->rngseed(params.get<uint64_t>("seed")); + } + loadArch(ctx.get(), root, proj.parent_path().string()); + } catch (...) { + log_error("Error loading project file.\n"); + } + return ctx; +} + +NEXTPNR_NAMESPACE_END diff --git a/common/project.h b/common/project.h new file mode 100644 index 00000000..14f03ecd --- /dev/null +++ b/common/project.h @@ -0,0 +1,42 @@ +/* + * nextpnr -- Next Generation Place and Route + * + * Copyright (C) 2018 Miodrag Milanovic <miodrag@symbioticeda.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. + * + */ + +#ifndef PROJECT_H +#define PROJECT_H + +#include <boost/property_tree/ptree.hpp> +#include "nextpnr.h" + +NEXTPNR_NAMESPACE_BEGIN + +namespace pt = boost::property_tree; + +struct ProjectHandler +{ + void save(Context *ctx, std::string filename); + std::unique_ptr<Context> load(std::string filename); + // implemented per arch + void saveArch(Context *ctx, pt::ptree &root); + std::unique_ptr<Context> createContext(pt::ptree &root); + void loadArch(Context *ctx, pt::ptree &root, std::string path); +}; + +NEXTPNR_NAMESPACE_END + +#endif // PROJECT_H |