From e5379c99e015d33243a02da824f9b156264335ba Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Sat, 13 Apr 2013 13:50:41 -0700 Subject: [PATCH] Add ability to run code indefinitely (or at least until we crash) --- src/debugger.c | 40 +++++++++++++++++++++++++++++++++------- src/debugger.h | 1 + src/gba.c | 9 +++++++++ src/gba.h | 4 ++++ src/main.c | 4 ++-- 5 files changed, 49 insertions(+), 9 deletions(-) diff --git a/src/debugger.c b/src/debugger.c index ee78a2627..414117caf 100644 --- a/src/debugger.c +++ b/src/debugger.c @@ -28,6 +28,7 @@ static const char* ERROR_MISSING_ARGS = "Arguments missing"; typedef void (DebuggerComamnd)(struct ARMDebugger*, struct DebugVector*); static void _breakInto(struct ARMDebugger*, struct DebugVector*); +static void _continue(struct ARMDebugger*, struct DebugVector*); static void _print(struct ARMDebugger*, struct DebugVector*); static void _printHex(struct ARMDebugger*, struct DebugVector*); static void _printStatus(struct ARMDebugger*, struct DebugVector*); @@ -40,6 +41,8 @@ struct { const char* name; DebuggerComamnd* command; } debuggerCommands[] = { + { "c", _continue }, + { "continue", _continue }, { "i", _printStatus }, { "info", _printStatus }, { "p", _print }, @@ -80,6 +83,11 @@ static void _breakInto(struct ARMDebugger* debugger, struct DebugVector* dv) { signal(SIGTRAP, oldSignal); } +static void _continue(struct ARMDebugger* debugger, struct DebugVector* dv) { + (void)(dv); + debugger->state = DEBUGGER_RUNNING; +} + static void _print(struct ARMDebugger* debugger, struct DebugVector* dv) { (void)(debugger); for ( ; dv; dv = dv->next) { @@ -441,19 +449,37 @@ static void _parse(struct ARMDebugger* debugger, const char* line) { void ARMDebuggerInit(struct ARMDebugger* debugger, struct ARMCore* cpu) { debugger->cpu = cpu; + debugger->state = DEBUGGER_PAUSED; +} + +void ARMDebuggerRun(struct ARMDebugger* debugger) { + while (debugger->state != DEBUGGER_EXITING) { + while (debugger->state == DEBUGGER_RUNNING) { + ARMRun(debugger->cpu); + } + switch (debugger->state) { + case DEBUGGER_PAUSED: + ARMDebuggerEnter(debugger); + break; + case DEBUGGER_EXITING: + return; + default: + // Should never be reached + break; + } + } } void ARMDebuggerEnter(struct ARMDebugger* debugger) { char* line; _printStatus(debugger, 0); - while ((line = linenoise("> "))) { + while (debugger->state == DEBUGGER_PAUSED) { + line = linenoise("> "); + if (!line) { + debugger->state = DEBUGGER_EXITING; + return; + } _parse(debugger, line); free(line); - switch (debugger->state) { - case DEBUGGER_EXITING: - return; - default: - break; - } } } diff --git a/src/debugger.h b/src/debugger.h index ab7b7f938..7018385cd 100644 --- a/src/debugger.h +++ b/src/debugger.h @@ -13,6 +13,7 @@ struct ARMDebugger { }; void ARMDebuggerInit(struct ARMDebugger*, struct ARMCore*); +void ARMDebuggerRun(struct ARMDebugger*); void ARMDebuggerEnter(struct ARMDebugger*); #endif diff --git a/src/gba.c b/src/gba.c index 43a7bc88f..3c5a58388 100644 --- a/src/gba.c +++ b/src/gba.c @@ -1,7 +1,10 @@ #include "gba.h" +#include "debugger.h" + #include #include +#include #include #include @@ -76,6 +79,11 @@ void GBABoardReset(struct ARMBoard* board) { cpu->gprs[ARM_SP] = SP_BASE_SYSTEM; } +void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger) { + ARMDebuggerInit(debugger, &gba->cpu); + gba->debugger = debugger; +} + void GBALoadROM(struct GBA* gba, int fd) { gba->memory.rom = mmap(0, SIZE_CART0, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FILE, fd, 0); // TODO: error check @@ -382,4 +390,5 @@ void GBALog(int level, const char* format, ...) { void GBAHitStub(struct ARMBoard* board, uint32_t opcode) { GBALog(GBA_LOG_STUB, "Stub opcode: %08x", opcode); + abort(); } diff --git a/src/gba.h b/src/gba.h index 3de802d41..5258ae47f 100644 --- a/src/gba.h +++ b/src/gba.h @@ -92,6 +92,8 @@ struct GBA { struct GBABoard board; struct GBAMemory memory; + struct ARMDebugger* debugger; + enum GBAError errno; const char* errstr; }; @@ -105,6 +107,8 @@ void GBAMemoryDeinit(struct GBAMemory* memory); void GBABoardInit(struct GBABoard* board); void GBABoardReset(struct ARMBoard* board); +void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger); + void GBALoadROM(struct GBA* gba, int fd); int32_t GBALoad32(struct ARMMemory* memory, uint32_t address); diff --git a/src/main.c b/src/main.c index 47d882bf4..4d2beae39 100644 --- a/src/main.c +++ b/src/main.c @@ -14,8 +14,8 @@ int main(int argc, char** argv) { GBALoadROM(&gba, fd); gba.cpu.gprs[ARM_PC] = 0x08000004; gba.memory.d.setActiveRegion(&gba.memory.d, gba.cpu.gprs[ARM_PC]); - ARMDebuggerInit(&debugger, &gba.cpu); - ARMDebuggerEnter(&debugger); + GBAAttachDebugger(&gba, &debugger); + ARMDebuggerRun(&debugger); GBADeinit(&gba); close(fd);