// File: /workspace/github_cpp_makefile_star2_O1/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 $0x28, %rsp
leaq 0x201c95(%rip), %rax    # 0x23ccc8
cmpl $0x0, (%rax)
je 0x3b127
leaq 0x1e85b5(%rip), %rax    # 0x2235f8
movl (%rax), %ecx
leaq 0x201c80(%rip), %rdx    # 0x23cccc
movl %ecx, (%rdx)
leaq 0x201c6f(%rip), %rcx    # 0x23ccc4
movl (%rcx), %edx
leaq 0x201c76(%rip), %rsi    # 0x23ccd4
movl %edx, (%rsi)
leaq 0x2fbfb9(%rip), %rdx    # 0x337020
movzbl (%rdx), %edx
leaq 0x201c5f(%rip), %rsi    # 0x23ccd0
movl %edx, (%rsi)
leaq 0x1e038e(%rip), %rdx    # 0x21b408
movb (%rdx), %sil
movb %sil, 0x3078d1(%rip)    # 0x342955
leaq 0x1e0388(%rip), %rsi    # 0x21b413
movb (%rsi), %dil
movb %dil, 0x3078bf(%rip)    # 0x342954
leaq 0x122c94(%rip), %rdi    # 0x15dd30
leaq 0x18(%rdi), %r8
xorl %r9d, %r9d
leaq 0x307896(%rip), %r10    # 0x342940
movb (%r8), %r11b
movb %r11b, (%r9,%r10)
movl $0x0, (%r8)
incq %r9
addq $0xe8, %r8
cmpq $0x14, %r9
jne 0x3b0aa
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 0x1120f
leaq 0x135b5c(%rip), %rax    # 0x170c60
movl $0x0, (%rax)
movl $0x1, %edi
movl $0xc, %esi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1120f
leaq 0x202832(%rip), %rbx    # 0x23d960
movw $0x23, %r14w
movw %r14w, (%rbx)
movl $0x1, %edi
callq 0xc3105
movups 0xdbd0c(%rip), %xmm0    # 0x116e54
movups %xmm0, 0xc(%rbx)
movupd 0xdbcf4(%rip), %xmm0    # 0x116e48
movapd %xmm0, (%rbx)
movl $0x1, %edi
callq 0xc3105
movw %r14w, (%rbx)
movl $0x1, %edi
callq 0xc3105
callq 0xd1a21
callq 0xd0eb8
leaq 0x201b4b(%rip), %rax    # 0x23cccc
movslq (%rax), %rax
leaq 0x1e846d(%rip), %rcx    # 0x2235f8
movq %rax, (%rcx)
leaq 0x201b3f(%rip), %rax    # 0x23ccd4
movl (%rax), %eax
leaq 0x201b26(%rip), %rcx    # 0x23ccc4
movl %eax, (%rcx)
leaq 0x201b29(%rip), %rax    # 0x23ccd0
movb (%rax), %al
leaq 0x2fbe70(%rip), %rcx    # 0x337020
movb %al, (%rcx)
movb 0x30779d(%rip), %al     # 0x342955
leaq 0x1e0249(%rip), %rcx    # 0x21b408
movb %al, (%rcx)
movb 0x30778d(%rip), %al     # 0x342954
leaq 0x1e0245(%rip), %rcx    # 0x21b413
movb %al, (%rcx)
leaq 0x122b59(%rip), %rax    # 0x15dd30
addq $0x18, %rax
xorl %ecx, %ecx
leaq 0x30775c(%rip), %rdx    # 0x342940
movzbl (%rcx,%rdx), %esi
movl %esi, (%rax)
incq %rcx
addq $0xe8, %rax
cmpq $0x14, %rcx
jne 0x3b1e4
leaq 0x1e0138(%rip), %r15    # 0x21b338
cmpq $0x0, (%r15)
je 0x3b354
leaq 0x203197(%rip), %r14    # 0x23e3a8
movq (%r14), %rdi
testq %rdi, %rdi
je 0x3b225
callq 0x9370
movq $0x0, (%r14)
leaq 0x135a5c(%rip), %rax    # 0x170c88
movq (%rax), %rdx
leaq 0x300fea(%rip), %rbx    # 0x33c220
leaq 0xdbc27(%rip), %rsi     # 0x116e64
movq %rbx, %rdi
xorl %eax, %eax
callq 0x9060
leaq 0xe608d(%rip), %rsi     # 0x1212db
movq %rbx, %rdi
callq 0x9a4d
movq %rax, (%r14)
testq %rax, %rax
je 0x3b354
leaq 0x3094f7(%rip), %rbx    # 0x344760
movl $0x27100, %edx          # imm = 0x27100
movq %rbx, %rdi
xorl %esi, %esi
callq 0x91a0
movq (%r15), %rax
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
xorpd %xmm0, %xmm0
testq %rax, %rax
jle 0x3b312
leaq 0x1e0101(%rip), %rcx    # 0x21b398
movq (%rcx), %rcx
leaq 0x1e006f(%rip), %rdx    # 0x21b310
movq (%rdx), %rdx
cvtsi2sd %rcx, %xmm3
addq $0x70, %rdx
movsd 0xcddf3(%rip), %xmm4    # 0x1090a8
movsd 0xcddab(%rip), %xmm5    # 0x109068
movq %rax, %rsi
xorpd %xmm6, %xmm6
testq %rcx, %rcx
je 0x3b2d1
movsd (%rdx), %xmm6
divsd %xmm3, %xmm6
movapd %xmm6, %xmm7
mulsd %xmm4, %xmm7
addsd %xmm5, %xmm7
cvttsd2si %xmm7, %edi
leal -0x4e20(%rdi), %r8d
cmpl $0xffff63c0, %r8d       # imm = 0xFFFF63C0
jb 0x3b2fa
addl $0x4e20, %edi           # imm = 0x4E20
incl (%rbx,%rdi,4)
addsd %xmm6, %xmm2
mulsd %xmm6, %xmm6
addsd %xmm6, %xmm0
addq $0xb0, %rdx
decq %rsi
jne 0x3b2c0
xorps %xmm3, %xmm3
cvtsi2sd %rax, %xmm3
xorpd %xmm4, %xmm4
testq %rax, %rax
je 0x3b32b
divsd %xmm3, %xmm2
movapd %xmm2, %xmm4
divsd %xmm3, %xmm0
movsd %xmm4, -0x38(%rbp)
movapd %xmm4, %xmm2
mulsd %xmm4, %xmm2
subsd %xmm2, %xmm0
andpd 0xcde38(%rip), %xmm0    # 0x109180
ucomisd %xmm1, %xmm0
jb 0x3b363
sqrtsd %xmm0, %xmm0
jmp 0x3b368
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x9270
movsd %xmm0, -0x48(%rbp)
movq (%r14), %rcx
leaq 0xdbaf4(%rip), %rdi     # 0x116e6b
movl $0xe, %esi
movl $0x1, %edx
callq 0x9750
xorl %r13d, %r13d
xorpd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
movsd 0xcdcd6(%rip), %xmm0    # 0x109070
movl $0xffffb1e0, %r12d      # imm = 0xFFFFB1E0
leaq 0xdbad3(%rip), %r15     # 0x116e7a
movsd %xmm0, -0x40(%rbp)
xorl %r14d, %r14d
movsd -0x38(%rbp), %xmm1
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
divsd 0xcdce4(%rip), %xmm0    # 0x1090a8
movl (%r13,%rbx), %edx
testl %edx, %edx
je 0x3b433
leaq 0x202fd4(%rip), %rax    # 0x23e3a8
movq (%rax), %rdi
movq %r15, %rsi
movsd %xmm0, -0x50(%rbp)
movb $0x1, %al
callq 0x9560
movl (%r13,%rbx), %eax
cmpl %r14d, %eax
jle 0x3b407
movl %eax, %r14d
movsd -0x50(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
movsd 0xcdc6b(%rip), %xmm0    # 0x109070
jmp 0x3b429
movsd -0x50(%rbp), %xmm1
jne 0x3b42e
movsd -0x30(%rbp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
movsd -0x40(%rbp), %xmm0
addsd 0xcdc47(%rip), %xmm0    # 0x109070
movsd %xmm0, -0x40(%rbp)
movsd -0x38(%rbp), %xmm1
incl %r12d
addq $0x4, %r13
cmpq $0x27100, %r13          # imm = 0x27100
jne 0x3b3b4
movsd -0x30(%rbp), %xmm0
divsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
leaq 0x1dfedb(%rip), %r12    # 0x21b338
movq (%r12), %rcx
leaq 0x3033e8(%rip), %rdi    # 0x33e850
leaq 0xdba14(%rip), %rbx     # 0x116e83
movq %rbx, %rsi
movl %r14d, %edx
movsd -0x48(%rbp), %xmm2
movb $0x3, %al
callq 0x9060
leaq 0x202f20(%rip), %r15    # 0x23e3a8
movq (%r15), %rdi
movq (%r12), %rcx
movq %rbx, %rsi
movsd -0x30(%rbp), %xmm0
movl %r14d, %edx
movsd -0x38(%rbp), %xmm1
movsd -0x48(%rbp), %xmm2
movb $0x3, %al
callq 0x9560
movq (%r15), %rdi
callq 0x9370
movq $0x0, (%r15)
jmp 0x3b0fd