Skip to content

Commit

Permalink
start on emscripten support
Browse files Browse the repository at this point in the history
builds but runs with errors
  • Loading branch information
ToadKing committed Feb 28, 2020
1 parent 8dcd819 commit 9ed9439
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,10 @@ namespace moodycamel

bool timed_wait(std::uint64_t usecs) AE_NO_TSAN
{
#ifdef EMSCRIPTEN
// emscripten doesn't have sem_timedwait, just do a try wait
return try_wait();
#else
struct timespec ts;
const int usecs_in_1_sec = 1000000;
const int nsecs_in_1_sec = 1000000000;
Expand All @@ -546,6 +550,7 @@ namespace moodycamel
rc = sem_timedwait(&m_sema, &ts);
} while (rc == -1 && errno == EINTR);
return !(rc == -1 && errno == ETIMEDOUT);
#endif
}

void signal() AE_NO_TSAN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ extern "C" {
extern void context_reset();
bool threaded_gl_safe_shutdown = false;

void gln64_thr_gl_invoke_command_loop()
void gln64_thr_gl_invoke_command_loop(void)
{
opengl::FunctionWrapper::commandLoop();
}
Expand Down
25 changes: 1 addition & 24 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -392,35 +392,12 @@ else ifeq ($(platform), emscripten)
GLES := 1
WITH_DYNAREC :=
CPUFLAGS += -DEMSCRIPTEN -DNO_ASM -s USE_ZLIB=1
PLATCFLAGS += \
-Dsinc_resampler=glupen_sinc_resampler \
-DCC_resampler=glupen_CC_resampler \
-Drglgen_symbol_map=glupen_rglgen_symbol_map \
-Drglgen_resolve_symbols_custom=glupen_rglgen_resolve_symbols_custom \
-Drglgen_resolve_symbols=glupen_rglgen_resolve_symbols \
-Dmemalign_alloc=glupen_memalign_alloc \
-Dmemalign_free=glupen_memalign_free \
-Dmemalign_alloc_aligned=glupen_memalign_alloc_aligned \
-Daudio_resampler_driver_find_handle=glupen_audio_resampler_driver_find_handle \
-Daudio_resampler_driver_find_ident=glupen_audio_resampler_driver_find_ident \
-Drarch_resampler_realloc=glupen_rarch_resampler_realloc \
-Dconvert_float_to_s16_C=glupen_convert_float_to_s16_C \
-Dconvert_float_to_s16_init_simd=glupen_convert_float_to_s16_init_simd \
-Dconvert_s16_to_float_C=glupen_convert_s16_to_float_C \
-Dconvert_s16_to_float_init_simd=glupen_convert_s16_to_float_init_simd \
-Dcpu_features_get_perf_counter=glupen_cpu_features_get_perf_counter \
-Dcpu_features_get_time_usec=glupen_cpu_features_get_time_usec \
-Dcpu_features_get_core_amount=glupen_cpu_features_get_core_amount \
-Dcpu_features_get=glupen_cpu_features_get \
-Dffs=glupen_ffs \
-Dstrlcpy_retro__=glupen_strlcpy_retro__ \
-Dstrlcat_retro__=glupen_strlcat_retro__
CC = emcc
CXX = em++
HAVE_NEON = 0

COREFLAGS += -DOS_LINUX
ASFLAGS = -f elf -d ELF_TYPE
STATIC_LINKING = 1
# Windows
else
TARGET := $(TARGET_NAME)_libretro.dll
Expand Down
5 changes: 5 additions & 0 deletions custom/dependencies/libzlib/gzguts.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
#endif
#include <fcntl.h>

/* TODO: Do other platforms need this? */
#ifdef EMSCRIPTEN
# include <unistd.h>
#endif

#ifdef _WIN32
# include <stddef.h>
#endif
Expand Down
2 changes: 1 addition & 1 deletion custom/mupen64plus-next_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ extern char* retro_dd_path_img;
extern char* retro_dd_path_rom;

// Threaded GL Callback
extern void gln64_thr_gl_invoke_command_loop();
extern void gln64_thr_gl_invoke_command_loop(void);
extern bool threaded_gl_safe_shutdown;

// Core options
Expand Down
64 changes: 64 additions & 0 deletions libretro-common/libco/emscripten_fiber.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
libco.emscripten (2020-02-27)
authors: Toad King
license: public domain
*/

#define LIBCO_C
#include <libco.h>
#include <stdlib.h>
#include <stddef.h>
#include <malloc.h>
#include <emscripten/fiber.h>

#define ASYNCIFY_STACK_SIZE (32768)

static thread_local emscripten_fiber_t *co_active_;

static void co_thunk(void *coentry)
{
((void (*)(void))coentry)();
}

static void co_init(void)
{
if (!co_active_)
{
emscripten_fiber_t *co_primary = calloc(1, sizeof(emscripten_fiber_t));
void *asyncify_stack = malloc(ASYNCIFY_STACK_SIZE);

emscripten_fiber_init_from_current_context(co_primary, asyncify_stack, ASYNCIFY_STACK_SIZE);
co_active_ = co_primary;
}
}

cothread_t co_active(void)
{
co_init();
return co_active_;
}

cothread_t co_create(unsigned int stacksize, void (*coentry)(void))
{
co_init();

emscripten_fiber_t *fiber = calloc(1, sizeof(emscripten_fiber_t));
void *asyncify_stack = malloc(ASYNCIFY_STACK_SIZE);
void *c_stack = memalign(16, stacksize);
emscripten_fiber_init(fiber, co_thunk, coentry, c_stack, stacksize, asyncify_stack, ASYNCIFY_STACK_SIZE);

return (cothread_t)fiber;
}

void co_delete(cothread_t cothread)
{
free(cothread);
}

void co_switch(cothread_t cothread)
{
emscripten_fiber_t *old_fiber = co_active_;
co_active_ = (emscripten_fiber_t *)cothread;

emscripten_fiber_swap(old_fiber, co_active_);
}
4 changes: 3 additions & 1 deletion libretro-common/libco/libco.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ void *genode_alloc_secondary_stack(unsigned long stack_size);
void genode_free_secondary_stack(void *stack);
#endif

#if defined _MSC_VER
#ifdef EMSCRIPTEN
#include "emscripten_fiber.c"
#elif defined _MSC_VER
#include <Windows.h>
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)
#include "fiber.c"
Expand Down
7 changes: 3 additions & 4 deletions libretro/libretro.c
Original file line number Diff line number Diff line change
Expand Up @@ -464,12 +464,11 @@ static void emu_step_initialize(void)
plugin_connect_all();
}

