Initial debugger
Jeffrey Pfau jeffrey@endrift.com
Fri, 12 Apr 2013 01:32:43 -0700
4 files changed,
77 insertions(+),
6 deletions(-)
M
CMakeLists.txt
→
CMakeLists.txt
@@ -1,5 +1,6 @@
cmake_minimum_required(VERSION 2.6) project(GBAc) set(CMAKE_C_FLAGS_DEBUG "-g -Wall -Wextra") -file(GLOB SOURCES src/*.c) +file(GLOB SOURCES src/*.c third-party/linenoise/linenoise.c) +include_directories(${CMAKE_SOURCE_DIR}/third-party/linenoise) add_executable(gbac ${SOURCES})
A
src/debugger.c
@@ -0,0 +1,60 @@
+#include "debugger.h" + +#include "arm.h" + +#include <stdio.h> +#include <stdlib.h> +#include <strings.h> +#include "linenoise.h" + +enum { + CMD_QUIT, + CMD_NEXT +}; + +static inline void _printPSR(union PSR psr) { + printf("%08x [%c%c%c%c%c%c%c]\n", psr.packed, + psr.n ? 'N' : '-', + psr.z ? 'Z' : '-', + psr.c ? 'C' : '-', + psr.v ? 'V' : '-', + psr.i ? 'I' : '-', + psr.f ? 'F' : '-', + psr.t ? 'T' : '-'); +} + +static void _printStatus(struct ARMDebugger* debugger) { + int r; + for (r = 0; r < 4; ++r) { + printf("%08x %08x %08x %08x\n", + debugger->cpu->gprs[r << 2], + debugger->cpu->gprs[(r << 2) + 1], + debugger->cpu->gprs[(r << 2) + 1], + debugger->cpu->gprs[(r << 2) + 3]); + } + _printPSR(debugger->cpu->cpsr); +} + +static int _parse(struct ARMDebugger* debugger, const char* line) { + if (strcasecmp(line, "q") == 0 || strcasecmp(line, "quit") == 0) { + return CMD_QUIT; + } + ARMRun(debugger->cpu); + _printStatus(debugger); + return CMD_NEXT; +} + +void ARMDebuggerInit(struct ARMDebugger* debugger, struct ARMCore* cpu) { + debugger->cpu = cpu; +} + +void ARMDebuggerEnter(struct ARMDebugger* debugger) { + char* line; + _printStatus(debugger); + while ((line = linenoise("> "))) { + if (_parse(debugger, line) == CMD_QUIT) { + break; + } + free(line); + } +}
A
src/debugger.h
@@ -0,0 +1,11 @@
+#ifndef DEBUGGER_H +#define DEBUGGER_H + +struct ARMDebugger { + struct ARMCore* cpu; +}; + +void ARMDebuggerInit(struct ARMDebugger*, struct ARMCore*); +void ARMDebuggerEnter(struct ARMDebugger*); + +#endif
M
src/main.c
→
src/main.c
@@ -1,22 +1,21 @@
#include "arm.h" +#include "debugger.h" #include "gba.h" -#include "isa-arm.h" #include <fcntl.h> #include <sys/stat.h> #include <unistd.h> 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]); - int i; - for (i = 0; i < 1024 * 1024 * 16; ++i) { - ARMRun(&gba.cpu); - } + ARMDebuggerInit(&debugger, &gba.cpu); + ARMDebuggerEnter(&debugger); GBADeinit(&gba); close(fd);