typo, missing semicolon
[dcpu16] / vm-dcpu16.c
index 86f773aac4cfa691921da0488ddddaceaa9243e9..f7f4c156f7e9d2b443c7c64ec507fbb85c9f0c70 100644 (file)
@@ -148,6 +148,53 @@ int file_load_(struct dcpu16 *vm, char *filename, DCPU16_WORD addr) {
     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_;
@@ -181,6 +228,9 @@ struct rfb_instance_ *rfbScreen_next_available_display_(struct dynamic_array *rf
         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);
@@ -324,7 +374,7 @@ COMMAND_IMPL(dump) {
         return 0;
     }
 
-    dcpu16_dump_ram(vm, addr[0], addr[1]);
+    dump_ram_(vm, addr[0], addr[1]);
 
     return 0;
 }
@@ -396,7 +446,7 @@ COMMAND_IMPL(step) {
         dcpu16_step(vm);
 
         if (count > 1 && opt_.verbose)
-            dcpu16_state_print(vm);
+            state_print_(vm);
     }
 
     return 0;
@@ -449,20 +499,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);
@@ -475,12 +526,14 @@ 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);
         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");
@@ -489,37 +542,44 @@ 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 */
+#if 0
+            fprintf(stderr, "cycle time overrun %ld.%09lds\n", ts_cycle_waste.tv_sec, ts_cycle_waste.tv_nsec);
+#endif
         }
+
+#if 0
+        /* how did we do */
+        gettimespecofday(&ts_cycle_end);
+        timespec_subtract(&ts_cycle_rem, &ts_cycle_end_target, &ts_cycle_end);
+        fprintf(stderr, "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
+
     }
 
-    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);
+    fprintf(stderr, "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");
 
@@ -782,13 +842,13 @@ int main(int argc, char **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;