ilo-vm/source/x11-mult-ilo.c

523 lines
14 KiB
C
Raw Normal View History

/***************************************************************
crc's _ _ ___ _ __
_ __ ___ _ _| | |_ / (_) | ___ / /_ __
| '_ ` _ \| | | | | __| / /| | |/ _ \ / /\ \/ /
| | | | | | |_| | | |_ / / | | | (_) / / > <
|_| |_| |_|\__,_|_|\__/_/ |_|_|\___/_/ /_/\_\
(c) charles childers
This is a special implementation of my ilo computer, with many
copies of ilo running under a single process. Under this you
can quickly switch between ilo instances.
- `bye` will act like `restart` on the current computer
- ctrl+c restarts the current computer
- ctrl+n switches to the next ilo
- ctrl+p switches to the previous ilo
- ctrl+a shuts down mult/ilo
- switching can only be done when the system is waiting for
input
**************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#define BLOCKS "ilo.blocks"
#define ROM "ilo.rom"
#define FONT "ilo.fnt"
#define FG 0xFFFFFF
#define BG 0x000000
/* Orange = 0xFFAA00, Red = 0xFF0000, Cyan = 0x00FFFF */
#define CURSOR 0xFFAA00
#define FW 640
#define FH 384
#define FONT_H 16
#define FONT_W 8
#define V void
#define I int
#define C char
#define ILOS 3
typedef struct ilo ilo;
struct ilo {
I ip, /* instruction pointer */
sp, /* data stack pointer */
rp, /* address stack pointer */
ds[33], /* data stack */
as[257], /* address stack */
m[65536]; /* memory */
C *blocks, /* name of block file (ilo.blocks) */
*rom; /* name of image (ilo.rom) */
I a, b, f, s, d, l, ok;
C i[1];
};
#define TERM_W (FW / FONT_W)
#define TERM_H (FH / FONT_H)
I frame[(FW * FH)/32]; /* frame buffer for display */
I font[4096]; /* font bitmap */
I tx, ty; /* text cursor location */
/* variables for Arland's DEC subset */
I acc[32], acc_i = 0, ground = 0, on = 0;
/* variables for X11 stuff */
Display *disp;
Window win;
GC gc;
XEvent event;
C text[255];
V dputc(I);
V redraw();
struct ilo systems[ILOS];
I cur;
#define DS v->ds
#define AS v->as
#define SP v->sp
#define RP v->rp
#define IP v->ip
#define M v->m
#define A v->a
#define B v->b
#define S v->s
#define D v->d
#define L v->l
#define T DS[SP] /* Top of Data Stack */
#define N DS[SP-1] /* Next on Data Stack */
#define R AS[RP] /* Top of Address Stack */
#define F v->f
V iof(ilo *);
V p(char *s) { while(*s) dputc(*s++); }
V e(ilo *v, char *s) { p("E:"); p(s); p("\n"); v->ok = 1; iof(v); }
V dso(ilo *v) { if ((SP + 1) > 32) { e(v, "DSO"); } }
V dsu(ilo *v) { if ((SP - 1) < 0) { e(v, "DSU"); } }
V rso(ilo *v) { if ((RP + 1) > 256) { e(v, "RSO"); } }
V rsu(ilo *v) { if ((RP - 1) < 0) { e(v, "RSU"); } }
V dbz(ilo *v) { if (T == 0) { e(v, "DBZ"); } }
V mem(ilo *v) { if (T < 0 || T > 65535) { e(v, "MEM"); } }
V uli(ilo *v) { if (F == -1) { e(v, "ULI"); } }
V ulf(ilo *v) { if (F == -1) { e(v, "ULF"); } }
V usi(ilo *v) { if (F == -1) { e(v, "URI"); } }
V urb(ilo *v) { if (F == -1) { e(v, "URB"); } }
V uwb(ilo *v) { if (F == -1) { e(v, "UWB"); } }
V push(ilo *v, I n) { dso(v); DS[SP + 1] = n; SP += 1; }
I pop(ilo *v) { dsu(v); SP -= 1; return DS[SP + 1]; }
V cursor() {
XSetForeground(disp, gc, CURSOR);
XFillRectangle(disp, win, gc, tx, ty, FONT_W, FONT_H);
XFlush(disp);
}
I wait_key() {
while (1) {
XNextEvent(disp, &event);
if (event.type == KeyPress) {
KeySym key = XLookupKeysym(&event.xkey, 0);
XLookupString(&event.xkey, text, 255, &key, 0);
if (text[0] == 13) text[0] = 10;
if (text[0] != 0) return (I) text[0];
}
if (event.type == Expose) { redraw(); cursor(); }
}
}
V load_font(ilo *v) {
F = open(FONT, O_RDONLY, 0666);
ulf(v);
if (!F) { return; };
read(F, &font, 4096);
close(F);
}
V pixel(I x, I y, I c) {
I index = y * FW + x;
if (c == 0) {
frame[(index / 32)] &= ~(1 << (31 - index % 32));
XSetForeground(disp, gc, BG);
} else {
frame[(index / 32)] |= 1 << (31 - index % 32);
XSetForeground(disp, gc, FG);
}
XDrawPoint(disp, win, gc, x, y);
}
I get_pixel(I x, I y) {
I index = y * FW + x;
I bit_pos = 31 - index % 32;
I mask = 1 << bit_pos;
return (frame[(index / 32)] & mask) >> bit_pos;
}
V redraw() {
for (I x = 0; x < FW; x++) {
for (I y = 0; y < FH; y++) {
XSetForeground(disp, gc, get_pixel(x, y) ? FG : BG);
XDrawPoint(disp, win, gc, x, y);
}
}
}
V scroll() {
I memsize = (FH * FW) / 32;
I last_row = (FW * FONT_H) / 32;
for (I i = 0; i < memsize - last_row; i++) {
frame[i] = frame[i + last_row];
}
for (I i = memsize - last_row; i < memsize; i++) {
frame[i] = 0;
}
redraw();
}
unsigned C reverse(unsigned C b) {
b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
return b;
}
V handle_special_character(I c) {
if (c == 8 || c == 127) { /* BACKSPACE/DELETE */
if (tx - FONT_W >= 0) {
tx -= FONT_W ;
dputc(' ');
tx -= FONT_W;
}
} else if (c == 9) { /* TAB */
I ts = tx / FONT_W;
for (I i = ts; i < ((ts + 7) / 8) * 8; i++) { dputc(' '); }
} else if (c == 10 || c == 13) { /* CR or LF */
ty += FONT_H;
tx = 0;
}
}
V handle_regular_character(C *bitmap) {
I x, y, set;
for (x = 0; x < FONT_H; x++) {
for (y = 0; y < FONT_W; y++) {
set = reverse(bitmap[x]) & 1 << y;
pixel(tx + y, ty + x, set ? 1 : 0);
}
}
tx += FONT_W;
}
V advance_cursor_and_scroll() {
if (tx >= FW) { tx = 0; ty += FONT_H; }
if (ty > (FH - FONT_H)) { ty -= FONT_H; scroll(); }
}
V dputc(I c) {
C *bitmap = (C *)font + (c * ((FONT_H * FONT_W) / 8));
handle_special_character(c);
if (!(c == 8 || c == 127 || c == 9 || c == 10 || c == 13)) {
handle_regular_character(bitmap);
}
advance_cursor_and_scroll();
}
I acc_pop() {
I k = acc_i;
if (acc_i > 0) {
acc_i -= 1;
if (acc_i == -1) {
acc_i = 0;
}
}
return acc[k];
}
V acc_reset() {
for (I x = 0; x < 32; x++) { acc[x] = 0; }
ground = on = 0;
}
V gt_up(I n) { ty -= FONT_H; if (ty < 0) ty = 0; }
V gt_down(I n) { ty += FONT_H; if (ty > 24 * FONT_H) ty = 24 * FONT_H; }
V gt_left(I n) { tx -= FONT_W; if (tx < 0) tx = 0; }
V gt_right(I n) { tx += FONT_W; if (tx > 79 * FONT_W) ty = 79 * FONT_W; }
V gt_move_cursor() {
if (acc_i >= 1) {
I a = (acc_pop() - 1) * FONT_W;
I b = (acc_pop() - 1) * FONT_H;
ty = b; if (ty < 0) ty = 0; if (ty > FONT_H * 24) ty = FONT_H * 24;
tx = a; if (tx < 0) tx = 0; if (tx > FONT_W * 79) tx = FONT_W * 79;
acc_reset();
return;
}
/* home */
tx = 0; ty = 0;
acc_reset();
}
V gt_reset() { acc_reset(); }
V gt_next() { acc_i += 1; acc[acc_i] = 0; }
V gt_clear() {
acc_reset();
for (I x = 0; x < (FW * FH) / 32; x++) frame[x] = 0;
tx = ty = 0;
redraw();
}
V gt_set_attr() { acc_reset(); }
V term_putc(I c) {
if (c == 27) {
on = -2;
return;
}
switch (on) {
case 0:
dputc(c);
return;
case -2:
if (c == '[') {
on = -1;
return;
}
on = 0;
dputc(c);
return;
}
if (c >= '0' && c <= '9') {
acc[acc_i] *= 10;
acc[acc_i] += c - 48;
if (c == '3') {
ground = 0;
}
if (c == '4') {
ground = 1;
}
} else {
switch (c) {
case 'A': gt_up(acc_pop()); break;
case 'B': gt_down(acc_pop()); break;
case 'C': gt_left(acc_pop()); break;
case 'D': gt_right(acc_pop()); break;
case 'm': gt_set_attr(); break;
case ';': gt_next(); break;
case 'H': gt_move_cursor(); break;
case 'J': gt_clear(); break;
default: on = 0;
dputc(c);
}
}
}
V load_image(ilo *v) {
F = open(v->rom, O_RDONLY, 0666);
if (!F) { return; };
read(F, &M, 65536 * 4);
close(F);
IP = SP = RP = 0;
}
V save_image(ilo *v) {
v->f = open(v->rom, O_WRONLY, 0666);
write(v->f, &M, 65536 * 4);
close(v->f);
}
V block_common(ilo *v) {
B = pop(v); /* block buffer */
A = pop(v); /* block number */
lseek(v->f, 4096 * A, SEEK_SET);
}
V read_block(ilo *v) {
v->f = open(v->blocks, O_RDONLY, 0666);
block_common(v);
read(v->f, M + B, 4096);
close(v->f);
}
V write_block(ilo *v) {
v->f = open(v->blocks, O_WRONLY, 0666);
block_common(v);
write(v->f, M + B, 4096);
close(v->f);
}
V save_ip(ilo *v) { RP += 1; R = IP; }
V symmetric(ilo *v) {
if (B >= 0 && N < 0) { T += 1; N -= B; } }
V li(ilo *v) { IP += 1; push(v, M[IP]); }
V du(ilo *v) { push(v, T); }
V dr(ilo *v) { DS[SP] = 0; SP -= 1; }
V sw(ilo *v) { A = T; T = N; N = A; }
V pu(ilo *v) { RP += 1; R = pop(v); }
V po(ilo *v) { push(v, R); RP -= 1; }
V ju(ilo *v) { IP = pop(v) - 1; }
V ca(ilo *v) { save_ip(v); IP = pop(v) - 1; }
V cc(ilo *v) { A = pop(v);
if (pop(v)) { save_ip(v);
IP = A - 1; } }
V cj(ilo *v) { A = pop(v);
if (pop(v)) { IP = A - 1; } }
V re(ilo *v) { IP = R; RP -= 1; }
V eq(ilo *v) { N = (N == T) ? -1 : 0; SP -= 1; }
V ne(ilo *v) { N = (N != T) ? -1 : 0; SP -= 1; }
V lt(ilo *v) { N = (N < T) ? -1 : 0; SP -= 1; }
V gt(ilo *v) { N = (N > T) ? -1 : 0; SP -= 1; }
V fe(ilo *v) { T = M[T]; }
V st(ilo *v) { M[T] = N; SP -= 2; }
V ad(ilo *v) { N += T; SP -= 1; }
V su(ilo *v) { N -= T; SP -= 1; }
V mu(ilo *v) { N *= T; SP -= 1; }
V di(ilo *v) { A = T; B = N;
if (A == 0) { v->ok = 1; iof(v); return; }
T = B / A; N = B % A;
symmetric(v); }
V an(ilo *v) { N = T & N; SP -= 1; }
V or(ilo *v) { N = T | N; SP -= 1; }
V xo(ilo *v) { N = T ^ N; SP -= 1; }
V sl(ilo *v) { N = N << T; SP -= 1; }
V sr(ilo *v) { N = N >> T; SP -= 1; }
V cp(ilo *v) { L = pop(v); D = pop(v); S = T; T = -1;
while (L) {
if (M[D] != M[S]) { T = 0; }
L -= 1; S += 1; D += 1; } }
V cy(ilo *v) { L = pop(v); D = pop(v); S = pop(v);
while (L) {
M[D] = M[S];
L -= 1; S += 1; D += 1; } }
V pre(V) { cur -= 1; if (cur < 0) cur = ILOS - 1; }
V nxt(V) { cur += 1; if (cur > (ILOS - 1)) cur = 0; }
V msgs(V) { printf("\n*** SWITCH TO %d ***\n", cur); }
V msgx(V) { printf("\n*** SHUTDOWN REQUESTED ***\n");
exit(1); }
V msgr(V) { printf("\n*** RESTART %d ***\n", cur); }
V term_putc(I c);
V ioa(ilo *v) { term_putc(pop(v)); }
V iob(ilo *v) { redraw(); cursor();
v->i[0] = wait_key(); push(v, v->i[0]); term_putc(T);
if (v->i[0] == 14) { T = 32; nxt(); msgs(); }
if (v->i[0] == 16) { T = 32; pre(); msgs(); }
if (v->i[0] == 3) { T = 32; msgr(); iof(v);
v->ok = 1; }
if (v->i[0] == 1) { T = 32; msgx(); } }
V ioc(ilo *v) { read_block(v); }
V iod(ilo *v) { write_block(v); }
V ioe(ilo *v) { save_image(v); }
V iof(ilo *v) { load_image(v); IP = -1; }
V iog(ilo *v) { iof(v); }
V ioh(ilo *v) { push(v, SP); push(v, RP); }
V iox(ilo *v) { I c = pop(v); I y = pop(v); I x = pop(v);
pixel(x, y, c); }
V ioy(ilo *v) { I y = pop(v); I x = pop(v); push(v, get_pixel(x, y)); }
V ioz(ilo *v) { I x, y, wx, wy, mask; Window w;
XQueryPointer(disp, win, &w, &w, &x, &y, &wx, &wy, &mask);
push(v, x); push(v, y); if (mask & Button1Mask) push(v, -1); else push(v, 0); }
V io(ilo *v) {
switch (pop(v)) {
case 0: ioa(v); break; case 1: iob(v); break;
case 2: ioc(v); break; case 3: iod(v); break;
case 4: ioe(v); break; case 5: iof(v); break;
case 6: iog(v); break; case 7: ioh(v); break;
case 33: iox(v); break; case 34: ioy(v); break;
case 35: ioz(v); break;
default: break;
}
}
V process(ilo *v, I o) {
switch (o) {
case 0: break; case 1: li(v); break;
case 2: du(v); break; case 3: dr(v); break;
case 4: sw(v); break; case 5: pu(v); break;
case 6: po(v); break; case 7: ju(v); break;
case 8: ca(v); break; case 9: cc(v); break;
case 10: cj(v); break; case 11: re(v); break;
case 12: eq(v); break; case 13: ne(v); break;
case 14: lt(v); break; case 15: gt(v); break;
case 16: fe(v); break; case 17: st(v); break;
case 18: ad(v); break; case 19: su(v); break;
case 20: mu(v); break; case 21: di(v); break;
case 22: an(v); break; case 23: or(v); break;
case 24: xo(v); break; case 25: sl(v); break;
case 26: sr(v); break; case 27: cp(v); break;
case 28: cy(v); break; case 29: io(v); break;
default: break;
}
}
#define VALID(n) (v->ok == 0) && ((opcode >> n) & 0xFF)
V execute() {
I opcode;
ilo *v = &systems[cur];
while (IP < 65536) {
opcode = M[IP];
v->ok = 0;
if (VALID(0)) process(v, opcode & 0xFF);
if (VALID(8)) process(v, (opcode >> 8) & 0xFF);
if (VALID(16)) process(v, (opcode >> 16) & 0xFF);
if (VALID(24)) process(v, (opcode >> 24) & 0xFF);
IP += 1;
v = &systems[cur];
}
}
V load_font(ilo *);
I main(I argc, C **argv) {
ilo *v;
I i;
cur = 0;
for (i = 0; i < ILOS; i++) {
v = &systems[i];
v->blocks = (argc > 1) ? argv[1] : BLOCKS;
v->rom = (argc > 2) ? argv[2] : ROM;
load_image(v);
}
load_font(&systems[0]);
disp = XOpenDisplay(NULL);
win = XCreateSimpleWindow(disp, RootWindow(disp, 0),
0, 0, FW, FH, 0, 0x0, 0x0);
XSelectInput(disp, win, ExposureMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask);
XStoreName(disp, win, "x/ilo");
gc = DefaultGC(disp, DefaultScreen(disp));
XMapWindow(disp, win);
XFlush(disp);
XMoveWindow(disp, win, (DisplayWidth(disp, DefaultScreen(disp)) - FW) / 2,
(DisplayHeight(disp, DefaultScreen(disp)) - FH) / 2);
execute();
XUnmapWindow(disp, win);
XDestroyWindow(disp, win);
XCloseDisplay(disp);
return 0;
}