uxn11/src/uxn11.c

246 lines
6.7 KiB
C
Raw Normal View History

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-04-07 12:33:52 -04:00
/*
Copyright (c) 2022 Devine Lu Linvega
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.
*/
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-06-13 13:37:18 -04:00
char *rom_path;
2022-03-28 14:51:15 -04:00
#define WIDTH (64 * 8)
#define HEIGHT (40 * 8)
2022-06-15 12:10:13 -04:00
#define PAD 4
#define CONINBUFSIZE 256
2022-03-26 21:32:46 -04:00
2023-04-10 14:32:16 -04:00
Uint16 deo_mask[] = {0xff08, 0x0300, 0xc028, 0x8000, 0x8000, 0x8000, 0x8000, 0x0000, 0x0000, 0x0000, 0xa260, 0xa260, 0x0000, 0x0000, 0x0000, 0x0000};
Uint16 dei_mask[] = {0x0000, 0x0000, 0x003c, 0x0014, 0x0014, 0x0014, 0x0014, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x07ff, 0x0000, 0x0000, 0x0000};
Uint8
uxn_dei(Uxn *u, Uint8 addr)
2022-03-26 21:32:46 -04:00
{
2023-04-10 14:32:16 -04:00
switch(addr & 0xf0) {
2023-04-10 13:47:05 -04:00
case 0x20: return screen_dei(u, addr);
2023-04-10 14:32:16 -04:00
case 0xc0: return datetime_dei(u, addr);
2022-04-04 22:57:44 -04:00
}
2022-04-05 14:42:50 -04:00
return u->dev[addr];
2022-03-26 21:32:46 -04:00
}
2023-04-10 14:32:16 -04:00
void
uxn_deo(Uxn *u, Uint8 addr)
2022-03-26 21:32:46 -04:00
{
2022-04-05 14:42:50 -04:00
Uint8 p = addr & 0x0f, d = addr & 0xf0;
switch(d) {
2022-04-05 15:08:49 -04:00
case 0x00:
system_deo(u, &u->dev[d], p);
if(p > 0x7 && p < 0xe)
screen_palette(&uxn_screen, &u->dev[0x8]);
break;
2022-04-05 14:42:50 -04:00
case 0x10: console_deo(&u->dev[d], p); break;
case 0x20: screen_deo(u->ram, &u->dev[d], p); break;
case 0xa0: file_deo(0, u->ram, &u->dev[d], p); break;
case 0xb0: file_deo(1, u->ram, &u->dev[d], p); break;
2022-04-04 22:57:44 -04:00
}
2022-03-26 21:32:46 -04:00
}
2022-03-27 12:11:14 -04:00
static void
2022-04-07 12:33:52 -04:00
emu_draw(void)
2022-03-26 23:20:29 -04:00
{
2023-04-11 18:14:32 -04:00
screen_redraw(&uxn_screen);
2023-04-30 14:32:11 -04:00
XPutImage(display, window, DefaultGC(display, 0), ximage, 0, 0, PAD, PAD, uxn_screen.width * ZOOM, uxn_screen.height * ZOOM);
2022-03-26 23:20:29 -04:00
}
2022-04-05 23:06:42 -04:00
static int
emu_start(Uxn *u, char *rom)
{
2023-02-02 11:45:03 -05:00
if(!system_load(u, rom))
return 0;
2022-06-13 14:08:33 -04:00
if(!uxn_screen.width || !uxn_screen.height)
screen_resize(&uxn_screen, WIDTH, HEIGHT);
2022-04-05 23:06:42 -04:00
if(!uxn_eval(u, PAGE_PROGRAM))
2023-04-10 17:32:54 -04:00
return system_error("boot", "Failed to start rom.");
2022-04-05 23:06:42 -04:00
return 1;
}
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
2022-04-05 22:40:49 -04:00
emu_event(Uxn *u)
2022-03-26 21:32:46 -04:00
{
XEvent ev;
XNextEvent(display, &ev);
switch(ev.type) {
case Expose:
2022-04-07 12:33:52 -04:00
emu_draw();
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 23:06:42 -04:00
if(sym == XK_F2)
system_inspect(u);
if(sym == XK_F4)
2022-06-13 13:37:18 -04:00
if(!emu_start(u, "boot.rom"))
emu_start(u, rom_path);
2022-04-05 14:42:50 -04:00
controller_down(u, &u->dev[0x80], get_button(sym));
2022-04-05 22:13:14 -04:00
controller_key(u, &u->dev[0x80], sym < 0x80 ? sym : (Uint8)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 14:42:50 -04:00
controller_up(u, &u->dev[0x80], 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 22:31:53 -04:00
if(e->button == 4)
mouse_scroll(u, &u->dev[0x90], 0, 1);
else if(e->button == 5)
mouse_scroll(u, &u->dev[0x90], 0, -1);
else
mouse_down(u, &u->dev[0x90], 0x1 << (e->button - 1));
2022-03-27 00:03:03 -04:00
} break;
case ButtonRelease: {
XButtonPressedEvent *e = (XButtonPressedEvent *)&ev;
2022-04-05 14:42:50 -04:00
mouse_up(u, &u->dev[0x90], 0x1 << (e->button - 1));
2022-03-27 00:03:03 -04:00
} break;
case MotionNotify: {
XMotionEvent *e = (XMotionEvent *)&ev;
2023-04-30 14:32:11 -04:00
mouse_pos(u, &u->dev[0x90], (e->x - PAD) / ZOOM, (e->y - PAD) / ZOOM);
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-03-27 12:11:14 -04:00
static int
2022-06-13 14:29:16 -04:00
display_start(char *title)
2022-03-27 12:11:14 -04:00
{
Atom wmDelete;
display = XOpenDisplay(NULL);
visual = DefaultVisual(display, 0);
2023-04-30 14:32:11 -04:00
window = XCreateSimpleWindow(display, RootWindow(display, 0), 0, 0, uxn_screen.width * ZOOM + PAD * 2, uxn_screen.height * ZOOM + PAD * 2, 1, 0, 0);
2022-03-27 12:11:14 -04:00
if(visual->class != TrueColor)
2023-04-10 17:32:54 -04:00
return system_error("init", "True-color visual failed");
2022-03-27 12:11:14 -04:00
XSelectInput(display, window, ButtonPressMask | ButtonReleaseMask | PointerMotionMask | ExposureMask | KeyPressMask | KeyReleaseMask);
wmDelete = XInternAtom(display, "WM_DELETE_WINDOW", True);
XSetWMProtocols(display, window, &wmDelete, 1);
2022-04-12 11:04:41 -04:00
XStoreName(display, window, title);
2022-03-27 12:11:14 -04:00
XMapWindow(display, window);
2023-04-30 14:32:11 -04:00
ximage = XCreateImage(display, visual, DefaultDepth(display, DefaultScreen(display)), ZPixmap, 0, (char *)uxn_screen.pixels, uxn_screen.width * ZOOM, uxn_screen.height * ZOOM, 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;
2023-03-18 14:12:48 -04:00
int i, n;
2022-03-27 22:10:32 -04:00
char expirations[8];
char coninp[CONINBUFSIZE];
struct pollfd fds[3];
2022-03-27 22:10:32 -04:00
static const struct itimerspec screen_tspec = {{0, 16666666}, {0, 16666666}};
2022-03-26 21:32:46 -04:00
if(argc < 2)
2023-04-10 17:32:54 -04:00
return system_error("usage", "uxn11 game.rom args");
2022-06-13 13:37:18 -04:00
rom_path = argv[1];
2023-04-10 14:32:16 -04:00
if(!uxn_boot(&u, (Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8))))
2023-04-10 17:32:54 -04:00
return system_error("boot", "Failed");
2022-06-13 14:21:15 -04:00
/* start sequence */
2022-06-13 13:37:18 -04:00
if(!emu_start(&u, rom_path))
2023-04-10 17:32:54 -04:00
return system_error("start", rom_path);
2022-06-13 14:29:16 -04:00
if(!display_start(rom_path))
2023-04-10 17:32:54 -04:00
return system_error("display", "Failed");
2022-03-27 13:01:06 -04:00
/* console vector */
for(i = 2; i < argc; i++) {
char *p = argv[i];
2023-04-17 14:36:17 -04:00
while(*p) console_input(&u, *p++, CONSOLE_ARG);
console_input(&u, '\n', i == argc ? CONSOLE_END : CONSOLE_EOA);
2022-03-27 13:01:06 -04:00
}
2022-04-07 12:33:52 -04:00
/* timer */
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[2].fd = STDIN_FILENO;
fds[0].events = fds[1].events = fds[2].events = POLLIN;
2022-03-27 13:01:06 -04:00
/* main loop */
2022-06-03 17:55:46 -04:00
while(!u.dev[0x0f]) {
if(poll(fds, 3, 1000) <= 0)
2022-03-27 22:10:32 -04:00
continue;
while(XPending(display))
2022-04-05 22:40:49 -04:00
emu_event(&u);
2022-03-27 22:10:32 -04:00
if(poll(&fds[1], 1, 0)) {
2023-04-10 13:08:40 -04:00
read(fds[1].fd, expirations, 8); /* Indicate we handled the timer */
uxn_eval(&u, PEEK2(&u.dev[0x20])); /* Call the vector once, even if the timer fired multiple times */
2022-03-27 22:10:32 -04:00
}
2023-03-18 14:12:48 -04:00
if((fds[2].revents & POLLIN) != 0) {
n = read(fds[2].fd, coninp, CONINBUFSIZE - 1);
coninp[n] = 0;
for(i = 0; i < n; i++)
2023-04-17 14:36:17 -04:00
console_input(&u, coninp[i], CONSOLE_STD);
}
2022-03-27 12:11:14 -04:00
if(uxn_screen.fg.changed || uxn_screen.bg.changed)
2022-04-07 12:33:52 -04:00
emu_draw();
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
}