blob: 577feee314d93b254bdf9bea2a499ec28b590dc3 [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 }
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
68typedef struct
69{
70 mqd_t mq;
71 cpu_t *cpu;
72} debug_prompt_arg_t;
73
74void 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)
swissChili6264a3b2020-07-30 19:02:07 -070082 {
83 char *input = readline("\033[33m> \033[0m");
swissChili62d6d5d2020-07-30 20:12:47 -070084 if (!input || !*input)
swissChili6264a3b2020-07-30 19:02:07 -070085 continue;
86
swissChilic6b4f7e2020-08-09 16:36:36 -070087 if (!strcmp(input, "quit") || !strcmp(input, "exit"))
88 running = false;
swissChili62d6d5d2020-07-30 20:12:47 -070089
swissChilic6b4f7e2020-08-09 16:36:36 -070090 mq_send(mq, input, strlen(input) + 1, 2);
swissChili6264a3b2020-07-30 19:02:07 -070091
92 add_history(input);
swissChilic6b4f7e2020-08-09 16:36:36 -070093 free(input);
swissChili6264a3b2020-07-30 19:02:07 -070094 }
95}
swissChilic6b4f7e2020-08-09 16:36:36 -070096
97pthread_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}