From d325bede6816bee63dba32d60ff8318e5d50074d Mon Sep 17 00:00:00 2001 From: tangxifan Date: Tue, 21 Apr 2020 12:02:10 -0600 Subject: [PATCH] add fabric bitstream writer --- openfpga/src/base/openfpga_bitstream.cpp | 17 +++++- .../src/base/openfpga_bitstream_command.cpp | 5 ++ ...m_writer.cpp => arch_bitstream_writer.cpp} | 2 +- ...tream_writer.h => arch_bitstream_writer.h} | 4 +- .../fabric_bitstream_writer.cpp | 61 +++++++++++++++++++ .../fpga_bitstream/fabric_bitstream_writer.h | 24 ++++++++ openfpga/test_script/and_k6_frac.openfpga | 2 +- 7 files changed, 110 insertions(+), 5 deletions(-) rename openfpga/src/fpga_bitstream/{bitstream_writer.cpp => arch_bitstream_writer.cpp} (99%) rename openfpga/src/fpga_bitstream/{bitstream_writer.h => arch_bitstream_writer.h} (91%) create mode 100644 openfpga/src/fpga_bitstream/fabric_bitstream_writer.cpp create mode 100644 openfpga/src/fpga_bitstream/fabric_bitstream_writer.h diff --git a/openfpga/src/base/openfpga_bitstream.cpp b/openfpga/src/base/openfpga_bitstream.cpp index 7e2a19fc4..e131a2bfc 100644 --- a/openfpga/src/base/openfpga_bitstream.cpp +++ b/openfpga/src/base/openfpga_bitstream.cpp @@ -12,7 +12,8 @@ #include "openfpga_digest.h" #include "build_device_bitstream.h" -#include "bitstream_writer.h" +#include "arch_bitstream_writer.h" +#include "fabric_bitstream_writer.h" #include "build_fabric_bitstream.h" #include "openfpga_bitstream.h" @@ -56,10 +57,24 @@ 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"); + /* Build fabric bitstream here */ openfpga_ctx.mutable_fabric_bitstream() = build_fabric_dependent_bitstream(openfpga_ctx.bitstream_manager(), openfpga_ctx.module_graph(), cmd_context.option_enable(cmd, opt_verbose)); + + /* Write fabric bitstream if required */ + 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)); + + /* Create directories */ + create_directory(src_dir_path); + + write_fabric_bitstream_to_text_file(openfpga_ctx.bitstream_manager(), + openfpga_ctx.fabric_bitstream(), + cmd_context.option_value(cmd, opt_file)); + } /* TODO: should identify the error code from internal function execution */ return CMD_EXEC_SUCCESS; diff --git a/openfpga/src/base/openfpga_bitstream_command.cpp b/openfpga/src/base/openfpga_bitstream_command.cpp index b64696d79..70149dd43 100644 --- a/openfpga/src/base/openfpga_bitstream_command.cpp +++ b/openfpga/src/base/openfpga_bitstream_command.cpp @@ -76,6 +76,11 @@ ShellCommandId add_openfpga_fabric_bitstream_command(openfpga::Shell& dependent_cmds) { Command shell_cmd("build_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"); + shell_cmd.set_option_short_name(opt_file, "f"); + shell_cmd.set_option_require_value(opt_file, openfpga::OPT_STRING); + /* Add an option '--verbose' */ shell_cmd.add_option("verbose", false, "Enable verbose output"); diff --git a/openfpga/src/fpga_bitstream/bitstream_writer.cpp b/openfpga/src/fpga_bitstream/arch_bitstream_writer.cpp similarity index 99% rename from openfpga/src/fpga_bitstream/bitstream_writer.cpp rename to openfpga/src/fpga_bitstream/arch_bitstream_writer.cpp index 6f8539d3f..6a87a6768 100644 --- a/openfpga/src/fpga_bitstream/bitstream_writer.cpp +++ b/openfpga/src/fpga_bitstream/arch_bitstream_writer.cpp @@ -17,7 +17,7 @@ #include "openfpga_naming.h" #include "bitstream_manager_utils.h" -#include "bitstream_writer.h" +#include "arch_bitstream_writer.h" /* begin namespace openfpga */ namespace openfpga { diff --git a/openfpga/src/fpga_bitstream/bitstream_writer.h b/openfpga/src/fpga_bitstream/arch_bitstream_writer.h similarity index 91% rename from openfpga/src/fpga_bitstream/bitstream_writer.h rename to openfpga/src/fpga_bitstream/arch_bitstream_writer.h index 338bfde35..a66880735 100644 --- a/openfpga/src/fpga_bitstream/bitstream_writer.h +++ b/openfpga/src/fpga_bitstream/arch_bitstream_writer.h @@ -1,5 +1,5 @@ -#ifndef BITSTREAM_WRITER_H -#define BITSTREAM_WRITER_H +#ifndef ARCH_BITSTREAM_WRITER_H +#define ARCH_BITSTREAM_WRITER_H /******************************************************************** * Include header files that are required by function declaration diff --git a/openfpga/src/fpga_bitstream/fabric_bitstream_writer.cpp b/openfpga/src/fpga_bitstream/fabric_bitstream_writer.cpp new file mode 100644 index 000000000..069a99345 --- /dev/null +++ b/openfpga/src/fpga_bitstream/fabric_bitstream_writer.cpp @@ -0,0 +1,61 @@ +/******************************************************************** + * This file includes functions that output a fabric-dependent + * bitstream database to files in different formats + *******************************************************************/ +#include +#include +#include + +/* Headers from vtrutil library */ +#include "vtr_assert.h" +#include "vtr_log.h" +#include "vtr_time.h" + +/* Headers from openfpgautil library */ +#include "openfpga_digest.h" + +#include "openfpga_naming.h" + +#include "bitstream_manager_utils.h" +#include "fabric_bitstream_writer.h" + +/* begin namespace openfpga */ +namespace openfpga { + +/******************************************************************** + * Write the fabric bitstream to a plain text file + * Notes: + * - This is the final bitstream which is loadable to the FPGA fabric + * (Verilog netlists etc.) + * - Do NOT include any comments or other characters that the 0|1 bitstream content + * in this file + *******************************************************************/ +void write_fabric_bitstream_to_text_file(const BitstreamManager& bitstream_manager, + const std::vector& fabric_bitstream, + const std::string& fname) { + /* 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"); + } + + std::string timer_message = std::string("Write ") + std::to_string(fabric_bitstream.size()) + std::string(" fabric bitstream into plain text file '") + fname + std::string("'"); + vtr::ScopedStartFinishTimer timer(timer_message); + + /* Create the file stream */ + std::fstream fp; + fp.open(fname, std::fstream::out | std::fstream::trunc); + + check_file_stream(fname.c_str(), fp); + + /* Put down pure 0|1 bitstream here */ + for (const ConfigBitId& fabric_bit : fabric_bitstream) { + fp << bitstream_manager.bit_value(fabric_bit); + } + /* Print an end to the file here */ + fp << std::endl; + + /* Close file handler */ + fp.close(); +} + +} /* end namespace openfpga */ diff --git a/openfpga/src/fpga_bitstream/fabric_bitstream_writer.h b/openfpga/src/fpga_bitstream/fabric_bitstream_writer.h new file mode 100644 index 000000000..6b2c0e771 --- /dev/null +++ b/openfpga/src/fpga_bitstream/fabric_bitstream_writer.h @@ -0,0 +1,24 @@ +#ifndef FABRIC_BITSTREAM_WRITER_H +#define FABRIC_BITSTREAM_WRITER_H + +/******************************************************************** + * Include header files that are required by function declaration + *******************************************************************/ +#include +#include +#include "bitstream_manager.h" + +/******************************************************************** + * Function declaration + *******************************************************************/ + +/* begin namespace openfpga */ +namespace openfpga { + +void write_fabric_bitstream_to_text_file(const BitstreamManager& bitstream_manager, + const std::vector& fabric_bitstream, + const std::string& fname); + +} /* end namespace openfpga */ + +#endif diff --git a/openfpga/test_script/and_k6_frac.openfpga b/openfpga/test_script/and_k6_frac.openfpga index e14811f49..20a75a661 100644 --- a/openfpga/test_script/and_k6_frac.openfpga +++ b/openfpga/test_script/and_k6_frac.openfpga @@ -34,7 +34,7 @@ repack #--verbose build_architecture_bitstream --verbose --file /var/tmp/xtang/openfpga_test_src/fabric_indepenent_bitstream.xml # Build fabric-dependent bitstream -build_fabric_bitstream --verbose +build_fabric_bitstream --verbose --file /var/tmp/xtang/openfpga_test_src/and.bitstream # Write the Verilog netlist for FPGA fabric # - Enable the use of explicit port mapping in Verilog netlist