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> |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame^] | 7 | #include <pthread.h> |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 8 | #include <stdlib.h> |
| 9 | |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame^] | 10 | bool debug_stmt(cpu_t *cpu, char *input, bool *running) |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 11 | { |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame^] | 12 | char *tok = strtok(input, " \t\r\n\v"); |
| 13 | |
| 14 | if (!tok || !*tok) |
| 15 | return false; |
| 16 | |
| 17 | if (!strcmp(tok, "step") || !strcmp(tok, "s")) |
| 18 | { |
| 19 | step(cpu); |
| 20 | } |
| 21 | else if (!strcmp(tok, "show") || !strcmp(tok, "print")) |
| 22 | { |
| 23 | if ((tok = strtok(NULL, " "))) |
| 24 | { |
| 25 | char *ok = 0; |
| 26 | if (tok[0] == '$') |
| 27 | { |
| 28 | uint16_t addr = strtol(tok + 1, &ok, 16); |
| 29 | |
| 30 | printf("Memory:\n"); |
| 31 | printf("\t$%x %x\n", addr, cpu->mem[addr]); |
| 32 | if (addr < 0xFFFF) |
| 33 | printf("\t$%x %x\n", addr + 1, cpu->mem[addr + 1]); |
| 34 | } |
| 35 | else |
| 36 | { |
| 37 | printf("Expected an address as an argument in the form " |
| 38 | "$1234, not %s\n", tok); |
| 39 | } |
| 40 | } |
| 41 | else |
| 42 | { |
| 43 | printf("Registers:\n"); |
| 44 | |
| 45 | printf("\tPC:\t$%x\n", cpu->pc); |
| 46 | #define R(r) printf("\t" #r ":\t$%x\n", cpu->regs[r]); |
| 47 | REGISTERS |
| 48 | #undef R |
| 49 | } |
| 50 | } |
| 51 | else if (!strcmp(tok, "run")) |
| 52 | { |
| 53 | *running = true; |
| 54 | } |
| 55 | else if (!strcmp(tok, "quit") || !strcmp(tok, "exit")) |
| 56 | { |
| 57 | printf("Bye\n"); |
| 58 | return true; |
| 59 | } |
| 60 | else |
| 61 | { |
| 62 | printf("Unknown command %s\n", tok); |
| 63 | } |
| 64 | |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | typedef struct |
| 69 | { |
| 70 | mqd_t mq; |
| 71 | cpu_t *cpu; |
| 72 | } debug_prompt_arg_t; |
| 73 | |
| 74 | void debug_prompt(debug_prompt_arg_t *arg) |
| 75 | { |
| 76 | mqd_t mq = arg->mq; |
| 77 | cpu_t *cpu = arg->cpu; |
| 78 | free(arg); |
| 79 | |
| 80 | bool running = true; |
| 81 | while (running) |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 82 | { |
| 83 | char *input = readline("\033[33m> \033[0m"); |
swissChili | 62d6d5d | 2020-07-30 20:12:47 -0700 | [diff] [blame] | 84 | if (!input || !*input) |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 85 | continue; |
| 86 | |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame^] | 87 | if (!strcmp(input, "quit") || !strcmp(input, "exit")) |
| 88 | running = false; |
swissChili | 62d6d5d | 2020-07-30 20:12:47 -0700 | [diff] [blame] | 89 | |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame^] | 90 | mq_send(mq, input, strlen(input) + 1, 2); |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 91 | |
| 92 | add_history(input); |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame^] | 93 | free(input); |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 94 | } |
| 95 | } |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame^] | 96 | |
| 97 | pthread_t start_debug_prompt(mqd_t mq, cpu_t *cpu) |
| 98 | { |
| 99 | debug_prompt_arg_t *arg = malloc(sizeof(debug_prompt_arg_t)); |
| 100 | arg->mq = mq; |
| 101 | arg->cpu = cpu; |
| 102 | |
| 103 | pthread_t thread; |
| 104 | pthread_create(&thread, NULL, (void *(*)(void *))&debug_prompt, arg); |
| 105 | return thread; |
| 106 | } |