// File: /workspace/github_cpp_makefile_star2_O3/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 0x2364d4(%rip), %rbx    # 0x33b81c
movl (%rbx), %eax
testl %eax, %eax
je 0x105379
leaq 0x11357b(%rip), %r14    # 0x2188d0
movq (%r14), %rcx
cmpq $0x2000000, %rcx        # imm = 0x2000000
je 0x105384
cmpq $0x10000, %rcx          # imm = 0x10000
jne 0x105393
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x104c93
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x1050bb
cmpl $0x1, %eax
jne 0x10547e
cmpq $0x1, %rcx
setne %dl
leaq 0x11355a(%rip), %rax    # 0x218904
movb (%rax), %r15b
testb %r15b, %r15b
setne %al
orb %dl, %al
jne 0x1053d2
leaq 0x115c16(%rip), %rdx    # 0x21afd4
movl (%rdx), %edx
leal -0x33(%rdx), %esi
cmpl $0x1d, %esi
ja 0x105412
movl $0x20004009, %edi       # imm = 0x20004009
btl %esi, %edi
jae 0x105412
movabsq $0x400741010, %rdx      # imm = 0x400741010
testq %rdx, %rcx
jne 0x105405
leaq 0x115be8(%rip), %rcx    # 0x21afd0
cmpl $0x0, (%rcx)
jne 0x105405
leaq 0x139b39(%rip), %rcx    # 0x23ef2d
cmpb $0x0, (%rcx)
jne 0x105405
leaq 0x115c38(%rip), %rcx    # 0x21b038
cmpl $0x0, (%rcx)
je 0x10543d
testb %al, %al
jne 0x10542d
leaq 0x115bc4(%rip), %rax    # 0x21afd4
movl (%rax), %edx
movl %edx, %ecx
xorl $0x41, %ecx
leaq 0x1134da(%rip), %rax    # 0x2188f8
movl (%rax), %eax
movl %eax, %esi
xorl $0x47, %esi
orl %ecx, %esi
jne 0x1055a0
leaq 0x139af9(%rip), %rax    # 0x23ef2d
cmpb $0x0, (%rax)
jne 0x1055c7
leaq 0x207c70(%rip), %rax    # 0x30d0b4
movss (%rax), %xmm0
movss %xmm0, -0x1c(%rbp)
callq 0xfaa32
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
addss -0x1c(%rbp), %xmm0
mulss 0x7c12(%rip), %xmm0     # 0x10d078
cmpb $0x2, %r15b
jb 0x1054c6
divss 0x7c14(%rip), %xmm0     # 0x10d088
movss 0x27470(%rip), %xmm1    # 0x12c8ec
jmp 0x1054e0
cmpl $0x5, %eax
jg 0x1054b6
incl %eax
movl %eax, (%rbx)
movups 0x30939(%rip), %xmm0    # 0x135dc7
leaq 0x2393bb(%rip), %rax    # 0x33e850
movaps %xmm0, (%rax)
movups 0x30938(%rip), %xmm0    # 0x135dd7
movaps %xmm0, 0x10(%rax)
movabsq $0x6867696820676e, %rcx # imm = 0x6867696820676E
movq %rcx, 0x1f(%rax)
jmp 0x105699
cmpl $0x6, %eax
jne 0x1054f7
movl $0x64, (%rbx)
jmp 0x1056e9
cmpq $0x1, (%r14)
divss 0x7bb6(%rip), %xmm0     # 0x10d088
jne 0x1056a8
movss 0x7b8c(%rip), %xmm1     # 0x10d06c
movss 0x7b80(%rip), %xmm2     # 0x10d068
movl $0xff, %edi
movl $0xff, %esi
jmp 0x1056d0
cmpl $0xa0, %eax
ja 0x1056f8
movslq 0x274877(%rip), %rdx    # 0x379d80
cmpq $0x5f, %rdx
jg 0x105553
leaq 0x2072aa(%rip), %rax    # 0x30c7c0
movsd (%rax), %xmm0
leaq 0x27486f(%rip), %rax    # 0x379d90
movsd %xmm0, (%rax,%rdx,8)
testl %edx, %edx
jle 0x105551
movq %rdx, %rcx
incq %rcx
movsd -0x10(%rax,%rcx,8), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x105551
movsd %xmm1, -0x8(%rax,%rcx,8)
movsd %xmm0, -0x10(%rax,%rcx,8)
decq %rcx
cmpq $0x1, %rcx
ja 0x105530
incl %edx
movl %edx, 0x274827(%rip)    # 0x379d80
movl %edx, %eax
shrl $0x1f, %eax
addl %edx, %eax
sarl %eax
cltq
leaq 0x274825(%rip), %rcx    # 0x379d90
xorps %xmm0, %xmm0
cvtsd2ss (%rcx,%rax,8), %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x2392d2(%rip), %rdi    # 0x33e850
leaq 0x30869(%rip), %rsi     # 0x135dee
movb $0x1, %al
callq 0x9060
leaq 0x239d39(%rip), %rax    # 0x33f2cc
movl $0x3, (%rax)
incl (%rbx)
jmp 0x1056e9
cmpl $0x41, %edx
jne 0x1055c7
cmpl $0x33, %eax
je 0x10542d
cmpl $0x47, %eax
je 0x10542d
leaq 0x13996f(%rip), %rax    # 0x23ef2d
cmpb $0x0, (%rax)
je 0x105818
leaq 0x23a10a(%rip), %r14    # 0x33f6d8
cmpb $0x0, (%r14)
je 0x1055ec
leaq 0x207eb9(%rip), %rax    # 0x30d494
movb $0x1, (%rax)
movl $0x5, %edi
callq 0x6d604
movb $0x0, (%r14)
movl $0x2, %edi
callq 0xfb285
leaq 0x231a1d(%rip), %r14    # 0x33701a
movb (%r14), %r15b
movb $0x1, (%r14)
movss 0x7a5c(%rip), %xmm1     # 0x10d068
xorps %xmm0, %xmm0
movl $0xff, %edi
movl $0xff, %esi
movapd %xmm1, %xmm2
movapd %xmm1, %xmm3
movl $0xff, %edx
callq 0x5cf59
movb %r15b, (%r14)
xorl %eax, %eax
movl %eax, 0x27474a(%rip)    # 0x379d80
movl %eax, 0x274748(%rip)    # 0x379d84
leaq 0x2073bd(%rip), %rax    # 0x30ca00
movsd (%rax), %xmm0
leaq 0x2361d2(%rip), %r14    # 0x33b820
movsd %xmm0, (%r14)
movl $0x4, %edi
callq 0x6d604
movsd (%r14), %xmm0
addsd 0x18ed6(%rip), %xmm0    # 0x11e540
movl $0x1, %edi
callq 0x6ce5e
incl (%rbx)
leaq 0x2391d3(%rip), %rax    # 0x33e850
movups 0x30720(%rip), %xmm0    # 0x135da4
movaps %xmm0, (%rax)
movups 0x30726(%rip), %xmm0    # 0x135db4
movaps %xmm0, 0x10(%rax)
movl $0x676e69, 0x1f(%rax)   # imm = 0x676E69
leaq 0x239c2c(%rip), %rax    # 0x33f2cc
movl $0x3, (%rax)
jmp 0x1056e9
movss %xmm0, -0x1c(%rbp)
callq 0xfaad4
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
movss 0x79a7(%rip), %xmm2     # 0x10d068
movl $0xff, %edi
movl $0xff, %esi
movss -0x1c(%rbp), %xmm0
movapd %xmm2, %xmm3
movl $0xff, %edx
callq 0x5cf59
callq 0xfa97
movl $0x0, (%rbx)
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x17eb5
cmpl $0xa1, %eax
jne 0x10575b
movups 0x3071e(%rip), %xmm0    # 0x135e24
leaq 0x239143(%rip), %rax    # 0x33e850
movaps %xmm0, 0x10(%rax)
movups 0x306fc(%rip), %xmm0    # 0x135e14
movaps %xmm0, (%rax)
movabsq $0x776f6c20676e69, %rcx # imm = 0x776F6C20676E69
movq %rcx, 0x1e(%rax)
leaq 0x239b9c(%rip), %rax    # 0x33f2cc
movl $0x3, (%rax)
leaq 0x2360e3(%rip), %rax    # 0x33b820
movsd (%rax), %xmm0
addsd 0x627f(%rip), %xmm0     # 0x10b9c8
movl $0x2, %edi
callq 0x6ce5e
movl $0x3e8, (%rbx)          # imm = 0x3E8
jmp 0x1056e9
cmpl $0x3ed, %eax            # imm = 0x3ED
ja 0x105768
incl %eax
movl %eax, (%rbx)
jmp 0x1056e9
cmpl $0x3ee, %eax            # imm = 0x3EE
jne 0x10577a
movl $0x2710, (%rbx)         # imm = 0x2710
jmp 0x1056e9
cmpl $0x274c, %eax           # imm = 0x274C
ja 0x105820
incl %eax
movl %eax, (%rbx)
movslq 0x2745f4(%rip), %rdx    # 0x379d84
cmpq $0x5f, %rdx
jg 0x1057da
leaq 0x207023(%rip), %rax    # 0x30c7c0
movsd (%rax), %xmm0
leaq 0x2748f8(%rip), %rax    # 0x37a0a0
movsd %xmm0, (%rax,%rdx,8)
testl %edx, %edx
jle 0x1057d8
movq %rdx, %rcx
incq %rcx
movsd -0x10(%rax,%rcx,8), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x1057d8
movsd %xmm1, -0x8(%rax,%rcx,8)
movsd %xmm0, -0x10(%rax,%rcx,8)
decq %rcx
cmpq $0x1, %rcx
ja 0x1057b7
incl %edx
movl %edx, 0x2745a4(%rip)    # 0x379d84
movl %edx, %eax
shrl $0x1f, %eax
addl %edx, %eax
sarl %eax
cltq
leaq 0x2748ae(%rip), %rcx    # 0x37a0a0
xorps %xmm0, %xmm0
cvtsd2ss (%rcx,%rax,8), %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x23904b(%rip), %rdi    # 0x33e850
leaq 0x3062e(%rip), %rsi     # 0x135e3a
movb $0x1, %al
callq 0x9060
jmp 0x105699
xorl %r15d, %r15d
jmp 0x10543d
movl 0x27455e(%rip), %eax    # 0x379d84
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
movslq %ecx, %rax
leaq 0x274867(%rip), %rcx    # 0x37a0a0
cvtsd2ss (%rcx,%rax,8), %xmm0
cvtss2sd %xmm0, %xmm0
movl 0x274538(%rip), %eax    # 0x379d80
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
movslq %ecx, %rax
leaq 0x274535(%rip), %rcx    # 0x379d90
cvtsd2ss (%rcx,%rax,8), %xmm1
cvtss2sd %xmm1, %xmm1
subsd %xmm1, %xmm0
divsd 0x3838(%rip), %xmm0     # 0x1090a8
leaq 0x207911(%rip), %rax    # 0x30d188
movsd %xmm0, (%rax)
leaq 0x2078f6(%rip), %rax    # 0x30d178
movabsq $0x407f400000000000, %rcx # imm = 0x407F400000000000
movq %rcx, (%rax)
leaq 0x2078ea(%rip), %rax    # 0x30d180
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movq %rcx, (%rax)
leaq 0x207876(%rip), %rax    # 0x30d120
movsd (%rax), %xmm2
leaq 0x2078db(%rip), %rax    # 0x30d190
movsd %xmm2, (%rax)
leaq 0x207868(%rip), %rax    # 0x30d128
movsd (%rax), %xmm3
leaq 0x2078cd(%rip), %rax    # 0x30d198
movsd %xmm3, (%rax)
leaq 0x20786a(%rip), %rax    # 0x30d140
movsd (%rax), %xmm1
leaq 0x2078cf(%rip), %rax    # 0x30d1b0
movsd %xmm1, (%rax)
leaq 0x20785c(%rip), %rax    # 0x30d148
movsd (%rax), %xmm1
leaq 0x2078c1(%rip), %rax    # 0x30d1b8
movsd %xmm1, (%rax)
leaq 0x235f1e(%rip), %r15    # 0x33b820
movsd (%r15), %xmm1
leaq 0x2078b2(%rip), %rax    # 0x30d1c0
movsd %xmm1, (%rax)
leaq 0x139623(%rip), %rax    # 0x23ef3c
cmpb $0x0, (%rax)
je 0x105929
leaq 0x20782b(%rip), %rax    # 0x30d150
movsd %xmm1, (%rax)
leaq 0x238f20(%rip), %rdi    # 0x33e850
leaq 0x30524(%rip), %rsi     # 0x135e5b
movsd 0x6009(%rip), %xmm4     # 0x10b948
movsd 0x3729(%rip), %xmm5     # 0x109070
movb $0x6, %al
callq 0x9060
leaq 0x239977(%rip), %rax    # 0x33f2cc
movl $0x3, (%rax)
movl $0x1, %edi
callq 0x70a37
movsd (%r15), %xmm0
movl $0x3, %edi
callq 0x6ce5e
movl $0x5, %edi
callq 0x6d604
xorl %edi, %edi
callq 0x6d604
callq 0x7097b
leaq 0x112f73(%rip), %rax    # 0x218904
movb (%rax), %r15b
jmp 0x10543d