9 #include <readline/readline.h>
14 * shell-like driver for dcpu16 core
15 * provides a basic interface to control a single emulation instance
17 * Justin Wind <justin.wind@gmail.com>
18 * 2012 04 10 - implementation started
19 * 2012 04 12 - cleanup, better shell loop
22 * handle quotes in shell command parsing
23 * use readline/history.h, since we're using readline anyhow
24 * ncurses windowing or something, for future display capabilities
27 static const char * const src_id_
= "$Id$";
29 /* global invocation options */
36 /* global run state, first sigint caught will drop out of run loop and back into shell */
37 static volatile unsigned int running_
= 0;
39 void sigint_handler_(int sig
) {
44 #define VERBOSE_PRINTF(...) do { if (opt_.verbose) printf(__VA_ARGS__); } while (0)
46 static void usage_(char *prog
, unsigned int full
) {
47 FILE *f
= full
? stdout
: stderr
;
48 char *x
= strrchr(prog
, '/');
54 fprintf(f
, "%s -- dcpu16 emulator core shell\n\n",
57 fprintf(f
, "Usage: %s [-v] [file]\n",
61 fprintf(f
, "\nOptions:\n"
62 "\t [file] -- ram image to load initially\n"
63 "\t -v -- verbose execution tracing\n"
64 "\t -h -- this screen\n");
66 fprintf(f
, "\n%78s\n", src_id_
);
70 /* simplified strtoul with range checking */
72 int str_to_word_(char *s
) {
79 l
= strtoul(s
, &ep
, 0);
82 || !(*s
&& *ep
== '\0') ) {
83 /* out of range of conversion, or invalid character encountered */
87 if (l
>= DCPU16_RAM
) {
88 /* out of range for our needs */
96 /* flense a buffer into a newly-allocated argument list */
97 /* FIXME: handle quotes */
99 int buf_tok_vect_(char ***v
, int *c
, char *buf
) {
100 const char *sep
= " \t";
101 const size_t v_grow
= 32;
106 *v
= malloc(v_sz
* sizeof **v
);
108 fprintf(stderr
, "%s():%s\n", "malloc", strerror(errno
));
112 for ( (*v
)[*c
] = strtok_r(buf
, sep
, &st
);
114 (*v
)[*c
] = strtok_r(NULL
, sep
, &st
)
118 if ((size_t)(*c
) == v_sz
) {
119 void *tmp_ptr
= realloc(*v
, (v_sz
+ v_grow
) * sizeof **v
);
120 if (tmp_ptr
== NULL
) {
121 fprintf(stderr
, "%s():%s\n", "realloc", strerror(errno
));
133 /* resets the instance and loads an image into ram starting at addr */
135 int file_load_(struct dcpu16
*vm
, char *filename
, DCPU16_WORD addr
) {
141 f
= fopen(filename
, "rb");
143 fprintf(stderr
, "%s('%s'):%s\n", "fopen", filename
, strerror(errno
));
147 r
= fread(vm
->ram
+ addr
, sizeof(DCPU16_WORD
), DCPU16_RAM
- addr
, f
);
148 VERBOSE_PRINTF("read %zu words", r
);
149 if (addr
) VERBOSE_PRINTF(" starting at 0x%04x", addr
);
150 VERBOSE_PRINTF("\n");
153 fprintf(stderr
, "%s('%s'):%s\n", "fread", filename
, strerror(errno
));
160 Here follows the various commands the shell can execute.
162 At invocation, a command function will have already had its
163 number of arguments vetted, but will need command-specific
164 argument verifications done.
166 The arg_vector contains the command as the first entry, and
167 as such, arg_count will always be at least 1.
168 However, the args_min and args_max entries in struct command_
169 only refer to the counts of arguments, not the entries in the
177 int (*func
)(struct dcpu16
*, int c
, char **v
);
178 void (*help
)(FILE *f
, unsigned int);
181 #define COMMAND_IMPL(x) static int command_##x##_(struct dcpu16 *vm, int arg_count, char **arg_vector)
182 #define COMMAND_HELP(x) static void command_##x##_help_(FILE *f, unsigned int summary)
183 #define COMMAND_ENTRY(x, y, z) { #x, y, z, command_##x##_, command_##x##_help_ }
187 (void)vm
, (void)arg_count
, (void)arg_vector
;
188 VERBOSE_PRINTF("done\n");
192 fprintf(f
, "\tquit\n");
195 fprintf(f
, "Exits the emulator.\n");
203 addr
= str_to_word_(arg_vector
[2]);
205 fprintf(stderr
, "address '%s' is not a valid word: %s\n", arg_vector
[2], strerror(errno
));
210 if (file_load_(vm
, arg_vector
[1], addr
)) {
211 fprintf(stderr
, "failed to load '%s'\n", arg_vector
[1]);
214 printf("loaded '%s'", arg_vector
[1]);
215 if (addr
) printf(" starting at 0x%04x", addr
);
221 fprintf(f
, "\tload file [addr]\n");
224 fprintf(f
, "Load binary image from 'file' into ram.\n");
232 for (i
= 1; i
< arg_count
; i
++) {
233 addr
[i
] = str_to_word_(arg_vector
[i
]);
235 fprintf(stderr
, "address '%s' is not a valid word: %s\n", arg_vector
[i
], strerror(errno
));
239 if (arg_count
< 2) addr
[0] = vm
->pc
;
240 if (arg_count
< 3) addr
[1] = addr
[0];
242 if (addr
[1] < addr
[0]) {
243 fprintf(stderr
, "\t'addr_start' must be before addr_end\n");
247 dcpu16_dump_ram(vm
, addr
[0], addr
[1]);
252 fprintf(f
, "\tdump [addr_start [addr_end]]\n");
255 fprintf(f
, "Displays contents of ram from addr_start to addr_end.\n");
259 COMMAND_IMPL(disassemble
) {
263 for (i
= 1; i
< arg_count
; i
++) {
264 addr
[i
] = str_to_word_(arg_vector
[i
]);
266 fprintf(stderr
, "address '%s' is not a valid word: %s\n", arg_vector
[i
], strerror(errno
));
270 if (arg_count
< 2) addr
[0] = vm
->pc
;
271 if (arg_count
< 3) addr
[1] = addr
[0];
273 if (addr
[1] < addr
[0]) {
274 fprintf(stderr
, "\t'addr_start' must be before addr_end\n");
278 for (i
= addr
[0]; i
<= addr
[1]; i
++)
279 dcpu16_disassemble_print(vm
, i
), printf("\n");
283 COMMAND_HELP(disassemble
) {
284 fprintf(f
, "\tdisassemble [addr_start [addr_end]]\n");
287 fprintf(f
, "Displays contents of ram parsed into instructions.\n");
292 unsigned long count
= 1;
295 if (arg_count
== 2) {
297 count
= strtoul(arg_vector
[1], &ep
, 0);
299 || !(*arg_vector
[0] && *ep
== '\0') ) {
300 fprintf(stderr
, "count '%s' is not a valid number: %s\n", arg_vector
[1], strerror(errno
));
305 fprintf(stderr
, "count must be positive\n");
311 VERBOSE_PRINTF("executing next cycle, instruction: ");
312 dcpu16_disassemble_print(vm
, vm
->pc
), printf("\n");
317 dcpu16_state_print(vm
);
323 fprintf(f
, "\tstep [count]\n");
326 fprintf(f
, "Executes the next instruction, or the next count instructions.\n");
332 (void)arg_count
, (void)arg_vector
;
336 /* install our new interrupt signal handler */
337 if ( (osig
= signal(SIGINT
, sigint_handler_
)) == SIG_ERR
) {
338 fprintf(stderr
, "%s():%s\n", "signal", strerror(errno
));
345 dcpu16_state_print(vm
);
348 /* restore the old interrupt signal handler */
349 if (signal(SIGINT
, osig
) == SIG_ERR
) {
350 fprintf(stderr
, "%s():%s\n", "signal", strerror(errno
));
354 VERBOSE_PRINTF("interrupted...\n");
359 fprintf(f
, "\trun\n");
362 fprintf(f
, "Begins executing continuously.\n");
365 /* gather all these together into a searchable table */
367 /* help command gets some assistance in declarations */
371 static struct command_ command_table_
[] = {
372 COMMAND_ENTRY(help
, 0, -1),
373 COMMAND_ENTRY(quit
, 0, -1),
374 COMMAND_ENTRY(load
, 1, 2),
375 COMMAND_ENTRY(dump
, 0, 2),
376 COMMAND_ENTRY(disassemble
, 0, 2),
377 COMMAND_ENTRY(step
, 0, 1),
378 COMMAND_ENTRY(run
, 0, 0),
379 { NULL
, 0, 0, NULL
, NULL
}
386 if (arg_count
== 2) {
387 for (c
= command_table_
; c
->func
; c
++) {
388 if (strcasecmp(arg_vector
[1], c
->name
) == 0) {
397 for (c
= command_table_
; c
->func
; c
++) {
404 fprintf(f
, "\thelp [command]\n");
407 fprintf(f
, "Displays a list of available commands, or help on a specific command.\n");
411 int main(int argc
, char **argv
) {
412 const char prompt_fmt
[] = "PC:%04x> ";
415 char *line
, *line_prev
;
416 char **tok_v
, **tok_v_prev
;
417 int tok_c
, tok_c_prev
;
420 while ( (c
= getopt(argc
, argv
, "hv")) != EOF
) {
435 if (opt_
.verbose
< 1) {
436 dcpu16_warn_cb_set(NULL
);
437 dcpu16_trace_cb_set(NULL
);
438 } else if (opt_
.verbose
< 2) {
439 dcpu16_trace_cb_set(NULL
);
444 if ((vm
= dcpu16_new()) == NULL
) {
445 fprintf(stderr
, "could not allocate new dcpu instance\n");
446 exit(EX_UNAVAILABLE
);
450 if (file_load_(vm
, *argv
, 0)) {
451 fprintf(stderr
, "couldn't load '%s'\n", *argv
);
456 /* show state, read commands */
457 for (line
= line_prev
= NULL
,
458 tok_v
= tok_v_prev
= NULL
,
459 tok_c
= tok_c_prev
= 0,
460 snprintf(prompt
, sizeof prompt
, prompt_fmt
, vm
->pc
),
461 dcpu16_state_print(vm
);
463 (line
= readline(prompt
));
466 snprintf(prompt
, sizeof prompt
, prompt_fmt
, vm
->pc
),
467 dcpu16_state_print(vm
)) {
468 const char whitespace
[] = " \t";
473 /* skip whitespaces */
474 line_start
= line
+ strspn(line
, whitespace
);
477 /* a new command, remember previous for possible repetition */
479 /* turn new line into new arg array */
480 if (buf_tok_vect_(&tok_v
, &tok_c
, line_start
)) {
481 fprintf(stderr
, "failed to process command\n");
485 /* and keep track if it all for the next time around */
486 if (line_prev
) free(line_prev
);
489 if (tok_v_prev
) free(tok_v_prev
);
493 /* blank new command, but no prior command to repeat? ask again */
494 if (tok_v_prev
== NULL
|| tok_v_prev
[0] == NULL
|| *(tok_v_prev
[0]) == '\0') {
499 /* otherwise discard new line and promote prior */
506 /* look up command */
507 for (c
= command_table_
; c
->name
; c
++) {
508 if (strcasecmp(tok_v
[0], c
->name
) == 0) {
509 if (c
->args_min
> tok_c
- 1) {
510 fprintf(stderr
, "%s: not enough arguments\n", c
->name
);
516 && tok_c
- 1 > c
->args_max
) {
517 fprintf(stderr
, "%s: too many arguments\n", c
->name
);
522 r
= c
->func(vm
, tok_c
, tok_v
);
530 fprintf(stderr
, "didn't recognize '%s'\n", tok_v
[0]);
533 printf("\nfinished\n");