// File: /workspace/github_cpp_makefile_star2_O1/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 0x237254(%rip), %r15    # 0x33b81c
cmpl $0x0, (%r15)
je 0x1049cb
leaq 0x1142f7(%rip), %rax    # 0x2188d0
cmpq $0x10000, (%rax)        # imm = 0x10000
je 0x1045ef
callq 0x701f2
testl %eax, %eax
je 0x1049cb
movl (%r15), %eax
cmpl $0x2, %eax
je 0x104740
cmpl $0x1, %eax
jne 0x10479b
movl $0x1150, %ecx           # imm = 0x1150
leaq 0x59720(%rip), %rax     # 0x15dd30
movl $0x0, (%rax,%rcx)
addq $-0xe8, %rcx
cmpq $-0xd0, %rcx
jne 0x104610
leaq 0x116dab(%rip), %rcx    # 0x21b3d9
xorl %r14d, %r14d
movb %r14b, (%rcx)
leaq 0x114189(%rip), %rcx    # 0x2187c4
movl %r14d, (%rcx)
leaq 0x116d96(%rip), %rcx    # 0x21b3db
movb %r14b, (%rcx)
movl $0x1, %ecx
movl %ecx, 0x670(%rax)
movl %ecx, 0x758(%rax)
movl %ecx, 0x840(%rax)
leaq 0x116bce(%rip), %rax    # 0x21b234
movb %r14b, (%rax)
leaq 0x20869c(%rip), %rax    # 0x30cd0c
movl (%rax), %edx
movl %edx, 0x351050(%rip)    # 0x4556c8
leaq 0x23a1d1(%rip), %rbx    # 0x33e850
leaq 0x31496(%rip), %rsi     # 0x135b1c
movq %rbx, %rdi
xorl %eax, %eax
callq 0x9060
callq 0xfb1a
leaq 0x23ac44(%rip), %r12    # 0x33f2e0
movl $0x302030, %r13d        # imm = 0x302030
movl %r13d, (%r12)
callq 0x35734
movups 0x3149c(%rip), %xmm0    # 0x135b4e
movups %xmm0, 0xc(%rbx)
movups 0x31485(%rip), %xmm0    # 0x135b42
movaps %xmm0, (%rbx)
callq 0xfb1a
movl %r13d, (%r12)
callq 0x35842
movabsq $0x65646f6d206775, %rax # imm = 0x65646F6D206775
movq %rax, 0x10(%rbx)
movupd 0x3147a(%rip), %xmm0    # 0x135b5e
movapd %xmm0, (%rbx)
callq 0xfb1a
movw $0x44, (%r12)
callq 0x3556e
callq 0xfb1a
movl $0x2, (%r15)
leaq 0x208644(%rip), %rax    # 0x30cd50
cvttsd2si (%rax), %eax
movl %r14d, 0x350fad(%rip)   # 0x4556c4
movl %eax, 0x350faf(%rip)    # 0x4556cc
leaq 0x208664(%rip), %rax    # 0x30cd88
movl (%rax), %eax
movl %eax, 0x350fa4(%rip)    # 0x4556d0
leaq 0x208659(%rip), %rax    # 0x30cd8c
movl (%rax), %eax
movl %eax, 0x350f99(%rip)    # 0x4556d4
jmp 0x1049cb
movl 0x350f7e(%rip), %eax    # 0x4556c4
leal 0x1(%rax), %edx
movl %edx, 0x350f75(%rip)    # 0x4556c4
cmpl $0x3, %eax
jg 0x104880
leaq 0x208151(%rip), %rax    # 0x30c8b0
xorl %ecx, %ecx
movq %rcx, (%rax)
leaq 0x20813d(%rip), %rax    # 0x30c8a8
movq %rcx, (%rax)
leaq 0x208153(%rip), %rax    # 0x30c8c8
movq %rcx, (%rax)
leaq 0x208141(%rip), %rax    # 0x30c8c0
movq %rcx, (%rax)
leaq 0x208147(%rip), %rax    # 0x30c8d0
movq %rcx, (%rax)
leaq 0x208125(%rip), %rax    # 0x30c8b8
movq %rcx, (%rax)
jmp 0x104985
cmpl $0x3, %eax
jl 0x1049cb
leaq 0x116a89(%rip), %rax    # 0x21b234
movb $0x1, (%rax)
leaq 0x6c4ab(%rip), %rax     # 0x170c60
movl $0x1, (%rax)
movl 0x350f0b(%rip), %edx    # 0x4556cc
leaq 0x23a088(%rip), %rbx    # 0x33e850
leaq 0x313ef(%rip), %rsi     # 0x135bbe
movq %rbx, %rdi
xorl %eax, %eax
callq 0x9060
callq 0xfb1a
movl 0x350ee8(%rip), %edx    # 0x4556cc
leaq 0x23aaf5(%rip), %r14    # 0x33f2e0
leaq 0x313e7(%rip), %rsi     # 0x135bd9
movq %r14, %rdi
xorl %eax, %eax
callq 0x9060
callq 0x35734
movl 0x350ec9(%rip), %edx    # 0x4556d0
movl 0x350ec7(%rip), %ecx    # 0x4556d4
leaq 0x313ca(%rip), %rsi     # 0x135bde
movq %rbx, %rdi
xorl %eax, %eax
callq 0x9060
callq 0xfb1a
movl 0x350ea7(%rip), %edx    # 0x4556d0
movl 0x350ea5(%rip), %ecx    # 0x4556d4
leaq 0x2ab96(%rip), %rsi     # 0x12f3cc
movq %r14, %rdi
xorl %eax, %eax
callq 0x9060
callq 0x35842
movl 0x350e7d(%rip), %edx    # 0x4556c8
leaq 0x313b3(%rip), %rsi     # 0x135c05
movq %rbx, %rdi
xorl %eax, %eax
callq 0x9060
callq 0xfb1a
movb 0x350e61(%rip), %al     # 0x4556c8
movb %al, (%r14)
movb $0x0, 0x1(%r14)
callq 0x3556e
movl $0x0, (%r15)
jmp 0x1049cb
cmpl $0xa, %edx
jne 0x1048e2
cvtsi2ssl 0x350e43(%rip), %xmm0 # 0x4556d0
cvtss2sd %xmm0, %xmm0
leaq 0x59498(%rip), %rax     # 0x15dd30
movsd %xmm0, 0x680(%rax)
xorl %ecx, %ecx
movl %ecx, 0x674(%rax)
xorps %xmm0, %xmm0
cvtsi2ssl 0x350e21(%rip), %xmm0 # 0x4556d4
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x768(%rax)
xorps %xmm0, %xmm0
cvtsi2ssl 0x350e02(%rip), %xmm0 # 0x4556cc
movl %ecx, 0x75c(%rax)
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x850(%rax)
movl %ecx, 0x844(%rax)
leaq 0x207fe7(%rip), %rax    # 0x30c8d0
movsd (%rax), %xmm1
xorps %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jne 0x1048f8
jnp 0x104919
leaq 0x207fb9(%rip), %rax    # 0x30c8b8
movsd (%rax), %xmm2
divsd %xmm1, %xmm2
mulsd 0x4841(%rip), %xmm2     # 0x109150
cvttsd2si %xmm2, %eax
movl %eax, 0x350db3(%rip)    # 0x4556cc
leaq 0x207fa0(%rip), %rax    # 0x30c8c0
movsd (%rax), %xmm1
ucomisd %xmm0, %xmm1
jne 0x10492c
jnp 0x10494d
leaq 0x207f75(%rip), %rax    # 0x30c8a8
movsd (%rax), %xmm0
divsd %xmm1, %xmm0
addsd 0x4725(%rip), %xmm0     # 0x109068
cvttsd2si %xmm0, %eax
movl %eax, 0x350d83(%rip)    # 0x4556d0
leaq 0x207f74(%rip), %rax    # 0x30c8c8
movsd (%rax), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x104964
jnp 0x104985
leaq 0x207f45(%rip), %rax    # 0x30c8b0
movsd (%rax), %xmm1
divsd %xmm0, %xmm1
addsd 0x46ed(%rip), %xmm1     # 0x109068
cvttsd2si %xmm1, %eax
movl %eax, 0x350d4f(%rip)    # 0x4556d4
leaq 0x208304(%rip), %rbx    # 0x30cc90
movl (%rbx), %ecx
movl 0x350d37(%rip), %r8d    # 0x4556cc
movl 0x350d34(%rip), %r9d    # 0x4556d0
movl 0x350d32(%rip), %eax    # 0x4556d4
movl %eax, (%rsp)
leaq 0x239ea4(%rip), %rdi    # 0x33e850
leaq 0x311c3(%rip), %rsi     # 0x135b76
xorl %eax, %eax
callq 0x9060
movl 0x350d04(%rip), %eax    # 0x4556c4
cmpl (%rbx), %eax
jl 0x1049cb
movl $0x3, (%r15)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq