#include #include #include #include #include #include #include #include #include #define STEP_MAX 0x80000000 #define PAGE_PROGRAM 0x100 #define RAM_PAGES 0x10 #define WIDTH (64 * 8) #define HEIGHT (40 * 8) /* Copyright (c) 2021-2025 Devine Lu Linvega, Andrew Alderwick, Andrew Richards, Eiríkr Åsheim, Sigrid Solveig Haflínudóttir Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE. cc -DNDEBUG -O2 -g0 -s src/uxn11.c -lX11 -lutil -o bin/uxn11 */ static XImage *ximage; static Display *display; static Window window; /* @|Uxn --------------------------------------------------------------- */ typedef unsigned char Uint8; typedef signed char Sint8; typedef unsigned short Uint16; typedef signed short Sint16; typedef unsigned int Uint32; typedef struct { Uint8 dat[0x100], ptr; } Stack; typedef struct Uxn { Uint8 *ram, dev[0x100]; Stack wst, rst; } Uxn; Uint8 emu_dei(Uint8 addr); void emu_deo(Uint8 addr, Uint8 value); static Uxn uxn; /* clang-format off */ #define PEEK2(d) (*(d) << 8 | (d)[1]) #define POKE2(d, v) { *(d) = (v) >> 8; (d)[1] = (v); } #define clamp(v,a,b) { if(v < a) v = a; else if(v >= b) v = b; } #define twos(v) (v & 0x8000 ? (int)v - 0x10000 : (int)v) #define OPC(opc, init, body) {\ case 0x00|opc: {const int _2=0,_r=0;init body;} break;\ case 0x20|opc: {const int _2=1,_r=0;init body;} break;\ case 0x40|opc: {const int _2=0,_r=1;init body;} break;\ case 0x60|opc: {const int _2=1,_r=1;init body;} break;\ case 0x80|opc: {const int _2=0,_r=0,k=uxn.wst.ptr;init uxn.wst.ptr=k;body;} break;\ case 0xa0|opc: {const int _2=1,_r=0,k=uxn.wst.ptr;init uxn.wst.ptr=k;body;} break;\ case 0xc0|opc: {const int _2=0,_r=1,k=uxn.rst.ptr;init uxn.rst.ptr=k;body;} break;\ case 0xe0|opc: {const int _2=1,_r=1,k=uxn.rst.ptr;init uxn.rst.ptr=k;body;} break;\ } /* Microcode */ #define JMI a = uxn.ram[pc] << 8 | uxn.ram[pc + 1], pc += a + 2; #define REM if(_r) uxn.rst.ptr -= 1 + _2; else uxn.wst.ptr -= 1 + _2; #define INC(s) uxn.s.dat[uxn.s.ptr++] #define DEC(s) uxn.s.dat[--uxn.s.ptr] #define JMP(x) { if(_2) pc = x; else pc += (Sint8)x; } #define PO1(o) { o = _r ? DEC(rst) : DEC(wst);} #define PO2(o) { if(_r) o = DEC(rst), o |= DEC(rst) << 8; else o = DEC(wst), o |= DEC(wst) << 8; } #define POx(o) { if(_2) PO2(o) else PO1(o) } #define PU1(i) { if(_r) INC(rst) = i; else INC(wst) = i; } #define RP1(i) { if(_r) INC(wst) = i; else INC(rst) = i; } #define PUx(i) { if(_2) { c = (i); PU1(c >> 8) PU1(c) } else PU1(i) } #define GET(o) { if(_2) PO1(o[1]) PO1(o[0]) } #define PUT(i) { PU1(i[0]) if(_2) PU1(i[1]) } #define DEI(i,o) o[0] = emu_dei(i); if(_2) o[1] = emu_dei(i + 1); PUT(o) #define DEO(i,j) emu_deo(i, j[0]); if(_2) emu_deo(i + 1, j[1]); #define PEK(i,o,m) o[0] = uxn.ram[i]; if(_2) o[1] = uxn.ram[(i + 1) & m]; PUT(o) #define POK(i,j,m) uxn.ram[i] = j[0]; if(_2) uxn.ram[(i + 1) & m] = j[1]; int uxn_eval(Uint16 pc) { Uint32 a, b, c, x[2], y[2], z[2], step; if(!pc || uxn.dev[0x0f]) return 0; for(step = STEP_MAX; step; step--) { switch(uxn.ram[pc++]) { /* BRK */ case 0x00: return 1; /* JCI */ case 0x20: if(DEC(wst)) { JMI break; } pc += 2; break; /* JMI */ case 0x40: JMI break; /* JSI */ case 0x60: c = pc + 2; INC(rst) = c >> 8; INC(rst) = c; JMI break; /* LI2 */ case 0xa0: INC(wst) = uxn.ram[pc++]; /* fall-through */ /* LIT */ case 0x80: INC(wst) = uxn.ram[pc++]; break; /* L2r */ case 0xe0: INC(rst) = uxn.ram[pc++]; /* fall-through */ /* LIr */ case 0xc0: INC(rst) = uxn.ram[pc++]; break; /* INC */ OPC(0x01,POx(a),PUx(a + 1)) /* POP */ OPC(0x02,REM ,{}) /* NIP */ OPC(0x03,GET(x) REM ,PUT(x)) /* SWP */ OPC(0x04,GET(x) GET(y),PUT(x) PUT(y)) /* ROT */ OPC(0x05,GET(x) GET(y) GET(z),PUT(y) PUT(x) PUT(z)) /* DUP */ OPC(0x06,GET(x),PUT(x) PUT(x)) /* OVR */ OPC(0x07,GET(x) GET(y),PUT(y) PUT(x) PUT(y)) /* EQU */ OPC(0x08,POx(a) POx(b),PU1(b == a)) /* NEQ */ OPC(0x09,POx(a) POx(b),PU1(b != a)) /* GTH */ OPC(0x0a,POx(a) POx(b),PU1(b > a)) /* LTH */ OPC(0x0b,POx(a) POx(b),PU1(b < a)) /* JMP */ OPC(0x0c,POx(a),JMP(a)) /* JCN */ OPC(0x0d,POx(a) PO1(b),if(b) JMP(a)) /* JSR */ OPC(0x0e,POx(a),RP1(pc >> 8) RP1(pc) JMP(a)) /* STH */ OPC(0x0f,GET(x),RP1(x[0]) if(_2) RP1(x[1])) /* LDZ */ OPC(0x10,PO1(a),PEK(a, x, 0xff)) /* STZ */ OPC(0x11,PO1(a) GET(y),POK(a, y, 0xff)) /* LDR */ OPC(0x12,PO1(a),PEK(pc + (Sint8)a, x, 0xffff)) /* STR */ OPC(0x13,PO1(a) GET(y),POK(pc + (Sint8)a, y, 0xffff)) /* LDA */ OPC(0x14,PO2(a),PEK(a, x, 0xffff)) /* STA */ OPC(0x15,PO2(a) GET(y),POK(a, y, 0xffff)) /* DEI */ OPC(0x16,PO1(a),DEI(a, x)) /* DEO */ OPC(0x17,PO1(a) GET(y),DEO(a, y)) /* ADD */ OPC(0x18,POx(a) POx(b),PUx(b + a)) /* SUB */ OPC(0x19,POx(a) POx(b),PUx(b - a)) /* MUL */ OPC(0x1a,POx(a) POx(b),PUx(b * a)) /* DIV */ OPC(0x1b,POx(a) POx(b),PUx(a ? b / a : 0)) /* AND */ OPC(0x1c,POx(a) POx(b),PUx(b & a)) /* ORA */ OPC(0x1d,POx(a) POx(b),PUx(b | a)) /* EOR */ OPC(0x1e,POx(a) POx(b),PUx(b ^ a)) /* SFT */ OPC(0x1f,PO1(a) POx(b),PUx(b >> (a & 0xf) << (a >> 4))) } } return 0; } /* clang-format on */ /* @|System ------------------------------------------------------------ */ static char *system_boot_path; static void system_print(char *name, Stack *s) { Uint8 i; fprintf(stderr, "%s ", name); for(i = s->ptr - 8; i != (Uint8)(s->ptr); i++) fprintf(stderr, "%02x%c", s->dat[i], i == 0xff ? '|' : ' '); fprintf(stderr, "<%02x\n", s->ptr); } static int system_load(Uint8 *ram, char *rom_path) { FILE *f = fopen(rom_path, "rb"); if(f) { int i = 0, l = fread(ram, 0x10000 - PAGE_PROGRAM, 1, f); while(l && ++i < RAM_PAGES) l = fread(ram + 0x10000 * i - PAGE_PROGRAM, 0x10000, 1, f); fclose(f); } return !!f; } static int system_boot(Uint8 *ram, char *rom_path, int has_args) { uxn.ram = ram; system_boot_path = rom_path; uxn.dev[0x17] = has_args; if(ram && system_load(uxn.ram + PAGE_PROGRAM, rom_path)) return uxn_eval(PAGE_PROGRAM); return 0; } static int system_reboot(int soft) { int i; for(i = 0x0; i < 0x100; i++) uxn.dev[i] = 0; for(i = soft ? 0x100 : 0; i < 0x10000; i++) uxn.ram[i] = 0; uxn.wst.ptr = uxn.rst.ptr = 0; return system_boot(uxn.ram, system_boot_path, 0); } static Uint8 system_dei(Uint8 addr) { switch(addr) { case 0x4: return uxn.wst.ptr; case 0x5: return uxn.rst.ptr; default: return uxn.dev[addr]; } } static void system_deo(Uint8 port) { switch(port) { case 0x3: { Uint16 value; Uint16 addr = PEEK2(uxn.dev + 2); Uint8 *aptr = uxn.ram + addr; Uint16 length = PEEK2(aptr + 1); if(uxn.ram[addr] == 0x0) { Uint32 a = PEEK2(aptr + 3) * 0x10000 + PEEK2(aptr + 5); Uint32 b = a + length; value = uxn.ram[addr + 7]; for(; a < b; uxn.ram[a++] = value); } else if(uxn.ram[addr] == 0x1) { Uint32 a = PEEK2(aptr + 3) * 0x10000 + PEEK2(aptr + 5); Uint32 b = a + length; Uint32 c = PEEK2(aptr + 7) * 0x10000 + PEEK2(aptr + 9); for(; a < b; uxn.ram[c++] = uxn.ram[a++]); } else if(uxn.ram[addr] == 0x2) { Uint32 a = PEEK2(aptr + 3) * 0x10000 + PEEK2(aptr + 5); Uint32 b = a + length; Uint32 c = PEEK2(aptr + 7) * 0x10000 + PEEK2(aptr + 9); Uint32 d = c + length; for(; b >= a; uxn.ram[--d] = uxn.ram[--b]); } else fprintf(stderr, "Unknown command: %s\n", &uxn.ram[addr]); break; } case 0x4: uxn.wst.ptr = uxn.dev[4]; break; case 0x5: uxn.rst.ptr = uxn.dev[5]; break; case 0xe: system_print("WST", &uxn.wst); system_print("RST", &uxn.rst); break; } } /* @|Console ----------------------------------------------------------- */ #define CONSOLE_STD 0x1 #define CONSOLE_ARG 0x2 #define CONSOLE_EOA 0x3 #define CONSOLE_END 0x4 static int console_vector; #undef _POSIX_C_SOURCE #define _POSIX_C_SOURCE 200112L #include #include #include #ifdef __linux #include #include #endif #ifdef __NetBSD__ #include #include #endif /* subprocess support */ static char *fork_args[4] = {"/bin/sh", "-c", "", NULL}; static int child_mode; static int to_child_fd[2]; static int from_child_fd[2]; static int saved_in; static int saved_out; static pid_t child_pid; /* child_mode: * 0x01: writes to child's stdin * 0x02: reads from child's stdout * 0x04: reads from child's stderr * 0x08: kill previous process (if any) but do not start * (other bits ignored for now ) */ #define CMD_LIVE 0x15 /* 0x00 not started, 0x01 running, 0xff dead */ #define CMD_EXIT 0x16 /* if dead, exit code of process */ #define CMD_ADDR 0x1c /* address to read command args from */ #define CMD_MODE 0x1e /* mode to execute, 0x00 to 0x07 */ #define CMD_EXEC 0x1f /* write to execute programs, etc */ /* call after we're sure the process has exited */ static void clean_after_child(void) { child_pid = 0; if(child_mode & 0x01) { close(to_child_fd[1]); dup2(saved_out, 1); } if(child_mode & (0x04 | 0x02)) { close(from_child_fd[0]); dup2(saved_in, 0); } child_mode = 0; saved_in = -1; saved_out = -1; } static void start_fork_pipe(void) { pid_t pid; pid_t parent_pid = getpid(); int addr = PEEK2(&uxn.dev[CMD_ADDR]); fflush(stdout); if(child_mode & 0x08) { uxn.dev[CMD_EXIT] = uxn.dev[CMD_LIVE] = 0x00; return; } if(child_mode & 0x01) { /* parent writes to child's stdin */ if(pipe(to_child_fd) == -1) { uxn.dev[CMD_EXIT] = uxn.dev[CMD_LIVE] = 0xff; fprintf(stderr, "Pipe error to child.\n"); return; } } if(child_mode & (0x04 | 0x02)) { /* parent reads from child's stdout and/or stderr */ if(pipe(from_child_fd) == -1) { uxn.dev[CMD_EXIT] = uxn.dev[CMD_LIVE] = 0xff; fprintf(stderr, "Pipe error from child.\n"); return; } } fork_args[2] = (char *)&uxn.ram[addr]; pid = fork(); if(pid < 0) { /* failure */ uxn.dev[CMD_EXIT] = uxn.dev[CMD_LIVE] = 0xff; fprintf(stderr, "Fork failure.\n"); } else if(pid == 0) { /* child */ #ifdef __linux__ int r = prctl(PR_SET_PDEATHSIG, SIGTERM); if(r == -1) { perror(0); exit(6); } if(getppid() != parent_pid) exit(13); #endif if(child_mode & 0x01) { dup2(to_child_fd[0], 0); close(to_child_fd[1]); } if(child_mode & (0x04 | 0x02)) { if(child_mode & 0x02) dup2(from_child_fd[1], 1); if(child_mode & 0x04) dup2(from_child_fd[1], 2); close(from_child_fd[0]); } fflush(stdout); execvp(fork_args[0], fork_args); exit(1); } else { /*parent*/ child_pid = pid; uxn.dev[CMD_LIVE] = 0x01; uxn.dev[CMD_EXIT] = 0x00; if(child_mode & 0x01) { saved_out = dup(1); dup2(to_child_fd[1], 1); close(to_child_fd[0]); } if(child_mode & (0x04 | 0x02)) { saved_in = dup(0); dup2(from_child_fd[0], 0); close(from_child_fd[1]); } } } static void check_child(void) { int wstatus; if(child_pid) { if(waitpid(child_pid, &wstatus, WNOHANG)) { uxn.dev[CMD_LIVE] = 0xff; uxn.dev[CMD_EXIT] = WEXITSTATUS(wstatus); clean_after_child(); } else { uxn.dev[CMD_LIVE] = 0x01; uxn.dev[CMD_EXIT] = 0x00; } } } static void kill_child(void) { int wstatus; if(child_pid) { kill(child_pid, 9); if(waitpid(child_pid, &wstatus, WNOHANG)) { uxn.dev[CMD_LIVE] = 0xff; uxn.dev[CMD_EXIT] = WEXITSTATUS(wstatus); clean_after_child(); } } } static void start_fork(void) { fflush(stderr); kill_child(); child_mode = uxn.dev[CMD_MODE]; start_fork_pipe(); } static void console_close(void) { kill_child(); } static int console_input(int c, int type) { if(c == EOF) c = 0, type = 4; uxn.dev[0x12] = c, uxn.dev[0x17] = type; uxn_eval(console_vector); return type != 4; } static void console_arguments(int i, int argc, char **argv) { for(; i < argc; i++) { char *p = argv[i]; while(*p) console_input(*p++, CONSOLE_ARG); console_input('\n', i == argc - 1 ? CONSOLE_END : CONSOLE_EOA); } } static Uint8 console_dei(Uint8 addr) { switch(addr) { case CMD_LIVE: case CMD_EXIT: check_child(); break; } return uxn.dev[addr]; } static void console_deo(Uint8 addr) { FILE *fd; switch(addr) { case 0x11: console_vector = PEEK2(&uxn.dev[0x10]); return; case 0x18: fd = stdout, fputc(uxn.dev[0x18], fd), fflush(fd); break; case 0x19: fd = stderr, fputc(uxn.dev[0x19], fd), fflush(fd); break; case CMD_EXEC: start_fork(); break; } } /* @|Screen ------------------------------------------------------------ */ #define MAR(x) (x + 0x8) #define MAR2(x) (x + 0x10) typedef struct UxnScreen { int width, height, vector, zoom, x1, y1, x2, y2; Uint32 palette[16], *pixels; Uint8 *fg, *bg; } UxnScreen; void emu_resize(void); static UxnScreen uxn_screen; static Uint8 blending[4][16] = { {0, 0, 0, 0, 1, 0, 1, 1, 2, 2, 0, 2, 3, 3, 3, 0}, {0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3}, {1, 2, 3, 1, 1, 2, 3, 1, 1, 2, 3, 1, 1, 2, 3, 1}, {2, 3, 1, 2, 2, 3, 1, 2, 2, 3, 1, 2, 2, 3, 1, 2}}; static int screen_changed(void) { clamp(uxn_screen.x1, 0, uxn_screen.width); clamp(uxn_screen.y1, 0, uxn_screen.height); clamp(uxn_screen.x2, 0, uxn_screen.width); clamp(uxn_screen.y2, 0, uxn_screen.height); return uxn_screen.x2 > uxn_screen.x1 && uxn_screen.y2 > uxn_screen.y1; } static void screen_change(int x1, int y1, int x2, int y2) { if(x1 < uxn_screen.x1) uxn_screen.x1 = x1; if(y1 < uxn_screen.y1) uxn_screen.y1 = y1; if(x2 > uxn_screen.x2) uxn_screen.x2 = x2; if(y2 > uxn_screen.y2) uxn_screen.y2 = y2; } static void screen_palette(void) { int i, shift, colors[4]; for(i = 0, shift = 4; i < 4; ++i, shift ^= 4) { Uint8 r = (uxn.dev[0x8 + i / 2] >> shift) & 0xf, g = (uxn.dev[0xa + i / 2] >> shift) & 0xf, b = (uxn.dev[0xc + i / 2] >> shift) & 0xf; colors[i] = 0x0f000000 | r << 16 | g << 8 | b; colors[i] |= colors[i] << 4; } for(i = 0; i < 16; i++) uxn_screen.palette[i] = colors[(i >> 2) ? (i >> 2) : (i & 3)]; screen_change(0, 0, uxn_screen.width, uxn_screen.height); } static void screen_apply(int width, int height) { int length; clamp(width, 8, 0x800); clamp(height, 8, 0x800); if(width == uxn_screen.width && height == uxn_screen.height) return; length = MAR2(width) * MAR2(height); uxn_screen.bg = realloc(uxn_screen.bg, length), uxn_screen.fg = realloc(uxn_screen.fg, length); memset(uxn_screen.bg, 0, length); memset(uxn_screen.fg, 0, length); uxn_screen.width = width, uxn_screen.height = height; screen_change(0, 0, width, height); emu_resize(); } static void screen_redraw(void) { int i, x, y, k, l; for(y = uxn_screen.y1; y < uxn_screen.y2; y++) { int ys = y * uxn_screen.zoom; for(x = uxn_screen.x1, i = MAR(x) + MAR(y) * MAR2(uxn_screen.width); x < uxn_screen.x2; x++, i++) { int c = uxn_screen.palette[uxn_screen.fg[i] << 2 | uxn_screen.bg[i]]; for(k = 0; k < uxn_screen.zoom; k++) { int oo = ((ys + k) * uxn_screen.width + x) * uxn_screen.zoom; for(l = 0; l < uxn_screen.zoom; l++) uxn_screen.pixels[oo + l] = c; } } } uxn_screen.x1 = uxn_screen.y1 = 9999; uxn_screen.x2 = uxn_screen.y2 = 0; } /* screen registers */ static int rX, rY, rA, rMX, rMY, rMA, rML, rDX, rDY; static Uint8 screen_dei(Uint8 addr) { switch(addr) { case 0x22: return uxn_screen.width >> 8; case 0x23: return uxn_screen.width; case 0x24: return uxn_screen.height >> 8; case 0x25: return uxn_screen.height; case 0x28: return rX >> 8; case 0x29: return rX; case 0x2a: return rY >> 8; case 0x2b: return rY; case 0x2c: return rA >> 8; case 0x2d: return rA; default: return uxn.dev[addr]; } } static void screen_deo(Uint8 addr) { switch(addr) { case 0x21: uxn_screen.vector = PEEK2(&uxn.dev[0x20]); return; case 0x23: screen_apply(PEEK2(&uxn.dev[0x22]), uxn_screen.height); return; case 0x25: screen_apply(uxn_screen.width, PEEK2(&uxn.dev[0x24])); return; case 0x26: rMX = uxn.dev[0x26] & 0x1, rMY = uxn.dev[0x26] & 0x2, rMA = uxn.dev[0x26] & 0x4, rML = uxn.dev[0x26] >> 4, rDX = rMX << 3, rDY = rMY << 2; return; case 0x28: case 0x29: rX = (uxn.dev[0x28] << 8) | uxn.dev[0x29], rX = twos(rX); return; case 0x2a: case 0x2b: rY = (uxn.dev[0x2a] << 8) | uxn.dev[0x2b], rY = twos(rY); return; case 0x2c: case 0x2d: rA = (uxn.dev[0x2c] << 8) | uxn.dev[0x2d]; return; case 0x2e: { int ctrl = uxn.dev[0x2e]; int color = ctrl & 0x3; int len = MAR2(uxn_screen.width); Uint8 *layer = ctrl & 0x40 ? uxn_screen.fg : uxn_screen.bg; /* fill mode */ if(ctrl & 0x80) { int x1, y1, x2, y2, ax, bx, ay, by, hor, ver; if(ctrl & 0x10) x1 = 0, x2 = rX; else x1 = rX, x2 = uxn_screen.width; if(ctrl & 0x20) y1 = 0, y2 = rY; else y1 = rY, y2 = uxn_screen.height; screen_change(x1, y1, x2, y2); x1 = MAR(x1), y1 = MAR(y1); hor = MAR(x2) - x1, ver = MAR(y2) - y1; for(ay = y1 * len, by = ay + ver * len; ay < by; ay += len) for(ax = ay + x1, bx = ax + hor; ax < bx; ax++) layer[ax] = color; } /* pixel mode */ else { if(rX >= 0 && rY >= 0 && rX < len && rY < uxn_screen.height) layer[MAR(rX) + MAR(rY) * len] = color; screen_change(rX, rY, rX + 1, rY + 1); if(rMX) rX++; if(rMY) rY++; } return; } case 0x2f: { int ctrl = uxn.dev[0x2f]; int blend = ctrl & 0xf, opaque = blend % 5; int fx = ctrl & 0x10 ? -1 : 1, fy = ctrl & 0x20 ? -1 : 1; int qfx = fx > 0 ? 7 : 0, qfy = fy < 0 ? 7 : 0; int dxy = fy * rDX, dyx = fx * rDY; int wmar = MAR(uxn_screen.width), wmar2 = MAR2(uxn_screen.width); int hmar2 = MAR2(uxn_screen.height); int i, x1, x2, y1, y2, ax, ay, qx, qy, x = rX, y = rY; Uint8 *layer = ctrl & 0x40 ? uxn_screen.fg : uxn_screen.bg; if(ctrl & 0x80) { int addr_incr = rMA << 2; for(i = 0; i <= rML; i++, x += dyx, y += dxy, rA += addr_incr) { Uint16 xmar = MAR(x), ymar = MAR(y); Uint16 xmar2 = MAR2(x), ymar2 = MAR2(y); if(xmar < wmar && ymar2 < hmar2) { Uint8 *sprite = &uxn.ram[rA]; int by = ymar2 * wmar2; for(ay = ymar * wmar2, qy = qfy; ay < by; ay += wmar2, qy += fy) { int ch1 = sprite[qy], ch2 = sprite[qy + 8] << 1, bx = xmar2 + ay; for(ax = xmar + ay, qx = qfx; ax < bx; ax++, qx -= fx) { int color = ((ch1 >> qx) & 1) | ((ch2 >> qx) & 2); if(opaque || color) layer[ax] = blending[color][blend]; } } } } } else { int addr_incr = rMA << 1; for(i = 0; i <= rML; i++, x += dyx, y += dxy, rA += addr_incr) { Uint16 xmar = MAR(x), ymar = MAR(y); Uint16 xmar2 = MAR2(x), ymar2 = MAR2(y); if(xmar < wmar && ymar2 < hmar2) { Uint8 *sprite = &uxn.ram[rA]; int by = ymar2 * wmar2; for(ay = ymar * wmar2, qy = qfy; ay < by; ay += wmar2, qy += fy) { int ch1 = sprite[qy], bx = xmar2 + ay; for(ax = xmar + ay, qx = qfx; ax < bx; ax++, qx -= fx) { int color = (ch1 >> qx) & 1; if(opaque || color) layer[ax] = blending[color][blend]; } } } } } if(fx < 0) x1 = x, x2 = rX; else x1 = rX, x2 = x; if(fy < 0) y1 = y, y2 = rY; else y1 = rY, y2 = y; screen_change(x1 - 8, y1 - 8, x2 + 8, y2 + 8); if(rMX) rX += rDX * fx; if(rMY) rY += rDY * fy; return; } } } /* @|Controller -------------------------------------------------------- */ static int controller_vector; static void controller_down(Uint8 mask) { if(mask) { uxn.dev[0x82] |= mask; uxn_eval(controller_vector); } } static void controller_up(Uint8 mask) { if(mask) { uxn.dev[0x82] &= (~mask); uxn_eval(controller_vector); } } static void controller_key(Uint8 key) { if(key) { uxn.dev[0x83] = key; uxn_eval(controller_vector); uxn.dev[0x83] = 0; } } static void controller_deo(Uint8 addr) { switch(addr) { case 0x81: controller_vector = PEEK2(&uxn.dev[0x80]); break; } } /* @|Mouse ------------------------------------------------------------- */ static int mouse_vector; static void mouse_down(Uint8 mask) { uxn.dev[0x96] |= mask; uxn_eval(mouse_vector); } static void mouse_up(Uint8 mask) { uxn.dev[0x96] &= (~mask); uxn_eval(mouse_vector); } static void mouse_pos(Uint16 x, Uint16 y) { uxn.dev[0x92] = x >> 8, uxn.dev[0x93] = x; uxn.dev[0x94] = y >> 8, uxn.dev[0x95] = y; uxn_eval(mouse_vector); } static void mouse_scroll(Uint16 x, Uint16 y) { uxn.dev[0x9a] = x >> 8, uxn.dev[0x9b] = x; uxn.dev[0x9c] = -y >> 8, uxn.dev[0x9d] = -y; uxn_eval(mouse_vector); uxn.dev[0x9a] = 0, uxn.dev[0x9b] = 0; uxn.dev[0x9c] = 0, uxn.dev[0x9d] = 0; } static void mouse_deo(Uint8 addr) { switch(addr) { case 0x91: mouse_vector = PEEK2(&uxn.dev[0x90]); break; } } /* @|File -------------------------------------------------------------- */ #include #include typedef struct { FILE *f; DIR *dir; char *filepath; enum { IDLE, FILE_READ, FILE_WRITE, DIR_READ, DIR_WRITE } state; } UxnFile; static UxnFile ufs[2]; static Uint8 dirbuf[0x10000], *_dirbuf = dirbuf; static void make_pathfile(char *pathbuf, const char *filepath, const char *basename) { char c = '/'; while(*filepath) c = *filepath++, *pathbuf = c, pathbuf++; if(c != '/') *pathbuf = '/', pathbuf++; while(*basename) *pathbuf = *basename++, pathbuf++; *pathbuf = 0; } static int put_fill(Uint8 *dest, int len, char c) { int i; for(i = 0; i < len; i++) *dest = c, dest++; return len; } static int put_size(Uint8 *dest, int len, int size) { int i; for(i = 0, dest += len; i < len; i++, size >>= 4) *(--dest) = "0123456789abcdef"[(Uint8)(size & 0xf)]; return len; } static int put_text(Uint8 *dest, const char *text) { Uint8 *anchor = dest; while(*text) *dest = *text++, dest++; *dest = 0; return dest - anchor; } static int put_stat(Uint8 *dest, int len, int size, int err, int dir, int capsize) { if(err) return put_fill(dest, len, '!'); if(dir) return put_fill(dest, len, '-'); if(capsize && size >= 0x10000) return put_fill(dest, len, '?'); return put_size(dest, len, size); } static int put_statfile(Uint8 *dest, const char *filepath, const char *basename) { int err, dir; struct stat st; Uint8 *anchor = dest; char pathbuf[0x2000]; make_pathfile(pathbuf, filepath, basename); err = stat(pathbuf, &st); dir = S_ISDIR(st.st_mode); dest += put_stat(dest, 4, st.st_size, err, dir, 1); dest += put_text(dest, " "); dest += put_text(dest, basename); dest += put_text(dest, dir ? "/\n" : "\n"); return dest - anchor; } static int put_fdir(Uint8 *dest, int len, const char *filepath, DIR *dir) { int i; struct dirent *de = readdir(dir); for(_dirbuf = dirbuf; de != NULL; de = readdir(dir)) { char *name = de->d_name; if(name[0] == '.' && (name[1] == '.' || name[1] == '\0')) continue; else _dirbuf += put_statfile(_dirbuf, filepath, name); } for(i = 0; i < len && dirbuf[i]; i++) dest[i] = dirbuf[i]; dest[i] = 0; return i; } static int is_dir_path(char *p) { char c; int saw_slash = 0; while((c = *p++)) saw_slash = c == '/'; return saw_slash; } static int is_dir_real(char *p) { struct stat st; return stat(p, &st) == 0 && S_ISDIR(st.st_mode); } static int file_write_dir(char *p) { int ok = 1; char c, *s = p; for(; ok && (c = *p); p++) { if(c == '/') { *p = '\0'; ok = is_dir_real(s) || (mkdir(s, 0755) == 0); *p = c; } } return ok; } static void file_reset(int id) { if(ufs[id].f != NULL) fclose(ufs[id].f), ufs[id].f = NULL; if(ufs[id].dir != NULL) closedir(ufs[id].dir), ufs[id].dir = NULL; ufs[id].state = IDLE; } static int file_init(int id, Uint16 addr) { file_reset(id); ufs[id].filepath = (char *)&uxn.ram[addr]; return 0; } static int file_not_ready(int id) { if(ufs[id].filepath == 0) { fprintf(stderr, "File %d is uninitialized\n", id); return 1; } else return 0; } static int file_read(int id, Uint16 addr, int len) { void *dest = &uxn.ram[addr]; if(file_not_ready(id)) return 0; if(ufs[id].state != FILE_READ && ufs[id].state != DIR_READ) { file_reset(id); if((ufs[id].dir = opendir(ufs[id].filepath)) != NULL) ufs[id].state = DIR_READ; else if((ufs[id].f = fopen(ufs[id].filepath, "rb")) != NULL) ufs[id].state = FILE_READ; } if(ufs[id].state == FILE_READ) return fread(dest, 1, len, ufs[id].f); if(ufs[id].state == DIR_READ) return put_fdir(dest, len, ufs[id].filepath, ufs[id].dir); return 0; } static int file_write(int id, Uint16 addr, int len, Uint8 flags) { int ret = 0; if(file_not_ready(id)) return 0; file_write_dir(ufs[id].filepath); if(ufs[id].state != FILE_WRITE && ufs[id].state != DIR_WRITE) { file_reset(id); if(is_dir_path(ufs[id].filepath)) ufs[id].state = DIR_WRITE; else if((ufs[id].f = fopen(ufs[id].filepath, (flags & 0x01) ? "ab" : "wb")) != NULL) ufs[id].state = FILE_WRITE; } if(ufs[id].state == FILE_WRITE) if((ret = fwrite(&uxn.ram[addr], 1, len, ufs[id].f)) > 0 && fflush(ufs[id].f) != 0) ret = 0; if(ufs[id].state == DIR_WRITE) ret = is_dir_real(ufs[id].filepath); return ret; } static int file_stat(int id, Uint16 addr, int len) { int err, dir; struct stat st; if(file_not_ready(id)) return 0; err = stat(ufs[id].filepath, &st); dir = S_ISDIR(st.st_mode); return put_stat(&uxn.ram[addr], len, st.st_size, err, dir, 0); } static int file_delete(int id) { if(file_not_ready(id)) return -1; return unlink(ufs[id].filepath); } static void file_success(int port, int value) { POKE2(&uxn.dev[port], value); } /* file registers */ static int rL1, rL2; static void file_deo(Uint8 port) { switch(port) { /* File 1 */ case 0xab: rL1 = PEEK2(&uxn.dev[0xaa]); break; case 0xa5: file_success(0xa2, file_stat(0, PEEK2(&uxn.dev[0xa4]), rL1)); break; case 0xa6: file_success(0xa2, file_delete(0)); break; case 0xa9: file_success(0xa2, file_init(0, PEEK2(&uxn.dev[0xa8]))); break; case 0xad: file_success(0xa2, file_read(0, PEEK2(&uxn.dev[0xac]), rL1)); break; case 0xaf: file_success(0xa2, file_write(0, PEEK2(&uxn.dev[0xae]), rL1, uxn.dev[0xa7])); break; /* File 2 */ case 0xbb: rL2 = PEEK2(&uxn.dev[0xba]); break; case 0xb5: file_success(0xb2, file_stat(1, PEEK2(&uxn.dev[0xb4]), rL2)); break; case 0xb6: file_success(0xb2, file_delete(1)); break; case 0xb9: file_success(0xb2, file_init(1, PEEK2(&uxn.dev[0xb8]))); break; case 0xbd: file_success(0xb2, file_read(1, PEEK2(&uxn.dev[0xbc]), rL2)); break; case 0xbf: file_success(0xb2, file_write(1, PEEK2(&uxn.dev[0xbe]), rL2, uxn.dev[0xb7])); break; } } /* @|Datetime ---------------------------------------------------------- */ #include static Uint8 datetime_dei(Uint8 addr) { time_t seconds = time(NULL); struct tm zt = {0}; struct tm *t = localtime(&seconds); if(t == NULL) t = &zt; switch(addr) { case 0xc0: return (t->tm_year + 1900) >> 8; case 0xc1: return (t->tm_year + 1900); case 0xc2: return t->tm_mon; case 0xc3: return t->tm_mday; case 0xc4: return t->tm_hour; case 0xc5: return t->tm_min; case 0xc6: return t->tm_sec; case 0xc7: return t->tm_wday; case 0xc8: return t->tm_yday >> 8; case 0xc9: return t->tm_yday; case 0xca: return t->tm_isdst; default: return uxn.dev[addr]; } } /* @|Core -------------------------------------------------------------- */ #define CONINBUFSIZE 256 Uint8 emu_dei(Uint8 addr) { switch(addr & 0xf0) { case 0x00: return system_dei(addr); case 0x10: return console_dei(addr); case 0x20: return screen_dei(addr); case 0xc0: return datetime_dei(addr); } return uxn.dev[addr]; } void emu_deo(Uint8 addr, Uint8 value) { uxn.dev[addr] = value; switch(addr & 0xf0) { case 0x00: system_deo(addr); if(addr > 0x7 && addr < 0xe) screen_palette(); break; case 0x10: console_deo(addr); break; case 0x20: screen_deo(addr); break; case 0x80: controller_deo(addr); break; case 0x90: mouse_deo(addr); break; case 0xa0: file_deo(addr); break; case 0xb0: file_deo(addr); break; } } static void emu_repaint(void) { int x = uxn_screen.x1 * uxn_screen.zoom; int y = uxn_screen.y1 * uxn_screen.zoom; int w = uxn_screen.x2 * uxn_screen.zoom - x; int h = uxn_screen.y2 * uxn_screen.zoom - y; screen_redraw(); XPutImage(display, window, DefaultGC(display, 0), ximage, x, y, x, y, w, h); } void emu_resize(void) { int width = uxn_screen.width * uxn_screen.zoom; int height = uxn_screen.height * uxn_screen.zoom; XResizeWindow(display, window, width, height); uxn_screen.pixels = realloc(uxn_screen.pixels, uxn_screen.width * uxn_screen.height * sizeof(Uint32) * uxn_screen.zoom * uxn_screen.zoom); if(ximage) free(ximage); ximage = XCreateImage(display, DefaultVisual(display, 0), DefaultDepth(display, DefaultScreen(display)), ZPixmap, 0, (char *)uxn_screen.pixels, width, height, 32, 0); uxn_screen.x1 = uxn_screen.y1 = 0; uxn_screen.x2 = uxn_screen.width; uxn_screen.y2 = uxn_screen.height; emu_repaint(); } static void emu_restart(int soft) { console_close(); screen_apply(WIDTH, HEIGHT); system_reboot(soft); } static void emu_rescale(void) { uxn_screen.zoom = uxn_screen.zoom >= 3 ? 1 : uxn_screen.zoom + 1; emu_resize(); } static Uint8 get_button(KeySym sym) { switch(sym) { case XK_Up: return 0x10; case XK_Down: return 0x20; case XK_Left: return 0x40; case XK_Right: return 0x80; case XK_Control_L: return 0x01; case XK_Alt_L: return 0x02; case XK_Shift_L: return 0x04; case XK_Home: return 0x08; case XK_Meta_L: return 0x02; } return 0x00; } static void emu_event(void) { char buf[7]; XEvent ev; XNextEvent(display, &ev); switch(ev.type) { case Expose: screen_change(0, 0, 9000, 9000); break; case ClientMessage: uxn.dev[0x0f] = 0x80; break; case KeyPress: { KeySym sym; XLookupString((XKeyPressedEvent *)&ev, buf, 7, &sym, 0); switch(sym) { case XK_F1: emu_rescale(); break; case XK_F2: emu_deo(0xe, 0x1); break; case XK_F4: emu_restart(0); break; case XK_F5: emu_restart(1); break; } controller_down(get_button(sym)); if(XLookupKeysym((XKeyPressedEvent *)&ev, 0) == XK_Tab) buf[0] = 0x9; controller_key(sym < 0x80 ? sym : (Uint8)buf[0]); } break; case KeyRelease: { KeySym sym; XLookupString((XKeyPressedEvent *)&ev, buf, 7, &sym, 0); controller_up(get_button(sym)); } break; case ButtonPress: { XButtonPressedEvent *e = (XButtonPressedEvent *)&ev; switch(e->button) { case 4: mouse_scroll(0, 1); break; case 5: mouse_scroll(0, -1); break; case 6: mouse_scroll(1, 0); break; case 7: mouse_scroll(-1, 0); break; default: mouse_down(0x1 << (e->button - 1)); } } break; case ButtonRelease: { XButtonPressedEvent *e = (XButtonPressedEvent *)&ev; mouse_up(0x1 << (e->button - 1)); } break; case LeaveNotify: case MotionNotify: { XMotionEvent *e = (XMotionEvent *)&ev; mouse_pos(e->x / uxn_screen.zoom, e->y / uxn_screen.zoom); } break; } } static void display_hidecursor(void) { XColor black = {0}; char empty[] = {0}; Pixmap bitmap = XCreateBitmapFromData(display, window, empty, 1, 1); Cursor blank = XCreatePixmapCursor(display, bitmap, bitmap, &black, &black, 0, 0); XDefineCursor(display, window, blank); XFreeCursor(display, blank); XFreePixmap(display, bitmap); } static void display_floating(Display *dpy, Window win) { Atom wmDelete = XInternAtom(display, "WM_DELETE_WINDOW", True); Atom wmType = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False); Atom wmDialog = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False); XSetWMProtocols(display, window, &wmDelete, 1); XChangeProperty(dpy, win, wmType, 4, 32, PropModeReplace, (unsigned char *)&wmDialog, 1); } static int display_init(void) { Visual *visual; XClassHint class = {"uxn11", "Uxn"}; /* start display */ display = XOpenDisplay(NULL); if(!display) return !fprintf(stderr, "Display: failed\n"); /* start window */ visual = DefaultVisual(display, 0); if(visual->class != TrueColor) return !fprintf(stderr, "Display: True-color visual failed\n"); window = XCreateSimpleWindow(display, RootWindow(display, 0), 0, 0, WIDTH, HEIGHT, 1, 0, 0); display_hidecursor(); display_floating(display, window); XSelectInput(display, window, ButtonPressMask | ButtonReleaseMask | PointerMotionMask | ExposureMask | KeyPressMask | KeyReleaseMask | LeaveWindowMask); XStoreName(display, window, "uxn11"); XSetClassHint(display, window, &class); XMapWindow(display, window); uxn_screen.zoom = 1; screen_apply(WIDTH, HEIGHT); return 1; } static int emu_run(void) { int has_input, has_eof; char expirations[8], coninp[CONINBUFSIZE]; struct pollfd fds[3]; static const struct itimerspec screen_tspec = {{0, 16666666}, {0, 16666666}}; /* timer */ fds[0].fd = XConnectionNumber(display); fds[1].fd = timerfd_create(CLOCK_MONOTONIC, 0); timerfd_settime(fds[1].fd, 0, &screen_tspec, NULL); fds[2].fd = STDIN_FILENO; fds[0].events = fds[1].events = fds[2].events = POLLIN; /* main loop */ while(!uxn.dev[0x0f]) { if(poll(fds, 3, 1000) <= 0) continue; while(XPending(display)) emu_event(); if(poll(&fds[1], 1, 0)) { read(fds[1].fd, expirations, 8); if(uxn_screen.vector) uxn_eval(uxn_screen.vector); if(uxn_screen.x2 && uxn_screen.y2 && screen_changed()) emu_repaint(); } has_input = fds[2].revents & POLLIN; has_eof = fds[2].revents & POLLHUP; if(has_input || has_eof) { int i = 1, n = read(fds[2].fd, coninp, CONINBUFSIZE - 1); for(i = 0, coninp[n] = 0; i < n; i++) console_input(coninp[i], CONSOLE_STD); if(n == 0) console_input(0, CONSOLE_STD); } } return 1; } int main(int argc, char **argv) { int i = 1; if(argc == 2 && argv[1][0] == '-' && argv[1][1] == 'v') return !fprintf(stdout, "Uxn11 - Varvara Emulator, 23 Apr 2025.\n"); else if(argc == 1) return !fprintf(stdout, "usage: %s [-v] file.rom [args..]\n", argv[0]); else if(!display_init()) return !fprintf(stdout, "Could not open display.\n"); else if(!system_boot((Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8)), argv[i++], argc > 2)) return !fprintf(stdout, "Could not load %s.\n", argv[i - 1]); console_arguments(i, argc, argv); emu_run(); console_close(); XDestroyImage(ximage), XDestroyWindow(display, window), XCloseDisplay(display); return uxn.dev[0x0f] & 0x7f; }