Browse code

CPU Cleanup

neauoire authored on 08/02/2021 19:44:37
Showing 11 changed files
... ...
@@ -14,5 +14,5 @@ cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werr
14 14
 cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined uxn.c -o uxn
15 15
 
16 16
 # run
17
-./uxnasm examples/condjump.usm boot.rom
17
+./uxnasm examples/test.usm boot.rom
18 18
 ./uxn boot.rom
19 19
deleted file mode 100644
... ...
@@ -1,6 +0,0 @@
1
-< arithmetic >
2
-
3
-0203 LTH .true JMC
4
-
5
-:false ee BRK
6
-:true ff BRK
... ...
@@ -26,8 +26,6 @@
26 26
 ,1234 ,1233 GTH^ #0e STR
27 27
 ,1234 ,1235 LTH^ #0f STR
28 28
 
29
-,ef ,ee EQU ,12 ,01 ROT ADD?
30
-
31 29
 |c000 @FRAME BRK 
32 30
 |d000 @ERROR BRK 
33 31
 |FFFA .RESET .FRAME .ERROR
... ...
@@ -1,30 +1,10 @@
1
-( blank project )
1
+( blank )
2 2
 
3
-;variable1
4
-:constant1 9abc
3
+;iterator
4
+:dev1r FFF0
5
+:dev1w FFF1
5 6
 
6
-|0100 ( -------------------------------- )
7
-
8
-@RESET
9
-
10
-,abcd
11
-
12
-BRK ( RESET-END )
13
-
14
-|c000 ( -------------------------------- )
15
-
16
-@FRAME ( FRAME-START )
17
-
18
-,abcd
19
-
20
-BRK ( FRAME-END )
21
-
22
-|d000 ( -------------------------------- )
23
-
24
-@ERROR BRK
25
-
26
-|FFFA ( -------------------------------- )
27
-
28
-.RESET
29
-.FRAME
30
-.ERROR
7
+|0100 @RESET BRK
8
+|c000 @FRAME BRK 
9
+|d000 @ERROR BRK 
10
+|FFFA .RESET .FRAME .ERROR
31 11
deleted file mode 100644
... ...
@@ -1,7 +0,0 @@
1
-< jump >
2
-
3
-.end JMI BRK
4
-
5
-:end 
6
-	ff
7
-	BRK
... ...
@@ -1,8 +1,24 @@
1
-< loop >
1
+( loop )
2 2
 
3
-01 .loop JSI ffff BRK
3
+|0100 @RESET
4 4
 
5
-:loop 
6
-	01 ADD
7
-	0f NEQ .loop JMC
8
-	RTS
5
+( increment value on stack )
6
+
7
+,00
8
+
9
+@loop1
10
+	,01 ADD DUP
11
+	,ff NEQ ,loop1 ROT JMP? POP^
12
+
13
+( increment value in memory )
14
+
15
+@loop2
16
+	#00 LDR ,01 ADD #00 STR
17
+	#00 LDR 
18
+	,ff NEQ ,loop2 ROT JMP? POP^
19
+
20
+BRK
21
+
22
+|c000 @FRAME BRK 
23
+|d000 @ERROR BRK 
24
+|FFFA .RESET .FRAME .ERROR
9 25
deleted file mode 100644
... ...
@@ -1,2 +0,0 @@
1
-< subcond >
2
-
3 0
deleted file mode 100644
... ...
@@ -1,9 +0,0 @@
1
-< subroutines >
2
-
3
-.part1 JSI 44 BRK
4
-
5
-:part1 11 .part2 JSI RTS
6
-
7
-:part2 22 .part3 JSI RTS
8
-
9
-:part3 33 RTS
10 0
\ No newline at end of file
11 1
similarity index 74%
12 2
rename from examples/core.usm
13 3
rename to examples/test.usm
... ...
@@ -1,4 +1,4 @@
1
-( hello world )
1
+( my default test file )
2 2
 
3 3
 ;iterator
4 4
 :dev1r FFF0
... ...
@@ -6,6 +6,8 @@
6 6
 
7 7
 |0100 @RESET
8 8
 
9
+,01 ,02 ADD 
10
+
9 11
 |c000 @FRAME BRK 
10 12
 |d000 @ERROR BRK 
11 13
 |FFFA .RESET .FRAME .ERROR
12 14
deleted file mode 100644
... ...
@@ -1,11 +0,0 @@
1
-< vectors >
2
-
3
-:RESET BRK
4
-:FRAME BRK
5
-:ERROR BRK
6
-
7
-@FFFA < vectors >
8
-
9
-.RESET
10
-.FRAME
11
-.ERROR
12 0
\ No newline at end of file
... ...
@@ -158,13 +158,13 @@ void (*ops[])() = {
158 158
 	op_add16, op_sub16, op_mul16, op_div16, op_equ16, op_neq16, op_gth16, op_lth16
159 159
 };
160 160
 
