11 #include <readline/readline.h>
16 #include "hw_lem1802.h"
19 * shell-like driver for dcpu16 core
20 * provides a basic interface to control a single emulation instance
22 * Justin Wind <justin.wind@gmail.com>
23 * 2012 04 10 - implementation started
24 * 2012 04 12 - cleanup, better shell loop
25 * 2012 05 12 - support v1.7 style devices
28 * handle quotes in shell command parsing
29 * use readline/history.h, since we're using readline anyhow
30 * ncurses windowing or something, for future display capabilities
33 static const char * const src_id_
= "$Id$";
35 /* global invocation options */
42 /* global run state, first sigint caught will drop out of run loop and back into shell */
43 static volatile unsigned int running_
= 0;
45 void sigint_handler_(int sig
) {
50 #define VERBOSE_PRINTF(...) do { if (opt_.verbose) printf(__VA_ARGS__); } while (0)
53 void usage_(char *prog
, unsigned int full
) {
54 FILE *f
= full
? stdout
: stderr
;
55 char *x
= strrchr(prog
, '/');
61 fprintf(f
, "%s -- dcpu16 emulator core shell\n\n",
64 fprintf(f
, "Usage: %s [-v] [file]\n",
68 fprintf(f
, "\nOptions:\n"
69 "\t [file] -- ram image to load initially\n"
70 "\t -v -- prints slightly more information while operating\n"
71 "\t -h -- this screen\n");
73 fprintf(f
, "\n%78s\n", src_id_
);
78 /* flense a buffer into a newly-allocated argument list */
80 int buf_tok_vect_(char ***v
, int *c
, char *buf
) {
81 const char *sep
= " \t";
82 const char *quot
= "\"'`";
83 const size_t v_grow
= 32;
88 *v
= malloc(v_sz
* sizeof **v
);
90 fprintf(stderr
, "%s():%s\n", "malloc", strerror(errno
));
94 for ( (*v
)[*c
] = strqtok_r(buf
, sep
, '\\', quot
, &qt
, &st
);
96 (*v
)[*c
] = strqtok_r(NULL
, sep
, '\\', quot
, &qt
, &st
)
100 if ((size_t)(*c
) == v_sz
) {
101 void *tmp_ptr
= realloc(*v
, (v_sz
+ v_grow
) * sizeof **v
);
102 if (tmp_ptr
== NULL
) {
103 fprintf(stderr
, "%s():%s\n", "realloc", strerror(errno
));
116 resets the vm if addr is zero then
117 loads an image from filename into ram starting at addr
120 int file_load_(struct dcpu16
*vm
, char *filename
, DCPU16_WORD addr
) {
127 f
= fopen(filename
, "rb");
129 fprintf(stderr
, "%s('%s'):%s\n", "fopen", filename
, strerror(errno
));
133 r
= fread(vm
->ram
+ addr
, sizeof(DCPU16_WORD
), DCPU16_RAM
- addr
, f
);
134 VERBOSE_PRINTF("read %zu words", r
);
135 if (addr
) VERBOSE_PRINTF(" starting at 0x%04x", addr
);
136 VERBOSE_PRINTF("\n");
139 fprintf(stderr
, "%s('%s'):%s\n", "fread", filename
, strerror(errno
));
147 Here follows the various commands the shell can execute.
149 At invocation, a command function will have already had its
150 number of arguments vetted, but will need command-specific
151 argument verifications done.
153 The arg_vector contains the command as the first entry, and
154 as such, arg_count will always be at least 1.
155 However, the args_min and args_max entries in struct command_
156 only refer to the counts of arguments, not the entries in the
164 int (*func
)(struct dcpu16
*, int c
, char **v
);
165 void (*help
)(FILE *f
, unsigned int);
168 #define COMMAND_IMPL(x) static int command_##x##_(struct dcpu16 *vm, int arg_count, char **arg_vector)
169 #define COMMAND_HELP(x) static void command_##x##_help_(FILE *f, unsigned int summary)
170 #define COMMAND_ENTRY(x, y, z) { #x, y, z, command_##x##_, command_##x##_help_ }
174 (void)vm
, (void)arg_count
, (void)arg_vector
;
179 fprintf(f
, "\tquit\n");
182 fprintf(f
, "Exits the emulator.\n");
186 COMMAND_IMPL(reset
) {
187 (void)arg_count
, (void)arg_vector
;
190 printf("initialized\n");
193 COMMAND_HELP(reset
) {
194 fprintf(f
, "\treset\n");
197 fprintf(f
, "Clears and reinitializes emulator.\n");
205 addr
= str_to_word(arg_vector
[2]);
207 fprintf(stderr
, "address '%s' is not a valid word: %s\n", arg_vector
[2], strerror(errno
));
212 if (file_load_(vm
, arg_vector
[1], addr
)) {
213 fprintf(stderr
, "failed to load '%s'\n", arg_vector
[1]);
216 printf("loaded '%s'", arg_vector
[1]);
217 if (addr
) printf(" starting at 0x%04x", addr
);
223 fprintf(f
, "\tload file [addr]\n");
226 fprintf(f
, "Load binary image from 'file' into ram.\n");
234 for (i
= 1; i
< arg_count
; i
++) {
235 addr
[i
-1] = str_to_word(arg_vector
[i
]);
237 fprintf(stderr
, "address '%s' is not a valid word: %s\n", arg_vector
[i
], strerror(errno
));
241 if (arg_count
< 2) addr
[0] = vm
->reg
[DCPU16_REG_PC
];
242 if (arg_count
< 3) addr
[1] = addr
[0];
244 if (addr
[1] < addr
[0]) {
245 fprintf(stderr
, "\t'addr_start' must be before addr_end\n");
249 dcpu16_dump_ram(vm
, addr
[0], addr
[1]);
254 fprintf(f
, "\tdump [addr_start [addr_end]]\n");
257 fprintf(f
, "Displays contents of ram from addr_start to addr_end.\n");
261 COMMAND_IMPL(disassemble
) {
265 for (i
= 1; i
< arg_count
; i
++) {
266 addr
[i
-1] = str_to_word(arg_vector
[i
]);
268 fprintf(stderr
, "address '%s' is not a valid word: %s\n", arg_vector
[i
], strerror(errno
));
272 if (arg_count
< 2) addr
[0] = vm
->reg
[DCPU16_REG_PC
];
273 if (arg_count
< 3) addr
[1] = addr
[0];
275 if (addr
[1] < addr
[0]) {
276 fprintf(stderr
, "\t'addr_start' must be before addr_end\n");
280 for (i
= addr
[0]; i
<= addr
[1]; /* */ ) {
281 printf("0x%04x: ", i
);
282 i
+= dcpu16_disassemble_print(vm
, i
);
288 COMMAND_HELP(disassemble
) {
289 fprintf(f
, "\tdisassemble [addr_start [addr_end]]\n");
292 fprintf(f
, "Displays contents of ram parsed into instructions.\n");
297 unsigned long count
= 1;
300 if (arg_count
== 2) {
302 count
= strtoul(arg_vector
[1], &ep
, 0);
304 || !(*arg_vector
[1] && *ep
== '\0') ) {
305 fprintf(stderr
, "count '%s' is not a valid number: %s\n", arg_vector
[1], strerror(errno
));
310 fprintf(stderr
, "count must be positive\n");
316 dcpu16_disassemble_print(vm
, vm
->reg
[DCPU16_REG_PC
]);
320 if (count
> 1 && opt_
.verbose
)
321 dcpu16_state_print(vm
);
327 fprintf(f
, "\tstep [count]\n");
330 fprintf(f
, "Executes the next instruction, or the next count instructions.\n");
340 /* check if addr is a register */
341 for (addr
= 0; dcpu16_reg_names
[addr
]; addr
++) {
342 if (strcasecmp(arg_vector
[1], dcpu16_reg_names
[addr
]) == 0)
345 if (addr
< DCPU16_REG__NUM
) {
348 addr
= str_to_word(arg_vector
[1]);
350 fprintf(stderr
, "address '%s' is not a valid word: %s\n", arg_vector
[1], strerror(errno
));
356 value
= str_to_word(arg_vector
[2]);
358 fprintf(stderr
, "address '%s' is not a valid word: %s\n", arg_vector
[2], strerror(errno
));
368 fprintf(f
, "\tset addr value\n");
371 fprintf(f
, "Sets addr to value.");
375 struct sigaction act
;
376 (void)arg_count
, (void)arg_vector
;
380 memset(&act
, 0, sizeof act
);
381 act
.sa_handler
= sigint_handler_
;
382 act
.sa_flags
= SA_RESETHAND
;
384 if (sigaction(SIGINT
, &act
, NULL
)) {
385 fprintf(stderr
, "%s():%s\n", "sigaction", strerror(errno
));
391 if (opt_
.verbose
> 1)
392 dcpu16_state_print(vm
);
393 else if (opt_
.verbose
) {
394 dcpu16_disassemble_print(vm
, vm
->reg
[DCPU16_REG_PC
]);
399 printf("interrupted...\n");
404 fprintf(f
, "\trun\n");
407 fprintf(f
, "Begins executing continuously.\n"
408 "May be interrupted with SIGINT.\n");
411 static const char * const display_filename_default_
=
414 #else /* HAVE_LIBPNG */
416 #endif /* HAVE_LIBPNG */
418 COMMAND_IMPL(display
) {
419 struct dcpu16_hw
*hw
= lem1802_new(vm
);
420 const char *renderer
= arg_vector
[1];
421 const char *renderer_arg
= NULL
;
422 void *renderer_data
= NULL
;
425 renderer_arg
= arg_vector
[2];
428 fprintf(stderr
, "failed to initialize new display\n");
432 /* handle per-renderer setup of data.. */
433 /* FIXME: these are awkward */
434 if (strcmp(renderer
, "pnm") == 0) {
435 if (renderer_arg
== NULL
)
436 renderer_arg
= display_filename_default_
;
437 renderer_data
= (void *)renderer_arg
;
441 if (strcmp(renderer
, "png") == 0) {
442 if (renderer_arg
== NULL
)
443 renderer_arg
= display_filename_default_
;
444 renderer_data
= (void *)renderer_arg
;
446 #endif /* HAVE_LIBPNG */
448 #ifdef HAVE_LIBVNCSERVER
449 if (strcmp(renderer
, "vnc") == 0) {
451 char *argv
[] = { "vm-dcpu16", NULL
};
453 renderer_data
= lem1802_vnc_init_data(argc
, argv
, hw
);
455 /* FIXME: keep refs to vnc displays around somewhere, in global list maybe.. */
456 /* keyboards will want to attach to them as well.. */
458 if (renderer_data
== NULL
) {
459 fprintf(stderr
, "failed to initialize vnc\n");
464 #endif /* HAVE_LIBVNCSERVER */
466 if (lem1802_renderer_set(hw
, renderer
, renderer_data
)) {
467 fprintf(stderr
, "failed to set back-end renderer for display\n");
472 if (dcpu16_hw_add(vm
, hw
)) {
473 fprintf(stderr
, "failed to attach new display\n");
480 COMMAND_HELP(display
) {
484 fprintf(f
, "\tdisplay renderer [renderer data]\n");
487 fprintf(f
, "Attaches new display unit, using 'renderer' as back-end output.\n"
490 fprintf(f
, "Supported renderers:\n");
492 while ( (lem1802_renderers_iter(&iter
, &name
, &args
)) ) {
493 fprintf(f
, "\t%s %s\n", name
, args
);
497 /* gather all these together into a searchable table */
499 /* help command gets some assistance in declarations */
503 static struct command_ command_table_
[] = {
504 COMMAND_ENTRY(help
, 0, -1),
505 COMMAND_ENTRY(quit
, 0, -1),
506 COMMAND_ENTRY(load
, 1, 2),
507 COMMAND_ENTRY(dump
, 0, 2),
508 COMMAND_ENTRY(disassemble
, 0, 2),
509 COMMAND_ENTRY(step
, 0, 1),
510 COMMAND_ENTRY(run
, 0, 0),
511 COMMAND_ENTRY(set
, 2, 2),
512 COMMAND_ENTRY(reset
, 0, 0),
513 COMMAND_ENTRY(display
, 1, 2),
514 { NULL
, 0, 0, NULL
, NULL
}
521 if (arg_count
== 2) {
522 for (c
= command_table_
; c
->func
; c
++) {
523 if (strcasecmp(arg_vector
[1], c
->name
) == 0) {
532 for (c
= command_table_
; c
->func
; c
++) {
539 fprintf(f
, "\thelp [command]\n");
542 fprintf(f
, "Displays a list of available commands, or detailed help on a specific command.\n");
546 int main(int argc
, char **argv
) {
547 const char prompt_fmt
[] = "PC:%04x> ";
550 char *line
, *line_prev
;
551 char **tok_v
, **tok_v_prev
;
552 int tok_c
, tok_c_prev
;
555 while ( (c
= getopt(argc
, argv
, "hv")) != EOF
) {
570 if (opt_
.verbose
< 1) {
571 dcpu16_warn_cb_set(NULL
);
572 dcpu16_trace_cb_set(NULL
);
573 } else if (opt_
.verbose
< 2) {
574 dcpu16_trace_cb_set(NULL
);
579 if ((vm
= dcpu16_new()) == NULL
) {
580 fprintf(stderr
, "could not allocate new dcpu16 instance\n");
581 exit(EX_UNAVAILABLE
);
585 if (file_load_(vm
, *argv
, 0)) {
586 fprintf(stderr
, "couldn't load '%s'\n", *argv
);
591 /* show state, read commands */
592 for (line
= line_prev
= NULL
,
593 tok_v
= tok_v_prev
= NULL
,
594 tok_c
= tok_c_prev
= 0,
595 snprintf(prompt
, sizeof prompt
, prompt_fmt
, vm
->reg
[DCPU16_REG_PC
]),
596 dcpu16_state_print(vm
);
598 (line
= readline(prompt
));
601 snprintf(prompt
, sizeof prompt
, prompt_fmt
, vm
->reg
[DCPU16_REG_PC
]),
602 dcpu16_state_print(vm
)) {
603 const char whitespace
[] = " \t";
608 /* skip whitespaces */
609 line_start
= line
+ strspn(line
, whitespace
);
612 /* a new command, remember previous for possible repetition */
614 /* turn new line into new arg array */
615 if (buf_tok_vect_(&tok_v
, &tok_c
, line_start
)) {
616 fprintf(stderr
, "failed to process command\n");
620 /* and keep track if it all for the next time around */
621 if (line_prev
) free(line_prev
);
624 if (tok_v_prev
) free(tok_v_prev
);
628 /* blank new command, but no prior command to repeat? ask again */
629 if (tok_v_prev
== NULL
|| tok_v_prev
[0] == NULL
|| *(tok_v_prev
[0]) == '\0') {
634 /* otherwise discard new line and promote prior */
641 /* look up command */
642 for (c
= command_table_
; c
->name
; c
++) {
643 if (strcasecmp(tok_v
[0], c
->name
) == 0) {
644 if (c
->args_min
> tok_c
- 1) {
645 fprintf(stderr
, "%s: not enough arguments\n", c
->name
);
651 && tok_c
- 1 > c
->args_max
) {
652 fprintf(stderr
, "%s: too many arguments\n", c
->name
);
657 r
= c
->func(vm
, tok_c
, tok_v
);
665 fprintf(stderr
, "didn't recognize '%s'\n", tok_v
[0]);
668 printf("\nfinished\n");