blob: e2ee986751b62665772393d671eba5cd00dcbdda [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>
swissChilic6b4f7e2020-08-09 16:36:36 -07007#include <pthread.h>
swissChili6264a3b2020-07-30 19:02:07 -07008#include <stdlib.h>
9
swissChilic6b4f7e2020-08-09 16:36:36 -070010bool debug_stmt(cpu_t *cpu, char *input, bool *running)
swissChili6264a3b2020-07-30 19:02:07 -070011{
swissChilic6b4f7e2020-08-09 16:36:36 -070012 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 }
swissChili82800a92020-08-10 19:33:17 -070051 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;
swissChili90db50a2020-08-11 14:17:14 -070068 if (!strcasecmp(reg_name, "A"))
swissChili82800a92020-08-10 19:33:17 -070069 reg = A;
swissChili90db50a2020-08-11 14:17:14 -070070 else if (!strcasecmp(reg_name, "X"))
swissChili82800a92020-08-10 19:33:17 -070071 reg = X;
swissChili90db50a2020-08-11 14:17:14 -070072 else if (!strcasecmp(reg_name, "Y"))
swissChili82800a92020-08-10 19:33:17 -070073 reg = Y;
swissChili90db50a2020-08-11 14:17:14 -070074 else if (!strcasecmp(reg_name, "SP"))
swissChili82800a92020-08-10 19:33:17 -070075 reg = SP;
swissChili90db50a2020-08-11 14:17:14 -070076 else if (!strcasecmp(reg_name, "PC"))
swissChili82800a92020-08-10 19:33:17 -070077 cpu->pc = val;
swissChili90db50a2020-08-11 14:17:14 -070078 else if (!strcasecmp(reg_name, "SR"))
swissChili82800a92020-08-10 19:33:17 -070079 *(uint8_t *)&cpu->status = val;
swissChili90db50a2020-08-11 14:17:14 -070080 else
81 printf("set command expected a register as the first argument, got %s\n",
82 reg_name);
swissChili82800a92020-08-10 19:33:17 -070083
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 }
swissChilic6b4f7e2020-08-09 16:36:36 -070095 else if (!strcmp(tok, "run"))
96 {
97 *running = true;
98 }
swissChili1970cb82020-08-10 13:22:39 -070099 else if (!strcmp(tok, "pause"))
100 {
101 *running = false;
102 }
swissChilic6b4f7e2020-08-09 16:36:36 -0700103 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
116typedef struct
117{
118 mqd_t mq;
119 cpu_t *cpu;
120} debug_prompt_arg_t;
121
122void 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)
swissChili6264a3b2020-07-30 19:02:07 -0700130 {
131 char *input = readline("\033[33m> \033[0m");
swissChili62d6d5d2020-07-30 20:12:47 -0700132 if (!input || !*input)
swissChili6264a3b2020-07-30 19:02:07 -0700133 continue;
134
swissChilic6b4f7e2020-08-09 16:36:36 -0700135 if (!strcmp(input, "quit") || !strcmp(input, "exit"))
136 running = false;
swissChili62d6d5d2020-07-30 20:12:47 -0700137
swissChilic6b4f7e2020-08-09 16:36:36 -0700138 mq_send(mq, input, strlen(input) + 1, 2);
swissChili6264a3b2020-07-30 19:02:07 -0700139
140 add_history(input);
swissChilic6b4f7e2020-08-09 16:36:36 -0700141 free(input);
swissChili6264a3b2020-07-30 19:02:07 -0700142 }
143}
swissChilic6b4f7e2020-08-09 16:36:36 -0700144
145pthread_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}