blob: f5ca437208a9f9825c5b58e6bca793cf4539b8e5 [file] [log] [blame]
swissChili6264a3b2020-07-30 19:02:07 -07001#include "dbg.h"
2#include "cpu.h"
3
4#include <readline/readline.h>
5#include <readline/history.h>
6#include <string.h>
7#include <stdlib.h>
8
9void debug(cpu_t *cpu)
10{
11 while (true)
12 {
13 char *input = readline("\033[33m> \033[0m");
swissChili62d6d5d2020-07-30 20:12:47 -070014 if (!input || !*input)
swissChili6264a3b2020-07-30 19:02:07 -070015 continue;
16
17 char *tok = strtok(input, " \t\r\n\v");
swissChili62d6d5d2020-07-30 20:12:47 -070018
19 if (!tok || !*tok)
20 continue;
swissChili6264a3b2020-07-30 19:02:07 -070021
22 if (!strcmp(tok, "step") || !strcmp(tok, "s"))
23 {
24 step(cpu);
25 }
26 else if (!strcmp(tok, "show") || !strcmp(tok, "print"))
27 {
28 if ((tok = strtok(NULL, " ")))
29 {
30 char *ok = 0;
31 if (tok[0] == '$')
32 {
33 uint16_t addr = strtol(tok + 1, &ok, 16);
34
swissChili62d6d5d2020-07-30 20:12:47 -070035 printf("Memory:\n");
36 printf("\t$%x %x\n", addr, cpu->mem[addr]);
37 if (addr < 0xFFFF)
swissChili6264a3b2020-07-30 19:02:07 -070038 printf("\t$%x %x\n", addr + 1, cpu->mem[addr + 1]);
swissChili6264a3b2020-07-30 19:02:07 -070039 }
40 else
41 {
swissChili62d6d5d2020-07-30 20:12:47 -070042 printf("Expected an address as an argument in the form "
43 "$1234, not %s\n", tok);
swissChili6264a3b2020-07-30 19:02:07 -070044 }
45 }
swissChili62d6d5d2020-07-30 20:12:47 -070046 else
47 {
48 printf("Registers:\n");
49
50 #define R(r) printf("\t" #r ":\t%x\n", cpu->regs[r]);
51 REGISTERS
52 #undef R
53 }
54 }
55 else if (!strcmp(tok, "quit") || !strcmp(tok, "exit"))
56 {
57 printf("Bye\n");
58 return;
59 }
60 else
61 {
62 printf("Unknown command %s\n", tok);
swissChili6264a3b2020-07-30 19:02:07 -070063 }
64
65 add_history(input);
66 }
67}