commit 5c2c060

neauoire  ·  2023-08-02 03:32:42 +0000 UTC
parent b49ded3
Brought the latest changes over
6 files changed,  +68, -53
+1, -1
1@@ -125,7 +125,7 @@ system_deo(Uxn *u, Uint8 *d, Uint8 port)
2 /* Errors */
3 
4 int
5-uxn_halt(Uxn *u, Uint8 instr, Uint8 err, Uint16 addr)
6+emu_halt(Uxn *u, Uint8 instr, Uint8 err, Uint16 addr)
7 {
8 	Uint8 *d = &u->dev[0];
9 	Uint16 handler = PEEK2(d);
+27, -30
 1@@ -11,35 +11,32 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 2 WITH REGARD TO THIS SOFTWARE.
 3 */
 4 
 5+/* Registers
 6+[ . ][ . ][ . ][ L ][ N ][ T ] <
 7+[ . ][ . ][ . ][   H2   ][ T ] <
 8+[   L2   ][   N2   ][   T2   ] <
 9+*/
10+
11 #define T s->dat[s->ptr - 1]
12 #define N s->dat[s->ptr - 2]
13 #define L s->dat[s->ptr - 3]
14-#define H2 PEEK2(s->dat + s->ptr - 3)
15 #define T2 PEEK2(s->dat + s->ptr - 2)
16+#define H2 PEEK2(s->dat + s->ptr - 3)
17 #define N2 PEEK2(s->dat + s->ptr - 4)
18 #define L2 PEEK2(s->dat + s->ptr - 6)
19 
20-/* Registers
21-
22-[ . ][ . ][ . ][ L ][ N ][ T ] <
23-[ . ][ . ][ . ][   H2   ][ T ] <
24-[   L2   ][   N2   ][   T2   ] <
25-
26-*/
27-
28-#define HALT(c) { return uxn_halt(u, ins, (c), pc - 1); }
29-#define SET(mul, add) { if(mul > s->ptr) HALT(1) tmp = (mul & k) + add + s->ptr; if(tmp > 254) HALT(2) s->ptr = tmp; }
30-#define PUT(o, v) { s->dat[(Uint8)(s->ptr - 1 - (o))] = (v); }
31-#define PUT2(o, v) { tmp = (v); s->dat[(Uint8)(s->ptr - o - 2)] = tmp >> 8; s->dat[(Uint8)(s->ptr - o - 1)] = tmp; }
32-#define PUSH(v) { if(s->ptr > 254) HALT(2) s->dat[s->ptr++] = (v); }
33-#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; }
34-#define DEO(a, b) { u->dev[(a)] = (b); if((deo_mask[(a) >> 4] >> ((a) & 0xf)) & 0x1) uxn_deo(u, (a)); }
35-#define DEI(a, b) { PUT((a), ((dei_mask[(b) >> 4] >> ((b) & 0xf)) & 0x1) ? uxn_dei(u, (b)) : u->dev[(b)]) }
36+#define HALT(c)    { return emu_halt(u, ins, (c), pc - 1); }
37+#define FLIP       { s = ins & 0x40 ? &u->wst : &u->rst; }
38+#define SET(x, y)  { if(x > s->ptr) HALT(1) tmp = (x & k) + y + s->ptr; if(tmp > 254) HALT(2) s->ptr = tmp; }
39+#define PUT(o, v)  { s->dat[(s->ptr - 1 - (o))] = (v); }
40+#define PUT2(o, v) { tmp = (v); POKE2(s->dat + (s->ptr - o - 2), tmp); }
41+#define DEO(a, b)  { u->dev[(a)] = (b); if((deo_mask[(a) >> 4] >> ((a) & 0xf)) & 0x1) emu_deo(u, (a)); }
42+#define DEI(a, b)  { PUT((a), ((dei_mask[(b) >> 4] >> ((b) & 0xf)) & 0x1) ? emu_dei(u, (b)) : u->dev[(b)]) }
43 
44 int
45 uxn_eval(Uxn *u, Uint16 pc)
46 {
47-	int t, n, l, k, tmp, opc, ins;
48+	int t, n, l, k, tmp, ins, opc;
49 	Uint8 *ram = u->ram;
50 	Stack *s;
51 	if(!pc || u->dev[0x0f]) return 0;
52@@ -50,14 +47,14 @@ uxn_eval(Uxn *u, Uint16 pc)
53 		opc = !(ins & 0x1f) ? (0 - (ins >> 5)) & 0xff : ins & 0x3f;
54 		switch(opc) {
55 			/* IMM */
56-			case 0x00: /* BRK   */ return 1;
57-			case 0xff: /* JCI   */ pc += !!s->dat[--s->ptr] * PEEK2(ram + pc) + 2; break;
58-			case 0xfe: /* JMI   */ pc += PEEK2(ram + pc) + 2; break;
59-			case 0xfd: /* JSI   */ s = &u->rst; PUSH2(pc + 2) pc += PEEK2(ram + pc) + 2; break;
60-			case 0xfc: /* LIT   */ PUSH(ram[pc++]) break;
61-			case 0xfb: /* LIT2  */ PUSH2(PEEK2(ram + pc)) pc += 2; break;
62-			case 0xfa: /* LITr  */ PUSH(ram[pc++]) break;
63-			case 0xf9: /* LIT2r */ PUSH2(PEEK2(ram + pc)) pc += 2; break;
64+			case 0x00: /* BRK   */                          return 1;
65+			case 0xff: /* JCI   */                          if(!s->dat[--s->ptr]) { pc += 2; break; }
66+			case 0xfe: /* JMI   */                          pc += PEEK2(ram + pc) + 2; break;
67+			case 0xfd: /* JSI   */                SET(0, 2) PUT2(0, pc + 2) pc += PEEK2(ram + pc) + 2; break;
68+			case 0xfc: /* LIT   */                SET(0, 1) PUT(0, ram[pc++]) break;
69+			case 0xfb: /* LIT2  */                SET(0, 2) PUT2(0, PEEK2(ram + pc)) pc += 2; break;
70+			case 0xfa: /* LITr  */                SET(0, 1) PUT(0, ram[pc++]) break;
71+			case 0xf9: /* LIT2r */                SET(0, 2) PUT2(0, PEEK2(ram + pc)) pc += 2; break;
72 			/* ALU */
73 			case 0x01: /* INC  */ t=T;            SET(1, 0) PUT(0, t + 1) break;
74 			case 0x21:            t=T2;           SET(2, 0) PUT2(0, t + 1) break;
75@@ -85,10 +82,10 @@ uxn_eval(Uxn *u, Uint16 pc)
76 			case 0x2c:            t=T2;           SET(2,-2) pc = t; break;
77 			case 0x0d: /* JCN  */ t=T;n=N;        SET(2,-2) if(n) pc += (Sint8)t; break;
78 			case 0x2d:            t=T2;n=L;       SET(3,-3) if(n) pc = t; break;
79-			case 0x0e: /* JSR  */ t=T;            SET(1,-1) s = &u->rst; PUSH2(pc) pc += (Sint8)t; break;
80-			case 0x2e:            t=T2;           SET(2,-2) s = &u->rst; PUSH2(pc) pc = t; break;
81-			case 0x0f: /* STH  */ t=T;            SET(1,-1) s = ins & 0x40 ? &u->wst : &u->rst; PUSH(t) break;
82-			case 0x2f:            t=T2;           SET(2,-2) s = ins & 0x40 ? &u->wst : &u->rst; PUSH2(t) break;
83+			case 0x0e: /* JSR  */ t=T;            SET(1,-1) FLIP SET(0,2) PUT2(0, pc) pc += (Sint8)t; break;
84+			case 0x2e:            t=T2;           SET(2,-2) FLIP SET(0,2) PUT2(0, pc) pc = t; break;
85+			case 0x0f: /* STH  */ t=T;            SET(1,-1) FLIP SET(0,1) PUT(0, t) break;
86+			case 0x2f:            t=T2;           SET(2,-2) FLIP SET(0,2) PUT2(0, t) break;
87 			case 0x10: /* LDZ  */ t=T;            SET(1, 0) PUT(0, ram[t]) break;
88 			case 0x30:            t=T;            SET(1, 1) PUT2(0, PEEK2(ram + t)) break;
89 			case 0x11: /* STZ  */ t=T;n=N;        SET(2,-2) ram[t] = n; break;
+3, -3
 1@@ -37,9 +37,9 @@ typedef struct Uxn {
 2 
 3 /* required functions */
 4 
 5-extern Uint8 uxn_dei(Uxn *u, Uint8 addr);
 6-extern void uxn_deo(Uxn *u, Uint8 addr);
 7-extern int uxn_halt(Uxn *u, Uint8 instr, Uint8 err, Uint16 addr);
 8+extern Uint8 emu_dei(Uxn *u, Uint8 addr);
 9+extern void emu_deo(Uxn *u, Uint8 addr);
10+extern int emu_halt(Uxn *u, Uint8 instr, Uint8 err, Uint16 addr);
11 extern Uint16 dei_mask[];
12 extern Uint16 deo_mask[];
13 
+2, -2
 1@@ -48,7 +48,7 @@ clamp(int val, int min, int max)
 2 }
 3 
 4 Uint8
 5-uxn_dei(Uxn *u, Uint8 addr)
 6+emu_dei(Uxn *u, Uint8 addr)
 7 {
 8 	switch(addr & 0xf0) {
 9 	case 0x20: return screen_dei(u, addr);
10@@ -58,7 +58,7 @@ uxn_dei(Uxn *u, Uint8 addr)
11 }
12 
13 void
14-uxn_deo(Uxn *u, Uint8 addr)
15+emu_deo(Uxn *u, Uint8 addr)
16 {
17 	Uint8 p = addr & 0x0f, d = addr & 0xf0;
18 	switch(d) {
+33, -15
  1@@ -35,12 +35,13 @@ typedef struct {
  2 
  3 typedef struct {
  4 	Uint8 data[LENGTH];
  5+	Uint8 lambda_stack[0x100], lambda_ptr, lambda_count;
  6+	char scope[0x40], lambda[0x10];
  7 	unsigned int ptr, length;
  8-	Uint16 llen, mlen, rlen;
  9+	Uint16 label_len, macro_len, refs_len;
 10 	Label labels[0x400];
 11 	Macro macros[0x100];
 12 	Reference refs[0x800];
 13-	char scope[0x40];
 14 } Program;
 15 
 16 Program p;
 17@@ -87,7 +88,7 @@ static Macro *
 18 findmacro(char *name)
 19 {
 20 	int i;
 21-	for(i = 0; i < p.mlen; i++)
 22+	for(i = 0; i < p.macro_len; i++)
 23 		if(scmp(p.macros[i].name, name, 0x40))
 24 			return &p.macros[i];
 25 	return NULL;
 26@@ -97,7 +98,7 @@ static Label *
 27 findlabel(char *name)
 28 {
 29 	int i;
 30-	for(i = 0; i < p.llen; i++)
 31+	for(i = 0; i < p.label_len; i++)
 32 		if(scmp(p.labels[i].name, name, 0x40))
 33 			return &p.labels[i];
 34 	return NULL;
 35@@ -139,9 +140,9 @@ makemacro(char *name, FILE *f)
 36 		return error("Macro name is hex number", name);
 37 	if(findopcode(name) || scmp(name, "BRK", 4) || !slen(name))
 38 		return error("Macro name is invalid", name);
 39-	if(p.mlen == 0x100)
 40+	if(p.macro_len == 0x100)
 41 		return error("Macros limit exceeded", name);
 42-	m = &p.macros[p.mlen++];
 43+	m = &p.macros[p.macro_len++];
 44 	scpy(name, m->name, 0x40);
 45 	while(fscanf(f, "%63s", word) == 1) {
 46 		if(word[0] == '{') continue;
 47@@ -165,9 +166,9 @@ makelabel(char *name)
 48 		return error("Label name is hex number", name);
 49 	if(findopcode(name) || scmp(name, "BRK", 4) || !slen(name))
 50 		return error("Label name is invalid", name);
 51-	if(p.llen == 0x400)
 52+	if(p.label_len == 0x400)
 53 		return error("Labels limit exceeded", name);
 54-	l = &p.labels[p.llen++];
 55+	l = &p.labels[p.label_len++];
 56 	l->addr = p.ptr;
 57 	l->refs = 0;
 58 	scpy(name, l->name, 0x40);
 59@@ -179,9 +180,9 @@ makereference(char *scope, char *label, char rune, Uint16 addr)
 60 {
 61 	char subw[0x40], parent[0x40];
 62 	Reference *r;
 63-	if(p.rlen >= 0x800)
 64+	if(p.refs_len >= 0x800)
 65 		return error("References limit exceeded", label);
 66-	r = &p.refs[p.rlen++];
 67+	r = &p.refs[p.refs_len++];
 68 	if(label[0] == '&') {
 69 		if(!sublabel(subw, scope, label + 1))
 70 			return error("Invalid sublabel", label);
 71@@ -200,6 +201,15 @@ makereference(char *scope, char *label, char rune, Uint16 addr)
 72 	return 1;
 73 }
 74 
 75+static char *
 76+makelambda(int id)
 77+{
 78+	scpy("lambda", p.lambda, 0x07);
 79+	p.lambda[6] = '0' + (id >> 0x4);
 80+	p.lambda[7] = '0' + (id & 0xf);
 81+	return p.lambda;
 82+}
 83+
 84 static int
 85 writebyte(Uint8 b)
 86 {
 87@@ -351,6 +361,14 @@ parse(char *w, FILE *f)
 88 		while((c = w[++i]))
 89 			if(!writebyte(c)) return 0;
 90 		break;
 91+	case '{': /* lambda start */
 92+		p.lambda_stack[p.lambda_ptr++] = p.lambda_count;
 93+		makereference(p.scope, makelambda(p.lambda_count++), ' ', p.ptr + 1);
 94+		return writebyte(0x60) && writeshort(0xffff, 0);
 95+	case '}': /* lambda end */
 96+		if(!makelabel(makelambda(p.lambda_stack[--p.lambda_ptr])))
 97+			return error("Invalid label", w);
 98+		return writebyte(0x6f);
 99 	case '[':
100 	case ']':
101 		if(slen(w) == 1) break; /* else fallthrough */
102@@ -384,7 +402,7 @@ resolve(void)
103 	Label *l;
104 	int i;
105 	Uint16 a;
106-	for(i = 0; i < p.rlen; i++) {
107+	for(i = 0; i < p.refs_len; i++) {
108 		Reference *r = &p.refs[i];
109 		switch(r->rune) {
110 		case '_':
111@@ -443,7 +461,7 @@ static void
112 review(char *filename)
113 {
114 	int i;
115-	for(i = 0; i < p.llen; i++)
116+	for(i = 0; i < p.label_len; i++)
117 		if(p.labels[i].name[0] >= 'A' && p.labels[i].name[0] <= 'Z')
118 			continue; /* Ignore capitalized labels(devices) */
119 		else if(!p.labels[i].refs)
120@@ -453,8 +471,8 @@ review(char *filename)
121 		filename,
122 		p.length - TRIM,
123 		(p.length - TRIM) / 652.80,
124-		p.llen,
125-		p.mlen);
126+		p.label_len,
127+		p.macro_len);
128 }
129 
130 static void
131@@ -467,7 +485,7 @@ writesym(char *filename)
132 		return;
133 	fp = fopen(scat(scpy(filename, symdst, slen(filename) + 1), ".sym"), "w");
134 	if(fp != NULL) {
135-		for(i = 0; i < p.llen; i++) {
136+		for(i = 0; i < p.label_len; i++) {
137 			Uint8 hb = p.labels[i].addr >> 8, lb = p.labels[i].addr & 0xff;
138 			fwrite(&hb, 1, 1, fp);
139 			fwrite(&lb, 1, 1, fp);
+2, -2
 1@@ -21,7 +21,7 @@ Uint16 deo_mask[] = {0xc028, 0x0300, 0xc028, 0x8000, 0x8000, 0x8000, 0x8000, 0x0
 2 Uint16 dei_mask[] = {0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x07ff, 0x0000, 0x0000, 0x0000};
 3 
 4 Uint8
 5-uxn_dei(Uxn *u, Uint8 addr)
 6+emu_dei(Uxn *u, Uint8 addr)
 7 {
 8 	switch(addr & 0xf0) {
 9 	case 0xc0: return datetime_dei(u, addr);
10@@ -30,7 +30,7 @@ uxn_dei(Uxn *u, Uint8 addr)
11 }
12 
13 void
14-uxn_deo(Uxn *u, Uint8 addr)
15+emu_deo(Uxn *u, Uint8 addr)
16 {
17 	Uint8 p = addr & 0x0f, d = addr & 0xf0;
18 	switch(d) {