diff --git a/src/main.c b/src/main.c index a3f3725..4356b14 100644 --- a/src/main.c +++ b/src/main.c @@ -1,18 +1,28 @@ #include "debug.h" #include "vm.h" +#ifdef __EMSCRIPTEN__ +#include +#endif /* #define MEMORY_SIZE 65536 /\* 64KB memory (adjustable) *\/ */ #define MEMORY_SIZE 1024 -void run_vm(Data *memory, uint32_t memory_size) { - uint32_t pc = 1; /* Program counter */ - while (pc) { - pc = step_vm(memory, memory_size, pc); +Data memory[MEMORY_SIZE] = {0}; /* Memory array */ +uint32_t pc = 1; /* Program counter */ + +void mainloop() { + pc = step_vm(memory, MEMORY_SIZE, pc); + if (pc == 0) { +#ifdef __EMSCRIPTEN__ + emscripten_cancel_main_loop(); /* this should "kill" the app. */ +#else + core_dump(memory, MEMORY_SIZE); + exit(0); +#endif } } int main() { - Data memory[MEMORY_SIZE] = {0}; /* Memory array */ int i = 1; memory[0].c[0] = 'z'; @@ -61,7 +71,11 @@ int main() { memory[102].f = 5.f; memory[103].f = 5.f; - run_vm(memory, MEMORY_SIZE); - - return core_dump(memory, MEMORY_SIZE); +#ifdef __EMSCRIPTEN__ + emscripten_set_main_loop(mainloop, 0, 1); +#else + while (1) { + mainloop(); + } +#endif }