Refactoring + fixes.
h3rald h3rald@h3rald.com
Fri, 24 Jul 2026 13:30:34 +0200
1 files changed,
19 insertions(+),
3 deletions(-)
jump to
M
xywrun.c
→
xywrun.c
@@ -585,6 +585,23 @@ }
//// Event dispatch (WAITING state) +// Addresses of all event handler slots — add new handlers here. +// Both has_event_handlers() and feed_next_event() stay in sync via this table. +static const xyw_word event_handler_addrs[] = { + TERMINAL_ON_ARGUMENT, + TERMINAL_ON_KEYPRESS, + CLOCK_ON_TIMER_ELAPSED, +}; +#define NUM_EVENT_HANDLERS (sizeof(event_handler_addrs) / sizeof(event_handler_addrs[0])) + +static int has_event_handlers(void) +{ + for (size_t i = 0; i < NUM_EVENT_HANDLERS; i++) + if (XYW_PEEKW(&xyw_memory[event_handler_addrs[i]])) + return 1; + return 0; +} + // Feed the next external event and dispatch its handler. // Returns 1 if an event was dispatched, 0 if no more events. static int feed_next_event(void)@@ -730,9 +747,7 @@ {
// clang-format off /* HLT */ case 0x00: xyw_memory[SYSTEM_STATE] &= ~SYSTEM_STATE_RUNNING; - if (XYW_PEEKW(&xyw_memory[TERMINAL_ON_ARGUMENT]) || - XYW_PEEKW(&xyw_memory[TERMINAL_ON_KEYPRESS]) || - XYW_PEEKW(&xyw_memory[CLOCK_ON_TIMER_ELAPSED])) + if (has_event_handlers()) { xyw_memory[SYSTEM_STATE] |= SYSTEM_STATE_WAITING; }@@ -790,6 +805,7 @@ break;
// clang-format on } (*pc)++; + if (xyw_memory[SYSTEM_ERROR] != XYW_ERROR_NONE) continue; } // Restore register state saved at entry (protects caller from event handler side-effects) *x = saved_x;