diff options
author | Miodrag Milanovic <mmicko@gmail.com> | 2019-06-13 20:42:11 +0200 |
---|---|---|
committer | Miodrag Milanovic <mmicko@gmail.com> | 2019-06-13 20:42:11 +0200 |
commit | 03dff10cbde4c55e4ac9b53a01ba84f4bdac169b (patch) | |
tree | 9777bd23e29585b8dbf10cfb64f41823c4c1f67c | |
parent | 4de147d9e42d7c932773544011a36e4550530a9e (diff) | |
download | nextpnr-03dff10cbde4c55e4ac9b53a01ba84f4bdac169b.tar.gz nextpnr-03dff10cbde4c55e4ac9b53a01ba84f4bdac169b.tar.bz2 nextpnr-03dff10cbde4c55e4ac9b53a01ba84f4bdac169b.zip |
Load properties from json and propagate to context create
-rw-r--r-- | common/command.cc | 9 | ||||
-rw-r--r-- | common/command.h | 2 | ||||
-rw-r--r-- | ecp5/main.cc | 4 | ||||
-rw-r--r-- | generic/main.cc | 4 | ||||
-rw-r--r-- | ice40/main.cc | 4 | ||||
-rw-r--r-- | json/jsonparse.cc | 49 | ||||
-rw-r--r-- | json/jsonparse.h | 2 |
7 files changed, 65 insertions, 9 deletions
diff --git a/common/command.cc b/common/command.cc index 4822585c..209f8cab 100644 --- a/common/command.cc +++ b/common/command.cc @@ -358,7 +358,14 @@ int CommandHandler::exec() if (executeBeforeContext()) return 0; - std::unique_ptr<Context> ctx = createContext(); + std::unordered_map<std::string,Property> values; + if (vm.count("json")) { + std::string filename = vm["json"].as<std::string>(); + std::ifstream f(filename); + if (!load_json_settings(f, filename, values)) + log_error("Loading design failed.\n"); + } + std::unique_ptr<Context> ctx = createContext(values); settings = std::unique_ptr<Settings>(new Settings(ctx.get())); setupContext(ctx.get()); setupArchContext(ctx.get()); diff --git a/common/command.h b/common/command.h index d4f9a12e..5c5ceb9a 100644 --- a/common/command.h +++ b/common/command.h @@ -39,7 +39,7 @@ class CommandHandler protected: virtual void setupArchContext(Context *ctx) = 0; - virtual std::unique_ptr<Context> createContext() = 0; + virtual std::unique_ptr<Context> createContext(std::unordered_map<std::string,Property> &values) = 0; virtual po::options_description getArchOptions() = 0; virtual void validate(){}; virtual void customAfterLoad(Context *ctx){}; diff --git a/ecp5/main.cc b/ecp5/main.cc index 9d0241e9..355e346d 100644 --- a/ecp5/main.cc +++ b/ecp5/main.cc @@ -34,7 +34,7 @@ class ECP5CommandHandler : public CommandHandler public: ECP5CommandHandler(int argc, char **argv); virtual ~ECP5CommandHandler(){}; - std::unique_ptr<Context> createContext() override; + std::unique_ptr<Context> createContext(std::unordered_map<std::string,Property> &values) override; void setupArchContext(Context *ctx) override{}; void customAfterLoad(Context *ctx) override; void validate() override; @@ -98,7 +98,7 @@ void ECP5CommandHandler::customBitstream(Context *ctx) write_bitstream(ctx, basecfg, textcfg); } -std::unique_ptr<Context> ECP5CommandHandler::createContext() +std::unique_ptr<Context> ECP5CommandHandler::createContext(std::unordered_map<std::string,Property> &values) { chipArgs.type = ArchArgs::LFE5U_45F; diff --git a/generic/main.cc b/generic/main.cc index c203f35c..8ab3b0e8 100644 --- a/generic/main.cc +++ b/generic/main.cc @@ -32,7 +32,7 @@ class GenericCommandHandler : public CommandHandler public: GenericCommandHandler(int argc, char **argv); virtual ~GenericCommandHandler(){}; - std::unique_ptr<Context> createContext() override; + std::unique_ptr<Context> createContext(std::unordered_map<std::string,Property> &values) override; void setupArchContext(Context *ctx) override{}; void customBitstream(Context *ctx) override; @@ -51,7 +51,7 @@ po::options_description GenericCommandHandler::getArchOptions() void GenericCommandHandler::customBitstream(Context *ctx) {} -std::unique_ptr<Context> GenericCommandHandler::createContext() +std::unique_ptr<Context> GenericCommandHandler::createContext(std::unordered_map<std::string,Property> &values) { return std::unique_ptr<Context>(new Context(chipArgs)); } diff --git a/ice40/main.cc b/ice40/main.cc index 4cbca2d4..00d7fe3e 100644 --- a/ice40/main.cc +++ b/ice40/main.cc @@ -36,7 +36,7 @@ class Ice40CommandHandler : public CommandHandler public: Ice40CommandHandler(int argc, char **argv); virtual ~Ice40CommandHandler(){}; - std::unique_ptr<Context> createContext() override; + std::unique_ptr<Context> createContext(std::unordered_map<std::string,Property> &values) override; void setupArchContext(Context *ctx) override; void validate() override; void customAfterLoad(Context *ctx) override; @@ -116,7 +116,7 @@ void Ice40CommandHandler::setupArchContext(Context *ctx) } } -std::unique_ptr<Context> Ice40CommandHandler::createContext() +std::unique_ptr<Context> Ice40CommandHandler::createContext(std::unordered_map<std::string,Property> &values) { if (vm.count("lp384")) { chipArgs.type = ArchArgs::LP384; diff --git a/json/jsonparse.cc b/json/jsonparse.cc index df8f45db..a6b45282 100644 --- a/json/jsonparse.cc +++ b/json/jsonparse.cc @@ -914,4 +914,53 @@ bool parse_json_file(std::istream &f, std::string &filename, Context *ctx) } } +bool load_json_settings(std::istream &f, std::string &filename, std::unordered_map<std::string,Property> &values) +{ + try { + using namespace JsonParser; + + if (!f) + log_error("failed to open JSON file.\n"); + + int lineno = 1; + + JsonNode root(f, lineno); + + if (root.type != 'D') + log_error("JSON root node is not a dictionary.\n"); + + if (root.data_dict.count("modules") != 0) { + JsonNode *modules = root.data_dict.at("modules"); + + if (modules->type != 'D') + log_error("JSON modules node is not a dictionary.\n"); + + for (auto &it : modules->data_dict) { + JsonNode *node = it.second; + if (is_blackbox(node)) + continue; + + if (node->data_dict.count("settings")) { + JsonNode *attr_node = node->data_dict.at("settings"); + for (int attrid = 0; attrid < GetSize(attr_node->data_dict_keys); attrid++) { + JsonNode *param = attr_node->data_dict.at(attr_node->data_dict_keys[attrid]); + std::string pId = attr_node->data_dict_keys[attrid]; + if (param->type == 'N') { + values[pId].setNumber(param->data_number); + } else if (param->type == 'S') + values[pId].setString(param->data_string); + else + log_error("JSON parameter type of \"%s\' of module not supported\n", pId.c_str()); + + } + } + } + } + + return true; + } catch (log_execution_error_exception) { + return false; + } +} + NEXTPNR_NAMESPACE_END diff --git a/json/jsonparse.h b/json/jsonparse.h index fe71444f..ca971e5f 100644 --- a/json/jsonparse.h +++ b/json/jsonparse.h @@ -27,7 +27,7 @@ NEXTPNR_NAMESPACE_BEGIN extern bool parse_json_file(std::istream &, std::string &, Context *); - +extern bool load_json_settings(std::istream &f, std::string &filename, std::unordered_map<std::string,Property> &values); NEXTPNR_NAMESPACE_END #endif |