// File: /workspace/github_cpp_makefile_star2_O2/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 0x236d38(%rip), %r14    # 0x32581c
movl (%r14), %eax
testl %eax, %eax
je 0xeeb16
leaq 0x113dde(%rip), %rbx    # 0x2028d0
movq (%rbx), %rcx
cmpq $0x2000000, %rcx        # imm = 0x2000000
je 0xeeb21
cmpq $0x10000, %rcx          # imm = 0x10000
jne 0xeeb30
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0xee44a
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0xee85f
cmpl $0x1, %eax
jne 0xeec88
cmpq $0x1, %rcx
setne %al
leaq 0x113dbd(%rip), %rdx    # 0x202904
movb (%rdx), %r15b
testb %r15b, %r15b
setne %dl
orb %al, %dl
jne 0xeeb6f
leaq 0x116479(%rip), %rax    # 0x204fd4
movl (%rax), %eax
leal -0x33(%rax), %esi
cmpl $0x1d, %esi
ja 0xeebb3
movl $0x20004009, %edi       # imm = 0x20004009
btl %esi, %edi
jae 0xeebb3
movabsq $0x400741010, %rax      # imm = 0x400741010
testq %rax, %rcx
jne 0xeeba6
leaq 0x11644b(%rip), %rax    # 0x204fd0
cmpl $0x0, (%rax)
jne 0xeeba6
leaq 0x13a39c(%rip), %rax    # 0x228f2d
cmpb $0x0, (%rax)
jne 0xeeba6
leaq 0x11649b(%rip), %rax    # 0x205038
cmpl $0x0, (%rax)
je 0xeed82
testb %dl, %dl
jne 0xeebce
leaq 0x116423(%rip), %rax    # 0x204fd4
movl (%rax), %eax
movl %eax, %edx
xorl $0x41, %edx
leaq 0x113d39(%rip), %rcx    # 0x2028f8
movl (%rcx), %ecx
movl %ecx, %esi
xorl $0x47, %esi
orl %edx, %esi
jne 0xeed4f
leaq 0x13a358(%rip), %rax    # 0x228f2d
cmpb $0x0, (%rax)
je 0xeed82
callq 0xef080
pushq $0x2
popq %rdi
callq 0xe5811
leaq 0x232428(%rip), %rbx    # 0x32101a
movb (%rbx), %r15b
movb $0x1, (%rbx)
movss 0x84e8(%rip), %xmm1     # 0xf70e8
xorps %xmm0, %xmm0
movl $0xff, %edi
movl $0xff, %esi
movaps %xmm1, %xmm2
movaps %xmm1, %xmm3
movl $0xff, %edx
callq 0x55b65
andl $0x0, 0x27515c(%rip)    # 0x363d80
movb %r15b, (%rbx)
andl $0x0, 0x275156(%rip)    # 0x363d84
leaq 0x207dcb(%rip), %rax    # 0x2f6a00
movsd (%rax), %xmm0
leaq 0x236be0(%rip), %rbx    # 0x325820
movsd %xmm0, (%rbx)
pushq $0x4
popq %rdi
callq 0x6434c
movsd (%rbx), %xmm0
addsd 0x198d8(%rip), %xmm0    # 0x108530
pushq $0x1
popq %rdi
callq 0x63c97
incl (%r14)
leaq 0x239be6(%rip), %rax    # 0x328850
movups 0x3103f(%rip), %xmm0    # 0x11fcb0
movaps %xmm0, (%rax)
movups 0x31045(%rip), %xmm0    # 0x11fcc0
movaps %xmm0, 0x10(%rax)
movl $0x676e69, 0x1f(%rax)   # imm = 0x676E69
jmp 0xeecbc
cmpl $0x5, %eax
jg 0xeecce
incl %eax
movl %eax, (%r14)
movups 0x3103a(%rip), %xmm0    # 0x11fcd3
leaq 0x239bb0(%rip), %rax    # 0x328850
movaps %xmm0, (%rax)
movups 0x31039(%rip), %xmm0    # 0x11fce3
movaps %xmm0, 0x10(%rax)
movabsq $0x6867696820676e, %rcx # imm = 0x6867696820676E
movq %rcx, 0x1f(%rax)
leaq 0x23a609(%rip), %rax    # 0x3292cc
movl $0x3, (%rax)
jmp 0xeee25
cmpl $0x6, %eax
jne 0xeecdf
movl $0x64, (%r14)
jmp 0xeee25
cmpl $0xa0, %eax
ja 0xeee34
leaq 0x207acf(%rip), %rax    # 0x2f67c0
movsd (%rax), %xmm0
movl 0x275085(%rip), %esi    # 0x363d80
leaq 0x27508e(%rip), %rbx    # 0x363d90
movq %rbx, %rdi
callq 0xe7291
movl %eax, %ecx
movl %eax, 0x27506e(%rip)    # 0x363d80
pushq $0x2
popq %rsi
cltd
idivl %esi
cltq
cvtsd2ss (%rbx,%rax,8), %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x239b26(%rip), %rdi    # 0x328850
leaq 0x30fc9(%rip), %rsi     # 0x11fcfa
movl %ecx, %edx
movb $0x1, %al
callq 0x9060
leaq 0x23a58b(%rip), %rax    # 0x3292cc
movl $0x3, (%rax)
incl (%r14)
jmp 0xeee25
cmpl $0x41, %eax
jne 0xeed66
cmpl $0x33, %ecx
je 0xeebce
cmpl $0x47, %ecx
je 0xeebce
cmpl $0x41, %eax
jne 0xeebde
leaq 0x13a1b7(%rip), %rax    # 0x228f2d
cmpb $0x0, (%rax)
jne 0xeebde
xorl %r15d, %r15d
leaq 0x20832b(%rip), %rax    # 0x2f70b4
movss (%rax), %xmm0
movss %xmm0, -0x1c(%rbp)
callq 0xe5082
cvtsi2ss %eax, %xmm0
addss -0x1c(%rbp), %xmm0
mulss 0x8350(%rip), %xmm0     # 0xf70f8
cmpb $0x2, %r15b
jb 0xeedc0
divss 0x8352(%rip), %xmm0     # 0xf7108
movss 0x27a4e(%rip), %xmm1    # 0x11680c
jmp 0xeedd6
cmpq $0x1, (%rbx)
divss 0x833c(%rip), %xmm0     # 0xf7108
jne 0xeedea
movss 0x8316(%rip), %xmm1     # 0xf70ec
movss 0x830a(%rip), %xmm2     # 0xf70e8
movl $0xff, %edi
movl $0xff, %esi
jmp 0xeee0f
movss %xmm0, -0x1c(%rbp)
callq 0xe5126
cvtsi2ss %eax, %xmm1
movss 0x82e8(%rip), %xmm2     # 0xf70e8
movl $0xff, %edi
movl $0xff, %esi
movss -0x1c(%rbp), %xmm0
movaps %xmm2, %xmm3
movl $0xff, %edx
callq 0x55b65
callq 0xf426
andl $0x0, (%r14)
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x1747f
cmpl $0xa1, %eax
jne 0xeee96
movups 0x30eee(%rip), %xmm0    # 0x11fd30
leaq 0x239a07(%rip), %rax    # 0x328850
movaps %xmm0, 0x10(%rax)
movups 0x30ecc(%rip), %xmm0    # 0x11fd20
movaps %xmm0, (%rax)
movabsq $0x776f6c20676e69, %rcx # imm = 0x776F6C20676E69
movq %rcx, 0x1e(%rax)
leaq 0x23a460(%rip), %rax    # 0x3292cc
movl $0x3, (%rax)
leaq 0x2369a7(%rip), %rax    # 0x325820
movsd (%rax), %xmm0
addsd 0x6bbb(%rip), %xmm0     # 0xf5a40
pushq $0x2
popq %rdi
callq 0x63c97
movl $0x3e8, (%r14)          # imm = 0x3E8
jmp 0xeee25
cmpl $0x3ed, %eax            # imm = 0x3ED
ja 0xeeea4
incl %eax
movl %eax, (%r14)
jmp 0xeee25
cmpl $0x3ee, %eax            # imm = 0x3EE
jne 0xeeeb7
movl $0x2710, (%r14)         # imm = 0x2710
jmp 0xeee25
cmpl $0x274c, %eax           # imm = 0x274C
ja 0xeef18
incl %eax
movl %eax, (%r14)
leaq 0x2078f6(%rip), %rax    # 0x2f67c0
movsd (%rax), %xmm0
movl 0x274eb0(%rip), %esi    # 0x363d84
leaq 0x2751c5(%rip), %rbx    # 0x3640a0
movq %rbx, %rdi
callq 0xe7291
movl %eax, %ecx
movl %eax, 0x274e99(%rip)    # 0x363d84
pushq $0x2
popq %rsi
cltd
idivl %esi
cltq
cvtsd2ss (%rbx,%rax,8), %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x23994d(%rip), %rdi    # 0x328850
leaq 0x30e3c(%rip), %rsi     # 0x11fd46
movl %ecx, %edx
movb $0x1, %al
callq 0x9060
jmp 0xeecbc
movl 0x274e66(%rip), %eax    # 0x363d84
pushq $0x2
popq %rcx
cltd
idivl %ecx
cltq
leaq 0x275173(%rip), %rdx    # 0x3640a0
cvtsd2ss (%rdx,%rax,8), %xmm0
cvtss2sd %xmm0, %xmm0
movl 0x274e44(%rip), %eax    # 0x363d80
cltd
idivl %ecx
cltq
leaq 0x274e48(%rip), %rcx    # 0x363d90
cvtsd2ss (%rcx,%rax,8), %xmm1
cvtss2sd %xmm1, %xmm1
subsd %xmm1, %xmm0
divsd 0x414b(%rip), %xmm0     # 0xf30a8
leaq 0x208224(%rip), %rax    # 0x2f7188
movsd %xmm0, (%rax)
leaq 0x208209(%rip), %rax    # 0x2f7178
movabsq $0x407f400000000000, %rcx # imm = 0x407F400000000000
movq %rcx, (%rax)
leaq 0x2081fd(%rip), %rax    # 0x2f7180
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movq %rcx, (%rax)
leaq 0x208189(%rip), %rax    # 0x2f7120
movsd (%rax), %xmm2
leaq 0x2081ee(%rip), %rax    # 0x2f7190
movsd %xmm2, (%rax)
leaq 0x20817b(%rip), %rax    # 0x2f7128
movsd (%rax), %xmm3
leaq 0x2081e0(%rip), %rax    # 0x2f7198
movsd %xmm3, (%rax)
leaq 0x20817d(%rip), %rax    # 0x2f7140
movsd (%rax), %xmm1
leaq 0x2081e2(%rip), %rax    # 0x2f71b0
movsd %xmm1, (%rax)
leaq 0x20816f(%rip), %rax    # 0x2f7148
movsd (%rax), %xmm1
leaq 0x2081d4(%rip), %rax    # 0x2f71b8
movsd %xmm1, (%rax)
leaq 0x236831(%rip), %r15    # 0x325820
movsd (%r15), %xmm1
leaq 0x2081c5(%rip), %rax    # 0x2f71c0
movsd %xmm1, (%rax)
leaq 0x139f36(%rip), %rax    # 0x228f3c
cmpb $0x0, (%rax)
je 0xef016
leaq 0x20813e(%rip), %rax    # 0x2f7150
movsd %xmm1, (%rax)
leaq 0x239833(%rip), %rdi    # 0x328850
leaq 0x30d43(%rip), %rsi     # 0x11fd67
movsd 0x6994(%rip), %xmm4     # 0xf59c0
movsd 0x403c(%rip), %xmm5     # 0xf3070
movb $0x6, %al
callq 0x9060
leaq 0x23a28a(%rip), %rax    # 0x3292cc
movl $0x3, (%rax)
pushq $0x1
popq %rdi
callq 0x67252
movsd (%r15), %xmm0
pushq $0x3
popq %rdi
callq 0x63c97
pushq $0x5
popq %rdi
callq 0x6434c
xorl %edi, %edi
callq 0x6434c
callq 0x671af
leaq 0x11388c(%rip), %rax    # 0x202904
movb (%rax), %r15b
jmp 0xeed82