swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 1 | #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 | |
| 9 | void debug(cpu_t *cpu) |
| 10 | { |
| 11 | while (true) |
| 12 | { |
| 13 | char *input = readline("\033[33m> \033[0m"); |
swissChili | 62d6d5d | 2020-07-30 20:12:47 -0700 | [diff] [blame] | 14 | if (!input || !*input) |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 15 | continue; |
| 16 | |
| 17 | char *tok = strtok(input, " \t\r\n\v"); |
swissChili | 62d6d5d | 2020-07-30 20:12:47 -0700 | [diff] [blame] | 18 | |
| 19 | if (!tok || !*tok) |
| 20 | continue; |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 21 | |
| 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 | |
swissChili | 62d6d5d | 2020-07-30 20:12:47 -0700 | [diff] [blame] | 35 | printf("Memory:\n"); |
| 36 | printf("\t$%x %x\n", addr, cpu->mem[addr]); |
| 37 | if (addr < 0xFFFF) |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 38 | printf("\t$%x %x\n", addr + 1, cpu->mem[addr + 1]); |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 39 | } |
| 40 | else |
| 41 | { |
swissChili | 62d6d5d | 2020-07-30 20:12:47 -0700 | [diff] [blame] | 42 | printf("Expected an address as an argument in the form " |
| 43 | "$1234, not %s\n", tok); |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 44 | } |
| 45 | } |
swissChili | 62d6d5d | 2020-07-30 20:12:47 -0700 | [diff] [blame] | 46 | 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); |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | add_history(input); |
| 66 | } |
| 67 | } |