// File: 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
subq $0x10, %rsp
leaq 0x23219d(%rip), %rax    # 0x39a9fc
cmpl $0x0, (%rax)
jne 0x168869
jmp 0x168b7a
leaq 0x23218c(%rip), %rax    # 0x39a9fc
cmpl $0x1, (%rax)
jne 0x168983
movl $0x13, -0x4(%rbp)
cmpl $0x0, -0x4(%rbp)
jl 0x1688ad
movslq -0x4(%rbp), %rcx
leaq 0x5448f(%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 0x168880
leaq 0x111d05(%rip), %rax    # 0x27a5b9
movb $0x0, (%rax)
leaq 0x10f0e6(%rip), %rax    # 0x2779a4
movl $0x0, (%rax)
leaq 0x111cf0(%rip), %rax    # 0x27a5bb
movb $0x0, (%rax)
leaq 0x5444b(%rip), %rax     # 0x1bcd20
movl $0x1, 0x18(%rax)
leaq 0x111b31(%rip), %rax    # 0x27a414
movb $0x0, (%rax)
leaq 0x235143(%rip), %rdi    # 0x39da30
leaq 0x2ff9d(%rip), %rsi     # 0x198891
movb $0x0, %al
callq 0x9060
leaq 0x2045fe(%rip), %rax    # 0x36cf00
movzbl (%rax), %eax
cmpl $0x0, %eax
jne 0x168954
leaq 0x2045f0(%rip), %rax    # 0x36cf01
movb $0x1, (%rax)
leaq 0x2045e6(%rip), %rax    # 0x36cf01
movb (%rax), %cl
leaq 0x2045d4(%rip), %rax    # 0x36cef8
movl (%rax), %eax
leaq 0x204673(%rip), %rdx    # 0x36cfa0
movsd (%rdx), %xmm0
leaq 0x204680(%rip), %rdx    # 0x36cfb8
movsd (%rdx), %xmm1
movss 0x69dc(%rip), %xmm2     # 0x16f320
movl $0x4, %edx
movzbl %cl, %edi
movzbl %al, %esi
callq 0x91b20
xorl %edi, %edi
callq 0x958c0
xorps %xmm0, %xmm0
callq 0x9a4f0
leaq 0x111672(%rip), %rax    # 0x279fdc
movl (%rax), %edi
callq 0xc4d00
leaq 0x232084(%rip), %rax    # 0x39a9fc
movl $0x2, (%rax)
jmp 0x168b7a
leaq 0x232072(%rip), %rax    # 0x39a9fc
cmpl $0x2, (%rax)
jne 0x1689fb
leaq 0x111782(%rip), %rax    # 0x27a118
movl (%rax), %edx
leaq 0x235091(%rip), %rdi    # 0x39da30
leaq 0x2ff1a(%rip), %rsi     # 0x1988c0
movb $0x0, %al
callq 0x9060
leaq 0x111764(%rip), %rax    # 0x27a118
cmpl $0x0, (%rax)
jle 0x1689d0
leaq 0x111758(%rip), %rax    # 0x27a118
movl (%rax), %ecx
addl $-0x1, %ecx
leaq 0x11174c(%rip), %rax    # 0x27a118
movl %ecx, (%rax)
jmp 0x1689f6
movl $0x5, %edi
callq 0x10a090
leaq 0x23201b(%rip), %rax    # 0x39a9fc
movl $0x3, (%rax)
movl $0x2, %edi
movl $0x24, %esi
callq 0x14df0
jmp 0x168b78
leaq 0x231ffa(%rip), %rax    # 0x39a9fc
cmpl $0x3, (%rax)
jne 0x168ad6
leaq 0x1116f2(%rip), %rax    # 0x27a104
movl (%rax), %eax
leaq 0x203455(%rip), %rcx    # 0x36be70
cmpl (%rcx), %eax
jle 0x168a31
leaq 0x231fd6(%rip), %rax    # 0x39a9fc
movl $0x4, (%rax)
jmp 0x168ad1
leaq 0x1116e4(%rip), %rax    # 0x27a11c
movl (%rax), %edi
callq 0xc4890
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %edi
callq 0x10a170
xorl %edi, %edi
callq 0x109c20
leaq 0x1116b8(%rip), %rax    # 0x27a110
movsd (%rax), %xmm0
movsd %xmm0, 0x34c184(%rip)   # 0x4b4be8
leaq 0x111699(%rip), %rax    # 0x27a104
cvtsi2sdl (%rax), %xmm1
movsd 0x34c171(%rip), %xmm0   # 0x4b4be8
divsd %xmm1, %xmm0
movsd %xmm0, 0x34c165(%rip)   # 0x4b4be8
leaq 0x1113be(%rip), %rax    # 0x279e48
movsd (%rax), %xmm1
movsd 0x34c152(%rip), %xmm0   # 0x4b4be8
divsd %xmm1, %xmm0
movsd %xmm0, 0x34c146(%rip)   # 0x4b4be8
leaq 0x11165b(%rip), %rax    # 0x27a104
movl (%rax), %edx
leaq 0x2033be(%rip), %rax    # 0x36be70
movl (%rax), %ecx
movsd 0x34c12c(%rip), %xmm0   # 0x4b4be8
leaq 0x234f6d(%rip), %rdi    # 0x39da30
leaq 0x2fe0e(%rip), %rsi     # 0x1988d8
movb $0x1, %al
callq 0x9060
jmp 0x168b76
leaq 0x231f1f(%rip), %rax    # 0x39a9fc
cmpl $0x4, (%rax)
jne 0x168b67
leaq 0x1115d3(%rip), %rax    # 0x27a0c0
movsd (%rax), %xmm0
addsd 0x34c0ef(%rip), %xmm0   # 0x4b4be8
leaq 0x234f30(%rip), %rdi    # 0x39da30
leaq 0x2fdfa(%rip), %rsi     # 0x198901
movb $0x1, %al
callq 0x9060
leaq 0x1115ab(%rip), %rax    # 0x27a0c0
movsd (%rax), %xmm0
addsd 0x34c0c7(%rip), %xmm0   # 0x4b4be8
callq 0x9a4f0
leaq 0x1114af(%rip), %rax    # 0x279fdc
movl (%rax), %edi
callq 0xc4d00
leaq 0x1119cd(%rip), %rax    # 0x27a508
movq (%rax), %rcx
leaq 0x11194b(%rip), %rax    # 0x27a490
movq %rcx, 0x8(%rax)
movl $0xca, %edi
callq 0x9ec0
callq 0x12ae0
leaq 0x231e9d(%rip), %rax    # 0x39a9fc
movl $0x0, (%rax)
jmp 0x168b74
leaq 0x231e8e(%rip), %rax    # 0x39a9fc
movl $0x0, (%rax)
jmp 0x168b76
jmp 0x168b78
jmp 0x168b7a
addq $0x10, %rsp
popq %rbp
retq