// File: 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
subq $0x10, %rsp
leaq 0x2326dd(%rip), %rax    # 0x39a9fc
cmpl $0x0, (%rax)
jne 0x168329
jmp 0x16883e
leaq 0x10f780(%rip), %rax    # 0x277ab0
cmpq $0x10000, (%rax)        # imm = 0x10000
jne 0x168342
movl $0xa, -0x8(%rbp)
jmp 0x16835c
callq 0x992e0
cmpl $0x0, %eax
je 0x168355
movl $0xd, -0x8(%rbp)
jmp 0x16835a
jmp 0x16883e
jmp 0x16835c
leaq 0x232699(%rip), %rax    # 0x39a9fc
cmpl $0x1, (%rax)
jne 0x1684fc
movl $0x13, -0x4(%rbp)
cmpl $0x0, -0x4(%rbp)
jl 0x1683a0
movslq -0x4(%rbp), %rcx
leaq 0x5499c(%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 0x168373
leaq 0x112212(%rip), %rax    # 0x27a5b9
movb $0x0, (%rax)
leaq 0x10f5f3(%rip), %rax    # 0x2779a4
movl $0x0, (%rax)
leaq 0x1121fd(%rip), %rax    # 0x27a5bb
movb $0x0, (%rax)
leaq 0x54958(%rip), %rax     # 0x1bcd20
movl $0x1, 0x670(%rax)
leaq 0x54947(%rip), %rax     # 0x1bcd20
movl $0x1, 0x758(%rax)
leaq 0x54936(%rip), %rax     # 0x1bcd20
movl $0x1, 0x840(%rax)
leaq 0x112019(%rip), %rax    # 0x27a414
movb $0x0, (%rax)
leaq 0x203ae7(%rip), %rax    # 0x36beec
movl (%rax), %eax
movl %eax, 0x34c7cb(%rip)    # 0x4b4bd8
leaq 0x203ad8(%rip), %rax    # 0x36beec
movl (%rax), %edx
leaq 0x235613(%rip), %rdi    # 0x39da30
leaq 0x30366(%rip), %rsi     # 0x19878a
movb $0x0, %al
callq 0x9060
callq 0x12ae0
leaq 0x236089(%rip), %rdi    # 0x39e4c0
leaq 0x30372(%rip), %rsi     # 0x1987b0
callq 0x9350
callq 0x4b4b0
leaq 0x2355e1(%rip), %rdi    # 0x39da30
leaq 0x3035e(%rip), %rsi     # 0x1987b4
movb $0x0, %al
callq 0x9060
callq 0x12ae0
leaq 0x236057(%rip), %rdi    # 0x39e4c0
leaq 0x30340(%rip), %rsi     # 0x1987b0
callq 0x9350
callq 0x4b5c0
leaq 0x2355af(%rip), %rdi    # 0x39da30
leaq 0x30348(%rip), %rsi     # 0x1987d0
movb $0x0, %al
callq 0x9060
callq 0x12ae0
leaq 0x236025(%rip), %rdi    # 0x39e4c0
leaq 0x10fb7(%rip), %rsi     # 0x179459
callq 0x9350
callq 0x4b270
callq 0x12ae0
leaq 0x232544(%rip), %rax    # 0x39a9fc
movl $0x2, (%rax)
movl $0x0, 0x34c70c(%rip)    # 0x4b4bd4
leaq 0x203a61(%rip), %rax    # 0x36bf30
cvttsd2si (%rax), %eax
movl %eax, 0x34c703(%rip)    # 0x4b4bdc
leaq 0x203a88(%rip), %rax    # 0x36bf68
movl (%rax), %eax
movl %eax, 0x34c6f8(%rip)    # 0x4b4be0
leaq 0x203a7d(%rip), %rax    # 0x36bf6c
movl (%rax), %eax
movl %eax, 0x34c6ed(%rip)    # 0x4b4be4
jmp 0x16883e
leaq 0x2324f9(%rip), %rax    # 0x39a9fc
cmpl $0x2, (%rax)
jne 0x16873a
movl 0x34c6c2(%rip), %eax    # 0x4b4bd4
addl $0x1, %eax
movl %eax, 0x34c6b9(%rip)    # 0x4b4bd4
cmpl $0x5, 0x34c6b2(%rip)    # 0x4b4bd4
jge 0x16857d
leaq 0x203565(%rip), %rax    # 0x36ba90
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
leaq 0x20354f(%rip), %rax    # 0x36ba88
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
leaq 0x203561(%rip), %rax    # 0x36baa8
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
leaq 0x20354b(%rip), %rax    # 0x36baa0
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
leaq 0x20354d(%rip), %rax    # 0x36bab0
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
leaq 0x203527(%rip), %rax    # 0x36ba98
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
jmp 0x1686dc
cmpl $0xa, 0x34c650(%rip)    # 0x4b4bd4
jne 0x16860e
cvtsi2ssl 0x34c64e(%rip), %xmm0 # 0x4b4be0
cvtss2sd %xmm0, %xmm0
leaq 0x54783(%rip), %rax     # 0x1bcd20
movsd %xmm0, 0x680(%rax)
leaq 0x54774(%rip), %rax     # 0x1bcd20
movl $0x0, 0x674(%rax)
cvtsi2ssl 0x34c626(%rip), %xmm0 # 0x4b4be4
cvtss2sd %xmm0, %xmm0
leaq 0x54757(%rip), %rax     # 0x1bcd20
movsd %xmm0, 0x768(%rax)
leaq 0x54748(%rip), %rax     # 0x1bcd20
movl $0x0, 0x75c(%rax)
cvtsi2ssl 0x34c5f2(%rip), %xmm0 # 0x4b4bdc
cvtss2sd %xmm0, %xmm0
leaq 0x5472b(%rip), %rax     # 0x1bcd20
movsd %xmm0, 0x850(%rax)
leaq 0x5471c(%rip), %rax     # 0x1bcd20
movl $0x0, 0x844(%rax)
leaq 0x20349b(%rip), %rax    # 0x36bab0
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x168626
jp 0x168626
jmp 0x168652
leaq 0x20346b(%rip), %rax    # 0x36ba98
movsd (%rax), %xmm0
leaq 0x203478(%rip), %rax    # 0x36bab0
divsd (%rax), %xmm0
movsd 0x6b2c(%rip), %xmm1     # 0x16f170
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x34c58a(%rip)    # 0x4b4bdc
leaq 0x203447(%rip), %rax    # 0x36baa0
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x16866a
jp 0x16866a
jmp 0x168696
leaq 0x203417(%rip), %rax    # 0x36ba88
movsd (%rax), %xmm0
leaq 0x203424(%rip), %rax    # 0x36baa0
divsd (%rax), %xmm0
movsd 0x6a08(%rip), %xmm1     # 0x16f090
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x34c54a(%rip)    # 0x4b4be0
leaq 0x20340b(%rip), %rax    # 0x36baa8
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1686ae
jp 0x1686ae
jmp 0x1686da
leaq 0x2033db(%rip), %rax    # 0x36ba90
movsd (%rax), %xmm0
leaq 0x2033e8(%rip), %rax    # 0x36baa8
divsd (%rax), %xmm0
movsd 0x69c4(%rip), %xmm1     # 0x16f090
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x34c50a(%rip)    # 0x4b4be4
jmp 0x1686dc
movl 0x34c4f2(%rip), %edx    # 0x4b4bd4
leaq 0x203787(%rip), %rax    # 0x36be70
movl (%rax), %ecx
movl 0x34c4ea(%rip), %r8d    # 0x4b4bdc
movl 0x34c4e7(%rip), %r9d    # 0x4b4be0
movl 0x34c4e5(%rip), %eax    # 0x4b4be4
leaq 0x23532a(%rip), %rdi    # 0x39da30
leaq 0x300db(%rip), %rsi     # 0x1987e8
movl %eax, (%rsp)
movb $0x0, %al
callq 0x9060
movl 0x34c4b7(%rip), %eax    # 0x4b4bd4
leaq 0x20374c(%rip), %rcx    # 0x36be70
cmpl (%rcx), %eax
jl 0x168735
leaq 0x2322cd(%rip), %rax    # 0x39a9fc
movl $0x3, (%rax)
jmp 0x16883c
leaq 0x2322bb(%rip), %rax    # 0x39a9fc
cmpl $0x3, (%rax)
jl 0x16883a
leaq 0x111cc3(%rip), %rax    # 0x27a414
movb $0x1, (%rax)
leaq 0x676e5(%rip), %rax     # 0x1cfe40
movl $0x1, (%rax)
movl 0x34c475(%rip), %edx    # 0x4b4bdc
leaq 0x2352c2(%rip), %rdi    # 0x39da30
leaq 0x300bb(%rip), %rsi     # 0x198830
movb $0x0, %al
callq 0x9060
callq 0x12ae0
movl 0x34c455(%rip), %edx    # 0x4b4bdc
leaq 0x235d32(%rip), %rdi    # 0x39e4c0
leaq 0x300b6(%rip), %rsi     # 0x19884b
movb $0x0, %al
callq 0x9060
callq 0x4b4b0
movl 0x34c439(%rip), %edx    # 0x4b4be0
movl 0x34c437(%rip), %ecx    # 0x4b4be4
leaq 0x23527c(%rip), %rdi    # 0x39da30
leaq 0x30095(%rip), %rsi     # 0x198850
movb $0x0, %al
callq 0x9060
callq 0x12ae0
movl 0x34c413(%rip), %edx    # 0x4b4be0
movl 0x34c411(%rip), %ecx    # 0x4b4be4
leaq 0x235ce6(%rip), %rdi    # 0x39e4c0
leaq 0x297d7(%rip), %rsi     # 0x191fb8
movb $0x0, %al
callq 0x9060
callq 0x4b5c0
movl 0x34c3e5(%rip), %edx    # 0x4b4bd8
leaq 0x235236(%rip), %rdi    # 0x39da30
leaq 0x30076(%rip), %rsi     # 0x198877
movb $0x0, %al
callq 0x9060
callq 0x12ae0
movl 0x34c3c5(%rip), %edx    # 0x4b4bd8
leaq 0x235ca6(%rip), %rdi    # 0x39e4c0
leaq 0x27de1(%rip), %rsi     # 0x190602
movb $0x0, %al
callq 0x9060
callq 0x4b270
leaq 0x2321c8(%rip), %rax    # 0x39a9fc
movl $0x0, (%rax)
jmp 0x16883c
jmp 0x16883e
addq $0x10, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)