Skip to content

Instantly share code, notes, and snippets.

@kode54
Created April 20, 2021 02:46
Show Gist options
  • Save kode54/eadc26361c090ed48be8faf803cd2313 to your computer and use it in GitHub Desktop.
Save kode54/eadc26361c090ed48be8faf803cd2313 to your computer and use it in GitHub Desktop.
PCem patch to get things working on Apple Silicon, partially at least
diff --git a/configure.ac b/configure.ac
index 407b057..b237e9e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -72,6 +72,9 @@ case "${host_cpu}" in
CPU=arm64
AC_MSG_RESULT(${host_cpu})
;;
+ arm)
+ CPU=arm64 # MacOS has "arm" in the host string
+ ;;
*)
AC_MSG_ERROR([Unsupported CPU. ${host_cpu}])
;;
diff --git a/src/386_dynarec.c b/src/386_dynarec.c
index 9dedf44..6a59d76 100644
--- a/src/386_dynarec.c
+++ b/src/386_dynarec.c
@@ -1,6 +1,9 @@
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
+#if defined __APPLE__ && defined __aarch64__
+#include <pthread.h>
+#endif
#include "ibm.h"
#include "x86.h"
#include "x86_ops.h"
@@ -410,6 +413,10 @@ static inline void exec_recompiler(void)
cpu_new_blocks++;
+#if defined __APPLE__ && defined __aarch64__
+ pthread_jit_write_protect_np(0);
+#endif
+
codegen_block_start_recompile(block);
codegen_in_recompile = 1;
@@ -481,6 +488,10 @@ static inline void exec_recompiler(void)
codegen_reset();
codegen_in_recompile = 0;
+
+#if defined __APPLE__ && defined __aarch64__
+ pthread_jit_write_protect_np(1);
+#endif
}
else if (!cpu_state.abrt)
{
diff --git a/src/Makefile.am b/src/Makefile.am
index 8197093..2a5d7ce 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -125,6 +125,7 @@ pcem_SOURCES += cdrom-ioctl-dummy.c wx-sdl2-display.c
endif
if OS_MACOSX
+DEFAULT_INCLUDES = -iquote /opt/homebrew/include
pcem_SOURCES += cdrom-ioctl-osx.c wx-sdl2-display.c
pcem_CFLAGS += -DPCEM_RENDER_WITH_TIMER -DPCEM_RENDER_TIMER_LOOP
pcem_CXXFLAGS += -DPCEM_RENDER_WITH_TIMER -DPCEM_RENDER_TIMER_LOOP
@@ -137,7 +138,7 @@ pcem_LDADD += wx.res
endif
if !HAS_OFF64T
-#pcem_CFLAGS += -Doff64_t=off_t -Dfopen64=fopen -Dfseeko64=fseek -Dftello64=ftell
+pcem_CFLAGS += -Doff64_t=off_t -Dfopen64=fopen -Dfseeko64=fseek -Dftello64=ftell
endif
if RELEASE_BUILD
diff --git a/src/codegen_allocator.c b/src/codegen_allocator.c
index 5807b5d..fa5ea17 100644
--- a/src/codegen_allocator.c
+++ b/src/codegen_allocator.c
@@ -3,6 +3,9 @@
#include <unistd.h>
#include <stdlib.h>
#endif
+#ifdef __APPLE__
+#include <libkern/OSCacheControl.h>
+#endif
#if defined WIN32 || defined _WIN32 || defined _WIN32
#include <windows.h>
#endif
@@ -30,6 +33,8 @@ void codegen_allocator_init()
#if defined WIN32 || defined _WIN32 || defined _WIN32
mem_block_alloc = VirtualAlloc(NULL, MEM_BLOCK_NR * MEM_BLOCK_SIZE, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
+#elif defined __APPLE__
+ mem_block_alloc = mmap(0, MEM_BLOCK_NR * MEM_BLOCK_SIZE, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_PRIVATE|MAP_JIT, 0, 0);
#else
mem_block_alloc = mmap(0, MEM_BLOCK_NR * MEM_BLOCK_SIZE, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_PRIVATE, 0, 0);
#endif
@@ -117,7 +122,11 @@ void codegen_allocator_clean_blocks(struct mem_block_t *block)
#if defined __ARM_EABI__ || defined __aarch64__
while (1)
{
+#if defined(IOS) || defined(__APPLE__)
+ sys_cache_control(kCacheFunctionPrepareForExecution, &mem_block_alloc[block->offset], MEM_BLOCK_SIZE);
+#else
__clear_cache(&mem_block_alloc[block->offset], &mem_block_alloc[block->offset + MEM_BLOCK_SIZE]);
+#endif
if (block->next)
block = &mem_blocks[block->next - 1];
else
diff --git a/src/pc.c b/src/pc.c
index 6ed0be9..1dc85d5 100644
--- a/src/pc.c
+++ b/src/pc.c
@@ -1,6 +1,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
+#if defined __APPLE__ && defined __aarch64__
+#include <pthread.h>
+#endif
#include "ibm.h"
#include "device.h"
@@ -306,9 +309,17 @@ void initpc(int argc, char *argv[])
mem_init();
loadbios();
mem_add_bios();
-
+
+#if defined __APPLE__ && defined __aarch64__
+ pthread_jit_write_protect_np(0);
+#endif
+
codegen_init();
-
+
+#if defined __APPLE__ && defined __aarch64__
+ pthread_jit_write_protect_np(1);
+#endif
+
timer_reset();
sound_reset();
io_init();
diff --git a/src/wx-thread.c b/src/wx-thread.c
index b5958ad..7900941 100644
--- a/src/wx-thread.c
+++ b/src/wx-thread.c
@@ -1,6 +1,9 @@
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
+#if defined __APPLE__
+#include <sys/time.h>
+#endif
#include "thread.h"
#if defined WIN32 || defined _WIN32 || defined _WIN32
#include <windows.h>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment