all repos — mgba @ 7a4ca414e5c7f5fc0c28739b1008f0096528aa70

mGBA Game Boy Advance Emulator

Restructure watchpoints
Jeffrey Pfau jeffrey@endrift.com
Sun, 20 Apr 2014 00:47:49 -0700
commit

7a4ca414e5c7f5fc0c28739b1008f0096528aa70

parent

0f68dbc832db31915d31a6e13438e566083dfb42

M CMakeLists.txtCMakeLists.txt

@@ -36,7 +36,7 @@ add_definitions(-DCOLOR_16_BIT -DCOLOR_5_6_5)

endif() endif() -set(DEBUGGER_SRC "${CMAKE_SOURCE_DIR}/src/debugger/debugger.c") +set(DEBUGGER_SRC "${CMAKE_SOURCE_DIR}/src/debugger/debugger.c;${CMAKE_SOURCE_DIR}/src/debugger/memory-debugger.c") if(USE_CLI_DEBUGGER) set(DEBUGGER_SRC "${DEBUGGER_SRC};${CMAKE_SOURCE_DIR}/src/debugger/cli-debugger.c")
M src/arm/arm.hsrc/arm/arm.h

@@ -111,7 +111,7 @@ void (*hitStub)(struct ARMCore* cpu, uint32_t opcode);

}; struct ARMComponent { - long id; + uint32_t id; void (*init)(struct ARMCore* cpu, struct ARMComponent* component); void (*deinit)(struct ARMComponent* component); };
M src/debugger/cli-debugger.csrc/debugger/cli-debugger.c

@@ -221,8 +221,8 @@ if (!dv || dv->type != INT_TYPE) {

printf("%s\n", ERROR_MISSING_ARGS); return; } - // TODO: Redo watchpoints - (void) (debugger); + uint32_t address = dv->intValue; + ARMDebuggerSetWatchpoint(&debugger->d, address); } static void _breakIntoDefault(int signal) {
M src/debugger/debugger.csrc/debugger/debugger.c

@@ -4,6 +4,8 @@ #include "arm.h"

#include "memory-debugger.h" +const uint32_t ARM_DEBUGGER_ID = 0xDEADBEEF; + static void _checkBreakpoints(struct ARMDebugger* debugger) { struct DebugBreakpoint* breakpoint; int instructionLength;

@@ -25,6 +27,7 @@ static void ARMDebuggerInit(struct ARMCore*, struct ARMComponent*);

static void ARMDebuggerDeinit(struct ARMComponent*); void ARMDebuggerCreate(struct ARMDebugger* debugger) { + debugger->d.id = ARM_DEBUGGER_ID; debugger->d.init = ARMDebuggerInit; debugger->d.deinit = ARMDebuggerDeinit; }

@@ -34,9 +37,8 @@ struct ARMDebugger* debugger = (struct ARMDebugger*) component;

debugger->cpu = cpu; debugger->state = DEBUGGER_RUNNING; debugger->breakpoints = 0; - debugger->memoryShim.original = cpu->memory; - debugger->memoryShim.p = debugger; - debugger->memoryShim.watchpoints = 0; + debugger->originalMemory = cpu->memory; + debugger->watchpoints = 0; if (debugger->init) { debugger->init(debugger); }

@@ -105,9 +107,11 @@ }

} void ARMDebuggerSetWatchpoint(struct ARMDebugger* debugger, uint32_t address) { - // FIXME: Make watchpoints work again + if (!debugger->watchpoints) { + ARMDebuggerInstallMemoryShim(debugger); + } struct DebugBreakpoint* watchpoint = malloc(sizeof(struct DebugBreakpoint)); watchpoint->address = address; - watchpoint->next = debugger->memoryShim.watchpoints; - debugger->memoryShim.watchpoints = watchpoint; + watchpoint->next = debugger->watchpoints; + debugger->watchpoints = watchpoint; }
M src/debugger/debugger.hsrc/debugger/debugger.h

@@ -5,6 +5,8 @@ #include "common.h"

#include "arm.h" +const uint32_t ARM_DEBUGGER_ID; + enum DebuggerState { DEBUGGER_PAUSED, DEBUGGER_RUNNING,

@@ -15,13 +17,6 @@

struct DebugBreakpoint { struct DebugBreakpoint* next; uint32_t address; -}; - -struct DebugMemoryShim { - struct ARMMemory original; - - struct ARMDebugger* p; - struct DebugBreakpoint* watchpoints; }; enum DebuggerEntryReason {

@@ -45,7 +40,8 @@ enum DebuggerState state;

struct ARMCore* cpu; struct DebugBreakpoint* breakpoints; - struct DebugMemoryShim memoryShim; + struct DebugBreakpoint* watchpoints; + struct ARMMemory originalMemory; void (*init)(struct ARMDebugger*); void (*deinit)(struct ARMDebugger*);
M src/debugger/memory-debugger.csrc/debugger/memory-debugger.c

@@ -4,23 +4,36 @@ #include "debugger.h"

#include <string.h> -static void ARMDebuggerShim_store32(struct ARMMemory*, uint32_t address, int32_t value, int* cycleCounter); -static void ARMDebuggerShim_store16(struct ARMMemory*, uint32_t address, int16_t value, int* cycleCounter); -static void ARMDebuggerShim_store8(struct ARMMemory*, uint32_t address, int8_t value, int* cycleCounter); -static void ARMDebuggerShim_setActiveRegion(struct ARMMemory* memory, uint32_t address); +static void ARMDebuggerShim_store32(struct ARMCore*, uint32_t address, int32_t value, int* cycleCounter); +static void ARMDebuggerShim_store16(struct ARMCore*, uint32_t address, int16_t value, int* cycleCounter); +static void ARMDebuggerShim_store8(struct ARMCore*, uint32_t address, int8_t value, int* cycleCounter); +static void ARMDebuggerShim_setActiveRegion(struct ARMCore* cpu, uint32_t address); + +#define FIND_DEBUGGER(DEBUGGER, CPU) \ + { \ + DEBUGGER = 0; \ + int i; \ + for (i = 0; i < CPU->numComponents; ++i) { \ + if (CPU->components[i]->id == ARM_DEBUGGER_ID) { \ + DEBUGGER = (struct ARMDebugger*) cpu->components[i]; \ + break; \ + } \ + } \ + } #define CREATE_SHIM(NAME, RETURN, TYPES, ARGS...) \ static RETURN ARMDebuggerShim_ ## NAME TYPES { \ - struct DebugMemoryShim* debugMemory = (struct DebugMemoryShim*) memory; \ - return debugMemory->original->NAME(debugMemory->original, ARGS); \ + struct ARMDebugger* debugger; \ + FIND_DEBUGGER(debugger, cpu); \ + return debugger->originalMemory.NAME(cpu, ARGS); \ } -CREATE_SHIM(load32, int32_t, (struct ARMMemory* memory, uint32_t address, int* cycleCounter), address, cycleCounter) -CREATE_SHIM(load16, int16_t, (struct ARMMemory* memory, uint32_t address, int* cycleCounter), address, cycleCounter) -CREATE_SHIM(loadU16, uint16_t, (struct ARMMemory* memory, uint32_t address, int* cycleCounter), address, cycleCounter) -CREATE_SHIM(load8, int8_t, (struct ARMMemory* memory, uint32_t address, int* cycleCounter), address, cycleCounter) -CREATE_SHIM(loadU8, uint8_t, (struct ARMMemory* memory, uint32_t address, int* cycleCounter), address, cycleCounter) -CREATE_SHIM(waitMultiple, int, (struct ARMMemory* memory, uint32_t startAddress, int count), startAddress, count) +CREATE_SHIM(load32, int32_t, (struct ARMCore* cpu, uint32_t address, int* cycleCounter), address, cycleCounter) +CREATE_SHIM(load16, int16_t, (struct ARMCore* cpu, uint32_t address, int* cycleCounter), address, cycleCounter) +CREATE_SHIM(loadU16, uint16_t, (struct ARMCore* cpu, uint32_t address, int* cycleCounter), address, cycleCounter) +CREATE_SHIM(load8, int8_t, (struct ARMCore* cpu, uint32_t address, int* cycleCounter), address, cycleCounter) +CREATE_SHIM(loadU8, uint8_t, (struct ARMCore* cpu, uint32_t address, int* cycleCounter), address, cycleCounter) +CREATE_SHIM(waitMultiple, int, (struct ARMCore* cpu, uint32_t startAddress, int count), startAddress, count) static int _checkWatchpoints(struct DebugBreakpoint* watchpoints, uint32_t address, int width) { width -= 1;

@@ -33,52 +46,48 @@ return 0;

} void ARMDebuggerInstallMemoryShim(struct ARMDebugger* debugger) { - debugger->memoryShim.original = debugger->cpu->memory; - memcpy(&debugger->memoryShim.d, debugger->cpu->memory, sizeof(struct ARMMemory)); - debugger->memoryShim.d.store32 = ARMDebuggerShim_store32; - debugger->memoryShim.d.store16 = ARMDebuggerShim_store16; - debugger->memoryShim.d.store8 = ARMDebuggerShim_store8; - debugger->memoryShim.d.load32 = ARMDebuggerShim_load32; - debugger->memoryShim.d.load16 = ARMDebuggerShim_load16; - debugger->memoryShim.d.loadU16 = ARMDebuggerShim_loadU16; - debugger->memoryShim.d.load8 = ARMDebuggerShim_load8; - debugger->memoryShim.d.loadU8 = ARMDebuggerShim_loadU8; - debugger->memoryShim.d.setActiveRegion = ARMDebuggerShim_setActiveRegion; - debugger->memoryShim.d.waitMultiple = ARMDebuggerShim_waitMultiple; - debugger->cpu->memory = &debugger->memoryShim.d; + debugger->originalMemory = debugger->cpu->memory; + debugger->cpu->memory.store32 = ARMDebuggerShim_store32; + debugger->cpu->memory.store16 = ARMDebuggerShim_store16; + debugger->cpu->memory.store8 = ARMDebuggerShim_store8; + debugger->cpu->memory.load32 = ARMDebuggerShim_load32; + debugger->cpu->memory.load16 = ARMDebuggerShim_load16; + debugger->cpu->memory.loadU16 = ARMDebuggerShim_loadU16; + debugger->cpu->memory.load8 = ARMDebuggerShim_load8; + debugger->cpu->memory.loadU8 = ARMDebuggerShim_loadU8; + debugger->cpu->memory.setActiveRegion = ARMDebuggerShim_setActiveRegion; + debugger->cpu->memory.waitMultiple = ARMDebuggerShim_waitMultiple; } -void ARMDebuggerShim_store32(struct ARMMemory* memory, uint32_t address, int32_t value, int* cycleCounter) { - struct DebugMemoryShim* debugMemory = (struct DebugMemoryShim*) memory; - if (_checkWatchpoints(debugMemory->watchpoints, address, 4)) { - ARMDebuggerEnter(debugMemory->p, DEBUGGER_ENTER_WATCHPOINT); +void ARMDebuggerShim_store32(struct ARMCore* cpu, uint32_t address, int32_t value, int* cycleCounter) { + struct ARMDebugger* debugger; + FIND_DEBUGGER(debugger, cpu); + if (_checkWatchpoints(debugger->watchpoints, address, 4)) { + ARMDebuggerEnter(debugger, DEBUGGER_ENTER_WATCHPOINT); } - debugMemory->original->store32(debugMemory->original, address, value, cycleCounter); + debugger->originalMemory.store32(debugger->cpu, address, value, cycleCounter); } -void ARMDebuggerShim_store16(struct ARMMemory* memory, uint32_t address, int16_t value, int* cycleCounter) { - struct DebugMemoryShim* debugMemory = (struct DebugMemoryShim*) memory; - if (_checkWatchpoints(debugMemory->watchpoints, address, 2)) { - ARMDebuggerEnter(debugMemory->p, DEBUGGER_ENTER_WATCHPOINT); +void ARMDebuggerShim_store16(struct ARMCore* cpu, uint32_t address, int16_t value, int* cycleCounter) { + struct ARMDebugger* debugger; + FIND_DEBUGGER(debugger, cpu); + if (_checkWatchpoints(debugger->watchpoints, address, 2)) { + ARMDebuggerEnter(debugger, DEBUGGER_ENTER_WATCHPOINT); } - debugMemory->original->store16(debugMemory->original, address, value, cycleCounter); + debugger->originalMemory.store16(debugger->cpu, address, value, cycleCounter); } -void ARMDebuggerShim_store8(struct ARMMemory* memory, uint32_t address, int8_t value, int* cycleCounter) { - struct DebugMemoryShim* debugMemory = (struct DebugMemoryShim*) memory; - if (_checkWatchpoints(debugMemory->watchpoints, address, 1)) { - ARMDebuggerEnter(debugMemory->p, DEBUGGER_ENTER_WATCHPOINT); +void ARMDebuggerShim_store8(struct ARMCore* cpu, uint32_t address, int8_t value, int* cycleCounter) { + struct ARMDebugger* debugger; + FIND_DEBUGGER(debugger, cpu); + if (_checkWatchpoints(debugger->watchpoints, address, 1)) { + ARMDebuggerEnter(debugger, DEBUGGER_ENTER_WATCHPOINT); } - debugMemory->original->store8(debugMemory->original, address, value, cycleCounter); + debugger->originalMemory.store8(debugger->cpu, address, value, cycleCounter); } -void ARMDebuggerShim_setActiveRegion(struct ARMMemory* memory, uint32_t address) { - struct DebugMemoryShim* debugMemory = (struct DebugMemoryShim*) memory; - debugMemory->original->setActiveRegion(debugMemory->original, address); - memory->activeRegion = debugMemory->original->activeRegion; - memory->activeMask = debugMemory->original->activeMask; - memory->activePrefetchCycles32 = debugMemory->original->activePrefetchCycles32; - memory->activePrefetchCycles16 = debugMemory->original->activePrefetchCycles16; - memory->activeNonseqCycles32 = debugMemory->original->activeNonseqCycles32; - memory->activeNonseqCycles16 = debugMemory->original->activeNonseqCycles16; +void ARMDebuggerShim_setActiveRegion(struct ARMCore* cpu, uint32_t address) { + struct ARMDebugger* debugger; + FIND_DEBUGGER(debugger, cpu); + debugger->originalMemory.setActiveRegion(cpu, address); }