From cb48145ea3911a438d3bc214e0622341ccbb9f15 Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Sat, 20 Apr 2013 15:54:09 -0700 Subject: [PATCH] Move main emulation into thread --- src/arm/arm.c | 2 ++ src/gba/gba-thread.c | 33 +++++++++++++++++++++++++++++++++ src/gba/gba-thread.h | 14 ++++++++++++++ src/main.c | 29 ++++++++++++++++------------- 4 files changed, 65 insertions(+), 13 deletions(-) create mode 100644 src/gba/gba-thread.c create mode 100644 src/gba/gba-thread.h diff --git a/src/arm/arm.c b/src/arm/arm.c index a2f0bdea2..645e98505 100644 --- a/src/arm/arm.c +++ b/src/arm/arm.c @@ -109,6 +109,8 @@ void ARMReset(struct ARMCore* cpu) { cpu->executionMode = MODE_THUMB; _ARMSetMode(cpu, MODE_ARM); + ARM_WRITE_PC; + cpu->board->reset(cpu->board); } diff --git a/src/gba/gba-thread.c b/src/gba/gba-thread.c new file mode 100644 index 000000000..3f22fbfcc --- /dev/null +++ b/src/gba/gba-thread.c @@ -0,0 +1,33 @@ +#include "gba-thread.h" + +#include "arm.h" +#include "debugger.h" +#include "gba.h" + +#include + +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); +} diff --git a/src/gba/gba-thread.h b/src/gba/gba-thread.h new file mode 100644 index 000000000..943376995 --- /dev/null +++ b/src/gba/gba-thread.h @@ -0,0 +1,14 @@ +#ifndef GBA_THREAD_H +#define GBA_THREAD_H + +#include + +struct GBAThread { + struct GBA* gba; + struct ARMDebugger* debugger; + int fd; +}; + +int GBAThreadStart(struct GBAThread* threadContext, pthread_t* thread); + +#endif diff --git a/src/main.c b/src/main.c index 4d2beae39..0bd115430 100644 --- a/src/main.c +++ b/src/main.c @@ -1,22 +1,25 @@ -#include "arm.h" -#include "debugger.h" -#include "gba.h" +#include "gba-thread.h" + #include -#include +#include +#include #include 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;