Move main emulation into thread
Jeffrey Pfau jeffrey@endrift.com
Sat, 20 Apr 2013 15:54:09 -0700
4 files changed,
65 insertions(+),
13 deletions(-)
M
src/arm/arm.c
→
src/arm/arm.c
@@ -109,6 +109,8 @@
cpu->executionMode = MODE_THUMB; _ARMSetMode(cpu, MODE_ARM); + ARM_WRITE_PC; + cpu->board->reset(cpu->board); }
A
src/gba/gba-thread.c
@@ -0,0 +1,33 @@
+#include "gba-thread.h" + +#include "arm.h" +#include "debugger.h" +#include "gba.h" + +#include <signal.h> + +static void* _GBAThreadRun(void* context) { + struct ARMDebugger debugger; + struct GBA gba; + struct GBAThread* threadContext = context; + + sigset_t signals; + sigfillset(&signals); + pthread_sigmask(SIG_UNBLOCK, &signals, 0); + + GBAInit(&gba); + threadContext->gba = &gba; + threadContext->debugger = &debugger; + if (threadContext->fd >= 0) { + GBALoadROM(&gba, threadContext->fd); + } + GBAAttachDebugger(&gba, &debugger); + ARMDebuggerRun(&debugger); + GBADeinit(&gba); + + return 0; +} + +int GBAThreadStart(struct GBAThread* threadContext, pthread_t* thread) { + return pthread_create(thread, 0, _GBAThreadRun, threadContext); +}
A
src/gba/gba-thread.h
@@ -0,0 +1,14 @@
+#ifndef GBA_THREAD_H +#define GBA_THREAD_H + +#include <pthread.h> + +struct GBAThread { + struct GBA* gba; + struct ARMDebugger* debugger; + int fd; +}; + +int GBAThreadStart(struct GBAThread* threadContext, pthread_t* thread); + +#endif
M
src/main.c
→
src/main.c
@@ -1,22 +1,25 @@
-#include "arm.h" -#include "debugger.h" -#include "gba.h" +#include "gba-thread.h" + #include <fcntl.h> -#include <sys/stat.h> +#include <errno.h> +#include <signal.h> #include <unistd.h> int main(int argc, char** argv) { - struct ARMDebugger debugger; - struct GBA gba; - GBAInit(&gba); int fd = open("test.rom", O_RDONLY); - GBALoadROM(&gba, fd); - gba.cpu.gprs[ARM_PC] = 0x08000004; - gba.memory.d.setActiveRegion(&gba.memory.d, gba.cpu.gprs[ARM_PC]); - GBAAttachDebugger(&gba, &debugger); - ARMDebuggerRun(&debugger); - GBADeinit(&gba); + + sigset_t signals; + sigaddset(&signals, SIGINT); + sigaddset(&signals, SIGTRAP); + pthread_sigmask(SIG_BLOCK, &signals, 0); + + struct GBAThread context; + context.fd = fd; + pthread_t thread; + GBAThreadStart(&context, &thread); + + pthread_join(thread, 0); close(fd); return 0;