diff --git a/CMakeLists.txt b/CMakeLists.txt index 420118522..6c294ecb2 100644 --- a/CMakeLists.txt +++ b/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}) diff --git a/src/debugger.c b/src/debugger.c new file mode 100644 index 000000000..f8a18b30b --- /dev/null +++ b/src/debugger.c @@ -0,0 +1,60 @@ +#include "debugger.h" + +#include "arm.h" + +#include +#include +#include +#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); + } +} diff --git a/src/debugger.h b/src/debugger.h new file mode 100644 index 000000000..f8571ffd4 --- /dev/null +++ b/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 diff --git a/src/main.c b/src/main.c index c2ed2d217..47d882bf4 100644 --- a/src/main.c +++ b/src/main.c @@ -1,22 +1,21 @@ #include "arm.h" +#include "debugger.h" #include "gba.h" -#include "isa-arm.h" #include #include #include 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);