diff --git a/src/devices/system.c b/src/devices/system.c index e628bb2..5c2a842 100644 --- a/src/devices/system.c +++ b/src/devices/system.c @@ -125,7 +125,7 @@ system_deo(Uxn *u, Uint8 *d, Uint8 port) /* Errors */ int -uxn_halt(Uxn *u, Uint8 instr, Uint8 err, Uint16 addr) +emu_halt(Uxn *u, Uint8 instr, Uint8 err, Uint16 addr) { Uint8 *d = &u->dev[0]; Uint16 handler = PEEK2(d); diff --git a/src/uxn.c b/src/uxn.c index bce2d3d..8f5e58e 100644 --- a/src/uxn.c +++ b/src/uxn.c @@ -11,35 +11,32 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE. */ -#define T s->dat[s->ptr - 1] -#define N s->dat[s->ptr - 2] -#define L s->dat[s->ptr - 3] -#define H2 PEEK2(s->dat + s->ptr - 3) -#define T2 PEEK2(s->dat + s->ptr - 2) -#define N2 PEEK2(s->dat + s->ptr - 4) -#define L2 PEEK2(s->dat + s->ptr - 6) - /* Registers - [ . ][ . ][ . ][ L ][ N ][ T ] < [ . ][ . ][ . ][ H2 ][ T ] < [ L2 ][ N2 ][ T2 ] < - */ -#define HALT(c) { return uxn_halt(u, ins, (c), pc - 1); } -#define SET(mul, add) { if(mul > s->ptr) HALT(1) tmp = (mul & k) + add + s->ptr; if(tmp > 254) HALT(2) s->ptr = tmp; } -#define PUT(o, v) { s->dat[(Uint8)(s->ptr - 1 - (o))] = (v); } -#define PUT2(o, v) { tmp = (v); s->dat[(Uint8)(s->ptr - o - 2)] = tmp >> 8; s->dat[(Uint8)(s->ptr - o - 1)] = tmp; } -#define PUSH(v) { if(s->ptr > 254) HALT(2) s->dat[s->ptr++] = (v); } -#define PUSH2(v) { if(s->ptr > 253) HALT(2) tmp = (v); s->dat[s->ptr] = tmp >> 8; s->dat[s->ptr + 1] = tmp; s->ptr += 2; } -#define DEO(a, b) { u->dev[(a)] = (b); if((deo_mask[(a) >> 4] >> ((a) & 0xf)) & 0x1) uxn_deo(u, (a)); } -#define DEI(a, b) { PUT((a), ((dei_mask[(b) >> 4] >> ((b) & 0xf)) & 0x1) ? uxn_dei(u, (b)) : u->dev[(b)]) } +#define T s->dat[s->ptr - 1] +#define N s->dat[s->ptr - 2] +#define L s->dat[s->ptr - 3] +#define T2 PEEK2(s->dat + s->ptr - 2) +#define H2 PEEK2(s->dat + s->ptr - 3) +#define N2 PEEK2(s->dat + s->ptr - 4) +#define L2 PEEK2(s->dat + s->ptr - 6) + +#define HALT(c) { return emu_halt(u, ins, (c), pc - 1); } +#define FLIP { s = ins & 0x40 ? &u->wst : &u->rst; } +#define SET(x, y) { if(x > s->ptr) HALT(1) tmp = (x & k) + y + s->ptr; if(tmp > 254) HALT(2) s->ptr = tmp; } +#define PUT(o, v) { s->dat[(s->ptr - 1 - (o))] = (v); } +#define PUT2(o, v) { tmp = (v); POKE2(s->dat + (s->ptr - o - 2), tmp); } +#define DEO(a, b) { u->dev[(a)] = (b); if((deo_mask[(a) >> 4] >> ((a) & 0xf)) & 0x1) emu_deo(u, (a)); } +#define DEI(a, b) { PUT((a), ((dei_mask[(b) >> 4] >> ((b) & 0xf)) & 0x1) ? emu_dei(u, (b)) : u->dev[(b)]) } int uxn_eval(Uxn *u, Uint16 pc) { - int t, n, l, k, tmp, opc, ins; + int t, n, l, k, tmp, ins, opc; Uint8 *ram = u->ram; Stack *s; if(!pc || u->dev[0x0f]) return 0; @@ -50,14 +47,14 @@ uxn_eval(Uxn *u, Uint16 pc) opc = !(ins & 0x1f) ? (0 - (ins >> 5)) & 0xff : ins & 0x3f; switch(opc) { /* IMM */ - case 0x00: /* BRK */ return 1; - case 0xff: /* JCI */ pc += !!s->dat[--s->ptr] * PEEK2(ram + pc) + 2; break; - case 0xfe: /* JMI */ pc += PEEK2(ram + pc) + 2; break; - case 0xfd: /* JSI */ s = &u->rst; PUSH2(pc + 2) pc += PEEK2(ram + pc) + 2; break; - case 0xfc: /* LIT */ PUSH(ram[pc++]) break; - case 0xfb: /* LIT2 */ PUSH2(PEEK2(ram + pc)) pc += 2; break; - case 0xfa: /* LITr */ PUSH(ram[pc++]) break; - case 0xf9: /* LIT2r */ PUSH2(PEEK2(ram + pc)) pc += 2; break; + case 0x00: /* BRK */ return 1; + case 0xff: /* JCI */ if(!s->dat[--s->ptr]) { pc += 2; break; } + case 0xfe: /* JMI */ pc += PEEK2(ram + pc) + 2; break; + case 0xfd: /* JSI */ SET(0, 2) PUT2(0, pc + 2) pc += PEEK2(ram + pc) + 2; break; + case 0xfc: /* LIT */ SET(0, 1) PUT(0, ram[pc++]) break; + case 0xfb: /* LIT2 */ SET(0, 2) PUT2(0, PEEK2(ram + pc)) pc += 2; break; + case 0xfa: /* LITr */ SET(0, 1) PUT(0, ram[pc++]) break; + case 0xf9: /* LIT2r */ SET(0, 2) PUT2(0, PEEK2(ram + pc)) pc += 2; break; /* ALU */ case 0x01: /* INC */ t=T; SET(1, 0) PUT(0, t + 1) break; case 0x21: t=T2; SET(2, 0) PUT2(0, t + 1) break; @@ -85,10 +82,10 @@ uxn_eval(Uxn *u, Uint16 pc) case 0x2c: t=T2; SET(2,-2) pc = t; break; case 0x0d: /* JCN */ t=T;n=N; SET(2,-2) if(n) pc += (Sint8)t; break; case 0x2d: t=T2;n=L; SET(3,-3) if(n) pc = t; break; - case 0x0e: /* JSR */ t=T; SET(1,-1) s = &u->rst; PUSH2(pc) pc += (Sint8)t; break; - case 0x2e: t=T2; SET(2,-2) s = &u->rst; PUSH2(pc) pc = t; break; - case 0x0f: /* STH */ t=T; SET(1,-1) s = ins & 0x40 ? &u->wst : &u->rst; PUSH(t) break; - case 0x2f: t=T2; SET(2,-2) s = ins & 0x40 ? &u->wst : &u->rst; PUSH2(t) break; + case 0x0e: /* JSR */ t=T; SET(1,-1) FLIP SET(0,2) PUT2(0, pc) pc += (Sint8)t; break; + case 0x2e: t=T2; SET(2,-2) FLIP SET(0,2) PUT2(0, pc) pc = t; break; + case 0x0f: /* STH */ t=T; SET(1,-1) FLIP SET(0,1) PUT(0, t) break; + case 0x2f: t=T2; SET(2,-2) FLIP SET(0,2) PUT2(0, t) break; case 0x10: /* LDZ */ t=T; SET(1, 0) PUT(0, ram[t]) break; case 0x30: t=T; SET(1, 1) PUT2(0, PEEK2(ram + t)) break; case 0x11: /* STZ */ t=T;n=N; SET(2,-2) ram[t] = n; break; diff --git a/src/uxn.h b/src/uxn.h index 5e4229f..223d22c 100644 --- a/src/uxn.h +++ b/src/uxn.h @@ -37,9 +37,9 @@ typedef struct Uxn { /* required functions */ -extern Uint8 uxn_dei(Uxn *u, Uint8 addr); -extern void uxn_deo(Uxn *u, Uint8 addr); -extern int uxn_halt(Uxn *u, Uint8 instr, Uint8 err, Uint16 addr); +extern Uint8 emu_dei(Uxn *u, Uint8 addr); +extern void emu_deo(Uxn *u, Uint8 addr); +extern int emu_halt(Uxn *u, Uint8 instr, Uint8 err, Uint16 addr); extern Uint16 dei_mask[]; extern Uint16 deo_mask[]; diff --git a/src/uxn11.c b/src/uxn11.c index bdd965e..f603fd8 100644 --- a/src/uxn11.c +++ b/src/uxn11.c @@ -48,7 +48,7 @@ clamp(int val, int min, int max) } Uint8 -uxn_dei(Uxn *u, Uint8 addr) +emu_dei(Uxn *u, Uint8 addr) { switch(addr & 0xf0) { case 0x20: return screen_dei(u, addr); @@ -58,7 +58,7 @@ uxn_dei(Uxn *u, Uint8 addr) } void -uxn_deo(Uxn *u, Uint8 addr) +emu_deo(Uxn *u, Uint8 addr) { Uint8 p = addr & 0x0f, d = addr & 0xf0; switch(d) { diff --git a/src/uxnasm.c b/src/uxnasm.c index b42ba78..df7a394 100644 --- a/src/uxnasm.c +++ b/src/uxnasm.c @@ -35,12 +35,13 @@ typedef struct { typedef struct { Uint8 data[LENGTH]; + Uint8 lambda_stack[0x100], lambda_ptr, lambda_count; + char scope[0x40], lambda[0x10]; unsigned int ptr, length; - Uint16 llen, mlen, rlen; + Uint16 label_len, macro_len, refs_len; Label labels[0x400]; Macro macros[0x100]; Reference refs[0x800]; - char scope[0x40]; } Program; Program p; @@ -87,7 +88,7 @@ static Macro * findmacro(char *name) { int i; - for(i = 0; i < p.mlen; i++) + for(i = 0; i < p.macro_len; i++) if(scmp(p.macros[i].name, name, 0x40)) return &p.macros[i]; return NULL; @@ -97,7 +98,7 @@ static Label * findlabel(char *name) { int i; - for(i = 0; i < p.llen; i++) + for(i = 0; i < p.label_len; i++) if(scmp(p.labels[i].name, name, 0x40)) return &p.labels[i]; return NULL; @@ -139,9 +140,9 @@ makemacro(char *name, FILE *f) return error("Macro name is hex number", name); if(findopcode(name) || scmp(name, "BRK", 4) || !slen(name)) return error("Macro name is invalid", name); - if(p.mlen == 0x100) + if(p.macro_len == 0x100) return error("Macros limit exceeded", name); - m = &p.macros[p.mlen++]; + m = &p.macros[p.macro_len++]; scpy(name, m->name, 0x40); while(fscanf(f, "%63s", word) == 1) { if(word[0] == '{') continue; @@ -165,9 +166,9 @@ makelabel(char *name) return error("Label name is hex number", name); if(findopcode(name) || scmp(name, "BRK", 4) || !slen(name)) return error("Label name is invalid", name); - if(p.llen == 0x400) + if(p.label_len == 0x400) return error("Labels limit exceeded", name); - l = &p.labels[p.llen++]; + l = &p.labels[p.label_len++]; l->addr = p.ptr; l->refs = 0; scpy(name, l->name, 0x40); @@ -179,9 +180,9 @@ makereference(char *scope, char *label, char rune, Uint16 addr) { char subw[0x40], parent[0x40]; Reference *r; - if(p.rlen >= 0x800) + if(p.refs_len >= 0x800) return error("References limit exceeded", label); - r = &p.refs[p.rlen++]; + r = &p.refs[p.refs_len++]; if(label[0] == '&') { if(!sublabel(subw, scope, label + 1)) return error("Invalid sublabel", label); @@ -200,6 +201,15 @@ makereference(char *scope, char *label, char rune, Uint16 addr) return 1; } +static char * +makelambda(int id) +{ + scpy("lambda", p.lambda, 0x07); + p.lambda[6] = '0' + (id >> 0x4); + p.lambda[7] = '0' + (id & 0xf); + return p.lambda; +} + static int writebyte(Uint8 b) { @@ -351,6 +361,14 @@ parse(char *w, FILE *f) while((c = w[++i])) if(!writebyte(c)) return 0; break; + case '{': /* lambda start */ + p.lambda_stack[p.lambda_ptr++] = p.lambda_count; + makereference(p.scope, makelambda(p.lambda_count++), ' ', p.ptr + 1); + return writebyte(0x60) && writeshort(0xffff, 0); + case '}': /* lambda end */ + if(!makelabel(makelambda(p.lambda_stack[--p.lambda_ptr]))) + return error("Invalid label", w); + return writebyte(0x6f); case '[': case ']': if(slen(w) == 1) break; /* else fallthrough */ @@ -384,7 +402,7 @@ resolve(void) Label *l; int i; Uint16 a; - for(i = 0; i < p.rlen; i++) { + for(i = 0; i < p.refs_len; i++) { Reference *r = &p.refs[i]; switch(r->rune) { case '_': @@ -443,7 +461,7 @@ static void review(char *filename) { int i; - for(i = 0; i < p.llen; i++) + for(i = 0; i < p.label_len; i++) if(p.labels[i].name[0] >= 'A' && p.labels[i].name[0] <= 'Z') continue; /* Ignore capitalized labels(devices) */ else if(!p.labels[i].refs) @@ -453,8 +471,8 @@ review(char *filename) filename, p.length - TRIM, (p.length - TRIM) / 652.80, - p.llen, - p.mlen); + p.label_len, + p.macro_len); } static void @@ -467,7 +485,7 @@ writesym(char *filename) return; fp = fopen(scat(scpy(filename, symdst, slen(filename) + 1), ".sym"), "w"); if(fp != NULL) { - for(i = 0; i < p.llen; i++) { + for(i = 0; i < p.label_len; i++) { Uint8 hb = p.labels[i].addr >> 8, lb = p.labels[i].addr & 0xff; fwrite(&hb, 1, 1, fp); fwrite(&lb, 1, 1, fp); diff --git a/src/uxncli.c b/src/uxncli.c index fd0ce81..bbeb519 100644 --- a/src/uxncli.c +++ b/src/uxncli.c @@ -21,7 +21,7 @@ Uint16 deo_mask[] = {0xc028, 0x0300, 0xc028, 0x8000, 0x8000, 0x8000, 0x8000, 0x0 Uint16 dei_mask[] = {0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x07ff, 0x0000, 0x0000, 0x0000}; Uint8 -uxn_dei(Uxn *u, Uint8 addr) +emu_dei(Uxn *u, Uint8 addr) { switch(addr & 0xf0) { case 0xc0: return datetime_dei(u, addr); @@ -30,7 +30,7 @@ uxn_dei(Uxn *u, Uint8 addr) } void -uxn_deo(Uxn *u, Uint8 addr) +emu_deo(Uxn *u, Uint8 addr) { Uint8 p = addr & 0x0f, d = addr & 0xf0; switch(d) {