161
-Uint8 opr[][2] = { /* TODO */
162
-	{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
163
-	{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
164
-	{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
165
-	{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
166
-	{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
167
-	{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}
161
+Uint8 opr[][2] = { 
162
+	{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {2,1}, {3,0},
163
+	{2,0}, {2,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
164
+	{1,0}, {1,2}, {2,2}, {3,3}, {3,3}, {2,1}, {2,1}, {2,1},
165
+	{2,1}, {2,1}, {2,1}, {2,1}, {2,1}, {2,1}, {2,1}, {2,1},
166
+	{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, /* TODO */
167
+	{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}  /* TODO */
168 168
 };
169 169
 
170 170
 /* clang-format on */
... ...
@@ -177,54 +177,52 @@ error(char *name, int id)
177 177
 }
178 178
 
179 179
 int
180
-device1(Uint8 *read, Uint8 *write)
180
+doliteral(Uint8 instr)
181 181
 {
182
-	printf("%c", *write);
183
-	*write = 0;
184
-	(void)read;
185
-	return 0;
182
+	if(cpu.wst.ptr >= 255)
183
+		return error("Stack overflow", instr);
184
+	wspush8(instr);
185
+	cpu.literal--;
186
+	return 1;
186 187
 }
187 188
 
188
-void
189
-opc(Uint8 src, Uint8 *op)
189
+int
190
+dodevices(void) /* experimental */
190 191
 {
191
-	*op = src;
192
-	*op &= ~(1 << 5);
193
-	*op &= ~(1 << 6);
194
-	*op &= ~(1 << 7);
192
+	if(cpu.ram.dat[0xfff1]) {
193
+		printf("%c", cpu.ram.dat[0xfff1]);
194
+		cpu.ram.dat[0xfff1] = 0x00;
195
+	}
196
+	return 1;
195 197
 }
196 198
 
197 199
 int
198
-eval(void)
200
+doopcode(Uint8 instr)
199 201
 {
200
-	Uint8 op, instr = cpu.ram.dat[cpu.ram.ptr++];
201
-	/* when literal */
202
-	if(cpu.literal > 0) {
203
-		wspush8(instr);
204
-		cpu.literal--;
205
-		return 1;
206
-	}
207
-	/* when opcode */
208
-	opc(instr, &op);
202
+	Uint8 op = instr & 0x1f;
209 203
 	setflag(FLAG_SHORT, (instr >> 5) & 1);
210
-	setflag(FLAG_SIGN, (instr >> 6) & 1); /* TODO: Implement */
204
+	setflag(FLAG_SIGN, (instr >> 6) & 1); /* usused */
211 205
 	setflag(FLAG_COND, (instr >> 7) & 1);
212
-	/* TODO: overflow */
213
-	if(cpu.wst.ptr < opr[op][0])
214
-		return error("Stack underflow", op);
215
-	/* short mode */
216 206
 	if(getflag(FLAG_SHORT))
217 207
 		op += 16;
218
-	/* cond mode */
219
-	if(getflag(FLAG_COND)) {
220
-		if(wspop8())
221
-			(*ops[op])();
222
-	} else
208
+	if(cpu.wst.ptr < opr[op][0])
209
+		return error("Stack underflow", op);
210
+	if(cpu.wst.ptr + opr[op][1] - opr[op][0] >= 255)
211
+		return error("Stack overflow", instr);
212
+	if(!getflag(FLAG_COND) || (getflag(FLAG_COND) && wspop8()))
223 213
 		(*ops[op])();
224
-	/* devices: experimental */
225
-	if(cpu.ram.dat[0xfff1])
226
-		device1(&cpu.ram.dat[0xfff0], &cpu.ram.dat[0xfff1]);
227
-	cpu.counter++;
214
+	dodevices();
215
+	return 1;
216
+}
217
+
218
+int
219
+eval(void)
220
+{
221
+	Uint8 instr = cpu.ram.dat[cpu.ram.ptr++];
222
+	if(cpu.literal > 0)
223
+		return doliteral(instr);
224
+	else
225
+		return doopcode(instr);
228 226
 	return 1;
229 227
 }
230 228
 
... ...
@@ -236,9 +234,9 @@ load(FILE *f)
236 234
 }
237 235
 
238 236
 void
239
-debug(void)
237
+echof(void)
240 238
 {
241
-	printf("ended @ %d steps | hf: %x sf: %x cf: %x tf: %x\n",
239
+	printf("ended @ %d steps | hf: %x sf: %x sf: %x cf: %x\n",
242 240
 		cpu.counter,
243 241
 		getflag(FLAG_HALT) != 0,
244 242
 		getflag(FLAG_SHORT) != 0,
... ...
@@ -256,12 +254,12 @@ boot(void)
256 254
 	cpu.ram.ptr = cpu.vreset;
257 255
 	setflag(FLAG_HALT, 0);
258 256
 	while(!(cpu.status & FLAG_HALT) && eval())
259
-		;
257
+		cpu.counter++;
260 258
 	/*eval frame */
261 259
 	cpu.ram.ptr = cpu.vframe;
262 260
 	setflag(FLAG_HALT, 0);
263 261
 	while(!(cpu.status & FLAG_HALT) && eval())
264
-		;
262
+		cpu.counter++;
265 263
 	return 1;
266 264
 }
267 265
 
... ...
@@ -280,6 +278,6 @@ main(int argc, char *argv[])
280 278
 	/* print result */
281 279
 	echos(&cpu.wst, 0x40, "stack");
282 280
 	echom(&cpu.ram, 0x40, "ram");
283
-	debug();
281
+	echof();
284 282
 	return 0;
285 283
 }