+/* 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.screen->port += rfbScreens->entries;
+ new_instance.screen->ipv6port += rfbScreens->entries;
+
+ 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 */
+