11 #include <readline/readline.h>
17 * shell-like driver for dcpu16 core
18 * provides a basic interface to control a single emulation instance
20 * Justin Wind <justin.wind@gmail.com>
21 * 2012 04 10 - implementation started
22 * 2012 04 12 - cleanup, better shell loop
25 * handle quotes in shell command parsing
26 * use readline/history.h, since we're using readline anyhow
27 * ncurses windowing or something, for future display capabilities
30 static const char * const src_id_
= "$Id$";
32 /* global invocation options */
39 /* global run state, first sigint caught will drop out of run loop and back into shell */
40 static volatile unsigned int running_
= 0;
42 void sigint_handler_(int sig
) {
47 #define VERBOSE_PRINTF(...) do { if (opt_.verbose) printf(__VA_ARGS__); } while (0)
50 void usage_(char *prog
, unsigned int full
) {
51 FILE *f
= full
? stdout
: stderr
;
52 char *x
= strrchr(prog
, '/');
58 fprintf(f
, "%s -- dcpu16 emulator core shell\n\n",
61 fprintf(f
, "Usage: %s [-v] [file]\n",
65 fprintf(f
, "\nOptions:\n"
66 "\t [file] -- ram image to load initially\n"
67 "\t -v -- displays slightly more information\n"
68 "\t -h -- this screen\n");
70 fprintf(f
, "\n%78s\n", src_id_
);
75 /* flense a buffer into a newly-allocated argument list */
77 int buf_tok_vect_(char ***v
, int *c
, char *buf
) {
78 const char *sep
= " \t";
79 const char *quot
= "\"'`";
80 const size_t v_grow
= 32;
85 *v
= malloc(v_sz
* sizeof **v
);
87 fprintf(stderr
, "%s():%s\n", "malloc", strerror(errno
));
91 for ( (*v
)[*c
] = strqtok_r(buf
, sep
, '\\', quot
, &qt
, &st
);
93 (*v
)[*c
] = strqtok_r(NULL
, sep
, '\\', quot
, &qt
, &st
)
97 if ((size_t)(*c
) == v_sz
) {
98 void *tmp_ptr
= realloc(*v
, (v_sz
+ v_grow
) * sizeof **v
);
99 if (tmp_ptr
== NULL
) {
100 fprintf(stderr
, "%s():%s\n", "realloc", strerror(errno
));
113 resets the vm if addr is zero then
114 loads an image from filename into ram starting at addr
117 int file_load_(struct dcpu16
*vm
, char *filename
, DCPU16_WORD addr
) {
124 f
= fopen(filename
, "rb");
126 fprintf(stderr
, "%s('%s'):%s\n", "fopen", filename
, strerror(errno
));
130 r
= fread(vm
->ram
+ addr
, sizeof(DCPU16_WORD
), DCPU16_RAM
- addr
, f
);
131 VERBOSE_PRINTF("read %zu words", r
);
132 if (addr
) VERBOSE_PRINTF(" starting at 0x%04x", addr
);
133 VERBOSE_PRINTF("\n");
136 fprintf(stderr
, "%s('%s'):%s\n", "fread", filename
, strerror(errno
));
143 Here follows the various commands the shell can execute.
145 At invocation, a command function will have already had its
146 number of arguments vetted, but will need command-specific
147 argument verifications done.
149 The arg_vector contains the command as the first entry, and
150 as such, arg_count will always be at least 1.
151 However, the args_min and args_max entries in struct command_
152 only refer to the counts of arguments, not the entries in the
160 int (*func
)(struct dcpu16
*, int c
, char **v
);
161 void (*help
)(FILE *f
, unsigned int);
164 #define COMMAND_IMPL(x) static int command_##x##_(struct dcpu16 *vm, int arg_count, char **arg_vector)
165 #define COMMAND_HELP(x) static void command_##x##_help_(FILE *f, unsigned int summary)
166 #define COMMAND_ENTRY(x, y, z) { #x, y, z, command_##x##_, command_##x##_help_ }
170 (void)vm
, (void)arg_count
, (void)arg_vector
;
175 fprintf(f
, "\tquit\n");
178 fprintf(f
, "Exits the emulator.\n");
182 COMMAND_IMPL(reset
) {
183 (void)arg_count
, (void)arg_vector
;
186 printf("initialized\n");
189 COMMAND_HELP(reset
) {
190 fprintf(f
, "\treset\n");
193 fprintf(f
, "Clears and reinitializes emulator.\n");
201 addr
= str_to_word(arg_vector
[2]);
203 fprintf(stderr
, "address '%s' is not a valid word: %s\n", arg_vector
[2], strerror(errno
));
208 if (file_load_(vm
, arg_vector
[1], addr
)) {
209 fprintf(stderr
, "failed to load '%s'\n", arg_vector
[1]);
212 printf("loaded '%s'", arg_vector
[1]);
213 if (addr
) printf(" starting at 0x%04x", addr
);
219 fprintf(f
, "\tload file [addr]\n");
222 fprintf(f
, "Load binary image from 'file' into ram.\n");
230 for (i
= 1; i
< arg_count
; i
++) {
231 addr
[i
-1] = str_to_word(arg_vector
[i
]);
233 fprintf(stderr
, "address '%s' is not a valid word: %s\n", arg_vector
[i
], strerror(errno
));
237 if (arg_count
< 2) addr
[0] = vm
->pc
;
238 if (arg_count
< 3) addr
[1] = addr
[0];
240 if (addr
[1] < addr
[0]) {
241 fprintf(stderr
, "\t'addr_start' must be before addr_end\n");
245 dcpu16_dump_ram(vm
, addr
[0], addr
[1]);
250 fprintf(f
, "\tdump [addr_start [addr_end]]\n");
253 fprintf(f
, "Displays contents of ram from addr_start to addr_end.\n");
257 COMMAND_IMPL(disassemble
) {
261 for (i
= 1; i
< arg_count
; i
++) {
262 addr
[i
-1] = str_to_word(arg_vector
[i
]);
264 fprintf(stderr
, "address '%s' is not a valid word: %s\n", arg_vector
[i
], strerror(errno
));
268 if (arg_count
< 2) addr
[0] = vm
->pc
;
269 if (arg_count
< 3) addr
[1] = addr
[0];
271 if (addr
[1] < addr
[0]) {
272 fprintf(stderr
, "\t'addr_start' must be before addr_end\n");
276 for (i
= addr
[0]; i
<= addr
[1]; /* */ ) {
277 printf("0x%04x: ", i
);
278 i
+= dcpu16_disassemble_print(vm
, i
);
284 COMMAND_HELP(disassemble
) {
285 fprintf(f
, "\tdisassemble [addr_start [addr_end]]\n");
288 fprintf(f
, "Displays contents of ram parsed into instructions.\n");
293 unsigned long count
= 1;
296 if (arg_count
== 2) {
298 count
= strtoul(arg_vector
[1], &ep
, 0);
300 || !(*arg_vector
[0] && *ep
== '\0') ) {
301 fprintf(stderr
, "count '%s' is not a valid number: %s\n", arg_vector
[1], strerror(errno
));
306 fprintf(stderr
, "count must be positive\n");
312 dcpu16_disassemble_print(vm
, vm
->pc
);
316 if (count
> 1 && opt_
.verbose
)
317 dcpu16_state_print(vm
);
323 fprintf(f
, "\tstep [count]\n");
326 fprintf(f
, "Executes the next instruction, or the next count instructions.\n");
331 struct sigaction act
;
332 (void)arg_count
, (void)arg_vector
;
336 memset(&act
, 0, sizeof act
);
337 act
.sa_handler
= sigint_handler_
;
338 act
.sa_flags
= SA_RESETHAND
;
340 if (sigaction(SIGINT
, &act
, NULL
)) {
341 fprintf(stderr
, "%s():%s\n", "sigaction", strerror(errno
));
347 if (opt_
.verbose
> 1)
348 dcpu16_state_print(vm
);
349 else if (opt_
.verbose
) {
350 dcpu16_disassemble_print(vm
, vm
->pc
);
355 printf("interrupted...\n");
360 fprintf(f
, "\trun\n");
363 fprintf(f
, "Begins executing continuously.\n"
364 "May be interrupted with SIGINT.\n");
367 /* gather all these together into a searchable table */
369 /* help command gets some assistance in declarations */
373 static struct command_ command_table_
[] = {
374 COMMAND_ENTRY(help
, 0, -1),
375 COMMAND_ENTRY(quit
, 0, -1),
376 COMMAND_ENTRY(load
, 1, 2),
377 COMMAND_ENTRY(dump
, 0, 2),
378 COMMAND_ENTRY(disassemble
, 0, 2),
379 COMMAND_ENTRY(step
, 0, 1),
380 COMMAND_ENTRY(run
, 0, 0),
381 COMMAND_ENTRY(reset
, 0, 0),
382 { NULL
, 0, 0, NULL
, NULL
}
389 if (arg_count
== 2) {
390 for (c
= command_table_
; c
->func
; c
++) {
391 if (strcasecmp(arg_vector
[1], c
->name
) == 0) {
400 for (c
= command_table_
; c
->func
; c
++) {
407 fprintf(f
, "\thelp [command]\n");
410 fprintf(f
, "Displays a list of available commands, or detailed help on a specific command.\n");
414 int main(int argc
, char **argv
) {
415 const char prompt_fmt
[] = "PC:%04x> ";
418 char *line
, *line_prev
;
419 char **tok_v
, **tok_v_prev
;
420 int tok_c
, tok_c_prev
;
423 while ( (c
= getopt(argc
, argv
, "hv")) != EOF
) {
438 if (opt_
.verbose
< 1) {
439 dcpu16_warn_cb_set(NULL
);
440 dcpu16_trace_cb_set(NULL
);
441 } else if (opt_
.verbose
< 2) {
442 dcpu16_trace_cb_set(NULL
);
447 if ((vm
= dcpu16_new()) == NULL
) {
448 fprintf(stderr
, "could not allocate new dcpu16 instance\n");
449 exit(EX_UNAVAILABLE
);
453 if (file_load_(vm
, *argv
, 0)) {
454 fprintf(stderr
, "couldn't load '%s'\n", *argv
);
459 /* show state, read commands */
460 for (line
= line_prev
= NULL
,
461 tok_v
= tok_v_prev
= NULL
,
462 tok_c
= tok_c_prev
= 0,
463 snprintf(prompt
, sizeof prompt
, prompt_fmt
, vm
->pc
),
464 dcpu16_state_print(vm
);
466 (line
= readline(prompt
));
469 snprintf(prompt
, sizeof prompt
, prompt_fmt
, vm
->pc
),
470 dcpu16_state_print(vm
)) {
471 const char whitespace
[] = " \t";
476 /* skip whitespaces */
477 line_start
= line
+ strspn(line
, whitespace
);
480 /* a new command, remember previous for possible repetition */
482 /* turn new line into new arg array */
483 if (buf_tok_vect_(&tok_v
, &tok_c
, line_start
)) {
484 fprintf(stderr
, "failed to process command\n");
488 /* and keep track if it all for the next time around */
489 if (line_prev
) free(line_prev
);
492 if (tok_v_prev
) free(tok_v_prev
);
496 /* blank new command, but no prior command to repeat? ask again */
497 if (tok_v_prev
== NULL
|| tok_v_prev
[0] == NULL
|| *(tok_v_prev
[0]) == '\0') {
502 /* otherwise discard new line and promote prior */
509 /* look up command */
510 for (c
= command_table_
; c
->name
; c
++) {
511 if (strcasecmp(tok_v
[0], c
->name
) == 0) {
512 if (c
->args_min
> tok_c
- 1) {
513 fprintf(stderr
, "%s: not enough arguments\n", c
->name
);
519 && tok_c
- 1 > c
->args_max
) {
520 fprintf(stderr
, "%s: too many arguments\n", c
->name
);
525 r
= c
->func(vm
, tok_c
, tok_v
);
533 fprintf(stderr
, "didn't recognize '%s'\n", tok_v
[0]);
536 printf("\nfinished\n");