X-Git-Url: http://git.squeep.com/?p=dcpu16;a=blobdiff_plain;f=vm-dcpu16.c;h=6324717f0b28f9e23d1f879abe6b568fa353cd8f;hp=df86455070e7c91b008456579eeb2360b0425959;hb=0a6b0889c67675bd71681ec41774d2ea57ce5335;hpb=df965ac8eeee9115d8f8c7e35e8a470b315d3fb4 diff --git a/vm-dcpu16.c b/vm-dcpu16.c index df86455..6324717 100644 --- a/vm-dcpu16.c +++ b/vm-dcpu16.c @@ -2,6 +2,8 @@ #include #include #include +#include +#include #include #include #include @@ -9,6 +11,9 @@ #include #include "dcpu16.h" +#include "common.h" + +#include "hw_lem1802.h" /* * shell-like driver for dcpu16 core @@ -17,6 +22,7 @@ * Justin Wind * 2012 04 10 - implementation started * 2012 04 12 - cleanup, better shell loop + * 2012 05 12 - support v1.7 style devices * * TODO * handle quotes in shell command parsing @@ -61,47 +67,22 @@ void usage_(char *prog, unsigned int full) { if (full) { fprintf(f, "\nOptions:\n" "\t [file] -- ram image to load initially\n" - "\t -v -- verbose execution tracing\n" + "\t -v -- prints slightly more information while operating\n" "\t -h -- this screen\n"); fprintf(f, "\n%78s\n", src_id_); } } -/* simplified strtoul with range checking */ -static -int str_to_word_(char *s) { - unsigned long l; - char *ep; - - assert(s); - - errno = 0; - l = strtoul(s, &ep, 0); - - if (errno - || !(*s && *ep == '\0') ) { - /* out of range of conversion, or invalid character encountered */ - return -1; - } - - if (l >= DCPU16_RAM) { - /* out of range for our needs */ - errno = ERANGE; - return -1; - } - - return l; -} /* flense a buffer into a newly-allocated argument list */ -/* FIXME: handle quotes */ static int buf_tok_vect_(char ***v, int *c, char *buf) { const char *sep = " \t"; + const char *quot = "\"'`"; const size_t v_grow = 32; size_t v_sz = 32; - char *st; + char *st, *qt; *c = 0; *v = malloc(v_sz * sizeof **v); @@ -110,9 +91,9 @@ int buf_tok_vect_(char ***v, int *c, char *buf) { return -1; } - for ( (*v)[*c] = strtok_r(buf, sep, &st); + for ( (*v)[*c] = strqtok_r(buf, sep, '\\', quot, &qt, &st); (*v)[*c]; - (*v)[*c] = strtok_r(NULL, sep, &st) + (*v)[*c] = strqtok_r(NULL, sep, '\\', quot, &qt, &st) ) { (*c)++; @@ -161,6 +142,7 @@ int file_load_(struct dcpu16 *vm, char *filename, DCPU16_WORD addr) { return 0; } + /* Here follows the various commands the shell can execute. @@ -190,7 +172,7 @@ struct command_ { COMMAND_IMPL(quit) { (void)vm, (void)arg_count, (void)arg_vector; - VERBOSE_PRINTF("done\n"); + return -1; } COMMAND_HELP(quit) { @@ -220,7 +202,7 @@ COMMAND_IMPL(load) { int addr = 0; if (arg_count > 2) { - addr = str_to_word_(arg_vector[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; @@ -250,13 +232,13 @@ COMMAND_IMPL(dump) { int i; for (i = 1; i < arg_count; i++) { - addr[i-1] = str_to_word_(arg_vector[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 < 2) addr[0] = vm->reg[DCPU16_REG_PC]; if (arg_count < 3) addr[1] = addr[0]; if (addr[1] < addr[0]) { @@ -281,13 +263,13 @@ COMMAND_IMPL(disassemble) { int i; for (i = 1; i < arg_count; i++) { - addr[i-1] = str_to_word_(arg_vector[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 < 2) addr[0] = vm->reg[DCPU16_REG_PC]; if (arg_count < 3) addr[1] = addr[0]; if (addr[1] < addr[0]) { @@ -319,7 +301,7 @@ COMMAND_IMPL(step) { errno = 0; count = strtoul(arg_vector[1], &ep, 0); if (errno - || !(*arg_vector[0] && *ep == '\0') ) { + || !(*arg_vector[1] && *ep == '\0') ) { fprintf(stderr, "count '%s' is not a valid number: %s\n", arg_vector[1], strerror(errno)); return 0; } @@ -331,12 +313,11 @@ COMMAND_IMPL(step) { } while (count--) { - VERBOSE_PRINTF("executing next cycle, instruction: "); - dcpu16_disassemble_print(vm, vm->pc), printf("\n"); - + dcpu16_disassemble_print(vm, vm->reg[DCPU16_REG_PC]); + printf("\n"); dcpu16_step(vm); - if (opt_.verbose) + if (count > 1 && opt_.verbose) dcpu16_state_print(vm); } @@ -350,31 +331,72 @@ COMMAND_HELP(step) { } +COMMAND_IMPL(set) { + int addr, value; + DCPU16_WORD *v; + + (void)arg_count; + + /* check if addr is a register */ + for (addr = 0; dcpu16_reg_names[addr]; addr++) { + if (strcasecmp(arg_vector[1], dcpu16_reg_names[addr]) == 0) + break; + } + if (addr < DCPU16_REG__NUM) { + v = vm->reg + addr; + } else { + addr = str_to_word(arg_vector[1]); + if (addr < 0) { + fprintf(stderr, "address '%s' is not a valid word: %s\n", arg_vector[1], strerror(errno)); + return 0; + } + v = vm->ram + addr; + } + + value = str_to_word(arg_vector[2]); + if (value < 0) { + fprintf(stderr, "address '%s' is not a valid word: %s\n", arg_vector[2], strerror(errno)); + return 0; + } + + *v = value; + + return 0; +} + +COMMAND_HELP(set) { + fprintf(f, "\tset addr value\n"); + if (summary) return; + + fprintf(f, "Sets addr to value."); +} + COMMAND_IMPL(run) { - sig_t osig; + struct sigaction act; (void)arg_count, (void)arg_vector; running_ = 1; - /* install our new interrupt signal handler */ - if ( (osig = signal(SIGINT, sigint_handler_)) == SIG_ERR ) { - fprintf(stderr, "%s():%s\n", "signal", strerror(errno)); + 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) + if (opt_.verbose > 1) dcpu16_state_print(vm); + else if (opt_.verbose) { + dcpu16_disassemble_print(vm, vm->reg[DCPU16_REG_PC]); + printf("\n"); + } } - /* restore the old interrupt signal handler */ - if (signal(SIGINT, osig) == SIG_ERR) { - fprintf(stderr, "%s():%s\n", "signal", strerror(errno)); - return -1; - } - - VERBOSE_PRINTF("interrupted...\n"); + printf("interrupted...\n"); return 0; } @@ -382,7 +404,94 @@ COMMAND_HELP(run) { fprintf(f, "\trun\n"); if (summary) return; - fprintf(f, "Begins executing continuously.\n"); + 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) { + struct dcpu16_hw *hw = lem1802_new(vm); + const char *renderer = arg_vector[1]; + const char *renderer_arg = NULL; + void *renderer_data = NULL; + + if (arg_count == 3) + renderer_arg = arg_vector[2]; + + if (hw == NULL) { + fprintf(stderr, "failed to initialize new display\n"); + return 0; + } + + /* handle per-renderer setup of data.. */ + /* FIXME: these are awkward */ + if (strcmp(renderer, "pnm") == 0) { + if (renderer_arg == NULL) + renderer_arg = display_filename_default_; + renderer_data = (void *)renderer_arg; + } + +#ifdef HAVE_LIBPNG + if (strcmp(renderer, "png") == 0) { + if (renderer_arg == NULL) + renderer_arg = display_filename_default_; + renderer_data = (void *)renderer_arg; + } +#endif /* HAVE_LIBPNG */ + +#ifdef HAVE_LIBVNCSERVER + if (strcmp(renderer, "vnc") == 0) { + int argc = 1; + char *argv[] = { "vm-dcpu16", NULL }; + + renderer_data = lem1802_vnc_init_data(argc, argv, hw); + + /* FIXME: keep refs to vnc displays around somewhere, in global list maybe.. */ + /* keyboards will want to attach to them as well.. */ + + if (renderer_data == NULL) { + fprintf(stderr, "failed to initialize vnc\n"); + lem1802_del(&hw); + return 0; + } + } +#endif /* HAVE_LIBVNCSERVER */ + + if (lem1802_renderer_set(hw, renderer, renderer_data)) { + fprintf(stderr, "failed to set back-end renderer for display\n"); + lem1802_del(&hw); + return 0; + } + + if (dcpu16_hw_add(vm, hw)) { + fprintf(stderr, "failed to attach new display\n"); + lem1802_del(&hw); + return 0; + } + + return 0; +} +COMMAND_HELP(display) { + char *name, *args; + void *iter; + + fprintf(f, "\tdisplay renderer [renderer data]\n"); + if (summary) return; + + fprintf(f, "Attaches new display unit, using 'renderer' as back-end output.\n" + ); + + fprintf(f, "Supported renderers:\n"); + iter = NULL; + while ( (lem1802_renderers_iter(&iter, &name, &args)) ) { + fprintf(f, "\t%s %s\n", name, args); + } } /* gather all these together into a searchable table */ @@ -399,7 +508,9 @@ static struct command_ command_table_[] = { COMMAND_ENTRY(disassemble, 0, 2), COMMAND_ENTRY(step, 0, 1), COMMAND_ENTRY(run, 0, 0), + COMMAND_ENTRY(set, 2, 2), COMMAND_ENTRY(reset, 0, 0), + COMMAND_ENTRY(display, 1, 2), { NULL, 0, 0, NULL, NULL } }; @@ -428,7 +539,7 @@ COMMAND_HELP(help) { fprintf(f, "\thelp [command]\n"); if (summary) return; - fprintf(f, "Displays a list of available commands, or help on a specific command.\n"); + fprintf(f, "Displays a list of available commands, or detailed help on a specific command.\n"); } @@ -466,7 +577,7 @@ int main(int argc, char **argv) { argv += optind; if ((vm = dcpu16_new()) == NULL) { - fprintf(stderr, "could not allocate new dcpu instance\n"); + fprintf(stderr, "could not allocate new dcpu16 instance\n"); exit(EX_UNAVAILABLE); } @@ -481,13 +592,13 @@ int main(int argc, char **argv) { for (line = line_prev = NULL, tok_v = tok_v_prev = NULL, tok_c = tok_c_prev= 0, - snprintf(prompt, sizeof prompt, prompt_fmt, vm->pc), + snprintf(prompt, sizeof prompt, prompt_fmt, vm->reg[DCPU16_REG_PC]), dcpu16_state_print(vm); (line = readline(prompt)); printf("\n"), - snprintf(prompt, sizeof prompt, prompt_fmt, vm->pc), + snprintf(prompt, sizeof prompt, prompt_fmt, vm->reg[DCPU16_REG_PC]), dcpu16_state_print(vm)) { const char whitespace[] = " \t"; char *line_start;