all repos — xyw @ c54e00b21018baefa7107d57ef6c34642eda51b3

A minimal virtual machine and assembler for terminals.

Renamed system errors.
h3rald h3rald@h3rald.com
Mon, 27 Jul 2026 13:20:56 +0200
commit

c54e00b21018baefa7107d57ef6c34642eda51b3

parent

a0b55dba7104efc34340708964ad6d56fee9a7a8

3 files changed, 30 insertions(+), 32 deletions(-)

jump to
M devices/system.cdevices/system.c

@@ -88,7 +88,7 @@ exec_pending = 0;

if (exec_stack_depth >= MAX_IMAGE_EXEC_DEPTH) { - data[SYSTEM_ERROR] = XYW_ERROR_EXEC_DEPTH_EXCEEDED; + data[SYSTEM_ERROR] = XYW_SYSTEM_ERROR_EXEC_DEPTH_EXCEEDED; return; } xyw_vm_save_snapshot(&exec_stack[exec_stack_depth++]);

@@ -120,7 +120,7 @@ error_handler_armed = 0;

} // Check for new error condition - if (data[SYSTEM_ERROR] != XYW_ERROR_NONE && !(data[SYSTEM_STATE] & XYW_STATE_ERROR)) + if (data[SYSTEM_ERROR] != XYW_SYSTEM_ERROR_NONE && !(data[SYSTEM_STATE] & XYW_STATE_ERROR)) { xyw_word on_error_handler = XYW_PEEKW(&data[SYSTEM_ON_ERROR]); if (on_error_handler != 0)
M xyw.hxyw.h

@@ -1,6 +1,7 @@

#ifndef XYW_H #define XYW_H +// Registers and memory #define XYW_MODE_X 0x80 #define XYW_MODE_Y 0x40 #define XYW_MODE_W 0x20

@@ -9,6 +10,21 @@ #define XYW_TOTAL_DEVICES 0x10

#define XYW_DEVICE_AREA_START 0xff00 #define XYW_ARGUMENT_SEPARATOR 0x1E #define XYW_END_OF_ARGUMENTS 0x1D + +// System state flags (architectural constants for SYSTEM_STATE register) +#define XYW_STATE_RUNNING 0x01 +#define XYW_STATE_WAITING 0x02 +#define XYW_STATE_DEBUG 0x40 +#define XYW_STATE_ERROR 0x80 + +// System Errors +#define XYW_SYSTEM_ERROR_NONE 0x00 +#define XYW_SYSTEM_ERROR_DIVISION_BY_ZERO 0x01 +#define XYW_SYSTEM_ERROR_STACK_UNDERFLOW 0x02 +#define XYW_SYSTEM_ERROR_STACK_OVERFLOW 0x03 +#define XYW_SYSTEM_ERROR_EXEC_DEPTH_EXCEEDED 0x04 +#define XYW_SYSTEM_ERROR_IMAGE_NOT_FOUND 0x05 +#define XYW_SYSTEM_ERROR_IMAGE_READ_ERROR 0x06 typedef unsigned char xyw_byte; typedef unsigned short xyw_word;

@@ -67,24 +83,6 @@ extern xyw_byte xyw_memory[];

extern xyw_word *pc, *xw, *yw; extern xyw_byte *x, *y; extern xyw_byte *dev_error; - -// System state flags (architectural constants for SYSTEM_STATE register) -#define XYW_STATE_RUNNING 0x01 -#define XYW_STATE_WAITING 0x02 -#define XYW_STATE_DEBUG 0x40 -#define XYW_STATE_ERROR 0x80 - -// Error codes (architectural constants for SYSTEM_ERROR register) -typedef enum -{ - XYW_ERROR_NONE = 0, - XYW_ERROR_DIVISION_BY_ZERO, - XYW_ERROR_STACK_UNDERFLOW, - XYW_ERROR_STACK_OVERFLOW, - XYW_ERROR_EXEC_DEPTH_EXCEEDED, - XYW_ERROR_IMAGE_NOT_FOUND, - XYW_ERROR_IMAGE_READ_ERROR, -} xyw_error; void xyw_vm_push_frame(xyw_word return_addr); xyw_word xyw_vm_pop_frame(void);
M xywrun.cxywrun.c

@@ -119,7 +119,7 @@ static void ss_push_frame(xyw_word return_addr)

