Skip to content

Commit 1efe85f

Browse files
committed
Add emscripten support for BlastEm patch
1 parent f52cfb1 commit 1efe85f

File tree

2 files changed

+96
-54
lines changed

2 files changed

+96
-54
lines changed

emulator-patches/blastem-0.6.2.patch

Lines changed: 56 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
commit afd33eaffc60bf38c895f370af0a683a6fe67aa2
1+
commit 9acf6e19a6633eeecb5feabd5fab0188468aa16e
22
Author: Joey Parrish <[email protected]>
33
Date: Wed Mar 27 07:39:28 2024 -0700
44

@@ -21,7 +21,7 @@ index 0000000..8f39f7f
2121
+zdis
2222
+kinetoscope
2323
diff --git a/Makefile b/Makefile
24-
index 0dddc9f..4655371 100644
24+
index 0dddc9f..cfa4645 100644
2525
--- a/Makefile
2626
+++ b/Makefile
2727
@@ -23,11 +23,11 @@ ifeq ($(CPU),i686)
@@ -38,21 +38,7 @@ index 0dddc9f..4655371 100644
3838
GLUDIR:=x64
3939
endif
4040
GLEW32S_LIB:=$(GLEW_PREFIX)/lib/Release/$(GLUDIR)/glew32s.lib
41-
@@ -39,6 +39,13 @@ LDFLAGS+= $(GLEW32S_LIB) -L"$(SDL2_PREFIX)/lib" -lSDL2main -lSDL2 -lopengl32 -lg
42-
endif
43-
LIBZOBJS=$(BUNDLED_LIBZ)
44-
45-
+# Static libcurl for kinetoscope emulation
46-
+CFLAGS+= -Icurl/include -DCURL_STATICLIB
47-
+LDFLAGS+= curl/lib/.libs/libcurl.a
48-
+# Pthread for kinetoscope emulation
49-
+CFLAGS+= -pthread
50-
+LDFLAGS+= -pthread
51-
+
52-
else
53-
54-
MEM:=mem.o
55-
@@ -96,10 +103,11 @@ LDFLAGS:=-lm glew/lib/libGLEW.a
41+
@@ -96,10 +96,11 @@ LDFLAGS:=-lm glew/lib/libGLEW.a
5642
endif
5743

5844
ifeq ($(OS),Darwin)
@@ -68,54 +54,59 @@ index 0dddc9f..4655371 100644
6854
else
6955
SDL_INCLUDE_PATH:=sdl/include
7056
LDFLAGS+= -Wl,-rpath='$$ORIGIN/lib' -Llib -lSDL2
71-
@@ -109,6 +117,16 @@ endif
72-
endif #Darwin
73-
CFLAGS+= -I$(SDL_INCLUDE_PATH)
57+
@@ -129,6 +130,38 @@ endif
58+
endif #PORTABLE
59+
endif #Windows
7460

