+/* 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;
+};
+
+enum rfbscreen_next_what_ {
+ NEXT_DISPLAY,
+ NEXT_KEYBOARD,
+};
+/* locate next rfb not paired to requested type */
+static
+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_ {
+ int argc;
+ char **argv;
+ } parg = { argc, argv };
+
+ for (i = 0; i < rfbScreens->entries; i++) {
+ struct dcpu16_hw *attached;
+
+ s = (struct rfb_instance_ *)DYNARRAY_ITEM(*rfbScreens, i);
+ 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\n");
+ return NULL;
+ }
+
+ new_instance.attached_display = NULL;
+ new_instance.attached_keyboard = NULL;
+
+ new_instance.screen->port += rfbScreens->entries;
+ new_instance.screen->ipv6port += rfbScreens->entries;
+
+ DEBUG_PRINTF("%s>> port:%u\n", __func__, new_instance.screen->port);
+
+ 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 */