#include <sys/time.h>
#include <readline/readline.h>
+#ifdef HAVE_LIBVNCSERVER
+#include "rfb/rfb.h"
+#endif /* HAVE_LIBVNCSERVER */
#include "dcpu16.h"
#include "common.h"
#include "hw_lem1802.h"
+#include "hw_keyboard.h"
/*
* shell-like driver for dcpu16 core
return 0;
}
+/* dump_ram_
+ * print raw ram contents from start to stop
+ */
+static
+void dump_ram_(struct dcpu16 *vm, DCPU16_WORD start, DCPU16_WORD end) {
+ unsigned int i, j;
+ const unsigned int n = 8; /* words per line */
+
+ if (!vm) return;
+
+ for (i = start, j = 0; i <= end; i++, j++) {
+ if (j % n == 0)
+ printf("0x%04x:\t", i);
+ printf(" %04x%s", vm->ram[i], (j % n) == (n - 1) ? "\n" : "");
+ }
+ if ((j % n) != (n - 1))
+ printf("\n");
+}
+
+
+/*
+ print the current state of the machine
+ shows current cycle count, registers, and next instruction
+*/
+static
+void state_print_(struct dcpu16 *vm) {
+ unsigned int i;
+
+ if (!vm) return;
+
+ printf(" ");
+ for (i = 0; i < 8; i++)
+ printf(" %s:0x%04x", dcpu16_reg_names[i], vm->reg[i]);
+ printf("\n");
+
+ printf("(0x%08llx) %2s:0x%04x %2s:0x%04x %2s:0x%04x %2s:0x%04x [%2s]:",
+ vm->cycle_,
+ dcpu16_reg_names[DCPU16_REG_EX], vm->reg[DCPU16_REG_EX],
+ dcpu16_reg_names[DCPU16_REG_SP], vm->reg[DCPU16_REG_SP],
+ dcpu16_reg_names[DCPU16_REG_PC], vm->reg[DCPU16_REG_PC],
+ dcpu16_reg_names[DCPU16_REG_IA], vm->reg[DCPU16_REG_IA],
+ "PC");
+
+ dcpu16_disassemble_print(vm, vm->reg[DCPU16_REG_PC]);
+ printf("\n");
+}
+
+
+#ifdef HAVE_LIBVNCSERVER
+static struct dynamic_array rfbScreens_;
+/* wups, kbdAddEvent isn't null by default, so I guess track associations externally */
+struct rfb_instance_ {
+ rfbScreenInfoPtr screen;
+ struct dcpu16_hw *attached_display;
+ struct dcpu16_hw *attached_keyboard;
+};
+
+/* locate or allocate the next display with an un-occupied framebuffer */
+static
+struct rfb_instance_ *rfbScreen_next_available_display_(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 };
+
+ fprintf(stderr, "DEBUG: rfbScreens->entries:%zu\n", rfbScreens->entries);
+
+ for (i = 0; i < rfbScreens->entries; i++) {
+ s = (struct rfb_instance_ *)DYNARRAY_ITEM(*rfbScreens, i);
+ if (s->attached_display == 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;
+ }
+
+ 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 };
+
+ 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;
+ }
+
+ 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);
+ rfbRunEventLoop(s, -1, TRUE);
+}
+#endif /* HAVE_LIBVNCSERVER */
/*
Here follows the various commands the shell can execute.
return 0;
}
- dcpu16_dump_ram(vm, addr[0], addr[1]);
+ dump_ram_(vm, addr[0], addr[1]);
return 0;
}
dcpu16_step(vm);
if (count > 1 && opt_.verbose)
- dcpu16_state_print(vm);
+ state_print_(vm);
}
return 0;
long long run_cycle_start;
struct timeval start_tv, now_tv, diff_tv;
long long cycle_start, cycles_to_wait;
- struct timespec sleep_time;
+ struct timespec sleep_time, rem_time;
+ long long run_usec;
(void)arg_count, (void)arg_vector;
running_ = 1;
gettimeofday(&run_start_tv, NULL);
- run_cycle_start = vm->cycle;
+ run_cycle_start = vm->cycle_;
memset(&act, 0, sizeof act);
act.sa_handler = sigint_handler_;
return -1;
}
- while(running_) {
+ while (running_) {
gettimeofday(&start_tv, NULL);
- cycle_start = vm->cycle;
+ cycle_start = vm->cycle_;
dcpu16_step(vm);
if (opt_.verbose > 1)
- dcpu16_state_print(vm);
+ state_print_(vm);
else if (opt_.verbose) {
dcpu16_disassemble_print(vm, vm->reg[DCPU16_REG_PC]);
printf("\n");
}
/* how many cycles did this instr use? */
- cycles_to_wait = vm->cycle - cycle_start;
+ cycles_to_wait = vm->cycle_ - cycle_start;
if (cycles_to_wait == 0)
continue;
/* each cycle wants 10 microseconds */
-
/* how much of that did we spend already */
gettimeofday(&now_tv, NULL);
/* 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;
- sleep_time.tv_nsec = 1000 * ( (cycles_to_wait * MICROSECONDS_PER_CYCLE) - diff_tv.tv_usec);
- sleep_time.tv_nsec = diff_tv.tv_usec * 1000;
+ /* this is not accurate.. */
+ sleep_time.tv_nsec = 250 * ( (cycles_to_wait * MICROSECONDS_PER_CYCLE) - diff_tv.tv_usec);
- nanosleep(&sleep_time, NULL);
+ /* 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);
+ }
}
}
gettimeofday(&run_stop_tv, NULL);
timeval_subtract(&diff_tv, &run_stop_tv, &run_start_tv);
- fprintf(stderr, "ran %llu cycles in %lds %dus\n",
- vm->cycle - run_cycle_start,
+ 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);
+ diff_tv.tv_usec,
+ run_usec);
printf("interrupted...\n");
#endif /* HAVE_LIBPNG */
;
COMMAND_IMPL(display) {
- struct dcpu16_hw *hw = lem1802_new(vm);
+ struct dcpu16_hw *hw;
const char *renderer = arg_vector[1];
const char *renderer_arg = NULL;
- void *renderer_data = NULL;
+ void *renderer_data;
if (arg_count == 3)
renderer_arg = arg_vector[2];
+ hw = dcpu16_hw_new(vm, &dcpu16_hw_module_lem1802, NULL);
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;
+ renderer_data = (void *)(renderer_arg ? renderer_arg : display_filename_default_);
}
#ifdef HAVE_LIBPNG
if (strcmp(renderer, "png") == 0) {
- if (renderer_arg == NULL)
- renderer_arg = display_filename_default_;
- renderer_data = (void *)renderer_arg;
+ renderer_data = (void *)(renderer_arg ? renderer_arg : display_filename_default_);
}
#endif /* HAVE_LIBPNG */
if (strcmp(renderer, "vnc") == 0) {
int argc = 1;
char *argv[] = { "vm-dcpu16", NULL };
+ struct rfb_instance_ *s;
- 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) {
+ s = rfbScreen_next_available_display_(&rfbScreens_, argc, argv);
+ if (s == NULL) {
fprintf(stderr, "failed to initialize vnc\n");
- lem1802_del(&hw);
+ dcpu16_hw_del(&hw);
return 0;
}
+
+ if (dcpu16_hw_ctl(hw, "associate_rfbScreen", s->screen, NULL)) {
+ fprintf(stderr, "failed to configure display/vnc");
+ dcpu16_hw_del(&hw);
+ return 0;
+ }
+ s->attached_display = hw;
+ rfbScreen_start(s->screen);
+ renderer_data = s->screen;
}
#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;
- }
+ dcpu16_hw_ctl(hw, "renderer", (char *)renderer, NULL);
+ dcpu16_hw_ctl(hw, "renderer_data", renderer_data, NULL);
- if (dcpu16_hw_add(vm, hw)) {
+ if (dcpu16_hw_attach(vm, hw)) {
fprintf(stderr, "failed to attach new display\n");
- lem1802_del(&hw);
+ dcpu16_hw_del(&hw);
return 0;
}
return 0;
}
COMMAND_HELP(display) {
- char *name, *args;
+ struct renderer_ {
+ char *name;
+ char *args;
+ } renderer;
void *iter;
fprintf(f, "\tdisplay renderer [renderer data]\n");
);
fprintf(f, "Supported renderers:\n");
+
iter = NULL;
- while ( (lem1802_renderers_iter(&iter, &name, &args)) ) {
- fprintf(f, "\t%s %s\n", name, args);
+ do {
+ if (dcpu16_hw_module_lem1802.ctl(NULL, "renderers_iter", &iter, &renderer)) {
+ fprintf(stderr, "error fetching next renderer\n");
+ break;
+ }
+ if (iter == NULL || renderer.name == NULL)
+ break;
+
+ fprintf(f, "\t%s %s\n", renderer.name, renderer.args);
+ } while (iter);
+}
+
+COMMAND_IMPL(keyboard) {
+ struct dcpu16_hw *hw;
+
+ (void)arg_count, (void)arg_vector;
+
+ hw = dcpu16_hw_new(vm, &dcpu16_hw_module_keyboard, NULL);
+ if (hw == NULL) {
+ fprintf(stderr, "failed to initialize new keyboard\n");
+ return 0;
+ }
+
+#ifdef HAVE_LIBVNCSERVER
+ struct rfb_instance_ *s;
+ int argc = 1;
+ char *argv[] = { "vm-dcpu16", NULL };
+
+ s = rfbScreen_next_available_keyboard_(&rfbScreens_, argc, argv);
+ if (s == NULL) {
+ fprintf(stderr, "failed to initialize vnc\n");
+ dcpu16_hw_del(&hw);
+ return 0;
}
+ if (dcpu16_hw_ctl(hw, "associate_rfbScreen", s->screen, NULL)) {
+ fprintf(stderr, "failed to configure keyboard/vnc\n");
+ dcpu16_hw_del(&hw);
+ return 0;
+ }
+ s->attached_keyboard = hw;
+
+ if (dcpu16_hw_attach(vm, hw)) {
+ fprintf(stderr, "failed to attach new keyboard\n");
+ dcpu16_hw_del(&hw);
+ return 0;
+ }
+#endif /* HAVE_LIBVNCSERVER */
+
+ return 0;
+}
+COMMAND_HELP(keyboard) {
+ fprintf(f, "\tkeyboard\n");
+ if (summary) return;
+
+ fprintf(f, "Attaches new keyboard unit.\n");
}
/* gather all these together into a searchable table */
COMMAND_ENTRY(set, 2, 2),
COMMAND_ENTRY(reset, 0, 0),
COMMAND_ENTRY(display, 1, 2),
+ COMMAND_ENTRY(keyboard, 0, 0),
{ NULL, 0, 0, NULL, NULL }
};
exit(EX_UNAVAILABLE);
}
+#ifdef HAVE_LIBVNCSERVER
+ if (dynarray_init(&rfbScreens_, sizeof(struct rfb_instance_), 4)) {
+ fprintf(stderr, "could not allocate rfb container\n");
+ exit(EX_UNAVAILABLE);
+ }
+#endif /* HAVE_LIBVNCSERVER */
+
if (argc) {
if (file_load_(vm, *argv, 0)) {
fprintf(stderr, "couldn't load '%s'\n", *argv);
tok_v = tok_v_prev = NULL,
tok_c = tok_c_prev= 0,
snprintf(prompt, sizeof prompt, prompt_fmt, vm->reg[DCPU16_REG_PC]),
- dcpu16_state_print(vm);
+ state_print_(vm);
(line = readline(prompt));
printf("\n"),
snprintf(prompt, sizeof prompt, prompt_fmt, vm->reg[DCPU16_REG_PC]),
- dcpu16_state_print(vm)) {
+ state_print_(vm)) {
const char whitespace[] = " \t";
char *line_start;
struct command_ *c;