// File: /workspace/github_cpp_makefile_star2_O3/jkingan[P]ladyheather/heathmsc.cpp
// Function: tune_ticc()
void tune_ticc()
{
int i;
static int ticc_time = 0;
static int old_ticc_mode;
static int fudge_val = 0;
static int cha_time2_val = 0;
static int chb_time2_val = 0;
unsigned port;

   if(dac_dac == 0) {
      return;
   }

   if(rcvr_type == TICC_RCVR) port = RCVR_PORT;
   else if(ticc_port_open())  port = TICC_PORT;
   else {
      return;
   }

   if(dac_dac == 1) {  // setup ticc to calculate new FUDGE and TIME2 parameters
      for(i=NUM_PLOTS+DERIVED_PLOTS-1; i>=0; i--) {
         plot[i].show_plot = 0;
      }
      plot_adev_data = 0;
      adev_decades_shown = 0;
      plot_sat_count = 0;
      plot[FOUR].show_plot = 1;
      plot[FIVE].show_plot = 1;
      plot[SIX].show_plot = 1;
      pause_data = 0;

      old_ticc_mode = ticc_mode;

      sprintf(plot_title, "Restting TICC fudge factors.  Mode:%c", ticc_mode);
      redraw_screen();
      strcpy(edit_buffer, "0 0");
      edit_ticc_fudge();

      sprintf(plot_title, "Restting TICC TIME2 factors");
      redraw_screen();
      strcpy(edit_buffer, "0 0");
      edit_ticc_time2();

      sprintf(plot_title, "Setting TICC debug mode");
      redraw_screen();
      strcpy(edit_buffer, "D");
      edit_ticc_mode();
      redraw_screen();

      dac_dac = 2;
      ticc_time = 0;

      fudge_val = (int) fudge_a;
      cha_time2_val = (int) time2_a;
      chb_time2_val = (int) time2_b;

   }
   else if(dac_dac == 2) {   // collect data
      ++ticc_time;
      if(ticc_time < 5) {
         cha_time2_sum = chb_time2_sum = 0.0;
         cha_time2_count = chb_time2_count = 0.0;
         fudge_sum = fudge_count = 0.0;
      }
      else {
if(ticc_time == 10) {   // keep plots from bouncing around
   plot[FOUR].plot_center = (float) cha_time2_val;
   plot[FOUR].float_center = 0;
   plot[FIVE].plot_center = (float) chb_time2_val;
   plot[FIVE].float_center = 0;
   plot[SIX].plot_center = (float) fudge_val;
   plot[SIX].float_center = 0;
}
         if(fudge_count) fudge_val = (int) ((fudge_sum / fudge_count)*1.0E12);
         if(cha_time2_count) cha_time2_val = (int) ((cha_time2_sum / cha_time2_count) + 0.50);
         if(chb_time2_count) chb_time2_val = (int) ((chb_time2_sum / chb_time2_count) + 0.50);
      }

      sprintf(plot_title, "Gathering data %d of %d secs: fudge:%d    chA TIME2:%d    chB TIME2b:%d", 
         ticc_time,ticc_tune_time, fudge_val, cha_time2_val,chb_time2_val);
      if(ticc_time >= ticc_tune_time) dac_dac = 3;
   }
   else if(dac_dac >= 3) {
pause_data = 1;         // stop capturing queue data
dont_reset_queues = 1;  // keep old queue data so we can review the autotune data
      sprintf(plot_title, "Setting chA fudge to %d ps", fudge_val);
      redraw_screen();
      sprintf(edit_buffer, "%d 0", fudge_val);
      edit_ticc_fudge();

      sprintf(plot_title, "Setting TIME2 values to chA:%d  chB:%d", cha_time2_val,chb_time2_val);
      redraw_screen();
      sprintf(edit_buffer, "%d %d", cha_time2_val,chb_time2_val);
      edit_ticc_time2();
      
      sprintf(plot_title, "Restoring TICC mode to %c", old_ticc_mode);
      redraw_screen();
      sprintf(edit_buffer, "%c", old_ticc_mode);
      edit_ticc_mode();

      dac_dac = 0;
   }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
leaq 0x236b74(%rip), %r15    # 0x33b81c
movl (%r15), %eax
testl %eax, %eax
je 0x1050ac
leaq 0x113c16(%rip), %rcx    # 0x2188d0
cmpq $0x10000, (%rcx)        # imm = 0x10000
je 0x104cd3
callq 0x702e7
testl %eax, %eax
je 0x1050ac
movl (%r15), %eax
cmpl $0x2, %eax
je 0x104e21
cmpl $0x1, %eax
jne 0x104e7c
movl $0x1150, %ecx           # imm = 0x1150
leaq 0x5903f(%rip), %rax     # 0x15dd30
movl $0x0, (%rax,%rcx)
addq $-0xe8, %rcx
cmpq $-0xd0, %rcx
jne 0x104cf1
leaq 0x1166ca(%rip), %rcx    # 0x21b3d9
xorl %r14d, %r14d
movb %r14b, (%rcx)
leaq 0x113aa8(%rip), %rcx    # 0x2187c4
movl %r14d, (%rcx)
leaq 0x1166b5(%rip), %rcx    # 0x21b3db
movb %r14b, (%rcx)
movl $0x1, %ecx
movl %ecx, 0x670(%rax)
movl %ecx, 0x758(%rax)
movl %ecx, 0x840(%rax)
leaq 0x1164ed(%rip), %rax    # 0x21b234
movb %r14b, (%rax)
leaq 0x207fbb(%rip), %rax    # 0x30cd0c
movl (%rax), %edx
movl %edx, 0x35088f(%rip)    # 0x4555e8
leaq 0x239af0(%rip), %rbx    # 0x33e850
leaq 0x30eae(%rip), %rsi     # 0x135c15
movq %rbx, %rdi
xorl %eax, %eax
callq 0x9060
callq 0xfa97
leaq 0x23a563(%rip), %r12    # 0x33f2e0
movl $0x302030, %r13d        # imm = 0x302030
movl %r13d, (%r12)
callq 0x35819
movups 0x30eb4(%rip), %xmm0    # 0x135c47
movups %xmm0, 0xc(%rbx)
movups 0x30e9d(%rip), %xmm0    # 0x135c3b
movaps %xmm0, (%rbx)
callq 0xfa97
movl %r13d, (%r12)
callq 0x35927
movabsq $0x65646f6d206775, %rax # imm = 0x65646F6D206775
movq %rax, 0x10(%rbx)
movupd 0x30e92(%rip), %xmm0    # 0x135c57
movapd %xmm0, (%rbx)
callq 0xfa97
movw $0x44, (%r12)
callq 0x3564f
callq 0xfa97
movl $0x2, (%r15)
leaq 0x207f63(%rip), %rax    # 0x30cd50
cvttsd2si (%rax), %eax
movl %r14d, 0x3507ec(%rip)   # 0x4555e4
movl %eax, 0x3507ee(%rip)    # 0x4555ec
leaq 0x207f83(%rip), %rax    # 0x30cd88
movl (%rax), %eax
movl %eax, 0x3507e3(%rip)    # 0x4555f0
leaq 0x207f78(%rip), %rax    # 0x30cd8c
movl (%rax), %eax
movl %eax, 0x3507d8(%rip)    # 0x4555f4
jmp 0x1050ac
movl 0x3507bd(%rip), %eax    # 0x4555e4
leal 0x1(%rax), %edx
movl %edx, 0x3507b4(%rip)    # 0x4555e4
cmpl $0x3, %eax
jg 0x104f61
leaq 0x207a70(%rip), %rax    # 0x30c8b0
xorl %ecx, %ecx
movq %rcx, (%rax)
leaq 0x207a5c(%rip), %rax    # 0x30c8a8
movq %rcx, (%rax)
leaq 0x207a72(%rip), %rax    # 0x30c8c8
movq %rcx, (%rax)
leaq 0x207a60(%rip), %rax    # 0x30c8c0
movq %rcx, (%rax)
leaq 0x207a66(%rip), %rax    # 0x30c8d0
movq %rcx, (%rax)
leaq 0x207a44(%rip), %rax    # 0x30c8b8
movq %rcx, (%rax)
jmp 0x105066
cmpl $0x3, %eax
jl 0x1050ac
leaq 0x1163a8(%rip), %rax    # 0x21b234
movb $0x1, (%rax)
leaq 0x6bdca(%rip), %rax     # 0x170c60
movl $0x1, (%rax)
movl 0x35074a(%rip), %edx    # 0x4555ec
leaq 0x2399a7(%rip), %rbx    # 0x33e850
leaq 0x30e07(%rip), %rsi     # 0x135cb7
movq %rbx, %rdi
xorl %eax, %eax
callq 0x9060
callq 0xfa97
movl 0x350727(%rip), %edx    # 0x4555ec
leaq 0x23a414(%rip), %r14    # 0x33f2e0
leaq 0x30dff(%rip), %rsi     # 0x135cd2
movq %r14, %rdi
xorl %eax, %eax
callq 0x9060
callq 0x35819
movl 0x350708(%rip), %edx    # 0x4555f0
movl 0x350706(%rip), %ecx    # 0x4555f4
leaq 0x30de2(%rip), %rsi     # 0x135cd7
movq %rbx, %rdi
xorl %eax, %eax
callq 0x9060
callq 0xfa97
movl 0x3506e6(%rip), %edx    # 0x4555f0
movl 0x3506e4(%rip), %ecx    # 0x4555f4
leaq 0x2a5a5(%rip), %rsi     # 0x12f4bc
movq %r14, %rdi
xorl %eax, %eax
callq 0x9060
callq 0x35927
movl 0x3506bc(%rip), %edx    # 0x4555e8
leaq 0x30dcb(%rip), %rsi     # 0x135cfe
movq %rbx, %rdi
xorl %eax, %eax
callq 0x9060
callq 0xfa97
movb 0x3506a0(%rip), %al     # 0x4555e8
movb %al, (%r14)
movb $0x0, 0x1(%r14)
callq 0x3564f
movl $0x0, (%r15)
jmp 0x1050ac
cmpl $0xa, %edx
jne 0x104fc3
cvtsi2ssl 0x350682(%rip), %xmm0 # 0x4555f0
cvtss2sd %xmm0, %xmm0
leaq 0x58db7(%rip), %rax     # 0x15dd30
movsd %xmm0, 0x680(%rax)
xorl %ecx, %ecx
movl %ecx, 0x674(%rax)
xorps %xmm0, %xmm0
cvtsi2ssl 0x350660(%rip), %xmm0 # 0x4555f4
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x768(%rax)
xorps %xmm0, %xmm0
cvtsi2ssl 0x350641(%rip), %xmm0 # 0x4555ec
movl %ecx, 0x75c(%rax)
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x850(%rax)
movl %ecx, 0x844(%rax)
leaq 0x207906(%rip), %rax    # 0x30c8d0
movsd (%rax), %xmm1
xorps %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jne 0x104fd9
jnp 0x104ffa
leaq 0x2078d8(%rip), %rax    # 0x30c8b8
movsd (%rax), %xmm2
divsd %xmm1, %xmm2
mulsd 0x4168(%rip), %xmm2     # 0x109158
cvttsd2si %xmm2, %eax
movl %eax, 0x3505f2(%rip)    # 0x4555ec
leaq 0x2078bf(%rip), %rax    # 0x30c8c0
movsd (%rax), %xmm1
ucomisd %xmm0, %xmm1
jne 0x10500d
jnp 0x10502e
leaq 0x207894(%rip), %rax    # 0x30c8a8
movsd (%rax), %xmm0
divsd %xmm1, %xmm0
addsd 0x4044(%rip), %xmm0     # 0x109068
cvttsd2si %xmm0, %eax
movl %eax, 0x3505c2(%rip)    # 0x4555f0
leaq 0x207893(%rip), %rax    # 0x30c8c8
movsd (%rax), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x105045
jnp 0x105066
leaq 0x207864(%rip), %rax    # 0x30c8b0
movsd (%rax), %xmm1
divsd %xmm0, %xmm1
addsd 0x400c(%rip), %xmm1     # 0x109068
cvttsd2si %xmm1, %eax
movl %eax, 0x35058e(%rip)    # 0x4555f4
leaq 0x207c23(%rip), %rbx    # 0x30cc90
movl (%rbx), %ecx
movl 0x350576(%rip), %r8d    # 0x4555ec
movl 0x350573(%rip), %r9d    # 0x4555f0
movl 0x350571(%rip), %eax    # 0x4555f4
movl %eax, (%rsp)
leaq 0x2397c3(%rip), %rdi    # 0x33e850
leaq 0x30bdb(%rip), %rsi     # 0x135c6f
xorl %eax, %eax
callq 0x9060
movl 0x350543(%rip), %eax    # 0x4555e4
cmpl (%rbx), %eax
jl 0x1050ac
movl $0x3, (%r15)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq