// File: 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
subq $0x100, %rsp            # imm = 0x100
leaq 0x248c96(%rip), %rax    # 0x29bea8
cmpl $0x0, (%rax)
je 0x53334
leaq 0x22f5b6(%rip), %rax    # 0x2827d8
movq (%rax), %rax
movl %eax, %ecx
leaq 0x248c7e(%rip), %rax    # 0x29beac
movl %ecx, (%rax)
leaq 0x248c6d(%rip), %rax    # 0x29bea4
movl (%rax), %ecx
leaq 0x248c74(%rip), %rax    # 0x29beb4
movl %ecx, (%rax)
leaq 0x342fb7(%rip), %rax    # 0x396200
movzbl (%rax), %ecx
leaq 0x248c5d(%rip), %rax    # 0x29beb0
movl %ecx, (%rax)
leaq 0x22738c(%rip), %rax    # 0x27a5e8
movb (%rax), %al
movb %al, 0x34e8d1(%rip)     # 0x3a1b35
leaq 0x227388(%rip), %rax    # 0x27a5f3
movb (%rax), %al
movb %al, 0x34e8c1(%rip)     # 0x3a1b34
movl $0x0, -0x4(%rbp)
cmpl $0x14, -0x4(%rbp)
jge 0x532cf
movslq -0x4(%rbp), %rcx
leaq 0x169a95(%rip), %rax    # 0x1bcd20
imulq $0xe8, %rcx, %rcx
addq %rcx, %rax
movl 0x18(%rax), %eax
movb %al, %dl
movslq -0x4(%rbp), %rcx
leaq 0x34e87b(%rip), %rax    # 0x3a1b20
movb %dl, (%rax,%rcx)
movslq -0x4(%rbp), %rcx
leaq 0x169a6d(%rip), %rax    # 0x1bcd20
imulq $0xe8, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x18(%rax)
movl -0x4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4(%rbp)
jmp 0x5327a
leaq 0x169a4a(%rip), %rax    # 0x1bcd20
movl $0x1, 0xbe0(%rax)
leaq 0x169a39(%rip), %rax    # 0x1bcd20
movl $0x1, 0xaf8(%rax)
leaq 0x2272f0(%rip), %rax    # 0x27a5e8
movb $0x0, (%rax)
leaq 0x2272f1(%rip), %rax    # 0x27a5f3
movb $0x0, (%rax)
leaq 0x22f4cc(%rip), %rax    # 0x2827d8
movq $0x0, (%rax)
leaq 0x248b8a(%rip), %rax    # 0x29bea4
movl $0x1, (%rax)
movl $0x7, %edi
movl $0x3f, %esi
callq 0x14df0
jmp 0x53840
leaq 0x249805(%rip), %rdi    # 0x29cb40
leaq 0x11ea5a(%rip), %rsi    # 0x171d9c
movb $0x0, %al
callq 0x9060
movl $0x1, %edi
callq 0x10cbe0
leaq 0x2497e6(%rip), %rdi    # 0x29cb40
leaq 0x128540(%rip), %rsi    # 0x17b8a1
movb $0x0, %al
callq 0x9060
movl $0x1, %edi
callq 0x10cbe0
leaq 0x2497c7(%rip), %rdi    # 0x29cb40
leaq 0x11ea1c(%rip), %rsi    # 0x171d9c
movb $0x0, %al
callq 0x9060
movl $0x1, %edi
callq 0x10cbe0
callq 0x11f0a0
callq 0x11e0b0
leaq 0x248b0a(%rip), %rax    # 0x29beac
movslq (%rax), %rcx
leaq 0x22f42c(%rip), %rax    # 0x2827d8
movq %rcx, (%rax)
leaq 0x248afe(%rip), %rax    # 0x29beb4
movl (%rax), %ecx
leaq 0x248ae5(%rip), %rax    # 0x29bea4
movl %ecx, (%rax)
leaq 0x248ae8(%rip), %rax    # 0x29beb0
movl (%rax), %eax
movb %al, %cl
leaq 0x342e2d(%rip), %rax    # 0x396200
movb %cl, (%rax)
movb 0x34e75a(%rip), %cl     # 0x3a1b35
leaq 0x227206(%rip), %rax    # 0x27a5e8
movb %cl, (%rax)
movb 0x34e74a(%rip), %cl     # 0x3a1b34
leaq 0x227202(%rip), %rax    # 0x27a5f3
movb %cl, (%rax)
movl $0x0, -0x4(%rbp)
cmpl $0x14, -0x4(%rbp)
jge 0x53432
movslq -0x4(%rbp), %rcx
leaq 0x34e715(%rip), %rax    # 0x3a1b20
movzbl (%rax,%rcx), %ecx
movslq -0x4(%rbp), %rdx
leaq 0x169906(%rip), %rax    # 0x1bcd20
imulq $0xe8, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x18(%rax)
movl -0x4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4(%rbp)
jmp 0x533fa
leaq 0x2270df(%rip), %rax    # 0x27a518
cmpq $0x0, (%rax)
jne 0x53444
jmp 0x5385c
leaq 0x24a13d(%rip), %rax    # 0x29d588
cmpq $0x0, (%rax)
je 0x5346e
leaq 0x24a130(%rip), %rax    # 0x29d588
movq (%rax), %rdi
callq 0x9380
leaq 0x24a121(%rip), %rax    # 0x29d588
movq $0x0, (%rax)
leaq 0x17c9f3(%rip), %rax    # 0x1cfe68
movq (%rax), %rdx
leaq 0x347f81(%rip), %rdi    # 0x39b400
leaq 0x128437(%rip), %rsi    # 0x17b8bd
movb $0x0, %al
callq 0x9060
leaq 0x347f6c(%rip), %rdi    # 0x39b400
leaq 0x1311b0(%rip), %rsi    # 0x18464b
callq 0x9b20
movq %rax, %rcx
leaq 0x24a0de(%rip), %rax    # 0x29d588
movq %rcx, (%rax)
leaq 0x24a0d4(%rip), %rax    # 0x29d588
cmpq $0x0, (%rax)
jne 0x534bf
jmp 0x5385c
movl $0x0, -0x4(%rbp)
cmpl $0x9c40, -0x4(%rbp)     # imm = 0x9C40
jge 0x534ec
movslq -0x4(%rbp), %rcx
leaq 0x350466(%rip), %rax    # 0x3a3940
movl $0x0, (%rax,%rcx,4)
movl -0x4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4(%rbp)
jmp 0x534c6
xorps %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x48(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
movl $0x0, -0x4(%rbp)
movslq -0x4(%rbp), %rax
leaq 0x227002(%rip), %rcx    # 0x27a518
cmpq (%rcx), %rax
jge 0x53610
leaq 0x226fca(%rip), %rax    # 0x27a4f0
movq (%rax), %rsi
movslq -0x4(%rbp), %rax
imulq $0xb0, %rax, %rax
addq %rax, %rsi
leaq -0xf8(%rbp), %rdi
movl $0xb0, %edx
callq 0x92c0
leaq 0x227029(%rip), %rax    # 0x27a578
cmpq $0x0, (%rax)
je 0x53574
movsd -0x88(%rbp), %xmm0
leaq 0x227014(%rip), %rax    # 0x27a578
cvtsi2sdq (%rax), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x20(%rbp)
jmp 0x5357c
xorps %xmm0, %xmm0
movsd %xmm0, -0x20(%rbp)
movsd -0x20(%rbp), %xmm0
addsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x40(%rbp)
movsd -0x20(%rbp), %xmm0
movsd -0x20(%rbp), %xmm2
movsd -0x48(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x48(%rbp)
movsd -0x20(%rbp), %xmm0
movsd 0x11bb0c(%rip), %xmm1   # 0x16f0c0
mulsd %xmm1, %xmm0
movsd 0x11bad0(%rip), %xmm1   # 0x16f090
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x8(%rbp)
movl -0x8(%rbp), %eax
addl $0x4e20, %eax           # imm = 0x4E20
movl %eax, -0x8(%rbp)
cmpl $0x0, -0x8(%rbp)
jl 0x535e5
cmpl $0x9c40, -0x8(%rbp)     # imm = 0x9C40
jl 0x535e7
jmp 0x53602
movslq -0x8(%rbp), %rcx
leaq 0x35034e(%rip), %rax    # 0x3a3940
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
leaq 0x350341(%rip), %rax    # 0x3a3940
movl %edx, (%rax,%rcx,4)
movl -0x4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4(%rbp)
jmp 0x5350b
leaq 0x226f01(%rip), %rax    # 0x27a518
cmpq $0x0, (%rax)
je 0x53637
movsd -0x40(%rbp), %xmm0
leaq 0x226eef(%rip), %rax    # 0x27a518
cvtsi2sdq (%rax), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
movsd -0x48(%rbp), %xmm1
leaq 0x226ed5(%rip), %rax    # 0x27a518
cvtsi2sdq (%rax), %xmm0
divsd %xmm0, %xmm1
movsd -0x30(%rbp), %xmm0
movsd -0x30(%rbp), %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movaps 0x11bc14(%rip), %xmm1   # 0x16f290
pand %xmm1, %xmm0
callq 0x9280
movsd %xmm0, -0x38(%rbp)
leaq 0x249ef7(%rip), %rax    # 0x29d588
movq (%rax), %rdi
leaq 0x128229(%rip), %rsi    # 0x17b8c4
movb $0x0, %al
callq 0x9550
movl $0x0, -0xc(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x18(%rbp)
movsd 0x11b9af(%rip), %xmm0   # 0x16f068
movsd %xmm0, -0x28(%rbp)
movl $0x0, -0x4(%rbp)
cmpl $0x9c40, -0x4(%rbp)     # imm = 0x9C40
jge 0x537b4
movl -0x4(%rbp), %eax
subl $0x4e20, %eax           # imm = 0x4E20
cvtsi2sd %eax, %xmm0
movsd 0x11b9da(%rip), %xmm1   # 0x16f0c0
divsd %xmm1, %xmm0
movsd %xmm0, -0x20(%rbp)
movslq -0x4(%rbp), %rcx
leaq 0x350246(%rip), %rax    # 0x3a3940
cmpl $0x0, (%rax,%rcx,4)
je 0x537a4
leaq 0x249e7d(%rip), %rax    # 0x29d588
movq (%rax), %rdi
movsd -0x20(%rbp), %xmm0
movslq -0x4(%rbp), %rcx
leaq 0x350222(%rip), %rax    # 0x3a3940
movl (%rax,%rcx,4), %edx
leaq 0x1281ab(%rip), %rsi    # 0x17b8d3
movb $0x1, %al
callq 0x9550
movslq -0x4(%rbp), %rcx
leaq 0x350206(%rip), %rax    # 0x3a3940
movl (%rax,%rcx,4), %eax
cmpl -0xc(%rbp), %eax
jle 0x5376c
movslq -0x4(%rbp), %rcx
leaq 0x3501f3(%rip), %rax    # 0x3a3940
movl (%rax,%rcx,4), %eax
movl %eax, -0xc(%rbp)
movsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x18(%rbp)
movsd 0x11b903(%rip), %xmm0   # 0x16f068
movsd %xmm0, -0x28(%rbp)
jmp 0x537a2
movslq -0x4(%rbp), %rcx
leaq 0x3501c9(%rip), %rax    # 0x3a3940
movl (%rax,%rcx,4), %eax
cmpl -0xc(%rbp), %eax
jne 0x537a0
movsd -0x20(%rbp), %xmm0
addsd -0x18(%rbp), %xmm0
movsd %xmm0, -0x18(%rbp)
movsd 0x11b8d2(%rip), %xmm0   # 0x16f068
addsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x28(%rbp)
jmp 0x537a2
jmp 0x537a4
jmp 0x537a6
movl -0x4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4(%rbp)
jmp 0x536c5
movsd -0x18(%rbp), %xmm0
divsd -0x28(%rbp), %xmm0
movl -0xc(%rbp), %edx
leaq 0x226d50(%rip), %rax    # 0x27a518
movq (%rax), %rcx
movsd -0x30(%rbp), %xmm1
movsd -0x38(%rbp), %xmm2
leaq 0x34a254(%rip), %rdi    # 0x39da30
leaq 0x1280f9(%rip), %rsi    # 0x17b8dc
movb $0x3, %al
callq 0x9060
leaq 0x249d97(%rip), %rax    # 0x29d588
movq (%rax), %rdi
movsd -0x18(%rbp), %xmm0
divsd -0x28(%rbp), %xmm0
movl -0xc(%rbp), %edx
leaq 0x226d10(%rip), %rax    # 0x27a518
movq (%rax), %rcx
movsd -0x30(%rbp), %xmm1
movsd -0x38(%rbp), %xmm2
leaq 0x1280c0(%rip), %rsi    # 0x17b8dc
movb $0x3, %al
callq 0x9550
leaq 0x249d5e(%rip), %rax    # 0x29d588
movq (%rax), %rdi
callq 0x9380
leaq 0x249d4f(%rip), %rax    # 0x29d588
movq $0x0, (%rax)
leaq 0x17c5f9(%rip), %rax    # 0x1cfe40
movl $0x0, (%rax)
movl $0x1, %edi
movl $0xc, %esi
callq 0x14df0
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopw %cs:(%rax,%rax)