uxn

Varvara Ordinator, written in ANSI C(SDL2)
git clone https://git.eamoncaddigan.net/uxn.git
Log | Files | Refs | README | LICENSE

commit 118afe5fb47d1a876c3be09ec6cf501b258b7f0d
parent 52eb7f11af2f3202e1278d01fc10372b29d2f611
Author: Andrew Alderwick <andrew@alderwick.co.uk>
Date:   Mon, 22 Mar 2021 23:22:47 +0000

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.

Diffstat:
Mbuild.sh | 3+++
Memulator.c | 131++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Mprojects/tests/opcodes.usm | 5+++--
Muxn.h | 3++-
4 files changed, 138 insertions(+), 4 deletions(-)

diff --git a/build.sh b/build.sh @@ -21,5 +21,8 @@ cc -std=c89 -DNO_SDL -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wex # cc uxn.c emulator.c -std=c89 -Os -DNDEBUG -g0 -s -Wall -Wno-unknown-pragmas -L/usr/local/lib -lSDL2 -o bin/emulator # run +if [ "${#}" -gt 0 ]; then + exec ./run.sh "${@}" +fi ./bin/assembler projects/software/noodle.usm bin/boot.rom ./bin/emulator bin/boot.rom diff --git a/emulator.c b/emulator.c @@ -412,6 +412,131 @@ file_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1) return b1; } +static void +stack_diff(Stack *old, Stack *new, char *title) +{ + size_t i; + printf("%6s: ", title); + for (i = 0;; ++i) { + if (i < old->ptr) { + if (i < new->ptr) { + if (old->dat[i] == new->dat[i]) { + printf(" \033[0m%02x", new->dat[i]); + } + else { + printf(" \033[0;31m%02x\033[33;1m%02x", old->dat[i], new->dat[i]); + } + } + else { /* only in old stack */ + printf(" \033[0;31m%02x", old->dat[i]); + } + } + else { + if (i < new->ptr) { /* only in new stack */ + printf(" \033[33;1m%02x", new->dat[i]); + } + else { /* in neither stack, end of loop */ + break; + } + } + } + printf("\033[0m\n"); +} + +static void +memory_diff(Uint8 *old, Uint8 *new, size_t start, size_t end) +{ + size_t i, j; + for (i = start; i < end; i += 0x10) { + int changes = 0; + for (j = i; j < i + 0x10; ++j ) { + if (old[j] != new[j]) { + changes = 1; + break; + } + } + if (!changes) continue; + printf("0x%04lx: ", i); + for (j = i; j < i + 0x10; ++j) { + printf("\033[%sm%02x", old[j] == new[j] ? "0" : "33;1", new[j]); + if (j % 2) putchar(' '); + } + printf(" "); + for (j = i; j < i + 0x10; ++j) { + printf("\033[%sm%c", old[j] == new[j] ? "0" : "33;1", + (new[j] < ' ' || new[j] > '~') ? '.' : new[j]); + } + printf("\033[0m\n"); + } +} + +Uint8 +debug_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1) +{ + size_t i; + (void)ptr; + switch (b0) { + case 0x08: /* stack */ + printf("pc %04x working stack:", u->ram.ptr); + for (i = 0; i < u->wst.ptr; ++i) { + printf(" %02x", u->wst.dat[i]); + } + printf(", return stack: "); + for (i = 0; i < u->rst.ptr; ++i) { + printf(" %02x", u->rst.dat[i]); + } + printf("\n"); + if (b1 && b1 != u->wst.ptr) { + printf("length %d failed to match %d!\n", b1, u->wst.ptr); + exit(1); + } + break; + case 0x09: /* snapshot */ + if (u->snapshot != NULL) { + if (!(b1 & 0x01)) { + stack_diff(&u->snapshot->wst, &u->wst, "work"); + } + if (!(b1 & 0x02)) { + stack_diff(&u->snapshot->rst, &u->rst, "return"); + } + if (!(b1 & 0x04)) { + memory_diff(u->snapshot->ram.dat, u->ram.dat, 0, PAGE_DEVICE); + memory_diff(u->snapshot->ram.dat, u->ram.dat, PAGE_DEVICE + 0x0100, 0x10000); + } + } + { + int want_snapshot = !(b1 & 0x80); + if (want_snapshot) { + if (u->snapshot == NULL) { + u->snapshot = malloc(sizeof(*u)); + } + for (i = 0; i < sizeof(*u); ++i) { + ((char *) u->snapshot)[i] = ((char *) u)[i]; + } + } + printf("pc 0x%04x snapshot%s taken\n", u->counter, want_snapshot ? "" : " not"); + } + break; + case 0x0a: /* exit */ + printf("Exited after 0x%04x cycles.\n", u->counter); + exit(b1); + break; + case 0x0f: /* test mode */ + u->test_mode = b1; + printf("Test mode is now 0x%02x: ", u->test_mode); + if (b1 & 0x01) { + printf("BRK resets stacks to zero length"); + } + else { + printf("all test mode features disabled"); + } + printf("\n"); + break; + } + fflush(stdout); + return b1; +} + Uint8 system_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1) { @@ -468,6 +593,10 @@ start(Uxn *u) } } #endif + if (u->test_mode & 0x01) { + u->wst.ptr = 0; + u->rst.ptr = 0; + } evaluxn(u, u->vframe); #ifndef NO_SDL if(screen.reqdraw) @@ -504,7 +633,7 @@ main(int argc, char **argv) portuxn(&u, "empty", ppnil); portuxn(&u, "empty", ppnil); portuxn(&u, "empty", ppnil); - portuxn(&u, "empty", ppnil); + portuxn(&u, "debug", debug_poke); portuxn(&u, "system", system_poke); /* Write screen size to dev/screen */ diff --git a/projects/tests/opcodes.usm b/projects/tests/opcodes.usm @@ -5,6 +5,7 @@ |0140 ;Keys { key 1 } |0150 ;Mouse { x 2 y 2 state 1 chord 1 } |0160 ;File { pad 8 name 2 length 2 load 2 save 2 } +|01E0 ;Debug { pad 8 stack 1 snapshot 1 exit 1 pad 4 test_mode 1 } |01F0 .RESET .FRAME .ERROR ( vectors ) |01F8 [ f07c f0e2 f0c2 ] ( palette ) @@ -22,10 +23,10 @@ TEST #01 #02 ADD #03 EQU PASS? [ add-result ] TEST #01 #02 ADD #ff EQU PASS? [ this-test-fails ] - ( infinite loop: change to HCF when implemented ) - TEST #fd JMP + TEST #00 =Debug.exit @RESET + #01 =Debug.test_mode ,tests #0001 SUB2 =current-test BRK diff --git a/uxn.h b/uxn.h @@ -40,11 +40,12 @@ typedef struct Device { } Device; typedef struct Uxn { - Uint8 literal, status, devices; + Uint8 literal, status, devices, test_mode; Uint16 counter, vreset, vframe, verror; Stack wst, rst, *src, *dst; Memory ram; Device dev[16]; + struct Uxn *snapshot; } Uxn; void setflag(Uint8 *status, char flag, int b);