all repos — xyw @ 94e19d39dc91bd9586bdad7c7a61d6be8b2a878d

A minimal virtual machine and assembler for terminals.

Updated device contract, preparing API to expose to devices.
h3rald h3rald@h3rald.com
Sun, 26 Jul 2026 14:27:40 +0200
commit

94e19d39dc91bd9586bdad7c7a61d6be8b2a878d

parent

f1a59986d12233f629ea0a41cc92ba89aacb3a12

2 files changed, 85 insertions(+), 23 deletions(-)

jump to
M xyw.hxyw.h

@@ -23,6 +23,7 @@ typedef xyw_byte (*xyw_input)(xyw_byte *data, xyw_byte addr, xyw_byte *error);

typedef void (*xyw_output)(xyw_byte *data, xyw_byte addr, xyw_byte *error); typedef void (*xyw_setup)(xyw_byte *data); typedef void (*xyw_teardown)(xyw_byte *data); +typedef int (*xyw_poll)(xyw_byte *data); typedef struct xyw_device {

@@ -31,6 +32,7 @@ xyw_setup setup;

xyw_input input; xyw_output output; xyw_teardown teardown; + xyw_poll poll; } xyw_device; // Main Globals

@@ -46,5 +48,23 @@ int xyw_assemble(const char *input, const char *output);

int xyw_run(const char *input); int xyw_eval(xyw_word pc); void xyw_request_exec(xyw_word path_addr); + +// Privileged VM API (for device implementations) +extern xyw_byte xyw_memory[]; +extern xyw_word *pc, *xw, *yw; +extern xyw_byte *x, *y; +extern xyw_byte *dev_error; + +void xyw_vm_push_frame(xyw_word return_addr); +xyw_word xyw_vm_pop_frame(void); +xyw_byte xyw_vm_get_system_depth(void); + +void xyw_vm_set_running(int running); +void xyw_vm_set_waiting(int waiting); +int xyw_vm_is_running(void); +int xyw_vm_is_waiting(void); +int xyw_vm_is_error_state(void); + +void xyw_vm_reset(void); #endif // XYW_H
M xywrun.cxywrun.c

@@ -208,6 +208,66 @@ for (int i = 0; i < SYSTEM_FRAME_SIZE; i++) base[i] = 0;

return return_addr; } +//// Internal VM state +xyw_byte error_handler_entry_s = 0; + +static int system_error_state() +{ + return xyw_memory[SYSTEM_STATE] & SYSTEM_STATE_ERROR; +} + +static int system_running_state() +{ + return xyw_memory[SYSTEM_STATE] & SYSTEM_STATE_RUNNING; +} + +static int system_waiting_state() +{ + return xyw_memory[SYSTEM_STATE] & SYSTEM_STATE_WAITING; +} + +//// Privileged VM API (public wrappers) + +void xyw_vm_push_frame(xyw_word return_addr) { ss_push_frame(return_addr); } +xyw_word xyw_vm_pop_frame(void) { return ss_pop_frame(); } +xyw_byte xyw_vm_get_system_depth(void) { return s; } + +void xyw_vm_set_running(int running) +{ + if (running) + xyw_memory[SYSTEM_STATE] |= SYSTEM_STATE_RUNNING; + else + xyw_memory[SYSTEM_STATE] &= ~SYSTEM_STATE_RUNNING; +} + +void xyw_vm_set_waiting(int waiting) +{ + if (waiting) + xyw_memory[SYSTEM_STATE] |= SYSTEM_STATE_WAITING; + else + xyw_memory[SYSTEM_STATE] &= ~SYSTEM_STATE_WAITING; +} + +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(); } + +void xyw_vm_reset(void) +{ + memset(xyw_memory, 0, sizeof(xyw_memory)); + memset(user_stack, 0, sizeof(user_stack)); + memset(system_stack, 0, sizeof(system_stack)); + s = 0; + u = 0; + x_val = 0; + y_val = 0; + xw_storage = 0; + yw_storage = 0; + pc_storage = 0; + error_handler_entry_s = 0; + xyw_argv_processed = 0; +} + //// User stack management static void us_push(xyw_byte val)

@@ -343,36 +403,18 @@ //// Initialize devices

xyw_device xyw_devices[XYW_TOTAL_DEVICES] = { // System device - {&xyw_memory[0xff00], system_setup, system_input, system_output, 0}, + {&xyw_memory[0xff00], system_setup, system_input, system_output, 0, 0}, // Terminal device - {&xyw_memory[0xff10], terminal_init, terminal_input, terminal_output, terminal_teardown}, + {&xyw_memory[0xff10], terminal_init, terminal_input, terminal_output, terminal_teardown, 0}, // Clock device - {&xyw_memory[0xff20], clock_setup, clock_input, clock_output, 0}, + {&xyw_memory[0xff20], clock_setup, clock_input, clock_output, 0, 0}, // File device - {&xyw_memory[0xff30], file_setup, file_input, file_output, 0}, + {&xyw_memory[0xff30], file_setup, file_input, file_output, 0, 0}, // Beeper device - {&xyw_memory[0xff40], 0, beeper_input, beeper_output, beeper_teardown}, + {&xyw_memory[0xff40], 0, beeper_input, beeper_output, beeper_teardown, 0}, // ... }; // clang-format on - -static int system_error_state() -{ - return xyw_memory[SYSTEM_STATE] & SYSTEM_STATE_ERROR; -} - -static int system_running_state() -{ - return xyw_memory[SYSTEM_STATE] & SYSTEM_STATE_RUNNING; -} - -static int system_waiting_state() -{ - return xyw_memory[SYSTEM_STATE] & SYSTEM_STATE_WAITING; -} - -//// Internal VM state -xyw_byte error_handler_entry_s = 0; // Argument feeding state (CLI args) static int arg_index = 1;