From fbfbbbf279ffc1e270a1acc8259fc280ec8141de Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Sun, 7 Feb 2016 16:16:03 -0800 Subject: [PATCH] Core: Merge ARMComponent and LR35902Component --- src/arm/arm.c | 2 +- src/arm/arm.h | 14 +++++--------- src/core/cpu.h | 17 +++++++++++++++++ src/debugger/debugger.c | 10 +++++----- src/debugger/debugger.h | 2 +- src/gb/gb.c | 6 +++--- src/gb/gb.h | 2 +- src/gba/cheats.c | 10 +++++----- src/gba/cheats.h | 2 +- src/gba/core.c | 2 +- src/gba/gba.c | 8 ++++---- src/gba/gba.h | 4 ++-- src/gba/supervisor/thread.c | 2 +- src/lr35902/lr35902.c | 2 +- src/lr35902/lr35902.h | 14 ++++---------- 15 files changed, 52 insertions(+), 45 deletions(-) create mode 100644 src/core/cpu.h diff --git a/src/arm/arm.c b/src/arm/arm.c index 6f9ca2990..9fc35de71 100644 --- a/src/arm/arm.c +++ b/src/arm/arm.c @@ -90,7 +90,7 @@ void ARMDeinit(struct ARMCore* cpu) { } } -void ARMSetComponents(struct ARMCore* cpu, struct ARMComponent* master, int extra, struct ARMComponent** extras) { +void ARMSetComponents(struct ARMCore* cpu, struct mCPUComponent* master, int extra, struct mCPUComponent** extras) { cpu->master = master; cpu->numComponents = extra; cpu->components = extras; diff --git a/src/arm/arm.h b/src/arm/arm.h index 4a9fdba6a..72231f24c 100644 --- a/src/arm/arm.h +++ b/src/arm/arm.h @@ -8,6 +8,8 @@ #include "util/common.h" +#include "core/cpu.h" + enum { ARM_SP = 13, ARM_LR = 14, @@ -129,12 +131,6 @@ struct ARMInterruptHandler { void (*hitStub)(struct ARMCore* cpu, uint32_t opcode); }; -struct ARMComponent { - uint32_t id; - void (*init)(struct ARMCore* cpu, struct ARMComponent* component); - void (*deinit)(struct ARMComponent* component); -}; - struct ARMCore { int32_t gprs[16]; union PSR cpsr; @@ -157,15 +153,15 @@ struct ARMCore { struct ARMMemory memory; struct ARMInterruptHandler irqh; - struct ARMComponent* master; + struct mCPUComponent* master; size_t numComponents; - struct ARMComponent** components; + struct mCPUComponent** components; }; void ARMInit(struct ARMCore* cpu); void ARMDeinit(struct ARMCore* cpu); -void ARMSetComponents(struct ARMCore* cpu, struct ARMComponent* master, int extra, struct ARMComponent** extras); +void ARMSetComponents(struct ARMCore* cpu, struct mCPUComponent* master, int extra, struct mCPUComponent** extras); void ARMHotplugAttach(struct ARMCore* cpu, size_t slot); void ARMHotplugDetach(struct ARMCore* cpu, size_t slot); diff --git a/src/core/cpu.h b/src/core/cpu.h new file mode 100644 index 000000000..40e62b045 --- /dev/null +++ b/src/core/cpu.h @@ -0,0 +1,17 @@ +/* Copyright (c) 2013-2016 Jeffrey Pfau + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#ifndef M_CPU_H +#define M_CPU_H + +#include "util/common.h" + +struct mCPUComponent { + uint32_t id; + void (*init)(void* cpu, struct mCPUComponent* component); + void (*deinit)(struct mCPUComponent* component); +}; + +#endif diff --git a/src/debugger/debugger.c b/src/debugger/debugger.c index cccfaf3b9..be2d7109b 100644 --- a/src/debugger/debugger.c +++ b/src/debugger/debugger.c @@ -43,8 +43,8 @@ static void _checkBreakpoints(struct ARMDebugger* debugger) { ARMDebuggerEnter(debugger, DEBUGGER_ENTER_BREAKPOINT, &info); } -static void ARMDebuggerInit(struct ARMCore*, struct ARMComponent*); -static void ARMDebuggerDeinit(struct ARMComponent*); +static void ARMDebuggerInit(void* cpu, struct mCPUComponent*); +static void ARMDebuggerDeinit(struct mCPUComponent*); void ARMDebuggerCreate(struct ARMDebugger* debugger) { debugger->d.id = ARM_DEBUGGER_ID; @@ -52,11 +52,11 @@ void ARMDebuggerCreate(struct ARMDebugger* debugger) { debugger->d.deinit = ARMDebuggerDeinit; } -void ARMDebuggerInit(struct ARMCore* cpu, struct ARMComponent* component) { +void ARMDebuggerInit(void* cpu, struct mCPUComponent* component) { struct ARMDebugger* debugger = (struct ARMDebugger*) component; debugger->cpu = cpu; debugger->state = DEBUGGER_RUNNING; - debugger->originalMemory = cpu->memory; + debugger->originalMemory = debugger->cpu->memory; debugger->currentBreakpoint = 0; DebugBreakpointListInit(&debugger->breakpoints, 0); DebugBreakpointListInit(&debugger->swBreakpoints, 0); @@ -66,7 +66,7 @@ void ARMDebuggerInit(struct ARMCore* cpu, struct ARMComponent* component) { } } -void ARMDebuggerDeinit(struct ARMComponent* component) { +void ARMDebuggerDeinit(struct mCPUComponent* component) { struct ARMDebugger* debugger = (struct ARMDebugger*) component; debugger->deinit(debugger); DebugBreakpointListDeinit(&debugger->breakpoints); diff --git a/src/debugger/debugger.h b/src/debugger/debugger.h index 7f54b4f7c..372caef2c 100644 --- a/src/debugger/debugger.h +++ b/src/debugger/debugger.h @@ -75,7 +75,7 @@ enum DebuggerLogLevel { }; struct ARMDebugger { - struct ARMComponent d; + struct mCPUComponent d; enum DebuggerState state; struct ARMCore* cpu; diff --git a/src/gb/gb.c b/src/gb/gb.c index 25b091a42..82cb03bfe 100644 --- a/src/gb/gb.c +++ b/src/gb/gb.c @@ -21,7 +21,7 @@ const uint32_t GB_COMPONENT_MAGIC = 0x400000; mLOG_DEFINE_CATEGORY(GB, "GB"); -static void GBInit(struct LR35902Core* cpu, struct LR35902Component* component); +static void GBInit(void* cpu, struct mCPUComponent* component); static void GBInterruptHandlerInit(struct LR35902InterruptHandler* irqh); static void GBProcessEvents(struct LR35902Core* cpu); static void GBSetInterrupts(struct LR35902Core* cpu, bool enable); @@ -34,11 +34,11 @@ void GBCreate(struct GB* gb) { gb->d.deinit = 0; } -static void GBInit(struct LR35902Core* cpu, struct LR35902Component* component) { +static void GBInit(void* cpu, struct mCPUComponent* component) { struct GB* gb = (struct GB*) component; gb->cpu = cpu; - GBInterruptHandlerInit(&cpu->irqh); + GBInterruptHandlerInit(&gb->cpu->irqh); GBMemoryInit(gb); gb->video.p = gb; diff --git a/src/gb/gb.h b/src/gb/gb.h index 229ef26a5..672a8dfc2 100644 --- a/src/gb/gb.h +++ b/src/gb/gb.h @@ -42,7 +42,7 @@ enum GBIRQVector { struct mCoreSync; struct GB { - struct LR35902Component d; + struct mCPUComponent d; struct LR35902Core* cpu; struct GBMemory memory; diff --git a/src/gba/cheats.c b/src/gba/cheats.c index 097679bc5..1bd4ebb88 100644 --- a/src/gba/cheats.c +++ b/src/gba/cheats.c @@ -99,8 +99,8 @@ static void _unpatchROM(struct GBACheatDevice* device, struct GBACheatSet* cheat } } -static void GBACheatDeviceInit(struct ARMCore*, struct ARMComponent*); -static void GBACheatDeviceDeinit(struct ARMComponent*); +static void GBACheatDeviceInit(void*, struct mCPUComponent*); +static void GBACheatDeviceDeinit(struct mCPUComponent*); void GBACheatDeviceCreate(struct GBACheatDevice* device) { device->d.id = GBA_CHEAT_DEVICE_ID; @@ -557,9 +557,9 @@ void GBACheatSetCopyProperties(struct GBACheatSet* newSet, struct GBACheatSet* s } } -void GBACheatDeviceInit(struct ARMCore* cpu, struct ARMComponent* component) { +void GBACheatDeviceInit(void* cpu, struct mCPUComponent* component) { struct GBACheatDevice* device = (struct GBACheatDevice*) component; - device->p = (struct GBA*) cpu->master; + device->p = (struct GBA*) ((struct ARMCore*) cpu)->master; size_t i; for (i = 0; i < GBACheatSetsSize(&device->cheats); ++i) { struct GBACheatSet* cheats = *GBACheatSetsGetPointer(&device->cheats, i); @@ -568,7 +568,7 @@ void GBACheatDeviceInit(struct ARMCore* cpu, struct ARMComponent* component) { } } -void GBACheatDeviceDeinit(struct ARMComponent* component) { +void GBACheatDeviceDeinit(struct mCPUComponent* component) { struct GBACheatDevice* device = (struct GBACheatDevice*) component; size_t i; for (i = GBACheatSetsSize(&device->cheats); i--;) { diff --git a/src/gba/cheats.h b/src/gba/cheats.h index 2bf992ff2..e43fb6f74 100644 --- a/src/gba/cheats.h +++ b/src/gba/cheats.h @@ -182,7 +182,7 @@ struct GBACheatSet { DECLARE_VECTOR(GBACheatSets, struct GBACheatSet*); struct GBACheatDevice { - struct ARMComponent d; + struct mCPUComponent d; struct GBA* p; struct GBACheatSets cheats; diff --git a/src/gba/core.c b/src/gba/core.c index 77a039d6d..aed881f5a 100644 --- a/src/gba/core.c +++ b/src/gba/core.c @@ -19,7 +19,7 @@ struct GBACore { struct mCore d; struct GBAVideoSoftwareRenderer renderer; int keys; - struct ARMComponent* components[GBA_COMPONENT_MAX]; + struct mCPUComponent* components[GBA_COMPONENT_MAX]; }; static bool _GBACoreInit(struct mCore* core) { diff --git a/src/gba/gba.c b/src/gba/gba.c index c005ca569..5ac435333 100644 --- a/src/gba/gba.c +++ b/src/gba/gba.c @@ -32,7 +32,7 @@ static const uint8_t GBA_ROM_MAGIC[] = { 0xEA }; static const size_t GBA_MB_MAGIC_OFFSET = 0xC0; -static void GBAInit(struct ARMCore* cpu, struct ARMComponent* component); +static void GBAInit(void* cpu, struct mCPUComponent* component); static void GBAInterruptHandlerInit(struct ARMInterruptHandler* irqh); static void GBAProcessEvents(struct ARMCore* cpu); static int32_t GBATimersProcessEvents(struct GBA* gba, int32_t cycles); @@ -55,13 +55,13 @@ void GBACreate(struct GBA* gba) { gba->d.deinit = 0; } -static void GBAInit(struct ARMCore* cpu, struct ARMComponent* component) { +static void GBAInit(void* cpu, struct mCPUComponent* component) { struct GBA* gba = (struct GBA*) component; gba->cpu = cpu; gba->debugger = 0; gba->sync = 0; - GBAInterruptHandlerInit(&cpu->irqh); + GBAInterruptHandlerInit(&gba->cpu->irqh); GBAMemoryInit(gba); GBASavedataInit(&gba->memory.savedata, 0); @@ -936,7 +936,7 @@ void GBAFrameEnded(struct GBA* gba) { } } -void GBASetBreakpoint(struct GBA* gba, struct ARMComponent* component, uint32_t address, enum ExecutionMode mode, uint32_t* opcode) { +void GBASetBreakpoint(struct GBA* gba, struct mCPUComponent* component, uint32_t address, enum ExecutionMode mode, uint32_t* opcode) { size_t immediate; for (immediate = 0; immediate < gba->cpu->numComponents; ++immediate) { if (gba->cpu->components[immediate] == component) { diff --git a/src/gba/gba.h b/src/gba/gba.h index 3786ea027..744aa3566 100644 --- a/src/gba/gba.h +++ b/src/gba/gba.h @@ -78,7 +78,7 @@ struct GBATimer { }; struct GBA { - struct ARMComponent d; + struct mCPUComponent d; struct ARMCore* cpu; struct GBAMemory memory; @@ -169,7 +169,7 @@ void GBAStop(struct GBA* gba); void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger); void GBADetachDebugger(struct GBA* gba); -void GBASetBreakpoint(struct GBA* gba, struct ARMComponent* component, uint32_t address, enum ExecutionMode mode, +void GBASetBreakpoint(struct GBA* gba, struct mCPUComponent* component, uint32_t address, enum ExecutionMode mode, uint32_t* opcode); void GBAClearBreakpoint(struct GBA* gba, uint32_t address, enum ExecutionMode mode, uint32_t opcode); diff --git a/src/gba/supervisor/thread.c b/src/gba/supervisor/thread.c index 216824a92..af155df35 100644 --- a/src/gba/supervisor/thread.c +++ b/src/gba/supervisor/thread.c @@ -151,7 +151,7 @@ static THREAD_ENTRY _GBAThreadRun(void* context) { struct Patch patch; struct GBACheatDevice cheatDevice; struct GBAThread* threadContext = context; - struct ARMComponent* components[GBA_COMPONENT_MAX] = { 0 }; + struct mCPUComponent* components[GBA_COMPONENT_MAX] = { 0 }; struct GBARRContext* movie = 0; int numComponents = GBA_COMPONENT_MAX; diff --git a/src/lr35902/lr35902.c b/src/lr35902/lr35902.c index a9a0c7300..f40ab6462 100644 --- a/src/lr35902/lr35902.c +++ b/src/lr35902/lr35902.c @@ -29,7 +29,7 @@ void LR35902Deinit(struct LR35902Core* cpu) { } } -void LR35902SetComponents(struct LR35902Core* cpu, struct LR35902Component* master, int extra, struct LR35902Component** extras) { +void LR35902SetComponents(struct LR35902Core* cpu, struct mCPUComponent* master, int extra, struct mCPUComponent** extras) { cpu->master = master; cpu->numComponents = extra; cpu->components = extras; diff --git a/src/lr35902/lr35902.h b/src/lr35902/lr35902.h index 4cbfdc251..240195c9d 100644 --- a/src/lr35902/lr35902.h +++ b/src/lr35902/lr35902.h @@ -8,6 +8,7 @@ #include "util/common.h" +#include "core/cpu.h" #include "lr35902/isa-lr35902.h" struct LR35902Core; @@ -67,13 +68,6 @@ struct LR35902InterruptHandler { void (*hitStub)(struct LR35902Core* cpu); }; -// TODO: Merge with ARMComponent? -struct LR35902Component { - uint32_t id; - void (*init)(struct LR35902Core* cpu, struct LR35902Component* component); - void (*deinit)(struct LR35902Component* component); -}; - struct LR35902Core { #pragma pack(push, 1) union { @@ -125,10 +119,10 @@ struct LR35902Core { struct LR35902Memory memory; struct LR35902InterruptHandler irqh; - struct LR35902Component* master; + struct mCPUComponent* master; size_t numComponents; - struct LR35902Component** components; + struct mCPUComponent** components; }; static inline uint16_t LR35902ReadHL(struct LR35902Core* cpu) { @@ -163,7 +157,7 @@ static inline void LR35902WriteDE(struct LR35902Core* cpu, uint16_t de) { void LR35902Init(struct LR35902Core* cpu); void LR35902Deinit(struct LR35902Core* cpu); -void LR35902SetComponents(struct LR35902Core* cpu, struct LR35902Component* master, int extra, struct LR35902Component** extras); +void LR35902SetComponents(struct LR35902Core* cpu, struct mCPUComponent* master, int extra, struct mCPUComponent** extras); void LR35902HotplugAttach(struct LR35902Core* cpu, size_t slot); void LR35902HotplugDetach(struct LR35902Core* cpu, size_t slot);