start split workload from the main.cpp in openfpga

This commit is contained in:
tangxifan 2020-01-23 13:24:35 -07:00
parent 521faebde2
commit ba207ee5a5
5 changed files with 114 additions and 74 deletions

View File

@ -0,0 +1,35 @@
#ifndef OPENFPGA_CONTEXT_H
#define OPENFPGA_CONTEXT_H
#include "openfpga_arch.h"
/********************************************************************
* This file includes the declaration of the date structure
* OpenfpgaContext, which is used for data exchange between
* different modules in OpenFPGA shell environment
*
* If a command of OpenFPGA needs to exchange data with other commands,
* it must use this data structure to access/mutate.
* In such case, you must add data structures to OpenfpgaContext
*
* Note:
* Please respect to the following rules when using the OpenfpgaContext
* 1. This data structure will be created only once in the main() function
* The data structure is design to be large and contain all the
* data structure required by each module of OpenFPGA core engine.
* Do NOT create or duplicate in your own module!
* 2. Be clear in your mind if you want to access/mutate the data inside OpenfpgaContext
* Read-only data should be accessed by
* const OpenfpgaContext&
* Mutate should use reference
* OpenfpgaContext&
* 3. Please keep the definition of OpenfpgaContext short
* Do put ONLY well-modularized data structure under this root.
*******************************************************************/
class OpenfpgaContext {
private:
/* Data structure to store information from read_openfpga_arch library */
openfpga::Arch arch_;
};
#endif

View File

@ -0,0 +1,43 @@
/********************************************************************
* This file includes the functions to create the title page
* which introduces generality of OpenFPGA framework
*******************************************************************/
#include "openfpga_title.h"
/********************************************************************
* Generate a string of openfpga title introduction
* This is mainly used when launching OpenFPGA shell
*******************************************************************/
const char* create_openfpga_title() {
std::string title;
title += std::string("\n");
title += std::string(" OpenFPGA: An Open-source FPGA IP Generator\n");
title += std::string("\n");
title += std::string("Contributors: Xifan Tang\tAurelien Alacchi\tBaudouin Chauviere\n");
title += std::string("\n");
title += std::string("The MIT License\n");
title += std::string("\n");
title += std::string("Copyright (c) 2018 LNIS - The University of Utah\n");
title += std::string("\n");
title += std::string("Permission is hereby granted, free of charge, to any person obtaining a copy\n");
title += std::string("of this software and associated documentation files (the \"Software\"), to deal\n");
title += std::string("in the Software without restriction, including without limitation the rights\n");
title += std::string("to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n");
title += std::string("copies of the Software, and to permit persons to whom the Software is\n");
title += std::string("furnished to do so, subject to the following conditions:\n");
title += std::string("\n");
title += std::string("The above copyright notice and this permission notice shall be included in\n");
title += std::string("all copies or substantial portions of the Software.\n");
title += std::string("\n");
title += std::string("THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n");
title += std::string("IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n");
title += std::string("FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n");
title += std::string("AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n");
title += std::string("LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n");
title += std::string("OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n");
title += std::string("THE SOFTWARE.\n");
title += std::string("\n");
return title.c_str();
}

View File

@ -0,0 +1,14 @@
#ifndef OPENFPGA_TITLE_H
#define OPENFPGA_TITLE_H
/********************************************************************
* Include header files that are required by function declaration
*******************************************************************/
#include <string>
/********************************************************************
* Function declaration
*******************************************************************/
const char* create_openfpga_title();
#endif

View File

