// File: /workspace/github_cpp_makefile_star2_O1/jkingan[P]ladyheather/heathmsc.cpp
// Function: calc_osc_gain()
void calc_osc_gain()
{
int old_read_only;
float el_filter;

   // dac_dac is the current gain measurement state
   if(dac_dac == 0) return;  // we are not measuring

   if(rcvr_type == TICC_RCVR) {
      tune_ticc();
      return;
   }
   else if(rcvr_type == X72_RCVR) {
      tune_x72();
      return;
   }

   if(dac_dac == 1) {        // initialize dac gain test
      if(!GPSDO) goto tune_res_t;
      if(!GPSDO_TUNE_OK) goto tune_res_t;

      #ifdef OSC_CONTROL
         disable_osc_control();
      #endif
      #ifdef PRECISE_STUFF
         stop_precision_survey(2);
      #endif

      old_read_only = read_only;
      read_only = 1;
      set_el_mask(0.0F);
      read_only = old_read_only;

      hour_lats = 0;         // setup to determine median osc offset values
      hour_lons = 0;
      gain_voltage = dac_voltage; // initial (current) DAC voltage
      set_discipline_mode(SET_DIS_MODE_DISABLE);     // disable disciplining
      set_dac_voltage(gain_voltage+(DATA_SIZE) (DAC_STEP*OSC_TUNE_GAIN), 1);  // bump the osc voltage up
      ++dac_dac;
      sprintf(plot_title, "Determining OSC gain: initializing");
      title_type = OTHER;
   }
   else if(dac_dac <= STABLE_TIME) {    // wait a little while for things to settle
      ++dac_dac;
      sprintf(plot_title, "Determining OSC gain: stabilizing high");
      title_type = OTHER;
   }
   else if(dac_dac == (STABLE_TIME+1)) {    // prepare to start taking data
      dac_dac = HIGH_STATE;
   }
   else if(dac_dac < (HIGH_STATE+1+GAIN_AVG_TIME)) {   // find median osc offset value with dac set high
      hour_lats = add_to_bin((double)osc_offset, lat_hr_bins, hour_lats);
      sprintf(plot_title, "Determining OSC gain: high osc(%d)=%f", hour_lats, (float) lat_hr_bins[hour_lats/2]);
      title_type = OTHER;
      ++dac_dac;
   }
   else if(dac_dac == (HIGH_STATE+1+GAIN_AVG_TIME)) {  // prepare to measure osc with dac at low value
      sprintf(plot_title, "Determining OSC gain: stabilizing low");
      title_type = OTHER;
      set_dac_voltage(gain_voltage - (DATA_SIZE) (DAC_STEP*OSC_TUNE_GAIN), 2);
      dac_dac = WAIT_STATE;
   }
   else if(dac_dac <= (WAIT_STATE+STABLE_TIME)) {  // wait for things to settle down some
      ++dac_dac;
   }
   else if(dac_dac == (WAIT_STATE+1+STABLE_TIME)) {  // prepare to start measuring osc with dac voltage low
      dac_dac = LOW_STATE;
   }
   else if(dac_dac < (LOW_STATE+GAIN_AVG_TIME+1)) {   // calculate the median osc value with dac set low
      ++dac_dac;
      hour_lons = add_to_bin((double)osc_offset, lon_hr_bins, hour_lons);
      sprintf(plot_title, "Determining OSC gain: low(%d)=%f", hour_lons, (float) lon_hr_bins[hour_lons/2]);
      title_type = OTHER;
   }
   else {  // we are done,  calc osc params and store into eeprom
      user_osc_gain  = (float) lon_hr_bins[hour_lons/2];
      user_osc_gain -= (float) lat_hr_bins[hour_lats/2];
      user_osc_gain /= OSC_TUNE_GAIN;

      user_time_constant   = 500.0F;
      user_damping_factor  = 1.0F;    //// !!!! 0.707F;  0.800F
      user_min_volts = min_volts;
      user_max_volts = max_volts;
      user_jam_sync = jam_sync;
      user_max_freq_offset = max_freq_offset;
      user_initial_voltage = gain_voltage;
      if(saw_ntpx) initial_voltage = gain_voltage;

//    sprintf(plot_title, "OSC gain: %f Hz/V", user_osc_gain);
sprintf(plot_title, "gain:%.3f iv=%.3f min=%.3f  max=%.3f  tc=%.3f  damp=%.3f", 
user_osc_gain, user_initial_voltage, user_min_volts, user_max_volts, user_time_constant, user_damping_factor);
title_type = OTHER;

      set_discipline_params(1);       // save params into eeprom
      set_dac_voltage(gain_voltage, 3);  // restore dac voltage
      set_discipline_mode(SET_DIS_MODE_ENABLE);   // re-enable disciplining
      set_discipline_mode(SET_DIS_MODE_JAMSYNC);  // jam sync the pps back in line
      request_all_dis_params();       // display the newest settings

      tune_res_t:
//    set_el_level();                 // set elevation mask level 
//    !!!! res-t does not like back to back config commands, set both params with one operation

      el_filter = (el_mask + (float) good_el_level()) / 2.0F;

      if(res_t && (res_t != RES_T))   set_el_amu(el_filter, 25.0F);  // set signal level mask level in dBc  // !!!!! RES_T_360, etc?
      else if(rcvr_type == TSIP_RCVR) set_el_amu(el_filter, 1.0F);   // set signal level mask level in AMU
      else                            set_el_amu(el_filter, (float) good_sig_level());

      redraw_screen();
      dac_dac = 0;                    // we are done dac-ing around
   }

   show_title();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
leaq 0x236bb5(%rip), %rbx    # 0x33b81c
movl (%rbx), %ecx
testl %ecx, %ecx
je 0x104c96
leaq 0x113c5c(%rip), %r14    # 0x2188d0
movq (%r14), %rax
cmpq $0x2000000, %rax        # imm = 0x2000000
je 0x104ca1
cmpq $0x10000, %rax          # imm = 0x10000
jne 0x104cb0
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x1045b3
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x1049da
cmpl $0x1, %ecx
jne 0x104e47
cmpq $0x1, %rax
setne %dl
leaq 0x113c3d(%rip), %rcx    # 0x218904
cmpb $0x0, (%rcx)
setne %cl
orb %dl, %cl
jne 0x104cec
leaq 0x1162fc(%rip), %rdx    # 0x21afd4
movl (%rdx), %edx
addl $-0x33, %edx
cmpl $0x1d, %edx
ja 0x104d23
movl $0x20004009, %esi       # imm = 0x20004009
btl %edx, %esi
jae 0x104d23
movabsq $0x400741010, %rdx      # imm = 0x400741010
testq %rdx, %rax
jne 0x104d23
leaq 0x1162ce(%rip), %rdx    # 0x21afd0
cmpl $0x0, (%rdx)
jne 0x104d23
leaq 0x13a21f(%rip), %rdx    # 0x23ef2d
cmpb $0x0, (%rdx)
jne 0x104d23
leaq 0x11631e(%rip), %rdx    # 0x21b038
cmpl $0x0, (%rdx)
je 0x1051da
testb %cl, %cl
jne 0x104d5b
leaq 0x1162a6(%rip), %rcx    # 0x21afd4
movl (%rcx), %ecx
movl %ecx, %esi
xorl $0x41, %esi
leaq 0x113bbc(%rip), %rdx    # 0x2188f8
movl (%rdx), %edx
movl %edx, %edi
xorl $0x47, %edi
orl %esi, %edi
je 0x104d5b
cmpl $0x41, %ecx
jne 0x104d56
cmpl $0x33, %edx
je 0x104d5b
cmpl $0x47, %edx
je 0x104d5b
cmpl $0x41, %ecx
jne 0x104d6b
leaq 0x13a1cb(%rip), %rcx    # 0x23ef2d
cmpb $0x0, (%rcx)
je 0x1051da
leaq 0x23a966(%rip), %r14    # 0x33f6d8
cmpb $0x0, (%r14)
je 0x104d98
leaq 0x208715(%rip), %rcx    # 0x30d494
movb $0x1, (%rcx)
cmpq $0x2000000, %rax        # imm = 0x2000000
je 0x104d94
movl $0x5, %edi
callq 0x6d6ce
movb $0x0, (%r14)
movl $0x2, %edi
callq 0xfaae1
leaq 0x232271(%rip), %r14    # 0x33701a
movb (%r14), %r15b
movb $0x1, (%r14)
movss 0x8218(%rip), %xmm1     # 0x10cfd0
xorps %xmm0, %xmm0
movl $0xff, %edi
movl $0xff, %esi
movapd %xmm1, %xmm2
movapd %xmm1, %xmm3
movl $0xff, %edx
callq 0x5d081
movb %r15b, (%r14)
xorl %eax, %eax
movl %eax, 0x274f9e(%rip)    # 0x379d80
movl %eax, 0x274f9c(%rip)    # 0x379d84
leaq 0x207c11(%rip), %rax    # 0x30ca00
movsd (%rax), %xmm0
leaq 0x236a26(%rip), %r14    # 0x33b820
movsd %xmm0, (%r14)
movl $0x4, %edi
callq 0x6d6ce
movsd (%r14), %xmm0
addsd 0x1968a(%rip), %xmm0    # 0x11e4a0
movl $0x1, %edi
callq 0x6ced6
incl (%rbx)
leaq 0x239a27(%rip), %rax    # 0x33e850
movups 0x30e7b(%rip), %xmm0    # 0x135cab
movaps %xmm0, (%rax)
movups 0x30e81(%rip), %xmm0    # 0x135cbb
movaps %xmm0, 0x10(%rax)
movl $0x676e69, 0x1f(%rax)   # imm = 0x676E69
jmp 0x104e7a
cmpl $0x5, %ecx
jg 0x104e8c
incl %ecx
movl %ecx, (%rbx)
movups 0x30e77(%rip), %xmm0    # 0x135cce
leaq 0x2399f2(%rip), %rax    # 0x33e850
movaps %xmm0, (%rax)
movups 0x30e76(%rip), %xmm0    # 0x135cde
movaps %xmm0, 0x10(%rax)
movabsq $0x6867696820676e, %rcx # imm = 0x6867696820676E
movq %rcx, 0x1f(%rax)
leaq 0x23a44b(%rip), %rax    # 0x33f2cc
movl $0x3, (%rax)
jmp 0x10528c
cmpl $0x6, %ecx
jne 0x104e9c
movl $0x64, (%rbx)
jmp 0x10528c
cmpl $0xa0, %ecx
jg 0x104f46
movslq 0x274ed1(%rip), %rdx    # 0x379d80
cmpq $0x5f, %rdx
jg 0x104ef9
leaq 0x207904(%rip), %rax    # 0x30c7c0
movsd (%rax), %xmm0
leaq 0x274ec9(%rip), %rax    # 0x379d90
movsd %xmm0, (%rax,%rdx,8)
testl %edx, %edx
jle 0x104ef7
movq %rdx, %rcx
incq %rcx
movsd -0x10(%rax,%rcx,8), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x104ef7
movsd %xmm1, -0x8(%rax,%rcx,8)
movsd %xmm0, -0x10(%rax,%rcx,8)
decq %rcx
cmpq $0x1, %rcx
jg 0x104ed6
incl %edx
movl %edx, 0x274e81(%rip)    # 0x379d80
movl %edx, %eax
shrl $0x1f, %eax
addl %edx, %eax
sarl %eax
cltq
leaq 0x274e7f(%rip), %rcx    # 0x379d90
xorps %xmm0, %xmm0
cvtsd2ss (%rcx,%rax,8), %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x23992c(%rip), %rdi    # 0x33e850
leaq 0x30dca(%rip), %rsi     # 0x135cf5
movb $0x1, %al
callq 0x9060
leaq 0x23a393(%rip), %rax    # 0x33f2cc
movl $0x3, (%rax)
incl (%rbx)
jmp 0x10528c
cmpl $0xa1, %ecx
jne 0x104fad
movups 0x30dd6(%rip), %xmm0    # 0x135d2b
leaq 0x2398f4(%rip), %rax    # 0x33e850
movaps %xmm0, 0x10(%rax)
movups 0x30db4(%rip), %xmm0    # 0x135d1b
movaps %xmm0, (%rax)
movabsq $0x776f6c20676e69, %rcx # imm = 0x776F6C20676E69
movq %rcx, 0x1e(%rax)
leaq 0x23a34d(%rip), %rax    # 0x33f2cc
movl $0x3, (%rax)
leaq 0x236894(%rip), %rax    # 0x33b820
movsd (%rax), %xmm0
addsd 0x6a30(%rip), %xmm0     # 0x10b9c8
movl $0x2, %edi
callq 0x6ced6
movl $0x3e8, (%rbx)          # imm = 0x3E8
jmp 0x10528c
cmpl $0x3ed, %ecx            # imm = 0x3ED
jg 0x104fbe
incl %ecx
movl %ecx, (%rbx)
jmp 0x10528c
cmpl $0x3ee, %ecx            # imm = 0x3EE
jne 0x104fd1
movl $0x2710, (%rbx)         # imm = 0x2710
jmp 0x10528c
cmpl $0x274c, %ecx           # imm = 0x274C
jg 0x105070
incl %ecx
movl %ecx, (%rbx)
movslq 0x274d9c(%rip), %rdx    # 0x379d84
cmpq $0x5f, %rdx
jg 0x105032
leaq 0x2077cb(%rip), %rax    # 0x30c7c0
movsd (%rax), %xmm0
leaq 0x2750a0(%rip), %rax    # 0x37a0a0
movsd %xmm0, (%rax,%rdx,8)
testl %edx, %edx
jle 0x105030
movq %rdx, %rcx
incq %rcx
movsd -0x10(%rax,%rcx,8), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x105030
movsd %xmm1, -0x8(%rax,%rcx,8)
movsd %xmm0, -0x10(%rax,%rcx,8)
decq %rcx
cmpq $0x1, %rcx
jg 0x10500f
incl %edx
movl %edx, 0x274d4c(%rip)    # 0x379d84
movl %edx, %eax
shrl $0x1f, %eax
addl %edx, %eax
sarl %eax
cltq
leaq 0x275056(%rip), %rcx    # 0x37a0a0
xorps %xmm0, %xmm0
cvtsd2ss (%rcx,%rax,8), %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x2397f3(%rip), %rdi    # 0x33e850
leaq 0x30cdd(%rip), %rsi     # 0x135d41
movb $0x1, %al
callq 0x9060
jmp 0x104e7a
movl 0x274d0e(%rip), %eax    # 0x379d84
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
movslq %ecx, %rax
leaq 0x275017(%rip), %rcx    # 0x37a0a0
cvtsd2ss (%rcx,%rax,8), %xmm0
movl 0x274cec(%rip), %eax    # 0x379d80
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
movslq %ecx, %rax
leaq 0x274ce9(%rip), %rcx    # 0x379d90
cvtsd2ss (%rcx,%rax,8), %xmm1
cvtss2sd %xmm0, %xmm0
leaq 0x2080d1(%rip), %rax    # 0x30d188
cvtss2sd %xmm1, %xmm1
subsd %xmm1, %xmm0
divsd 0x3fe1(%rip), %xmm0     # 0x1090a8
movsd %xmm0, (%rax)
leaq 0x2080a6(%rip), %rax    # 0x30d178
movabsq $0x407f400000000000, %rcx # imm = 0x407F400000000000
movq %rcx, (%rax)
leaq 0x20809a(%rip), %rax    # 0x30d180
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movq %rcx, (%rax)
leaq 0x208026(%rip), %rax    # 0x30d120
movsd (%rax), %xmm2
leaq 0x20808b(%rip), %rax    # 0x30d190
movsd %xmm2, (%rax)
leaq 0x208018(%rip), %rax    # 0x30d128
movsd (%rax), %xmm3
leaq 0x20807d(%rip), %rax    # 0x30d198
movsd %xmm3, (%rax)
leaq 0x20801a(%rip), %rax    # 0x30d140
movsd (%rax), %xmm1
leaq 0x20807f(%rip), %rax    # 0x30d1b0
movsd %xmm1, (%rax)
leaq 0x20800c(%rip), %rax    # 0x30d148
movsd (%rax), %xmm1
leaq 0x208071(%rip), %rax    # 0x30d1b8
movsd %xmm1, (%rax)
leaq 0x2366ce(%rip), %r15    # 0x33b820
movsd (%r15), %xmm1
leaq 0x208062(%rip), %rax    # 0x30d1c0
movsd %xmm1, (%rax)
leaq 0x139dd3(%rip), %rax    # 0x23ef3c
cmpb $0x0, (%rax)
je 0x105179
leaq 0x207fdb(%rip), %rax    # 0x30d150
movsd %xmm1, (%rax)
leaq 0x2396d0(%rip), %rdi    # 0x33e850
leaq 0x30bdb(%rip), %rsi     # 0x135d62
movsd 0x67b1(%rip), %xmm4     # 0x10b940
movsd 0x3ed9(%rip), %xmm5     # 0x109070
movb $0x6, %al
callq 0x9060
leaq 0x23a127(%rip), %rax    # 0x33f2cc
movl $0x3, (%rax)
movl $0x1, %edi
callq 0x7096f
movsd (%r15), %xmm0
movl $0x3, %edi
callq 0x6ced6
movl $0x5, %edi
callq 0x6d6ce
xorl %edi, %edi
callq 0x6d6ce
callq 0x708b3
leaq 0x207ed3(%rip), %rax    # 0x30d0b4
movss (%rax), %xmm0
movss %xmm0, -0x1c(%rbp)
callq 0xfa2de
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
addss -0x1c(%rbp), %xmm0
mulss 0x7de1(%rip), %xmm0     # 0x10cfe4
leaq 0x1136fa(%rip), %rax    # 0x218904
cmpb $0x2, (%rax)
jb 0x105221
divss 0x7ddd(%rip), %xmm0     # 0x10cff4
movss 0x275e5(%rip), %xmm1    # 0x12c804
jmp 0x105237
cmpq $0x1, (%r14)
divss 0x7dc7(%rip), %xmm0     # 0x10cff4
jne 0x10524b
movss 0x7d9d(%rip), %xmm1     # 0x10cfd4
movss 0x7d91(%rip), %xmm2     # 0x10cfd0
movl $0xff, %edi
movl $0xff, %esi
jmp 0x105273
movss %xmm0, -0x1c(%rbp)
callq 0xfa380
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
movss 0x7d6c(%rip), %xmm2     # 0x10cfd0
movl $0xff, %edi
movl $0xff, %esi
movss -0x1c(%rbp), %xmm0
movapd %xmm2, %xmm3
movl $0xff, %edx
callq 0x5d081
callq 0xfb1a
movl $0x0, (%rbx)
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x17e7f