Created
August 2, 2016 18:57
-
-
Save yifanlu/43a35324f3b76391cd15c6b96ae8b831 to your computer and use it in GitHub Desktop.
Vita dynarec example
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
#include <psp2/kernel/sysmem.h> | |
int dynarec_test(void) | |
{ | |
int block; | |
int ret; | |
void *base; | |
int (*func)(void); | |
// allocate block in VM domain | |
block = sceKernelAllocMemBlockForVM("code", 0x100000); | |
if (block < 0) | |
{ | |
return block; | |
} | |
// get base address | |
ret = sceKernelGetMemBlockBase(block, &base); | |
if (ret < 0) | |
{ | |
return ret; | |
} | |
// set domain to be writable by user | |
ret = sceKernelOpenVMDomain(); | |
if (ret < 0) | |
{ | |
return ret; | |
} | |
// write thumb code: MOVS R0, #5, BX LR | |
*(uint32_t *)base = 0x47702005; | |
// set domain back to read-only | |
ret = sceKernelCloseVMDomain(); | |
if (ret < 0) | |
{ | |
return ret; | |
} | |
// flush icache | |
ret = sceKernelSyncVMDomain(block, base, 0x1000); | |
if (ret < 0) | |
{ | |
return ret; | |
} | |
// call function | |
func = (char*)base + 1; | |
ret = func(); // should return 5 | |
// release block when we're done | |
sceKernelFreeMemBlock(block); | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment