aboutsummaryrefslogtreecommitdiff
path: root/src/orgaasm.c
blob: d48ad94700c18a52b812ff781327ab44802265db (plain)
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include "ops.h"
#include "drain.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

typedef struct Label Label;
struct Label {
	const char *s;
	size_t addr;
	Label *next;
};

static const int hextable[256] = {
	['0'] = 0x0, ['1'] = 0x1, ['2'] = 0x2, ['3'] = 0x3, ['4'] = 0x4,
	['5'] = 0x5, ['6'] = 0x6, ['7'] = 0x7, ['8'] = 0x8, ['9'] = 0x9,
	['a'] = 0xa, ['b'] = 0xb, ['c'] = 0xc, ['d'] = 0xd, ['e'] = 0xe,
	['f'] = 0xf,
};
static const char *sep = "\t\n\v\f\r ";
static int verbose = 0;
static char *data = NULL, *data_dup = NULL;
FILE *outfile = NULL;
static Label *label_root = NULL;

static int
fail(void)
{
	if (data != NULL)
		free(data);
	if (data_dup != NULL)
		free(data_dup);
	while (label_root != NULL) {
		Label *const next = label_root->next;
		free(label_root);
		label_root = next;
	}
	if (outfile != NULL)
		fclose(outfile);
	return 1;
}

static void
skip_comments(void)
{
	char *tok;
	do
		tok = strtok(NULL, sep);
	while (tok != NULL && strcmp(tok, ")") != 0);

	if (tok == NULL) {
		fprintf(stderr, "unclosed comment");
		exit(fail());
	}
}

static void
write_short(uint_least16_t v)
{
	if (verbose) printf("write: %04x\n", v);
	if (fputc(v & 0x00ff, outfile) == EOF ||
	    fputc(v / 0x0100, outfile) == EOF) {
		fprintf(stderr, "fputc failed\n");
		exit(fail());
	}
}

static void
register_label(const char *s, size_t pc)
{
	Label *const label = malloc(sizeof(Label));
	if (label == NULL) {
		perror("register_label:malloc");
		exit(fail());
	}

	label->s = s;
	label->addr = pc;
	label->next = label_root;
	label_root = label;
	if (verbose) printf("label: '%s' at pc %zu\n", s, pc);
}

static void
write_label(const char *s)
{
	for (Label *label = label_root; label != NULL; label = label->next) {
		if (strcmp(s, label->s) == 0) {
			write_short(label->addr);
			return;
		}
	}
	fprintf(stderr, "unknown label '%s'\n", s);
}

static int
opcmp(const char *s0, const char *s1)
{
	return (s0[0] == s1[0] && s0[1] == s1[1] && s0[2] == s1[2]);
}

static void
write_opcode(const char *s)
{
	for (unsigned i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
		if (opcmp(s, ops[i])) {
			write_short(i);
			return;
		}
	}
	fprintf(stderr, "unknown opcode '%s'\n", s);
	exit(fail());
}

static int
ishex(int c)
{
	return ((c >= 'a' && c <= 'f') || (c >= '0' && c <= '9'));
}

static void
write_literal(const char *s)
{
	if (!ishex(s[0]) && !ishex(s[1]) && !ishex(s[2]) && !ishex(s[3])) {
		fprintf(stderr, "invalid literal '%s'\n", s);
		exit(fail());
	}
	write_short((hextable[(int)s[0]] * 0x1000) |
	            (hextable[(int)s[1]] * 0x100) |
	            (hextable[(int)s[2]] * 0x10) |
	            (hextable[(int)s[3]]));
}

static void
first_pass(char *s)
{
	char *tok;
	size_t pc = 0;
	for (tok = strtok(s, sep); tok != NULL; tok = strtok(NULL, sep)) {
		/* Skip comment. */
		if (strcmp(tok, "(") == 0) {
			skip_comments();
			continue;
		}

		/* Label marker. */
		if (tok[0] == '@')
			register_label(tok + 1, pc);
		else
			pc += 1;
	}
}

static void
second_pass(char *s)
{
	char *tok;
	for (tok = strtok(s, sep); tok != NULL; tok = strtok(NULL, sep)) {
		switch (tok[0]) {
		case '@': /* label declaration, skip */
			continue;
		case ',': /* label insertion */
			write_label(tok + 1);
			continue;
		default:
			break;
		}

		/* Skip comment. */
		if (strcmp(tok, "(") == 0) {
			skip_comments();
			continue;
		}

		const size_t tok_len = strlen(tok);
		switch (tok_len) {
		case 3: /* opcode */
			if (verbose) printf("opcode: %s\n", tok);
			write_opcode(tok);
			break;
		case 4: /* literal */
			if (verbose) printf("literal: %s\n", tok);
			write_literal(tok);
			break;
		default: /* wtf */
			fprintf(stderr, "unknown token '%s'\n", tok);
			exit(fail());
		}
	}
}

int
main(int argc, char **argv)
{
	if (!(argc == 3 || (argc == 4 && strcmp(argv[3], "-v") == 0))) {
		fprintf(stderr, "usage: %s <asm> <rom> [-v]\n", argv[0]);
		return fail();
	}
	verbose = (argc == 4);

	/* Dump file to memory. */
	FILE *const fp = fopen(argv[1], "rb");
	if (fp == NULL) {
		perror(argv[1]);
		return fail();
	}
	long size;
	data = drain(fp, &size);
	fclose(fp);
	if (data == NULL)
		return fail();

	/* Duplicate data. */
	data_dup = malloc(strlen(data) + 1);
	if (data_dup == NULL) {
		perror("main:malloc");
		return fail();
	}
	strcpy(data_dup, data);

	/* First pass, find labels. */
	first_pass(data);

	/* Open out file. */
	outfile = fopen(argv[2], "wb");
	if (outfile == NULL) {
		perror(argv[2]);
		return fail();
	}

	/* Second pass, generate bytecode. */
	second_pass(data_dup);

	return fail(), 0;
}