// File: /workspace/github_cpp_makefile_star2_O3/jkingan[P]ladyheather/heathgps.cpp
// Function: set_pps_freq(int, double, double, int, double, double, int)
void set_pps_freq(int chan, double freq, double duty, int polarity, double cable_delay, double pps_delay, int save)
{
u32 flags;
u32 val;
int i;

   if(read_only) return;

   if(rcvr_type == CS_RCVR) {
      if(freq <= 11.0) freq *= 1.0E6;
      set_cs_freq(chan+1, freq);
   }
   else if((rcvr_type == ESIP_RCVR) && (chan == 1)) {  // GCLK
      if((freq == 0.0) || (duty == 0.0)) {
         sprintf(out, "PERDAPI,FREQ,0,%d,%d,0", (int) pps2_freq, (int) (pps2_duty*100.0));
      }
      else {
         sprintf(out, "PERDAPI,FREQ,1,%d,%d,0", (int) freq, (int) (duty*100.0));
      }
      send_esip_cmd(out);
      if(save) save_segment(129, 87);
      request_rcvr_config(8787);
   }
   else if(rcvr_type == NVS_RCVR) {
      if(chan != 0) return;
      if(freq <= 0.0) return;
      freq = 1.0;  // can only do 1pps

      val = nvs_pps_width;
      nvs_pps_width = (u32) ((1.0E9 / freq) * duty);
//sprintf(debug_text, "nvs set width: %d ns", nvs_pps_width);
      set_pps(user_pps_enable, polarity,  cable_delay, pps1_delay,  pps_threshold, 45);
      nvs_pps_width = val;
   }
   else if(rcvr_type == TM4_RCVR) {
      if     (chan == 0) sprintf(out, "#09,%d", (int) freq);
      else if(chan == 1) sprintf(out, "#14,%d", (int) freq); 
      else return;
      send_tm4_cmd(out);
   }
   else if(rcvr_type == UBX_RCVR) {
      if(saw_ubx_tp5 == 0) return;

      send_ubx_start(UBX_TP5, 32);

      send_byte(chan); // output number (0 / 1)
      send_byte(0);    // rsvd
      send_word(0);    // rsvd
      send_word((s16) (cable_delay*1.0E9));
      send_word(0);    // rf delay

      flags = 0x0001;     // enable PPS
      if(freq <= 0.0) {
         flags = 0x0000;  // disable PPS
         freq = 1.0;
      }

      if(duty <= 0.0) duty = 0.500;
      else if(duty >= 100.0) duty = 0.500;
      val = (u32) (duty * (double) 0xFFFFFFFFU);

      send_dword((u32) freq); // freq / period
      send_dword((u32) freq); // gps locked freq/period

      send_dword(val); // ratio / duty cycle
      send_dword(val); // gps locked ratio / duty cycle

      send_dword((S32) pps_delay);

      flags |= 0x0001;  // !!!! pulse active
      flags |= 0x0002;  // sync to GPS
      flags |= 0x0004;  // !!!! enable alt values if not locked to GPS

      flags |= 0x0008;  // 1=freq mode         0=rate mode
//    flags |= 0x0010;  // 1=pulse width mode  0=duty cycle mode
      flags |= 0x0020;  // !!!! align pulse to TOW
      if(polarity == 0) flags |= 0x0040;   // flag bit: 1=rising edge  0=falling edge
      if((time_flags & TFLAGS_UTC) == 0) flags |= 0x0080;  // sync to GPS time

// !!!!! bits 8/9/10/11 are what GNSS time system to use !!!!!
//       0=UTC 1=GPS 2=GLONASS 3=BEIDOU 4=GALILEO
// !!!!! bits 12/13/14 are how to sync time

      send_dword(flags);    // flags

      send_ubx_end();

// sprintf(plot_title, "SETPPS %d: %lf %lf flags:%08X", chan, freq, duty, flags); 

      if(save) save_segment(6, 29);

      if(saw_ubx_tp5) request_ubx_tp5(chan);
      else            request_ubx_msg(UBX_PPS);
   }
   else if(rcvr_type == VENUS_RCVR) {
//    if(chan != 0) return;
      if(freq <= 0.0) return;

      send_venus_start(SET_VENUS_FREQ);
      send_byte(3);
      send_dword((u32) freq);
      send_venus_save(1);

      if(duty > 0.0) {
         val = (u32) ((1.0E6 / freq) * duty);
         send_venus_start(SET_VENUS_WIDTH);
         send_byte(1);
         send_dword(val);
         send_venus_save(1);
      }
   }
   else if(rcvr_type == ZYFER_RCVR) {
      if(duty == (-1.0)) i = (-1);
      else if(duty == (-2.0)) i = (-2);
      else i = (int) duty;
      sprintf(out, "$PULW,%d", i);
      send_zyfer_cmd(out);
   }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
leaq 0x2a21b6(%rip), %rax    # 0x33701a
cmpb $0x0, (%rax)
je 0x94e78
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movapd %xmm2, %xmm4
movapd %xmm0, %xmm2
movl %edi, %r14d
leaq 0x183a46(%rip), %rax    # 0x2188d0
movq (%rax), %rax
cmpq $0x20000, %rax          # imm = 0x20000
jne 0x94ed3
movsd 0x741b3(%rip), %xmm1    # 0x109050
mulsd %xmm2, %xmm1
movapd %xmm2, %xmm0
cmpnlesd 0x898da(%rip), %xmm0    # 0x11e788
andpd %xmm0, %xmm2
andnpd %xmm1, %xmm0
orpd %xmm2, %xmm0
incl %r14d
movl %r14d, %edi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x95384
movl %edx, %ebx
movl $0x80000000, %ecx       # imm = 0x80000000
cmpq %rcx, %rax
setne %cl
cmpl $0x1, %r14d
setne %dl
orb %cl, %dl
jne 0x94f2a
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
jne 0x94efb
jnp 0x94fce
ucomisd %xmm0, %xmm1
jne 0x94f07
jnp 0x94fce
cvttsd2si %xmm2, %edx
mulsd 0x7414d(%rip), %xmm1    # 0x109060
cvttsd2si %xmm1, %ecx
leaq 0x2a7302(%rip), %rdi    # 0x33c220
leaq 0x8f897(%rip), %rsi     # 0x1247bc
jmp 0x94ffe
movl %esi, %r15d
cmpq $0x7ff, %rax            # imm = 0x7FF
jle 0x95037
movabsq $0x400000000, %rcx      # imm = 0x400000000
cmpq %rcx, %rax
je 0x950f8
cmpq $0x100000, %rax         # imm = 0x100000
je 0x95142
cmpq $0x800, %rax            # imm = 0x800
jne 0x94e69
testl %r14d, %r14d
jne 0x94e69
xorpd %xmm0, %xmm0
ucomisd %xmm2, %xmm0
jae 0x94e69
leaq 0x278de6(%rip), %rbx    # 0x30dd68
mulsd 0x7409e(%rip), %xmm1    # 0x109028
cvttsd2si %xmm1, %rax
movl (%rbx), %r14d
movl %eax, (%rbx)
leaq 0x278d86(%rip), %rax    # 0x30dd21
movzbl (%rax), %edi
leaq 0x278e33(%rip), %rax    # 0x30ddd8
movsd (%rax), %xmm1
leaq 0x278d6c(%rip), %rax    # 0x30dd1c
movss (%rax), %xmm2
movzbl %r15b, %esi
movapd %xmm4, %xmm0
movl $0x2d, %edx
callq 0x6add8
movl %r14d, (%rbx)
jmp 0x94e69
leaq 0x278d6b(%rip), %rax    # 0x30dd40
cvttsd2si (%rax), %edx
leaq 0x278d68(%rip), %rax    # 0x30dd48
movsd (%rax), %xmm0
mulsd 0x74074(%rip), %xmm0    # 0x109060
cvttsd2si %xmm0, %ecx
leaq 0x2a7229(%rip), %rdi    # 0x33c220
leaq 0x8f7a7(%rip), %rsi     # 0x1247a5
xorl %eax, %eax
callq 0x9060
leaq 0x2a7214(%rip), %rdi    # 0x33c220
callq 0x5ee9b
testl %ebx, %ebx
je 0x9501f
movl $0x81, %edi
callq 0x6a4ce
movl $0x2253, %edi           # imm = 0x2253
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x667f2
cmpq $0x4, %rax
je 0x95199
cmpq $0x80, %rax
jne 0x94e69
xorpd %xmm0, %xmm0
ucomisd %xmm2, %xmm0
jae 0x94e69
movl $0x65, %edi
movl $0x7, %esi
movsd %xmm1, -0x38(%rbp)
movapd %xmm2, -0x50(%rbp)
callq 0x5fb3c
movl $0x3, %edi
callq 0x590f7
cvttsd2si -0x50(%rbp), %rdi
callq 0x592eb
movl $0x1, %edi
callq 0x5fc00
movapd -0x50(%rbp), %xmm2
movsd -0x38(%rbp), %xmm1
ucomisd 0x74073(%rip), %xmm1    # 0x109118
jbe 0x94e69
movsd 0x73f9d(%rip), %xmm0    # 0x109050
divsd %xmm2, %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rbx
movl $0x65, %edi
movl $0x7, %esi
callq 0x5fb3c
movl $0x1, %edi
callq 0x590f7
movl %ebx, %edi
callq 0x592eb
movl $0x1, %edi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5fc00
cmpl $0x1, %r14d
ja 0x94e69
testl %r14d, %r14d
leaq 0x8f6c7(%rip), %rax     # 0x1247d3
leaq 0x8f6c7(%rip), %rsi     # 0x1247da
cmoveq %rax, %rsi
cvttsd2si %xmm2, %edx
leaq 0x2a70fe(%rip), %rbx    # 0x33c220
movq %rbx, %rdi
xorl %eax, %eax
callq 0x9060
movq %rbx, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5f170
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
ucomisd 0x73fa9(%rip), %xmm1    # 0x1090f8
jne 0x95153
jnp 0x95168
movl $0xfffffffe, %edx       # imm = 0xFFFFFFFE
ucomisd 0x768c0(%rip), %xmm1    # 0x10ba20
jne 0x95164
jnp 0x95168
cvttsd2si %xmm1, %edx
leaq 0x2a70b1(%rip), %rbx    # 0x33c220
leaq 0x8f66b(%rip), %rsi     # 0x1247e1
movq %rbx, %rdi
xorl %eax, %eax
callq 0x9060
incq %rbx
movq %rbx, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5f0e5
leaq 0x27828c(%rip), %rax    # 0x30d42c
cmpl $0x0, (%rax)
je 0x94e69
movsd %xmm3, -0x60(%rbp)
movl $0x631, %edi            # imm = 0x631
movl $0x20, %esi
movsd %xmm1, -0x38(%rbp)
movapd %xmm2, -0x50(%rbp)
movsd %xmm4, -0x58(%rbp)
callq 0x5f37d
movzbl %r14b, %edi
callq 0x590f7
xorl %r13d, %r13d
xorl %edi, %edi
callq 0x590f7
movw %r13w, -0x2a(%rbp)
leaq 0x1835e9(%rip), %r12    # 0x2187d4
xorl %eax, %eax
cmpb $0x0, (%r12)
sete %al
movzbl -0x2a(%rbp,%rax), %edi
callq 0x590f7
xorl %eax, %eax
cmpb $0x0, (%r12)
setne %al
movzbl -0x2a(%rbp,%rax), %edi
callq 0x590f7
movsd -0x58(%rbp), %xmm0
mulsd 0x73e08(%rip), %xmm0    # 0x109028
cvttsd2si %xmm0, %eax
movw %ax, -0x2c(%rbp)
xorl %eax, %eax
cmpb $0x0, (%r12)
sete %al
movzbl -0x2c(%rbp,%rax), %edi
callq 0x590f7
xorl %eax, %eax
cmpb $0x0, (%r12)
setne %al
movzbl -0x2c(%rbp,%rax), %edi
callq 0x590f7
movw %r13w, -0x2e(%rbp)
xorl %eax, %eax
cmpb $0x0, (%r12)
sete %al
movzbl -0x2e(%rbp,%rax), %edi
callq 0x590f7
xorl %eax, %eax
cmpb $0x0, (%r12)
setne %al
movzbl -0x2e(%rbp,%rax), %edi
callq 0x590f7
xorpd %xmm2, %xmm2
movapd -0x50(%rbp), %xmm1
movapd %xmm1, %xmm0
cmpnlesd %xmm2, %xmm0
andpd %xmm0, %xmm1
movapd %xmm1, %xmm3
movsd 0x73dd1(%rip), %xmm1    # 0x109070
andnpd %xmm1, %xmm0
orpd %xmm3, %xmm0
movsd 0x73db9(%rip), %xmm1    # 0x109068
ucomisd -0x38(%rbp), %xmm2
movsd -0x38(%rbp), %xmm2
jae 0x952c5
ucomisd 0x73d9d(%rip), %xmm2    # 0x109060
jb 0x952c9
movapd %xmm1, %xmm2
mulsd 0x894af(%rip), %xmm2    # 0x11e780
cvttsd2si %xmm2, %r12
cvttsd2si %xmm0, %r13
movl %r13d, %edi
callq 0x592eb
movl %r13d, %edi
callq 0x592eb
movl %r12d, %edi
callq 0x592eb
movl %r12d, %edi
callq 0x592eb
cvttsd2si -0x60(%rbp), %edi
callq 0x592eb
xorl %eax, %eax
testl %r15d, %r15d
sete %al
shll $0x6, %eax
leaq 0x27818d(%rip), %rcx    # 0x30d4a4
movl (%rcx), %ecx
shll $0x7, %ecx
movzbl %cl, %ecx
leal (%rax,%rcx), %edi
addl $0x2f, %edi
xorl $0x80, %edi
callq 0x592eb
callq 0x5f456
testl %ebx, %ebx
je 0x95343
movl $0x6, %edi
callq 0x6a4ce
leaq 0x2780e2(%rip), %rax    # 0x30d42c
cmpl $0x0, (%rax)
je 0x95365
movl %r14d, %edi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6ada1
movl $0x607, %edi            # imm = 0x607
xorl %esi, %esi
callq 0x5f37d
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5f456