Browse code

Minor cleanup

neauoire authored on 13/03/2021 22:55:29
Showing 2 changed files
... ...
@@ -17,27 +17,23 @@ typedef unsigned short Uint16;
17 17
 typedef signed short Sint16;
18 18
 
19 19
 typedef struct {
20
-	Uint8 data[256 * 256];
21
-	Uint16 ptr;
22
-} Program;
23
-
24
-typedef struct {
25
-	Uint8 len, length[16], size, refs;
26 20
 	char name[64], params[16][64];
21
+	Uint8 len, length[16], size, refs;
27 22
 } Template;
28 23
 
29 24
 typedef struct {
25
+	char name[64];
30 26
 	Uint8 len, offset, refs;
31 27
 	Uint16 addr;
32
-	char name[64];
33 28
 	Template *template;
34 29
 } Label;
35 30
 
36
-int templateslen;
37
-Template templates[256];
38
-
39
-int labelslen;
40
-Label labels[256];
31
+typedef struct {
32
+	Uint8 data[256 * 256], tlen, llen;
33
+	Uint16 ptr;
34
+	Template templates[256];
35
+	Label labels[256];
36
+} Program;
41 37
 
42 38
 Program p;
43 39
 
... ...
@@ -66,16 +62,14 @@ char *scpy(char *src, char *dst, int len) { int i = 0; while((dst[i] = src[i]) &
66 62
 void
67 63
 pushbyte(Uint8 b, int lit)
68 64
 {
69
-	if(lit)
70
-		pushbyte(0x02, 0);
65
+	if(lit) pushbyte(0x02, 0);
71 66
 	p.data[p.ptr++] = b;
72 67
 }
73 68
 
74 69
 void
75 70
 pushshort(Uint16 s, int lit)
76 71
 {
77
-	if(lit)
78
-		pushbyte(0x22, 0);
72
+	if(lit) pushbyte(0x22, 0);
79 73
 	pushbyte((s >> 8) & 0xff, 0);
80 74
 	pushbyte(s & 0xff, 0);
81 75
 }
... ...
@@ -85,19 +79,17 @@ pushtext(char *s, int lit)
85 79
 {
86 80
 	int i = 0;
87 81
 	char c;
88
-	if(lit)
89
-		pushbyte(0x22, 0);
90
-	while((c = s[i++]))
91
-		pushbyte(c, 0);
82
+	if(lit) pushbyte(0x22, 0);
83
+	while((c = s[i++])) pushbyte(c, 0);
92 84
 }
93 85
 
94 86
 Template *
95 87
 findtemplate(char *s)
96 88
 {
97 89
 	int i;
98
-	for(i = 0; i < templateslen; ++i)
99
-		if(scmp(templates[i].name, s, 64))
100
-			return &templates[i];
90
+	for(i = 0; i < p.tlen; ++i)
91
+		if(scmp(p.templates[i].name, s, 64))
92
+			return &p.templates[i];
101 93
 	return NULL;
102 94
 }
103 95
 
... ...
@@ -107,9 +99,9 @@ findlabel(char *s)
107 99
 	int i, rng = scin(s, '.');
108 100
 	char name[64];
109 101
 	scpy(s, name, rng > 0 ? rng + 1 : 64);
110
-	for(i = 0; i < labelslen; ++i)
111
-		if(scmp(labels[i].name, name, 64))
112
-			return &labels[i];
102
+	for(i = 0; i < p.llen; ++i)
103
+		if(scmp(p.labels[i].name, name, 64))
104
+			return &p.labels[i];
113 105
 	return NULL;
114 106
 }
115 107
 
... ...
@@ -197,13 +189,11 @@ maketemplate(char *name, FILE *f)
197 189
 		return error("Template name is hex number", name);
198 190
 	if(findopcode(name))
199 191
 		return error("Template name is invalid", name);
200
-	m = &templates[templateslen++];
192
+	m = &p.templates[p.tlen++];
201 193
 	scpy(name, m->name, 64);
202 194
 	while(fscanf(f, "%s", wv)) {
203
-		if(wv[0] == '{')
204
-			continue;
205
-		if(wv[0] == '}')
206
-			break;
195
+		if(wv[0] == '{') continue;
196
+		if(wv[0] == '}') break;
207 197
 		if(mode == 0)
208 198
 			scpy(wv, m->params[m->len], 64);
209 199
 		else {
... ...
@@ -227,7 +217,7 @@ makelabel(char *name, Uint16 addr, Uint8 len, Template *m)
227 217
 		return error("Label name is hex number", name);
228 218
 	if(findopcode(name))
229 219
 		return error("Label name is invalid", name);
230
-	l = &labels[labelslen++];
220
+	l = &p.labels[p.llen++];
231 221
 	l->addr = addr;
232 222
 	l->len = len;
233 223
 	l->refs = 0;
... ...
@@ -335,6 +325,8 @@ pass2(FILE *f)
335 325
 		Label *l;
336 326
 		if(w[0] == '&') continue;
337 327
 		if(w[0] == '$') continue;
328
+		if(skipblock(w, &ccmnt, '(', ')')) continue;
329
+		if(skipblock(w, &ctemplate, '{', '}')) continue;
338 330
 		if(w[0] == '@') {
339 331
 			scpy(w + 1, scope, 64);
340 332
 			continue;
... ...
@@ -343,8 +335,6 @@ pass2(FILE *f)
343 335
 			sublabel(subw, scope, w + 2);
344 336
 			scpy(subw, w + 1, 64);
345 337
 		}
346
-		if(skipblock(w, &ccmnt, '(', ')')) continue;
347
-		if(skipblock(w, &ctemplate, '{', '}')) continue;
348 338
 		/* clang-format off */
349 339
 		if(skipblock(w, &cbits, '[', ']')) {
350 340
 			if(w[0] == '[' || w[0] == ']') { continue; }
... ...
@@ -352,14 +342,13 @@ pass2(FILE *f)
352 342
 			else if(slen(w) == 2 && sihx(w)) pushbyte(shex(w), 0); 
353 343
 			else pushtext(w, 0);
354 344
 		}
355
-		else if(w[0] == '|') p.ptr = shex(w + 1);
356
-		else if((op = findopcode(w)) || scmp(w, "BRK", 4)) pushbyte(op, 0);
357 345
 		else if(w[0] == '^' && (l = findlabel(w + 1))) { 
358 346
 			int off = l->addr - p.ptr - 3;
359 347
 			if(off < -126 || off > 126){ printf("Address %s is too far(%d).\n", w, off); return 0; } 
360 348
 			pushbyte((Sint8)(l->addr - p.ptr - 3), 1); l->refs++; 
361 349
 		}
362
-		else if(w[0] == ':') fscanf(f, "%s", w);
350
+		else if(w[0] == '|') p.ptr = shex(w + 1);
351
+		else if((op = findopcode(w)) || scmp(w, "BRK", 4)) pushbyte(op, 0);
363 352
 		else if(w[0] == ';') fscanf(f, "%s", w);
364 353
 		else if(w[0] == '.' && (l = findlabel(w + 1))) { pushshort(findlabeladdr(w + 1), 0); l->refs++; }
365 354
 		else if(w[0] == ',' && (l = findlabel(w + 1))) { pushshort(findlabeladdr(w + 1), 1); l->refs++; }
... ...
@@ -381,12 +370,12 @@ void
381 370
 cleanup(void)
382 371
 {
383 372
 	int i;
384
-	for(i = 0; i < labelslen; ++i)
385
-		if(!labels[i].refs)
386
-			printf("--- Unused label: %s\n", labels[i].name);
387
-	for(i = 0; i < templateslen; ++i)
388
-		if(!templates[i].refs)
389
-			printf("--- Unused template: %s\n", templates[i].name);
373
+	for(i = 0; i < p.llen; ++i)
374
+		if(!p.labels[i].refs)
375
+			printf("--- Unused label: %s\n", p.labels[i].name);
376
+	for(i = 0; i < p.tlen; ++i)
377
+		if(!p.templates[i].refs)
378
+			printf("--- Unused template: %s\n", p.templates[i].name);
390 379
 }
391 380
 
392 381
 int
... ...
@@ -20,5 +20,5 @@ cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werr
20 20
 # cc uxn.c emulator.c -std=c89 -Os -DNDEBUG -g0 -s -Wall -Wno-unknown-pragmas -L/usr/local/lib -lSDL2 -o bin/emulator
21 21
 
22 22
 # run
23
-./bin/assembler projects/software/left.usm bin/boot.rom
24
-# ./bin/emulator bin/boot.rom
23
+./bin/assembler projects/examples/dev.ctrl.usm bin/boot.rom
24
+./bin/emulator bin/boot.rom