From e046ad3a4f634ef58a84d803c0b4de71cfbe9f2a Mon Sep 17 00:00:00 2001 From: Marcus Comstedt Date: Fri, 21 Oct 2016 22:19:16 +0200 Subject: Improve input parsing of icebram Add support for the following in the input hexfiles: * Horizontal whitespace (space, tab, cr) * Multiple words on the same line * Empty lines * Embedded underscores (_) in the words * x and z nibbles (interpreted as zero) In addition, allow for the to_hexfile to be shorter than the from_hexfile, padding with zeroes as needed. --- icebram/icebram.cc | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) (limited to 'icebram') diff --git a/icebram/icebram.cc b/icebram/icebram.cc index fbb237b..245a0ab 100644 --- a/icebram/icebram.cc +++ b/icebram/icebram.cc @@ -43,6 +43,18 @@ uint64_t xorshift64star(void) { return x * UINT64_C(2685821657736338717); } +void push_back_bitvector(vector> &hexfile, const vector &digits) +{ + if (digits.empty()) + return; + + hexfile.push_back(vector(digits.size() * 4)); + + for (int i = 0; i < int(digits.size()) * 4; i++) + if ((digits.at(digits.size() - i/4 -1) & (1 << (i%4))) != 0) + hexfile.back().at(i) = true; +} + void parse_hexfile_line(const char *filename, int linenr, vector> &hexfile, string &line) { vector digits; @@ -54,14 +66,18 @@ void parse_hexfile_line(const char *filename, int linenr, vector> & digits.push_back(10 + c - 'a'); else if ('A' <= c && c <= 'F') digits.push_back(10 + c - 'A'); - else goto error; + else if ('x' == c || 'X' == c || + 'z' == c || 'Z' == c) + digits.push_back(0); + else if ('_' == c) + ; + else if (' ' == c || '\t' == c || '\r' == c) { + push_back_bitvector(hexfile, digits); + digits.clear(); + } else goto error; } - hexfile.push_back(vector(digits.size() * 4)); - - for (int i = 0; i < int(digits.size()) * 4; i++) - if ((digits.at(digits.size() - i/4 -1) & (1 << (i%4))) != 0) - hexfile.back().at(i) = true; + push_back_bitvector(hexfile, digits); return; @@ -181,6 +197,15 @@ int main(int argc, char **argv) for (int i = 1; getline(to_hexfile_f, line); i++) parse_hexfile_line(to_hexfile_n, i, to_hexfile, line); + if (to_hexfile.size() > 0 && from_hexfile.size() > to_hexfile.size()) { + if (verbose) + fprintf(stderr, "Padding to_hexfile from %d words to %d\n", + int(to_hexfile.size()), int(from_hexfile.size())); + do + to_hexfile.push_back(vector(to_hexfile.at(0).size())); + while (from_hexfile.size() > to_hexfile.size()); + } + if (from_hexfile.size() != to_hexfile.size()) { fprintf(stderr, "Hexfiles have different number of words! (%d vs. %d)\n", int(from_hexfile.size()), int(to_hexfile.size())); exit(1); -- cgit v1.2.3