Moved random to system device.
h3rald h3rald@h3rald.com
Mon, 02 Feb 2026 09:47:28 +0100
9 files changed,
38 insertions(+),
33 deletions(-)
M
Makefile
→
Makefile
@@ -1,8 +1,8 @@
CC = gcc CFLAGS = -Wall -Wextra -g -SOURCES = xyw.c xywasm.c xywrun.c devices/terminal.c devices/clock.c -OBJS = xyw.o xywasm.o xywrun.o devices/terminal.o devices/clock.o +SOURCES = xyw.c xywasm.c xywrun.c devices/system.c devices/terminal.c devices/clock.c +OBJS = xyw.o xywasm.o xywrun.o devices/system.o devices/terminal.o devices/clock.o .PHONY: clean@@ -17,6 +17,9 @@ $(CC) $(CFLAGS) -c xywrun.c -o xywrun.o
xywasm.o: xywasm.c xyw.h $(CC) $(CFLAGS) -c xywasm.c -o xywasm.o + +devices/system.o: devices/system.c xyw.h + $(CC) $(CFLAGS) -c devices/system.c -o devices/system.o devices/terminal.o: devices/terminal.c xyw.h $(CC) $(CFLAGS) -c devices/terminal.c -o devices/terminal.o
M
devices/clock.c
→
devices/clock.c
@@ -1,6 +1,4 @@
#include <time.h> -#include <stdlib.h> -#include <stdio.h> #include "../xyw.h" /* clock device */@@ -11,15 +9,8 @@ #define CLOCK_WEEKDAY 0x4
#define CLOCK_CLOCK_HOUR 0x5 #define CLOCK_MINUTE 0x6 #define CLOCK_SECOND 0x7 -#define CLOCK_RANDOM 0x8 -#define CLOCK_TIMER 0x9 -#define CLOCK_ON_TIMER_ELAPSED 0x9 -void clock_init(xyw_byte *data) -{ - (void)data; - // Initialize random generator seed - srand(time(NULL)); -} +#define CLOCK_TIMER 0x8 +#define CLOCK_ON_TIMER_ELAPSED 0xA xyw_byte clock_input(xyw_byte *data, xyw_byte addr, xyw_byte *error) {@@ -50,8 +41,6 @@ case CLOCK_MINUTE:
return (xyw_byte)lt->tm_min; case CLOCK_SECOND: return (xyw_byte)lt->tm_sec; - case CLOCK_RANDOM: - return (xyw_byte)(rand() & 0xFF); default: return 0; }
M
devices/clock.h
→
devices/clock.h
@@ -1,4 +1,3 @@
#include "../xyw.h" -void clock_init(xyw_byte *data); xyw_byte clock_input(xyw_byte *data, xyw_byte addr, xyw_byte *error);
M
devices/system.c
→
devices/system.c
@@ -1,13 +1,26 @@
-#include <stdio.h> +#include <time.h> +#include <stdlib.h> #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 +void system_init(xyw_byte *data) +{ + (void)data; + // Initialize random generator seed + srand(time(NULL)); +} + xyw_byte system_input(xyw_byte *data, xyw_byte addr, xyw_byte *error) { if (addr == SYSTEM_RANDOM) { - // TODO + return (xyw_byte)(rand() & 0xFF); } (void)error; return data[addr];
M
devices/system.h
→
devices/system.h
@@ -1,4 +1,4 @@
#include "../xyw.h" -void system_output(xyw_byte *data, xyw_byte addr, xyw_byte *error); +void system_init(xyw_byte *data); xyw_byte system_input(xyw_byte *data, xyw_byte addr, xyw_byte *error);
M
examples/d6.xyw
→
examples/d6.xyw
@@ -1,7 +1,7 @@
.include "devices.xyw" ; Get random number and calculate modulo 6 -clock.random LDB $6 DIV POP +system.random LDB $6 DIV POP ; Print result+1 and new line $31 ADD terminal.output STB
M
examples/devices.abs.xyw
→
examples/devices.abs.xyw
@@ -4,7 +4,8 @@ .macro system.error $FF01
.macro system.ssp $FF02 .macro system.usp $FF03 .macro system.page $FF04 -.macro system.on_error $FF05 +.macro system.random $FF05 +.macro system.on_error $FF06 ;terminal .macro terminal.input $FF10@@ -19,9 +20,8 @@ .macro clock.weekday $FF24
.macro clock.hour $FF25 .macro clock.minute $FF26 .macro clock.second $FF27 -.macro clock.random $FF28 .macro clock.timer $FF29 -.macro clock.on_timer_elapsed $FF2b +.macro clock.on_timer $FF2A ;file .macro file.path $FF30
M
examples/devices.xyw
→
examples/devices.xyw
@@ -4,7 +4,8 @@ .macro system.error $01
.macro system.ssp $02 .macro system.usp $03 .macro system.page $04 -.macro system.on_error $05 +.macro system.random $05 +.macro system.on_error $06 ;terminal .macro terminal.input $10@@ -19,9 +20,8 @@ .macro clock.weekday $24
.macro clock.hour $25 .macro clock.minute $26 .macro clock.second $27 -.macro clock.random $28 .macro clock.timer $29 -.macro clock.on_timer_elapsed $2b +.macro clock.on_timer $2A ;file .macro file.path $30
M
xywrun.c
→
xywrun.c
@@ -2,6 +2,7 @@ #include <stdio.h>
#include <stdlib.h> #include "xyw.h" +#include "devices/system.h" #include "devices/terminal.h" #include "devices/clock.h"@@ -14,7 +15,7 @@ #define USER_STACK_START 0xfd00
#define SYSTEM_STACK_START 0xfe00 #define DEVICE_AREA_START 0xff00 -/* system device */ +/* system device addresses */ #define SYSTEM_STATE 0xff00 #define SYSTEM_ERROR 0xff01 #define SYSTEM_S 0xff02@@ -22,6 +23,10 @@ #define SYSTEM_U 0xff03
#define SYSTEM_PAGE 0xff04 #define SYSTEM_RANDOM 0xff05 #define SYSTEM_ON_ERROR 0xff06 + +/* Terminal device addresses */ +#define TERMINAL_INPUT 0xff10 +#define TERMINAL_ON_KEYPRESS 0xff12 #ifdef _WIN32 #include <conio.h>@@ -259,11 +264,11 @@ //// Initialize devices
xyw_device xyw_devices[XYW_TOTAL_DEVICES] = { // System device (not implemented yet) - {&xyw_memory[0xff00], 0, 0, 0}, + {&xyw_memory[0xff00], system_init, system_input, 0}, // Terminal device {&xyw_memory[0xff10], 0, terminal_input, terminal_output}, // Clock device - {&xyw_memory[0xff20], clock_init, clock_input, 0}, + {&xyw_memory[0xff20], 0, clock_input, 0}, // File device {&xyw_memory[0xff30], 0, 0, 0}, // ...@@ -319,10 +324,6 @@ (*pc)++;
} return 0; } - -//// Terminal device addresses -#define TERMINAL_INPUT 0xff10 -#define TERMINAL_ON_KEYPRESS 0xff12 //// Main run function int xyw_run(const char *input)