Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Libco #4

Merged
merged 5 commits into from
Jun 30, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 34 additions & 3 deletions Makefile.libretro
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,17 @@ else ifneq ($(findstring MINGW,$(shell uname -a)),)
system_platform = win
endif

CC_AS ?= $(CC)


ifeq ($(platform), unix)
ifneq (,$(findstring unix,$(platform)))
TARGET := $(TARGET_NAME)_libretro.so
fpic := -fPIC
SHARED := -shared -Wl,-version-script=link.T -Wl,-no-undefined
else ifneq (,$(findstring rpi,$(platform)))
TARGET := $(TARGET_NAME)_libretro.so
LDFLAGS += -shared -Wl,--version-script=libretro/link.T
fpic = -fPIC
SHARED := -shared -Wl,-version-script=link.T -Wl,-no-undefined
else ifeq ($(platform), osx)
TARGET := $(TARGET_NAME)_libretro.dylib
fpic := -fPIC
Expand All @@ -53,46 +58,57 @@ else ifeq ($(platform), ios)
fpic := -fPIC
SHARED := -dynamiclib


CC = clang -arch armv7 -isysroot $(IOSSDK)
CC_AS = perl ./tools/gas-preprocessor.pl $(CC)
CXX = clang++ -arch armv7 -isysroot $(IOSSDK)
OSXVER = `sw_vers -productVersion | cut -c 4`
ifneq ($(OSXVER),9)
CC += -miphoneos-version-min=5.0
CXX += -miphoneos-version-min=5.0
CC_AS += -miphoneos-version-min=5.0
PLATFORM_DEFINES := -miphoneos-version-min=5.0
endif
else ifeq ($(platform), qnx)
TARGET := $(TARGET_NAME)_libretro_qnx.so
fpic := -fPIC
SHARED := -lcpp -lm -shared -Wl,-version-script=link.T
CC = qcc -Vgcc_ntoarmv7le
CC_AS = qcc -Vgcc_ntoarmv7le
CXX = QCC -Vgcc_ntoarmv7le_cpp
AR = QCC -Vgcc_ntoarmv7le
PLATFORM_DEFINES := -D__BLACKBERRY_QNX__ -fexceptions -marm -mcpu=cortex-a9 -mfpu=neon -mfloat-abi=softfp
else ifeq ($(platform), ps3)
TARGET := $(TARGET_NAME)_libretro_ps3.a
CC = $(CELL_SDK)/host-win32/ppu/bin/ppu-lv2-gcc.exe
CC_AS = $(CELL_SDK)/host-win32/ppu/bin/ppu-lv2-gcc.exe
CXX = $(CELL_SDK)/host-win32/ppu/bin/ppu-lv2-g++.exe
AR = $(CELL_SDK)/host-win32/ppu/bin/ppu-lv2-ar.exe
PLATFORM_DEFINES := -D__CELLOS_LV2__
STATIC_LINKING = 1
CSRCS += Src/Libretro/libco/ppc.c
else ifeq ($(platform), sncps3)
TARGET := $(TARGET_NAME)_libretro_ps3.a
CC = $(CELL_SDK)/host-win32/sn/bin/ps3ppusnc.exe
CC_AS = $(CELL_SDK)/host-win32/sn/bin/ps3ppusnc.exe
CXX = $(CELL_SDK)/host-win32/sn/bin/ps3ppusnc.exe
AR = $(CELL_SDK)/host-win32/sn/bin/ps3snarl.exe
PLATFORM_DEFINES := -D__CELLOS_LV2__
STATIC_LINKING = 1
CSRCS += Src/Libretro/libco/ppc.c
else ifeq ($(platform), psl1ght)
TARGET := $(TARGET_NAME)_libretro_psl1ght.a
CC = $(PS3DEV)/ppu/bin/ppu-gcc$(EXE_EXT)
CC_AS = $(PS3DEV)/ppu/bin/ppu-gcc$(EXE_EXT)
CXX = $(PS3DEV)/ppu/bin/ppu-g++$(EXE_EXT)
AR = $(PS3DEV)/ppu/bin/ppu-ar$(EXE_EXT)
PLATFORM_DEFINES := -D__CELLOS_LV2__
STATIC_LINKING = 1
CSRCS += Src/Libretro/libco/ppc.c
else ifeq ($(platform), psp1)
TARGET := $(TARGET_NAME)_libretro_psp1.a
CC = psp-gcc$(EXE_EXT)
CC_AS = psp-gcc$(EXE_EXT)
CXX = psp-g++$(EXE_EXT)
AR = psp-ar$(EXE_EXT)
PLATFORM_DEFINES := -DPSP
Expand All @@ -105,29 +121,36 @@ else ifeq ($(platform), psp1)
else ifeq ($(platform), xenon)
TARGET := $(TARGET_NAME)_libretro_xenon360.a
CC = xenon-gcc$(EXE_EXT)
CC_AS = xenon-gcc$(EXE_EXT)
CXX = xenon-g++$(EXE_EXT)
AR = xenon-ar$(EXE_EXT)
PLATFORM_DEFINES := -D__LIBXENON__
STATIC_LINKING = 1
CSRCS += Src/Libretro/libco/ppc.c
else ifeq ($(platform), ngc)
TARGET := $(TARGET_NAME)_libretro_ngc.a
CC = $(DEVKITPPC)/bin/powerpc-eabi-gcc$(EXE_EXT)
CC_AS = $(DEVKITPPC)/bin/powerpc-eabi-gcc$(EXE_EXT)
CXX = $(DEVKITPPC)/bin/powerpc-eabi-g++$(EXE_EXT)
AR = $(DEVKITPPC)/bin/powerpc-eabi-ar$(EXE_EXT)
PLATFORM_DEFINES += -DGEKKO -DHW_DOL -mrvl -mcpu=750 -meabi -mhard-float
STATIC_LINKING = 1
CSRCS += Src/Libretro/libco/ppc.c
else ifeq ($(platform), wii)
TARGET := $(TARGET_NAME)_libretro_wii.a
CC = $(DEVKITPPC)/bin/powerpc-eabi-gcc$(EXE_EXT)
CC_AS = $(DEVKITPPC)/bin/powerpc-eabi-gcc$(EXE_EXT)
CXX = $(DEVKITPPC)/bin/powerpc-eabi-g++$(EXE_EXT)
AR = $(DEVKITPPC)/bin/powerpc-eabi-ar$(EXE_EXT)
PLATFORM_DEFINES += -DGEKKO -DHW_RVL -mrvl -mcpu=750 -meabi -mhard-float
STATIC_LINKING = 1
CSRCS += Src/Libretro/libco/ppc.c
else ifneq (,$(findstring armv,$(platform)))
TARGET := $(TARGET_NAME)_libretro.so
fpic := -fPIC
SHARED := -shared -Wl,-version-script=link.T
CC = gcc
CC_AS = gcc
CXX = g++
ifneq (,$(findstring cortexa8,$(platform)))
PLATFORM_DEFINES += -marm -mcpu=cortex-a8
Expand All @@ -150,6 +173,7 @@ else ifeq ($(platform), emscripten)
else
TARGET := $(TARGET_NAME)_libretro.dll
CC = gcc
CC_AS = gcc
CXX = g++
USE_SSE = 1
SHARED := -shared -static-libgcc -static-libstdc++ -Wl,-no-undefined -Wl,-version-script=link.T
Expand Down Expand Up @@ -184,6 +208,10 @@ CXXFLAGS += -w

CSRCS += libretro.c

ifneq ($(platform), psp1)
CSRCS += Src/Libretro/libco/libco.c
endif

CSRCS += Src/Libretro/blueMSXlite.c
CSRCS += Src/Libretro/Dialog.c
CSRCS += Src/Libretro/Event.c
Expand Down Expand Up @@ -486,7 +514,7 @@ CSRCS += Src/Input/SviJoyIo.c
CSRCS += Src/Input/SviJoystick.c


OBJS := $(CSRCS:.c=.o) $(CXXSRCS:.cpp=.o)
OBJS += $(CSRCS:.c=.o) $(CXXSRCS:.cpp=.o)


INCDIRS := -I.
Expand Down Expand Up @@ -533,6 +561,9 @@ endif
%.o: %.c
$(CC) -c -o $@ $< $(CFLAGS) $(INCDIRS)

%.o: %.S
$(CC_AS) -c -o $@ $< $(CFLAGS) $(INCDIRS)

clean-objs:
rm -f $(OBJS)

Expand Down
17 changes: 7 additions & 10 deletions Src/Libretro/Thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
**
******************************************************************************
*/
#ifdef PSP
#include <pspthreadman.h>
#endif
#include "thread.h"

#ifndef PSP

#include "ArchThread.h"
Expand All @@ -36,7 +41,6 @@
#include <errno.h>
#include <sys/time.h>
#include <stdlib.h>
#include "thread.h"