#ifdef HAVE_LIBNX
#define EMUTHREAD_RET_TYPE void
#if defined(HAVE_LIBNX)
static void EmuThreadFunction(void* param)
#else
#define EMUTHREAD_RET_TYPE void*
static void EmuThreadFunction(void)
#endif
static EMUTHREAD_RET_TYPE EmuThreadFunction(void* param)
{
log_cb(RETRO_LOG_DEBUG, CORE_NAME ": [EmuThread] M64CMD_EXECUTE\n");

Expand Down
2 changes: 1 addition & 1 deletion mupen64plus-core/src/device/r4300/idec.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,6 @@ size_t idec_u53(uint32_t iw, uint8_t u53, uint8_t* u5);

#define IDEC_U53(r4300, iw, u53, u5) (void*)(((char*)(r4300)) + idec_u53((iw), (u53), (u5)))

const char* g_r4300_opcodes[R4300_OPCODES_COUNT];
extern const char* g_r4300_opcodes[R4300_OPCODES_COUNT];

#endif
1 change: 0 additions & 1 deletion mupen64plus-core/src/main/workqueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

struct work_struct;

struct work_struct *work;
typedef void (*work_func_t)(struct work_struct *work);
struct work_struct {
work_func_t func;
Expand Down

0 comments on commit 9ed9439

Please sign in to comment.