// File: /workspace/github_cpp_makefile_star2_O3/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 0x236750(%rip), %rbx    # 0x33b81c
movl (%rbx), %eax
cmpq $0x4, %rax
ja 0x105247
leaq 0x26479(%rip), %rcx     # 0x12b558
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x1150, %ecx           # imm = 0x1150
leaq 0x58c3c(%rip), %rax     # 0x15dd30
movl $0x0, (%rax,%rcx)
addq $-0xe8, %rcx
cmpq $-0xd0, %rcx
jne 0x1050f4
leaq 0x1162c7(%rip), %rcx    # 0x21b3d9
xorl %edx, %edx
movb %dl, (%rcx)
leaq 0x1136a7(%rip), %rcx    # 0x2187c4
movl $0x0, (%rcx)
leaq 0x1162b1(%rip), %rcx    # 0x21b3db
movb %dl, (%rcx)
movl $0x1, 0x18(%rax)
leaq 0x1160fa(%rip), %rax    # 0x21b234
movb %dl, (%rax)
movups 0x30bd5(%rip), %xmm0    # 0x135d18
leaq 0x239706(%rip), %rax    # 0x33e850
movaps %xmm0, (%rax)
movups 0x30bd4(%rip), %xmm0    # 0x135d28
movaps %xmm0, 0x10(%rax)
movupd 0x30bd7(%rip), %xmm0    # 0x135d37
movupd %xmm0, 0x1f(%rax)
leaq 0x208bb4(%rip), %rax    # 0x30dd20
cmpb %dl, (%rax)
jne 0x1051b1
leaq 0x208baa(%rip), %rax    # 0x30dd21
movb $0x1, (%rax)
leaq 0x208b97(%rip), %rax    # 0x30dd18
movzbl (%rax), %esi
leaq 0x208c35(%rip), %rax    # 0x30ddc0
movsd (%rax), %xmm0
leaq 0x208c42(%rip), %rax    # 0x30ddd8
movsd (%rax), %xmm1
movss 0x27746(%rip), %xmm2    # 0x12c8e8
movl $0x1, %edi
movl $0x4, %edx
callq 0x6add8
xorl %edi, %edi
callq 0x6db23
xorpd %xmm0, %xmm0
callq 0x70fb0
leaq 0x115c34(%rip), %rax    # 0x21adfc
movl (%rax), %edi
callq 0x8fbce
movl $0x2, (%rbx)
jmp 0x10529c
leaq 0x115cff(%rip), %r14    # 0x21aee0
movsd (%r14), %xmm0
addsd 0x35040a(%rip), %xmm0   # 0x4555f8
leaq 0x23965b(%rip), %rdi    # 0x33e850
leaq 0x30b8c(%rip), %rsi     # 0x135d88
movb $0x1, %al
callq 0x9060
movsd (%r14), %xmm0
addsd 0x3503e8(%rip), %xmm0   # 0x4555f8
callq 0x70fb0
leaq 0x115be0(%rip), %rax    # 0x21adfc
movl (%rax), %edi
callq 0x8fbce
leaq 0x1160fe(%rip), %rax    # 0x21b328
movq (%rax), %rax
leaq 0x11607c(%rip), %rcx    # 0x21b2b0
movq %rax, 0x8(%rcx)
movl $0xca, %edi
callq 0x9d78
callq 0xfa97
movl $0x0, (%rbx)
jmp 0x10529c
leaq 0x115ce2(%rip), %r14    # 0x21af38
movl (%r14), %edx
leaq 0x2395f0(%rip), %rdi    # 0x33e850
leaq 0x30ae0(%rip), %rsi     # 0x135d47
xorl %eax, %eax
callq 0x9060
movl (%r14), %eax
testl %eax, %eax
jle 0x10530e
decl %eax
movl %eax, (%r14)
jmp 0x10529c
leaq 0x115c9d(%rip), %r15    # 0x21af24
movl (%r15), %eax
leaq 0x2079ff(%rip), %r14    # 0x30cc90
cmpl (%r14), %eax
jle 0x1052a7
movl $0x4, (%rbx)
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
leaq 0x115c8e(%rip), %rax    # 0x21af3c
movl (%rax), %edi
callq 0x8f8f5
movl %eax, %edi
callq 0xc1674
xorl %edi, %edi
callq 0xc13ab
leaq 0x115c66(%rip), %rax    # 0x21af30
movsd (%rax), %xmm0
movl (%r15), %edx
cvtsi2sd %edx, %xmm1
divsd %xmm1, %xmm0
leaq 0x115988(%rip), %rax    # 0x21ac68
divsd (%rax), %xmm0
movsd %xmm0, 0x35030c(%rip)   # 0x4555f8
movl (%r14), %ecx
leaq 0x23955a(%rip), %rdi    # 0x33e850
leaq 0x30a62(%rip), %rsi     # 0x135d5f
movb $0x1, %al
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x9060
movl $0x5, %edi
callq 0xc15e1
movl $0x3, (%rbx)
movl $0x2, %edi
movl $0x24, %esi
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x1118c