From 655f84b00eb12eb460e75d1e37c0742b02715146 Mon Sep 17 00:00:00 2001 From: tangxifan Date: Thu, 23 Jan 2020 20:58:15 -0700 Subject: [PATCH] add write_openfpga_arch command to openfpga shell --- .../src/write_xml_openfpga_arch.cpp | 5 ++++ libopenfpga/libopenfpgashell/src/shell.h | 6 ++--- libopenfpga/libopenfpgashell/src/shell.tpp | 12 ++++----- openfpga/src/base/openfpga_read_arch.cpp | 25 +++++++++++++++++++ openfpga/src/base/openfpga_read_arch.h | 3 +++ openfpga/src/base/openfpga_setup_command.cpp | 13 ++++++++++ 6 files changed, 55 insertions(+), 9 deletions(-) diff --git a/libopenfpga/libarchopenfpga/src/write_xml_openfpga_arch.cpp b/libopenfpga/libarchopenfpga/src/write_xml_openfpga_arch.cpp index 6d6945a66..e262735e4 100644 --- a/libopenfpga/libarchopenfpga/src/write_xml_openfpga_arch.cpp +++ b/libopenfpga/libarchopenfpga/src/write_xml_openfpga_arch.cpp @@ -4,6 +4,9 @@ *******************************************************************/ #include +/* Headers from vtrutil library */ +#include "vtr_time.h" + /* Headers from openfpgautil library */ #include "openfpga_digest.h" @@ -20,6 +23,8 @@ *******************************************************************/ void write_xml_openfpga_arch(const char* fname, const openfpga::Arch& openfpga_arch) { + vtr::ScopedStartFinishTimer timer("Write OpenFPGA architecture"); + /* Create a file handler */ std::fstream fp; /* Open the file stream */ diff --git a/libopenfpga/libopenfpgashell/src/shell.h b/libopenfpga/libopenfpgashell/src/shell.h index c053842a9..2f67dd610 100644 --- a/libopenfpga/libopenfpgashell/src/shell.h +++ b/libopenfpga/libopenfpgashell/src/shell.h @@ -95,12 +95,12 @@ class Shell { * 4. Marco function, which directly call a macro function without command parsing * Users just need to specify the function object and its type will be automatically inferred */ - void set_command_execute_function(const ShellCommandId& cmd_id, + void set_command_const_execute_function(const ShellCommandId& cmd_id, std::function exec_func); void set_command_execute_function(const ShellCommandId& cmd_id, std::function exec_func); - void set_command_execute_function(const ShellCommandId& cmd_id, - std::function exec_func); + void set_command_const_execute_function(const ShellCommandId& cmd_id, + std::function exec_func); void set_command_execute_function(const ShellCommandId& cmd_id, std::function exec_func); void set_command_execute_function(const ShellCommandId& cmd_id, diff --git a/libopenfpga/libopenfpgashell/src/shell.tpp b/libopenfpga/libopenfpgashell/src/shell.tpp index 1df5ef744..30534cfeb 100644 --- a/libopenfpga/libopenfpgashell/src/shell.tpp +++ b/libopenfpga/libopenfpgashell/src/shell.tpp @@ -153,11 +153,11 @@ void Shell::set_command_class(const ShellCommandId& cmd_id, const ShellComman } template -void Shell::set_command_execute_function(const ShellCommandId& cmd_id, - std::function exec_func) { +void Shell::set_command_const_execute_function(const ShellCommandId& cmd_id, + std::function exec_func) { VTR_ASSERT(true == valid_command_id(cmd_id)); command_execute_function_types_[cmd_id] = CONST_STANDARD; - command_standard_execute_functions_[cmd_id] = exec_func; + command_const_execute_functions_[cmd_id] = exec_func; } template @@ -169,11 +169,11 @@ void Shell::set_command_execute_function(const ShellCommandId& cmd_id, } template -void Shell::set_command_execute_function(const ShellCommandId& cmd_id, - std::function exec_func) { +void Shell::set_command_const_execute_function(const ShellCommandId& cmd_id, + std::function exec_func) { VTR_ASSERT(true == valid_command_id(cmd_id)); command_execute_function_types_[cmd_id] = CONST_SHORT; - command_short_execute_functions_[cmd_id] = exec_func; + command_short_const_execute_functions_[cmd_id] = exec_func; } template diff --git a/openfpga/src/base/openfpga_read_arch.cpp b/openfpga/src/base/openfpga_read_arch.cpp index 765241db8..1a36e85a3 100644 --- a/openfpga/src/base/openfpga_read_arch.cpp +++ b/openfpga/src/base/openfpga_read_arch.cpp @@ -8,6 +8,7 @@ /* Headers from archopenfpga library */ #include "read_xml_openfpga_arch.h" #include "check_circuit_library.h" +#include "write_xml_openfpga_arch.h" #include "openfpga_read_arch.h" @@ -45,5 +46,29 @@ void read_arch(OpenfpgaContext& openfpga_context, check_circuit_library(openfpga_context.arch().circuit_lib); } +/******************************************************************** + * A function to write an OpenFPGA architecture file + * we use the APIs from the libarchopenfpga library + * + * The command will accept an option '--file' which is the architecture + * file provided by users + *******************************************************************/ +void write_arch(const OpenfpgaContext& openfpga_context, + const Command& cmd, const CommandContext& cmd_context) { + /* Check the option '--file' is enabled or not + * Actually, it must be enabled as the shell interface will check + * before reaching this fuction + */ + CommandOptionId opt_file = cmd.option("file"); + VTR_ASSERT(true == cmd_context.option_enable(cmd, opt_file)); + VTR_ASSERT(false == cmd_context.option_value(cmd, opt_file).empty()); + + std::string arch_file_name = cmd_context.option_value(cmd, opt_file); + + VTR_LOG("Writing XML architecture to '%s'...\n", + arch_file_name.c_str()); + write_xml_openfpga_arch(arch_file_name.c_str(), openfpga_context.arch()); +} + } /* end namespace openfpga */ diff --git a/openfpga/src/base/openfpga_read_arch.h b/openfpga/src/base/openfpga_read_arch.h index 8b300bd54..dcc04d77a 100644 --- a/openfpga/src/base/openfpga_read_arch.h +++ b/openfpga/src/base/openfpga_read_arch.h @@ -18,6 +18,9 @@ namespace openfpga { void read_arch(OpenfpgaContext& openfpga_context, const Command& cmd, const CommandContext& cmd_context); +void write_arch(const OpenfpgaContext& openfpga_context, + const Command& cmd, const CommandContext& cmd_context); + } /* end namespace openfpga */ #endif diff --git a/openfpga/src/base/openfpga_setup_command.cpp b/openfpga/src/base/openfpga_setup_command.cpp index a974e2704..99d827bfb 100644 --- a/openfpga/src/base/openfpga_setup_command.cpp +++ b/openfpga/src/base/openfpga_setup_command.cpp @@ -20,9 +20,22 @@ void add_openfpga_setup_commands(openfpga::Shell& shell) { shell_cmd_read_arch.set_option_short_name(read_arch_opt_file, "f"); shell_cmd_read_arch.set_option_require_value(read_arch_opt_file, openfpga::OPT_STRING); + /* Add command 'read_openfpga_arch' to the Shell */ ShellCommandId shell_cmd_read_arch_id = shell.add_command(shell_cmd_read_arch, "read OpenFPGA architecture file"); shell.set_command_class(shell_cmd_read_arch_id, openfpga_setup_cmd_class); shell.set_command_execute_function(shell_cmd_read_arch_id, read_arch); + + /* Command 'write_openfpga_arch' */ + Command shell_cmd_write_arch("write_openfpga_arch"); + /* Add an option '--file' in short '-f'*/ + CommandOptionId write_arch_opt_file = shell_cmd_write_arch.add_option("file", true, "file path to the architecture XML"); + shell_cmd_write_arch.set_option_short_name(write_arch_opt_file, "f"); + shell_cmd_write_arch.set_option_require_value(write_arch_opt_file, openfpga::OPT_STRING); + + /* Add command 'write_openfpga_arch' to the Shell */ + ShellCommandId shell_cmd_write_arch_id = shell.add_command(shell_cmd_write_arch, "write OpenFPGA architecture file"); + shell.set_command_class(shell_cmd_write_arch_id, openfpga_setup_cmd_class); + shell.set_command_const_execute_function(shell_cmd_write_arch_id, write_arch); } } /* end namespace openfpga */