2 * Copyright © 2008 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 struct command_function {
24 enum command_result (*func)(FILE *output, int argc, char **argv);
29 static struct command_function functions[] = {
30 { "help", "?", command_help, "help", "Print this list\n" },
31 { "quit", "q", command_quit, "[q]uit", "Quit\n" },
32 { "di", "di", command_di, "di <start> <end>",
34 { "ds", "ds", command_ds, "ds <start> <end>",
36 { "dx", "dx", command_dx, "dx <start> <end>",
38 { "set", "t", command_set, "se[t] mem <prefix> <start> <end>",
39 "Set mem {xram|rom|iram|sfr} <start> <end>\n"
41 { "dump", "d", command_dump, "[d]ump <prefix> <start> <end>",
42 "Dump {xram|rom|iram|sfr} <start> <end>\n" },
43 { "pc", "p", command_pc, "[p]c [addr]",
44 "Get or set pc value\n" },
45 { "break", "b", command_break,"[b]reak <addr>",
46 "Set break point\n" },
47 { "clear", "c", command_clear,"[c]lear <addr>",
48 "Clear break point\n" },
49 { "run", "r", command_run, "[r]un [start] [stop]",
50 "Run with optional start and temp breakpoint addresses\n" },
51 { "next", "n", command_next, "[n]ext",
52 "Step over one instruction, past any call\n" },
53 { "step", "s", command_step, "[s]tep",
55 { "load", "l", command_load, "[l]oad <file>",
56 "Load a hex file into memory or flash" },
57 { "halt", "h", command_halt, "[h]alt",
58 "Halt the processor\n" },
59 { "reset","res",command_reset, "[res]et",
61 { "status","status",command_status, "status",
62 "Display CC1111 debug status\n" },
65 #define NUM_FUNCTIONS (sizeof functions / sizeof functions[0])
73 string_to_int(char *s, int *v)
77 if (isdigit(s[0]) || s[0] == '-' || s[0] == '+') {
78 *v = strtol(s, &endptr, 0);
81 } else if (*s == '\'') {
107 static struct command_function *
108 command_string_to_function(char *name)
111 for (i = 0; i < NUM_FUNCTIONS; i++)
112 if (!strcmp(name, functions[i].name) ||
113 !strcmp(name, functions[i].alias))
114 return &functions[i];
119 command_split_into_words(char *line, char **argv)
126 while (isspace(*line))
134 while (*line && *line != quotechar)
141 while (*line && !isspace(*line))
152 command_help(FILE *output, int argc, char **argv)
155 struct command_function *func;
158 for (i = 0; i < NUM_FUNCTIONS; i++)
159 fprintf(output, "%-10s%s\n", functions[i].name,
162 for (i = 1; i < argc; i++) {
163 func = command_string_to_function(argv[i]);
165 fprintf(output, "%-10s unknown command\n", argv[i]);
166 return command_syntax;
168 fprintf(output, "%-10s %s\n%s", func->name,
169 func->usage, func->help);
172 return command_debug;
176 command_syntax_error(FILE *output, int argc, char **argv)
178 fprintf(output, "Syntax error in:");
180 fprintf(output, " %s", *argv++);
181 fprintf(output, "\n");
185 command_read (FILE *input, FILE *output)
190 enum command_result result;
191 struct command_function *func;
193 s51_dbg = ccdbg_open ();
195 perror("ccdbg_open");
198 ccdbg_debug_mode(s51_dbg);
199 fprintf(output, "Welcome to the non-simulated processor\n");
202 fprintf(output, "%s", s51_prompt);
206 if (!fgets (line, sizeof line, input))
208 argc = command_split_into_words(line, argv);
210 func = command_string_to_function(argv[0]);
212 command_syntax_error(output, argc, argv);
215 result = (*func->func)(output, argc, argv);
218 command_syntax_error(output, argc, argv);
221 fprintf(output, "Error\n");
223 case command_proceed:
231 ccdbg_close(s51_dbg);
232 fprintf(output, "...\n");