Improved debugging.
h3rald h3rald@h3rald.com
Tue, 28 Jul 2026 07:30:01 +0200
M
xyw.h
→
xyw.h
@@ -37,7 +37,7 @@
// clang-format off #define XYW_PEEKW(d) (*(d) << 8 | (d)[1]) #define XYW_POKEW(d, v) { *(d) = (v) >> 8; (d)[1] = (v); } -#define XYW_DBG(...) do { if (xyw_debug) fprintf(stdout, __VA_ARGS__); } while(0) +#define XYW_DBG(...) do { if (xyw_debug) fprintf(stderr, __VA_ARGS__); } while(0) // clang-format on // Devices API
M
xywrun.c
→
xywrun.c
@@ -221,7 +221,6 @@ {
xyw_memory[SYSTEM_ERROR] = XYW_SYSTEM_ERROR_USER_STACK_OVERFLOW; return; } - XYW_DBG(" => %02X\n", val); user_stack[u++] = val; }@@ -234,7 +233,6 @@ return;
} user_stack[u] = (xyw_byte)(val >> 8); user_stack[u + 1] = (xyw_byte)(val & 0xFF); - XYW_DBG(" => %04X\n", val); u += 2; }@@ -471,6 +469,39 @@ }
return 0; } +// Reconstruct the modes given an opcode (used for debugging) +static const char *mode_suffix(xyw_byte opcode) +{ + static char buf[4]; + int i = 0; + if (opcode & XYW_MODE_X) buf[i++] = 'x'; + if (opcode & XYW_MODE_Y) buf[i++] = 'y'; + if (opcode & XYW_MODE_W) buf[i++] = 'w'; + buf[i] = '\0'; + return buf; +} + +// Format the first four bytes on the stack (used for debugging) +static const char *format_stack_peek(xyw_byte mode_w) +{ + static char buf[24]; + if (mode_w) + { + xyw_word top = u >= 2 ? (xyw_word)((user_stack[u-2] << 8) | user_stack[u-1]) : 0; + xyw_word second = u >= 4 ? (xyw_word)((user_stack[u-4] << 8) | user_stack[u-3]) : 0; + snprintf(buf, sizeof(buf), "[ %04X %04X ]", second, top); + } + else + { + xyw_byte b3 = u >= 4 ? user_stack[u-4] : 0; + xyw_byte b2 = u >= 3 ? user_stack[u-3] : 0; + xyw_byte b1 = u >= 2 ? user_stack[u-2] : 0; + xyw_byte b0 = u >= 1 ? user_stack[u-1] : 0; + snprintf(buf, sizeof(buf), "[%02X %02X %02X %02X]", b3, b2, b1, b0); + } + return buf; +} + //// Main evaluation function - runs from current pc until BRK int xyw_eval(xyw_word start_pc) {@@ -533,7 +564,10 @@ if (!system_running_state())
{ continue; } - XYW_DBG(" -- PC: %04X -> %02X (%s) [U:%02X|S:%02X|X:%02X|Y:%02X|XW:%04X|YW:%04X]\n", *pc, xyw_memory[*pc], xyw_instructions[xyw_memory[*pc] & 0x1F], u, s, *x, *y, *xw, *yw); + XYW_DBG(" -- PC:%04X %s%-3s U:%02X S:%02X X:%02X Y:%02X XW:%04X YW:%04X stk:%s\n", + *pc, xyw_instructions[xyw_memory[*pc] & 0x1F], mode_suffix(xyw_memory[*pc]), + u, s, *x, *y, *xw, *yw, + format_stack_peek(xyw_memory[*pc] & XYW_MODE_W)); switch (xyw_memory[*pc]) { // clang-format off