From a7947faa9d9e534f3e54d585b94b9b1af4be102d Mon Sep 17 00:00:00 2001 From: Jens Andersen Date: Sun, 24 Feb 2019 13:52:33 +0100 Subject: [PATCH 1/2] libtrellis: Allow overriding idcode on deserialise Signed-off-by: Jens Andersen --- libtrellis/include/Bitstream.hpp | 4 +++- libtrellis/src/Bitstream.cpp | 7 ++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/libtrellis/include/Bitstream.hpp b/libtrellis/include/Bitstream.hpp index 08b2d828..ab20abdb 100644 --- a/libtrellis/include/Bitstream.hpp +++ b/libtrellis/include/Bitstream.hpp @@ -8,6 +8,8 @@ #include #include #include +#include + using namespace std; namespace Trellis { @@ -47,7 +49,7 @@ class Bitstream { static Bitstream serialise_chip(const Chip &chip, const map options); // Deserialise a bitstream to a Chip - Chip deserialise_chip(); + Chip deserialise_chip(boost::optional idcode = boost::optional()); // Write a Lattice .bit file (metadata + bitstream) void write_bit(ostream &out); diff --git a/libtrellis/src/Bitstream.cpp b/libtrellis/src/Bitstream.cpp index d87186a8..0bc5bcfa 100644 --- a/libtrellis/src/Bitstream.cpp +++ b/libtrellis/src/Bitstream.cpp @@ -234,7 +234,7 @@ Bitstream Bitstream::read_bit(istream &in) { static const vector preamble = {0xFF, 0xFF, 0xBD, 0xB3}; -Chip Bitstream::deserialise_chip() { +Chip Bitstream::deserialise_chip(boost::optional idcode) { cerr << "bitstream size: " << data.size() * 8 << " bits" << endl; BitstreamReadWriter rd(data); boost::optional chip; @@ -256,6 +256,11 @@ Chip Bitstream::deserialise_chip() { case BitstreamCommand::VERIFY_ID: { rd.skip_bytes(3); uint32_t id = rd.get_uint32(); + if (idcode) { + BITSTREAM_NOTE("Overriding device ID from 0x" << hex << setw(8) << setfill('0') << id << " to 0x" << *idcode); + id = *idcode; + } + BITSTREAM_NOTE("device ID: 0x" << hex << setw(8) << setfill('0') << id); chip = boost::make_optional(Chip(id)); chip->metadata = metadata; From ef3fb4f2ae8fa253cbf6caae1e5baf07236e8257 Mon Sep 17 00:00:00 2001 From: Jens Andersen Date: Sun, 24 Feb 2019 13:53:01 +0100 Subject: [PATCH 2/2] libtrellis: Allow specifying idcode override in ecpunpack Signed-off-by: Jens Andersen --- libtrellis/tools/ecpunpack.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/libtrellis/tools/ecpunpack.cpp b/libtrellis/tools/ecpunpack.cpp index d76aba17..68ad3a89 100644 --- a/libtrellis/tools/ecpunpack.cpp +++ b/libtrellis/tools/ecpunpack.cpp @@ -3,6 +3,7 @@ #include "Chip.hpp" #include "Database.hpp" #include +#include #include #include #include @@ -14,6 +15,7 @@ int main(int argc, char *argv[]) { using namespace Trellis; namespace po = boost::program_options; + boost::optional idcode; std::string database_folder = TRELLIS_PREFIX "/share/trellis/database"; @@ -21,6 +23,7 @@ int main(int argc, char *argv[]) options.add_options()("help,h", "show help"); options.add_options()("verbose,v", "verbose output"); options.add_options()("db", po::value(), "Trellis database folder location"); + options.add_options()("idcode", po::value(), "IDCODE to override in bitstream"); po::positional_options_description pos; options.add_options()("input", po::value()->required(), "input bitstream file"); pos.add("input", 1); @@ -59,6 +62,17 @@ int main(int argc, char *argv[]) database_folder = vm["db"].as(); } + if (vm.count("idcode")) { + string idcode_str = vm["idcode"].as(); + uint32_t idcode_val; + idcode_val = uint32_t(strtoul(idcode_str.c_str(), nullptr, 0)); + if (idcode_val == 0) { + cerr << "Invalid idcode: " << idcode_str << endl; + return 1; + } + idcode = idcode_val; + } + try { load_database(database_folder); } catch (runtime_error &e) { @@ -67,7 +81,7 @@ int main(int argc, char *argv[]) } try { - Chip c = Bitstream::read_bit(bit_file).deserialise_chip(); + Chip c = Bitstream::read_bit(bit_file).deserialise_chip(idcode); ChipConfig cc = ChipConfig::from_chip(c); ofstream out_file(vm["textcfg"].as()); if (!out_file) {