X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=vm-dcpu16.c;h=aefbcf16560c6ec4733503574dd4dfa73d1af525;hb=1470698024f5511a3b94b7bb50b1097cc6659b32;hp=b4798838328aeae8e7d9698c9af64c25b79014a3;hpb=33efa05d43acf927c97daab10c00d1cbc2a7b9f6;p=dcpu16 diff --git a/vm-dcpu16.c b/vm-dcpu16.c index b479883..aefbcf1 100644 --- a/vm-dcpu16.c +++ b/vm-dcpu16.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -54,6 +55,11 @@ void sigint_handler_(int sig) { } #define VERBOSE_PRINTF(...) do { if (opt_.verbose) printf(__VA_ARGS__); } while (0) +#ifdef DEBUG +#define DEBUG_PRINTF(...) do { if (opt_.verbose > 4) fprintf(stderr, __VA_ARGS__); } while (0) +#else /* DEBUG */ +#define DEBUG_PRINTF(...) do { } while (0) +#endif /* DEBUG */ static void usage_(char *prog, unsigned int full) { @@ -205,9 +211,13 @@ struct rfb_instance_ { struct dcpu16_hw *attached_keyboard; }; -/* locate or allocate the next display with an un-occupied framebuffer */ +enum rfbscreen_next_what_ { + NEXT_DISPLAY, + NEXT_KEYBOARD, +}; +/* locate next rfb not paired to requested type */ static -struct rfb_instance_ *rfbScreen_next_available_display_(struct dynamic_array *rfbScreens, int argc, char *argv[]) { +struct rfb_instance_ *rfbScreen_next_available_(enum rfbscreen_next_what_ what, struct dynamic_array *rfbScreens, int argc, char *argv[]) { size_t i; struct rfb_instance_ new_instance, *s; struct packed_args_ { @@ -215,52 +225,38 @@ struct rfb_instance_ *rfbScreen_next_available_display_(struct dynamic_array *rf char **argv; } parg = { argc, argv }; - fprintf(stderr, "DEBUG: rfbScreens->entries:%zu\n", rfbScreens->entries); - for (i = 0; i < rfbScreens->entries; i++) { + struct dcpu16_hw *attached; + s = (struct rfb_instance_ *)DYNARRAY_ITEM(*rfbScreens, i); - if (s->attached_display == NULL) + switch (what) { + case NEXT_DISPLAY: attached = s->attached_display; break; + case NEXT_KEYBOARD: attached = s->attached_keyboard; break; + } + if (attached == NULL) return s; } + /* no available rfb, create new */ if (dcpu16_hw_module_lem1802.ctl(NULL, "new_rfbScreen", &parg, &new_instance.screen)) { - fprintf(stderr, "failed to allocate new rfbScreen"); + fprintf(stderr, "failed to allocate new rfbScreen\n"); return NULL; } new_instance.attached_display = NULL; new_instance.attached_keyboard = NULL; - s = dynarray_add(rfbScreens, &new_instance); - return s; -} -/* locate or allocate the next display with an un-occupied keyboard */ -static -struct rfb_instance_ *rfbScreen_next_available_keyboard_(struct dynamic_array *rfbScreens, int argc, char *argv[]) { - size_t i; - struct rfb_instance_ new_instance, *s; - struct packed_args_ { - int argc; - char **argv; - } parg = { argc, argv }; + new_instance.screen->port += rfbScreens->entries; + new_instance.screen->ipv6port += rfbScreens->entries; - for (i = 0; i < rfbScreens->entries; i++) { - s = (struct rfb_instance_ *)DYNARRAY_ITEM(*rfbScreens, i); - if (s->attached_keyboard == NULL) - return s; - } - - if (dcpu16_hw_module_lem1802.ctl(NULL, "new_rfbScreen", &parg, &new_instance.screen)) { - fprintf(stderr, "failed to allocate new rfbScreen"); - return NULL; - } + DEBUG_PRINTF("%s>> port:%u\n", __func__, new_instance.screen->port); - new_instance.attached_display = NULL; - new_instance.attached_keyboard = NULL; s = dynarray_add(rfbScreens, &new_instance); + return s; } + /* begin serving a screen */ void rfbScreen_start(rfbScreenInfoPtr s) { rfbInitServer(s); @@ -313,6 +309,7 @@ COMMAND_IMPL(reset) { dcpu16_reset(vm); printf("initialized\n"); + return 0; } COMMAND_HELP(reset) { @@ -323,6 +320,27 @@ COMMAND_HELP(reset) { } +COMMAND_IMPL(verbosity) { + int l; + (void)vm, (void)arg_count; + + l = str_to_word(arg_vector[1]); + if (l < 0) { + fprintf(stderr, "invalid level\n"); + return 0; + } + + opt_.verbose = l; + + return 0; +} +COMMAND_HELP(verbosity) { + fprintf(f, "\tverbosity level\n"); + if (summary) return; + + fprintf(f, "sets the verbosity level\n"); +} + COMMAND_IMPL(load) { int addr = 0; @@ -496,20 +514,21 @@ COMMAND_HELP(set) { fprintf(f, "Sets addr to value."); } -#define MICROSECONDS_PER_CYCLE 10 +#define NANOSECONDS_PER_CYCLE 10000 +#define MIN_NANOSLEEP 31000 COMMAND_IMPL(run) { struct sigaction act; - struct timeval run_start_tv, run_stop_tv; - long long run_cycle_start; - struct timeval start_tv, now_tv, diff_tv; + long long run_cycle_start, run_cycle_end; long long cycle_start, cycles_to_wait; - struct timespec sleep_time, rem_time; - long long run_usec; + + struct timespec ts_run_start, ts_run_end, ts_run_diff; + struct timespec ts_cycle_start, ts_cycle_end_target, ts_cycle_end, ts_cycle_waste, ts_cycle_rem; + const struct timespec ts_cycle_time = { .tv_sec = 0, .tv_nsec = NANOSECONDS_PER_CYCLE }; (void)arg_count, (void)arg_vector; running_ = 1; - gettimeofday(&run_start_tv, NULL); + gettimespecofday(&ts_run_start); run_cycle_start = vm->cycle_; memset(&act, 0, sizeof act); @@ -522,7 +541,9 @@ COMMAND_IMPL(run) { } while (running_) { - gettimeofday(&start_tv, NULL); + gettimespecofday(&ts_cycle_start); + ts_cycle_end_target = ts_cycle_start; + cycle_start = vm->cycle_; dcpu16_step(vm); @@ -536,37 +557,42 @@ COMMAND_IMPL(run) { /* how many cycles did this instr use? */ cycles_to_wait = vm->cycle_ - cycle_start; - if (cycles_to_wait == 0) - continue; - - /* each cycle wants 10 microseconds */ + /* each cycle wants to take 10 microseconds */ + while (cycles_to_wait--) + timespec_add(&ts_cycle_end_target, &ts_cycle_time); /* how much of that did we spend already */ - gettimeofday(&now_tv, NULL); - timeval_subtract(&diff_tv, &now_tv, &start_tv); - /* do we have time to kill? */ - if (cycles_to_wait * MICROSECONDS_PER_CYCLE > diff_tv.tv_usec) { - sleep_time.tv_sec = diff_tv.tv_sec; - /* this is not accurate.. */ - sleep_time.tv_nsec = 250 * ( (cycles_to_wait * MICROSECONDS_PER_CYCLE) - diff_tv.tv_usec); + gettimespecofday(&ts_cycle_end); + /* do we have time to kill? */ + if (timespec_subtract(&ts_cycle_waste, &ts_cycle_end_target, &ts_cycle_end) == 0) { /* nanosleep doesn't interfere with libvncserver, unlike usleep */ - while ( nanosleep(&sleep_time, &rem_time) ) { - sleep_time = rem_time; - fprintf(stderr, "rem:%ld %ld\n", rem_time.tv_sec, rem_time.tv_nsec); - } + if (ts_cycle_waste.tv_sec == 0 && ts_cycle_waste.tv_nsec >= MIN_NANOSLEEP) + while ( nanosleep(&ts_cycle_waste, &ts_cycle_rem) ) + ts_cycle_waste = ts_cycle_rem; + } else { + /* negative, we've already blown our time */ + DEBUG_PRINTF("cycle time overrun %ld.%09lds\n", ts_cycle_waste.tv_sec, ts_cycle_waste.tv_nsec); } + +#ifdef DEBUG + /* how did we do */ + gettimespecofday(&ts_cycle_end); + timespec_subtract(&ts_cycle_rem, &ts_cycle_end_target, &ts_cycle_end); + DEBUG_PRINTF("projected end: %ld.%09ld actual end: %ld.%09ld diff: %ld.%09ld\n", + ts_cycle_end_target.tv_sec, ts_cycle_end_target.tv_nsec, + ts_cycle_end.tv_sec, ts_cycle_end.tv_nsec, + ts_cycle_rem.tv_sec, ts_cycle_rem.tv_nsec); +#endif /* DEBUG */ + } - gettimeofday(&run_stop_tv, NULL); - timeval_subtract(&diff_tv, &run_stop_tv, &run_start_tv); - run_usec = diff_tv.tv_sec * 1000000; - run_usec += diff_tv.tv_usec; - fprintf(stderr, "ran %llu cycles in %lds %dus (%lldus)\n", - vm->cycle_ - run_cycle_start, - diff_tv.tv_sec, - diff_tv.tv_usec, - run_usec); + run_cycle_end = vm->cycle_; + gettimespecofday(&ts_run_end); + timespec_subtract(&ts_run_diff, &ts_run_end, &ts_run_start); + VERBOSE_PRINTF("ran %lld cycles in %ld.%09lds\n", + run_cycle_end - run_cycle_start, + ts_run_diff.tv_sec, ts_run_diff.tv_nsec); printf("interrupted...\n"); @@ -620,7 +646,7 @@ COMMAND_IMPL(display) { char *argv[] = { "vm-dcpu16", NULL }; struct rfb_instance_ *s; - s = rfbScreen_next_available_display_(&rfbScreens_, argc, argv); + s = rfbScreen_next_available_(NEXT_DISPLAY, &rfbScreens_, argc, argv); if (s == NULL) { fprintf(stderr, "failed to initialize vnc\n"); dcpu16_hw_del(&hw); @@ -635,6 +661,8 @@ COMMAND_IMPL(display) { s->attached_display = hw; rfbScreen_start(s->screen); renderer_data = s->screen; + + DEBUG_PRINTF("attached display to rfb (frameBuffer:%p)\n", s->screen->frameBuffer); } #endif /* HAVE_LIBVNCSERVER */ @@ -693,7 +721,7 @@ COMMAND_IMPL(keyboard) { int argc = 1; char *argv[] = { "vm-dcpu16", NULL }; - s = rfbScreen_next_available_keyboard_(&rfbScreens_, argc, argv); + s = rfbScreen_next_available_(NEXT_KEYBOARD, &rfbScreens_, argc, argv); if (s == NULL) { fprintf(stderr, "failed to initialize vnc\n"); dcpu16_hw_del(&hw); @@ -731,6 +759,7 @@ COMMAND_HELP(help); static struct command_ command_table_[] = { COMMAND_ENTRY(help, 0, -1), COMMAND_ENTRY(quit, 0, -1), + COMMAND_ENTRY(verbosity, 1, 1), COMMAND_ENTRY(load, 1, 2), COMMAND_ENTRY(dump, 0, 2), COMMAND_ENTRY(disassemble, 0, 2), @@ -771,6 +800,28 @@ COMMAND_HELP(help) { fprintf(f, "Displays a list of available commands, or detailed help on a specific command.\n"); } +static +void msg_verbose_filter_(unsigned int level, char *fmt, ...) { + static const char * const levels[] = { "error", "info", "debug" }; + FILE *f = (level <= MSG_ERROR) ? stderr : stdout; + va_list ap; + + if (level + 2 > opt_.verbose) + return; + + if (level < sizeof levels / sizeof *levels) + fprintf(f, "[%s %s] ", "dcpu16", levels[level]); + else + fprintf(f, "[%s %u] ", "dcpu16", level); + + va_start(ap, fmt); + vfprintf(f, fmt, ap); + va_end(ap); + + fprintf(f, "\n"); + + fflush(f); +} int main(int argc, char **argv) { const char prompt_fmt[] = "PC:%04x> "; @@ -796,15 +847,11 @@ int main(int argc, char **argv) { exit(EX_USAGE); } } - if (opt_.verbose < 1) { - dcpu16_warn_cb_set(NULL); - dcpu16_trace_cb_set(NULL); - } else if (opt_.verbose < 2) { - dcpu16_trace_cb_set(NULL); - } argc -= optind; argv += optind; + dcpu16_msg_set_default(msg_verbose_filter_); + if ((vm = dcpu16_new()) == NULL) { fprintf(stderr, "could not allocate new dcpu16 instance\n"); exit(EX_UNAVAILABLE);