11 #include <readline/readline.h>
18 * shell-like driver for dcpu16 core
19 * provides a basic interface to control a single emulation instance
21 * Justin Wind <justin.wind@gmail.com>
22 * 2012 04 10 - implementation started
23 * 2012 04 12 - cleanup, better shell loop
26 * handle quotes in shell command parsing
27 * use readline/history.h, since we're using readline anyhow
28 * ncurses windowing or something, for future display capabilities
31 static const char * const src_id_
= "$Id$";
33 /* global invocation options */
40 /* global run state, first sigint caught will drop out of run loop and back into shell */
41 static volatile unsigned int running_
= 0;
43 void sigint_handler_(int sig
) {
48 #define VERBOSE_PRINTF(...) do { if (opt_.verbose) printf(__VA_ARGS__); } while (0)
51 void usage_(char *prog
, unsigned int full
) {
52 FILE *f
= full
? stdout
: stderr
;
53 char *x
= strrchr(prog
, '/');
59 fprintf(f
, "%s -- dcpu16 emulator core shell\n\n",
62 fprintf(f
, "Usage: %s [-v] [file]\n",
66 fprintf(f
, "\nOptions:\n"
67 "\t [file] -- ram image to load initially\n"
68 "\t -v -- displays slightly more information\n"
69 "\t -h -- this screen\n");
71 fprintf(f
, "\n%78s\n", src_id_
);
76 /* flense a buffer into a newly-allocated argument list */
78 int buf_tok_vect_(char ***v
, int *c
, char *buf
) {
79 const char *sep
= " \t";
80 const char *quot
= "\"'`";
81 const size_t v_grow
= 32;
86 *v
= malloc(v_sz
* sizeof **v
);
88 fprintf(stderr
, "%s():%s\n", "malloc", strerror(errno
));
92 for ( (*v
)[*c
] = strqtok_r(buf
, sep
, '\\', quot
, &qt
, &st
);
94 (*v
)[*c
] = strqtok_r(NULL
, sep
, '\\', quot
, &qt
, &st
)
98 if ((size_t)(*c
) == v_sz
) {
99 void *tmp_ptr
= realloc(*v
, (v_sz
+ v_grow
) * sizeof **v
);
100 if (tmp_ptr
== NULL
) {
101 fprintf(stderr
, "%s():%s\n", "realloc", strerror(errno
));
114 resets the vm if addr is zero then
115 loads an image from filename into ram starting at addr
118 int file_load_(struct dcpu16
*vm
, char *filename
, DCPU16_WORD addr
) {
125 f
= fopen(filename
, "rb");
127 fprintf(stderr
, "%s('%s'):%s\n", "fopen", filename
, strerror(errno
));
131 r
= fread(vm
->ram
+ addr
, sizeof(DCPU16_WORD
), DCPU16_RAM
- addr
, f
);
132 VERBOSE_PRINTF("read %zu words", r
);
133 if (addr
) VERBOSE_PRINTF(" starting at 0x%04x", addr
);
134 VERBOSE_PRINTF("\n");
137 fprintf(stderr
, "%s('%s'):%s\n", "fread", filename
, strerror(errno
));
144 Here follows the various commands the shell can execute.
146 At invocation, a command function will have already had its
147 number of arguments vetted, but will need command-specific
148 argument verifications done.
150 The arg_vector contains the command as the first entry, and
151 as such, arg_count will always be at least 1.
152 However, the args_min and args_max entries in struct command_
153 only refer to the counts of arguments, not the entries in the
161 int (*func
)(struct dcpu16
*, int c
, char **v
);
162 void (*help
)(FILE *f
, unsigned int);
165 #define COMMAND_IMPL(x) static int command_##x##_(struct dcpu16 *vm, int arg_count, char **arg_vector)
166 #define COMMAND_HELP(x) static void command_##x##_help_(FILE *f, unsigned int summary)
167 #define COMMAND_ENTRY(x, y, z) { #x, y, z, command_##x##_, command_##x##_help_ }
171 (void)vm
, (void)arg_count
, (void)arg_vector
;
176 fprintf(f
, "\tquit\n");
179 fprintf(f
, "Exits the emulator.\n");
183 COMMAND_IMPL(reset
) {
184 (void)arg_count
, (void)arg_vector
;
187 printf("initialized\n");
190 COMMAND_HELP(reset
) {
191 fprintf(f
, "\treset\n");
194 fprintf(f
, "Clears and reinitializes emulator.\n");
202 addr
= str_to_word(arg_vector
[2]);
204 fprintf(stderr
, "address '%s' is not a valid word: %s\n", arg_vector
[2], strerror(errno
));
209 if (file_load_(vm
, arg_vector
[1], addr
)) {
210 fprintf(stderr
, "failed to load '%s'\n", arg_vector
[1]);
213 printf("loaded '%s'", arg_vector
[1]);
214 if (addr
) printf(" starting at 0x%04x", addr
);
220 fprintf(f
, "\tload file [addr]\n");
223 fprintf(f
, "Load binary image from 'file' into ram.\n");
231 for (i
= 1; i
< arg_count
; i
++) {
232 addr
[i
-1] = str_to_word(arg_vector
[i
]);
234 fprintf(stderr
, "address '%s' is not a valid word: %s\n", arg_vector
[i
], strerror(errno
));
238 if (arg_count
< 2) addr
[0] = vm
->pc
;
239 if (arg_count
< 3) addr
[1] = addr
[0];
241 if (addr
[1] < addr
[0]) {
242 fprintf(stderr
, "\t'addr_start' must be before addr_end\n");
246 dcpu16_dump_ram(vm
, addr
[0], addr
[1]);
251 fprintf(f
, "\tdump [addr_start [addr_end]]\n");
254 fprintf(f
, "Displays contents of ram from addr_start to addr_end.\n");
258 COMMAND_IMPL(disassemble
) {
262 for (i
= 1; i
< arg_count
; i
++) {
263 addr
[i
-1] = str_to_word(arg_vector
[i
]);
265 fprintf(stderr
, "address '%s' is not a valid word: %s\n", arg_vector
[i
], strerror(errno
));
269 if (arg_count
< 2) addr
[0] = vm
->pc
;
270 if (arg_count
< 3) addr
[1] = addr
[0];
272 if (addr
[1] < addr
[0]) {
273 fprintf(stderr
, "\t'addr_start' must be before addr_end\n");
277 for (i
= addr
[0]; i
<= addr
[1]; /* */ ) {
278 printf("0x%04x: ", i
);
279 i
+= dcpu16_disassemble_print(vm
, i
);
285 COMMAND_HELP(disassemble
) {
286 fprintf(f
, "\tdisassemble [addr_start [addr_end]]\n");
289 fprintf(f
, "Displays contents of ram parsed into instructions.\n");
294 unsigned long count
= 1;
297 if (arg_count
== 2) {
299 count
= strtoul(arg_vector
[1], &ep
, 0);
301 || !(*arg_vector
[0] && *ep
== '\0') ) {
302 fprintf(stderr
, "count '%s' is not a valid number: %s\n", arg_vector
[1], strerror(errno
));
307 fprintf(stderr
, "count must be positive\n");
313 dcpu16_disassemble_print(vm
, vm
->pc
);
317 if (count
> 1 && opt_
.verbose
)
318 dcpu16_state_print(vm
);
324 fprintf(f
, "\tstep [count]\n");
327 fprintf(f
, "Executes the next instruction, or the next count instructions.\n");
332 struct sigaction act
;
333 (void)arg_count
, (void)arg_vector
;
337 memset(&act
, 0, sizeof act
);
338 act
.sa_handler
= sigint_handler_
;
339 act
.sa_flags
= SA_RESETHAND
;
341 if (sigaction(SIGINT
, &act
, NULL
)) {
342 fprintf(stderr
, "%s():%s\n", "sigaction", strerror(errno
));
348 if (opt_
.verbose
> 1)
349 dcpu16_state_print(vm
);
350 else if (opt_
.verbose
) {
351 dcpu16_disassemble_print(vm
, vm
->pc
);
356 printf("interrupted...\n");
361 fprintf(f
, "\trun\n");
364 fprintf(f
, "Begins executing continuously.\n"
365 "May be interrupted with SIGINT.\n");
368 static const char * const display_filename_default_
=
371 #else /* HAVE_LIBPNG */
373 #endif /* HAVE_LIBPNG */
375 COMMAND_IMPL(display
) {
376 static DCPU16_DISPLAY
*display
= NULL
;
377 const char *filename
= display_filename_default_
;
379 if (arg_count
== 2) {
380 filename
= arg_vector
[1];
384 printf("display already enabled..\n");
388 display
= display_new(filename
);
390 if (display
== NULL
) {
391 fprintf(stderr
, "failed to initialize display buffer\n");
395 if (dcpu16_acct_add(vm
, DCPU16_ACCT_EV_WRITE
, display_fn
, display
)) {
396 fprintf(stderr
, "failed to register display update callback\n");
400 if (dcpu16_acct_add(vm
, DCPU16_ACCT_EV_RESET
, display_reset_fn
, display
)) {
401 fprintf(stderr
, "failed to register display reset callback\n");
405 if (dcpu16_acct_add(vm
, DCPU16_ACCT_EV_CYCLE
, display_cycle_fn
, display
)) {
406 fprintf(stderr
, "failed to register display cycle callback\n");
410 /* init display as if reset occurred */
411 display_reset_fn(vm
, DCPU16_ACCT_EV_RESET
, 0, display
);
415 COMMAND_HELP(display
) {
416 fprintf(f
, "\tdisplay [file]\n");
419 fprintf(f
, "Attaches display interface, begins updating an image file of display contents...\n"
420 "Image filename may be specified, defaults to '%s'\n",
421 display_filename_default_
425 /* gather all these together into a searchable table */
427 /* help command gets some assistance in declarations */
431 static struct command_ command_table_
[] = {
432 COMMAND_ENTRY(help
, 0, -1),
433 COMMAND_ENTRY(quit
, 0, -1),
434 COMMAND_ENTRY(load
, 1, 2),
435 COMMAND_ENTRY(dump
, 0, 2),
436 COMMAND_ENTRY(disassemble
, 0, 2),
437 COMMAND_ENTRY(step
, 0, 1),
438 COMMAND_ENTRY(run
, 0, 0),
439 COMMAND_ENTRY(reset
, 0, 0),
440 COMMAND_ENTRY(display
, 0, 1),
441 { NULL
, 0, 0, NULL
, NULL
}
448 if (arg_count
== 2) {
449 for (c
= command_table_
; c
->func
; c
++) {
450 if (strcasecmp(arg_vector
[1], c
->name
) == 0) {
459 for (c
= command_table_
; c
->func
; c
++) {
466 fprintf(f
, "\thelp [command]\n");
469 fprintf(f
, "Displays a list of available commands, or detailed help on a specific command.\n");
473 int main(int argc
, char **argv
) {
474 const char prompt_fmt
[] = "PC:%04x> ";
477 char *line
, *line_prev
;
478 char **tok_v
, **tok_v_prev
;
479 int tok_c
, tok_c_prev
;
482 while ( (c
= getopt(argc
, argv
, "hv")) != EOF
) {
497 if (opt_
.verbose
< 1) {
498 dcpu16_warn_cb_set(NULL
);
499 dcpu16_trace_cb_set(NULL
);
500 } else if (opt_
.verbose
< 2) {
501 dcpu16_trace_cb_set(NULL
);
506 if ((vm
= dcpu16_new()) == NULL
) {
507 fprintf(stderr
, "could not allocate new dcpu16 instance\n");
508 exit(EX_UNAVAILABLE
);
512 if (file_load_(vm
, *argv
, 0)) {
513 fprintf(stderr
, "couldn't load '%s'\n", *argv
);
518 /* show state, read commands */
519 for (line
= line_prev
= NULL
,
520 tok_v
= tok_v_prev
= NULL
,
521 tok_c
= tok_c_prev
= 0,
522 snprintf(prompt
, sizeof prompt
, prompt_fmt
, vm
->pc
),
523 dcpu16_state_print(vm
);
525 (line
= readline(prompt
));
528 snprintf(prompt
, sizeof prompt
, prompt_fmt
, vm
->pc
),
529 dcpu16_state_print(vm
)) {
530 const char whitespace
[] = " \t";
535 /* skip whitespaces */
536 line_start
= line
+ strspn(line
, whitespace
);
539 /* a new command, remember previous for possible repetition */
541 /* turn new line into new arg array */
542 if (buf_tok_vect_(&tok_v
, &tok_c
, line_start
)) {
543 fprintf(stderr
, "failed to process command\n");
547 /* and keep track if it all for the next time around */
548 if (line_prev
) free(line_prev
);
551 if (tok_v_prev
) free(tok_v_prev
);
555 /* blank new command, but no prior command to repeat? ask again */
556 if (tok_v_prev
== NULL
|| tok_v_prev
[0] == NULL
|| *(tok_v_prev
[0]) == '\0') {
561 /* otherwise discard new line and promote prior */
568 /* look up command */
569 for (c
= command_table_
; c
->name
; c
++) {
570 if (strcasecmp(tok_v
[0], c
->name
) == 0) {
571 if (c
->args_min
> tok_c
- 1) {
572 fprintf(stderr
, "%s: not enough arguments\n", c
->name
);
578 && tok_c
- 1 > c
->args_max
) {
579 fprintf(stderr
, "%s: too many arguments\n", c
->name
);
584 r
= c
->func(vm
, tok_c
, tok_v
);
592 fprintf(stderr
, "didn't recognize '%s'\n", tok_v
[0]);
595 printf("\nfinished\n");