75-
+# Static libcurl for kinetoscope emulation
76-
+CFLAGS+= -Icurl/include
61+
+
62+
+# Kinetoscope code itself
63+
+CFLAGS+= -I. -Ikinetoscope/software/player/inc/
64+
+
65+
+ifeq ($(PLATFORM),emscripten)
66+
+CFLAGS+= -sMEMORY64=1
67+
+LDFLAGS+= -sMEMORY64=1
68+
+endif
69+
+
70+
+ifneq ($(PLATFORM),emscripten)
71+
+# Curl, but only if it's not an emscripten environment
72+
+ifdef PORTABLE
73+
+# Static libcurl
74+
+CFLAGS+= -Icurl/include -DCURL_STATICLIB
7775
+LDFLAGS+= curl/lib/.libs/libcurl.a
76+
+else
77+
+# System libcurl
78+
+LDFLAGS+= $(shell curl-config --libs)
79+
+endif
80+
+endif
81+
+
82+
+# Pthread is required
83+
+CFLAGS+= -pthread
84+
+LDFLAGS+= -pthread
85+
+
7886
+ifeq ($(OS),Darwin)
87+
+# Needed for curl on macOS?
7988
+LDFLAGS+= -framework SystemConfiguration
8089
+endif #Darwin
81-
+# Pthread for kinetoscope emulation
82-
+CFLAGS+= -pthread
83-
+LDFLAGS+= -pthread
8490
+
85-
else
86-
ifeq ($(MAKECMDGOALS),libblastem.$(SO))
87-
LDFLAGS:=-lm
88-
@@ -126,6 +144,12 @@ LDFLAGS+= -framework OpenGL -framework AppKit
89-
endif
90-
endif
91-
92-
+# System libcurl for Kinetoscope emulation
93-
+LDFLAGS+= $(shell curl-config --libs)
94-
+# Pthread for kinetoscope emulation
95-
+CFLAGS+= -pthread
96-
+LDFLAGS+= -pthread
9791
+
98-
endif #PORTABLE
99-
endif #Windows
100-
101-
@@ -221,6 +245,7 @@ endif
92+
+
93+
ifndef OPT
94+
ifdef DEBUG
95+
OPT:=-g3 -O0
96+
@@ -221,10 +254,12 @@ endif
10297

10398
MAINOBJS=blastem.o system.o genesis.o debug.o gdb_remote.o vdp.o $(RENDEROBJS) io.o romdb.o hash.o menu.o xband.o \
10499
realtec.o i2c.o nor.o sega_mapper.o multi_game.o megawifi.o $(NET) serialize.o $(TERMINAL) $(CONFIGOBJS) gst.o \
105100
+ kinetoscope/emulator-patches/kinetoscope.o \
106101
$(M68KOBJS) $(TRANSOBJS) $(AUDIOOBJS) saves.o zip.o bindings.o jcart.o gen_player.o
107102

108103
LIBOBJS=libblastem.o system.o genesis.o debug.o gdb_remote.o vdp.o io.o romdb.o hash.o xband.o realtec.o \
109-
@@ -277,6 +302,8 @@ ifeq ($(MAKECMDGOALS),libblastem.$(SO))
110-
CFLAGS+= -fpic -DIS_LIB
111-
endif
112-
113-
+CFLAGS+= -I. -Ikinetoscope/software/player/inc/
114-
+
115-
all : $(ALL)
116-
117-
libblastem.$(SO) : $(LIBOBJS)
118-
@@ -366,7 +393,7 @@ m68k.c : m68k.cpu cpu_dsl.py
104+
i2c.o nor.o sega_mapper.o multi_game.o megawifi.o $(NET) serialize.o $(TERMINAL) $(CONFIGOBJS) gst.o \
105+
+ kinetoscope/emulator-patches/kinetoscope.o \
106+
$(M68KOBJS) $(TRANSOBJS) $(AUDIOOBJS) saves.o jcart.o rom.db.o gen_player.o $(LIBZOBJS)
107+
108+
ifdef NONUKLEAR
109+
@@ -366,7 +401,7 @@ m68k.c : m68k.cpu cpu_dsl.py
119110
$(CC) $(CFLAGS) -c -o $@ $<
120111

121112
%.png : %.xcf
@@ -124,11 +115,24 @@ index 0dddc9f..4655371 100644
124115

125116
%.tiles : %.spec
126117
./img2tiles.py -s $< $@
127-
@@ -393,3 +420,4 @@ tmss.md : font.tiles
118+
@@ -393,3 +428,4 @@ tmss.md : font.tiles
128119

129120
clean :
130121
rm -rf $(ALL) trans ztestrun ztestgen *.o nuklear_ui/*.o zlib/*.o
131122
+ rm -f kinetoscope/emulator-patches/kinetoscope.o
123+
diff --git a/Makefile.libretro b/Makefile.libretro
124+
index a8af9bc..f890d01 100644
125+
--- a/Makefile.libretro
126+
+++ b/Makefile.libretro
127+
@@ -58,7 +58,7 @@ endif
128+
129+
130+
core: $(OBJ)
131+
- $(MAKE) $(target) OS=$(OS) CC=$(CC) CPU=$(ABI) LIBRETRO=$(LIBRETRO)
132+
+ $(MAKE) $(target) OS=$(OS) CC=$(CC) CPU=$(ABI) LIBRETRO=$(LIBRETRO) PLATFORM=$(platform)
133+
cp -v $(target) $(libname)
134+
135+
install: $(libname)
132136
diff --git a/build_release b/build_release
133137
index f5faa10..aa30c75 100755
134138
--- a/build_release

emulator-patches/kinetoscope.c

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@
88

99
#include <stdbool.h>
1010
#include <stdint.h>
11+
#include <stdlib.h>
1112
#include <string.h>
1213
#include <unistd.h>
1314

14-
#include <curl/curl.h>
15+
#if defined(__EMSCRIPTEN__)
16+
# include <emscripten/fetch.h>
17+
#else
18+
# include <curl/curl.h>
19+
#endif
20+
1521
#include <pthread.h>
1622

1723
#include "genesis.h"
@@ -182,7 +188,7 @@ static void write_error_to_sram() {
182188
// Writes HTTP data to SRAM.
183189
static size_t http_data_to_sram(char* data, size_t size, size_t n, void* ctx) {
184190
if (global_compressed) {
185-
rle_to_sram(data, size * n);
191+
rle_to_sram((const uint8_t*)data, size * n);
186192
} else {
187193
write_sram(global_sram_offset, (const uint8_t*)data, size * n);
188194
global_sram_offset += size * n;
@@ -223,6 +229,7 @@ static bool fetch_range(const char* url, size_t first_byte, size_t size,
223229
// snprintf doesn't guarantee a terminator when it overflows.
224230
range[31] = '\0';
225231

232+
#if !defined(__EMSCRIPTEN__)
226233
CURL* handle = curl_easy_init();
227234
curl_easy_setopt(handle, CURLOPT_URL, url);
228235
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_callback);
@@ -245,6 +252,35 @@ static bool fetch_range(const char* url, size_t first_byte, size_t size,
245252
}
246253

247254
return res == CURLE_OK && (http_code == 200 || http_code == 206);
255+
#else
256+
emscripten_fetch_attr_t fetch_attributes;
257+
emscripten_fetch_attr_init(&fetch_attributes);
258+
259+
strcpy(fetch_attributes.requestMethod, "GET");
260+
fetch_attributes.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY;
261+
fetch_attributes.attributes |= EMSCRIPTEN_FETCH_SYNCHRONOUS;
262+
263+
const char* headers[] = { "Range", range, NULL };
264+
fetch_attributes.requestHeaders = headers;
265+
266+
emscripten_fetch_t *fetch = emscripten_fetch(&fetch_attributes, url);
267+
268+
int http_code = fetch->status;
269+
printf("Kinetoscope: url = %s, http status = %d\n",
270+
url, http_code);
271+
272+
bool ok = http_code == 200 || http_code == 206;
273+
if (ok) {
274+
write_callback(fetch->data, fetch->numBytes, 1, ctx);
275+
} else {
276+
char buf[64];
277+
snprintf(buf, 64, "fetch error, code %d", http_code);
278+
report_error(buf);
279+
}
280+
281+
emscripten_fetch_close(fetch);
282+
return ok;
283+
#endif
248284
}
249285

250286
static bool fetch_range_to_sram(const char* url, size_t first_byte,
@@ -469,7 +505,9 @@ void kinetoscope_init(void *sram_buffer, uint32_t sram_size) {
469505
global_sram_buffer = sram_buffer;
470506
global_sram_size = sram_size;
471507

508+
#if !defined(__EMSCRIPTEN__)
472509
curl_global_init(CURL_GLOBAL_ALL);
510+
#endif
473511

474512
global_fetch_busy = false;
475513
pthread_create(&global_fetch_thread, NULL, fetch_thread, NULL);

0 commit comments

Comments
 (0)