aboutsummaryrefslogtreecommitdiffstats
path: root/bba
diff options
context:
space:
mode:
authorMiodrag Milanovic <mmicko@gmail.com>2018-07-25 09:31:44 +0200
committerMiodrag Milanovic <mmicko@gmail.com>2018-07-25 09:31:44 +0200
commit8b60ed5fd1bda845b4d3f8ac727f5b285713ff0e (patch)
tree1d6238b8ab05e50e57760312681e818f4fc95a1c /bba
parentc3859072d4e621186e7b4ded3d2311ce0e8d31ba (diff)
downloadnextpnr-8b60ed5fd1bda845b4d3f8ac727f5b285713ff0e.tar.gz
nextpnr-8b60ed5fd1bda845b4d3f8ac727f5b285713ff0e.tar.bz2
nextpnr-8b60ed5fd1bda845b4d3f8ac727f5b285713ff0e.zip
Fix bba to compile on windows (no unistd there)
Diffstat (limited to 'bba')
-rw-r--r--bba/bba.cmake1
-rw-r--r--bba/main.cc97
2 files changed, 55 insertions, 43 deletions
diff --git a/bba/bba.cmake b/bba/bba.cmake
index 4bb9ea0e..dd803d5d 100644
--- a/bba/bba.cmake
+++ b/bba/bba.cmake
@@ -5,6 +5,7 @@ ENDIF(CMAKE_CROSSCOMPILING)
IF(NOT CMAKE_CROSSCOMPILING)
ADD_EXECUTABLE(bbasm bba/main.cc)
+ target_link_libraries(bbasm LINK_PUBLIC ${Boost_LIBRARIES})
ENDIF(NOT CMAKE_CROSSCOMPILING)
IF(NOT CMAKE_CROSSCOMPILING)
diff --git a/bba/main.cc b/bba/main.cc
index 53c70b83..ee6129ac 100644
--- a/bba/main.cc
+++ b/bba/main.cc
@@ -1,12 +1,13 @@
+#include <assert.h>
+#include <boost/program_options.hpp>
+#include <iostream>
#include <map>
-#include <vector>
-#include <string>
+#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
-#include <stdint.h>
#include <string.h>
-#include <assert.h>
-#include <unistd.h>
+#include <string>
+#include <vector>
enum TokenType : int8_t
{
@@ -51,35 +52,50 @@ int main(int argc, char **argv)
bool writeC = false;
char buffer[512];
- int opt;
- while ((opt = getopt(argc, argv, "vbc")) != -1)
- {
- switch (opt)
- {
- case 'v':
- verbose = true;
- break;
- case 'b':
- bigEndian = true;
- break;
- case 'c':
- writeC = true;
- break;
- default:
- assert(0);
- }
+ namespace po = boost::program_options;
+ po::positional_options_description pos;
+ po::options_description options("Allowed options");
+ options.add_options()("v", "verbose output");
+ options.add_options()("b", "big endian");
+ options.add_options()("c", "write c strings");
+ options.add_options()("files", po::value<std::vector<std::string>>(), "file parameters");
+ pos.add("files", -1);
+
+ po::variables_map vm;
+ try {
+ po::parsed_options parsed = po::command_line_parser(argc, argv).options(options).positional(pos).run();
+
+ po::store(parsed, vm);
+
+ po::notify(vm);
+ } catch (std::exception &e) {
+ std::cout << e.what() << "\n";
+ return 1;
+ }
+ if (vm.count("v"))
+ verbose = true;
+ if (vm.count("b"))
+ bigEndian = true;
+ if (vm.count("c"))
+ writeC = true;
+
+ if (vm.count("files") == 0) {
+ printf("File parameters are mandatory\n");
+ exit(-1);
+ }
+ std::vector<std::string> files = vm["files"].as<std::vector<std::string>>();
+ if (files.size() != 2) {
+ printf("Input and output parameters must be set\n");
+ exit(-1);
}
- assert(optind+2 == argc);
-
- FILE *fileIn = fopen(argv[optind], "rt");
+ FILE *fileIn = fopen(files.at(0).c_str(), "rt");
assert(fileIn != nullptr);
- FILE *fileOut = fopen(argv[optind+1], writeC ? "wt" : "wb");
+ FILE *fileOut = fopen(files.at(1).c_str(), writeC ? "wt" : "wb");
assert(fileOut != nullptr);
- while (fgets(buffer, 512, fileIn) != nullptr)
- {
+ while (fgets(buffer, 512, fileIn) != nullptr) {
std::string cmd = strtok(buffer, " \t\r\n");
if (cmd == "pre") {
@@ -178,8 +194,7 @@ int main(int argc, char **argv)
int cursor = 0;
for (auto &s : streams) {
for (int i = 0; i < int(s.tokenTypes.size()); i++) {
- switch (s.tokenTypes[i])
- {
+ switch (s.tokenTypes[i]) {
case TOK_LABEL:
labels[s.tokenValues[i]] = cursor;
break;
@@ -205,7 +220,7 @@ int main(int argc, char **argv)
if (verbose) {
printf("resolved positions for %d labels.\n", int(labels.size()));
- printf("total data (including strings): %.2f MB\n", double(cursor) / (1024*1024));
+ printf("total data (including strings): %.2f MB\n", double(cursor) / (1024 * 1024));
}
std::vector<uint8_t> data(cursor);
@@ -216,8 +231,7 @@ int main(int argc, char **argv)
uint32_t value = s.tokenValues[i];
int numBytes = 0;
- switch (s.tokenTypes[i])
- {
+ switch (s.tokenTypes[i]) {
case TOK_LABEL:
break;
case TOK_REF:
@@ -238,8 +252,7 @@ int main(int argc, char **argv)
}
if (bigEndian) {
- switch (numBytes)
- {
+ switch (numBytes) {
case 4:
data[cursor++] = value >> 24;
data[cursor++] = value >> 16;
@@ -256,14 +269,13 @@ int main(int argc, char **argv)
assert(0);
}
} else {
- switch (numBytes)
- {
+ switch (numBytes) {
case 4:
- data[cursor+3] = value >> 24;
- data[cursor+2] = value >> 16;
+ data[cursor + 3] = value >> 24;
+ data[cursor + 2] = value >> 16;
/* fall-through */
case 2:
- data[cursor+1] = value >> 8;
+ data[cursor + 1] = value >> 8;
/* fall-through */
case 1:
data[cursor] = value;
@@ -284,7 +296,7 @@ int main(int argc, char **argv)
for (auto &s : preText)
fprintf(fileOut, "%s\n", s.c_str());
- fprintf(fileOut, "const char %s[%d] =\n\"", streams[0].name.c_str(), int(data.size())+1);
+ fprintf(fileOut, "const char %s[%d] =\n\"", streams[0].name.c_str(), int(data.size()) + 1);
cursor = 1;
for (auto d : data) {
@@ -300,8 +312,7 @@ int main(int argc, char **argv)
if (d < 32 || d >= 128) {
fprintf(fileOut, "\\%03o", int(d));
cursor += 4;
- } else
- if (d == '\"' || d == '\'' || d == '\\') {
+ } else if (d == '\"' || d == '\'' || d == '\\') {
fputc('\\', fileOut);
fputc(d, fileOut);
cursor += 2;