// File: /workspace/github_cpp_makefile_star2_O2/jkingan[P]ladyheather/heathmsc.cpp
// Function: tune_x72()
void tune_x72()
{
int i;
static double slope;
#define DDS_RESET_DELAY 5

   if(dac_dac == 0) {  // not tuning
      return;
   }

   if(dac_dac == 1) {  // setup to calculate tuning word
      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[PPS].show_plot = 1;
      pause_data = 0;

      sprintf(plot_title, "Reseting DAC tune word to 0.0E-12 and TIC to 0");
      if(pps_enabled == 0) {  // PPS output must be enabled in order for TIC command to work
         user_pps_enable = 1;
         set_pps(user_pps_enable, pps_polarity,  delay_value, pps1_delay,  300.0, 4);
      }
      set_x72_discipline(0);
      set_x72_dds(0.0);
      set_x72_tic(x72_pps);

      dac_dac = 2;  // now wait for TIC change to take effect
   }
   else if(dac_dac == 2) {   // wait for TIC set to complete
      sprintf(plot_title, "Waiting for TIC set: %d", x72_tic_timer);
      if(x72_tic_timer > 0) {
         --x72_tic_timer;
      }
      else {  // start collecting data
         new_x72_interval(5);
         dac_dac = 3;
         new_queue(RESET_PLOT_Q, 36);
      }
   }
   else if(dac_dac == 3) {   // collecting data
      if(x72_pps_interval > ticc_tune_time) {  // analyze data and set DDS word
         dac_dac = 4;
      }
      else {
         i = fix_x72_tics(x72_ival);  // fixup possible wrap near 0
         x72_update_pps_list(i);      // add fixed up ival to pps list
         x72_pps_stats(0);            // calculate trend line
         slope = x72_pps_trend;
         slope /= (double) x72_pps_interval;
         slope /= (double) x72_osc;

         sprintf(plot_title, "Gathering data: %d of %d secs.  Slope:%g", 
            x72_pps_interval, ticc_tune_time, slope);
      }
   }
   else if(dac_dac == 4) {  // set the DDS tuning word
      sprintf(plot_title, "Setting DDS tune word to %g", last_x72_dds+slope);
      set_x72_dds(last_x72_dds + slope);
      set_x72_tic(x72_pps);
      mark_q_entry[1] = plot_q_in;
      BEEP(202);
      redraw_screen();

      dac_dac = 0;
   }
   else {  // should never happen
      dac_dac = 0;
   }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
leaq 0x236fac(%rip), %rbx    # 0x32581c
movl (%rbx), %eax
cmpq $0x4, %rax
ja 0xee91d
leaq 0x26b25(%rip), %rcx     # 0x1153a8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
pushq $0x13
popq %rcx
leaq 0x5949a(%rip), %rax     # 0x147d30
testl %ecx, %ecx
js 0xee97e
movl %ecx, %edx
imulq $0xe8, %rdx, %rdx
andl $0x0, 0x18(%rax,%rdx)
decl %ecx
jmp 0xee896
leaq 0x116629(%rip), %r14    # 0x204ee0
movsd (%r14), %xmm0
addsd 0x350d34(%rip), %xmm0   # 0x43f5f8
leaq 0x239f85(%rip), %rdi    # 0x328850
leaq 0x313c2(%rip), %rsi     # 0x11fc94
movb $0x1, %al
callq 0x9060
movsd (%r14), %xmm0
addsd 0x350d12(%rip), %xmm0   # 0x43f5f8
callq 0x6779b
leaq 0x11650a(%rip), %rax    # 0x204dfc
movl (%rax), %edi
callq 0x82eb3
leaq 0x116a28(%rip), %rax    # 0x205328
movq (%rax), %rax
leaq 0x1169a6(%rip), %rcx    # 0x2052b0
movq %rax, 0x8(%rcx)
movl $0xca, %edi
callq 0x9d23
callq 0xf426
andl $0x0, (%rbx)
jmp 0xeea3e
leaq 0x11660c(%rip), %r14    # 0x204f38
movl (%r14), %edx
leaq 0x239f1a(%rip), %rdi    # 0x328850
leaq 0x31316(%rip), %rsi     # 0x11fc53
xorl %eax, %eax
callq 0x9060
movl (%r14), %eax
testl %eax, %eax
jle 0xeeab0
decl %eax
movl %eax, (%r14)
jmp 0xeea3e
leaq 0x1165c4(%rip), %r15    # 0x204f24
movl (%r15), %eax
leaq 0x208326(%rip), %r14    # 0x2f6c90
cmpl (%r14), %eax
jle 0xeea49
movl $0x4, (%rbx)
jmp 0xeea3e
leaq 0x116a54(%rip), %rcx    # 0x2053d9
leaq 0x113e38(%rip), %rdx    # 0x2027c4
andl $0x0, (%rdx)
xorl %edx, %edx
movb %dl, (%rcx)
leaq 0x116a41(%rip), %rcx    # 0x2053db
movb %dl, (%rcx)
movl $0x1, 0x18(%rax)
leaq 0x11688a(%rip), %rax    # 0x205234
movb %dl, (%rax)
movups 0x31271(%rip), %xmm0    # 0x11fc24
leaq 0x239e96(%rip), %rax    # 0x328850
movaps %xmm0, (%rax)
movups 0x31270(%rip), %xmm0    # 0x11fc34
movaps %xmm0, 0x10(%rax)
movups 0x31274(%rip), %xmm0    # 0x11fc43
movups %xmm0, 0x1f(%rax)
leaq 0x209346(%rip), %rax    # 0x2f7d20
cmpb %dl, (%rax)
jne 0xeea1b
leaq 0x20933c(%rip), %rax    # 0x2f7d21
movb $0x1, (%rax)
leaq 0x209329(%rip), %rax    # 0x2f7d18
movzbl (%rax), %esi
leaq 0x2093c7(%rip), %rax    # 0x2f7dc0
movsd (%rax), %xmm0
leaq 0x2093d4(%rip), %rax    # 0x2f7dd8
movsd (%rax), %xmm1
pushq $0x1
popq %rdi
movss 0x27df5(%rip), %xmm2    # 0x116808
pushq $0x4
popq %rdx
callq 0x61e97
xorl %edi, %edi
callq 0x647f2
xorps %xmm0, %xmm0
callq 0x6779b
leaq 0x1163cb(%rip), %rax    # 0x204dfc
movl (%rax), %edi
callq 0x82eb3
movl $0x2, (%rbx)
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1164ec(%rip), %rax    # 0x204f3c
movl (%rax), %edi
callq 0x82be7
movl %eax, %edi
callq 0xb06eb
xorl %edi, %edi
callq 0xb0447
leaq 0x1164c4(%rip), %rax    # 0x204f30
movsd (%rax), %xmm0
movl (%r15), %edx
cvtsi2sd %edx, %xmm1
divsd %xmm1, %xmm0
leaq 0x1161e6(%rip), %rax    # 0x204c68
divsd (%rax), %xmm0
movsd %xmm0, 0x350b6a(%rip)   # 0x43f5f8
movl (%r14), %ecx
leaq 0x239db8(%rip), %rdi    # 0x328850
leaq 0x311cc(%rip), %rsi     # 0x11fc6b
movb $0x1, %al
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x9060
pushq $0x5
popq %rdi
callq 0xb0658
movl $0x3, (%rbx)
pushq $0x2
popq %rdi
pushq $0x24
popq %rsi
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x10a99