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 | } |
swissChili | 82800a9 | 2020-08-10 19:33:17 -0700 | [diff] [blame] | 51 | else if (!strcmp(tok, "set")) |
| 52 | { |
| 53 | char reg_name[32]; |
| 54 | uint16_t addr; |
| 55 | uint8_t val; |
| 56 | |
| 57 | tok = strtok(NULL, "\0"); |
| 58 | |
| 59 | printf("%s\n", tok); |
| 60 | |
| 61 | if (sscanf(tok, "$%hx #$%hhx", &addr, &val)) |
| 62 | { |
| 63 | cpu->mem[addr] = val; |
| 64 | } |
| 65 | else if (sscanf(tok, "%s #$%hhx", reg_name, &val)) |
| 66 | { |
| 67 | int reg = -1; |
swissChili | 90db50a | 2020-08-11 14:17:14 -0700 | [diff] [blame] | 68 | if (!strcasecmp(reg_name, "A")) |
swissChili | 82800a9 | 2020-08-10 19:33:17 -0700 | [diff] [blame] | 69 | reg = A; |
swissChili | 90db50a | 2020-08-11 14:17:14 -0700 | [diff] [blame] | 70 | else if (!strcasecmp(reg_name, "X")) |
swissChili | 82800a9 | 2020-08-10 19:33:17 -0700 | [diff] [blame] | 71 | reg = X; |
swissChili | 90db50a | 2020-08-11 14:17:14 -0700 | [diff] [blame] | 72 | else if (!strcasecmp(reg_name, "Y")) |
swissChili | 82800a9 | 2020-08-10 19:33:17 -0700 | [diff] [blame] | 73 | reg = Y; |
swissChili | 90db50a | 2020-08-11 14:17:14 -0700 | [diff] [blame] | 74 | else if (!strcasecmp(reg_name, "SP")) |
swissChili | 82800a9 | 2020-08-10 19:33:17 -0700 | [diff] [blame] | 75 | reg = SP; |
swissChili | 90db50a | 2020-08-11 14:17:14 -0700 | [diff] [blame] | 76 | else if (!strcasecmp(reg_name, "PC")) |
swissChili | 82800a9 | 2020-08-10 19:33:17 -0700 | [diff] [blame] | 77 | cpu->pc = val; |
swissChili | 90db50a | 2020-08-11 14:17:14 -0700 | [diff] [blame] | 78 | else if (!strcasecmp(reg_name, "SR")) |
swissChili | 82800a9 | 2020-08-10 19:33:17 -0700 | [diff] [blame] | 79 | *(uint8_t *)&cpu->status = val; |
swissChili | 90db50a | 2020-08-11 14:17:14 -0700 | [diff] [blame] | 80 | else |
| 81 | printf("set command expected a register as the first argument, got %s\n", |
| 82 | reg_name); |
swissChili | 82800a9 | 2020-08-10 19:33:17 -0700 | [diff] [blame] | 83 | |
| 84 | if (reg != -1) |
| 85 | { |
| 86 | cpu->regs[reg] = val; |
| 87 | } |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | printf("set command expects either a memory address and a constant, " |
| 92 | "or a register and a constant as arguments.\n"); |
| 93 | } |
| 94 | } |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame] | 95 | else if (!strcmp(tok, "run")) |
| 96 | { |
| 97 | *running = true; |
| 98 | } |
swissChili | 1970cb8 | 2020-08-10 13:22:39 -0700 | [diff] [blame] | 99 | else if (!strcmp(tok, "pause")) |
| 100 | { |
| 101 | *running = false; |
| 102 | } |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame] | 103 | else if (!strcmp(tok, "quit") || !strcmp(tok, "exit")) |
| 104 | { |
| 105 | printf("Bye\n"); |
| 106 | return true; |
| 107 | } |
| 108 | else |
| 109 | { |
| 110 | printf("Unknown command %s\n", tok); |
| 111 | } |
| 112 | |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | typedef struct |
| 117 | { |
| 118 | mqd_t mq; |
| 119 | cpu_t *cpu; |
| 120 | } debug_prompt_arg_t; |
| 121 | |
| 122 | void debug_prompt(debug_prompt_arg_t *arg) |
| 123 | { |
| 124 | mqd_t mq = arg->mq; |
| 125 | cpu_t *cpu = arg->cpu; |
| 126 | free(arg); |
| 127 | |
| 128 | bool running = true; |
| 129 | while (running) |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 130 | { |
| 131 | char *input = readline("\033[33m> \033[0m"); |
swissChili | 62d6d5d | 2020-07-30 20:12:47 -0700 | [diff] [blame] | 132 | if (!input || !*input) |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 133 | continue; |
| 134 | |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame] | 135 | if (!strcmp(input, "quit") || !strcmp(input, "exit")) |
| 136 | running = false; |
swissChili | 62d6d5d | 2020-07-30 20:12:47 -0700 | [diff] [blame] | 137 | |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame] | 138 | mq_send(mq, input, strlen(input) + 1, 2); |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 139 | |
| 140 | add_history(input); |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame] | 141 | free(input); |
swissChili | 6264a3b | 2020-07-30 19:02:07 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame] | 144 | |
| 145 | pthread_t start_debug_prompt(mqd_t mq, cpu_t *cpu) |
| 146 | { |
| 147 | debug_prompt_arg_t *arg = malloc(sizeof(debug_prompt_arg_t)); |
| 148 | arg->mq = mq; |
| 149 | arg->cpu = cpu; |
| 150 | |
| 151 | pthread_t thread; |
| 152 | pthread_create(&thread, NULL, (void *(*)(void *))&debug_prompt, arg); |
| 153 | return thread; |
| 154 | } |