// File: /workspace/github_cpp_makefile_star2_O1/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 0x236e31(%rip), %rbx    # 0x33b81c
movl (%rbx), %eax
cmpq $0x4, %rax
ja 0x104b66
leaq 0x26a8a(%rip), %rcx     # 0x12b488
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x1150, %ecx           # imm = 0x1150
leaq 0x5931d(%rip), %rax     # 0x15dd30
movl $0x0, (%rax,%rcx)
addq $-0xe8, %rcx
cmpq $-0xd0, %rcx
jne 0x104a13
leaq 0x1169a8(%rip), %rcx    # 0x21b3d9
xorl %edx, %edx
movb %dl, (%rcx)
leaq 0x113d88(%rip), %rcx    # 0x2187c4
movl $0x0, (%rcx)
leaq 0x116992(%rip), %rcx    # 0x21b3db
movb %dl, (%rcx)
movl $0x1, 0x18(%rax)
leaq 0x1167db(%rip), %rax    # 0x21b234
movb %dl, (%rax)
movups 0x311bd(%rip), %xmm0    # 0x135c1f
leaq 0x239de7(%rip), %rax    # 0x33e850
movaps %xmm0, (%rax)
movups 0x311bc(%rip), %xmm0    # 0x135c2f
movaps %xmm0, 0x10(%rax)
movupd 0x311bf(%rip), %xmm0    # 0x135c3e
movupd %xmm0, 0x1f(%rax)
leaq 0x209295(%rip), %rax    # 0x30dd20
cmpb %dl, (%rax)
jne 0x104ad0
leaq 0x20928b(%rip), %rax    # 0x30dd21
movb $0x1, (%rax)
leaq 0x209278(%rip), %rax    # 0x30dd18
movzbl (%rax), %esi
leaq 0x209316(%rip), %rax    # 0x30ddc0
movsd (%rax), %xmm0
leaq 0x209323(%rip), %rax    # 0x30ddd8
movsd (%rax), %xmm1
movss 0x27d3f(%rip), %xmm2    # 0x12c800
movl $0x1, %edi
movl $0x4, %edx
callq 0x6ad1d
xorl %edi, %edi
callq 0x6dc2d
xorpd %xmm0, %xmm0
callq 0x70f27
leaq 0x116315(%rip), %rax    # 0x21adfc
movl (%rax), %edi
callq 0x8fb74
movl $0x2, (%rbx)
jmp 0x104bbb
leaq 0x1163e0(%rip), %r14    # 0x21aee0
movsd (%r14), %xmm0
addsd 0x350bcb(%rip), %xmm0   # 0x4556d8
leaq 0x239d3c(%rip), %rdi    # 0x33e850
leaq 0x31174(%rip), %rsi     # 0x135c8f
movb $0x1, %al
callq 0x9060
movsd (%r14), %xmm0
addsd 0x350ba9(%rip), %xmm0   # 0x4556d8
callq 0x70f27
leaq 0x1162c1(%rip), %rax    # 0x21adfc
movl (%rax), %edi
callq 0x8fb74
leaq 0x1167df(%rip), %rax    # 0x21b328
movq (%rax), %rax
leaq 0x11675d(%rip), %rcx    # 0x21b2b0
movq %rax, 0x8(%rcx)
movl $0xca, %edi
callq 0x9d80
callq 0xfb1a
movl $0x0, (%rbx)
jmp 0x104bbb
leaq 0x1163c3(%rip), %r14    # 0x21af38
movl (%r14), %edx
leaq 0x239cd1(%rip), %rdi    # 0x33e850
leaq 0x310c8(%rip), %rsi     # 0x135c4e
xorl %eax, %eax
callq 0x9060
movl (%r14), %eax
testl %eax, %eax
jle 0x104c2d
decl %eax
movl %eax, (%r14)
jmp 0x104bbb
leaq 0x11637e(%rip), %r15    # 0x21af24
movl (%r15), %eax
leaq 0x2080e0(%rip), %r14    # 0x30cc90
cmpl (%r14), %eax
jle 0x104bc6
movl $0x4, (%rbx)
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
leaq 0x11636f(%rip), %rax    # 0x21af3c
movl (%rax), %edi
callq 0x8f89a
movl %eax, %edi
callq 0xc10ac
xorl %edi, %edi
callq 0xc0dcd
leaq 0x116347(%rip), %rax    # 0x21af30
movsd (%rax), %xmm0
movl (%r15), %edx
cvtsi2sd %edx, %xmm1
divsd %xmm1, %xmm0
leaq 0x116069(%rip), %rax    # 0x21ac68
divsd (%rax), %xmm0
movsd %xmm0, 0x350acd(%rip)   # 0x4556d8
movl (%r14), %ecx
leaq 0x239c3b(%rip), %rdi    # 0x33e850
leaq 0x3104a(%rip), %rsi     # 0x135c66
movb $0x1, %al
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x9060
movl $0x5, %edi
callq 0xc1019
movl $0x3, (%rbx)
movl $0x2, %edi
movl $0x24, %esi
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x1120f