Browse code

Starting cleanup

neauoire authored on 22/03/2021 23:51:12
Showing 5 changed files
... ...
@@ -17,12 +17,12 @@ clang-format -i uxn.c
17 17
 clang-format -i emulator.c
18 18
 rm -f ./bin/emulator
19 19
 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 emulator.c -L/usr/local/lib -lSDL2 -o bin/emulator
20
-cc -std=c89 -DNO_SDL -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 emulator.c -L/usr/local/lib -lSDL2 -o bin/emulator-nosdl
21 20
 # cc uxn.c emulator.c -std=c89 -Os -DNDEBUG -g0 -s -Wall -Wno-unknown-pragmas -L/usr/local/lib -lSDL2 -o bin/emulator
22 21
 
22
+# Emulator(CLI)
23
+clang-format -i emulator-cli.c
24
+rm -f ./bin/emulator-cli
25
+
23 26
 # run
24
-if [ "${#}" -gt 0 ]; then
25
-	exec ./run.sh "${@}"
26
-fi
27
-./bin/assembler projects/software/noodle.usm bin/boot.rom
27
+./bin/assembler projects/software/nasu.usm bin/boot.rom
28 28
 ./bin/emulator bin/boot.rom
29 29
new file mode 100644
... ...
@@ -0,0 +1,236 @@
1
+#include <stdio.h>
2
+
3
+/*
4
+Copyright (c) 2021 Devine Lu Linvega
5
+
6
+Permission to use, copy, modify, and distribute this software for any
7
+purpose with or without fee is hereby granted, provided that the above
8
+copyright notice and this permission notice appear in all copies.
9
+
10
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
+WITH REGARD TO THIS SOFTWARE.
12
+*/
13
+
14
+#include "uxn.h"
15
+
16
+#pragma mark - Core
17
+
18
+int
19
+error(char *msg, const char *err)
20
+{
21
+	printf("Error %s: %s\n", msg, err);
22
+	return 0;
23
+}
24
+
25
+#pragma mark - Devices
26
+
27
+Uint8
28
+console_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
29
+{
30
+	Uint8 *m = u->ram.dat;
31
+	switch(b0) {
32
+	case 0x08: printf("%c", b1); break;
33
+	case 0x09: printf("0x%02x\n", b1); break;
34
+	case 0x0b: printf("0x%04x\n", (m[ptr + 0x0a] << 8) + b1); break;
35
+	}
36
+	fflush(stdout);
37
+	(void)m;
38
+	(void)ptr;
39
+	(void)b0;
40
+	return b1;
41
+}
42
+
43
+Uint8
44
+file_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
45
+{
46
+	Uint8 *m = u->ram.dat;
47
+	char *name = (char *)&m[(m[ptr + 8] << 8) + m[ptr + 8 + 1]];
48
+	Uint16 length = (m[ptr + 8 + 2] << 8) + m[ptr + 8 + 3];
49
+	if(b0 == 0x0d) {
50
+		Uint16 addr = (m[ptr + 8 + 4] << 8) + b1;
51
+		FILE *f = fopen(name, "r");
52
+		if(f && fread(&m[addr], length, 1, f)) {
53
+			fclose(f);
54
+			printf("Loaded %d bytes, at %04x from %s\n", length, addr, name);
55
+		}
56
+	} else if(b0 == 0x0f) {
57
+		Uint16 addr = (m[ptr + 8 + 6] << 8) + b1;
58
+		FILE *f = fopen(name, "w");
59
+		if(fwrite(&m[addr], length, 1, f)) {
60
+			fclose(f);
61
+			printf("Saved %d bytes, at %04x from %s\n", length, addr, name);
62
+		}
63
+	}
64
+	return b1;
65
+}
66
+
67
+static void
68
+stack_diff(Stack *old, Stack *new, char *title)
69
+{
70
+	size_t i;
71
+	printf("%6s: ", title);
72
+	for(i = 0;; ++i) {
73
+		if(i < old->ptr) {
74
+			if(i < new->ptr) {
75
+				if(old->dat[i] == new->dat[i]) {
76
+					printf(" \033[0m%02x", new->dat[i]);
77
+				} else {
78
+					printf(" \033[0;31m%02x\033[33;1m%02x", old->dat[i], new->dat[i]);
79
+				}
80
+			} else { /* only in old stack */
81
+				printf(" \033[0;31m%02x", old->dat[i]);
82
+			}
83
+		} else {
84
+			if(i < new->ptr) { /* only in new stack */
85
+				printf(" \033[33;1m%02x", new->dat[i]);
86
+			} else { /* in neither stack, end of loop */
87
+				break;
88
+			}
89
+		}
90
+	}
91
+	printf("\033[0m\n");
92
+}
93
+
94
+static void
95
+memory_diff(Uint8 *old, Uint8 *new, size_t start, size_t end)
96
+{
97
+	size_t i, j;
98
+	for(i = start; i < end; i += 0x10) {
99
+		int changes = 0;
100
+		for(j = i; j < i + 0x10; ++j) {
101
+			if(old[j] != new[j]) {
102
+				changes = 1;
103
+				break;
104
+			}
105
+		}
106
+		if(!changes) continue;
107
+		printf("0x%04lx:  ", i);
108
+		for(j = i; j < i + 0x10; ++j) {
109
+			printf("\033[%sm%02x", old[j] == new[j] ? "0" : "33;1", new[j]);
110
+			if(j % 2) putchar(' ');
111
+		}
112
+		printf("  ");
113
+		for(j = i; j < i + 0x10; ++j) {
114
+			printf("\033[%sm%c", old[j] == new[j] ? "0" : "33;1", (new[j] < ' ' || new[j] > '~') ? '.' : new[j]);
115
+		}
116
+		printf("\033[0m\n");
117
+	}
118
+}
119
+
120
+Uint8
121
+debug_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
122
+{
123
+	size_t i;
124
+	(void)ptr;
125
+	switch(b0) {
126
+	case 0x08: /* stack */
127
+		printf("pc %04x working stack:", u->ram.ptr);
128
+		for(i = 0; i < u->wst.ptr; ++i) {
129
+			printf(" %02x", u->wst.dat[i]);
130
+		}
131
+		printf(", return stack: ");
132
+		for(i = 0; i < u->rst.ptr; ++i) {
133
+			printf(" %02x", u->rst.dat[i]);
134
+		}
135
+		printf("\n");
136
+		if(b1 && b1 != u->wst.ptr) {
137
+			printf("length %d failed to match %d!\n", b1, u->wst.ptr);
138
+			exit(1);
139
+		}
140
+		break;
141
+	case 0x09: /* snapshot */
142
+		if(u->snapshot != NULL) {
143
+			if(!(b1 & 0x01)) {
144
+				stack_diff(&u->snapshot->wst, &u->wst, "work");
145
+			}
146
+			if(!(b1 & 0x02)) {
147
+				stack_diff(&u->snapshot->rst, &u->rst, "return");
148
+			}
149
+			if(!(b1 & 0x04)) {
150
+				memory_diff(u->snapshot->ram.dat, u->ram.dat, 0, PAGE_DEVICE);
151
+				memory_diff(u->snapshot->ram.dat, u->ram.dat, PAGE_DEVICE + 0x0100, 0x10000);
152
+			}
153
+		}
154
+		{
155
+			int want_snapshot = !(b1 & 0x80);
156
+			if(want_snapshot) {
157
+				if(u->snapshot == NULL) {
158
+					u->snapshot = malloc(sizeof(*u));
159
+				}
160
+				for(i = 0; i < sizeof(*u); ++i) {
161
+					((char *)u->snapshot)[i] = ((char *)u)[i];
162
+				}
163
+			}
164
+			printf("pc 0x%04x snapshot%s taken\n", u->counter, want_snapshot ? "" : " not");
165
+		}
166
+		break;
167
+	case 0x0a: /* exit */
168
+		printf("Exited after 0x%04x cycles.\n", u->counter);
169
+		exit(b1);
170
+		break;
171
+	case 0x0f: /* test mode */
172
+		u->test_mode = b1;
173
+		printf("Test mode is now 0x%02x: ", u->test_mode);
174
+		if(b1 & 0x01) {
175
+			printf("BRK resets stacks to zero length");
176
+		} else {
177
+			printf("all test mode features disabled");
178
+		}
179
+		printf("\n");
180
+		break;
181
+	}
182
+	fflush(stdout);
183
+	return b1;
184
+}
185
+
186
+Uint8
187
+ppnil(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
188
+{
189
+	(void)u;
190
+	(void)ptr;
191
+	(void)b0;
192
+	return b1;
193
+}
194
+
195
+#pragma mark - Generics
196
+
197
+int
198
+start(Uxn *u)
199
+{
200
+	evaluxn(u, u->vreset);
201
+	evaluxn(u, u->vframe);
202
+}
203
+
204
+int
205
+main(int argc, char **argv)
206
+{
207
+	Uxn u;
208
+
209
+	if(argc < 2)
210
+		return error("Input", "Missing");
211
+	if(!bootuxn(&u))
212
+		return error("Boot", "Failed");
213
+	if(!loaduxn(&u, argv[1]))
214
+		return error("Load", "Failed");
215
+	if(!init())
216
+		return error("Init", "Failed");
217
+
218
+	portuxn(&u, "console", console_poke);
219
+	portuxn(&u, "empty", ppnil);
220
+	portuxn(&u, "empty", ppnil);
221
+	portuxn(&u, "empty", ppnil);
222
+	portuxn(&u, "empty", ppnil);
223
+	portuxn(&u, "empty", ppnil);
224
+	portuxn(&u, "file", file_poke);
225
+	portuxn(&u, "empty", ppnil);
226
+	portuxn(&u, "empty", ppnil);
227
+	portuxn(&u, "empty", ppnil);
228
+	portuxn(&u, "empty", ppnil);
229
+	portuxn(&u, "empty", ppnil);
230
+	portuxn(&u, "empty", ppnil);
231
+	portuxn(&u, "empty", ppnil);
232
+	portuxn(&u, "debug", debug_poke);
233
+	portuxn(&u, "system", system_poke);
234
+
235
+	return 0;
236
+}
... ...
@@ -238,7 +238,6 @@ quit(void)
238 238
 int
239 239
 init(void)
240 240
 {
241
-#ifndef NO_SDL
242 241
 	if(SDL_Init(SDL_INIT_VIDEO) < 0)
243 242
 		return error("Init", SDL_GetError());
244 243
 	gWindow = SDL_CreateWindow("Uxn", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH * ZOOM, HEIGHT * ZOOM, SDL_WINDOW_SHOWN);
... ...
@@ -255,7 +254,6 @@ init(void)
255 254
 	clear(pixels);
256 255
 	SDL_StartTextInput();
257 256
 	SDL_ShowCursor(SDL_DISABLE);
258
-#endif
259 257
 	screen.bounds.x1 = PAD * 8;
260 258
 	screen.bounds.x2 = WIDTH - PAD * 8 - 1;
261 259
 	screen.bounds.y1 = PAD * 8;
... ...
@@ -412,131 +410,6 @@ file_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
412 410
 	return b1;
413 411
 }
414 412
 
415
-static void
416
-stack_diff(Stack *old, Stack *new, char *title)
417
-{
418
-	size_t i;
419
-	printf("%6s: ", title);
420
-	for (i = 0;; ++i) {
421
-		if (i < old->ptr) {
422
-			if (i < new->ptr) {
423
-				if (old->dat[i] == new->dat[i]) {
424
-					printf(" \033[0m%02x", new->dat[i]);
425
-				}
426
-				else {
427
-					printf(" \033[0;31m%02x\033[33;1m%02x", old->dat[i], new->dat[i]);
428
-				}
429
-			}
430
-			else { /* only in old stack */
431
-				printf(" \033[0;31m%02x", old->dat[i]);
432
-			}
433
-		}
434
-		else {
435
-			if (i < new->ptr) { /* only in new stack */
436
-				printf(" \033[33;1m%02x", new->dat[i]);
437
-			}
438
-			else { /* in neither stack, end of loop */
439
-				break;
440
-			}
441
-		}
442
-	}
443
-	printf("\033[0m\n");
444
-}
445
-
446
-static void
447
-memory_diff(Uint8 *old, Uint8 *new, size_t start, size_t end)
448
-{
449
-	size_t i, j;
450
-	for (i = start; i < end; i += 0x10) {
451
-		int changes = 0;
452
-		for (j = i; j < i + 0x10; ++j ) {
453
-			if (old[j] != new[j]) {
454
-				changes = 1;
455
-				break;
456
-			}
457
-		}
458
-		if (!changes) continue;
459
-		printf("0x%04lx:  ", i);
460
-		for (j = i; j < i + 0x10; ++j) {
461
-			printf("\033[%sm%02x", old[j] == new[j] ? "0" : "33;1", new[j]);
462
-			if (j % 2) putchar(' ');
463
-		}
464
-		printf("  ");
465
-		for (j = i; j < i + 0x10; ++j) {
466
-			printf("\033[%sm%c", old[j] == new[j] ? "0" : "33;1",
467
-				(new[j] < ' ' || new[j] > '~') ? '.' : new[j]);
468
-		}
469
-		printf("\033[0m\n");
470
-	}
471
-}
472
-
473
-Uint8
474
-debug_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
475
-{
476
-	size_t i;
477
-	(void)ptr;
478
-	switch (b0) {
479
-		case 0x08: /* stack */
480
-			printf("pc %04x working stack:", u->ram.ptr);
481
-			for (i = 0; i < u->wst.ptr; ++i) {
482
-				printf(" %02x", u->wst.dat[i]);
483
-			}
484
-			printf(", return stack: ");
485
-			for (i = 0; i < u->rst.ptr; ++i) {
486
-				printf(" %02x", u->rst.dat[i]);
487
-			}
488
-			printf("\n");
489
-			if (b1 && b1 != u->wst.ptr) {
490
-				printf("length %d failed to match %d!\n", b1, u->wst.ptr);
491
-				exit(1);
492
-			}
493
-			break;
494
-		case 0x09: /* snapshot */
495
-			if (u->snapshot != NULL) {
496
-				if (!(b1 & 0x01)) {
497
-					stack_diff(&u->snapshot->wst, &u->wst, "work");
498
-				}
499
-				if (!(b1 & 0x02)) {
500
-					stack_diff(&u->snapshot->rst, &u->rst, "return");
501
-				}
502
-				if (!(b1 & 0x04)) {
503
-					memory_diff(u->snapshot->ram.dat, u->ram.dat, 0, PAGE_DEVICE);
504
-					memory_diff(u->snapshot->ram.dat, u->ram.dat, PAGE_DEVICE + 0x0100, 0x10000);
505
-				}
506
-			}
507
-			{
508
-				int want_snapshot = !(b1 & 0x80);
509
-				if (want_snapshot) {
510
-					if (u->snapshot == NULL) {
511
-						u->snapshot = malloc(sizeof(*u));
512
-					}
513
-					for (i = 0; i < sizeof(*u); ++i) {
514
-						((char *) u->snapshot)[i] = ((char *) u)[i];
515
-					}
516
-				}
517
-				printf("pc 0x%04x snapshot%s taken\n", u->counter, want_snapshot ? "" : " not");
518
-			}
519
-			break;
520
-		case 0x0a: /* exit */
521
-			printf("Exited after 0x%04x cycles.\n", u->counter);
522
-			exit(b1);
523
-			break;
524
-		case 0x0f: /* test mode */
525
-			u->test_mode = b1;
526
-			printf("Test mode is now 0x%02x: ", u->test_mode);
527
-			if (b1 & 0x01) {
528
-				printf("BRK resets stacks to zero length");
529
-			}
530
-			else {
531
-				printf("all test mode features disabled");
532
-			}
533
-			printf("\n");
534
-			break;
535
-	}
536
-	fflush(stdout);
537
-	return b1;
538
-}
539
-
540 413
 Uint8
541 414
 system_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
542 415
 {
... ...
@@ -561,17 +434,12 @@ ppnil(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
561 434
 int
562 435
 start(Uxn *u)
563 436
 {
564
-#ifndef NO_SDL
565 437
 	int ticknext = 0;
566
-#endif
567 438
 	evaluxn(u, u->vreset);
568 439
 	loadtheme(u->ram.dat + PAGE_DEVICE + 0x00f8);
569
-#ifndef NO_SDL
570 440
 	if(screen.reqdraw)
571 441
 		redraw(pixels, u);
572
-#endif
573 442
 	while(1) {
574
-#ifndef NO_SDL
575 443
 		int tick = SDL_GetTicks();
576 444
 		SDL_Event event;
577 445
 		if(tick < ticknext)
... ...
@@ -592,16 +460,9 @@ start(Uxn *u)
592 460
 				break;
593 461
 			}
594 462
 		}
595
-#endif
596
-		if (u->test_mode & 0x01) {
597
-			u->wst.ptr = 0;
598
-			u->rst.ptr = 0;
599
-		}
600 463
 		evaluxn(u, u->vframe);
601
-#ifndef NO_SDL
602 464
 		if(screen.reqdraw)
603 465
 			redraw(pixels, u);
604
-#endif
605 466
 	}
606 467
 }
607 468
 
... ...
@@ -633,7 +494,6 @@ main(int argc, char **argv)
633 494
 	portuxn(&u, "empty", ppnil);
634 495
 	portuxn(&u, "empty", ppnil);
635 496
 	portuxn(&u, "empty", ppnil);
636
-	portuxn(&u, "debug", debug_poke);
637 497
 	portuxn(&u, "system", system_poke);
638 498
 
639 499
 	/* Write screen size to dev/screen */
640 500
deleted file mode 100644
... ...
@@ -1,31 +0,0 @@
1
-This is the first line.
2
-	This is a tabbed line.
3
-
4
-The previous line is blank.
5
-	This is another tabbed line.
6
-
7
-This is a very long line, This is a very long line, This is a very long line, This is a very long line.
8
-
9
-This is a text block This is
10
-is a text block This is a te
11
-This is a text block This is
12
-is a text block This is a te
13
-This is a text block This is
14
-is a text block This is a te
15
-This is a text block This is
16
-is a text block This is a te
17
-
18
-oneword
19
-two words
20
-
21
-( parens )
22
-[ block ]
23
-{ curlies }
24
-
25
-Depth1
26
-	Depth2
27
-		Depth3
28
-			Depth4
29
-				Depth5
30
-
31
-This is the last line.
32 0
\ No newline at end of file
... ...
@@ -40,12 +40,11 @@ typedef struct Device {
40 40
 } Device;
41 41
 
42 42
 typedef struct Uxn {
43
-	Uint8 literal, status, devices, test_mode;
43
+	Uint8 literal, status, devices;
44 44
 	Uint16 counter, vreset, vframe, verror;
45 45
 	Stack wst, rst, *src, *dst;
46 46
 	Memory ram;
47 47
 	Device dev[16];
48
-	struct Uxn *snapshot;
49 48
 } Uxn;
50 49
 
51 50
 void setflag(Uint8 *status, char flag, int b);