gbemu/lib/emu.c

94 lines
1.5 KiB
C
Raw Normal View History

2025-01-30 14:30:19 -07:00
#include <emu.h>
#include <stdio.h>
#include <cart.h>
#include <cpu.h>
#include <ui.h>
#ifdef _WIN32
#include <windows.h>
#endif
#ifdef _UNIX
#include <pthread.h>
#include <unistd.h>
#endif
2025-01-30 14:30:19 -07:00
static emu_context ctx;
emu_context *emu_get_context() {
return &ctx;
}
DWORD WINAPI cpu_run(void *p) {
2025-01-30 14:30:19 -07:00
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;
2025-01-30 16:27:27 -07:00
}
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 <rom_file>\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;
}
2025-01-30 16:27:27 -07:00
void emu_cycles(int cpu_cycles) {
//TODO
2025-01-30 14:30:19 -07:00
}