// File: 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
subq $0x50, %rsp
movl %edi, -0x4(%rbp)
movsd %xmm0, -0x10(%rbp)
movsd %xmm1, -0x18(%rbp)
movl %esi, -0x1c(%rbp)
movsd %xmm2, -0x28(%rbp)
movsd %xmm3, -0x30(%rbp)
movl %edx, -0x34(%rbp)
leaq 0x2cad4e(%rip), %rax    # 0x3961fa
cmpb $0x0, (%rax)
je 0xcb4b6
jmp 0xcb9ef
leaq 0x1ac5f3(%rip), %rax    # 0x277ab0
cmpq $0x20000, (%rax)        # imm = 0x20000
jne 0xcb4fc
movsd 0xb7b8a(%rip), %xmm0    # 0x183058
ucomisd -0x10(%rbp), %xmm0
jb 0xcb4e7
movsd 0xa3b63(%rip), %xmm0    # 0x16f040
mulsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x10(%rbp)
movl -0x4(%rbp), %edi
addl $0x1, %edi
movsd -0x10(%rbp), %xmm0
callq 0xcba00
jmp 0xcb9ef
movl $0x80000000, %ecx       # imm = 0x80000000
leaq 0x1ac5a8(%rip), %rax    # 0x277ab0
cmpq %rcx, (%rax)
jne 0xcb5d1
cmpl $0x1, -0x4(%rbp)
jne 0xcb5d1
movsd -0x10(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0xcb52d
jp 0xcb52d
jmp 0xcb53d
movsd -0x18(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0xcb576
jp 0xcb576
leaq 0x2a19dc(%rip), %rax    # 0x36cf20
cvttsd2si (%rax), %edx
movsd 0xa3b00(%rip), %xmm0    # 0x16f050
leaq 0x2a19d1(%rip), %rax    # 0x36cf28
mulsd (%rax), %xmm0
cvttsd2si %xmm0, %ecx
leaq 0x2cfe9a(%rip), %rdi    # 0x39b400
leaq 0xbc524(%rip), %rsi     # 0x187a91
movb $0x0, %al
callq 0x9060
jmp 0xcb5a1
cvttsd2si -0x10(%rbp), %edx
movsd 0xa3acd(%rip), %xmm0    # 0x16f050
mulsd -0x18(%rbp), %xmm0
cvttsd2si %xmm0, %ecx
leaq 0x2cfe6d(%rip), %rdi    # 0x39b400
leaq 0xbc50e(%rip), %rsi     # 0x187aa8
movb $0x0, %al
callq 0x9060
leaq 0x2cfe58(%rip), %rdi    # 0x39b400
callq 0x830d0
cmpl $0x0, -0x34(%rbp)
je 0xcb5c2
movl $0x81, %edi
movl $0x57, %esi
callq 0x90d70
movl $0x2253, %edi           # imm = 0x2253
callq 0x8c270
jmp 0xcb9ed
leaq 0x1ac4d8(%rip), %rax    # 0x277ab0
cmpq $0x800, (%rax)          # imm = 0x800
jne 0xcb68b
cmpl $0x0, -0x4(%rbp)
je 0xcb5f0
jmp 0xcb9ef
xorps %xmm0, %xmm0
ucomisd -0x10(%rbp), %xmm0
jb 0xcb5ff
jmp 0xcb9ef
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, -0x10(%rbp)
leaq 0x2a1934(%rip), %rax    # 0x36cf48
movl (%rax), %eax
movl %eax, -0x3c(%rbp)
movsd -0x10(%rbp), %xmm1
movsd 0xa3a02(%rip), %xmm0    # 0x16f028
divsd %xmm1, %xmm0
movsd -0x18(%rbp), %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movl %eax, %ecx
leaq 0x2a1907(%rip), %rax    # 0x36cf48
movl %ecx, (%rax)
leaq 0x2a18b7(%rip), %rax    # 0x36cf01
movb (%rax), %cl
movl -0x1c(%rbp), %eax
movsd -0x28(%rbp), %xmm0
leaq 0x2a195d(%rip), %rdx    # 0x36cfb8
movsd (%rdx), %xmm1
leaq 0x2a1896(%rip), %rdx    # 0x36cefc
movss (%rdx), %xmm2
movl $0x2d, %edx
movzbl %cl, %edi
movzbl %al, %esi
callq 0x91b20
movl -0x3c(%rbp), %ecx
leaq 0x2a18c4(%rip), %rax    # 0x36cf48
movl %ecx, (%rax)
jmp 0xcb9eb
movabsq $0x400000000, %rcx      # imm = 0x400000000
leaq 0x1ac414(%rip), %rax    # 0x277ab0
cmpq %rcx, (%rax)
jne 0xcb6fd
cmpl $0x0, -0x4(%rbp)
jne 0xcb6c3
cvttsd2si -0x10(%rbp), %edx
leaq 0x2cfd4d(%rip), %rdi    # 0x39b400
leaq 0xbc405(%rip), %rsi     # 0x187abf
movb $0x0, %al
callq 0x9060
jmp 0xcb6ec
cmpl $0x1, -0x4(%rbp)
jne 0xcb6e5
cvttsd2si -0x10(%rbp), %edx
leaq 0x2cfd2b(%rip), %rdi    # 0x39b400
leaq 0xbc3ea(%rip), %rsi     # 0x187ac6
movb $0x0, %al
callq 0x9060
jmp 0xcb6ea
jmp 0xcb9ef
jmp 0xcb6ec
leaq 0x2cfd0d(%rip), %rdi    # 0x39b400
callq 0x83490
jmp 0xcb9e9
leaq 0x1ac3ac(%rip), %rax    # 0x277ab0
cmpq $0x4, (%rax)
jne 0xcb8b9
leaq 0x2a0ef7(%rip), %rax    # 0x36c60c
cmpl $0x0, (%rax)
jne 0xcb71f
jmp 0xcb9ef
movl $0x631, %edi            # imm = 0x631
movl $0x20, %esi
callq 0x83710
movzbl -0x4(%rbp), %edi
callq 0x7b210
xorl %edi, %edi
movl %edi, -0x44(%rbp)
callq 0x7b210
movl -0x44(%rbp), %edi
callq 0x7b4b0
movsd -0x28(%rbp), %xmm0
movsd 0xa38d2(%rip), %xmm1    # 0x16f028
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movzwl %ax, %edi
callq 0x7b4b0
xorl %edi, %edi
callq 0x7b4b0
movl $0x1, -0x38(%rbp)
xorps %xmm0, %xmm0
ucomisd -0x10(%rbp), %xmm0
jb 0xcb792
movl $0x0, -0x38(%rbp)
movsd 0xa38db(%rip), %xmm0    # 0x16f068
movsd %xmm0, -0x10(%rbp)
xorps %xmm0, %xmm0
ucomisd -0x18(%rbp), %xmm0
jb 0xcb7ab
movsd 0xa38ec(%rip), %xmm0    # 0x16f090
movsd %xmm0, -0x18(%rbp)
jmp 0xcb7cd
movsd -0x18(%rbp), %xmm0
movsd 0xa3898(%rip), %xmm1    # 0x16f050
ucomisd %xmm1, %xmm0
jb 0xcb7cb
movsd 0xa38ca(%rip), %xmm0    # 0x16f090
movsd %xmm0, -0x18(%rbp)
jmp 0xcb7cd
movsd -0x18(%rbp), %xmm0
movsd 0xb7876(%rip), %xmm1    # 0x183050
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movl %eax, -0x3c(%rbp)
cvttsd2si -0x10(%rbp), %rax
movl %eax, %edi
callq 0x7b520
cvttsd2si -0x10(%rbp), %rax
movl %eax, %edi
callq 0x7b520
movl -0x3c(%rbp), %edi
callq 0x7b520
movl -0x3c(%rbp), %edi
callq 0x7b520
cvttsd2si -0x30(%rbp), %edi
callq 0x7b520
movl -0x38(%rbp), %eax
orl $0x1, %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
orl $0x2, %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
orl $0x4, %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
orl $0x8, %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
orl $0x20, %eax
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x1c(%rbp)
jne 0xcb856
movl -0x38(%rbp), %eax
orl $0x40, %eax
movl %eax, -0x38(%rbp)
leaq 0x2a0e27(%rip), %rax    # 0x36c684
movl (%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0xcb872
movl -0x38(%rbp), %eax
orl $0x80, %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %edi
callq 0x7b520
callq 0x83820
cmpl $0x0, -0x34(%rbp)
je 0xcb894
movl $0x6, %edi
movl $0x1d, %esi
callq 0x90d70
leaq 0x2a0d71(%rip), %rax    # 0x36c60c
cmpl $0x0, (%rax)
je 0xcb8aa
movl -0x4(%rbp), %edi
callq 0x91ae0
jmp 0xcb8b4
movl $0x607, %edi            # imm = 0x607
callq 0x85d00
jmp 0xcb9e7
leaq 0x1ac1f0(%rip), %rax    # 0x277ab0
cmpq $0x80, (%rax)
jne 0xcb969
xorps %xmm0, %xmm0
ucomisd -0x10(%rbp), %xmm0
jb 0xcb8dc
jmp 0xcb9ef
movl $0x65, %edi
movl $0x7, %esi
callq 0x84080
movl $0x3, %edi
callq 0x7b210
cvttsd2si -0x10(%rbp), %rax
movl %eax, %edi
callq 0x7b520
movl $0x1, %edi
callq 0x84130
movsd -0x18(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xcb967
movsd -0x10(%rbp), %xmm1
movsd 0xa3719(%rip), %xmm0    # 0x16f040
divsd %xmm1, %xmm0
movsd -0x18(%rbp), %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movl %eax, -0x3c(%rbp)
movl $0x65, %edi
movl $0x7, %esi
callq 0x84080
movl $0x1, %edi
callq 0x7b210
movl -0x3c(%rbp), %edi
callq 0x7b520
movl $0x1, %edi
callq 0x84130
jmp 0xcb9e5
leaq 0x1ac140(%rip), %rax    # 0x277ab0
cmpq $0x100000, (%rax)       # imm = 0x100000
jne 0xcb9e3
movsd -0x18(%rbp), %xmm0
movsd 0xa3792(%rip), %xmm1    # 0x16f118
ucomisd %xmm1, %xmm0
jne 0xcb997
jp 0xcb997
movl $0xffffffff, -0x40(%rbp) # imm = 0xFFFFFFFF
jmp 0xcb9bf
movsd -0x18(%rbp), %xmm0
movsd 0xa5dfc(%rip), %xmm1    # 0x1717a0
ucomisd %xmm1, %xmm0
jne 0xcb9b5
jp 0xcb9b5
movl $0xfffffffe, -0x40(%rbp) # imm = 0xFFFFFFFE
jmp 0xcb9bd
cvttsd2si -0x18(%rbp), %eax
movl %eax, -0x40(%rbp)
jmp 0xcb9bf
movl -0x40(%rbp), %edx
leaq 0x2cfa37(%rip), %rdi    # 0x39b400
leaq 0xbc0fd(%rip), %rsi     # 0x187acd
movb $0x0, %al
callq 0x9060
leaq 0x2cfa22(%rip), %rdi    # 0x39b400
callq 0x85d30
jmp 0xcb9e5
jmp 0xcb9e7
jmp 0xcb9e9
jmp 0xcb9eb
jmp 0xcb9ed
jmp 0xcb9ef
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)