Split the writer of build_fabric_bitstream to a separated command so that users will output multiple files in different formats

This commit is contained in:
tangxifan 2020-07-27 14:16:33 -06:00
parent fcd8a3cf4d
commit d68e77f322
7 changed files with 106 additions and 42 deletions

View File

@ -66,8 +66,6 @@ int build_fabric_bitstream(OpenfpgaContext& openfpga_ctx,
const Command& cmd, const CommandContext& cmd_context) {
CommandOptionId opt_verbose = cmd.option("verbose");
CommandOptionId opt_file = cmd.option("file");
CommandOptionId opt_file_format = cmd.option("format");
/* Build fabric bitstream here */
openfpga_ctx.mutable_fabric_bitstream() = build_fabric_dependent_bitstream(openfpga_ctx.bitstream_manager(),
@ -75,35 +73,51 @@ int build_fabric_bitstream(OpenfpgaContext& openfpga_ctx,
openfpga_ctx.arch().config_protocol,
cmd_context.option_enable(cmd, opt_verbose));
/* TODO: should identify the error code from internal function execution */
return CMD_EXEC_SUCCESS;
}
/********************************************************************
* A wrapper function to call the write_fabric_bitstream() in FPGA bitstream
*******************************************************************/
int write_fabric_bitstream(const OpenfpgaContext& openfpga_ctx,
const Command& cmd, const CommandContext& cmd_context) {
CommandOptionId opt_verbose = cmd.option("verbose");
CommandOptionId opt_file = cmd.option("file");
CommandOptionId opt_file_format = cmd.option("format");
/* Write fabric bitstream if required */
int status = CMD_EXEC_SUCCESS;
if (true == cmd_context.option_enable(cmd, opt_file)) {
std::string src_dir_path = find_path_dir_name(cmd_context.option_value(cmd, opt_file));
VTR_ASSERT(true == cmd_context.option_enable(cmd, opt_file));
/* Create directories */
create_directory(src_dir_path);
std::string src_dir_path = find_path_dir_name(cmd_context.option_value(cmd, opt_file));
/* Check file format requirements */
std::string file_format("plain_text");
if (true == cmd_context.option_enable(cmd, opt_file_format)) {
file_format = cmd_context.option_value(cmd, opt_file_format);
}
/* Create directories */
create_directory(src_dir_path);
if (std::string("xml") == file_format) {
status = write_fabric_bitstream_to_xml_file(openfpga_ctx.bitstream_manager(),
openfpga_ctx.fabric_bitstream(),
openfpga_ctx.arch().config_protocol,
cmd_context.option_value(cmd, opt_file));
} else {
/* By default, output in plain text format */
status = write_fabric_bitstream_to_text_file(openfpga_ctx.bitstream_manager(),
openfpga_ctx.fabric_bitstream(),
openfpga_ctx.arch().config_protocol,
cmd_context.option_value(cmd, opt_file));
}
/* Check file format requirements */
std::string file_format("plain_text");
if (true == cmd_context.option_enable(cmd, opt_file_format)) {
file_format = cmd_context.option_value(cmd, opt_file_format);
}
if (std::string("xml") == file_format) {
status = write_fabric_bitstream_to_xml_file(openfpga_ctx.bitstream_manager(),
openfpga_ctx.fabric_bitstream(),
openfpga_ctx.arch().config_protocol,
cmd_context.option_value(cmd, opt_file),
cmd_context.option_enable(cmd, opt_verbose));
} else {
/* By default, output in plain text format */
status = write_fabric_bitstream_to_text_file(openfpga_ctx.bitstream_manager(),
openfpga_ctx.fabric_bitstream(),
openfpga_ctx.arch().config_protocol,
cmd_context.option_value(cmd, opt_file),
cmd_context.option_enable(cmd, opt_verbose));
}
/* TODO: should identify the error code from internal function execution */
return status;
}

View File

@ -21,6 +21,9 @@ int fpga_bitstream(OpenfpgaContext& openfpga_ctx,
int build_fabric_bitstream(OpenfpgaContext& openfpga_ctx,
const Command& cmd, const CommandContext& cmd_context);
int write_fabric_bitstream(const OpenfpgaContext& openfpga_ctx,
const Command& cmd, const CommandContext& cmd_context);
} /* end namespace openfpga */
#endif

View File

@ -41,9 +41,9 @@ ShellCommandId add_openfpga_repack_command(openfpga::Shell<OpenfpgaContext>& she
* - Add command dependency
*******************************************************************/
static
ShellCommandId add_openfpga_arch_bitstream_command(openfpga::Shell<OpenfpgaContext>& shell,
const ShellCommandClassId& cmd_class_id,
const std::vector<ShellCommandId>& dependent_cmds) {
ShellCommandId add_openfpga_build_arch_bitstream_command(openfpga::Shell<OpenfpgaContext>& shell,
const ShellCommandClassId& cmd_class_id,
const std::vector<ShellCommandId>& dependent_cmds) {
Command shell_cmd("build_architecture_bitstream");
/* Add an option '--write_file' */
@ -75,13 +75,38 @@ ShellCommandId add_openfpga_arch_bitstream_command(openfpga::Shell<OpenfpgaConte
* - Add command dependency
*******************************************************************/
static
ShellCommandId add_openfpga_fabric_bitstream_command(openfpga::Shell<OpenfpgaContext>& shell,
const ShellCommandClassId& cmd_class_id,
const std::vector<ShellCommandId>& dependent_cmds) {
ShellCommandId add_openfpga_build_fabric_bitstream_command(openfpga::Shell<OpenfpgaContext>& shell,
const ShellCommandClassId& cmd_class_id,
const std::vector<ShellCommandId>& dependent_cmds) {
Command shell_cmd("build_fabric_bitstream");
/* Add an option '--verbose' */
shell_cmd.add_option("verbose", false, "Enable verbose output");
/* Add command 'fabric_bitstream' to the Shell */
ShellCommandId shell_cmd_id = shell.add_command(shell_cmd, "Reorganize the fabric-independent bitstream for the FPGA fabric created by FPGA-Verilog");
shell.set_command_class(shell_cmd_id, cmd_class_id);
shell.set_command_execute_function(shell_cmd_id, build_fabric_bitstream);
/* Add command dependency to the Shell */
shell.set_command_dependency(shell_cmd_id, dependent_cmds);
return shell_cmd_id;
}
/********************************************************************
* - Add a command to Shell environment: write_fabric_bitstream
* - Add associated options
* - Add command dependency
*******************************************************************/
static
ShellCommandId add_openfpga_write_fabric_bitstream_command(openfpga::Shell<OpenfpgaContext>& shell,
const ShellCommandClassId& cmd_class_id,
const std::vector<ShellCommandId>& dependent_cmds) {
Command shell_cmd("write_fabric_bitstream");
/* Add an option '--file' in short '-f'*/
CommandOptionId opt_file = shell_cmd.add_option("file", false, "file path to output the fabric bitstream to plain text file");
CommandOptionId opt_file = shell_cmd.add_option("file", true, "file path to output the fabric bitstream to plain text file");
shell_cmd.set_option_short_name(opt_file, "f");
shell_cmd.set_option_require_value(opt_file, openfpga::OPT_STRING);
@ -93,7 +118,7 @@ ShellCommandId add_openfpga_fabric_bitstream_command(openfpga::Shell<OpenfpgaCon
shell_cmd.add_option("verbose", false, "Enable verbose output");
/* Add command 'fabric_bitstream' to the Shell */
ShellCommandId shell_cmd_id = shell.add_command(shell_cmd, "Reorganize the fabric-independent bitstream for the FPGA fabric created by FPGA-Verilog");
ShellCommandId shell_cmd_id = shell.add_command(shell_cmd, "Write the fabric-dependent bitstream to a file");
shell.set_command_class(shell_cmd_id, cmd_class_id);
shell.set_command_execute_function(shell_cmd_id, build_fabric_bitstream);
@ -125,17 +150,25 @@ void add_openfpga_bitstream_commands(openfpga::Shell<OpenfpgaContext>& shell) {
* Command 'build_architecture_bitstream'
*/
/* The 'build_architecture_bitstream' command should NOT be executed before 'repack' */
std::vector<ShellCommandId> cmd_dependency_arch_bitstream;
cmd_dependency_arch_bitstream.push_back(shell_cmd_repack_id);
ShellCommandId shell_cmd_arch_bitstream_id = add_openfpga_arch_bitstream_command(shell, openfpga_bitstream_cmd_class, cmd_dependency_arch_bitstream);
std::vector<ShellCommandId> cmd_dependency_build_arch_bitstream;
cmd_dependency_build_arch_bitstream.push_back(shell_cmd_repack_id);
ShellCommandId shell_cmd_build_arch_bitstream_id = add_openfpga_build_arch_bitstream_command(shell, openfpga_bitstream_cmd_class, cmd_dependency_build_arch_bitstream);
/********************************
* Command 'build_fabric_bitstream'
*/
/* The 'build_fabric_bitstream' command should NOT be executed before 'build_architecture_bitstream' */
std::vector<ShellCommandId> cmd_dependency_fabric_bitstream;
cmd_dependency_fabric_bitstream.push_back(shell_cmd_arch_bitstream_id);
add_openfpga_fabric_bitstream_command(shell, openfpga_bitstream_cmd_class, cmd_dependency_fabric_bitstream);
std::vector<ShellCommandId> cmd_dependency_build_fabric_bitstream;
cmd_dependency_build_fabric_bitstream.push_back(shell_cmd_build_arch_bitstream_id);
ShellCommandId shell_cmd_build_fabric_bitstream_id = add_openfpga_build_fabric_bitstream_command(shell, openfpga_bitstream_cmd_class, cmd_dependency_build_fabric_bitstream);
/********************************
* Command 'write_fabric_bitstream'
*/
/* The 'write_fabric_bitstream' command should NOT be executed before 'build_fabric_bitstream' */
std::vector<ShellCommandId> cmd_dependency_write_fabric_bitstream;
cmd_dependency_write_fabric_bitstream.push_back(shell_cmd_build_fabric_bitstream_id);
add_openfpga_write_fabric_bitstream_command(shell, openfpga_bitstream_cmd_class, cmd_dependency_write_fabric_bitstream);
}
} /* end namespace openfpga */

View File

@ -95,7 +95,8 @@ int write_fabric_config_bit_to_text_file(std::fstream& fp,
int write_fabric_bitstream_to_text_file(const BitstreamManager& bitstream_manager,
const FabricBitstream& fabric_bitstream,
const ConfigProtocol& config_protocol,
const std::string& fname) {
const std::string& fname,
const bool& verbose) {
/* Ensure that we have a valid file name */
if (true == fname.empty()) {
VTR_LOG_ERROR("Received empty file name to output bitstream!\n\tPlease specify a valid file name.\n");
@ -127,6 +128,11 @@ int write_fabric_bitstream_to_text_file(const BitstreamManager& bitstream_manage
/* Close file handler */
fp.close();
VTR_LOGV(verbose,
"Outputted %lu configuration bits to plain text file: %s\n",
fabric_bitstream.bits().size(),
fname.c_str());
return status;
}

View File

@ -20,7 +20,8 @@ namespace openfpga {
int write_fabric_bitstream_to_text_file(const BitstreamManager& bitstream_manager,
const FabricBitstream& fabric_bitstream,
const ConfigProtocol& config_protocol,
const std::string& fname);
const std::string& fname,
const bool& verbose);
} /* end namespace openfpga */

View File

@ -156,7 +156,8 @@ int write_fabric_config_bit_to_xml_file(std::fstream& fp,
int write_fabric_bitstream_to_xml_file(const BitstreamManager& bitstream_manager,
const FabricBitstream& fabric_bitstream,
const ConfigProtocol& config_protocol,
const std::string& fname) {
const std::string& fname,
const bool& verbose) {
/* Ensure that we have a valid file name */
if (true == fname.empty()) {
VTR_LOG_ERROR("Received empty file name to output bitstream!\n\tPlease specify a valid file name.\n");
@ -191,6 +192,11 @@ int write_fabric_bitstream_to_xml_file(const BitstreamManager& bitstream_manager
/* Close file handler */
fp.close();
VTR_LOGV(verbose,
"Outputted %lu configuration bits to XML file: %s\n",
fabric_bitstream.bits().size(),
fname.c_str());
return status;
}

View File

@ -20,7 +20,8 @@ namespace openfpga {
int write_fabric_bitstream_to_xml_file(const BitstreamManager& bitstream_manager,
const FabricBitstream& fabric_bitstream,
const ConfigProtocol& config_protocol,
const std::string& fname);
const std::string& fname,
const bool& verbose);
} /* end namespace openfpga */