all repos — xyw @ 9c68b98b4a7d270ca497eaf5e400053e9b67aae5

A minimal virtual machine and assembler for terminals.

Added device teardown + beeper teardown.
h3rald h3rald@h3rald.com
Tue, 21 Jul 2026 16:13:24 +0200
commit

9c68b98b4a7d270ca497eaf5e400053e9b67aae5

parent

ee129909a2376e6d76053a164fedfe672186efb5

4 files changed, 32 insertions(+), 6 deletions(-)

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

@@ -156,3 +156,14 @@ {

(void)error; return data[addr]; } + + +void beeper_teardown(xyw_byte *data) +{ + (void)data; + if (beeper_device_initialized) + { + ma_device_uninit(&beeper_device); + beeper_device_initialized = 0; + } +}
M devices/beeper.hdevices/beeper.h

@@ -2,3 +2,4 @@ #include "../xyw.h"

void beeper_output(xyw_byte *data, xyw_byte addr, xyw_byte *error); xyw_byte beeper_input(xyw_byte *data, xyw_byte addr, xyw_byte *error); +void beeper_teardown(xyw_byte *data);
M xyw.hxyw.h

@@ -22,6 +22,7 @@ // Devices API

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 struct xyw_device {

@@ -29,6 +30,7 @@ xyw_byte *data;

xyw_setup setup; xyw_input input; xyw_output output; + xyw_teardown teardown; } xyw_device; typedef struct
M xywrun.cxywrun.c

@@ -366,15 +366,15 @@ //// Initialize devices

xyw_device xyw_devices[XYW_TOTAL_DEVICES] = { // System device (not implemented yet) - {&xyw_memory[0xff00], system_setup, system_input, 0}, + {&xyw_memory[0xff00], system_setup, system_input, 0, 0}, // Terminal device - {&xyw_memory[0xff10], 0, terminal_input, terminal_output}, + {&xyw_memory[0xff10], 0, terminal_input, terminal_output, 0}, // Clock device - {&xyw_memory[0xff20], clock_setup, clock_input, clock_output}, + {&xyw_memory[0xff20], clock_setup, clock_input, clock_output, 0}, // File device - {&xyw_memory[0xff30], file_setup, file_input, file_output}, + {&xyw_memory[0xff30], file_setup, file_input, file_output, 0}, // Beeper device - {&xyw_memory[0xff40], 0, beeper_input, beeper_output}, + {&xyw_memory[0xff40], 0, beeper_input, beeper_output, beeper_teardown}, // ... }; // clang-format on

@@ -524,6 +524,17 @@ *yw = saved_yw;

return 0; } +static void xyw_teardown_devices(void) +{ + for (int i = 0; i < XYW_TOTAL_DEVICES; i++) + { + if (xyw_devices[i].teardown) + { + xyw_devices[i].teardown(xyw_devices[i].data); + } + } +} + //// Main run function int xyw_run(const char *input) {

@@ -544,7 +555,7 @@ }

XYW_DBG("Loaded %zu bytes into memory\n", bytes_read); fclose(fp); xyw_memory[SYSTEM_STATE] |= SYSTEM_STATE_RUNNING; - // Initialize devices + // Setup devices for (int i = 0; i < XYW_TOTAL_DEVICES; i++) { if (xyw_devices[i].setup)

@@ -552,6 +563,7 @@ {

xyw_devices[i].setup(xyw_devices[i].data); } } + atexit(xyw_teardown_devices); // Set direct page to device page by default xyw_memory[SYSTEM_PAGE] = 0xff;