Skip to content

Commit 9216745

Browse files
authored
Merge pull request #70 from EnJens/ecpunpack-idcode-override
Add support for overriding IDCode in ecpunpack
2 parents dbd7b0d + ef3fb4f commit 9216745

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

libtrellis/include/Bitstream.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <string>
99
#include <stdexcept>
1010
#include <map>
11+
#include <boost/optional.hpp>
12+
1113
using namespace std;
1214

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

4951
// Deserialise a bitstream to a Chip
50-
Chip deserialise_chip();
52+
Chip deserialise_chip(boost::optional<uint32_t> idcode = boost::optional<uint32_t>());
5153

5254
// Write a Lattice .bit file (metadata + bitstream)
5355
void write_bit(ostream &out);

libtrellis/src/Bitstream.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ Bitstream Bitstream::read_bit(istream &in) {
234234

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

237-
Chip Bitstream::deserialise_chip() {
237+
Chip Bitstream::deserialise_chip(boost::optional<uint32_t> idcode) {
238238
cerr << "bitstream size: " << data.size() * 8 << " bits" << endl;
239239
BitstreamReadWriter rd(data);
240240
boost::optional<Chip> chip;
@@ -256,6 +256,11 @@ Chip Bitstream::deserialise_chip() {
256256
case BitstreamCommand::VERIFY_ID: {
257257
rd.skip_bytes(3);
258258
uint32_t id = rd.get_uint32();
259+
if (idcode) {
260+
BITSTREAM_NOTE("Overriding device ID from 0x" << hex << setw(8) << setfill('0') << id << " to 0x" << *idcode);
261+
id = *idcode;
262+
}
263+
259264
BITSTREAM_NOTE("device ID: 0x" << hex << setw(8) << setfill('0') << id);
260265
chip = boost::make_optional(Chip(id));
261266
chip->metadata = metadata;

libtrellis/tools/ecpunpack.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "Chip.hpp"
44
#include "Database.hpp"
55
#include <iostream>
6+
#include <boost/optional.hpp>
67
#include <boost/program_options.hpp>
78
#include <stdexcept>
89
#include <streambuf>
@@ -14,13 +15,15 @@ int main(int argc, char *argv[])
1415
{
1516
using namespace Trellis;
1617
namespace po = boost::program_options;
18+
boost::optional<uint32_t> idcode;
1719

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

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

65+
if (vm.count("idcode")) {
66+
string idcode_str = vm["idcode"].as<string>();
67+
uint32_t idcode_val;
68+
idcode_val = uint32_t(strtoul(idcode_str.c_str(), nullptr, 0));
69+
if (idcode_val == 0) {
70+
cerr << "Invalid idcode: " << idcode_str << endl;
71+
return 1;
72+
}
73+
idcode = idcode_val;
74+
}
75+
6276
try {
6377
load_database(database_folder);
6478
} catch (runtime_error &e) {
@@ -67,7 +81,7 @@ int main(int argc, char *argv[])
6781
}
6882

6983
try {
70-
Chip c = Bitstream::read_bit(bit_file).deserialise_chip();
84+
Chip c = Bitstream::read_bit(bit_file).deserialise_chip(idcode);
7185
ChipConfig cc = ChipConfig::from_chip(c);
7286
ofstream out_file(vm["textcfg"].as<string>());
7387
if (!out_file) {

0 commit comments

Comments
 (0)