@ -1,34 +1,27 @@
/******************************************************************** /********************************************************************
* Build the OpenFPGA shell interface * Build the OpenFPGA shell interface
*******************************************************************/ *******************************************************************/
/* Header file from vtrutil library */
#include "vtr_log.h" #include "vtr_log.h"
/* Header file from libopenfpgashell library */
#include "command_parser.h" #include "command_parser.h"
#include "command_echo.h" #include "command_echo.h"
#include "shell.h" #include "shell.h"
/* Header file from openfpga */
#include "vpr_main.h" #include "vpr_main.h"
#include "openfpga_title.h"
#include "openfpga_context.h"
using namespace openfpga; using namespace openfpga;
class ShellContext { /********************************************************************
public: * Main function to start OpenFPGA shell interface
int a; *******************************************************************/
};
static
void shell_execute_set(ShellContext& context,
const Command& cmd, const CommandContext& cmd_context) {
CommandOptionId opt_id = cmd.option("value");
/* Get the value of a in the command context */
context.a = std::atoi(cmd_context.option_value(cmd, opt_id).c_str());
}
static
void shell_execute_print(ShellContext& context) {
VTR_LOG("a=%d\n", context.a);
}
int main(int argc, char** argv) { int main(int argc, char** argv) {
/* Create the command to launch shell in different modes */ /* Create the command to launch shell in different modes */
Command start_cmd("OpenFPGA"); Command start_cmd("OpenFPGA");
/* Add two options: /* Add two options:
@ -47,70 +40,23 @@ int main(int argc, char** argv) {
/* Create a shell object /* Create a shell object
* Add two commands, which are * Add two commands, which are
* 1. help * 1. exit
* 2. exit * 2. help. This must the last to add
*/ */
Shell<ShellContext> shell("OpenFPGA"); Shell<OpenfpgaContext> shell("OpenFPGA");
std::string shell_title;
shell_title += std::string("\n"); shell.add_title(create_openfpga_title());
shell_title += std::string(" OpenFPGA: An Open-source FPGA IP Generator\n");
shell_title += std::string("\n");
shell_title += std::string("Contributors: Xifan Tang\tAurelien Alacchi\tBaudouin Chauviere\n");
shell_title += std::string("\n");
shell_title += std::string("The MIT License\n");
shell_title += std::string("\n");
shell_title += std::string("Copyright (c) 2018 LNIS - The University of Utah\n");
shell_title += std::string("\n");
shell_title += std::string("Permission is hereby granted, free of charge, to any person obtaining a copy\n");
shell_title += std::string("of this software and associated documentation files (the \"Software\"), to deal\n");
shell_title += std::string("in the Software without restriction, including without limitation the rights\n");
shell_title += std::string("to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n");
shell_title += std::string("copies of the Software, and to permit persons to whom the Software is\n");
shell_title += std::string("furnished to do so, subject to the following conditions:\n");
shell_title += std::string("\n");
shell_title += std::string("The above copyright notice and this permission notice shall be included in\n");
shell_title += std::string("all copies or substantial portions of the Software.\n");
shell_title += std::string("\n");
shell_title += std::string("THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n");
shell_title += std::string("IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n");
shell_title += std::string("FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n");
shell_title += std::string("AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n");
shell_title += std::string("LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n");
shell_title += std::string("OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n");
shell_title += std::string("THE SOFTWARE.\n");
shell.add_title(shell_title.c_str());
/* Add a new class of commands */ /* Add a new class of commands */
ShellCommandClassId arith_cmd_class = shell.add_command_class("Arithmetic"); ShellCommandClassId arith_cmd_class = shell.add_command_class("VPR");
/* Create a command of 'set' with a required option 'value' with a value
* This function sets a value to an internal variable of ShellContext
*/
Command shell_cmd_set("set");
CommandOptionId set_opt_value = shell_cmd_set.add_option("value", true, "value of variable");
shell_cmd_set.set_option_require_value(set_opt_value, OPT_STRING);
ShellCommandId shell_cmd_set_id = shell.add_command(shell_cmd_set, "Set a value to internal variable 'a'");
shell.set_command_class(shell_cmd_set_id, arith_cmd_class);
shell.set_command_execute_function(shell_cmd_set_id, shell_execute_set);
/* Create a command of 'print'
* This function will print the value of an internal variable of ShellContext
*/
Command shell_cmd_print("print");
ShellCommandId shell_cmd_print_id = shell.add_command(shell_cmd_print, "Print the value of internal variable 'a'");
shell.set_command_class(shell_cmd_print_id, arith_cmd_class);
shell.set_command_execute_function(shell_cmd_print_id, shell_execute_print);
/* Create a macro command of 'vpr' which will call the main engine of vpr /* Create a macro command of 'vpr' which will call the main engine of vpr
*/ */
Command shell_cmd_vpr("vpr"); Command shell_cmd_vpr("vpr");
ShellCommandId shell_cmd_vpr_id = shell.add_command(shell_cmd_vpr, "A macro function to print arguments"); ShellCommandId shell_cmd_vpr_id = shell.add_command(shell_cmd_vpr, "Start VPR core engine to pack, place and route a BLIF design on a FPGA architecture");
shell.set_command_class(shell_cmd_vpr_id, arith_cmd_class); shell.set_command_class(shell_cmd_vpr_id, arith_cmd_class);
shell.set_command_execute_function(shell_cmd_vpr_id, vpr::vpr); shell.set_command_execute_function(shell_cmd_vpr_id, vpr::vpr);
/* Add a new class of commands */ /* Add a new class of commands */
ShellCommandClassId basic_cmd_class = shell.add_command_class("Basic"); ShellCommandClassId basic_cmd_class = shell.add_command_class("Basic");
@ -126,7 +72,7 @@ int main(int argc, char** argv) {
shell.set_command_execute_function(shell_cmd_help_id, [shell](){shell.print_commands();}); shell.set_command_execute_function(shell_cmd_help_id, [shell](){shell.print_commands();});
/* Create the data base for the shell */ /* Create the data base for the shell */
ShellContext shell_context; OpenfpgaContext openfpga_context;
/* Parse the option, to avoid issues, we use the command name to replace the argv[0] */ /* Parse the option, to avoid issues, we use the command name to replace the argv[0] */
std::vector<std::string> cmd_opts; std::vector<std::string> cmd_opts;
@ -142,13 +88,13 @@ int main(int argc, char** argv) {
} else { } else {
/* Parse succeed. Start a shell */ /* Parse succeed. Start a shell */
if (true == start_cmd_context.option_enable(start_cmd, opt_interactive)) { if (true == start_cmd_context.option_enable(start_cmd, opt_interactive)) {
shell.run_interactive_mode(shell_context); shell.run_interactive_mode(openfpga_context);
return 0; return 0;
} }
if (true == start_cmd_context.option_enable(start_cmd, opt_script_mode)) { if (true == start_cmd_context.option_enable(start_cmd, opt_script_mode)) {
shell.run_script_mode(start_cmd_context.option_value(start_cmd, opt_script_mode).c_str(), shell.run_script_mode(start_cmd_context.option_value(start_cmd, opt_script_mode).c_str(),
shell_context); openfpga_context);
return 0; return 0;
} }
/* Reach here there is something wrong, show the help desk */ /* Reach here there is something wrong, show the help desk */

View File

@ -19,6 +19,8 @@
#include "vpr_signal_handler.h" #include "vpr_signal_handler.h"
#include "vpr_tatum_error.h" #include "vpr_tatum_error.h"
#include "vpr_main.h"
#include "globals.h" #include "globals.h"
namespace vpr { namespace vpr {