src/debugger.c (view raw)
1#include "debugger.h"
2
3#include "arm.h"
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <strings.h>
8#include "linenoise.h"
9
10enum {
11 CMD_QUIT,
12 CMD_NEXT
13};
14
15static inline void _printPSR(union PSR psr) {
16 printf("%08x [%c%c%c%c%c%c%c]\n", psr.packed,
17 psr.n ? 'N' : '-',
18 psr.z ? 'Z' : '-',
19 psr.c ? 'C' : '-',
20 psr.v ? 'V' : '-',
21 psr.i ? 'I' : '-',
22 psr.f ? 'F' : '-',
23 psr.t ? 'T' : '-');
24}
25
26static void _printStatus(struct ARMDebugger* debugger) {
27 int r;
28 for (r = 0; r < 4; ++r) {
29 printf("%08x %08x %08x %08x\n",
30 debugger->cpu->gprs[r << 2],
31 debugger->cpu->gprs[(r << 2) + 1],
32 debugger->cpu->gprs[(r << 2) + 1],
33 debugger->cpu->gprs[(r << 2) + 3]);
34 }
35 _printPSR(debugger->cpu->cpsr);
36}
37
38static int _parse(struct ARMDebugger* debugger, const char* line) {
39 if (strcasecmp(line, "q") == 0 || strcasecmp(line, "quit") == 0) {
40 return CMD_QUIT;
41 }
42 ARMRun(debugger->cpu);
43 _printStatus(debugger);
44 return CMD_NEXT;
45}
46
47void ARMDebuggerInit(struct ARMDebugger* debugger, struct ARMCore* cpu) {
48 debugger->cpu = cpu;
49}
50
51void ARMDebuggerEnter(struct ARMDebugger* debugger) {
52 char* line;
53 _printStatus(debugger);
54 while ((line = linenoise("> "))) {
55 if (_parse(debugger, line) == CMD_QUIT) {
56 break;
57 }
58 free(line);
59 }
60}