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