blob: 2de4eb6f19ec1c9142be2712941860a359d365c1 [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;
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 }
swissChilic6b4f7e2020-08-09 16:36:36 -070092 else if (!strcmp(tok, "run"))
93 {
94 *running = true;
95 }
swissChili1970cb82020-08-10 13:22:39 -070096 else if (!strcmp(tok, "pause"))
97 {
98 *running = false;
99 }
swissChilic6b4f7e2020-08-09 16:36:36 -0700100 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
113typedef struct
114{
115 mqd_t mq;
116 cpu_t *cpu;
117} debug_prompt_arg_t;
118
119void 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)
swissChili6264a3b2020-07-30 19:02:07 -0700127 {
128 char *input = readline("\033[33m> \033[0m");
swissChili62d6d5d2020-07-30 20:12:47 -0700129 if (!input || !*input)
swissChili6264a3b2020-07-30 19:02:07 -0700130 continue;
131
swissChilic6b4f7e2020-08-09 16:36:36 -0700132 if (!strcmp(input, "quit") || !strcmp(input, "exit"))
133 running = false;
swissChili62d6d5d2020-07-30 20:12:47 -0700134
swissChilic6b4f7e2020-08-09 16:36:36 -0700135 mq_send(mq, input, strlen(input) + 1, 2);
swissChili6264a3b2020-07-30 19:02:07 -0700136
137 add_history(input);
swissChilic6b4f7e2020-08-09 16:36:36 -0700138 free(input);
swissChili6264a3b2020-07-30 19:02:07 -0700139 }
140}
swissChilic6b4f7e2020-08-09 16:36:36 -0700141
142pthread_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}