pvbemu/web/core/wasm.c

79 lines
1.9 KiB
C

#undef VBAPI
#include <stdlib.h>
#include <emscripten/emscripten.h>
#include <vb.h>
/////////////////////////////// Module Commands ///////////////////////////////
// Create and initialize a new simulation
EMSCRIPTEN_KEEPALIVE VB* Create() {
VB *vb = malloc(sizeof (VB));
vbInit(vb);
return vb;
}
// Delete all memory used by a simulation
EMSCRIPTEN_KEEPALIVE void Delete(VB *vb) {
free(vb->cart.ram);
free(vb->cart.rom);
free(vb);
}
// Proxy for free()
EMSCRIPTEN_KEEPALIVE void Free(void *ptr) {
free(ptr);
}
// Proxy for malloc()
EMSCRIPTEN_KEEPALIVE void* Malloc(int size) {
return malloc(size);
}
// Size in bytes of a pointer
EMSCRIPTEN_KEEPALIVE int PointerSize() {
return sizeof (void *);
}
////////////////////////////// Debugger Commands //////////////////////////////
// Execute until the following instruction
uint32_t RunNextAddress;
static int RunNextFetch(VB *vb, int fetch, VBAccess *access) {
return access->address == RunNextAddress;
}
static int RunNextExecute(VB *vb, VBInstruction *inst) {
RunNextAddress = inst->address + inst->size;
vbSetOnExecute(vb, NULL);
vbSetOnFetch(vb, &RunNextFetch);
return 0;
}
EMSCRIPTEN_KEEPALIVE void RunNext(VB **vbs, int count) {
uint32_t clocks = 20000000; // 1s
vbSetOnExecute(vbs[0], &RunNextExecute);
vbEmulateEx (vbs, count, &clocks);
vbSetOnExecute(vbs[0], NULL);
vbSetOnFetch (vbs[0], NULL);
}
// Execute the current instruction
static int SingleStepBreak;
static int SingleStepFetch(VB *vb, int fetch, VBAccess *access) {
if (fetch != 0)
return 0;
if (SingleStepBreak == 1)
return 1;
SingleStepBreak = 1;
return 0;
}
EMSCRIPTEN_KEEPALIVE void SingleStep(VB **vbs, int count) {
uint32_t clocks = 20000000; // 1s
SingleStepBreak = vbs[0]->cpu.stage == 0 ? 0 : 1;
vbSetOnFetch(vbs[0], &SingleStepFetch);
vbEmulateEx (vbs, count, &clocks);
vbSetOnFetch(vbs[0], NULL);
}