// File: /workspace/github_cpp_makefile_star2_O3/jkingan[P]ladyheather/heathui.cpp
// Function: set_jitter_config()
void set_jitter_config()
{
int i;
int ndx;
int max_bin;
double max_val;
double val;
double peak_div;
double avg;
double sdev;
double sum_y, sum_yy;
struct PLOT_Q q;

   // set up the machine to measure and display receiver message timing

   if(measure_jitter) {  // jitter measurements just enabled
      old_idle = idle_sleep;       // save current settings
      old_jit_adev = jitter_adev;
      old_poll = no_poll;

      old_plot_watch = plot_watch;
      old_plot_azel = plot_azel;
      for(i=0; i<NUM_PLOTS+DERIVED_PLOTS; i++) {  // disable all plots
         old_show_plots[i] = plot[i].show_plot;
         plot[i].show_plot = 0;
      }

      plot[MSGJIT].show_plot = 1;
      plot[MSGOFS].show_plot = 1;
      plot_watch = 0;
      plot_azel = 0;
                                   // set optimum values for jitter measurement
      idle_sleep = 0;
      jitter_adev = 1;
//    no_poll = 1;                 // rrrrr messes up things like SCPI that must be polled
      new_queue(RESET_ALL_QUEUES, 63); // flush the queues
   }
   else {  // jitter measurements disabled, restore old settings, calculate histogram
      sprintf(log_text, "#");
      write_log_comment(1);
      sprintf(log_text, "#  Jitter measurement ADEVs");
      write_log_comment(1);
      sprintf(log_text, "#");
      write_log_comment(1);
      log_adevs();
      log_stats();

      idle_sleep = old_idle;
      jitter_adev = old_jit_adev;
      no_poll = old_poll;

      plot_watch = old_plot_watch;
      plot_azel = old_plot_azel;
      for(i=0; i<NUM_PLOTS+DERIVED_PLOTS; i++) {  // disable all plots
         plot[i].show_plot = old_show_plots[i];
      }


      // calculate histogram of message arrival times (+/- 2000 msecs, 0.1 msec res
      if(plot_q_count == 0) return;
      if(hist_file) {
         fclose(hist_file);
         hist_file = 0;
      }
      sprintf(out, "%s.jit", unit_file_name);
      hist_file = topen(out, "w");
      if(hist_file == 0) return;

      for(i=0; i<HIST_BINS; i++) hist[i] = 0;

      avg = 0.0;
      sum_y = sum_yy = 0.0;
      for(i=0; i<plot_q_count; i++) {  // calculate histogram of message offset times
         q = plot_q[i];
         if(queue_interval) val = ((double) q.data[MSGOFS] / (double) queue_interval);
         else val = 0.0;

         sum_y += val;
         sum_yy += (val * val);

         ndx = (int) (val*HIST_SCALE+0.50);
         ndx += (HIST_BINS/2);
         if((ndx < 0) || (ndx >= HIST_BINS)) continue;
         ++hist[ndx];
      }
      if(plot_q_count) avg = sum_y / (double) plot_q_count;
      sdev = sqrt(fabs((sum_yy / (double) plot_q_count) - (avg*avg)));


fprintf(hist_file, "# bin   count\n");
      max_bin = 0;
      max_val = 0.0;
      peak_div = 1.0;
      for(i=0; i<HIST_BINS; i++) {  // write histogram data
         if(HIST_SCALE) val = (double) (i - (HIST_BINS/2)) / (double) HIST_SCALE;
         else val = 0.0;
         if(hist[i]) {
            fprintf(hist_file, "%.2f %d\n", val, hist[i]);
            if(hist[i] > max_bin) {  // a new peak
               max_bin = hist[i];
               max_val = val;
               peak_div = 1.0;
            }
            else if(hist[i] == max_bin) {  // broad peak, average the values
               max_val += val;
               ++peak_div;
            }
         }
      }

      sprintf(plot_title, "# msg offset time: /tsx=%.2f msec   max hits:%d points:%ld   avg:%.2f sdev:%.2f", max_val/peak_div,  max_bin, plot_q_count, avg,sdev);
      fprintf(hist_file,  "# msg offset time: /tsx=%.2f msec   max hits:%d points:%ld   avg:%.2f sdev:%.2f", max_val/peak_div,  max_bin, plot_q_count, avg,sdev);
      fclose(hist_file);
      hist_file = 0;
   }

   dont_reset_queues = 0;
   new_queue(RESET_ADEV_Q, 12);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
leaq 0x201c0f(%rip), %rax    # 0x23ccc8
cmpl $0x0, (%rax)
je 0x3b1ad
leaq 0x1e852f(%rip), %rax    # 0x2235f8
movl (%rax), %ecx
leaq 0x201bfa(%rip), %rdx    # 0x23cccc
movl %ecx, (%rdx)
leaq 0x201be9(%rip), %rcx    # 0x23ccc4
movl (%rcx), %edx
leaq 0x201bf0(%rip), %rsi    # 0x23ccd4
movl %edx, (%rsi)
leaq 0x2fbf33(%rip), %rdx    # 0x337020
movzbl (%rdx), %edx
leaq 0x201bd9(%rip), %rsi    # 0x23ccd0
movl %edx, (%rsi)
leaq 0x1e0308(%rip), %rdx    # 0x21b408
movb (%rdx), %sil
movb %sil, 0x30784b(%rip)    # 0x342955
leaq 0x1e0302(%rip), %rsi    # 0x21b413
movb (%rsi), %dil
movb %dil, 0x307839(%rip)    # 0x342954
leaq 0x122c0e(%rip), %rdi    # 0x15dd30
leaq 0x18(%rdi), %r8
xorl %r9d, %r9d
leaq 0x307810(%rip), %r10    # 0x342940
movb (%r8), %r11b
movb %r11b, (%r9,%r10)
movl $0x0, (%r8)
incq %r9
addq $0xe8, %r8
cmpq $0x14, %r9
jne 0x3b130
movl $0x1, %r8d
movl %r8d, 0xbe0(%rdi)
movl %r8d, 0xaf8(%rdi)
xorl %edi, %edi
movb %dil, (%rdx)
movb %dil, (%rsi)
movq $0x0, (%rax)
movl %r8d, (%rcx)
movl $0x7, %edi
movl $0x3f, %esi
callq 0x1118c
leaq 0x135ad6(%rip), %rax    # 0x170c60
movl $0x0, (%rax)
movl $0x1, %edi
movl $0xc, %esi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1118c
leaq 0x2027ac(%rip), %rbx    # 0x23d960
movw $0x23, %r14w
movw %r14w, (%rbx)
movl $0x1, %edi
callq 0xc36e2
movups 0xdbd1a(%rip), %xmm0    # 0x116ee8
movups %xmm0, 0xc(%rbx)
movupd 0xdbd02(%rip), %xmm0    # 0x116edc
movapd %xmm0, (%rbx)
movl $0x1, %edi
callq 0xc36e2
movw %r14w, (%rbx)
movl $0x1, %edi
callq 0xc36e2
callq 0xd20d6
callq 0xd1551
leaq 0x201ac5(%rip), %rax    # 0x23cccc
movslq (%rax), %rax
leaq 0x1e83e7(%rip), %rcx    # 0x2235f8
movq %rax, (%rcx)
leaq 0x201ab9(%rip), %rax    # 0x23ccd4
movl (%rax), %eax
leaq 0x201aa0(%rip), %rcx    # 0x23ccc4
movl %eax, (%rcx)
leaq 0x201aa3(%rip), %rax    # 0x23ccd0
movb (%rax), %al
leaq 0x2fbdea(%rip), %rcx    # 0x337020
movb %al, (%rcx)
movb 0x307717(%rip), %al     # 0x342955
leaq 0x1e01c3(%rip), %rcx    # 0x21b408
movb %al, (%rcx)
movb 0x307707(%rip), %al     # 0x342954
leaq 0x1e01bf(%rip), %rcx    # 0x21b413
movb %al, (%rcx)
leaq 0x122ad3(%rip), %rax    # 0x15dd30
addq $0x18, %rax
xorl %ecx, %ecx
leaq 0x3076d6(%rip), %rdx    # 0x342940
movzbl (%rcx,%rdx), %esi
movl %esi, (%rax)
incq %rcx
addq $0xe8, %rax
cmpq $0x14, %rcx
jne 0x3b26a
leaq 0x1e00b2(%rip), %r12    # 0x21b338
cmpq $0x0, (%r12)
je 0x3b525
leaq 0x203110(%rip), %r15    # 0x23e3a8
movq (%r15), %rdi
testq %rdi, %rdi
je 0x3b2ac
callq 0x9370
movq $0x0, (%r15)
leaq 0x1359d5(%rip), %rax    # 0x170c88
movq (%rax), %rdx
leaq 0x300f63(%rip), %rbx    # 0x33c220
leaq 0xdbc34(%rip), %rsi     # 0x116ef8
movq %rbx, %rdi
xorl %eax, %eax
callq 0x9060
leaq 0xe60e6(%rip), %rsi     # 0x1213bb
movq %rbx, %rdi
callq 0x9a47
movq %rax, (%r15)
testq %rax, %rax
je 0x3b525
movq %rax, %r14
leaq 0x30946d(%rip), %rbx    # 0x344760
movl $0x27100, %edx          # imm = 0x27100
movq %rbx, %rdi
xorl %esi, %esi
callq 0x91a0
movq (%r12), %rax
xorpd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
xorpd %xmm6, %xmm6
testq %rax, %rax
jle 0x3b39e
leaq 0x1e0075(%rip), %rcx    # 0x21b398
movq (%rcx), %rcx
leaq 0x1dffe3(%rip), %rdx    # 0x21b310
movq (%rdx), %rdx
cvtsi2sd %rcx, %xmm1
addq $0x70, %rdx
movsd 0xcdd67(%rip), %xmm2    # 0x1090a8
movsd 0xcdd1f(%rip), %xmm3    # 0x109068
movq %rax, %rsi
xorpd %xmm4, %xmm4
testq %rcx, %rcx
je 0x3b35d
movsd (%rdx), %xmm4
divsd %xmm1, %xmm4
movapd %xmm4, %xmm5
mulsd %xmm2, %xmm5
addsd %xmm3, %xmm5
cvttsd2si %xmm5, %edi
leal -0x4e20(%rdi), %r8d
cmpl $0xffff63c0, %r8d       # imm = 0xFFFF63C0
jb 0x3b386
addl $0x4e20, %edi           # imm = 0x4E20
incl (%rbx,%rdi,4)
addsd %xmm4, %xmm0
mulsd %xmm4, %xmm4
addsd %xmm4, %xmm6
addq $0xb0, %rdx
decq %rsi
jne 0x3b34c
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
xorpd %xmm2, %xmm2
testq %rax, %rax
je 0x3b3b7
divsd %xmm1, %xmm0
movapd %xmm0, %xmm2
divsd %xmm1, %xmm6
movsd %xmm2, -0x48(%rbp)
movapd %xmm2, %xmm0
mulsd %xmm2, %xmm0
subsd %xmm0, %xmm6
andpd 0xcddbc(%rip), %xmm6    # 0x109190
movapd %xmm6, -0x60(%rbp)
leaq 0xdbb1f(%rip), %rdi     # 0x116eff
movl $0xe, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x9750
xorl %r13d, %r13d
movsd 0xcdc73(%rip), %xmm0    # 0x109070
movl $0xffffb1e0, %r12d      # imm = 0xFFFFB1E0
leaq 0xdbb04(%rip), %r15     # 0x116f0e
movsd %xmm0, -0x38(%rbp)
xorl %r14d, %r14d
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
divsd 0xcdc86(%rip), %xmm0    # 0x1090a8
movl (%r13,%rbx), %edx
testl %edx, %edx
je 0x3b48c
leaq 0x202f76(%rip), %rax    # 0x23e3a8
movq (%rax), %rdi
movq %r15, %rsi
movsd %xmm0, -0x40(%rbp)
movb $0x1, %al
callq 0x9560
movl (%r13,%rbx), %eax
cmpl %r14d, %eax
jle 0x3b465
movl %eax, %r14d
movsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
movsd 0xcdc0d(%rip), %xmm0    # 0x109070
jmp 0x3b487
movsd -0x40(%rbp), %xmm1
jne 0x3b48c
movsd -0x30(%rbp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
movsd -0x38(%rbp), %xmm0
addsd 0xcdbe9(%rip), %xmm0    # 0x109070
movsd %xmm0, -0x38(%rbp)
incl %r12d
addq $0x4, %r13
cmpq $0x27100, %r13          # imm = 0x27100
jne 0x3b412
xorps %xmm2, %xmm2
sqrtsd -0x60(%rbp), %xmm2
movsd %xmm2, -0x40(%rbp)
movsd -0x30(%rbp), %xmm0
divsd -0x38(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
leaq 0x1dfe75(%rip), %r12    # 0x21b338
movq (%r12), %rcx
leaq 0x303382(%rip), %rdi    # 0x33e850
leaq 0xdba42(%rip), %rbx     # 0x116f17
movq %rbx, %rsi
movl %r14d, %edx
movsd -0x48(%rbp), %xmm1
movb $0x3, %al
callq 0x9060
leaq 0x202eba(%rip), %r15    # 0x23e3a8
movq (%r15), %rdi
movq (%r12), %rcx
movq %rbx, %rsi
movsd -0x30(%rbp), %xmm0
movl %r14d, %edx
movsd -0x48(%rbp), %xmm1
movsd -0x40(%rbp), %xmm2
movb $0x3, %al
callq 0x9560
movq (%r15), %rdi
callq 0x9370
movq $0x0, (%r15)
jmp 0x3b183
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq