summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBobby Bingham <koorogi@koorogi.info>2017-01-19 19:11:54 -0600
committerBobby Bingham <koorogi@koorogi.info>2017-01-19 19:11:54 -0600
commitf07d5cdbaf5d906b145b152d549915271d355798 (patch)
tree74ca01e5ccf06c67ac25e769f5c4a3f71aa7bbe6
parent7561230a9dda9728b93306c0bd398c5b1cc1ce4d (diff)
nqasm: add command line processing
-rw-r--r--common.h24
-rw-r--r--nqasm.c60
-rw-r--r--nqdasm.c16
3 files changed, 82 insertions, 18 deletions
diff --git a/common.h b/common.h
new file mode 100644
index 0000000..82a6328
--- /dev/null
+++ b/common.h
@@ -0,0 +1,24 @@
+#ifndef NQ_COMMON_H
+#define NQ_COMMON_H
+
+#include <errno.h>
+#include <stdio.h>
+
+extern const char *progname;
+
+#define ERROR(...) \
+ do { \
+ fprintf(stderr, "%s: ", progname); \
+ fprintf(stderr, __VA_ARGS__); \
+ fprintf(stderr, "\n"); \
+ } while (0)
+
+#define PERROR(str) \
+ do { \
+ int err = errno; \
+ fprintf(stderr, "%s: ", progname); \
+ errno = err; \
+ perror(str); \
+ } while (0)
+
+#endif
diff --git a/nqasm.c b/nqasm.c
index e915bd7..15b3238 100644
--- a/nqasm.c
+++ b/nqasm.c
@@ -1,7 +1,9 @@
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
+#include <unistd.h>
+#include "common.h"
#include "mnemonics.h"
#include "nqasm.h"
@@ -9,6 +11,8 @@
#include "parser.h"
#include "vector.h"
+const char *progname;
+
struct label {
const char *name;
uint16_t addr;
@@ -94,7 +98,7 @@ static long eval_argument(const struct argument *arg, uint16_t pc)
}
}
-static void assemble_instruction(const struct instruction *inst, uint16_t pc)
+static void assemble_instruction(FILE *out, const struct instruction *inst, uint16_t pc)
{
const struct mnemonic *mnem = &mnemonics[inst->mnem];
uint16_t bits = mnem->bits;
@@ -132,7 +136,7 @@ static void assemble_instruction(const struct instruction *inst, uint16_t pc)
bits |= (value & 255) << ops[i].shift;
}
- printf("%04x\n", bits);
+ fprintf(out, "%04x\n", bits);
}
void add_label(const char *name)
@@ -149,8 +153,56 @@ void add_label(const char *name)
};
}
+static void usage(void)
+{
+ printf("usage: %s [-o output] [input]\n", progname);
+ printf(" %s -h\n", progname);
+ printf("\n");
+ printf("\t%-16s %s\n", "-h", "display this help text and exit");
+ printf("\t%-16s %s\n", "-o output", "write output to 'output' (default=stdout)");
+ printf("\t%-16s %s\n", "input", "read input from 'input' (default=stdin)");
+}
+
int main(int argc, char **argv)
{
+ const char *outname = NULL;
+ int error = 0;
+
+ extern int optind;
+ extern char *optarg;
+
+ progname = argv[0];
+ for (int opt; (opt = getopt(argc, argv, "+ho:")) != -1;) {
+ switch (opt) {
+ case 'h':
+ usage();
+ return 0;
+ case 'o':
+ outname = optarg;
+ break;
+ default:
+ error = 1;
+ }
+ }
+
+ if (optind < argc - 1) {
+ ERROR("Too many input files");
+ return 1;
+ }
+ if (error) return 1;
+
+ FILE *out = stdout;
+
+ if (optind < argc && !(yyin = fopen(argv[optind], "r"))) {
+ PERROR("Error opening input file");
+ return 1;
+ }
+ if (outname && !(out = fopen(outname, "w"))) {
+ PERROR("Error opening output file");
+ fclose(yyin);
+ return 1;
+ }
+
labels_init();
instrs_init();
@@ -159,9 +211,11 @@ int main(int argc, char **argv)
vector_sort(&labels->v, cmp_label);
for (size_t i = 0; i < instrs->v.count; i++) {
- assemble_instruction(instrs->i + i, 2 * i);
+ assemble_instruction(out, instrs->i + i, 2 * i);
}
+ fclose(yyin);
+ fclose(out);
return 0;
}
diff --git a/nqdasm.c b/nqdasm.c
index f918136..bb26d81 100644
--- a/nqdasm.c
+++ b/nqdasm.c
@@ -5,25 +5,11 @@
#include <string.h>
#include <unistd.h>
+#include "common.h"
#include "mnemonics.h"
const char *progname;
-#define ERROR(...) \
- do { \
- fprintf(stderr, "%s: ", progname); \
- fprintf(stderr, __VA_ARGS__); \
- fprintf(stderr, "\n"); \
- } while (0)
-
-#define PERROR(str) \
- do { \
- int err = errno; \
- fprintf(stderr, "%s: ", progname); \
- errno = err; \
- perror(str); \
- } while (0)
-
static struct line {
int label;
uint16_t value;