Skip to content

Instantly share code, notes, and snippets.

@yifanlu yifanlu/dynarec.c
Created Aug 2, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.