forked from skmp/reicast-emulator
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1 parent
8861888
commit 4c293f3
Showing
7 changed files
with
218 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ | |
#include <string.h> | ||
#include <stdint.h> | ||
|
||
#ifndef IOS | ||
#ifndef __APPLE__ | ||
#include <malloc.h> | ||
#endif | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#define LIBCO_C | ||
#include "libco.h" | ||
|
||
#include <stdlib.h> | ||
#include <stdint.h> | ||
#include <kernel.h> | ||
|
||
/* Since cothread_t is a void pointer it must contain an address. We can't return a reference to a local variable | ||
* because it would go out of scope, so we create a static variable instead so we can return a reference to it. | ||
*/ | ||
static int32_t active_thread_id = -1; | ||
extern void *_gp; | ||
|
||
cothread_t co_active() | ||
{ | ||
active_thread_id = GetThreadId(); | ||
return &active_thread_id; | ||
} | ||
|
||
cothread_t co_create(unsigned int size, void (*entrypoint)(void)) | ||
{ | ||
/* Similar scenario as with active_thread_id except there will only be one active_thread_id while there could be many | ||
* new threads each with their own handle, so we create them on the heap instead and delete them manually when they're | ||
* no longer needed in co_delete(). | ||
*/ | ||
cothread_t handle = malloc(sizeof(cothread_t)); | ||
ee_thread_t thread; | ||
|
||
// u8 threadStack[size/8] __attribute__ ((aligned(16))); | ||
void *threadStack = (void *)malloc(size); | ||
|
||
if ( threadStack== NULL) | ||
{ | ||
printf("libco: ERROR: creating threadStack\n"); | ||
return(-1); | ||
} | ||
|
||
thread.stack_size = size; | ||
thread.gp_reg = &_gp; | ||
thread.func = (void *)entrypoint; | ||
thread.stack = threadStack; | ||
thread.option = 0; | ||
thread.initial_priority = 1; | ||
|
||
int32_t new_thread_id = CreateThread(&thread); | ||
if (new_thread_id < 0) | ||
printf("libco: ERROR: creating thread\n"); | ||
|
||
StartThread(new_thread_id, NULL); | ||
*(uint32_t *)handle = new_thread_id; | ||
return handle; | ||
} | ||
|
||
void co_delete(cothread_t handle) | ||
{ | ||
TerminateThread(*(uint32_t *)handle); | ||
DeleteThread(*(uint32_t *)handle); | ||
free(handle); | ||
} | ||
|
||
void co_switch(cothread_t handle) | ||
{ | ||
WakeupThread(*(uint32_t *)handle); | ||
/* Sleep the currently active thread so the new thread can start */ | ||
SleepThread(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
.globl .co_swap_asm | ||
.globl co_swap_asm | ||
.type .co_swap_asm, @function | ||
.type co_swap_asm, @function | ||
.co_swap_asm: | ||
co_swap_asm: | ||
mfcr 8 | ||
std 1,40(4) | ||
mflr 9 | ||
std 14,72(4) | ||
std 15,80(4) | ||
std 16,88(4) | ||
std 17,96(4) | ||
std 18,104(4) | ||
std 19,112(4) | ||
std 20,120(4) | ||
std 21,128(4) | ||
std 22,136(4) | ||
std 23,144(4) | ||
std 24,152(4) | ||
std 25,160(4) | ||
std 26,168(4) | ||
std 27,176(4) | ||
std 28,184(4) | ||
std 29,192(4) | ||
std 30,200(4) | ||
std 31,208(4) | ||
std 9,32(4) | ||
ld 7,32(3) | ||
ld 1,40(3) | ||
bl swap | ||
trap | ||
swap: stw 8,48(4) | ||
lwz 6,48(3) | ||
mtctr 7 | ||
ld 14,72(3) | ||
ld 15,80(3) | ||
ld 16,88(3) | ||
ld 17,96(3) | ||
ld 18,104(3) | ||
ld 19,112(3) | ||
ld 20,120(3) | ||
ld 21,128(3) | ||
ld 22,136(3) | ||
ld 23,144(3) | ||
ld 24,152(3) | ||
ld 25,160(3) | ||
ld 26,168(3) | ||
ld 27,176(3) | ||
ld 28,184(3) | ||
ld 29,192(3) | ||
ld 30,200(3) | ||
ld 31,208(3) | ||
mtcr 6 | ||
bctr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#define LIBCO_C | ||
#include "libco.h" | ||
|
||
#include <stdlib.h> | ||
#include <pspthreadman.h> | ||
|
||
typedef void (*entrypoint_t)(void); | ||
|
||
cothread_t co_active() | ||
{ | ||
return (void *) sceKernelGetThreadId(); | ||
} | ||
|
||
static int thread_wrap(unsigned int argc, void *argp) | ||
{ | ||
entrypoint_t entrypoint = *(entrypoint_t *) argp; | ||
sceKernelSleepThread(); | ||
entrypoint(); | ||
return 0; | ||
} | ||
|
||
cothread_t co_create(unsigned int size, void (*entrypoint)(void)) | ||
{ | ||
SceUID new_thread_id = sceKernelCreateThread("cothread", thread_wrap, 0x12, size, 0, NULL); | ||
sceKernelStartThread(new_thread_id, sizeof (entrypoint), &entrypoint); | ||
return (void *) new_thread_id; | ||
} | ||
|
||
void co_delete(cothread_t handle) | ||
{ | ||
SceUID id = (SceUID) handle; | ||
sceKernelTerminateDeleteThread(id); | ||
} | ||
|
||
void co_switch(cothread_t handle) | ||
{ | ||
SceUID id = (SceUID) handle; | ||
sceKernelWakeupThread(id); | ||
/* Sleep the currently active thread so the new thread can start */ | ||
sceKernelSleepThread(); | ||
} |