Browse code

Add debug device.

Writing to Debug.stack prints a stack dump; writing to Debug.snapshot
makes a snapshot of the running VM and compares it to a previous one if
it exists; writing to Debug.exit terminates the VM; and writing to
Debug.test_mode activates features handy for automated testing.
test_mode is certainly subject to change.

Andrew Alderwick authored on 22/03/2021 23:22:47
Showing 4 changed files
... ...
@@ -21,5 +21,8 @@ cc -std=c89 -DNO_SDL -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wex
21 21
 # cc uxn.c emulator.c -std=c89 -Os -DNDEBUG -g0 -s -Wall -Wno-unknown-pragmas -L/usr/local/lib -lSDL2 -o bin/emulator
22 22
 
23 23
 # run
24
+if [ "${#}" -gt 0 ]; then
25
+	exec ./run.sh "${@}"
26
+fi
24 27
 ./bin/assembler projects/software/noodle.usm bin/boot.rom
25 28
 ./bin/emulator bin/boot.rom
... ...
@@ -412,6 +412,131 @@ file_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
412 412
 	return b1;
413 413
 }
414 414
 
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
+
415 540
 Uint8
416 541
 system_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
417 542
 {
... ...
@@ -468,6 +593,10 @@ start(Uxn *u)
468 593
 			}
469 594
 		}
470 595
 #endif
596
+		if (u->test_mode & 0x01) {
597
+			u->wst.ptr = 0;
598
+			u->rst.ptr = 0;
599
+		}
471 600
 		evaluxn(u, u->vframe);
472 601
 #ifndef NO_SDL
473 602
 		if(screen.reqdraw)
... ...
@@ -504,7 +633,7 @@ main(int argc, char **argv)
504 633
 	portuxn(&u, "empty", ppnil);
505 634
 	portuxn(&u, "empty", ppnil);
506 635
 	portuxn(&u, "empty", ppnil);
507
-	portuxn(&u, "empty", ppnil);
636
+	portuxn(&u, "debug", debug_poke);
508 637
 	portuxn(&u, "system", system_poke);
509 638
 
510 639
 	/* Write screen size to dev/screen */
... ...
@@ -5,6 +5,7 @@
5 5
 |0140 ;Keys { key 1 }
6 6
 |0150 ;Mouse { x 2 y 2 state 1 chord 1 }
7 7
 |0160 ;File { pad 8 name 2 length 2 load 2 save 2 }
8
+|01E0 ;Debug { pad 8 stack 1 snapshot 1 exit 1 pad 4 test_mode 1 }
8 9
 |01F0 .RESET .FRAME .ERROR ( vectors )
9 10
 |01F8 [ f07c f0e2 f0c2 ]   ( palette )
10 11
 
... ...
@@ -22,10 +23,10 @@
22 23
 	TEST #01 #02 ADD #03 EQU PASS? [ add-result ]
23 24
 	TEST #01 #02 ADD #ff EQU PASS? [ this-test-fails ]
24 25
 
25
-	( infinite loop: change to HCF when implemented )
26
-	TEST #fd JMP
26
+	TEST #00 =Debug.exit
27 27
 
28 28
 @RESET
29
+	#01 =Debug.test_mode
29 30
 	,tests #0001 SUB2 =current-test
30 31
 	BRK
31 32
 	
... ...
@@ -40,11 +40,12 @@ typedef struct Device {
40 40
 } Device;
41 41
 
42 42
 typedef struct Uxn {
43
-	Uint8 literal, status, devices;
43
+	Uint8 literal, status, devices, test_mode;
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;
48 49
 } Uxn;
49 50
 
50 51
 void setflag(Uint8 *status, char flag, int b);