all repos — xyw @ d89860e6dde57ff8a2d55511463a80e788af7863

A minimal virtual machine and assembler for terminals.

Implemented error handling, added test.
h3rald h3rald@h3rald.com
Tue, 03 Feb 2026 15:40:41 +0100
commit

d89860e6dde57ff8a2d55511463a80e788af7863

parent

8f83ba6c8680326389ba40de23c5dbef77b5c995

3 files changed, 10 insertions(+), 7 deletions(-)

jump to
M examples/on-error.xywexamples/on-error.xyw

@@ -12,18 +12,18 @@ .label on_error

system.error $1 EQU on_error.print JCNw RTS .label on_error.print - ;error print JSRw - $55 $fe00 STBw - $0 system.error STB + error print JSRw + $2 system.error STB RTS -; a16 -- ; printing subroutine +; a16 -- .label print POPxw .label print.loop LDBxw terminal.output STB INCxw LDBxw print.loop JCNw + CLRxw RTS .label error
M test.shtest.sh

@@ -5,6 +5,7 @@ ### Test cases

TEST_CASES=( "run_test helloworld.xyw assert_output_equals \$'Hello, World!\\n'" "run_test d6.xyw assert_output_in_range 1 6" + "run_test on-error.xyw assert_output_equals \$'Error: Division by zero.\\n'" ) ### Helper functions

@@ -191,6 +192,8 @@ TMPDIR="$(mktemp -d 2>/dev/null || mktemp -d -t xywtest)"

trap "rm -rf \"$TMPDIR\"" EXIT XYW_BIN="$(find_xyw_bin)" + + echo "" for tc in "${TEST_CASES[@]}"; do eval "$tc"
M xywrun.cxywrun.c

@@ -305,7 +305,7 @@ }

while ((xyw_memory[SYSTEM_STATE] & SYSTEM_STATE_RUNNING) && *pc < SYSTEM_MEMORY_START) { - printf("ERROR: %d - State: %04X\n", xyw_memory[SYSTEM_ERROR], xyw_memory[SYSTEM_STATE]); + // printf("ERROR: %d - State: %04X\n", xyw_memory[SYSTEM_ERROR], xyw_memory[SYSTEM_STATE]); if (xyw_memory[SYSTEM_ERROR] == XYW_ERROR_NONE) { // Clear error state if no error

@@ -358,7 +358,7 @@ /* SHR */ OPC_SR1r(0x0B, xyw_byte val = us_pop(); SET(ARGX >> val); SET(ARGY >> val); );

/* ADD */ OPC_SR2(0x0C, PUSH(a + b)); /* SUB */ OPC_SR2(0x0D, PUSH(a - b)); /* MUL */ OPC_SR2(0x0E, PUSH(a * b)); - /* DIV */ OPC_SR2(0x0F, printf("b: %d\n", b); if (b) { PUSH(a % b); PUSH(a / b); } else { xyw_memory[SYSTEM_ERROR] = XYW_ERROR_DIVISION_BY_ZERO; } ); + /* DIV */ OPC_SR2(0x0F, if (b) { PUSH(a % b); PUSH(a / b); } else { xyw_memory[SYSTEM_ERROR] = XYW_ERROR_DIVISION_BY_ZERO; } ); /* EQU */ OPC_SR2(0x10, PUSH(a == b)); /* NEQ */ OPC_SR2(0x11, PUSH(a != b)); /* GTH */ OPC_SR2(0x12, PUSH(a > b));

@@ -374,7 +374,7 @@ /* DUP */ OPC_SR1r(0x19, SETX(READ(USER_STACK_START + (*u) - (w ? 2 : 1))); SETY(READ(USER_STACK_START + (*u) - (w ? 2 : 1))); );

/* OVR */ OPC_SR2(0x1A, PUSH(b); PUSH(a); PUSH(b)); /* ROT */ OPC_SR2(0x1B, PUSH(b); PUSH(POP()); PUSH(a)); /* JMP */ OPC_SR1(0x1C, , *pc = ADDR(ARG)-1;); - /* JCN */ OPC_SR1(0x1D, , xyw_word addr = ADDR(ARG); xyw_byte cond = us_pop(); printf("JCN to %04X if %d\n", addr, cond); if (cond) { *pc = addr-1; }); + /* JCN */ OPC_SR1(0x1D, , xyw_word addr = ADDR(ARG); xyw_byte cond = us_pop(); if (cond) { *pc = addr-1; }); /* JSR */ OPC_SR1(0x1E, xyw_word addr = ADDR(ARG), ss_pushw(*pc+1); *pc = addr-1;); /* RTS */ case 0x1F: *pc = ss_popw()-1; break; // clang-format on