+/*
+ Here follows the various commands the shell can execute.
+
+ At invocation, a command function will have already had its
+ number of arguments vetted, but will need command-specific
+ argument verifications done.
+
+ The arg_vector contains the command as the first entry, and
+ as such, arg_count will always be at least 1.
+ However, the args_min and args_max entries in struct command_
+ only refer to the counts of arguments, not the entries in the
+ argv.
+*/
+
+struct command_ {
+ char *name;
+ int args_min;
+ int args_max;
+ int (*func)(struct dcpu16 *, int c, char **v);
+ void (*help)(FILE *f, unsigned int);
+};
+
+#define COMMAND_IMPL(x) static int command_##x##_(struct dcpu16 *vm, int arg_count, char **arg_vector)
+#define COMMAND_HELP(x) static void command_##x##_help_(FILE *f, unsigned int summary)
+#define COMMAND_ENTRY(x, y, z) { #x, y, z, command_##x##_, command_##x##_help_ }
+
+
+COMMAND_IMPL(quit) {
+ (void)vm, (void)arg_count, (void)arg_vector;
+
+ return -1;
+}
+COMMAND_HELP(quit) {
+ fprintf(f, "\tquit\n");
+ if (summary) return;
+
+ fprintf(f, "Exits the emulator.\n");
+}
+
+
+COMMAND_IMPL(reset) {
+ (void)arg_count, (void)arg_vector;
+
+ dcpu16_reset(vm);
+ printf("initialized\n");
+ return 0;
+}
+COMMAND_HELP(reset) {
+ fprintf(f, "\treset\n");
+ if (summary) return;
+
+ fprintf(f, "Clears and reinitializes emulator.\n");
+}
+
+
+COMMAND_IMPL(load) {
+ int addr = 0;
+
+ if (arg_count > 2) {
+ addr = str_to_word(arg_vector[2]);
+ if (addr < 0) {
+ fprintf(stderr, "address '%s' is not a valid word: %s\n", arg_vector[2], strerror(errno));
+ return 0;
+ }
+ }
+
+ if (file_load_(vm, arg_vector[1], addr)) {
+ fprintf(stderr, "failed to load '%s'\n", arg_vector[1]);
+ return 0;
+ }
+ printf("loaded '%s'", arg_vector[1]);
+ if (addr) printf(" starting at 0x%04x", addr);
+ printf("\n");
+
+ return 0;
+}
+COMMAND_HELP(load) {
+ fprintf(f, "\tload file [addr]\n");
+ if (summary) return;
+
+ fprintf(f, "Load binary image from 'file' into ram.\n");
+}
+
+
+COMMAND_IMPL(dump) {
+ int addr[2];
+ int i;
+
+ for (i = 1; i < arg_count; i++) {
+ addr[i-1] = str_to_word(arg_vector[i]);
+ if (addr[i-1] < 0) {
+ fprintf(stderr, "address '%s' is not a valid word: %s\n", arg_vector[i], strerror(errno));
+ return 0;
+ }
+ }
+ if (arg_count < 2) addr[0] = vm->pc;
+ if (arg_count < 3) addr[1] = addr[0];
+
+ if (addr[1] < addr[0]) {
+ fprintf(stderr, "\t'addr_start' must be before addr_end\n");
+ return 0;
+ }
+
+ dcpu16_dump_ram(vm, addr[0], addr[1]);
+
+ return 0;
+}
+COMMAND_HELP(dump) {
+ fprintf(f, "\tdump [addr_start [addr_end]]\n");
+ if (summary) return;
+
+ fprintf(f, "Displays contents of ram from addr_start to addr_end.\n");
+}
+
+
+COMMAND_IMPL(disassemble) {
+ int addr[2];
+ int i;
+
+ for (i = 1; i < arg_count; i++) {
+ addr[i-1] = str_to_word(arg_vector[i]);
+ if (addr[i-1] < 0) {
+ fprintf(stderr, "address '%s' is not a valid word: %s\n", arg_vector[i], strerror(errno));
+ return 0;
+ }
+ }
+ if (arg_count < 2) addr[0] = vm->pc;
+ if (arg_count < 3) addr[1] = addr[0];
+
+ if (addr[1] < addr[0]) {
+ fprintf(stderr, "\t'addr_start' must be before addr_end\n");
+ return 0;
+ }
+
+ for (i = addr[0]; i <= addr[1]; /* */ ) {
+ printf("0x%04x: ", i);
+ i += dcpu16_disassemble_print(vm, i);
+ printf("\n");
+ }
+
+ return 0;
+}
+COMMAND_HELP(disassemble) {
+ fprintf(f, "\tdisassemble [addr_start [addr_end]]\n");
+ if (summary) return;
+
+ fprintf(f, "Displays contents of ram parsed into instructions.\n");
+}
+
+
+COMMAND_IMPL(step) {
+ unsigned long count = 1;
+ char *ep;
+
+ if (arg_count == 2) {
+ errno = 0;
+ count = strtoul(arg_vector[1], &ep, 0);
+ if (errno
+ || !(*arg_vector[0] && *ep == '\0') ) {
+ fprintf(stderr, "count '%s' is not a valid number: %s\n", arg_vector[1], strerror(errno));
+ return 0;
+ }
+
+ if (count <= 0) {
+ fprintf(stderr, "count must be positive\n");
+ return 0;
+ }
+ }
+
+ while (count--) {
+ dcpu16_disassemble_print(vm, vm->pc);
+ printf("\n");
+ dcpu16_step(vm);
+
+ if (count > 1 && opt_.verbose)
+ dcpu16_state_print(vm);
+ }
+
+ return 0;
+}
+COMMAND_HELP(step) {
+ fprintf(f, "\tstep [count]\n");
+ if (summary) return;
+
+ fprintf(f, "Executes the next instruction, or the next count instructions.\n");
+}
+
+
+COMMAND_IMPL(run) {
+ struct sigaction act;
+ (void)arg_count, (void)arg_vector;
+
+ running_ = 1;
+
+ memset(&act, 0, sizeof act);
+ act.sa_handler = sigint_handler_;
+ act.sa_flags = SA_RESETHAND;
+
+ if (sigaction(SIGINT, &act, NULL)) {
+ fprintf(stderr, "%s():%s\n", "sigaction", strerror(errno));
+ return -1;
+ }
+
+ while(running_) {
+ dcpu16_step(vm);
+ if (opt_.verbose > 1)
+ dcpu16_state_print(vm);
+ else if (opt_.verbose) {
+ dcpu16_disassemble_print(vm, vm->pc);
+ printf("\n");
+ }
+ }
+
+ printf("interrupted...\n");
+
+ return 0;
+}
+COMMAND_HELP(run) {
+ fprintf(f, "\trun\n");
+ if (summary) return;
+
+ fprintf(f, "Begins executing continuously.\n"
+ "May be interrupted with SIGINT.\n");
+}
+
+static const char * const display_filename_default_ =
+#ifdef HAVE_LIBPNG
+ "dcpu16-display.png"
+#else /* HAVE_LIBPNG */
+ "dcpu16-display.pnm"
+#endif /* HAVE_LIBPNG */
+;
+COMMAND_IMPL(display) {
+ static DCPU16_DISPLAY *display = NULL;
+ const char *filename = display_filename_default_;
+
+ if (arg_count == 2) {
+ filename = arg_vector[1];
+ }
+
+ if (display) {
+ printf("display already enabled..\n");
+ return 0;
+ }
+
+ display = display_new(filename);
+
+ if (display == NULL) {
+ fprintf(stderr, "failed to initialize display buffer\n");
+ return 0;
+ }
+
+ if (dcpu16_acct_add(vm, DCPU16_ACCT_EV_WRITE, display_fn, display)) {
+ fprintf(stderr, "failed to register display update callback\n");
+ return 0;
+ }
+
+ if (dcpu16_acct_add(vm, DCPU16_ACCT_EV_RESET, display_reset_fn, display)) {
+ fprintf(stderr, "failed to register display reset callback\n");
+ return 0;
+ }
+
+ if (dcpu16_acct_add(vm, DCPU16_ACCT_EV_CYCLE, display_cycle_fn, display)) {
+ fprintf(stderr, "failed to register display cycle callback\n");
+ return 0;
+ }
+
+ /* init display as if reset occurred */
+ display_reset_fn(vm, DCPU16_ACCT_EV_RESET, 0, display);
+
+ return 0;
+}
+COMMAND_HELP(display) {
+ fprintf(f, "\tdisplay [file]\n");
+ if (summary) return;
+
+ fprintf(f, "Attaches display interface, begins updating an image file of display contents...\n"
+ "Image filename may be specified, defaults to '%s'\n",
+ display_filename_default_
+ );
+}
+
+/* gather all these together into a searchable table */
+
+/* help command gets some assistance in declarations */
+COMMAND_IMPL(help);
+COMMAND_HELP(help);
+
+static struct command_ command_table_[] = {
+ COMMAND_ENTRY(help, 0, -1),
+ COMMAND_ENTRY(quit, 0, -1),
+ COMMAND_ENTRY(load, 1, 2),
+ COMMAND_ENTRY(dump, 0, 2),
+ COMMAND_ENTRY(disassemble, 0, 2),
+ COMMAND_ENTRY(step, 0, 1),
+ COMMAND_ENTRY(run, 0, 0),
+ COMMAND_ENTRY(reset, 0, 0),
+ COMMAND_ENTRY(display, 0, 1),
+ { NULL, 0, 0, NULL, NULL }
+};
+
+COMMAND_IMPL(help) {
+ struct command_ *c;
+ (void)vm;
+
+ if (arg_count == 2) {
+ for (c = command_table_; c->func; c++) {
+ if (strcasecmp(arg_vector[1], c->name) == 0) {
+ if (c->help)
+ c->help(stdout, 0);
+ break;
+ }
+ }
+ return 0;
+ }
+
+ for (c = command_table_; c->func; c++) {
+ if (c->help)
+ c->help(stdout, 1);
+ }
+ return 0;
+}
+COMMAND_HELP(help) {
+ fprintf(f, "\thelp [command]\n");
+ if (summary) return;
+
+ fprintf(f, "Displays a list of available commands, or detailed help on a specific command.\n");
+}
+
+