-
Notifications
You must be signed in to change notification settings - Fork 11
/
asmmain.c
73 lines (61 loc) · 1.37 KB
/
asmmain.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "chip8.h"
static void usage(const char *name) {
printf("usage: %s [options] infile.asm\n", name);
printf("where options are:\n");
printf(" -o outfile : Output file\n");
printf(" -v : Verbose mode\n");
}
int main(int argc, char *argv[]) {
int opt;
const char *infile = NULL;
const char *outfile = "a.ch8";
char *text;
while((opt = getopt(argc, argv, "vo:?")) != -1) {
switch(opt) {
case 'v': {
c8_verbose++;
} break;
case 'o': {
outfile = optarg;
} break;
case '?' : {
usage(argv[0]);
return 1;
}
}
}
if(optind >= argc) {
usage(argv[0]);
return 1;
}
infile = argv[optind++];
if(c8_verbose)
printf("Reading input from '%s'...\n", infile);
text = c8_load_txt(infile);
if(!text) {
fprintf(stderr, "error: unable to read '%s'\n", infile);
return 1;
}
if(c8_verbose)
printf("Input read.\n");
c8_reset();
int return_code = c8_assemble(text);
if(c8_verbose)
printf("Writing output to '%s'...\n", outfile);
if(!c8_save_file(outfile)) {
fprintf(stderr, "error: unable to write output to '%s': %s\n", outfile, strerror(errno));
return 1;
}
if(c8_verbose)
printf("Output written.\n");
free(text);
if(c8_verbose)
printf("Success.\n");
return return_code;
}