diff --git a/build.sh b/build.sh index 33e527b..de48653 100755 --- a/build.sh +++ b/build.sh @@ -2,8 +2,8 @@ RELEASE_FLAGS="-Os -DNDEBUG -g0 -s" DEBUG_FLAGS="-std=c89 -D_POSIX_C_SOURCE=199309L -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined" -EMU_INC="src/uxn.c src/devices/system.c src/devices/screen.c src/devices/controller.c src/devices/mouse.c src/devices/file.c src/devices/datetime.c src/uxn11.c -o bin/uxn11 -lX11" -CLI_INC="src/uxn.c src/devices/system.c src/devices/file.c src/devices/datetime.c src/uxncli.c -o bin/uxncli" +EMU_INC="src/uxn.c src/devices/system.c src/devices/console.c src/devices/screen.c src/devices/controller.c src/devices/mouse.c src/devices/file.c src/devices/datetime.c src/uxn11.c -o bin/uxn11 -lX11" +CLI_INC="src/uxn.c src/devices/system.c src/devices/console.c src/devices/file.c src/devices/datetime.c src/uxncli.c -o bin/uxncli" # find X11 libs on various systems if [ -e /usr/X11R6 ]; then diff --git a/src/devices/console.c b/src/devices/console.c new file mode 100644 index 0000000..bda96a2 --- /dev/null +++ b/src/devices/console.c @@ -0,0 +1,35 @@ +#include +#include + +#include "../uxn.h" +#include "console.h" + +/* +Copyright (c) 2022-2023 Devine Lu Linvega, Andrew Alderwick + +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. +*/ + +int +console_input(Uxn *u, char c) +{ + Uint8 *d = &u->dev[0x10]; + d[0x02] = c; + return uxn_eval(u, PEEK2(d)); +} + +void +console_deo(Uint8 *d, Uint8 port) +{ + FILE *fd = port == 0x8 ? stdout : port == 0x9 ? stderr : + 0; + if(fd) { + fputc(d[port], fd); + fflush(fd); + } +} \ No newline at end of file diff --git a/src/devices/console.h b/src/devices/console.h new file mode 100644 index 0000000..965daf8 --- /dev/null +++ b/src/devices/console.h @@ -0,0 +1,13 @@ +/* +Copyright (c) 2021 Devine Lu Linvega, Andrew Alderwick + +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. +*/ + +int console_input(Uxn *u, char c); +void console_deo(Uint8 *d, Uint8 port); diff --git a/src/devices/controller.c b/src/devices/controller.c index 55a7d06..1bd9ce4 100644 --- a/src/devices/controller.c +++ b/src/devices/controller.c @@ -17,7 +17,7 @@ controller_down(Uxn *u, Uint8 *d, Uint8 mask) { if(mask) { d[2] |= mask; - uxn_eval(u, GETVEC(d)); + uxn_eval(u, PEEK2(d)); } } @@ -26,7 +26,7 @@ controller_up(Uxn *u, Uint8 *d, Uint8 mask) { if(mask) { d[2] &= (~mask); - uxn_eval(u, GETVEC(d)); + uxn_eval(u, PEEK2(d)); } } @@ -35,7 +35,7 @@ controller_key(Uxn *u, Uint8 *d, Uint8 key) { if(key) { d[3] = key; - uxn_eval(u, GETVEC(d)); + uxn_eval(u, PEEK2(d)); d[3] = 0x00; } } diff --git a/src/devices/mouse.c b/src/devices/mouse.c index 0d6d7e8..63689e2 100644 --- a/src/devices/mouse.c +++ b/src/devices/mouse.c @@ -16,14 +16,14 @@ void mouse_down(Uxn *u, Uint8 *d, Uint8 mask) { d[6] |= mask; - uxn_eval(u, GETVEC(d)); + uxn_eval(u, PEEK2(d)); } void mouse_up(Uxn *u, Uint8 *d, Uint8 mask) { d[6] &= (~mask); - uxn_eval(u, GETVEC(d)); + uxn_eval(u, PEEK2(d)); } void @@ -31,7 +31,7 @@ mouse_pos(Uxn *u, Uint8 *d, Uint16 x, Uint16 y) { POKDEV(0x2, x); POKDEV(0x4, y); - uxn_eval(u, GETVEC(d)); + uxn_eval(u, PEEK2(d)); } void @@ -39,7 +39,7 @@ mouse_scroll(Uxn *u, Uint8 *d, Uint16 x, Uint16 y) { POKDEV(0xa, x); POKDEV(0xc, -y); - uxn_eval(u, GETVEC(d)); + uxn_eval(u, PEEK2(d)); POKDEV(0xa, 0); POKDEV(0xc, 0); } diff --git a/src/devices/system.c b/src/devices/system.c index 94e4031..8178191 100644 --- a/src/devices/system.c +++ b/src/devices/system.c @@ -89,7 +89,7 @@ int uxn_halt(Uxn *u, Uint8 instr, Uint8 err, Uint16 addr) { Uint8 *d = &u->dev[0x00]; - Uint16 handler = GETVEC(d); + Uint16 handler = PEEK2(d); if(handler) { u->wst->ptr = 4; u->wst->dat[0] = addr >> 0x8; diff --git a/src/uxn.h b/src/uxn.h index e67b746..c6c5912 100644 --- a/src/uxn.h +++ b/src/uxn.h @@ -19,7 +19,9 @@ typedef unsigned int Uint32; /* clang-format off */ -#define GETVEC(d) ((d)[0] << 8 | (d)[1]) +#define POKE2(d, v) { (d)[0] = (v) >> 8; (d)[1] = (v); } +#define PEEK2(d) ((d)[0] << 8 | (d)[1]) + #define POKDEV(x, y) { d[(x)] = (y) >> 8; d[(x) + 1] = (y); } #define PEKDEV(o, x) { (o) = (d[(x)] << 8) + d[(x) + 1]; } diff --git a/src/uxn11.c b/src/uxn11.c index ee80ad5..2726c7b 100644 --- a/src/uxn11.c +++ b/src/uxn11.c @@ -9,6 +9,7 @@ #include "uxn.h" #include "devices/system.h" +#include "devices/console.h" #include "devices/screen.h" #include "devices/controller.h" #include "devices/mouse.h" @@ -46,25 +47,6 @@ emu_error(char *msg, const char *err) return 0; } -static int -console_input(Uxn *u, char c) -{ - Uint8 *d = &u->dev[0x10]; - d[0x02] = c; - return uxn_eval(u, GETVEC(d)); -} - -static void -console_deo(Uint8 *d, Uint8 port) -{ - FILE *fd = port == 0x8 ? stdout : port == 0x9 ? stderr : - 0; - if(fd) { - fputc(d[port], fd); - fflush(fd); - } -} - static Uint8 emu_dei(Uxn *u, Uint8 addr) { @@ -260,8 +242,8 @@ main(int argc, char **argv) while(XPending(display)) emu_event(&u); if(poll(&fds[1], 1, 0)) { - read(fds[1].fd, expirations, 8); /* Indicate we handled the timer */ - uxn_eval(&u, GETVEC(&u.dev[0x20])); /* Call the vector once, even if the timer fired multiple times */ + 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 */ } if((fds[2].revents & POLLIN) != 0) { n = read(fds[2].fd, coninp, CONINBUFSIZE - 1); diff --git a/src/uxncli.c b/src/uxncli.c index 35dfc8b..deb5269 100644 --- a/src/uxncli.c +++ b/src/uxncli.c @@ -31,7 +31,7 @@ console_input(Uxn *u, char c) { Uint8 *d = &u->dev[0x10]; d[0x02] = c; - return uxn_eval(u, GETVEC(d)); + return uxn_eval(u, PEEK2(d)); } static void