Minor cleanup

This commit is contained in:
neauoire 2021-03-13 14:55:29 -08:00
parent fd883dc4c8
commit 0d9df02190
2 changed files with 34 additions and 45 deletions

View File

@ -17,27 +17,23 @@ typedef unsigned short Uint16;
typedef signed short Sint16; typedef signed short Sint16;
typedef struct { typedef struct {
Uint8 data[256 * 256];
Uint16 ptr;
} Program;
typedef struct {
Uint8 len, length[16], size, refs;
char name[64], params[16][64]; char name[64], params[16][64];
Uint8 len, length[16], size, refs;
} Template; } Template;
typedef struct { typedef struct {
char name[64];
Uint8 len, offset, refs; Uint8 len, offset, refs;
Uint16 addr; Uint16 addr;
char name[64];
Template *template; Template *template;
} Label; } Label;
int templateslen; typedef struct {
Template templates[256]; Uint8 data[256 * 256], tlen, llen;
Uint16 ptr;
int labelslen; Template templates[256];
Label labels[256]; Label labels[256];
} Program;
Program p; Program p;
@ -66,16 +62,14 @@ char *scpy(char *src, char *dst, int len) { int i = 0; while((dst[i] = src[i]) &
void void
pushbyte(Uint8 b, int lit) pushbyte(Uint8 b, int lit)
{ {
if(lit) if(lit) pushbyte(0x02, 0);
pushbyte(0x02, 0);
p.data[p.ptr++] = b; p.data[p.ptr++] = b;
} }
void void
pushshort(Uint16 s, int lit) pushshort(Uint16 s, int lit)
{ {
if(lit) if(lit) pushbyte(0x22, 0);
pushbyte(0x22, 0);
pushbyte((s >> 8) & 0xff, 0); pushbyte((s >> 8) & 0xff, 0);
pushbyte(s & 0xff, 0); pushbyte(s & 0xff, 0);
} }
@ -85,19 +79,17 @@ pushtext(char *s, int lit)
{ {
int i = 0; int i = 0;
char c; char c;
if(lit) if(lit) pushbyte(0x22, 0);
pushbyte(0x22, 0); while((c = s[i++])) pushbyte(c, 0);
while((c = s[i++]))
pushbyte(c, 0);
} }
Template * Template *
findtemplate(char *s) findtemplate(char *s)
{ {
int i; int i;
for(i = 0; i < templateslen; ++i) for(i = 0; i < p.tlen; ++i)
if(scmp(templates[i].name, s, 64)) if(scmp(p.templates[i].name, s, 64))
return &templates[i]; return &p.templates[i];
return NULL; return NULL;
} }
@ -107,9 +99,9 @@ findlabel(char *s)
int i, rng = scin(s, '.'); int i, rng = scin(s, '.');
char name[64]; char name[64];
scpy(s, name, rng > 0 ? rng + 1 : 64); scpy(s, name, rng > 0 ? rng + 1 : 64);
for(i = 0; i < labelslen; ++i) for(i = 0; i < p.llen; ++i)
if(scmp(labels[i].name, name, 64)) if(scmp(p.labels[i].name, name, 64))
return &labels[i]; return &p.labels[i];
return NULL; return NULL;
} }
@ -197,13 +189,11 @@ maketemplate(char *name, FILE *f)
return error("Template name is hex number", name); return error("Template name is hex number", name);
if(findopcode(name)) if(findopcode(name))
return error("Template name is invalid", name); return error("Template name is invalid", name);
m = &templates[templateslen++]; m = &p.templates[p.tlen++];
scpy(name, m->name, 64); scpy(name, m->name, 64);
while(fscanf(f, "%s", wv)) { while(fscanf(f, "%s", wv)) {
if(wv[0] == '{') if(wv[0] == '{') continue;
continue; if(wv[0] == '}') break;
if(wv[0] == '}')
break;
if(mode == 0) if(mode == 0)
scpy(wv, m->params[m->len], 64); scpy(wv, m->params[m->len], 64);
else { else {
@ -227,7 +217,7 @@ makelabel(char *name, Uint16 addr, Uint8 len, Template *m)
return error("Label name is hex number", name); return error("Label name is hex number", name);
if(findopcode(name)) if(findopcode(name))
return error("Label name is invalid", name); return error("Label name is invalid", name);
l = &labels[labelslen++]; l = &p.labels[p.llen++];
l->addr = addr; l->addr = addr;
l->len = len; l->len = len;
l->refs = 0; l->refs = 0;
@ -335,6 +325,8 @@ pass2(FILE *f)
Label *l; Label *l;
if(w[0] == '&') continue; if(w[0] == '&') continue;
if(w[0] == '$') continue; if(w[0] == '$') continue;
if(skipblock(w, &ccmnt, '(', ')')) continue;
if(skipblock(w, &ctemplate, '{', '}')) continue;
if(w[0] == '@') { if(w[0] == '@') {
scpy(w + 1, scope, 64); scpy(w + 1, scope, 64);
continue; continue;
@ -343,8 +335,6 @@ pass2(FILE *f)
sublabel(subw, scope, w + 2); sublabel(subw, scope, w + 2);
scpy(subw, w + 1, 64); scpy(subw, w + 1, 64);
} }
if(skipblock(w, &ccmnt, '(', ')')) continue;
if(skipblock(w, &ctemplate, '{', '}')) continue;
/* clang-format off */ /* clang-format off */
if(skipblock(w, &cbits, '[', ']')) { if(skipblock(w, &cbits, '[', ']')) {
if(w[0] == '[' || w[0] == ']') { continue; } if(w[0] == '[' || w[0] == ']') { continue; }
@ -352,14 +342,13 @@ pass2(FILE *f)
else if(slen(w) == 2 && sihx(w)) pushbyte(shex(w), 0); else if(slen(w) == 2 && sihx(w)) pushbyte(shex(w), 0);
else pushtext(w, 0); else pushtext(w, 0);
} }
else if(w[0] == '|') p.ptr = shex(w + 1);
else if((op = findopcode(w)) || scmp(w, "BRK", 4)) pushbyte(op, 0);
else if(w[0] == '^' && (l = findlabel(w + 1))) { else if(w[0] == '^' && (l = findlabel(w + 1))) {
int off = l->addr - p.ptr - 3; int off = l->addr - p.ptr - 3;
if(off < -126 || off > 126){ printf("Address %s is too far(%d).\n", w, off); return 0; } if(off < -126 || off > 126){ printf("Address %s is too far(%d).\n", w, off); return 0; }
pushbyte((Sint8)(l->addr - p.ptr - 3), 1); l->refs++; pushbyte((Sint8)(l->addr - p.ptr - 3), 1); l->refs++;
} }
else if(w[0] == ':') fscanf(f, "%s", w); else if(w[0] == '|') p.ptr = shex(w + 1);
else if((op = findopcode(w)) || scmp(w, "BRK", 4)) pushbyte(op, 0);
else if(w[0] == ';') fscanf(f, "%s", w); else if(w[0] == ';') fscanf(f, "%s", w);
else if(w[0] == '.' && (l = findlabel(w + 1))) { pushshort(findlabeladdr(w + 1), 0); l->refs++; } else if(w[0] == '.' && (l = findlabel(w + 1))) { pushshort(findlabeladdr(w + 1), 0); l->refs++; }
else if(w[0] == ',' && (l = findlabel(w + 1))) { pushshort(findlabeladdr(w + 1), 1); l->refs++; } else if(w[0] == ',' && (l = findlabel(w + 1))) { pushshort(findlabeladdr(w + 1), 1); l->refs++; }
@ -381,12 +370,12 @@ void
cleanup(void) cleanup(void)
{ {
int i; int i;
for(i = 0; i < labelslen; ++i) for(i = 0; i < p.llen; ++i)
if(!labels[i].refs) if(!p.labels[i].refs)
printf("--- Unused label: %s\n", labels[i].name); printf("--- Unused label: %s\n", p.labels[i].name);
for(i = 0; i < templateslen; ++i) for(i = 0; i < p.tlen; ++i)
if(!templates[i].refs) if(!p.templates[i].refs)
printf("--- Unused template: %s\n", templates[i].name); printf("--- Unused template: %s\n", p.templates[i].name);
} }
int int

View File

@ -20,5 +20,5 @@ cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werr
# cc uxn.c emulator.c -std=c89 -Os -DNDEBUG -g0 -s -Wall -Wno-unknown-pragmas -L/usr/local/lib -lSDL2 -o bin/emulator # cc uxn.c emulator.c -std=c89 -Os -DNDEBUG -g0 -s -Wall -Wno-unknown-pragmas -L/usr/local/lib -lSDL2 -o bin/emulator
# run # run
./bin/assembler projects/software/left.usm bin/boot.rom ./bin/assembler projects/examples/dev.ctrl.usm bin/boot.rom
# ./bin/emulator bin/boot.rom ./bin/emulator bin/boot.rom