#include #include #include #include #include #ifdef _WIN32 #include #endif #ifdef _UNIX #include #include #endif static emu_context ctx; emu_context *emu_get_context() { return &ctx; } DWORD WINAPI cpu_run(void *p) { cpu_init(); ctx.running = true; ctx.paused = false; ctx.ticks = 0; while (ctx.running) { if (ctx.paused) { delay(10); continue; } if (!cpu_step()) { printf("CPU stopped\n"); return -3; } ctx.ticks++; } return 0; } void sleep_ms(int milis) { #ifdef _WIN32 Sleep(milis); #endif #ifdef _UNIX usleep(milis * 1000); #endif } int emu_run(int argc, char **argv) { if (argc < 2) { printf("Usage: gbemu \n"); return -1; } if(!cart_load(argv[1])) { printf("Failed to load ROM file: %s\n", argv[1]); return -2; } printf("Cart loaded..\n"); ui_init(); #ifdef _WIN32 HANDLE thread = CreateThread(NULL, 0, cpu_run, NULL, 0, NULL); if(!thread) { fprintf(stderr, "Unable to create main CPU thread!\n"); return -1; } #endif #ifdef _UNIX pthread_t t1; if(pthread_create(&t1, NULL, cpu_run, NULL)) { fprintf(stderr, "Unable to create main CPU thread!\n"); return -1; } #endif while(!ctx.die) { sleep_ms(1); ui_handle_events(); } return 0; } void emu_cycles(int cpu_cycles) { //TODO }