Refactoring.
h3rald h3rald@h3rald.com
Wed, 03 Dec 2025 16:47:59 +0100
3 files changed,
60 insertions(+),
33 deletions(-)
A
devices/system.c
@@ -0,0 +1,25 @@
+#include "../xyw.h" + +xyw_byte system_read(xyw_byte *data, xyw_byte addr, xyw_byte *error) +{ + (void)error; + return data[addr]; +} + +void system_write(xyw_byte *data, xyw_byte addr, xyw_byte val, xyw_byte *error) +{ + (void)error; + data[addr] = val; +} + +xyw_word system_readw(xyw_byte *data, xyw_byte addr, xyw_byte *error) +{ + (void)error; + return XYW_PEEKW(&data[addr]); +} + +void system_writew(xyw_byte *data, xyw_byte addr, xyw_word val, xyw_byte *error) +{ + (void)error; + XYW_POKEW(&data[addr], val); +}
M
xyw.h
→
xyw.h
@@ -8,9 +8,24 @@ #define XYW_TOTAL_INSTRUCTIONS 0x20
#define XYW_TOTAL_DEVICES 0x10 // clang-format off -#define PEEKW(d) (*(d) << 8 | (d)[1]) -#define POKEW(d, v) { *(d) = (v) >> 8; (d)[1] = (v); } +#define XYW_PEEKW(d) (*(d) << 8 | (d)[1]) +#define XYW_POKEW(d, v) { *(d) = (v) >> 8; (d)[1] = (v); } // clang-format on + +/* system device */ +#define XYW_SYSTEM_STATE 0xff00 +#define XYW_SYSTEM_ERROR 0xff01 +#define XYW_SYSTEM_S 0xff02 +#define XYW_SYSTEM_U 0xff03 +#define XYW_SYSTEM_PAGE 0xff04 +#define XYW_SYSTEM_ON_ERROR 0xff05 + +/* console device */ +#define XYW_CONSOLE_BASE 0xff10 +#define XYW_CONSOLE_INPUT 0xff10 +#define XYW_CONSOLE_OUTPUT 0xff11 +#define XYW_CONSOLE_ERROR 0xff12 +#define XYW_CONSOLE_ON_KEYPRESS 0xff13 typedef unsigned char xyw_byte; typedef unsigned short xyw_word;
M
xywrun.c
→
xywrun.c
@@ -1,6 +1,8 @@
#include <stdio.h> #include "xyw.h" +#include "devices/system.c" + #define MEMORY_SIZE 0xffff #define STACK_SIZE 0xff@@ -20,36 +22,21 @@ XYW_ERROR_DEVICE_UNAVAILABLE,
XYW_ERROR_INVALID_DEVICE_PORT } xyw_error; -/* system device */ -#define SYSTEM_STATE 0xff00 -#define SYSTEM_ERROR 0xff01 -#define SYSTEM_S 0xff02 -#define SYSTEM_U 0xff03 -#define SYSTEM_PAGE 0xff04 -#define SYSTEM_ON_ERROR 0xff05 - -/* console device */ -#define CONSOLE_BASE 0xff10 -#define CONSOLE_INPUT 0xff10 -#define CONSOLE_OUTPUT 0xff11 -#define CONSOLE_ERROR 0xff12 -#define CONSOLE_ON_KEYPRESS 0xff13 - /// Main memory and stacks xyw_byte xyw_memory[MEMORY_SIZE]; xyw_byte system_stack[STACK_SIZE]; xyw_byte user_stack[STACK_SIZE]; -#define DIRECT_PAGE (xyw_memory[SYSTEM_PAGE] << 8) +#define DIRECT_PAGE (xyw_memory[XYW_SYSTEM_PAGE] << 8) //// 1-byte pointers and registers -xyw_byte *s = &xyw_memory[SYSTEM_S]; -xyw_byte *u = &xyw_memory[SYSTEM_U]; +xyw_byte *s = &xyw_memory[XYW_SYSTEM_S]; +xyw_byte *u = &xyw_memory[XYW_SYSTEM_U]; static xyw_byte x_val = 0; // X register storage static xyw_byte y_val = 0; // Y register storage xyw_byte *x = &x_val; xyw_byte *y = &y_val; -xyw_byte *error = &xyw_memory[SYSTEM_ERROR]; +xyw_byte *error = &xyw_memory[XYW_SYSTEM_ERROR]; //// 2-byte registers static xyw_word pc_storage = 0; // Program counter storage@@ -71,8 +58,8 @@ // Should return a number between 0 and 15, add validation
xyw_byte port = (addr - DEVICE_AREA_START) % 16; if (port >= 16) { - *error = XYW_ERROR_INVALID_DEVICE_PORT; - return 0xFF; + *error = XYW_ERROR_DEVICE_UNAVAILABLE; + return -1; } return port; }@@ -92,7 +79,7 @@ }
xyw_byte dev_addr = get_device_port(addr); return xyw_devices[dev_num].readw(dev_addr, error); } - return PEEKW(&xyw_memory[addr]); + return XYW_PEEKW(&xyw_memory[addr]); } static inline void writew(xyw_word addr, xyw_word val)@@ -109,7 +96,7 @@ xyw_byte dev_addr = get_device_port(addr);
xyw_devices[dev_num].writew(dev_addr, val, error); return; } - POKEW(&xyw_memory[addr], val); + XYW_POKEW(&xyw_memory[addr], val); } static inline xyw_byte read(xyw_word addr)@@ -153,7 +140,7 @@ static xyw_word ss_popw()
{ if (*s < 2) { - xyw_memory[SYSTEM_ERROR] = XYW_ERROR_STACK_UNDERFLOW; + xyw_memory[XYW_SYSTEM_ERROR] = XYW_ERROR_STACK_UNDERFLOW; return -1; } xyw_word val = readw(SYSTEM_STACK_START + (*s) - 2);@@ -166,7 +153,7 @@ static void ss_pushw(xyw_word val)
{ if (*s > STACK_SIZE - 2) { - xyw_memory[SYSTEM_ERROR] = XYW_ERROR_STACK_OVERFLOW; + xyw_memory[XYW_SYSTEM_ERROR] = XYW_ERROR_STACK_OVERFLOW; return; } writew(SYSTEM_STACK_START + *s, val);@@ -179,7 +166,7 @@ static void us_push(xyw_byte val)
{ if (*u > STACK_SIZE - 1) { - xyw_memory[SYSTEM_ERROR] = XYW_ERROR_STACK_OVERFLOW; + xyw_memory[XYW_SYSTEM_ERROR] = XYW_ERROR_STACK_OVERFLOW; return; } xyw_dbg(" => %02X\n", val);@@ -190,7 +177,7 @@ static void us_pushw(xyw_word val)
{ if (*u > STACK_SIZE - 2) { - xyw_memory[SYSTEM_ERROR] = XYW_ERROR_STACK_OVERFLOW; + xyw_memory[XYW_SYSTEM_ERROR] = XYW_ERROR_STACK_OVERFLOW; return; } writew(USER_STACK_START + *u, val);@@ -202,7 +189,7 @@ static xyw_byte us_pop()
{ if (*u < 1) { - xyw_memory[SYSTEM_ERROR] = XYW_ERROR_STACK_UNDERFLOW; + xyw_memory[XYW_SYSTEM_ERROR] = XYW_ERROR_STACK_UNDERFLOW; return -1; } (*u) -= 1;@@ -215,7 +202,7 @@ static xyw_word us_popw()
{ if (*u < 2) { - xyw_memory[SYSTEM_ERROR] = XYW_ERROR_STACK_UNDERFLOW; + xyw_memory[XYW_SYSTEM_ERROR] = XYW_ERROR_STACK_UNDERFLOW; return -1; } xyw_word val = readw(USER_STACK_START + (*u) - 2);@@ -351,8 +338,8 @@ return -1;
} xyw_dbg("Loaded %zu bytes into memory\n", bytes_read); fclose(fp); - xyw_memory[SYSTEM_STATE] = 1; - while (xyw_memory[SYSTEM_STATE] && *pc < SYSTEM_MEMORY_START) + xyw_memory[XYW_SYSTEM_STATE] = 1; + while (xyw_memory[XYW_SYSTEM_STATE] && *pc < SYSTEM_MEMORY_START) { xyw_dbg("PC: %04X -> %02X (%s)\n", *pc, xyw_memory[*pc], xyw_instructions[xyw_memory[*pc] & 0x1F]); switch (xyw_memory[*pc])