Removed stacks and pointers from memory.
h3rald h3rald@h3rald.com
Fri, 27 Feb 2026 07:17:42 +0100
4 files changed,
61 insertions(+),
66 deletions(-)
M
devices/system.c
→
devices/system.c
@@ -5,10 +5,8 @@ #include "../xyw.h"
#define SYSTEM_STATE 0x00 #define SYSTEM_ERROR 0x01 -#define SYSTEM_S 0x02 -#define SYSTEM_U 0x03 -#define SYSTEM_PAGE 0x04 -#define SYSTEM_RANDOM 0x05 +#define SYSTEM_PAGE 0x02 +#define SYSTEM_RANDOM 0x03 void system_init(xyw_byte *data) {
M
examples/lib/macros.xyw
→
examples/lib/macros.xyw
@@ -3,11 +3,9 @@
;system .macro system.state $00 .macro system.error $01 -.macro system.ssp $02 -.macro system.usp $03 -.macro system.page $04 -.macro system.random $05 -.macro system.on_error $06 +.macro system.page $02 +.macro system.random $03 +.macro system.on_error $04 ;terminal .macro terminal.input $10
M
xywasm.c
→
xywasm.c
@@ -81,9 +81,9 @@ // Current write pointer
xyw_word ptr = 0; // Flat single-string "dictionary" for symbols (macros and labels) -// Removing 768 bytes to leave room for devices and stacks +// Removing 256 bytes to leave room for devices. // Actually following Uxn design here ;) -char dict[0x8000 - 0x300]; +char dict[0x8000 - 0x100]; char *dictnext = dict; // List of symbols (macros and labels)
M
xywrun.c
→
xywrun.c
@@ -11,24 +11,18 @@
#define MEMORY_SIZE 0x10000 #define USER_STACK_SIZE 128 -#define SYSTEM_STACK_SIZE 384 #define SYSTEM_FRAME_SIZE 8 #define SYSTEM_MAX_FRAMES 48 #define USER_MEMORY_START 0x000 -#define SYSTEM_MEMORY_START 0xfd00 -#define USER_STACK_START 0xfd00 -#define SYSTEM_STACK_START 0xfd80 #define DEVICE_AREA_START 0xff00 /* system device addresses */ #define SYSTEM_STATE 0xff00 #define SYSTEM_ERROR 0xff01 -#define SYSTEM_S 0xff02 -#define SYSTEM_U 0xff03 -#define SYSTEM_PAGE 0xff04 -#define SYSTEM_RANDOM 0xff05 -#define SYSTEM_ON_ERROR 0xff06 +#define SYSTEM_PAGE 0xff02 +#define SYSTEM_RANDOM 0xff03 +#define SYSTEM_ON_ERROR 0xff04 /* Terminal device addresses */ #define TERMINAL_INPUT 0xff10@@ -66,14 +60,18 @@ #define SYSTEM_STATE_WAITING 0x02
#define SYSTEM_STATE_DEBUG 0x40 #define SYSTEM_STATE_ERROR 0x80 -/// Main memory and stacks +/// Main memory xyw_byte xyw_memory[MEMORY_SIZE]; +/// Stacks (not part of addressable memory) +static xyw_byte user_stack[USER_STACK_SIZE]; +static xyw_byte system_stack[SYSTEM_MAX_FRAMES * SYSTEM_FRAME_SIZE]; +static xyw_byte s = 0; // system stack frame index +static xyw_byte u = 0; // user stack pointer + #define DIRECT_PAGE (xyw_memory[SYSTEM_PAGE] << 8) -//// 1-byte pointers and registers -xyw_byte *s = &xyw_memory[SYSTEM_S]; -xyw_byte *u = &xyw_memory[SYSTEM_U]; +//// registers static xyw_byte x_val = 0; // X register storage static xyw_byte y_val = 0; // Y register storage xyw_byte *x = &x_val;@@ -160,40 +158,39 @@ // *s is a frame index (0..SYSTEM_MAX_FRAMES-1), not a byte offset.
static void ss_push_frame(xyw_word return_addr) { - if (*s >= SYSTEM_MAX_FRAMES) + if (s >= SYSTEM_MAX_FRAMES) { xyw_memory[SYSTEM_ERROR] = XYW_ERROR_STACK_OVERFLOW; return; } - xyw_word base = SYSTEM_STACK_START + (*s) * SYSTEM_FRAME_SIZE; - writew(base + 0, return_addr); - xyw_memory[base + 2] = *x; - xyw_memory[base + 3] = *y; - writew(base + 4, *xw); - writew(base + 6, *yw); - (*s)++; + xyw_byte *base = &system_stack[s * SYSTEM_FRAME_SIZE]; + base[0] = (xyw_byte)(return_addr >> 8); + base[1] = (xyw_byte)(return_addr & 0xFF); + base[2] = *x; + base[3] = *y; + base[4] = (xyw_byte)(*xw >> 8); + base[5] = (xyw_byte)(*xw & 0xFF); + base[6] = (xyw_byte)(*yw >> 8); + base[7] = (xyw_byte)(*yw & 0xFF); + s++; } static xyw_word ss_pop_frame() { - if (*s < 1) + if (s < 1) { xyw_memory[SYSTEM_ERROR] = XYW_ERROR_STACK_UNDERFLOW; return -1; } - (*s)--; - xyw_word base = SYSTEM_STACK_START + (*s) * SYSTEM_FRAME_SIZE; - xyw_word return_addr = readw(base + 0); - *x = xyw_memory[base + 2]; - *y = xyw_memory[base + 3]; - *xw = readw(base + 4); - *yw = readw(base + 6); + s--; + xyw_byte *base = &system_stack[s * SYSTEM_FRAME_SIZE]; + xyw_word return_addr = (xyw_word)((base[0] << 8) | base[1]); + *x = base[2]; + *y = base[3]; + *xw = (xyw_word)((base[4] << 8) | base[5]); + *yw = (xyw_word)((base[6] << 8) | base[7]); // Clear the frame - writew(base + 0, 0); - xyw_memory[base + 2] = 0; - xyw_memory[base + 3] = 0; - writew(base + 4, 0); - writew(base + 6, 0); + for (int i = 0; i < SYSTEM_FRAME_SIZE; i++) base[i] = 0; return return_addr; }@@ -201,50 +198,52 @@ //// User stack management
static void us_push(xyw_byte val) { - if (*u > USER_STACK_SIZE - 1) + if (u > USER_STACK_SIZE - 1) { xyw_memory[SYSTEM_ERROR] = XYW_ERROR_STACK_OVERFLOW; return; } XYW_DBG(" => %02X\n", val); - xyw_memory[USER_STACK_START + (*u)++] = val; + user_stack[u++] = val; } static void us_pushw(xyw_word val) { - if (*u > USER_STACK_SIZE - 2) + if (u > USER_STACK_SIZE - 2) { xyw_memory[SYSTEM_ERROR] = XYW_ERROR_STACK_OVERFLOW; return; } - writew(USER_STACK_START + *u, val); + user_stack[u] = (xyw_byte)(val >> 8); + user_stack[u + 1] = (xyw_byte)(val & 0xFF); XYW_DBG(" => %04X\n", val); - (*u) += 2; + u += 2; } static xyw_byte us_pop() { - if (*u < 1) + if (u < 1) { xyw_memory[SYSTEM_ERROR] = XYW_ERROR_STACK_UNDERFLOW; return -1; } - (*u) -= 1; - xyw_byte val = xyw_memory[USER_STACK_START + (*u)]; - xyw_memory[USER_STACK_START + (*u)] = 0; + u -= 1; + xyw_byte val = user_stack[u]; + user_stack[u] = 0; return val; } static xyw_word us_popw() { - if (*u < 2) + if (u < 2) { xyw_memory[SYSTEM_ERROR] = XYW_ERROR_STACK_UNDERFLOW; return -1; } - xyw_word val = readw(USER_STACK_START + (*u) - 2); - writew(USER_STACK_START + (*u) - 2, 0); - (*u) -= 2; + xyw_word val = (xyw_word)((user_stack[u - 2] << 8) | user_stack[u - 1]); + user_stack[u - 2] = 0; + user_stack[u - 1] = 0; + u -= 2; return val; }@@ -299,8 +298,8 @@ #define SET(v) do { if (rx) { SETX(v); } else if (ry) { SETY(v); } else { PUSH(v); } } while(0)
// Read a value from memory #define READ(addr) (w ? readw(addr) : readb(addr)) -// Read top of the user stack -#define TOP READ(USER_STACK_START + (*u) - (w ? 2 : 1)) +// Read top of the user stack (direct array access, no device dispatch) +#define TOP (w ? (xyw_word)((user_stack[u - 2] << 8) | user_stack[u - 1]) : (xyw_word)user_stack[u - 1]) //// Initialize devices@@ -349,7 +348,7 @@ {
XYW_DBG("Error occurred: %02X, jumping to error handler at %04X\n", xyw_memory[SYSTEM_ERROR], on_error_handler); // Enter error state xyw_memory[SYSTEM_STATE] |= SYSTEM_STATE_ERROR; - error_handler_entry_s = *s; // Record stack depth before pushing return address + error_handler_entry_s = s; // Record stack depth before pushing return address ss_push_frame(*pc); *pc = on_error_handler; }@@ -410,10 +409,10 @@ if (xyw_debug)
{ xyw_memory[SYSTEM_STATE] |= SYSTEM_STATE_DEBUG; } - while ((system_running_state()) && *pc < SYSTEM_MEMORY_START) + while ((system_running_state()) && *pc < DEVICE_AREA_START) { process_events(); - 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 -> %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); switch (xyw_memory[*pc]) { // clang-format off@@ -452,8 +451,8 @@ /* NOT */ OPC_SR1r(0x14, SET(~ARGX); SET(~ARGY); );
/* AND */ OPC_SR2(0x15, PUSH(a & b)); /* IOR */ OPC_SR2(0x16, PUSH(a | b)); /* XOR */ OPC_SR2(0x17, PUSH(a ^ b)); - /* DUP */ OPC_SR1(0x18, , SET(READ(USER_STACK_START + (*u) - (w ? 2 : 1)))); - /* DUP */ OPC_SR1r(0x18, SETX(READ(USER_STACK_START + (*u) - (w ? 2 : 1))); SETY(READ(USER_STACK_START + (*u) - (w ? 2 : 1))); ); + /* DUP */ OPC_SR1(0x18, , SET(TOP)); + /* DUP */ OPC_SR1r(0x18, SETX(TOP); SETY(TOP); ); /* SWP */ OPC_SR2(0x19, if ((rx||ry)) { SET(b); } else { PUSH(b); PUSH(a); } ); /* OVR */ OPC_SR2(0x1A, PUSH(b); PUSH(a); PUSH(b)); /* ROT */ OPC_SR2(0x1B, PUSH(b); PUSH(POP()); PUSH(a));@@ -463,7 +462,7 @@ /* JSR */ OPC_SR1(0x1E, xyw_word addr = ADDR(ARG), ss_push_frame(*pc+1); *pc = addr-1;);
/* RTS */ case 0x1F: *pc = ss_pop_frame()-1; // If returning from error handler, clear flag to allow re-trigger - if ((system_error_state()) && *s == error_handler_entry_s) { + if ((system_error_state()) && s == error_handler_entry_s) { xyw_memory[SYSTEM_STATE] &= ~SYSTEM_STATE_ERROR; error_handler_entry_s = 0; }