Browse code

Reorganized operators

neauoire authored on 07/02/2021 18:21:41
Showing 4 changed files
... ...
@@ -18,14 +18,14 @@ BRK
18 18
 
19 19
 @strlen
20 20
 	,0001 ADD^ PEK
21
-	RTS
21
+	RTU
22 22
 
23 23
 @incr
24 24
 	,iterator LDR
25 25
 	,01 ADD
26 26
 	,iterator STR 
27 27
 	,iterator LDR
28
-	RTS
28
+	RTU
29 29
 
30 30
 |c000 @FRAME BRK 
31 31
 |d000 @ERROR BRK 
... ...
@@ -18,14 +18,14 @@ BRK
18 18
 
19 19
 @strlen
20 20
 	,0001 ADD^ PEK
21
-	RTS
21
+	RTU
22 22
 
23 23
 @incr
24 24
 	,iterator LDR
25 25
 	,01 ADD
26 26
 	,iterator STR 
27 27
 	,iterator LDR
28
-	RTS
28
+	RTU
29 29
 
30 30
 |c000 @FRAME BRK 
31 31
 |d000 @ERROR BRK 
... ...
@@ -104,78 +104,75 @@ Uint16 wspeek16(Uint8 o) { return bytes2short(cpu.wst.dat[cpu.wst.ptr - o], cpu.
104 104
 Uint16 rspop16(void) { return cpu.rst.dat[--cpu.rst.ptr]; }
105 105
 void rspush16(Uint16 a) { cpu.rst.dat[cpu.rst.ptr++] = a; }
106 106
 
107
-/* new flexy pop/push */
108
-
107
+/* I/O */
109 108
 void op_brk() { setflag(FLAG_HALT, 1); }
110
-void op_rts() {	cpu.rom.ptr = rspop16(); }
111 109
 void op_lit() { cpu.literal += cpu.rom.dat[cpu.rom.ptr++]; }
112
-
113
-void op_drp() { wspop8(); }
110
+void op_nop() { }
111
+void op_ldr() { wspush8(cpu.ram.dat[wspop16()]); }
112
+void op_str() { cpu.ram.dat[wspop16()] = wspop8(); }
113
+/* Logic */
114
+void op_jmu() { cpu.rom.ptr = wspop16(); }
115
+void op_jmc() { Uint8 a = wspop8(); if(a) op_jmu(); }
116
+void op_jsu() { rspush16(cpu.rom.ptr); cpu.rom.ptr = wspop16(); }
117
+void op_jsc() { Uint8 a = wspop8(); if(a) op_jsu(); }
118
+void op_rtu() {	cpu.rom.ptr = rspop16(); }
119
+void op_rtc() {	/* TODO */ }
120
+/* Stack */
121
+void op_pop() { wspop8(); }
114 122
 void op_dup() { wspush8(wspeek8(1)); }
115 123
 void op_swp() { Uint8 b = wspop8(), a = wspop8(); wspush8(b); wspush8(a); }
116 124
 void op_ovr() { Uint8 a = wspeek8(2); wspush8(a); }
117 125
 void op_rot() { Uint8 c = wspop8(),b = wspop8(),a = wspop8(); wspush8(b); wspush8(c); wspush8(a); }
118
-
119
-void op_jmu() { cpu.rom.ptr = wspop16(); }
120
-void op_jsu() { rspush16(cpu.rom.ptr); cpu.rom.ptr = wspop16(); }
121
-void op_jmc() { Uint8 a = wspop8(); if(a) op_jmu(); }
122
-void op_jsc() { Uint8 a = wspop8(); if(a) op_jsu(); }
123
-
124
-void op_equ() { Uint8 a = wspop8(), b = wspop8(); wspush8(a == b); }
125
-void op_neq() { Uint8 a = wspop8(), b = wspop8(); wspush8(a != b); }
126
-void op_gth() { Uint8 a = wspop8(), b = wspop8(); wspush8(a < b); }
127
-void op_lth() { Uint8 a = wspop8(), b = wspop8(); wspush8(a > b); }
128 126
 void op_and() { Uint8 a = wspop8(), b = wspop8(); wspush8(a & b); }
129 127
 void op_ora() { Uint8 a = wspop8(), b = wspop8(); wspush8(a | b); }
130 128
 void op_rol() { Uint8 a = wspop8(), b = wspop8(); wspush8(a << b); }
131
-void op_ror() { Uint8 a = wspop8(), b = wspop8(); wspush8(a >> b); }
129
+/* Arithmetic */
132 130
 void op_add() { Uint8 a = wspop8(), b = wspop8(); wspush8(a + b); }
133 131
 void op_sub() { Uint8 a = wspop8(), b = wspop8(); wspush8(a - b); }
134 132
 void op_mul() { Uint8 a = wspop8(), b = wspop8(); wspush8(a * b); }
135 133
 void op_div() { Uint8 a = wspop8(), b = wspop8(); wspush8(a / b); }
136
-void op_ldr() { wspush8(cpu.ram.dat[wspop16()]); }
137
-void op_str() { cpu.ram.dat[wspop16()] = wspop8(); }
138
-void op_pek() { wspush8(cpu.rom.dat[wspop16()]); }
139
-void op_pok() { printf("TODO:\n");}
140
-
141
-void op_drp16() { wspop16(); }
134
+void op_equ() { Uint8 a = wspop8(), b = wspop8(); wspush8(a == b); }
135
+void op_neq() { Uint8 a = wspop8(), b = wspop8(); wspush8(a != b); }
136
+void op_gth() { Uint8 a = wspop8(), b = wspop8(); wspush8(a < b); }
137
+void op_lth() { Uint8 a = wspop8(), b = wspop8(); wspush8(a > b); }
138
+/* Stack(16-bits) */
139
+void op_pop16() { wspop16(); }
142 140
 void op_dup16() { wspush16(wspeek16(2)); }
143 141
 void op_swp16() { Uint16 b = wspop16(), a = wspop16(); wspush16(b); wspush16(a); }
144 142
 void op_ovr16() { Uint16 a = wspeek16(4); wspush16(a); }
145 143
 void op_rot16() { Uint16 c = wspop16(), b = wspop16(), a = wspop16(); wspush16(b); wspush16(c); wspush16(a); }
146
-
147
-void op_equ16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a == b); }
148
-void op_neq16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a != b); }
149
-void op_gth16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a < b); }
150
-void op_lth16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a > b); }
151 144
 void op_and16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a & b); }
