-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmain.c
executable file
·215 lines (190 loc) · 5 KB
/
main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <ctype.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "gbadisasm.h"
struct ConfigLabel
{
uint32_t addr;
uint8_t type;
const char *label;
};
uint8_t *gInputFileBuffer;
size_t gInputFileBufferSize;
void fatal_error(const char *fmt, ...)
{
va_list args;
fputs("error: ", stderr);
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
fputs("\n", stderr);
exit(1);
}
static void read_input_file(const char *fname)
{
FILE *file = fopen(fname, "rb");
if (file == NULL)
fatal_error("could not open input file '%s'", fname);
fseek(file, 0, SEEK_END);
gInputFileBufferSize = ftell(file);
fseek(file, 0, SEEK_SET);
gInputFileBuffer = malloc(gInputFileBufferSize);
if (gInputFileBuffer == NULL)
fatal_error("failed to alloc file buffer for '%s'", fname);
if (fread(gInputFileBuffer, 1, gInputFileBufferSize, file) != gInputFileBufferSize)
fatal_error("failed to read from file '%s'", fname);
fclose(file);
}
static char *split_word(char *s)
{
while (!isspace(*s))
{
if (*s == '\0')
return s;
s++;
}
*s++ = '\0';
while (isspace(*s))
s++;
return s;
}
static char *split_line(char *s)
{
while (*s != '\n' && *s != '\r')
{
if (*s == '\0')
return s;
s++;
}
*s++ = '\0';
while (*s == '\n' || *s == '\r')
s++;
return s;
}
static char *skip_whitespace(char *s)
{
while (isspace(*s))
s++;
return s;
}
static char *dup_string(const char *s)
{
char *new = malloc(strlen(s) + 1);
if (new == NULL)
fatal_error("could not alloc space for string '%s'", s);
strcpy(new, s);
return new;
}
static void read_config(const char *fname)
{
FILE *file = fopen(fname, "rb");
char *buffer;
size_t size;
char *line;
char *next;
int lineNum = 1;
if (file == NULL)
fatal_error("could not open config file '%s'", fname);
fseek(file, 0, SEEK_END);
size = ftell(file);
fseek(file, 0, SEEK_SET);
buffer = malloc(size + 1);
if (buffer == NULL)
fatal_error("could not alloc buffer for '%s'", fname);
if (fread(buffer, 1, size, file) != size)
fatal_error("failed to read from file '%s'", fname);
buffer[size] = '\0';
fclose(file);
for (line = next = buffer; *line != '\0'; line = next, lineNum++)
{
char *tokens[3];
char *name = NULL;
int i;
next = split_line(line);
tokens[0] = line = skip_whitespace(line);
for (i = 1; i < 3; i++)
tokens[i] = line = split_word(line);
if (tokens[0][0] == '#')
continue;
if (strcmp(tokens[0], "arm_func") == 0)
{
int addr;
if (sscanf(tokens[1], "%i", &addr) == 1)
{
if (strlen(tokens[2]) != 0)
name = dup_string(tokens[2]);
disasm_add_label(addr, LABEL_ARM_CODE, name);
}
else
{
fatal_error("%s: syntax error on line %i\n", fname, lineNum);
}
}
else if (strcmp(tokens[0], "thumb_func") == 0)
{
int addr;
if (sscanf(tokens[1], "%i", &addr) == 1)
{
if (strlen(tokens[2]) != 0)
name = dup_string(tokens[2]);
disasm_add_label(addr, LABEL_THUMB_CODE, name);
}
else
{
fatal_error("%s: syntax error on line %i\n", fname, lineNum);
}
}
else
{
fprintf(stderr, "%s: warning: unrecognized command '%s' on line %i\n", fname, tokens[0], lineNum);
}
}
free(buffer);
}
int main(int argc, char **argv)
{
int i;
const char *romFileName = NULL;
const char *configFileName = NULL;
ROM_LOAD_ADDR = 0x08000000;
#ifdef _WIN32
// Work around MinGW bug that prevents us from seeing the assert message
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
#endif
for (i = 1; i < argc; i++)
{
if (strcmp(argv[i], "-c") == 0)
{
if (i + 1 >= argc)
fatal_error("expected filename for option -c");
i++;
configFileName = argv[i];
}
else if (strcmp(argv[i], "-l") == 0)
{
char * end;
if (i + 1 >= argc)
fatal_error("expected integer for option -l");
i++;
ROM_LOAD_ADDR = strtoul(argv[i], &end, 0);
if (*end != 0)
fatal_error("invalid integer value for option -l");
}
else
{
romFileName = argv[i];
}
}
if (romFileName == NULL)
fatal_error("no ROM file specified");
if (configFileName != NULL)
read_config(configFileName);
read_input_file(romFileName);
disasm_disassemble();
free(gInputFileBuffer);
return 0;
}