From f9120b842fb148d61b0212d5581a6f7144be1828 Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Wed, 7 Jan 2015 02:05:04 -0800 Subject: [PATCH] ARM: Create component hotplugging and use it for attaching and detaching the debugger --- src/arm/arm.c | 19 +++++++++++++++++-- src/arm/arm.h | 2 ++ src/gba/gba-thread.c | 9 ++------- src/gba/gba.c | 4 ++++ src/gba/gba.h | 5 +++++ 5 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/arm/arm.c b/src/arm/arm.c index 17aa099b8..30ce314eb 100644 --- a/src/arm/arm.c +++ b/src/arm/arm.c @@ -73,7 +73,9 @@ void ARMInit(struct ARMCore* cpu) { cpu->master->init(cpu, cpu->master); int i; for (i = 0; i < cpu->numComponents; ++i) { - cpu->components[i]->init(cpu, cpu->components[i]); + if (cpu->components[i] && cpu->components[i]->init) { + cpu->components[i]->init(cpu, cpu->components[i]); + } } } @@ -83,7 +85,7 @@ void ARMDeinit(struct ARMCore* cpu) { } int i; for (i = 0; i < cpu->numComponents; ++i) { - if (cpu->components[i]->deinit) { + if (cpu->components[i] && cpu->components[i]->deinit) { cpu->components[i]->deinit(cpu->components[i]); } } @@ -95,6 +97,19 @@ void ARMSetComponents(struct ARMCore* cpu, struct ARMComponent* master, int extr cpu->components = extras; } +void ARMHotplugAttach(struct ARMCore* cpu, int slot) { + if (slot >= cpu->numComponents) { + return; + } + cpu->components[slot]->init(cpu, cpu->components[slot]); +} + +void ARMHotplugDetach(struct ARMCore* cpu, int slot) { + if (slot >= cpu->numComponents) { + return; + } + cpu->components[slot]->init(cpu, cpu->components[slot]); +} void ARMReset(struct ARMCore* cpu) { int i; diff --git a/src/arm/arm.h b/src/arm/arm.h index 23efd086a..bd4059868 100644 --- a/src/arm/arm.h +++ b/src/arm/arm.h @@ -165,6 +165,8 @@ struct ARMCore { void ARMInit(struct ARMCore* cpu); void ARMDeinit(struct ARMCore* cpu); void ARMSetComponents(struct ARMCore* cpu, struct ARMComponent* master, int extra, struct ARMComponent** extras); +void ARMHotplugAttach(struct ARMCore* cpu, int slot); +void ARMHotplugDetach(struct ARMCore* cpu, int slot); void ARMReset(struct ARMCore* cpu); void ARMSetPrivilegeMode(struct ARMCore*, enum PrivilegeMode); diff --git a/src/gba/gba-thread.c b/src/gba/gba-thread.c index 807edf001..66d30e63c 100644 --- a/src/gba/gba-thread.c +++ b/src/gba/gba-thread.c @@ -108,13 +108,8 @@ static THREAD_ENTRY _GBAThreadRun(void* context) { struct ARMCore cpu; struct Patch patch; struct GBAThread* threadContext = context; - struct ARMComponent* components[1] = {}; - int numComponents = 0; - - if (threadContext->debugger) { - components[numComponents] = &threadContext->debugger->d; - ++numComponents; - } + struct ARMComponent* components[GBA_COMPONENT_MAX] = {}; + int numComponents = GBA_COMPONENT_MAX; #if !defined(_WIN32) && defined(USE_PTHREADS) sigset_t signals; diff --git a/src/gba/gba.c b/src/gba/gba.c index 14eb83f2f..2ad2a57e6 100644 --- a/src/gba/gba.c +++ b/src/gba/gba.c @@ -434,10 +434,14 @@ static int32_t GBATimersProcessEvents(struct GBA* gba, int32_t cycles) { void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger) { gba->debugger = debugger; + gba->cpu->components[GBA_COMPONENT_DEBUGGER] = &debugger->d; + ARMHotplugAttach(gba->cpu, GBA_COMPONENT_DEBUGGER); } void GBADetachDebugger(struct GBA* gba) { gba->debugger = 0; + ARMHotplugDetach(gba->cpu, GBA_COMPONENT_DEBUGGER); + gba->cpu->components[GBA_COMPONENT_DEBUGGER] = 0; } void GBALoadROM(struct GBA* gba, struct VFile* vf, struct VFile* sav, const char* fname) { diff --git a/src/gba/gba.h b/src/gba/gba.h index e653021f9..78385071d 100644 --- a/src/gba/gba.h +++ b/src/gba/gba.h @@ -75,6 +75,11 @@ enum GBAKey { GBA_KEY_NONE = -1 }; +enum GBAComponent { + GBA_COMPONENT_DEBUGGER, + GBA_COMPONENT_MAX +}; + struct GBA; struct GBARotationSource; struct Patch;