Skip to content

Instantly share code, notes, and snippets.

@yifanlu
Created August 2, 2016 18:57
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yifanlu/43a35324f3b76391cd15c6b96ae8b831 to your computer and use it in GitHub Desktop.
Save yifanlu/43a35324f3b76391cd15c6b96ae8b831 to your computer and use it in GitHub Desktop.
Vita dynarec example
#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