static void* threadEntry(void* data)
Expand Down Expand Up @@ -105,14 +109,9 @@ void archThreadDestroy(void* thread)
}


void archThreadSleep(int milliseconds)
{
retro_sleep(milliseconds);
}

#else

#include <pspthreadman.h>

typedef void (*entryPoint_type)();

Expand Down Expand Up @@ -151,11 +150,9 @@ void archThreadDestroy(void* thread)
SceUID thread_uid = (SceUID)thread;
sceKernelDeleteThread((SceUID)thread);
}

#endif

void archThreadSleep(int milliseconds)
{
sceKernelDelayThread(1000 * milliseconds);
retro_sleep(milliseconds);
}

#endif
104 changes: 104 additions & 0 deletions Src/Libretro/libco/amd64.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
libco.amd64 (2009-10-12)
author: byuu
license: public domain
*/

#define LIBCO_C
#include "libco.h"
#include <assert.h>
#include <stdlib.h>

#ifdef __cplusplus
extern "C" {
#endif

static thread_local long long co_active_buffer[64];
static thread_local cothread_t co_active_handle = 0;
static void (*co_swap)(cothread_t, cothread_t) = 0;

#ifdef _WIN32
//ABI: Win64
static unsigned char co_swap_function[] = {
0x48, 0x89, 0x22, 0x48, 0x8B, 0x21, 0x58, 0x48, 0x89, 0x6A, 0x08, 0x48, 0x89, 0x72, 0x10, 0x48,
0x89, 0x7A, 0x18, 0x48, 0x89, 0x5A, 0x20, 0x4C, 0x89, 0x62, 0x28, 0x4C, 0x89, 0x6A, 0x30, 0x4C,
0x89, 0x72, 0x38, 0x4C, 0x89, 0x7A, 0x40, 0x48, 0x81, 0xC2, 0x80, 0x00, 0x00, 0x00, 0x48, 0x83,
0xE2, 0xF0, 0x0F, 0x29, 0x32, 0x0F, 0x29, 0x7A, 0x10, 0x44, 0x0F, 0x29, 0x42, 0x20, 0x44, 0x0F,
0x29, 0x4A, 0x30, 0x44, 0x0F, 0x29, 0x52, 0x40, 0x44, 0x0F, 0x29, 0x5A, 0x50, 0x44, 0x0F, 0x29,
0x62, 0x60, 0x44, 0x0F, 0x29, 0x6A, 0x70, 0x44, 0x0F, 0x29, 0xB2, 0x80, 0x00, 0x00, 0x00, 0x44,
0x0F, 0x29, 0xBA, 0x90, 0x00, 0x00, 0x00, 0x48, 0x8B, 0x69, 0x08, 0x48, 0x8B, 0x71, 0x10, 0x48,
0x8B, 0x79, 0x18, 0x48, 0x8B, 0x59, 0x20, 0x4C, 0x8B, 0x61, 0x28, 0x4C, 0x8B, 0x69, 0x30, 0x4C,
0x8B, 0x71, 0x38, 0x4C, 0x8B, 0x79, 0x40, 0x48, 0x81, 0xC1, 0x80, 0x00, 0x00, 0x00, 0x48, 0x83,
0xE1, 0xF0, 0x0F, 0x29, 0x31, 0x0F, 0x29, 0x79, 0x10, 0x44, 0x0F, 0x29, 0x41, 0x20, 0x44, 0x0F,
0x29, 0x49, 0x30, 0x44, 0x0F, 0x29, 0x51, 0x40, 0x44, 0x0F, 0x29, 0x59, 0x50, 0x44, 0x0F, 0x29,
0x61, 0x60, 0x44, 0x0F, 0x29, 0x69, 0x70, 0x44, 0x0F, 0x29, 0xB1, 0x80, 0x00, 0x00, 0x00, 0x44,
0x0F, 0x29, 0xB9, 0x90, 0x00, 0x00, 0x00, 0xFF, 0xE0,
};

#include <windows.h>

void co_init() {
DWORD old_privileges;
VirtualProtect(co_swap_function, sizeof co_swap_function, PAGE_EXECUTE_READWRITE, &old_privileges);
}
#else
//ABI: SystemV
static unsigned char co_swap_function[] = {
0x48, 0x89, 0x26, 0x48, 0x8B, 0x27, 0x58, 0x48, 0x89, 0x6E, 0x08, 0x48, 0x89, 0x5E, 0x10, 0x4C,
0x89, 0x66, 0x18, 0x4C, 0x89, 0x6E, 0x20, 0x4C, 0x89, 0x76, 0x28, 0x4C, 0x89, 0x7E, 0x30, 0x48,
0x8B, 0x6F, 0x08, 0x48, 0x8B, 0x5F, 0x10, 0x4C, 0x8B, 0x67, 0x18, 0x4C, 0x8B, 0x6F, 0x20, 0x4C,
0x8B, 0x77, 0x28, 0x4C, 0x8B, 0x7F, 0x30, 0xFF, 0xE0,
};

#include <unistd.h>
#include <sys/mman.h>

void co_init() {
unsigned long long addr = (unsigned long long)co_swap_function;
unsigned long long base = addr - (addr % sysconf(_SC_PAGESIZE));
unsigned long long size = (addr - base) + sizeof co_swap_function;
mprotect((void*)base, size, PROT_READ | PROT_WRITE | PROT_EXEC);
}
#endif

static void crash() {
assert(0); /* called only if cothread_t entrypoint returns */
}

cothread_t co_active() {
if(!co_active_handle) co_active_handle = &co_active_buffer;
return co_active_handle;
}

cothread_t co_create(unsigned int size, void (*entrypoint)(void)) {
cothread_t handle;
if(!co_swap) {
co_init();
co_swap = (void (*)(cothread_t, cothread_t))co_swap_function;
}
if(!co_active_handle) co_active_handle = &co_active_buffer;
size += 512; /* allocate additional space for storage */
size &= ~15; /* align stack to 16-byte boundary */

if(handle = (cothread_t)malloc(size)) {
long long *p = (long long*)((char*)handle + size); /* seek to top of stack */
*--p = (long long)crash; /* crash if entrypoint returns */
*--p = (long long)entrypoint; /* start of function */
*(long long*)handle = (long long)p; /* stack pointer */
}

return handle;
}

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

void co_switch(cothread_t handle) {
register cothread_t co_previous_handle = co_active_handle;
co_swap(co_active_handle = handle, co_previous_handle);
}

#ifdef __cplusplus
}
#endif
88 changes: 88 additions & 0 deletions Src/Libretro/libco/armeabi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
libco.armeabi (2013-04-05)
author: Themaister
license: public domain
*/