152 145
 void op_ora16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a | b); }
153 146
 void op_rol16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a << b); }
154
-void op_ror16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a >> b); }
147
+/* Arithmetic(16-bits) */
155 148
 void op_add16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a + b); }
156 149
 void op_sub16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a - b); }
157 150
 void op_mul16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a * b); }
158 151
 void op_div16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a / b); }
152
+void op_equ16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a == b); }
153
+void op_neq16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a != b); }
154
+void op_gth16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a < b); }
155
+void op_lth16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a > b); }
156
+/* remove */
157
+void op_pek() { wspush8(cpu.rom.dat[wspop16()]); }
158
+void op_pok() { printf("TODO:\n");}
159 159
 
160
-void (*ops8[])() = {
161
-	op_brk, op_rts, op_lit, op_drp, op_dup, op_swp, op_ovr, op_rot, 
162
-	op_jmu, op_jsu, op_jmc, op_jsc, op_equ, op_neq, op_gth, op_lth, 
163
-	op_and, op_ora, op_rol, op_ror, op_add, op_sub, op_mul, op_div,
164
-	op_ldr, op_str, op_pek, op_pok, op_brk, op_brk, op_brk, op_brk
165
-};
166
-
167
-void (*ops16[])() = {
168
-	op_brk, op_rts, op_lit, op_drp16, op_dup16, op_swp16, op_ovr16, op_rot16, 
169
-	op_jmu, op_jsu, op_jmc, op_jsc, op_equ, op_neq, op_gth, op_lth, 
170
-	op_and16, op_ora16, op_rol16, op_ror16, op_add16, op_sub16, op_mul16, op_div16,
171
-	op_ldr, op_str, op_pek, op_pok, op_brk, op_brk, op_brk, op_brk
160
+void (*ops[])() = {
161
+	op_brk, op_lit, op_nop, op_nop, op_pek, op_pok, op_ldr, op_str, 
162
+	op_jmu, op_jmc, op_jsu, op_jsc, op_rtu, op_rtc, op_nop, op_nop, 
163
+	op_pop, op_dup, op_swp, op_ovr, op_rot, op_and, op_ora, op_rol,
164
+	op_add, op_sub, op_mul, op_div, op_equ, op_neq, op_gth, op_lth,
165
+	op_pop16, op_dup16, op_swp16, op_ovr16, op_rot16, op_and16, op_ora16, op_rol16,
166
+	op_add16, op_sub16, op_mul16, op_div16, op_equ16, op_neq16, op_gth16, op_lth16
172 167
 };
173 168
 
174 169
 Uint8 opr[][2] = { /* todo: 16 bits mode */
175
-	{0,0}, {0,0}, {0,0}, {1,0}, {0,1}, {1,1}, {0,1}, {3,3},
176
-	{2,0}, {2,0}, {2,0}, {2,0}, {2,1}, {2,1}, {2,1}, {2,1},
177
-	{1,0}, {1,0}, {1,0}, {1,0}, {2,1}, {0,0}, {0,0}, {0,0},
178
-	{2,1}, {3,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}
170
+	{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
171
+	{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
172
+	{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
173
+	{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
174
+	{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
175
+	{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}
179 176
 };
180 177
 
181 178
 /* clang-format on */
... ...
@@ -231,14 +228,11 @@ eval(void)
231 228
 	setflag(FLAG_SIGN, (instr >> 6) & 1);
232 229
 	if((instr >> 7) & 1)
233 230
 		printf("Unused flag: %02x\n", instr);
234
-	/* TODO: setflag(FLAG_B, (instr >> 6) & 1); */
235
-	/* TODO: setflag(FLAG_C, (instr >> 7) & 1); */
236 231
 	if(cpu.wst.ptr < opr[op][0])
237 232
 		return error("Stack underflow", op);
238 233
 	if(getflag(FLAG_SHORT))
239
-		(*ops16[op])();
240
-	else
241
-		(*ops8[op])();
234
+		op += 16;
235
+	(*ops[op])();
242 236
 	cpu.counter++;
243 237
 	return 1;
244 238
 }
... ...
@@ -26,98 +26,27 @@ typedef struct {
26 26
 
27 27
 int labelslen;
28 28
 Label labels[256];
29
+Program p;
29 30
 
30 31
 /* clang-format off */
31 32
 
32 33
 char ops[][4] = {
33
-	"BRK", "RTS", "LIT", "POP", "DUP", "SWP", "OVR", "ROT",
34
-	"JMU", "JSU", "JMC", "JSC", "EQU", "NEQ", "GTH", "LTH",
35
-	"AND", "ORA", "ROL", "ROR", "ADD", "SUB", "MUL", "DIV",
36
-	"LDR", "STR", "PEK", "POK", "---", "---", "---", "---"
34
+	"BRK", "LIT", "---", "---", "PEK", "POK", "LDR", "STR",
35
+	"JMU", "JMC", "JSU", "JSC", "RTU", "RTC", "---", "---",
36
+	"POP", "DUP", "SWP", "OVR", "ROT", "AND", "ORA", "ROL",
37
+	"ADD", "SUB", "MUL", "DIV", "EQU", "NEQ", "GTH", "LTH"
37 38
 };
38 39
 
39
-/* clang-format on */
40
-
41
-Program p;
40
+int scmp(char *a, char *b) { int i = 0; while(a[i] == b[i]) if(!a[i++]) return 1; return 0; } /* string compare */
41
+int slen(char *s) { int i = 0; while(s[i] && s[++i]) ; return i; } /* string length */
42
+int sihx(char *s) { int i = 0; char c; while((c = s[i++])) if(!(c >= '0' && c <= '9') && !(c >= 'a' && c <= 'f') && !(c >= 'A' && c <= 'F')) return 0; return 1; } /* string is hexadecimal */
43
+int shex(char *s) { int n = 0, i = 0; char c; while((c = s[i++])) if(c >= '0' && c <= '9') n = n * 16 + (c - '0'); else if(c >= 'A' && c <= 'F') n = n * 16 + 10 + (c - 'A'); else if(c >= 'a' && c <= 'f') n = n * 16 + 10 + (c - 'a'); return n; } /* string to num */
44
+int cmnt(char *w, int *skip) { if(w[0] == ')') { *skip = 0; return 1; } if(w[0] == '(') *skip = 1; if(*skip) return 1; return 0; } /* comment helper */
45
+char *scpy(char *src, char *dst, int len) { int i = 0; while((dst[i] = src[i]) && i < len - 2) i++; dst[i + 1] = '\0'; return dst; } /* string copy */
42 46
 
43 47
 #pragma mark - Helpers
44 48
 
45
-int
46
-scmp(char *a, char *b) /* string compare */
47
-{
48
-	int i = 0;
49
-	while(a[i] == b[i])
50
-		if(!a[i++])
51
-			return 1;
52
-	return 0;
53
-}
54
-
55
-char *
56
-scpy(char *src, char *dst, int len) /* string copy */
57
-{
58
-	int i = 0;
59
-	while((dst[i] = src[i]) && i < len - 2)
60
-		i++;
61
-	dst[i + 1] = '\0';
62
-	return dst;
63
-}
64
-
65
-int
66
-slen(char *s) /* string length */
67
-{
68
-	int i = 0;
69
-	while(s[i] && s[++i])
70
-		;
71
-	return i;
72
-}
73
-
74
-char *
75
-suca(char *s) /* string to uppercase */
76
-{
77
-	int i = 0;
78
-	char c;
79
-	while((c = s[i]))
80
-		s[i++] = c >= 'a' && c <= 'z' ? c - ('a' - 'A') : c;
81
-	return s;
82
-}
83
-
84
-int
85
-sihx(char *s) /* string is hexadecimal */
86
-{
87
-	int i = 0;
88
-	char c;
89
-	while((c = s[i++]))
90
-		if(!(c >= '0' && c <= '9') && !(c >= 'a' && c <= 'f') && !(c >= 'A' && c <= 'F'))
91
-			return 0;
92
-	return 1;
93
-}
94
-
95
-int
96
-shex(char *s) /* string to num */
97
-{
98
-	int n = 0, i = 0;
99
-	char c;
100
-	while((c = s[i++]))
101
-		if(c >= '0' && c <= '9')
102
-			n = n * 16 + (c - '0');
103
-		else if(c >= 'A' && c <= 'F')
104
-			n = n * 16 + 10 + (c - 'A');
105
-		else if(c >= 'a' && c <= 'f')
106
-			n = n * 16 + 10 + (c - 'a');
107
-	return n;
108
-}
109
-
110
-int
111
-iscomment(char *w, int *skip)
112
-{
113
-	if(w[0] == ')') {
114
-		*skip = 0;
115
-		return 1;
116
-	}
117
-	if(w[0] == '(') *skip = 1;
118
-	if(*skip) return 1;
119
-	return 0;
120
-}
49
+/* clang-format on */
121 50
 
122 51
 #pragma mark - I/O
123 52
 
... ...
@@ -125,7 +54,7 @@ void
125 54
 pushbyte(Uint8 b, int lit)
126 55
 {
127 56
 	if(lit) {
128
-		pushbyte(0x02, 0);
57
+		pushbyte(0x01, 0);
129 58
 		pushbyte(0x01, 0);
130 59
 	}
131 60
 	p.data[p.ptr++] = b;
... ...
@@ -135,7 +64,7 @@ void
135 64
 pushshort(Uint16 s, int lit)
136 65
 {
137 66
 	if(lit) {
138
-		pushbyte(0x02, 0);
67
+		pushbyte(0x01, 0);
139 68
 		pushbyte(0x02, 0);
140 69
 	}
141 70
 	pushbyte((s >> 8) & 0xff, 0);
... ...
@@ -143,10 +72,10 @@ pushshort(Uint16 s, int lit)
143 72
 }
144 73
 
145 74
 void
146
-pushword(char *w)
75
+pushtext(char *w)
147 76
 {
148 77
 	int i = slen(w);
149
-	pushbyte(0x02, 0);
78
+	pushbyte(0x01, 0);
150 79
 	pushbyte(slen(w), 0);
151 80
 	while(i > 0)
152 81
 		pushbyte(w[--i], 0);
... ...
@@ -163,19 +92,18 @@ findlabel(char *s)
163 92
 }
164 93
 
165 94
 Uint8
166
-findop(char *s)
95
+findoperator(char *s)
167 96
 {
168 97
 	int i;
169
-	for(i = 0; i < 32; ++i) {
98
+	for(i = 0; i < 0x20; ++i) {
170 99
 		int m = 0;
171
-		if(ops[i][0] != s[0]) continue;
172
-		if(ops[i][1] != s[1]) continue;
173
-		if(ops[i][2] != s[2]) continue;
100
+		char *o = ops[i];
101
+		if(o[0] != s[0] || o[1] != s[1] || o[2] != s[2])
102
+			continue;
174 103
 		while(s[3 + m]) {
175
-			char c = s[3 + m];
176
-			if(c == '^') i |= (1 << 5); /* mode: 16 bits */
177
-			if(c == '~') i |= (1 << 6); /* mode: signed */
178
-			if(c == '&') i |= (1 << 7); /* mode: unused */
104
+			if(s[3 + m] == '^') i |= (1 << 5); /* mode: 16 bits */
105
+			if(s[3 + m] == '~') i |= (1 << 6); /* mode: signed */
106
+			if(s[3 + m] == '&') i |= (1 << 7); /* mode: unused */
179 107
 			m++;
180 108
 		}
181 109
 		return i;
... ...
@@ -193,14 +121,14 @@ error(char *name, char *id)
193 121
 }
194 122
 
195 123
 int
196
-makelabel(char *id, Uint16 addr)
124
+makelabel(char *name, Uint16 addr)
197 125
 {
198 126
 	Label *l;
199
-	if(findlabel(id))
200
-		return error("Label duplicate", id);
127
+	if(findlabel(name))
128
+		return error("Label duplicate", name);
201 129
 	l = &labels[labelslen++];
202
-	scpy(id, l->name, 64);
203 130
 	l->addr = addr;
131
+	scpy(name, l->name, 64);
204 132
 	printf("New label: %s[0x%02x]\n", l->name, l->addr);
205 133
 	return 1;
206 134
 }
... ...
@@ -220,7 +148,8 @@ pass1(FILE *f)
220 148
 	Uint16 addr = 0;
221 149
 	char w[64];
222 150
 	while(fscanf(f, "%s", w) == 1) {
223
-		if(iscomment(w, &skip)) continue;
151
+		if(cmnt(w, &skip))
152
+			continue;
224 153
 		if(w[0] == '@' && !makelabel(w + 1, addr))
225 154
 			return error("Pass1 failed", w);
226 155
 		if(w[0] == ';' && !makelabel(w + 1, vars++))
... ...
@@ -231,23 +160,19 @@ pass1(FILE *f)
231 160
 			else
232 161
 				continue;
233 162
 		}
234
-		/* move addr ptr */
235
-		if(findop(w) || scmp(w, "BRK"))
163
+		if(findoperator(w) || scmp(w, "BRK"))
236 164
 			addr += 1;
237
-		else if(w[0] == '|')
238
-			addr = shex(w + 1);
239
-		else if(w[0] == '@')
240
-			addr += 0;
241
-		else if(w[0] == ';')
242
-			addr += 0;
243
-		else if(w[0] == '.')
244
-			addr += 2;
245
-		else if(w[0] == '"')
246
-			addr += slen(w + 1) + 2;
247
-		else if(w[0] == ',')
248
-			addr += 2 + (sihx(w + 1) && slen(w + 1) == 2 ? 1 : 2);
249
-		else
250
-			return error("Unknown label", w);
165
+		else {
166
+			switch(w[0]) {
167
+			case '|': addr = shex(w + 1); break;
168
+			case '@':
169
+			case ';': break;
170
+			case '.': addr += 2; break;
171
+			case '"': addr += slen(w + 1) + 2; break;
172
+			case ',': addr += 2 + (sihx(w + 1) && slen(w + 1) == 2 ? 1 : 2); break;
173
+			default: return error("Unknown label", w);
174
+			}
175
+		}
251 176
 	}
252 177
 	rewind(f);
253 178
 	return 1;
... ...
@@ -263,16 +188,16 @@ pass2(FILE *f)
263 188
 		Label *l;
264 189
 		if(w[0] == '@') continue;
265 190
 		if(w[0] == ';') continue;
266
-		if(iscomment(w, &skip)) continue;
191
+		if(cmnt(w, &skip)) continue;
267 192
 		if(w[0] == '|')
268 193
 			p.ptr = shex(w + 1);
269 194
 		else if(w[0] == ':')
270 195
 			fscanf(f, "%s", w);
271 196
 		else if(w[0] == '"')
272
-			pushword(w + 1);
197
+			pushtext(w + 1);
273 198
 		else if((l = findlabel(w + 1)))
274 199
 			pushshort(l->addr, w[0] == ',');
275
-		else if((op = findop(w)) || scmp(w, "BRK"))
200
+		else if((op = findoperator(w)) || scmp(w, "BRK"))
276 201
 			pushbyte(op, 0);
277 202
 		else if(sihx(w + 1) && slen(w + 1) == 2)
278 203
 			pushbyte(shex(w + 1), w[0] == ',');