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",
63 #define NUM_FUNCTIONS (sizeof functions / sizeof functions[0])
71 string_to_int(char *s, int *v)
75 if (isdigit(s[0]) || s[0] == '-' || s[0] == '+') {
76 *v = strtol(s, &endptr, 0);
79 } else if (*s == '\'') {
105 static struct command_function *
106 command_string_to_function(char *name)
109 for (i = 0; i < NUM_FUNCTIONS; i++)
110 if (!strcmp(name, functions[i].name) ||
111 !strcmp(name, functions[i].alias))
112 return &functions[i];
117 command_split_into_words(char *line, char **argv)
124 while (isspace(*line))
132 while (*line && *line != quotechar)
139 while (*line && !isspace(*line))
150 command_help(FILE *output, int argc, char **argv)
153 struct command_function *func;
156 for (i = 0; i < NUM_FUNCTIONS; i++)
157 fprintf(output, "%-10s%s\n", functions[i].name,
160 for (i = 1; i < argc; i++) {
161 func = command_string_to_function(argv[i]);
163 fprintf(output, "%-10s unknown command\n", argv[i]);
164 return command_syntax;
166 fprintf(output, "%-10s %s\n%s", func->name,
167 func->usage, func->help);
170 return command_debug;
174 command_syntax_error(FILE *output, int argc, char **argv)
176 fprintf(output, "Syntax error in:");
178 fprintf(output, " %s", *argv++);
179 fprintf(output, "\n");
183 command_read (FILE *input, FILE *output)
188 enum command_result result;
189 struct command_function *func;
191 s51_dbg = ccdbg_open ();
193 perror("ccdbg_open");
196 ccdbg_debug_mode(s51_dbg);
197 fprintf(output, "Welcome to the non-simulated processor\n");
200 fprintf(output, "%s", s51_prompt);
204 if (!fgets (line, sizeof line, input))
206 argc = command_split_into_words(line, argv);
208 func = command_string_to_function(argv[0]);
210 command_syntax_error(output, argc, argv);
213 result = (*func->func)(output, argc, argv);
216 command_syntax_error(output, argc, argv);
219 fprintf(output, "Error\n");
221 case command_proceed:
229 ccdbg_close(s51_dbg);
230 fprintf(output, "...\n");