#define LIBCO_C
#include "libco.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#ifndef IOS
#include <malloc.h>
#endif

#ifdef __cplusplus
extern "C" {
#endif

static thread_local uint32_t co_active_buffer[64];
static thread_local cothread_t co_active_handle;

asm (
".arm\n"
".align 4\n"
".globl co_switch_arm\n"
".globl _co_switch_arm\n"
"co_switch_arm:\n"
"_co_switch_arm:\n"
" stmia r1!, {r4, r5, r6, r7, r8, r9, r10, r11, sp, lr}\n"
" ldmia r0!, {r4, r5, r6, r7, r8, r9, r10, r11, sp, pc}\n"
);

// ASM
void co_switch_arm(cothread_t handle, cothread_t current);

static void crash(void) {
assert(0); // Called only if cothread_t entrypoint returns.
}

cothread_t co_create(unsigned int size, void (*entrypoint)(void)) {
size = (size + 1023) & ~1023;
cothread_t handle = 0;
#if HAVE_POSIX_MEMALIGN >= 1
if (posix_memalign(&handle, 1024, size + 256) < 0)
return 0;
#else
handle = memalign(1024, size + 256);
#endif

if (!handle)
return handle;

uint32_t *ptr = (uint32_t*)handle;
// Non-volatiles
ptr[0] = 0; // r4
ptr[1] = 0; // r5
ptr[2] = 0; // r6
ptr[3] = 0; // r7
ptr[4] = 0; // r8
ptr[5] = 0; // r9
ptr[6] = 0; // r10
ptr[7] = 0; // r11
ptr[8] = (uintptr_t)ptr + size + 256 - 4; // r13, stack pointer
ptr[9] = (uintptr_t)entrypoint; // r15, PC (link register r14 gets saved here).
return handle;
}

cothread_t co_active(void) {
if (!co_active_handle)
co_active_handle = co_active_buffer;
return co_active_handle;
}

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

void co_switch(cothread_t handle) {
cothread_t co_previous_handle = co_active();
co_switch_arm(co_active_handle = handle, co_previous_handle);
}

#ifdef __cplusplus
}
#endif