1 #include "Bitstream.hpp"
2 #include "Chip.hpp"
3 #include "Util.hpp"
4 #include <sstream>
5 #include <cstring>
6 #include <algorithm>
7 #include <iterator>
8 #include <iostream>
9 #include <boost/optional.hpp>
10 #include <iomanip>
11 #include <fstream>
12 #include <array>
13 
14 namespace Trellis {
15 
16 static const uint16_t CRC16_POLY = 0x8005;
17 static const uint16_t CRC16_INIT = 0x0000;
18 
19 static const vector<pair<std::string, uint8_t>> frequencies =
20     {{"2.4", 0x00},
21      {"4.8", 0x01},
22      {"9.7", 0x20},
23      {"19.4", 0x30},
24      {"38.8", 0x38},
25      {"62.0", 0x3b}};
26 
27 static const vector<pair<std::string, uint8_t>> spi_modes =
28     {{"fast-read", 0x49},
29      {"dual-spi", 0x51},
30      {"qspi", 0x59}};
31 
32 static const uint32_t multiboot_flag = 1 << 20;
33 
34 // The BitstreamReadWriter class stores state (including CRC16) whilst reading
35 // the bitstream
36 class BitstreamReadWriter {
37 public:
BitstreamReadWriter()38     BitstreamReadWriter() : data(), iter(data.begin()) {};
39 
BitstreamReadWriter(const vector<uint8_t> & data)40     BitstreamReadWriter(const vector<uint8_t> &data) : data(data), iter(this->data.begin()) {};
41 
42     vector<uint8_t> data;
43     vector<uint8_t>::iterator iter;
44 
45     uint16_t crc16 = CRC16_INIT;
46 
47     // Add a single byte to the running CRC16 accumulator
update_crc16(uint8_t val)48     void update_crc16(uint8_t val) {
49         int bit_flag;
50         for (int i = 7; i >= 0; i--) {
51             bit_flag = crc16 >> 15;
52 
53             /* Get next bit: */
54             crc16 <<= 1;
55             crc16 |= (val >> i) & 1; // item a) work from the least significant bits
56 
57             /* Cycle check: */
58             if (bit_flag)
59                 crc16 ^= CRC16_POLY;
60         }
61 
62     }
63 
64     // Return a single byte and update CRC
get_byte()65     inline uint8_t get_byte() {
66         assert(iter < data.end());
67         uint8_t val = *(iter++);
68         //cerr << hex << setw(2) << int(val) << endl;
69         update_crc16(val);
70         return val;
71     }
72 
73     // The command opcode is a byte so this works like get_byte
74     // but doesn't update the CRC if it's a dummy, because the docs
75     // says that dummy commands don't update the crc
get_command_opcode()76     inline BitstreamCommand get_command_opcode() {
77         assert(iter < data.end());
78         uint8_t val = *(iter++);
79         BitstreamCommand cmd = BitstreamCommand(val);
80         if (cmd != BitstreamCommand::DUMMY)
81             update_crc16(val);
82         return cmd;
83     }
84 
85     // Write a single byte and update CRC
write_byte(uint8_t b)86     inline void write_byte(uint8_t b) {
87         data.push_back(b);
88         update_crc16(b);
89     }
90 
91     // Copy multiple bytes into an OutputIterator and update CRC
92     template<typename T>
get_bytes(T out,size_t count)93     void get_bytes(T out, size_t count) {
94         for (size_t i = 0; i < count; i++) {
95             *out = get_byte();
96             ++out;
97         }
98     }
99 
100     // Decompress and copy multiple bytes into an OutputIterator and update CRC
101     template<typename T>
get_compressed_bytes(T out,size_t count,array<uint8_t,8> compression_dict)102     void get_compressed_bytes(T out, size_t count, array<uint8_t, 8> compression_dict) {
103         // Here we store data already read by read_byte(), it may be more than 1 byte at times!!
104         uint16_t read_data = 0;
105         size_t remaining_bits = 0;
106         bool next_bit;
107 
108         uint8_t udata;
109 
110         //
111         // Every byte can be encoded by on of 4 cases
112         // It's a prefix-free code so we can identify each one just by looking at the first bits:
113         // 0 -> Byte zero (0000 0000)
114         // 100 xxx -> Stored byte in compression_dict, xxx is the index (0-7)
115         // 101 xxx -> Byte with a single bit set, xxx is the index of the set bit (0 is lsb, 7 is msb)
116         // 11 xxxxxxxx -> Literal byte, xxxxxxxx is the encoded byte
117         //
118         for (size_t i = 0; i < count; i++) {
119             // Make sure we have at least one bit in the buffer
120             if (!remaining_bits) {
121                 read_data = (uint32_t) get_byte();
122                 remaining_bits = 8;
123             }
124             next_bit = bool(read_data >> (remaining_bits-1) & 1);
125             remaining_bits--;
126 
127             // Check the 4 cases leaving the uncompressed byte in udata
128             if (next_bit) {
129                 // Starts with 1, so check next bit/bits
130                 // For each of the 3 remaining cases we will need at least 5 more bits,
131                 // so if we have less than that it's ok to read another byte
132                 if (remaining_bits < 5) {
133                     read_data = (read_data << 8) | ((uint32_t) get_byte());
134                     remaining_bits += 8;
135                 }
136                 next_bit = bool(read_data >> (remaining_bits-1) & 1);
137                 remaining_bits--;
138 
139                 if (next_bit) {
140                     // 11 xxxx xxxx: Literal byte, just read the next 8 bits & use that
141                     // we consumed 10 bits total
142                     if (remaining_bits < 8) {
143                         read_data = (read_data << 8) | ((uint32_t) get_byte());
144                         remaining_bits += 8;
145                     }
146                     udata = uint8_t((read_data >> (remaining_bits - 8)) & 0xff);
147                     remaining_bits -= 8;
148                 } else {
149                     // Starts with 10, it could be a stored literal or a single-bit-set byte
150                     // 10 ? xxx: In both cases we need the index xxx, so extract it now
151                     // We already have all the bits we need buffered
152                     next_bit = bool(read_data >> (remaining_bits-1) & 1);
153                     remaining_bits--;
154                     size_t idx = (size_t) ((read_data >> (remaining_bits-3)) & 0x7);
155                     remaining_bits -= 3;
156                     if (next_bit) {
157                         // 101 xxx: Stored byte.  Just use xxx as index in the dictionary,
158                         // we consumed 6 bits
159                         udata = compression_dict[idx];
160                     } else {
161                         // 100 xxx: Single-bit-set byte, xxx is the index of the set bit
162                         // we consumed 6 bits
163                         udata = uint8_t(1 << idx);
164                     }
165                 }
166             } else {
167                 // 0: the uncompressed byte is zero
168                 // we consumed just one bit
169                 udata = 0;
170             }
171             *out = udata;
172             ++out;
173         }
174         // if remaining bits > 0 they are just padding bits added to the end so we can ignore them
175     }
176 
177     // Write multiple bytes from an InputIterator and update CRC
178     template<typename T>
write_bytes(T in,size_t count)179     void write_bytes(T in, size_t count) {
180         for (size_t i = 0; i < count; i++)
181             write_byte(*(in++));
182     }
183 
184     // Skip over bytes while updating CRC
skip_bytes(size_t count)185     void skip_bytes(size_t count) {
186         for (size_t i = 0; i < count; i++) get_byte();
187     }
188 
189     // Insert zeros while updating CRC
insert_zeros(size_t count)190     void insert_zeros(size_t count) {
191         for (size_t i = 0; i < count; i++) write_byte(0x00);
192     }
193 
194     // Insert dummy bytes into the bitstream, without updating CRC
insert_dummy(size_t count)195     void insert_dummy(size_t count) {
196         for (size_t i = 0; i < count; i++)
197             data.push_back(0xFF);
198     }
199 
200     // Read a big endian uint32 from the bitstream
get_uint32()201     uint32_t get_uint32() {
202         uint8_t tmp[4];
203         get_bytes(tmp, 4);
204         return (tmp[0] << 24UL) | (tmp[1] << 16UL) | (tmp[2] << 8UL) | (tmp[3]);
205     }
206 
207     // Write a big endian uint32_t into the bitstream
write_uint32(uint32_t val)208     void write_uint32(uint32_t val) {
209         write_byte(uint8_t((val >> 24UL) & 0xFF));
210         write_byte(uint8_t((val >> 16UL) & 0xFF));
211         write_byte(uint8_t((val >> 8UL) & 0xFF));
212         write_byte(uint8_t(val & 0xFF));
213     }
214 
215     // Search for a preamble, setting bitstream position to be after the preamble
216     // Returns true on success, false on failure
find_preamble(const vector<uint8_t> & preamble)217     bool find_preamble(const vector<uint8_t> &preamble) {
218         auto found = search(iter, data.end(), preamble.begin(), preamble.end());
219         if (found == data.end())
220             return false;
221         iter = found + preamble.size();
222         return true;
223     }
224 
finalise_crc16()225     uint16_t finalise_crc16() {
226         // item b) "push out" the last 16 bits
227         int i;
228         bool bit_flag;
229         for (i = 0; i < 16; ++i) {
230             bit_flag = bool(crc16 >> 15);
231             crc16 <<= 1;
232             if (bit_flag)
233                 crc16 ^= CRC16_POLY;
234         }
235 
236         return crc16;
237     }
238 
reset_crc16()239     void reset_crc16() {
240         crc16 = CRC16_INIT;
241     }
242 
243     // Get the offset into the bitstream
get_offset()244     size_t get_offset() {
245         return size_t(distance(data.begin(), iter));
246     }
247 
248     // Check the calculated CRC16 against an actual CRC16, expected in the next 2 bytes
check_crc16()249     void check_crc16() {
250         uint8_t crc_bytes[2];
251         uint16_t actual_crc = finalise_crc16();
252         get_bytes(crc_bytes, 2);
253         // cerr << hex << int(crc_bytes[0]) << " " << int(crc_bytes[1]) << endl;
254         uint16_t exp_crc = (crc_bytes[0] << 8) | crc_bytes[1];
255         if (actual_crc != exp_crc) {
256             ostringstream err;
257             err << "crc fail, calculated 0x" << hex << actual_crc << " but expecting 0x" << exp_crc;
258             throw BitstreamParseError(err.str(), get_offset());
259         }
260         reset_crc16();
261     }
262 
263     // Insert the calculated CRC16 into the bitstream, and then reset it
insert_crc16()264     void insert_crc16() {
265         uint16_t actual_crc = finalise_crc16();
266         write_byte(uint8_t((actual_crc >> 8) & 0xFF));
267         write_byte(uint8_t((actual_crc) & 0xFF));
268         reset_crc16();
269     }
270 
is_end()271     bool is_end() {
272         return (iter >= data.end());
273     }
274 
get()275     const vector<uint8_t> &get() {
276         return data;
277     };
278 };
279 
Bitstream(const vector<uint8_t> & data,const vector<string> & metadata)280 Bitstream::Bitstream(const vector<uint8_t> &data, const vector<string> &metadata) : data(data), metadata(metadata) {}
281 
read_bit(istream & in)282 Bitstream Bitstream::read_bit(istream &in) {
283     vector<uint8_t> bytes;
284     vector<string> meta;
285     auto hdr1 = uint8_t(in.get());
286     auto hdr2 = uint8_t(in.get());
287     if (hdr1 != 0xFF || hdr2 != 0x00) {
288         throw BitstreamParseError("Lattice .BIT files must start with 0xFF, 0x00", 0);
289     }
290     std::string temp;
291     uint8_t c;
292     while ((c = uint8_t(in.get())) != 0xFF) {
293         if (in.eof())
294             throw BitstreamParseError("Encountered end of file before start of bitstream data");
295         if (c == '\0') {
296             meta.push_back(temp);
297             temp = "";
298         } else {
299             temp += char(c);
300         }
301     }
302     in.seekg(0, in.end);
303     size_t length = in.tellg();
304     in.seekg(0, in.beg);
305     bytes.resize(length);
306     in.read(reinterpret_cast<char *>(&(bytes[0])), length);
307     return Bitstream(bytes, meta);
308 }
309 
310 // TODO: replace these macros with something more flexible
311 #define BITSTREAM_DEBUG(x) if (verbosity >= VerbosityLevel::DEBUG) cerr << "bitstream: " << x << endl
312 #define BITSTREAM_NOTE(x) if (verbosity >= VerbosityLevel::NOTE) cerr << "bitstream: " << x << endl
313 #define BITSTREAM_FATAL(x, pos) { ostringstream ss; ss << x; throw BitstreamParseError(ss.str(), pos); }
314 
315 static const vector<uint8_t> preamble = {0xFF, 0xFF, 0xBD, 0xB3};
316 
deserialise_chip()317 Chip Bitstream::deserialise_chip() {
318     return deserialise_chip(boost::none);
319 }
320 
deserialise_chip(boost::optional<uint32_t> idcode)321 Chip Bitstream::deserialise_chip(boost::optional<uint32_t> idcode) {
322     cerr << "bitstream size: " << data.size() * 8 << " bits" << endl;
323     BitstreamReadWriter rd(data);
324     boost::optional<Chip> chip;
325     bool found_preamble = rd.find_preamble(preamble);
326     boost::optional<array<uint8_t, 8>> compression_dict;
327 
328     if (!found_preamble)
329         throw BitstreamParseError("preamble not found in bitstream");
330 
331     uint16_t current_ebr = 0;
332     int addr_in_ebr = 0;
333 
334     while (!rd.is_end()) {
335         BitstreamCommand cmd = rd.get_command_opcode();
336         switch (cmd) {
337             case BitstreamCommand::LSC_RESET_CRC:
338                 BITSTREAM_DEBUG("reset crc");
339                 rd.skip_bytes(3);
340                 rd.reset_crc16();
341                 break;
342             case BitstreamCommand::VERIFY_ID: {
343                 rd.skip_bytes(3);
344                 uint32_t id = rd.get_uint32();
345                 if (idcode) {
346                     BITSTREAM_NOTE("Overriding device ID from 0x" << hex << setw(8) << setfill('0') << id << " to 0x" << *idcode);
347                     id = *idcode;
348                 }
349 
350                 BITSTREAM_NOTE("device ID: 0x" << hex << setw(8) << setfill('0') << id);
351                 chip = boost::make_optional(Chip(id));
352                 chip->metadata = metadata;
353             }
354                 break;
355             case BitstreamCommand::LSC_PROG_CNTRL0: {
356                 rd.skip_bytes(3);
357                 uint32_t cfg = rd.get_uint32();
358                 chip->ctrl0 = cfg;
359                 BITSTREAM_DEBUG("set control reg 0 to 0x" << hex << setw(8) << setfill('0') << cfg);
360             }
361                 break;
362             case BitstreamCommand::ISC_PROGRAM_DONE:
363                 rd.skip_bytes(3);
364                 BITSTREAM_NOTE("program DONE");
365                 break;
366             case BitstreamCommand::ISC_PROGRAM_SECURITY:
367                 rd.skip_bytes(3);
368                 BITSTREAM_NOTE("program SECURITY");
369                 break;
370             case BitstreamCommand::ISC_PROGRAM_USERCODE: {
371                 bool check_crc = (rd.get_byte() & 0x80) != 0;
372                 rd.skip_bytes(2);
373                 uint32_t uc = rd.get_uint32();
374                 BITSTREAM_NOTE("set USERCODE to 0x" << hex << setw(8) << setfill('0') << uc);
375                 chip->usercode = uc;
376                 if (check_crc)
377                     rd.check_crc16();
378             }
379                 break;
380             case BitstreamCommand::LSC_WRITE_COMP_DIC: {
381                 bool check_crc = (rd.get_byte() & 0x80) != 0;
382                 rd.skip_bytes(2);
383                 compression_dict = boost::make_optional(array<uint8_t, 8>());
384                 // patterns are stored in the bitstream in reverse order: pattern7 to pattern0
385                 for (int i = 7; i >= 0; i--) {
386                   uint8_t pattern = rd.get_byte();
387                   compression_dict.get()[i] = pattern;
388                 }
389                 BITSTREAM_DEBUG("write compression dictionary: " <<
390                                 "0x" << hex << setw(2) << setfill('0') << int(compression_dict.get()[0]) << " " <<
391                                 "0x" << hex << setw(2) << setfill('0') << int(compression_dict.get()[1]) << " " <<
392                                 "0x" << hex << setw(2) << setfill('0') << int(compression_dict.get()[2]) << " " <<
393                                 "0x" << hex << setw(2) << setfill('0') << int(compression_dict.get()[3]) << " " <<
394                                 "0x" << hex << setw(2) << setfill('0') << int(compression_dict.get()[4]) << " " <<
395                                 "0x" << hex << setw(2) << setfill('0') << int(compression_dict.get()[5]) << " " <<
396                                 "0x" << hex << setw(2) << setfill('0') << int(compression_dict.get()[6]) << " " <<
397                                 "0x" << hex << setw(2) << setfill('0') << int(compression_dict.get()[7]));;
398                 if (check_crc)
399                   rd.check_crc16();
400             }
401                 break;
402             case BitstreamCommand::LSC_INIT_ADDRESS:
403                 rd.skip_bytes(3);
404                 BITSTREAM_DEBUG("init address");
405                 break;
406             case BitstreamCommand::LSC_PROG_INCR_CMP:
407                 // This is the main bitstream payload (compressed)
408                 BITSTREAM_DEBUG("Compressed bitstream found");
409                 if (!compression_dict)
410                     throw BitstreamParseError("start of compressed bitstream data before compression dictionary was stored", rd.get_offset());
411                 // fall through
412             case BitstreamCommand::LSC_PROG_INCR_RTI: {
413                 // This is the main bitstream payload
414                 if (!chip)
415                     throw BitstreamParseError("start of bitstream data before chip was identified", rd.get_offset());
416                 bool reversed_frames;
417                 if (chip->info.family == "MachXO2")
418                     reversed_frames = false;
419                 else if (chip->info.family == "ECP5")
420                     reversed_frames = true;
421                 else
422                     throw BitstreamParseError("Unknown chip family: " + chip->info.family);
423 
424                 uint8_t params[3];
425                 rd.get_bytes(params, 3);
426                 BITSTREAM_DEBUG("settings: " << hex << setw(2) << int(params[0]) << " " << int(params[1]) << " "
427                                              << int(params[2]));
428                 // I've only seen 0x81 for the ecp5 and 0x8e for the xo2 so far...
429                 bool check_crc = params[0] & 0x80U;
430                 // inverted value: a 0 means check after every frame
431                 bool crc_after_each_frame = check_crc && !(params[0] & 0x40U);
432                 // I don't know what these two are for I've seen both 1s (XO2) and both 0s (ECP5)
433                 // The names are from the ECP5 docs
434                 // bool include_dummy_bits = params[0] & 0x20U;
435                 // bool include_dummy_bytes = params[0] & 0x10U;
436                 size_t dummy_bytes = params[0] & 0x0FU;
437                 size_t frame_count = (params[1] << 8U) | params[2];
438                 BITSTREAM_NOTE("reading " << std::dec << frame_count << " config frames (with " << std::dec << dummy_bytes << " dummy bytes)");
439                 size_t bytes_per_frame = (chip->info.bits_per_frame + chip->info.pad_bits_after_frame +
440                                           chip->info.pad_bits_before_frame) / 8U;
441                 // If compressed 0 bits are added to the stream before compression to make it 64 bit bounded, so
442                 // we should consider that space here but they shouldn't be copied to the output
443                 if (cmd == BitstreamCommand::LSC_PROG_INCR_CMP)
444                     bytes_per_frame += (7 - ((bytes_per_frame - 1) % 8));
445                 unique_ptr<uint8_t[]> frame_bytes = make_unique<uint8_t[]>(bytes_per_frame);
446                 for (size_t i = 0; i < frame_count; i++) {
447                     size_t idx = reversed_frames? (chip->info.num_frames - 1) - i : i;
448                     if (cmd == BitstreamCommand::LSC_PROG_INCR_CMP)
449                         rd.get_compressed_bytes(frame_bytes.get(), bytes_per_frame, compression_dict.get());
450                     else
451                         rd.get_bytes(frame_bytes.get(), bytes_per_frame);
452 
453                     for (int j = 0; j < chip->info.bits_per_frame; j++) {
454                         size_t ofs = j + chip->info.pad_bits_after_frame;
455                         chip->cram.bit(idx, j) = (char)
456                             ((frame_bytes[(bytes_per_frame - 1) - (ofs / 8)] >> (ofs % 8)) & 0x01);
457                     }
458                     if (crc_after_each_frame || (check_crc && (i == frame_count-1)))
459                       rd.check_crc16();
460                     rd.skip_bytes(dummy_bytes);
461                 }
462             }
463                 break;
464             case BitstreamCommand::LSC_EBR_ADDRESS: {
465                 rd.skip_bytes(3);
466                 uint32_t data = rd.get_uint32();
467                 current_ebr = (data >> 11) & 0x3FF;
468                 addr_in_ebr = data & 0x7FF;
469                 chip->bram_data[current_ebr].resize(2048);
470             }
471                 break;
472             case BitstreamCommand::LSC_EBR_WRITE: {
473                 uint8_t params[3];
474                 rd.get_bytes(params, 3);
475                 int frame_count = (params[1] << 8U) | params[2];
476                 int frames_read = 0;
477 
478                 while (frames_read < frame_count) {
479 
480                     if (addr_in_ebr >= 2048) {
481                         addr_in_ebr = 0;
482                         current_ebr++;
483                         chip->bram_data[current_ebr].resize(2048);
484                     }
485 
486                     auto &ebr = chip->bram_data[current_ebr];
487                     frames_read++;
488                     uint8_t frame[9];
489                     rd.get_bytes(frame, 9);
490                     ebr.at(addr_in_ebr+0) = (frame[0] << 1)        | (frame[1] >> 7);
491                     ebr.at(addr_in_ebr+1) = (frame[1] & 0x7F) << 2 | (frame[2] >> 6);
492                     ebr.at(addr_in_ebr+2) = (frame[2] & 0x3F) << 3 | (frame[3] >> 5);
493                     ebr.at(addr_in_ebr+3) = (frame[3] & 0x1F) << 4 | (frame[4] >> 4);
494                     ebr.at(addr_in_ebr+4) = (frame[4] & 0x0F) << 5 | (frame[5] >> 3);
495                     ebr.at(addr_in_ebr+5) = (frame[5] & 0x07) << 6 | (frame[6] >> 2);
496                     ebr.at(addr_in_ebr+6) = (frame[6] & 0x03) << 7 | (frame[7] >> 1);
497                     ebr.at(addr_in_ebr+7) = (frame[7] & 0x01) << 8 | frame[8];
498                     addr_in_ebr += 8;
499 
500                 }
501                 rd.check_crc16();
502             }
503                 break;
504             case BitstreamCommand::SPI_MODE: {
505                 uint8_t spi_mode;
506                 rd.get_bytes(&spi_mode, 1);
507                 rd.skip_bytes(2);
508 
509                 auto spimode = find_if(spi_modes.begin(), spi_modes.end(), [&](const pair<string, uint8_t> &fp){
510                     return fp.second == spi_mode;
511                 });
512                 if (spimode == spi_modes.end())
513                     throw runtime_error("bad SPI mode" + std::to_string(spi_mode));
514 
515                 BITSTREAM_NOTE("SPI Mode " <<  spimode->first);
516             }
517                 break;
518             case BitstreamCommand::JUMP:
519                 rd.skip_bytes(3);
520                 BITSTREAM_DEBUG("Jump command");
521                 /* TODO: Parse address and SPI Flash read speed */
522                 rd.skip_bytes(4);
523                 break;
524             case BitstreamCommand::DUMMY:
525                 break;
526             default: BITSTREAM_FATAL("unsupported command 0x" << hex << setw(2) << setfill('0') << int(cmd),
527                                      rd.get_offset());
528         }
529     }
530     if (chip) {
531         return *chip;
532     } else {
533         throw BitstreamParseError("failed to parse bitstream, no valid payload found");
534     }
535 }
536 
generate_jump(uint32_t address)537 Bitstream Bitstream::generate_jump(uint32_t address) {
538     BitstreamReadWriter wr;
539 
540     // Dummy bytes
541     wr.insert_dummy(16);
542     // Preamble
543     wr.write_bytes(preamble.begin(), preamble.size());
544 
545     // Padding
546     wr.insert_dummy(4);
547 
548     // Dummy control register
549     wr.write_byte(uint8_t(BitstreamCommand::LSC_PROG_CNTRL0));
550     wr.insert_zeros(3);
551     wr.insert_zeros(4);
552 
553     // Jump command
554     wr.write_byte(uint8_t(BitstreamCommand::JUMP));
555     wr.insert_zeros(3);
556     wr.write_byte(0x03); // TODO: Allow specifying SPI Flash read speed
557 
558     wr.write_byte(uint8_t((address >> 16UL) & 0xFF));
559     wr.write_byte(uint8_t((address >> 8UL) & 0xFF));
560     wr.write_byte(uint8_t(address & 0xFF));
561 
562     wr.insert_dummy(18);
563 
564     return Bitstream(wr.get(), std::vector<string>());
565 }
566 
serialise_chip(const Chip & chip,const map<string,string> options)567 Bitstream Bitstream::serialise_chip(const Chip &chip, const map<string, string> options) {
568     BitstreamReadWriter wr;
569     // Preamble
570     wr.write_bytes(preamble.begin(), preamble.size());
571     // Padding
572     wr.insert_dummy(4);
573 
574     if (options.count("spimode")) {
575         auto spimode = find_if(spi_modes.begin(), spi_modes.end(), [&](const pair<string, uint8_t> &fp){
576             return fp.first == options.at("spimode");
577         });
578         if (spimode == spi_modes.end())
579             throw runtime_error("bad spimode option " + options.at("spimode"));
580 
581         wr.write_byte(uint8_t(BitstreamCommand::SPI_MODE));
582         wr.write_byte(uint8_t(spimode->second));
583         wr.insert_zeros(2);
584     }
585 
586     // Reset CRC
587     wr.write_byte(uint8_t(BitstreamCommand::LSC_RESET_CRC));
588     wr.insert_zeros(3);
589     wr.reset_crc16();
590     // Verify ID
591     wr.write_byte(uint8_t(BitstreamCommand::VERIFY_ID));
592     wr.insert_zeros(3);
593     wr.write_uint32(chip.info.idcode);
594     // Set control reg 0 to 0x40000000
595     wr.write_byte(uint8_t(BitstreamCommand::LSC_PROG_CNTRL0));
596     wr.insert_zeros(3);
597     uint32_t ctrl0  = chip.ctrl0;
598     if (options.count("freq")) {
599         auto freq = find_if(frequencies.begin(), frequencies.end(), [&](const pair<string, uint8_t> &fp){
600             return fp.first == options.at("freq");
601         });
602         if (freq == frequencies.end())
603             throw runtime_error("bad frequency option " + options.at("freq"));
604         ctrl0 |= freq->second;
605     }
606     if (options.count("multiboot")) {
607         if (options.at("multiboot") == "yes")
608             ctrl0 |= multiboot_flag;
609         else
610             ctrl0 &= ~multiboot_flag;
611     }
612     wr.write_uint32(ctrl0);
613     // Init address
614     wr.write_byte(uint8_t(BitstreamCommand::LSC_INIT_ADDRESS));
615     wr.insert_zeros(3);
616     // Bitstream data
617     wr.write_byte(uint8_t(BitstreamCommand::LSC_PROG_INCR_RTI));
618     wr.write_byte(0x91); //CRC check, 1 dummy byte
619     uint16_t frames = uint16_t(chip.info.num_frames);
620     wr.write_byte(uint8_t((frames >> 8) & 0xFF));
621     wr.write_byte(uint8_t(frames & 0xFF));
622     size_t bytes_per_frame = (chip.info.bits_per_frame + chip.info.pad_bits_after_frame +
623                               chip.info.pad_bits_before_frame) / 8U;
624     unique_ptr<uint8_t[]> frame_bytes = make_unique<uint8_t[]>(bytes_per_frame);
625     for (size_t i = 0; i < frames; i++) {
626         fill(frame_bytes.get(), frame_bytes.get() + bytes_per_frame, 0x00);
627         for (int j = 0; j < chip.info.bits_per_frame; j++) {
628             size_t ofs = j + chip.info.pad_bits_after_frame;
629             assert(((bytes_per_frame - 1) - (ofs / 8)) < bytes_per_frame);
630             frame_bytes[(bytes_per_frame - 1) - (ofs / 8)] |=
631                     (chip.cram.bit((chip.info.num_frames - 1) - i, j) & 0x01) << (ofs % 8);
632         }
633         wr.write_bytes(frame_bytes.get(), bytes_per_frame);
634         wr.insert_crc16();
635         wr.write_byte(0xFF);
636     }
637     // Post-bitstream space for SECURITY and SED (not used here)
638     wr.insert_dummy(12);
639     // Program Usercode
640     wr.write_byte(uint8_t(BitstreamCommand::ISC_PROGRAM_USERCODE));
641     wr.write_byte(0x80);
642     wr.insert_zeros(2);
643     wr.write_uint32(chip.usercode);
644     wr.insert_crc16();
645     for (const auto &ebr : chip.bram_data) {
646         // BlockRAM initialisation
647 
648         // Set EBR address
649         wr.write_byte(uint8_t(BitstreamCommand::LSC_EBR_ADDRESS));
650         wr.insert_zeros(3);
651         wr.write_uint32(ebr.first << 11UL);
652 
653         // Write EBR data
654         wr.write_byte(uint8_t(BitstreamCommand::LSC_EBR_WRITE));
655         wr.write_byte(0xD0); // Dummy/CRC config
656         wr.write_byte(0x01); // 0x0100 = 256x 72-bit frames
657         wr.write_byte(0x00);
658 
659         uint8_t frame[9];
660         const auto &data = ebr.second;
661         for (int addr_in_ebr = 0; addr_in_ebr < 2048; addr_in_ebr+=8) {
662             frame[0] = data.at(addr_in_ebr+0) >> 1;
663             frame[1] = (data.at(addr_in_ebr+0) & 0x01) << 7 | (data.at(addr_in_ebr+1) >> 2);
664             frame[2] = (data.at(addr_in_ebr+1) & 0x03) << 6 | (data.at(addr_in_ebr+2) >> 3);
665             frame[3] = (data.at(addr_in_ebr+2) & 0x07) << 5 | (data.at(addr_in_ebr+3) >> 4);
666             frame[4] = (data.at(addr_in_ebr+3) & 0x0F) << 4 | (data.at(addr_in_ebr+4) >> 5);
667             frame[5] = (data.at(addr_in_ebr+4) & 0x1F) << 3 | (data.at(addr_in_ebr+5) >> 6);
668             frame[6] = (data.at(addr_in_ebr+5) & 0x3F) << 2 | (data.at(addr_in_ebr+6) >> 7);
669             frame[7] = (data.at(addr_in_ebr+6) & 0x7F) << 1 | (data.at(addr_in_ebr+7) >> 8);
670             frame[8] = data.at(addr_in_ebr+7);
671             wr.write_bytes(frame, 9);
672         }
673         wr.insert_crc16();
674     }
675     // Program DONE
676     wr.write_byte(uint8_t(BitstreamCommand::ISC_PROGRAM_DONE));
677     wr.insert_zeros(3);
678     // Trailing padding
679     wr.insert_dummy(4);
680     return Bitstream(wr.get(), chip.metadata);
681 }
682 
write_bit(ostream & out)683 void Bitstream::write_bit(ostream &out) {
684     // Write metadata header
685     out.put(char(0xFF));
686     out.put(0x00);
687     for (const auto &str : metadata) {
688         out << str;
689         out.put(0x00);
690     }
691     out.put(char(0xFF));
692     // Dump raw bitstream
693     out.write(reinterpret_cast<const char *>(&(data[0])), data.size());
694 }
695 
get_bytes()696 vector<uint8_t> Bitstream::get_bytes() {
697     vector<uint8_t> bytes;
698     bytes.push_back(0xFF);
699     bytes.push_back(0x00);
700     for (const auto &str : metadata) {
701         copy(str.begin(), str.end(), back_inserter(bytes));
702         bytes.push_back(0x00);
703     }
704     bytes.push_back(0xFF);
705     copy(data.begin(), data.end(), back_inserter(bytes));
706     return bytes;
707 }
708 
write_bin(ostream & out)709 void Bitstream::write_bin(ostream &out) {
710     out.write(reinterpret_cast<const char *>(&(data[0])), data.size());
711 }
712 
read_bit_py(string file)713 Bitstream Bitstream::read_bit_py(string file) {
714     ifstream inf(file, ios::binary);
715     if (!inf)
716         throw runtime_error("failed to open input file " + file);
717     return read_bit(inf);
718 }
719 
write_bit_py(string file)720 void Bitstream::write_bit_py(string file) {
721     ofstream ouf(file, ios::binary);
722     if (!ouf)
723         throw runtime_error("failed to open output file " + file);
724     write_bit(ouf);
725 }
726 
BitstreamParseError(const string & desc)727 BitstreamParseError::BitstreamParseError(const string &desc) : runtime_error(desc.c_str()), desc(desc), offset(-1) {}
728 
BitstreamParseError(const string & desc,size_t offset)729 BitstreamParseError::BitstreamParseError(const string &desc, size_t offset) : runtime_error(desc.c_str()), desc(desc),
730                                                                               offset(int(offset)) {}
731 
what() const732 const char *BitstreamParseError::what() const noexcept {
733     ostringstream ss;
734     ss << "Bitstream Parse Error: ";
735     ss << desc;
736     if (offset != -1)
737         ss << " [at 0x" << hex << offset << "]";
738     return strdup(ss.str().c_str());
739 }
740 
741 }
742