From 79a788279b75083b065785793822bcf38aad1a62 Mon Sep 17 00:00:00 2001 From: Hayley Patton Date: Mon, 13 Nov 2023 15:18:49 +1100 Subject: [PATCH 1/2] Add Emscripten/WASM support --- .gitignore | 2 ++ bin/makefile-emscripten.wasm-wasm | 19 +++++++++++++++++++ bin/makeright | 5 +++++ inc/maiko/platform.h | 13 +++++++++++++ src/timer.c | 5 +++++ src/unixfork.c | 5 +++++ src/xc.c | 10 ++++++++++ 7 files changed, 59 insertions(+) create mode 100644 bin/makefile-emscripten.wasm-wasm diff --git a/.gitignore b/.gitignore index 0111213..306defa 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,8 @@ cmake-build-*/** *.x86_64-x/** *.x86_64-sdl/** *.x86_64/** +*.wasm/** +*.wasm-wasm/** *.armv7l-x/** *.armv7l/** *.aarch64-x/** diff --git a/bin/makefile-emscripten.wasm-wasm b/bin/makefile-emscripten.wasm-wasm new file mode 100644 index 0000000..2007eec --- /dev/null +++ b/bin/makefile-emscripten.wasm-wasm @@ -0,0 +1,19 @@ +# Options for Emscripten, WASM and SDL + +CC = emcc -sUSE_SDL=2 -sASYNCIFY -sALLOW_MEMORY_GROWTH $(CLANG_CFLAGS) + +XFILES = $(OBJECTDIR)sdl.o + +XFLAGS = -DSDL + +# OPTFLAGS is normally -O2. +OPTFLAGS = -O2 -g3 +DFLAGS = $(XFLAGS) -DRELEASE=351 + +LD = emcc +LDFLAGS = -sUSE_SDL=2 --preload-file $(SYSOUT)@full.sysout +LDELDFLAGS = + +OBJECTDIR = ../$(RELEASENAME)/ + +default : ../$(OSARCHNAME)/lde ../$(OSARCHNAME)/ldesdl.js diff --git a/bin/makeright b/bin/makeright index 1fde522..156685c 100755 --- a/bin/makeright +++ b/bin/makeright @@ -74,6 +74,11 @@ case "$display" in sdl) releasename=${osversion}.${architecture}-${display} ldename=ldesdl ;; + wasm) osversion=emscripten + architecture=wasm + releasename=${osversion}.${architecture}-${display} + ldename=ldesdl.js + ;; *) echo "display-option: $display is not supported." exit ;; diff --git a/inc/maiko/platform.h b/inc/maiko/platform.h index 2fa747e..a69b21f 100644 --- a/inc/maiko/platform.h +++ b/inc/maiko/platform.h @@ -83,6 +83,19 @@ # define MAIKO_OS_DETECTED 1 #endif +#ifdef __EMSCRIPTEN__ +# define MAIKO_OS_LINUX 1 +# define MAIKO_OS_EMSCRIPTEN 1 +# define MAIKO_OS_NAME "Emscripten" +# define MAIKO_EMULATE_TIMER_INTERRUPTS 1 +# define MAIKO_EMULATE_ASYNC_INTERRUPTS 1 +# define MAIKO_OS_UNIX_LIKE 1 +# define MAIKO_OS_DETECTED +# define MAIKO_ARCH_NAME "WebAssembly" +# define MAIKO_ARCH_WORD_BITS 32 +# define MAIKO_ARCH_DETECTED 1 +#endif + /* __x86_64__: GNU C, __x86_64: Sun Studio, _M_AMD64: Visual Studio */ #if defined(__x86_64__) || defined(__x86_64) || defined(_M_AMD64) # define MAIKO_ARCH_X86_64 1 diff --git a/src/timer.c b/src/timer.c index 5d75bb2..2cb14c2 100644 --- a/src/timer.c +++ b/src/timer.c @@ -73,6 +73,11 @@ extern int ether_fd; extern DspInterface currentdsp; #endif /* XWINDOW */ +#ifdef MAIKO_OS_EMSCRIPTEN +/* We can't touch the system clock */ +#define settimeofday(tv, tz) +#endif + #define LISP_UNIX_TIME_DIFF 29969152 #define LISP_ALTO_TIME_MASK 0x80000000 #define UNIX_ALTO_TIME_DIFF 2177452800U diff --git a/src/unixfork.c b/src/unixfork.c index 3b9dce1..b35f1ef 100644 --- a/src/unixfork.c +++ b/src/unixfork.c @@ -45,6 +45,11 @@ int flushing = 0; #endif +#ifdef MAIKO_OS_EMSCRIPTEN +/* We don't have any shell */ +#define getusershell() NULL +#endif + /* Used to communicate between Unix subprocesses and LISP */ static long StartTime; /* Time, for creating pipe filenames */ diff --git a/src/xc.c b/src/xc.c index 5140042..644e00d 100644 --- a/src/xc.c +++ b/src/xc.c @@ -33,6 +33,10 @@ #include #endif /* DOS */ +#ifdef __EMSCRIPTEN__ +#include /* Defines emscripten_sleep */ +#endif /* __EMSCRIPTEN__ */ + #include "lispemul.h" #include "emlglob.h" #include "address.h" @@ -183,6 +187,9 @@ static int pseudoTimerAsyncCountdown = MAIKO_TIMER_ASYNC_EMULATION_INSNS_COUNTDO void dispatch(void) { InstPtr pccache; +#ifdef __EMSCRIPTEN__ + int cycles = 0; +#endif #if defined(OPDISP) static const void* optable[256] = { @@ -261,6 +268,9 @@ op_ufn : { /* DISPATCH "LOOP" */ nextopcode: +#ifdef __EMSCRIPTEN__ + if (++cycles % 1000000 == 0) emscripten_sleep(1); +#endif #ifdef MYOPTRACE if ((struct fnhead *)NativeAligned4FromLAddr(0x2ed600) == FuncObj) { quick_stack_check(); From 7b2cf44c7cca62e5faf2efa5b4912da79cee0ffc Mon Sep 17 00:00:00 2001 From: Hayley Patton Date: Mon, 13 Nov 2023 15:30:19 +1100 Subject: [PATCH 2/2] Add ldesdl.html --- .gitignore | 1 + emscripten.wasm/ldesdl.html | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 emscripten.wasm/ldesdl.html diff --git a/.gitignore b/.gitignore index 306defa..8e0d861 100644 --- a/.gitignore +++ b/.gitignore @@ -20,6 +20,7 @@ cmake-build-*/** *.x86_64-sdl/** *.x86_64/** *.wasm/** +!emscripten.wasm/ldesdl.html *.wasm-wasm/** *.armv7l-x/** *.armv7l/** diff --git a/emscripten.wasm/ldesdl.html b/emscripten.wasm/ldesdl.html new file mode 100644 index 0000000..3ddffcd --- /dev/null +++ b/emscripten.wasm/ldesdl.html @@ -0,0 +1,20 @@ + + + + + + + + +
+ +
+ + + +