// File: /workspace/github_cpp_makefile_star2_O2/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 %r12
pushq %rbx
subq $0x10, %rsp
leaq 0x2373bc(%rip), %r15    # 0x32581c
movl (%r15), %eax
testl %eax, %eax
je 0xee852
leaq 0x11445e(%rip), %rcx    # 0x2028d0
cmpq $0x10000, (%rcx)        # imm = 0x10000
je 0xee48b
callq 0x66b41
testl %eax, %eax
je 0xee852
movl (%r15), %eax
cmpl $0x2, %eax
je 0xee5cb
cmpl $0x1, %eax
jne 0xee62a
pushq $0x13
popq %rcx
leaq 0x59889(%rip), %rax     # 0x147d30
testl %ecx, %ecx
js 0xee4bd
movl %ecx, %edx
imulq $0xe8, %rdx, %rdx
andl $0x0, 0x18(%rax,%rdx)
decl %ecx
jmp 0xee4a7
leaq 0x116f15(%rip), %rcx    # 0x2053d9
leaq 0x1142f9(%rip), %rdx    # 0x2027c4
andl $0x0, (%rdx)
xorl %edx, %edx
movb %dl, (%rcx)
leaq 0x116f02(%rip), %rcx    # 0x2053db
movb %dl, (%rcx)
pushq $0x1
popq %rcx
movl %ecx, 0x670(%rax)
movl %ecx, 0x758(%rax)
movl %ecx, 0x840(%rax)
leaq 0x116d3d(%rip), %rax    # 0x205234
movb %dl, (%rax)
leaq 0x20880c(%rip), %rax    # 0x2f6d0c
movl (%rax), %edx
movl %edx, 0x3510e0(%rip)    # 0x43f5e8
leaq 0x23a341(%rip), %rbx    # 0x328850
leaq 0x3160b(%rip), %rsi     # 0x11fb21
movq %rbx, %rdi
xorl %eax, %eax
callq 0x9060
callq 0xf426
leaq 0x23adb4(%rip), %r14    # 0x3292e0
movl $0x302030, %r12d        # imm = 0x302030
movl %r12d, (%r14)
callq 0x32985
movups 0x31612(%rip), %xmm0    # 0x11fb53
movups %xmm0, 0xc(%rbx)
movups 0x315fb(%rip), %xmm0    # 0x11fb47
movaps %xmm0, (%rbx)
callq 0xf426
movl %r12d, (%r14)
callq 0x32a90
movabsq $0x65646f6d206775, %rax # imm = 0x65646F6D206775
movq %rax, 0x10(%rbx)
movups 0x315f2(%rip), %xmm0    # 0x11fb63
movaps %xmm0, (%rbx)
callq 0xf426
movw $0x44, (%r14)
callq 0x327ca
callq 0xf426
andl $0x0, 0x351054(%rip)    # 0x43f5e4
leaq 0x2087b9(%rip), %rax    # 0x2f6d50
cvttsd2si (%rax), %eax
movl $0x2, (%r15)
movl %eax, 0x351044(%rip)    # 0x43f5ec
leaq 0x2087d9(%rip), %rax    # 0x2f6d88
movl (%rax), %eax
movl %eax, 0x351039(%rip)    # 0x43f5f0
leaq 0x2087ce(%rip), %rax    # 0x2f6d8c
movl (%rax), %eax
movl %eax, 0x35102e(%rip)    # 0x43f5f4
jmp 0xee852
movl 0x351013(%rip), %eax    # 0x43f5e4
leal 0x1(%rax), %edx
movl %edx, 0x35100a(%rip)    # 0x43f5e4
cmpl $0x3, %eax
jg 0xee70c
leaq 0x2082c6(%rip), %rax    # 0x2f68b0
andq $0x0, (%rax)
leaq 0x2082b3(%rip), %rax    # 0x2f68a8
andq $0x0, (%rax)
leaq 0x2082c8(%rip), %rax    # 0x2f68c8
andq $0x0, (%rax)
leaq 0x2082b5(%rip), %rax    # 0x2f68c0
andq $0x0, (%rax)
leaq 0x2082ba(%rip), %rax    # 0x2f68d0
andq $0x0, (%rax)
leaq 0x208297(%rip), %rax    # 0x2f68b8
andq $0x0, (%rax)
jmp 0xee80c
cmpl $0x3, %eax
jl 0xee852
leaq 0x116bfa(%rip), %rax    # 0x205234
movb $0x1, (%rax)
leaq 0x6c61c(%rip), %rax     # 0x15ac60
movl $0x1, (%rax)
movl 0x350f9c(%rip), %edx    # 0x43f5ec
leaq 0x23a1f9(%rip), %rbx    # 0x328850
leaq 0x31565(%rip), %rsi     # 0x11fbc3
movq %rbx, %rdi
xorl %eax, %eax
callq 0x9060
callq 0xf426
movl 0x350f79(%rip), %edx    # 0x43f5ec
leaq 0x23ac66(%rip), %r14    # 0x3292e0
leaq 0x3155d(%rip), %rsi     # 0x11fbde
movq %r14, %rdi
xorl %eax, %eax
callq 0x9060
callq 0x32985
movl 0x350f5a(%rip), %edx    # 0x43f5f0
movl 0x350f58(%rip), %ecx    # 0x43f5f4
leaq 0x31540(%rip), %rsi     # 0x11fbe3
movq %rbx, %rdi
xorl %eax, %eax
callq 0x9060
callq 0xf426
movl 0x350f38(%rip), %edx    # 0x43f5f0
movl 0x350f36(%rip), %ecx    # 0x43f5f4
leaq 0x2ad17(%rip), %rsi     # 0x1193dc
movq %r14, %rdi
xorl %eax, %eax
callq 0x9060
callq 0x32a90
movl 0x350f0e(%rip), %edx    # 0x43f5e8
leaq 0x31529(%rip), %rsi     # 0x11fc0a
movq %rbx, %rdi
xorl %eax, %eax
callq 0x9060
callq 0xf426
movb 0x350ef2(%rip), %al     # 0x43f5e8
movb %al, (%r14)
movb $0x0, 0x1(%r14)
callq 0x327ca
andl $0x0, (%r15)
jmp 0xee852
cmpl $0xa, %edx
jne 0xee769
cvtsi2ssl 0x350ed7(%rip), %xmm0 # 0x43f5f0
cvtss2sd %xmm0, %xmm0
leaq 0x5960c(%rip), %rax     # 0x147d30
movsd %xmm0, 0x680(%rax)
andl $0x0, 0x674(%rax)
cvtsi2ssl 0x350eb9(%rip), %xmm0 # 0x43f5f4
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x768(%rax)
andl $0x0, 0x75c(%rax)
cvtsi2ssl 0x350e96(%rip), %xmm0 # 0x43f5ec
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x850(%rax)
andl $0x0, 0x844(%rax)
leaq 0x208160(%rip), %rax    # 0x2f68d0
movsd (%rax), %xmm1
xorps %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jne 0xee77f
jnp 0xee7a0
leaq 0x208132(%rip), %rax    # 0x2f68b8
movsd (%rax), %xmm2
divsd %xmm1, %xmm2
mulsd 0x49c2(%rip), %xmm2     # 0xf3158
cvttsd2si %xmm2, %eax
movl %eax, 0x350e4c(%rip)    # 0x43f5ec
leaq 0x208119(%rip), %rax    # 0x2f68c0
movsd (%rax), %xmm1
ucomisd %xmm0, %xmm1
jne 0xee7b3
jnp 0xee7d4
leaq 0x2080ee(%rip), %rax    # 0x2f68a8
movsd (%rax), %xmm0
divsd %xmm1, %xmm0
addsd 0x489e(%rip), %xmm0     # 0xf3068
cvttsd2si %xmm0, %eax
movl %eax, 0x350e1c(%rip)    # 0x43f5f0
leaq 0x2080ed(%rip), %rax    # 0x2f68c8
movsd (%rax), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0xee7eb
jnp 0xee80c
leaq 0x2080be(%rip), %rax    # 0x2f68b0
movsd (%rax), %xmm1
divsd %xmm0, %xmm1
addsd 0x4866(%rip), %xmm1     # 0xf3068
cvttsd2si %xmm1, %eax
movl %eax, 0x350de8(%rip)    # 0x43f5f4
leaq 0x20847d(%rip), %rbx    # 0x2f6c90
movl (%rbx), %ecx
movl 0x350dd0(%rip), %r8d    # 0x43f5ec
movl 0x350dcd(%rip), %r9d    # 0x43f5f0
movl 0x350dcb(%rip), %eax    # 0x43f5f4
movl %eax, (%rsp)
leaq 0x23a01d(%rip), %rdi    # 0x328850
leaq 0x31341(%rip), %rsi     # 0x11fb7b
xorl %eax, %eax
callq 0x9060
movl 0x350d9d(%rip), %eax    # 0x43f5e4
cmpl (%rbx), %eax
jl 0xee852
movl $0x3, (%r15)
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq