2022-03-26 21:32:46 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2022-03-27 12:11:14 -04:00
|
|
|
#include <X11/Xlib.h>
|
2022-03-27 17:29:46 -04:00
|
|
|
#include <X11/Xutil.h>
|
2022-03-28 14:03:02 -04:00
|
|
|
#include <X11/keysymdef.h>
|
2022-03-27 22:10:32 -04:00
|
|
|
#include <sys/timerfd.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <poll.h>
|
2022-03-26 21:32:46 -04:00
|
|
|
|
|
|
|
#include "uxn.h"
|
|
|
|
#include "devices/system.h"
|
|
|
|
#include "devices/screen.h"
|
2022-03-26 22:58:48 -04:00
|
|
|
#include "devices/controller.h"
|
2022-03-27 00:03:03 -04:00
|
|
|
#include "devices/mouse.h"
|
2022-03-27 13:01:06 -04:00
|
|
|
#include "devices/file.h"
|
2022-03-27 00:03:03 -04:00
|
|
|
#include "devices/datetime.h"
|
2022-03-26 22:58:48 -04:00
|
|
|
|
2022-03-27 01:38:03 -04:00
|
|
|
static XImage *ximage;
|
2022-03-27 12:11:14 -04:00
|
|
|
static Display *display;
|
|
|
|
static Visual *visual;
|
|
|
|
static Window window;
|
|
|
|
|
2022-03-28 13:22:23 -04:00
|
|
|
static Device *devscreen, *devctrl, *devmouse;
|
2022-03-27 12:11:14 -04:00
|
|
|
|
2022-03-28 14:51:15 -04:00
|
|
|
#define WIDTH (64 * 8)
|
|
|
|
#define HEIGHT (40 * 8)
|
2022-03-26 21:32:46 -04:00
|
|
|
|
|
|
|
static int
|
|
|
|
error(char *msg, const char *err)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error %s: %s\n", msg, err);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-04-05 12:35:49 -04:00
|
|
|
system_deo_special(Uint8 *d, Uint8 port)
|
2022-03-26 21:32:46 -04:00
|
|
|
{
|
|
|
|
if(port > 0x7 && port < 0xe)
|
2022-04-05 12:35:49 -04:00
|
|
|
screen_palette(&uxn_screen, &d[0x8]);
|
2022-03-26 21:32:46 -04:00
|
|
|
}
|
|
|
|
|
2022-03-27 13:01:06 -04:00
|
|
|
static int
|
|
|
|
console_input(Uxn *u, char c)
|
|
|
|
{
|
|
|
|
Device *d = &u->dev[1];
|
|
|
|
d->dat[0x2] = c;
|
2022-04-05 12:29:06 -04:00
|
|
|
return uxn_eval(u, GETVEC(d->dat));
|
2022-03-27 13:01:06 -04:00
|
|
|
}
|
|
|
|
|
2022-03-26 21:32:46 -04:00
|
|
|
static void
|
2022-04-05 12:29:06 -04:00
|
|
|
console_deo(Uint8 *d, Uint8 port)
|
2022-03-26 21:32:46 -04:00
|
|
|
{
|
|
|
|
FILE *fd = port == 0x8 ? stdout : port == 0x9 ? stderr
|
|
|
|
: 0;
|
|
|
|
if(fd) {
|
2022-04-05 12:29:06 -04:00
|
|
|
fputc(d[port], fd);
|
2022-03-26 21:32:46 -04:00
|
|
|
fflush(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Uint8
|
2022-04-04 22:57:44 -04:00
|
|
|
uxn11_dei(struct Uxn *u, Uint8 addr)
|
2022-03-26 21:32:46 -04:00
|
|
|
{
|
2022-04-04 22:57:44 -04:00
|
|
|
Uint8 p = addr & 0x0f;
|
|
|
|
Device *d = &u->dev[addr >> 4];
|
|
|
|
switch(addr & 0xf0) {
|
2022-04-05 12:22:33 -04:00
|
|
|
case 0x20: return screen_dei(d->dat, p); break;
|
2022-04-05 12:01:05 -04:00
|
|
|
case 0xa0: return file_dei(d, p); break;
|
|
|
|
case 0xb0: return file_dei(d, p); break;
|
|
|
|
case 0xc0: return datetime_dei(d->dat, p); break;
|
2022-04-04 22:57:44 -04:00
|
|
|
}
|
|
|
|
return d->dat[p];
|
2022-03-26 21:32:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-04-04 22:57:44 -04:00
|
|
|
uxn11_deo(Uxn *u, Uint8 addr, Uint8 v)
|
2022-03-26 21:32:46 -04:00
|
|
|
{
|
2022-04-04 22:57:44 -04:00
|
|
|
Uint8 p = addr & 0x0f;
|
|
|
|
Device *d = &u->dev[addr >> 4];
|
|
|
|
d->dat[p] = v;
|
|
|
|
switch(addr & 0xf0) {
|
2022-04-05 12:35:49 -04:00
|
|
|
case 0x00: system_deo(u, d->dat, p); break;
|
2022-04-05 12:29:06 -04:00
|
|
|
case 0x10: console_deo(d->dat, p); break;
|
2022-04-05 12:22:33 -04:00
|
|
|
case 0x20: screen_deo(u->ram, d->dat, p); break;
|
2022-04-04 22:57:44 -04:00
|
|
|
case 0xa0: file_deo(d, p); break;
|
|
|
|
case 0xb0: file_deo(d, p); break;
|
|
|
|
}
|
2022-03-26 21:32:46 -04:00
|
|
|
}
|
|
|
|
|
2022-03-27 12:11:14 -04:00
|
|
|
static void
|
|
|
|
redraw(void)
|
2022-03-26 23:20:29 -04:00
|
|
|
{
|
|
|
|
screen_redraw(&uxn_screen, uxn_screen.pixels);
|
|
|
|
XPutImage(display, window, DefaultGC(display, 0), ximage, 0, 0, 0, 0, uxn_screen.width, uxn_screen.height);
|
|
|
|
}
|
|
|
|
|
2022-03-27 22:47:05 -04:00
|
|
|
static void
|
|
|
|
hide_cursor(void)
|
|
|
|
{
|
|
|
|
Cursor blank;
|
|
|
|
Pixmap bitmap;
|
|
|
|
XColor black;
|
|
|
|
static char empty[] = {0, 0, 0, 0, 0, 0, 0, 0};
|
|
|
|
black.red = black.green = black.blue = 0;
|
|
|
|
bitmap = XCreateBitmapFromData(display, window, empty, 8, 8);
|
|
|
|
blank = XCreatePixmapCursor(display, bitmap, bitmap, &black, &black, 0, 0);
|
|
|
|
XDefineCursor(display, window, blank);
|
|
|
|
XFreeCursor(display, blank);
|
|
|
|
XFreePixmap(display, bitmap);
|
|
|
|
}
|
|
|
|
|
2022-03-28 12:39:05 -04:00
|
|
|
static Uint8
|
|
|
|
get_button(KeySym sym)
|
|
|
|
{
|
|
|
|
switch(sym) {
|
2022-03-28 14:03:02 -04:00
|
|
|
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;
|
2022-03-28 12:39:05 -04:00
|
|
|
}
|
|
|
|
return 0x00;
|
|
|
|
}
|
|
|
|
|
2022-03-27 12:11:14 -04:00
|
|
|
static void
|
|
|
|
processEvent(void)
|
2022-03-26 21:32:46 -04:00
|
|
|
{
|
|
|
|
XEvent ev;
|
|
|
|
XNextEvent(display, &ev);
|
|
|
|
switch(ev.type) {
|
|
|
|
case Expose:
|
2022-03-27 12:11:14 -04:00
|
|
|
redraw();
|
2022-03-26 21:32:46 -04:00
|
|
|
break;
|
2022-03-27 12:11:14 -04:00
|
|
|
case ClientMessage: {
|
|
|
|
XDestroyImage(ximage);
|
|
|
|
XDestroyWindow(display, window);
|
|
|
|
XCloseDisplay(display);
|
|
|
|
exit(0);
|
|
|
|
} break;
|
2022-03-26 22:58:48 -04:00
|
|
|
case KeyPress: {
|
2022-03-28 12:39:05 -04:00
|
|
|
KeySym sym;
|
2022-03-27 17:29:46 -04:00
|
|
|
char buf[7];
|
2022-03-28 13:31:31 -04:00
|
|
|
XLookupString((XKeyPressedEvent *)&ev, buf, 7, &sym, 0);
|
2022-04-05 12:10:29 -04:00
|
|
|
controller_down(devctrl->u, devctrl->dat, get_button(sym));
|
|
|
|
controller_key(devctrl->u, devctrl->dat, sym < 0x80 ? sym : buf[0]);
|
2022-03-26 22:58:48 -04:00
|
|
|
} break;
|
|
|
|
case KeyRelease: {
|
2022-03-28 12:39:05 -04:00
|
|
|
KeySym sym;
|
|
|
|
char buf[7];
|
2022-03-28 13:31:31 -04:00
|
|
|
XLookupString((XKeyPressedEvent *)&ev, buf, 7, &sym, 0);
|
2022-04-05 12:10:29 -04:00
|
|
|
controller_up(devctrl->u, devctrl->dat, get_button(sym));
|
2022-03-26 22:58:48 -04:00
|
|
|
} break;
|
2022-03-27 00:03:03 -04:00
|
|
|
case ButtonPress: {
|
|
|
|
XButtonPressedEvent *e = (XButtonPressedEvent *)&ev;
|
2022-04-05 12:17:08 -04:00
|
|
|
mouse_down(devmouse->u, devmouse->dat, 0x1 << (e->button - 1));
|
2022-03-27 00:03:03 -04:00
|
|
|
} break;
|
|
|
|
case ButtonRelease: {
|
|
|
|
XButtonPressedEvent *e = (XButtonPressedEvent *)&ev;
|
2022-04-05 12:17:08 -04:00
|
|
|
mouse_up(devmouse->u, devmouse->dat, 0x1 << (e->button - 1));
|
2022-03-27 00:03:03 -04:00
|
|
|
} break;
|
|
|
|
case MotionNotify: {
|
|
|
|
XMotionEvent *e = (XMotionEvent *)&ev;
|
2022-04-05 12:17:08 -04:00
|
|
|
mouse_pos(devmouse->u, devmouse->dat, e->x, e->y);
|
2022-03-27 00:03:03 -04:00
|
|
|
} break;
|
2022-03-26 23:20:29 -04:00
|
|
|
}
|
2022-03-26 21:32:46 -04:00
|
|
|
}
|
|
|
|
|
2022-04-04 23:04:32 -04:00
|
|
|
static Uint8
|
|
|
|
nil_dei(Device *d, Uint8 port)
|
|
|
|
{
|
|
|
|
return d->dat[port];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
nil_deo(Device *d, Uint8 port)
|
|
|
|
{
|
|
|
|
(void)d;
|
|
|
|
(void)port;
|
|
|
|
}
|
|
|
|
|
2022-03-26 21:32:46 -04:00
|
|
|
static int
|
2022-03-27 12:11:14 -04:00
|
|
|
start(Uxn *u, char *rom)
|
2022-03-26 21:32:46 -04:00
|
|
|
{
|
2022-03-30 13:37:47 -04:00
|
|
|
if(!uxn_boot(u, (Uint8 *)calloc(0x10200, sizeof(Uint8))))
|
2022-03-26 21:32:46 -04:00
|
|
|
return error("Boot", "Failed");
|
2022-03-28 14:03:02 -04:00
|
|
|
if(!load_rom(u, rom))
|
2022-03-27 12:11:14 -04:00
|
|
|
return error("Load", "Failed");
|
2022-03-28 14:03:02 -04:00
|
|
|
fprintf(stderr, "Loaded %s\n", rom);
|
2022-04-04 22:57:44 -04:00
|
|
|
u->dei = uxn11_dei;
|
|
|
|
u->deo = uxn11_deo;
|
2022-04-04 23:04:32 -04:00
|
|
|
|
2022-04-05 12:35:49 -04:00
|
|
|
/* system */ uxn_port(u, 0x0, nil_dei, nil_deo);
|
2022-04-05 12:29:06 -04:00
|
|
|
/* console */ uxn_port(u, 0x1, nil_dei, nil_deo);
|
2022-04-05 12:22:33 -04:00
|
|
|
/* screen */ devscreen = uxn_port(u, 0x2, nil_dei, nil_deo);
|
2022-04-04 23:04:32 -04:00
|
|
|
/* empty */ uxn_port(u, 0x3, nil_dei, nil_deo);
|
|
|
|
/* empty */ uxn_port(u, 0x4, nil_dei, nil_deo);
|
|
|
|
/* empty */ uxn_port(u, 0x5, nil_dei, nil_deo);
|
|
|
|
/* empty */ uxn_port(u, 0x6, nil_dei, nil_deo);
|
|
|
|
/* empty */ uxn_port(u, 0x7, nil_dei, nil_deo);
|
|
|
|
/* control */ devctrl = uxn_port(u, 0x8, nil_dei, nil_deo);
|
|
|
|
/* mouse */ devmouse = uxn_port(u, 0x9, nil_dei, nil_deo);
|
|
|
|
/* file0 */ uxn_port(u, 0xa, file_dei, file_deo);
|
|
|
|
/* file1 */ uxn_port(u, 0xb, file_dei, file_deo);
|
2022-04-05 12:01:05 -04:00
|
|
|
/* datetime */ uxn_port(u, 0xc, nil_dei, nil_deo);
|
2022-04-04 23:04:32 -04:00
|
|
|
/* empty */ uxn_port(u, 0xd, nil_dei, nil_deo);
|
|
|
|
/* reserved */ uxn_port(u, 0xe, nil_dei, nil_deo);
|
|
|
|
/* reserved */ uxn_port(u, 0xf, nil_dei, nil_deo);
|
|
|
|
|
2022-03-27 12:11:14 -04:00
|
|
|
screen_resize(&uxn_screen, WIDTH, HEIGHT);
|
|
|
|
if(!uxn_eval(u, PAGE_PROGRAM))
|
|
|
|
return error("Boot", "Failed to start rom.");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
init(void)
|
|
|
|
{
|
|
|
|
Atom wmDelete;
|
|
|
|
display = XOpenDisplay(NULL);
|
|
|
|
visual = DefaultVisual(display, 0);
|
|
|
|
window = XCreateSimpleWindow(display, RootWindow(display, 0), 0, 0, uxn_screen.width, uxn_screen.height, 1, 0, 0);
|
|
|
|
if(visual->class != TrueColor)
|
|
|
|
return error("Init", "True-color visual failed");
|
|
|
|
XSelectInput(display, window, ButtonPressMask | ButtonReleaseMask | PointerMotionMask | ExposureMask | KeyPressMask | KeyReleaseMask);
|
|
|
|
wmDelete = XInternAtom(display, "WM_DELETE_WINDOW", True);
|
|
|
|
XSetWMProtocols(display, window, &wmDelete, 1);
|
|
|
|
XMapWindow(display, window);
|
|
|
|
ximage = XCreateImage(display, visual, DefaultDepth(display, DefaultScreen(display)), ZPixmap, 0, (char *)uxn_screen.pixels, uxn_screen.width, uxn_screen.height, 32, 0);
|
2022-03-27 22:47:05 -04:00
|
|
|
hide_cursor();
|
2022-03-26 21:32:46 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
Uxn u;
|
2022-03-27 13:01:06 -04:00
|
|
|
int i;
|
2022-03-27 22:10:32 -04:00
|
|
|
char expirations[8];
|
|
|
|
struct pollfd fds[2];
|
|
|
|
static const struct itimerspec screen_tspec = {{0, 16666666}, {0, 16666666}};
|
2022-03-26 21:32:46 -04:00
|
|
|
if(argc < 2)
|
|
|
|
return error("Usage", "uxncli game.rom args");
|
2022-03-27 12:11:14 -04:00
|
|
|
if(!start(&u, argv[1]))
|
2022-03-26 21:32:46 -04:00
|
|
|
return error("Start", "Failed");
|
2022-03-27 12:11:14 -04:00
|
|
|
if(!init())
|
|
|
|
return error("Init", "Failed");
|
2022-03-27 13:01:06 -04:00
|
|
|
/* console vector */
|
|
|
|
for(i = 2; i < argc; i++) {
|
|
|
|
char *p = argv[i];
|
|
|
|
while(*p) console_input(&u, *p++);
|
|
|
|
console_input(&u, '\n');
|
|
|
|
}
|
2022-03-27 22:10:32 -04:00
|
|
|
fds[0].fd = XConnectionNumber(display);
|
|
|
|
fds[1].fd = timerfd_create(CLOCK_MONOTONIC, 0);
|
|
|
|
timerfd_settime(fds[1].fd, 0, &screen_tspec, NULL);
|
|
|
|
fds[0].events = fds[1].events = POLLIN;
|
2022-03-27 13:01:06 -04:00
|
|
|
/* main loop */
|
2022-03-26 21:32:46 -04:00
|
|
|
while(1) {
|
2022-03-27 22:10:32 -04:00
|
|
|
if(poll(fds, 2, 1000) <= 0)
|
|
|
|
continue;
|
|
|
|
while(XPending(display))
|
|
|
|
processEvent();
|
|
|
|
if(poll(&fds[1], 1, 0)) {
|
|
|
|
read(fds[1].fd, expirations, 8); /* Indicate we handled the timer */
|
|
|
|
uxn_eval(&u, GETVECTOR(devscreen)); /* Call the vector once, even if the timer fired multiple times */
|
|
|
|
}
|
2022-03-27 12:11:14 -04:00
|
|
|
if(uxn_screen.fg.changed || uxn_screen.bg.changed)
|
|
|
|
redraw();
|
2022-03-26 21:32:46 -04:00
|
|
|
}
|
2022-03-27 01:38:03 -04:00
|
|
|
XDestroyImage(ximage);
|
2022-03-26 21:32:46 -04:00
|
|
|
return 0;
|
2022-03-27 01:38:03 -04:00
|
|
|
}
|