blob: fe1355d632311be3d4b09e31b6b92f90481bfede [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 }
swissChili1970cb82020-08-10 13:22:39 -070055 else if (!strcmp(tok, "pause"))
56 {
57 *running = false;
58 }
swissChilic6b4f7e2020-08-09 16:36:36 -070059 else if (!strcmp(tok, "quit") || !strcmp(tok, "exit"))
60 {
61 printf("Bye\n");
62 return true;
63 }
64 else
65 {
66 printf("Unknown command %s\n", tok);
67 }
68
69 return false;
70}
71
72typedef struct
73{
74 mqd_t mq;
75 cpu_t *cpu;
76} debug_prompt_arg_t;
77
78void debug_prompt(debug_prompt_arg_t *arg)
79{
80 mqd_t mq = arg->mq;
81 cpu_t *cpu = arg->cpu;
82 free(arg);
83
84 bool running = true;
85 while (running)
swissChili6264a3b2020-07-30 19:02:07 -070086 {
87 char *input = readline("\033[33m> \033[0m");
swissChili62d6d5d2020-07-30 20:12:47 -070088 if (!input || !*input)
swissChili6264a3b2020-07-30 19:02:07 -070089 continue;
90
swissChilic6b4f7e2020-08-09 16:36:36 -070091 if (!strcmp(input, "quit") || !strcmp(input, "exit"))
92 running = false;
swissChili62d6d5d2020-07-30 20:12:47 -070093
swissChilic6b4f7e2020-08-09 16:36:36 -070094 mq_send(mq, input, strlen(input) + 1, 2);
swissChili6264a3b2020-07-30 19:02:07 -070095
96 add_history(input);
swissChilic6b4f7e2020-08-09 16:36:36 -070097 free(input);
swissChili6264a3b2020-07-30 19:02:07 -070098 }
99}
swissChilic6b4f7e2020-08-09 16:36:36 -0700100
101pthread_t start_debug_prompt(mqd_t mq, cpu_t *cpu)
102{
103 debug_prompt_arg_t *arg = malloc(sizeof(debug_prompt_arg_t));
104 arg->mq = mq;
105 arg->cpu = cpu;
106
107 pthread_t thread;
108 pthread_create(&thread, NULL, (void *(*)(void *))&debug_prompt, arg);
109 return thread;
110}