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"
|
2023-08-08 19:26:13 -04:00
|
|
|
#include "devices/console.h"
|
2022-03-26 21:32:46 -04:00
|
|
|
#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 Window window;
|
|
|
|
|
2022-03-28 14:51:15 -04:00
|
|
|
#define WIDTH (64 * 8)
|
|
|
|
#define HEIGHT (40 * 8)
|
2023-06-07 11:42:22 -04:00
|
|
|
#define PAD 2
|
2023-03-18 14:04:50 -04:00
|
|
|
#define CONINBUFSIZE 256
|
2022-03-26 21:32:46 -04:00
|
|
|
|
2023-06-07 11:42:22 -04:00
|
|
|
static int
|
|
|
|
clamp(int val, int min, int max)
|
|
|
|
{
|
|
|
|
return (val >= min) ? (val <= max) ? val : max : min;
|
|
|
|
}
|
|
|
|
|
2023-04-10 14:32:16 -04:00
|
|
|
Uint8
|
2023-08-01 23:32:42 -04:00
|
|
|
emu_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-08-19 10:36:26 -04:00
|
|
|
case 0x00: return system_dei(u, addr);
|
2023-11-10 12:32:34 -05:00
|
|
|
case 0x10: return console_dei(u, addr);
|
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
|
2023-10-31 12:00:02 -04:00
|
|
|
emu_deo(Uxn *u, Uint8 addr, Uint8 value)
|
2022-03-26 21:32:46 -04:00
|
|
|
{
|
2022-04-05 14:42:50 -04:00
|
|
|
Uint8 p = addr & 0x0f, d = addr & 0xf0;
|
2023-10-31 12:00:02 -04:00
|
|
|
u->dev[addr] = value;
|
2022-04-05 14:42:50 -04:00
|
|
|
switch(d) {
|
2022-04-05 15:08:49 -04:00
|
|
|
case 0x00:
|
|
|
|
system_deo(u, &u->dev[d], p);
|
|
|
|
if(p > 0x7 && p < 0xe)
|
2023-05-04 23:43:06 -04:00
|
|
|
screen_palette(&u->dev[0x8]);
|
2022-04-05 15:08:49 -04:00
|
|
|
break;
|
2023-11-10 12:32:34 -05:00
|
|
|
case 0x10: console_deo(u, &u->dev[d], p); break;
|
2022-04-05 14:42:50 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-08-01 23:39:16 -04:00
|
|
|
int
|
2024-01-15 13:17:37 -05:00
|
|
|
emu_resize(int w, int h)
|
2023-08-04 14:40:24 -04:00
|
|
|
{
|
2023-11-13 23:18:58 -05:00
|
|
|
if(window) {
|
2024-01-15 14:18:36 -05:00
|
|
|
static Visual *visual;
|
|
|
|
w *= uxn_screen.scale, h *= uxn_screen.scale;
|
2023-11-13 23:18:58 -05:00
|
|
|
visual = DefaultVisual(display, 0);
|
2024-01-15 14:18:36 -05:00
|
|
|
ximage = XCreateImage(display, visual, DefaultDepth(display, DefaultScreen(display)), ZPixmap, 0, (char *)uxn_screen.pixels, w, h, 32, 0);
|
2024-01-15 23:15:48 -05:00
|
|
|
XResizeWindow(display, window, w + PAD * 2, h + PAD * 2);
|
2023-11-13 23:18:58 -05:00
|
|
|
XMapWindow(display, window);
|
|
|
|
}
|
2023-08-16 15:50:46 -04:00
|
|
|
return 1;
|
2023-08-01 23:39:16 -04:00
|
|
|
}
|
|
|
|
|
2023-08-16 16:22:41 -04:00
|
|
|
static void
|
|
|
|
emu_restart(Uxn *u, char *rom, int soft)
|
2022-04-05 23:06:42 -04:00
|
|
|
{
|
2023-11-13 23:18:58 -05:00
|
|
|
screen_resize(WIDTH, HEIGHT, uxn_screen.scale);
|
2023-11-11 23:59:08 -05:00
|
|
|
screen_rect(uxn_screen.bg, 0, 0, uxn_screen.width, uxn_screen.height, 0);
|
|
|
|
screen_rect(uxn_screen.fg, 0, 0, uxn_screen.width, uxn_screen.height, 0);
|
2023-08-16 16:22:41 -04:00
|
|
|
system_reboot(u, rom, soft);
|
2022-04-05 23:06:42 -04:00
|
|
|
}
|
|
|
|
|
2023-08-16 16:37:17 -04:00
|
|
|
static int
|
|
|
|
emu_end(Uxn *u)
|
|
|
|
{
|
|
|
|
free(u->ram);
|
|
|
|
XDestroyImage(ximage);
|
|
|
|
XDestroyWindow(display, window);
|
|
|
|
XCloseDisplay(display);
|
|
|
|
exit(0);
|
|
|
|
return u->dev[0x0f] & 0x7f;
|
|
|
|
}
|
|
|
|
|
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;
|
2024-01-16 20:22:07 -05:00
|
|
|
case XK_Meta_L: return 0x02;
|
2022-03-28 12:39:05 -04:00
|
|
|
}
|
|
|
|
return 0x00;
|
|
|
|
}
|
|
|
|
|
2023-11-13 23:18:58 -05:00
|
|
|
static void
|
2024-01-15 13:17:37 -05:00
|
|
|
toggle_scale(void)
|
2023-11-13 23:18:58 -05:00
|
|
|
{
|
|
|
|
int s = uxn_screen.scale + 1;
|
2023-11-18 13:59:40 -05:00
|
|
|
if(s > 3) s = 1;
|
2023-11-13 23:18:58 -05:00
|
|
|
screen_resize(uxn_screen.width, uxn_screen.height, s);
|
|
|
|
}
|
|
|
|
|
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) {
|
2024-01-16 19:55:03 -05:00
|
|
|
case Expose: {
|
|
|
|
int w = uxn_screen.width * uxn_screen.scale;
|
|
|
|
int h = uxn_screen.height * uxn_screen.scale;
|
|
|
|
XResizeWindow(display, window, w + PAD * 2, h + PAD * 2);
|
|
|
|
XPutImage(display, window, DefaultGC(display, 0), ximage, 0, 0, PAD, PAD, w, h);
|
|
|
|
} break;
|
2024-01-15 14:29:37 -05:00
|
|
|
case ClientMessage:
|
2023-08-16 16:37:17 -04:00
|
|
|
emu_end(u);
|
2024-01-15 14:29:37 -05:00
|
|
|
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);
|
2024-01-15 23:15:48 -05:00
|
|
|
switch(sym) {
|
|
|
|
case XK_F1: toggle_scale(); break;
|
|
|
|
case XK_F2: u->dev[0x0e] = !u->dev[0x0e]; break;
|
|
|
|
case XK_F4: emu_restart(u, boot_rom, 0); break;
|
|
|
|
case XK_F5: emu_restart(u, boot_rom, 1); break;
|
2024-01-15 14:33:56 -05:00
|
|
|
}
|
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;
|
2024-01-15 14:29:37 -05:00
|
|
|
int x = clamp((e->x - PAD) / uxn_screen.scale, 0, uxn_screen.width - 1);
|
|
|
|
int y = clamp((e->y - PAD) / uxn_screen.scale, 0, uxn_screen.height - 1);
|
2023-06-07 11:42:22 -04:00
|
|
|
mouse_pos(u, &u->dev[0x90], x, 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-03-27 12:11:14 -04:00
|
|
|
static int
|
2024-01-15 13:17:37 -05:00
|
|
|
display_init(void)
|
2022-03-27 12:11:14 -04:00
|
|
|
{
|
|
|
|
Atom wmDelete;
|
2023-08-16 16:22:41 -04:00
|
|
|
static Visual *visual;
|
2024-01-15 13:17:37 -05:00
|
|
|
XColor black = {0};
|
|
|
|
static char empty[] = {0};
|
|
|
|
Pixmap bitmap;
|
|
|
|
Cursor blank;
|
|
|
|
display = XOpenDisplay(NULL);
|
|
|
|
if(!display)
|
|
|
|
return system_error("init", "Display failed");
|
2024-01-15 13:54:58 -05:00
|
|
|
screen_resize(WIDTH, HEIGHT, 1);
|
2024-01-15 14:18:36 -05:00
|
|
|
/* start window */
|
2022-03-27 12:11:14 -04:00
|
|
|
visual = DefaultVisual(display, 0);
|
|
|
|
if(visual->class != TrueColor)
|
2023-04-10 17:32:54 -04:00
|
|
|
return system_error("init", "True-color visual failed");
|
2024-01-15 14:18:36 -05:00
|
|
|
window = XCreateSimpleWindow(display, RootWindow(display, 0), 0, 0, uxn_screen.width + PAD * 2, uxn_screen.height + PAD * 2, 1, 0, 0);
|
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);
|
2024-01-15 13:17:37 -05:00
|
|
|
XStoreName(display, window, boot_rom);
|
2022-03-27 12:11:14 -04:00
|
|
|
XMapWindow(display, window);
|
2024-01-15 13:54:58 -05:00
|
|
|
ximage = XCreateImage(display, visual, DefaultDepth(display, DefaultScreen(display)), ZPixmap, 0, (char *)uxn_screen.pixels, uxn_screen.width, uxn_screen.height, 32, 0);
|
2024-01-15 13:17:37 -05:00
|
|
|
/* hide cursor */
|
|
|
|
bitmap = XCreateBitmapFromData(display, window, empty, 1, 1);
|
|
|
|
blank = XCreatePixmapCursor(display, bitmap, bitmap, &black, &black, 0, 0);
|
|
|
|
XDefineCursor(display, window, blank);
|
|
|
|
XFreeCursor(display, blank);
|
|
|
|
XFreePixmap(display, bitmap);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
emu_run(Uxn *u, char *rom)
|
|
|
|
{
|
|
|
|
int i = 1, n;
|
|
|
|
char expirations[8];
|
|
|
|
char coninp[CONINBUFSIZE];
|
|
|
|
struct pollfd fds[3];
|
|
|
|
static const struct itimerspec screen_tspec = {{0, 16666666}, {0, 16666666}};
|
2023-08-16 15:50:46 -04:00
|
|
|
/* 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(!u->dev[0x0f]) {
|
|
|
|
if(poll(fds, 3, 1000) <= 0)
|
|
|
|
continue;
|
|
|
|
while(XPending(display))
|
|
|
|
emu_event(u);
|
|
|
|
if(poll(&fds[1], 1, 0)) {
|
2024-01-15 14:18:36 -05:00
|
|
|
read(fds[1].fd, expirations, 8);
|
|
|
|
uxn_eval(u, u->dev[0x20] << 8 | u->dev[0x21]);
|
2024-01-20 19:32:44 -05:00
|
|
|
if(screen_changed()) {
|
2024-01-15 14:29:37 -05:00
|
|
|
int x = uxn_screen.x1 * uxn_screen.scale, y = uxn_screen.y1 * uxn_screen.scale;
|
|
|
|
int w = uxn_screen.x2 * uxn_screen.scale - x, h = uxn_screen.y2 * uxn_screen.scale - y;
|
2023-08-16 16:29:15 -04:00
|
|
|
screen_redraw(u);
|
2024-01-15 14:29:37 -05:00
|
|
|
XPutImage(display, window, DefaultGC(display, 0), ximage, x, y, x + PAD, y + PAD, w, h);
|
2023-08-16 16:29:15 -04:00
|
|
|
}
|
2023-08-16 15:50:46 -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++)
|
|
|
|
console_input(u, coninp[i], CONSOLE_STD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2023-10-31 12:00:02 -04:00
|
|
|
Uxn u = {0};
|
2023-08-16 15:50:46 -04:00
|
|
|
int i = 1;
|
2024-01-15 13:05:45 -05:00
|
|
|
if(i == argc) {
|
|
|
|
fprintf(stdout, "usage: %s [-v] file.rom [args..]\n", argv[0]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(argv[i][0] == '-' && argv[i][1] == 'v') {
|
2024-02-14 11:59:12 -05:00
|
|
|
fprintf(stdout, "Uxn11 - Varvara Emulator, 14 Feb 2023.\n");
|
2024-01-15 13:05:45 -05:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if(!system_boot(&u, (Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8)), argv[i++])) {
|
|
|
|
fprintf(stdout, "Could not boot.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2024-01-15 13:17:37 -05:00
|
|
|
if(!display_init()) {
|
|
|
|
fprintf(stdout, "Could not open display.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2023-08-16 16:10:42 -04:00
|
|
|
/* Game Loop */
|
|
|
|
u.dev[0x17] = argc - i;
|
|
|
|
if(uxn_eval(&u, PAGE_PROGRAM)) {
|
|
|
|
console_listen(&u, i, argc, argv);
|
|
|
|
emu_run(&u, boot_rom);
|
2022-03-27 13:01:06 -04:00
|
|
|
}
|
2023-08-16 16:01:50 -04:00
|
|
|
return emu_end(&u);
|
2022-03-27 01:38:03 -04:00
|
|
|
}
|