// File: 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
subq $0x20, %rsp
leaq 0x231e6d(%rip), %rax    # 0x39a9fc
cmpl $0x0, (%rax)
jne 0x168b99
jmp 0x169387
leaq 0x10ef10(%rip), %rax    # 0x277ab0
cmpq $0x10000, (%rax)        # imm = 0x10000
jne 0x168bb3
callq 0x168310
jmp 0x169387
leaq 0x10eef6(%rip), %rax    # 0x277ab0
cmpq $0x2000000, (%rax)      # imm = 0x2000000
jne 0x168bcd
callq 0x168850
jmp 0x169387
jmp 0x168bcf
leaq 0x231e26(%rip), %rax    # 0x39a9fc
cmpl $0x1, (%rax)
jne 0x168e23
leaq 0x10eeca(%rip), %rax    # 0x277ab0
cmpq $0x1, (%rax)
jne 0x168c67
leaq 0x10eef1(%rip), %rax    # 0x277ae4
cmpb $0x0, (%rax)
jne 0x168c67
leaq 0x10eeb1(%rip), %rax    # 0x277ab0
cmpq $0x1, (%rax)
jne 0x168c11
leaq 0x1115a8(%rip), %rax    # 0x27a1b4
cmpl $0x50, (%rax)
je 0x168c67
leaq 0x10ee98(%rip), %rax    # 0x277ab0
cmpq $0x1, (%rax)
jne 0x168c2a
leaq 0x11158f(%rip), %rax    # 0x27a1b4
cmpl $0x41, (%rax)
je 0x168c67
leaq 0x10ee7f(%rip), %rax    # 0x277ab0
cmpq $0x1, (%rax)
jne 0x168caa
leaq 0x111576(%rip), %rax    # 0x27a1b4
cmpl $0x36, (%rax)
je 0x168c67
leaq 0x11156a(%rip), %rax    # 0x27a1b4
cmpl $0x33, (%rax)
je 0x168c67
leaq 0x11155e(%rip), %rax    # 0x27a1b4
cmpl $0x50, (%rax)
je 0x168c67
leaq 0x111552(%rip), %rax    # 0x27a1b4
cmpl $0x41, (%rax)
jne 0x168caa
leaq 0x10ee42(%rip), %rcx    # 0x277ab0
movabsq $0x400741010, %rax      # imm = 0x400741010
andq (%rcx), %rax
cmpq $0x0, %rax
jne 0x168caa
leaq 0x111528(%rip), %rax    # 0x27a1b0
cmpl $0x0, (%rax)
jne 0x168caa
leaq 0x135479(%rip), %rax    # 0x29e10d
cmpb $0x0, (%rax)
jne 0x168caa
leaq 0x111578(%rip), %rax    # 0x27a218
cmpl $0x0, (%rax)
jne 0x168caa
jmp 0x16925d
leaq 0x10edff(%rip), %rax    # 0x277ab0
cmpq $0x1, (%rax)
jne 0x168d32
leaq 0x10ee26(%rip), %rax    # 0x277ae4
cmpb $0x0, (%rax)
jne 0x168d32
leaq 0x10ede6(%rip), %rax    # 0x277ab0
cmpq $0x1, (%rax)
jne 0x168ce8
leaq 0x1114dd(%rip), %rax    # 0x27a1b4
cmpl $0x41, (%rax)
jne 0x168ce8
leaq 0x10edf5(%rip), %rax    # 0x277ad8
cmpl $0x47, (%rax)
je 0x168d32
leaq 0x10edc1(%rip), %rax    # 0x277ab0
cmpq $0x1, (%rax)
jne 0x168d19
leaq 0x1114b8(%rip), %rax    # 0x27a1b4
cmpl $0x41, (%rax)
jne 0x168d19
leaq 0x10edd0(%rip), %rax    # 0x277ad8
cmpl $0x33, (%rax)
je 0x168d32
leaq 0x10edc4(%rip), %rax    # 0x277ad8
cmpl $0x47, (%rax)
je 0x168d32
leaq 0x10ed90(%rip), %rax    # 0x277ab0
cmpq $0x1, (%rax)
jne 0x168d43
leaq 0x111487(%rip), %rax    # 0x27a1b4
cmpl $0x41, (%rax)
jne 0x168d43
leaq 0x1353d4(%rip), %rax    # 0x29e10d
cmpb $0x0, (%rax)
jne 0x168d43
jmp 0x16925d
callq 0x169390
movl $0x2, %edi
callq 0x15a050
leaq 0x22d4a1(%rip), %rax    # 0x3961fa
movzbl (%rax), %eax
movl %eax, -0x4(%rbp)
leaq 0x22d494(%rip), %rax    # 0x3961fa
movb $0x1, (%rax)
movl $0xff, %edx
xorps %xmm0, %xmm0
movss 0x8bbf(%rip), %xmm3     # 0x171938
movl %edx, %edi
movl %edx, %esi
movaps %xmm3, %xmm1
movaps %xmm3, %xmm2
callq 0x805d0
movl -0x4(%rbp), %eax
movb %al, %cl
leaq 0x22d466(%rip), %rax    # 0x3961fa
movb %cl, (%rax)
movl $0x0, 0x270220(%rip)    # 0x3d8fc0
movl $0x0, 0x27021a(%rip)    # 0x3d8fc4
leaq 0x202e2f(%rip), %rax    # 0x36bbe0
movsd (%rax), %xmm0
leaq 0x231c44(%rip), %rax    # 0x39aa00
movsd %xmm0, (%rax)
movl $0x4, %edi
callq 0x95180
movsd 0x899e(%rip), %xmm0     # 0x171770
leaq 0x231c27(%rip), %rax    # 0x39aa00
addsd (%rax), %xmm0
movl $0x1, %edi
callq 0x94640
leaq 0x231c0e(%rip), %rax    # 0x39a9fc
movl (%rax), %ecx
addl $0x1, %ecx
leaq 0x231c02(%rip), %rax    # 0x39a9fc
movl %ecx, (%rax)
leaq 0x234c2d(%rip), %rdi    # 0x39da30
leaq 0x2fb13(%rip), %rsi     # 0x19891d
movb $0x0, %al
callq 0x9060
leaq 0x235694(%rip), %rax    # 0x39e4ac
movl $0x3, (%rax)
jmp 0x169382
leaq 0x231bd2(%rip), %rax    # 0x39a9fc
cmpl $0x5, (%rax)
jg 0x168e6b
leaq 0x231bc6(%rip), %rax    # 0x39a9fc
movl (%rax), %ecx
addl $0x1, %ecx
leaq 0x231bba(%rip), %rax    # 0x39a9fc
movl %ecx, (%rax)
leaq 0x234be5(%rip), %rdi    # 0x39da30
leaq 0x2faee(%rip), %rsi     # 0x198940
movb $0x0, %al
callq 0x9060
leaq 0x23564c(%rip), %rax    # 0x39e4ac
movl $0x3, (%rax)
jmp 0x169380
leaq 0x231b8a(%rip), %rax    # 0x39a9fc
cmpl $0x6, (%rax)
jne 0x168e89
leaq 0x231b7e(%rip), %rax    # 0x39a9fc
movl $0x64, (%rax)
jmp 0x16937e
leaq 0x231b6c(%rip), %rax    # 0x39a9fc
cmpl $0xa1, (%rax)
jge 0x168f2c
leaq 0x202afd(%rip), %rax    # 0x36b9a0
movsd (%rax), %xmm0
movl 0x270113(%rip), %esi    # 0x3d8fc0
leaq 0x27011c(%rip), %rdi    # 0x3d8fd0
callq 0x15c580
movl %eax, 0x270101(%rip)    # 0x3d8fc0
movl 0x2700fb(%rip), %eax    # 0x3d8fc0
movl %eax, -0xc(%rbp)
movl 0x2700f2(%rip), %eax    # 0x3d8fc0
movl $0x2, %ecx
cltd
idivl %ecx
movl -0xc(%rbp), %edx
movslq %eax, %rcx
leaq 0x2700ed(%rip), %rax    # 0x3d8fd0
movsd (%rax,%rcx,8), %xmm0
cvtsd2ss %xmm0, %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x234b39(%rip), %rdi    # 0x39da30
leaq 0x2fa69(%rip), %rsi     # 0x198967
movb $0x1, %al
callq 0x9060
leaq 0x2355a0(%rip), %rax    # 0x39e4ac
movl $0x3, (%rax)
leaq 0x231ae3(%rip), %rax    # 0x39a9fc
movl (%rax), %ecx
addl $0x1, %ecx
leaq 0x231ad7(%rip), %rax    # 0x39a9fc
movl %ecx, (%rax)
jmp 0x16937c
leaq 0x231ac9(%rip), %rax    # 0x39a9fc
cmpl $0xa1, (%rax)
jne 0x168f90
leaq 0x234aee(%rip), %rdi    # 0x39da30
leaq 0x2fa44(%rip), %rsi     # 0x19898d
movb $0x0, %al
callq 0x9060
leaq 0x235555(%rip), %rax    # 0x39e4ac
movl $0x3, (%rax)
leaq 0x231a9c(%rip), %rax    # 0x39aa00
movsd (%rax), %xmm0
movsd 0x8800(%rip), %xmm1     # 0x171770
subsd %xmm1, %xmm0
movl $0x2, %edi
callq 0x94640
leaq 0x231a77(%rip), %rax    # 0x39a9fc
movl $0x3e8, (%rax)          # imm = 0x3E8
jmp 0x16937a
leaq 0x231a65(%rip), %rax    # 0x39a9fc
cmpl $0x3ed, (%rax)          # imm = 0x3ED
jg 0x168fb9
leaq 0x231a56(%rip), %rax    # 0x39a9fc
movl (%rax), %ecx
addl $0x1, %ecx
leaq 0x231a4a(%rip), %rax    # 0x39a9fc
movl %ecx, (%rax)
jmp 0x169378
leaq 0x231a3c(%rip), %rax    # 0x39a9fc
cmpl $0x3ee, (%rax)          # imm = 0x3EE
jne 0x168fda
leaq 0x231a2d(%rip), %rax    # 0x39a9fc
movl $0x2710, (%rax)         # imm = 0x2710
jmp 0x169376
leaq 0x231a1b(%rip), %rax    # 0x39a9fc
cmpl $0x274d, (%rax)         # imm = 0x274D
jge 0x16907d
leaq 0x231a08(%rip), %rax    # 0x39a9fc
movl (%rax), %ecx
addl $0x1, %ecx
leaq 0x2319fc(%rip), %rax    # 0x39a9fc
movl %ecx, (%rax)
leaq 0x202997(%rip), %rax    # 0x36b9a0
movsd (%rax), %xmm0
movl 0x26ffb1(%rip), %esi    # 0x3d8fc4
leaq 0x2702c6(%rip), %rdi    # 0x3d92e0
callq 0x15c580
movl %eax, 0x26ff9f(%rip)    # 0x3d8fc4
movl 0x26ff99(%rip), %eax    # 0x3d8fc4
movl %eax, -0x10(%rbp)
movl 0x26ff90(%rip), %eax    # 0x3d8fc4
movl $0x2, %ecx
cltd
idivl %ecx
movl -0x10(%rbp), %edx
movslq %eax, %rcx
leaq 0x270297(%rip), %rax    # 0x3d92e0
movsd (%rax,%rcx,8), %xmm0
cvtsd2ss %xmm0, %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x2349d3(%rip), %rdi    # 0x39da30
leaq 0x2f94f(%rip), %rsi     # 0x1989b3
movb $0x1, %al
callq 0x9060
leaq 0x23543a(%rip), %rax    # 0x39e4ac
movl $0x3, (%rax)
jmp 0x169374
movl 0x26ff41(%rip), %eax    # 0x3d8fc4
movl $0x2, %ecx
cltd
idivl %ecx
movslq %eax, %rcx
leaq 0x27024b(%rip), %rax    # 0x3d92e0
movsd (%rax,%rcx,8), %xmm0
cvtsd2ss %xmm0, %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x2032bf(%rip), %rax    # 0x36c368
movsd %xmm0, (%rax)
movl 0x26ff0d(%rip), %eax    # 0x3d8fc0
movl $0x2, %ecx
cltd
idivl %ecx
movslq %eax, %rcx
leaq 0x26ff0b(%rip), %rax    # 0x3d8fd0
movsd (%rax,%rcx,8), %xmm0
cvtsd2ss %xmm0, %xmm0
cvtss2sd %xmm0, %xmm1
leaq 0x20328f(%rip), %rax    # 0x36c368
movsd (%rax), %xmm0
subsd %xmm1, %xmm0
leaq 0x203280(%rip), %rax    # 0x36c368
movsd %xmm0, (%rax)
leaq 0x203275(%rip), %rax    # 0x36c368
movsd (%rax), %xmm0
movsd 0x5fc1(%rip), %xmm1     # 0x16f0c0
divsd %xmm1, %xmm0
leaq 0x20325e(%rip), %rax    # 0x36c368
movsd %xmm0, (%rax)
leaq 0x203243(%rip), %rax    # 0x36c358
movsd 0x60d3(%rip), %xmm0     # 0x16f1f0
movsd %xmm0, (%rax)
leaq 0x203238(%rip), %rax    # 0x36c360
movsd 0x5f38(%rip), %xmm0     # 0x16f068
movsd %xmm0, (%rax)
leaq 0x2031c5(%rip), %rax    # 0x36c300
movsd (%rax), %xmm0
leaq 0x20322a(%rip), %rax    # 0x36c370
movsd %xmm0, (%rax)
leaq 0x2031b7(%rip), %rax    # 0x36c308
movsd (%rax), %xmm0
leaq 0x20321c(%rip), %rax    # 0x36c378
movsd %xmm0, (%rax)
leaq 0x2031b9(%rip), %rax    # 0x36c320
movsd (%rax), %xmm0
leaq 0x20321e(%rip), %rax    # 0x36c390
movsd %xmm0, (%rax)
leaq 0x2031ab(%rip), %rax    # 0x36c328
movsd (%rax), %xmm0
leaq 0x203210(%rip), %rax    # 0x36c398
movsd %xmm0, (%rax)
leaq 0x23186d(%rip), %rax    # 0x39aa00
movsd (%rax), %xmm0
leaq 0x203202(%rip), %rax    # 0x36c3a0
movsd %xmm0, (%rax)
leaq 0x134f73(%rip), %rax    # 0x29e11c
cmpb $0x0, (%rax)
je 0x1691c4
leaq 0x23184b(%rip), %rax    # 0x39aa00
movsd (%rax), %xmm0
leaq 0x203170(%rip), %rax    # 0x36c330
movsd %xmm0, (%rax)
leaq 0x20319d(%rip), %rax    # 0x36c368
movsd (%rax), %xmm0
leaq 0x2031ca(%rip), %rax    # 0x36c3a0
movsd (%rax), %xmm1
leaq 0x20318f(%rip), %rax    # 0x36c370
movsd (%rax), %xmm2
leaq 0x20318c(%rip), %rax    # 0x36c378
movsd (%rax), %xmm3
leaq 0x203161(%rip), %rax    # 0x36c358
movsd (%rax), %xmm4
leaq 0x20315e(%rip), %rax    # 0x36c360
movsd (%rax), %xmm5
leaq 0x234823(%rip), %rdi    # 0x39da30
leaq 0x2f7c0(%rip), %rsi     # 0x1989d4
movb $0x6, %al
callq 0x9060
leaq 0x23528a(%rip), %rax    # 0x39e4ac
movl $0x3, (%rax)
movl $0x1, %edi
callq 0x99d80
leaq 0x2317c7(%rip), %rax    # 0x39aa00
movsd (%rax), %xmm0
movl $0x3, %edi
callq 0x94640
movl $0x5, %edi
callq 0x95180
xorl %edi, %edi
callq 0x95180
callq 0x99cb0
leaq 0x203030(%rip), %rax    # 0x36c294
movss (%rax), %xmm0
movss %xmm0, -0x14(%rbp)
callq 0x159590
movss -0x14(%rbp), %xmm0
cvtsi2ss %eax, %xmm1
addss %xmm1, %xmm0
movss 0x1a0e5(%rip), %xmm1    # 0x18336c
divss %xmm1, %xmm0
movss %xmm0, -0x8(%rbp)
leaq 0x10e84d(%rip), %rax    # 0x277ae4
cmpb $0x0, (%rax)
je 0x1692e2
leaq 0x10e841(%rip), %rax    # 0x277ae4
movzbl (%rax), %eax
cmpl $0x1, %eax
je 0x1692e2
movss -0x8(%rbp), %xmm0
movss 0x869c(%rip), %xmm1     # 0x171954
divss %xmm1, %xmm0
movl $0xff, %edx
movss 0x25ff7(%rip), %xmm1    # 0x18f2c0
movss 0x8667(%rip), %xmm3     # 0x171938
movl %edx, %edi
movl %edx, %esi
movaps %xmm3, %xmm2
callq 0x805d0
jmp 0x169362
leaq 0x10e7c7(%rip), %rax    # 0x277ab0
cmpq $0x1, (%rax)
jne 0x169323
movss -0x8(%rbp), %xmm0
movss 0x8658(%rip), %xmm1     # 0x171954
divss %xmm1, %xmm0
movl $0xff, %edx
movss 0x862f(%rip), %xmm1     # 0x17193c
movss 0x8623(%rip), %xmm3     # 0x171938
movl %edx, %edi
movl %edx, %esi
movaps %xmm3, %xmm2
callq 0x805d0
jmp 0x169360
movss -0x8(%rbp), %xmm0
movss 0x8624(%rip), %xmm1     # 0x171954
divss %xmm1, %xmm0
movss %xmm0, -0x18(%rbp)
callq 0x159730
movss -0x18(%rbp), %xmm0
cvtsi2ss %eax, %xmm1
movl $0xff, %edx
movss 0x85e4(%rip), %xmm3     # 0x171938
movl %edx, %edi
movl %edx, %esi
movaps %xmm3, %xmm2
callq 0x805d0
jmp 0x169362
callq 0x12ae0
leaq 0x23168e(%rip), %rax    # 0x39a9fc
movl $0x0, (%rax)
jmp 0x169376
jmp 0x169378
jmp 0x16937a
jmp 0x16937c
jmp 0x16937e
jmp 0x169380
jmp 0x169382
callq 0x21820
addq $0x20, %rsp
popq %rbp
retq
nopl (%rax)