Skip to content

Commit

Permalink
Merge pull request YosysHQ#70 from EnJens/ecpunpack-idcode-override
Browse files Browse the repository at this point in the history
Add support for overriding IDCode in ecpunpack
  • Loading branch information
gatecat committed Feb 27, 2019
2 parents dbd7b0d + ef3fb4f commit 9216745
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
4 changes: 3 additions & 1 deletion libtrellis/include/Bitstream.hpp
Expand Up @@ -8,6 +8,8 @@
#include <string>
#include <stdexcept>
#include <map>
#include <boost/optional.hpp>

using namespace std;

namespace Trellis {
Expand Down Expand Up @@ -47,7 +49,7 @@ class Bitstream {
static Bitstream serialise_chip(const Chip &chip, const map<string, string> options);

// Deserialise a bitstream to a Chip
Chip deserialise_chip();
Chip deserialise_chip(boost::optional<uint32_t> idcode = boost::optional<uint32_t>());

// Write a Lattice .bit file (metadata + bitstream)
void write_bit(ostream &out);
Expand Down
7 changes: 6 additions & 1 deletion libtrellis/src/Bitstream.cpp
Expand Up @@ -234,7 +234,7 @@ Bitstream Bitstream::read_bit(istream &in) {

static const vector<uint8_t> preamble = {0xFF, 0xFF, 0xBD, 0xB3};

Chip Bitstream::deserialise_chip() {
Chip Bitstream::deserialise_chip(boost::optional<uint32_t> idcode) {
cerr << "bitstream size: " << data.size() * 8 << " bits" << endl;
BitstreamReadWriter rd(data);
boost::optional<Chip> chip;
Expand All @@ -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;
Expand Down
16 changes: 15 additions & 1 deletion libtrellis/tools/ecpunpack.cpp
Expand Up @@ -3,6 +3,7 @@
#include "Chip.hpp"
#include "Database.hpp"
#include <iostream>
#include <boost/optional.hpp>
#include <boost/program_options.hpp>
#include <stdexcept>
#include <streambuf>
Expand All @@ -14,13 +15,15 @@ int main(int argc, char *argv[])
{
using namespace Trellis;
namespace po = boost::program_options;
boost::optional<uint32_t> idcode;

std::string database_folder = TRELLIS_PREFIX "/share/trellis/database";

po::options_description options("Allowed options");
options.add_options()("help,h", "show help");
options.add_options()("verbose,v", "verbose output");
options.add_options()("db", po::value<std::string>(), "Trellis database folder location");
options.add_options()("idcode", po::value<std::string>(), "IDCODE to override in bitstream");
po::positional_options_description pos;
options.add_options()("input", po::value<std::string>()->required(), "input bitstream file");
pos.add("input", 1);
Expand Down Expand Up @@ -59,6 +62,17 @@ int main(int argc, char *argv[])
database_folder = vm["db"].as<string>();
}

if (vm.count("idcode")) {
string idcode_str = vm["idcode"].as<string>();
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) {
Expand All @@ -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<string>());
if (!out_file) {
Expand Down

0 comments on commit 9216745

Please sign in to comment.