Add some basic input to the debugger

This commit is contained in:
Jeffrey Pfau 2013-04-12 02:03:11 -07:00
parent 1db7f5b179
commit 688af6cdee
2 changed files with 54 additions and 11 deletions

View File

@ -3,17 +3,30 @@
#include "arm.h" #include "arm.h"
#include <stdio.h> #include <stdio.h>
#include <stdarg.h>
#include <stdlib.h> #include <stdlib.h>
#include <strings.h> #include <strings.h>
#include "linenoise.h" #include "linenoise.h"
enum { typedef void (DebuggerComamnd)(struct ARMDebugger*);
CMD_QUIT,
CMD_NEXT static void _printStatus(struct ARMDebugger*);
static void _quit(struct ARMDebugger*);
struct {
const char* name;
DebuggerComamnd* command;
} debuggerCommands[] = {
{ "i", _printStatus },
{ "info", _printStatus },
{ "q", _quit },
{ "quit", _quit },
{ "status", _printStatus },
{ 0, 0 }
}; };
static inline void _printPSR(union PSR psr) { static inline void _printPSR(union PSR psr) {
printf("%08x [%c%c%c%c%c%c%c]\n", psr.packed, printf("%08X [%c%c%c%c%c%c%c]\n", psr.packed,
psr.n ? 'N' : '-', psr.n ? 'N' : '-',
psr.z ? 'Z' : '-', psr.z ? 'Z' : '-',
psr.c ? 'C' : '-', psr.c ? 'C' : '-',
@ -26,7 +39,7 @@ static inline void _printPSR(union PSR psr) {
static void _printStatus(struct ARMDebugger* debugger) { static void _printStatus(struct ARMDebugger* debugger) {
int r; int r;
for (r = 0; r < 4; ++r) { for (r = 0; r < 4; ++r) {
printf("%08x %08x %08x %08x\n", printf("%08X %08X %08X %08X\n",
debugger->cpu->gprs[r << 2], debugger->cpu->gprs[r << 2],
debugger->cpu->gprs[(r << 2) + 1], debugger->cpu->gprs[(r << 2) + 1],
debugger->cpu->gprs[(r << 2) + 1], debugger->cpu->gprs[(r << 2) + 1],
@ -35,13 +48,32 @@ static void _printStatus(struct ARMDebugger* debugger) {
_printPSR(debugger->cpu->cpsr); _printPSR(debugger->cpu->cpsr);
} }
static int _parse(struct ARMDebugger* debugger, const char* line) { static void _quit(struct ARMDebugger* debugger) {
if (strcasecmp(line, "q") == 0 || strcasecmp(line, "quit") == 0) { debugger->state = DEBUGGER_EXITING;
return CMD_QUIT; }
static void _parse(struct ARMDebugger* debugger, const char* line) {
char* firstSpace = strchr(line, ' ');
size_t cmdLength;
if (firstSpace) {
cmdLength = line - firstSpace;
} else {
cmdLength = strlen(line);
}
int i;
const char* name;
for (i = 0; (name = debuggerCommands[i].name); ++i) {
if (strlen(name) != cmdLength) {
continue;
}
if (strncasecmp(name, line, cmdLength) == 0) {
debuggerCommands[i].command(debugger);
return;
}
} }
ARMRun(debugger->cpu); ARMRun(debugger->cpu);
_printStatus(debugger); _printStatus(debugger);
return CMD_NEXT;
} }
void ARMDebuggerInit(struct ARMDebugger* debugger, struct ARMCore* cpu) { void ARMDebuggerInit(struct ARMDebugger* debugger, struct ARMCore* cpu) {
@ -52,9 +84,13 @@ void ARMDebuggerEnter(struct ARMDebugger* debugger) {
char* line; char* line;
_printStatus(debugger); _printStatus(debugger);
while ((line = linenoise("> "))) { while ((line = linenoise("> "))) {
if (_parse(debugger, line) == CMD_QUIT) { _parse(debugger, line);
free(line);
switch (debugger->state) {
case DEBUGGER_EXITING:
return;
default:
break; break;
} }
free(line);
} }
} }

View File

@ -1,7 +1,14 @@
#ifndef DEBUGGER_H #ifndef DEBUGGER_H
#define DEBUGGER_H #define DEBUGGER_H
enum DebuggerState {
DEBUGGER_PAUSED,
DEBUGGER_RUNNING,
DEBUGGER_EXITING
};
struct ARMDebugger { struct ARMDebugger {
enum DebuggerState state;
struct ARMCore* cpu; struct ARMCore* cpu;
}; };