{ if (s >= SYSTEM_MAX_FRAMES) { - xyw_memory[SYSTEM_ERROR] = XYW_ERROR_STACK_OVERFLOW; + xyw_memory[SYSTEM_ERROR] = XYW_SYSTEM_ERROR_STACK_OVERFLOW; return; } xyw_byte *base = &system_stack[s * SYSTEM_FRAME_SIZE];

@@ -138,7 +138,7 @@ static xyw_word ss_pop_frame()

{ if (s < 1) { - xyw_memory[SYSTEM_ERROR] = XYW_ERROR_STACK_UNDERFLOW; + xyw_memory[SYSTEM_ERROR] = XYW_SYSTEM_ERROR_STACK_UNDERFLOW; return -1; } s--;

@@ -155,7 +155,7 @@ }

//// Internal VM state -static int system_error_state() +static int XYW_SYSTEM_ERROR_state() { return xyw_memory[SYSTEM_STATE] & XYW_STATE_ERROR; }

@@ -194,7 +194,7 @@ }

int xyw_vm_is_running(void) { return system_running_state(); } int xyw_vm_is_waiting(void) { return system_waiting_state(); } -int xyw_vm_is_error_state(void) { return system_error_state(); } +int xyw_vm_is_error_state(void) { return XYW_SYSTEM_ERROR_state(); } void xyw_vm_reset(void) {

@@ -218,7 +218,7 @@ static void us_push(xyw_byte val)

{ if (u > XYW_USER_STACK_SIZE - 1) { - xyw_memory[SYSTEM_ERROR] = XYW_ERROR_STACK_OVERFLOW; + xyw_memory[SYSTEM_ERROR] = XYW_SYSTEM_ERROR_STACK_OVERFLOW; return; } XYW_DBG(" => %02X\n", val);

@@ -229,7 +229,7 @@ static void us_pushw(xyw_word val)

{ if (u > XYW_USER_STACK_SIZE - 2) { - xyw_memory[SYSTEM_ERROR] = XYW_ERROR_STACK_OVERFLOW; + xyw_memory[SYSTEM_ERROR] = XYW_SYSTEM_ERROR_STACK_OVERFLOW; return; } user_stack[u] = (xyw_byte)(val >> 8);

@@ -242,7 +242,7 @@ static xyw_byte us_pop()

{ if (u < 1) { - xyw_memory[SYSTEM_ERROR] = XYW_ERROR_STACK_UNDERFLOW; + xyw_memory[SYSTEM_ERROR] = XYW_SYSTEM_ERROR_STACK_UNDERFLOW; return -1; } u -= 1;

@@ -255,7 +255,7 @@ static xyw_word us_popw()

{ if (u < 2) { - xyw_memory[SYSTEM_ERROR] = XYW_ERROR_STACK_UNDERFLOW; + xyw_memory[SYSTEM_ERROR] = XYW_SYSTEM_ERROR_STACK_UNDERFLOW; return -1; } xyw_word val = (xyw_word)((user_stack[u - 2] << 8) | user_stack[u - 1]);

@@ -424,7 +424,7 @@ FILE *fp = fopen(path, "rb");

if (!fp) { fprintf(stderr, "Error - Could not open file [%s]\n", path); - xyw_memory[SYSTEM_ERROR] = XYW_ERROR_IMAGE_NOT_FOUND; + xyw_memory[SYSTEM_ERROR] = XYW_SYSTEM_ERROR_IMAGE_NOT_FOUND; xyw_memory[SYSTEM_STATE] &= ~XYW_STATE_RUNNING; return -1; }

@@ -433,7 +433,7 @@ fclose(fp);

if (bytes_read == 0) { fprintf(stderr, "Error - Could not read file contents [%s]\n", path); - xyw_memory[SYSTEM_ERROR] = XYW_ERROR_IMAGE_READ_ERROR; + xyw_memory[SYSTEM_ERROR] = XYW_SYSTEM_ERROR_IMAGE_READ_ERROR; xyw_memory[SYSTEM_STATE] &= ~XYW_STATE_RUNNING; return -1; }

@@ -560,7 +560,7 @@ /* SHR */ OPC_SR1r(0x0B, xyw_byte val = us_pop(); SETX(ARGX >> val); SETY(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, 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_SYSTEM_ERROR_DIVISION_BY_ZERO; } ); /* EQU */ OPC_SR2(0x10, us_push(a == b)); /* NEQ */ OPC_SR2(0x11, us_push(a != b)); /* GTH */ OPC_SR2(0x12, us_push(a > b));

@@ -584,7 +584,7 @@ break;

// clang-format on } (*pc)++; - if (xyw_memory[SYSTEM_ERROR] != XYW_ERROR_NONE) continue; + if (xyw_memory[SYSTEM_ERROR] != XYW_SYSTEM_ERROR_NONE) continue; } // Restore register state saved at entry (protects caller from event handler side-effects) *x = saved_x;