// File: /workspace/github_cpp_makefile_star2_O1/jkingan[P]ladyheather/heathgps.cpp
// Function: set_ubx_pps(int, unsigned char, unsigned char, unsigned char, double, double, int)
void set_ubx_pps(int chan, u08 pps_rate, u08 pps_enable,  u08 pps_polarity,  double delay, double pps_delay, int save)
{
   if(read_only) return;

   if(saw_ubx_tp5) {
      if(pps_enable == 0) {
//sprintf(debug_text2, "pps disable:%02X  tp5:%02X", pps_rate, saw_ubx_tp5); 
         set_pps_freq(chan, 0.0, 0.10, pps_polarity, delay, pps_delay, save);
      }
      else {
         if(pps_rate == RATE_100PPS) {  // 100 hz
            set_pps_freq(chan, 100.0, 0.10, pps_polarity, delay, pps_delay, save);
         }
         else if(pps_rate == RATE_1000PPS) { // 1000 Hz
            set_pps_freq(chan, 1000.0, 0.10, pps_polarity, delay, pps_delay, save);
         }
         else {
            set_pps_freq(chan, 1.0, 0.10, pps_polarity, delay, pps_delay, save);
         }
//sprintf(debug_text2, "pps enable:%02X  tp5:%02X", pps_rate, saw_ubx_tp5);
      }
   }
   else {
      send_ubx_start(UBX_PPS, 20);

      if(pps_rate == RATE_100PPS) {
         send_dword(10000);    // 100 pps
         send_dword(100);      // 100 us width
      }
      else if(pps_rate == RATE_1000PPS) {
         send_dword(1000);     // 1000 pps
         send_dword(500);      // 500 us width
      }
      else {
         send_dword(1000000);  // 1 sec
         send_dword(10000);    // 10 msec width
      }

      if(pps_enable == 0)   send_byte(0);    // pulse off
      else if(pps_polarity) send_byte(0xFF); // neg pulse
      else                  send_byte(1);    // pos pulse

      if(time_flags & TFLAGS_UTC) send_byte(0);  // align to UTC
      else                        send_byte(1);  // align to GPS

      send_byte(1);  // always on

      send_byte(0);  // rsvd

      send_word((s16) (delay*1.0E9));
      send_word(0);  // rf delay
      send_dword((S32) pps_delay);

      send_ubx_end();
   }


   if(save) save_segment(6, 28);  // save PPS timing config in EEPROM

   if(saw_ubx_tp5) request_ubx_tp5(chan);
   else if(chan == 0) request_ubx_msg(UBX_PPS);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
leaq 0x2cb557(%rip), %rax    # 0x33701a
cmpb $0x0, (%rax)
je 0x6bad7
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r8d, %r14d
movaps %xmm1, %xmm3
movapd %xmm0, %xmm2
movl %ecx, %r12d
movl %edx, %r15d
movl %esi, %r13d
movl %edi, %ebx
leaq 0x2a1939(%rip), %rax    # 0x30d42c
cmpl $0x0, (%rax)
je 0x6bb1c
movzbl %r12b, %esi
testb %r15b, %r15b
je 0x6bb53
cmpb $-0x7d, %r13b
je 0x6bb61
movzbl %r13b, %eax
cmpl $0x82, %eax
jne 0x6bb6b
movsd 0x9d546(%rip), %xmm0    # 0x109060
jmp 0x6bb73
movsd %xmm2, -0x30(%rbp)
movsd %xmm3, -0x38(%rbp)
movl $0x607, %edi            # imm = 0x607
movl $0x14, %esi
callq 0x5f3f5
cmpb $-0x7e, %r13b
je 0x6bb8a
movzbl %r13b, %eax
cmpl $0x83, %eax
jne 0x6bb97
movl $0x1f4, %r13d           # imm = 0x1F4
movl $0x3e8, %edi            # imm = 0x3E8
jmp 0x6bba2
movsd 0x9d5ad(%rip), %xmm1    # 0x109108
xorpd %xmm0, %xmm0
jmp 0x6bb7b
movsd 0x9d4cf(%rip), %xmm0    # 0x109038
jmp 0x6bb73
movsd 0x9d4fd(%rip), %xmm0    # 0x109070
movsd 0x9d58d(%rip), %xmm1    # 0x109108
movl %ebx, %edi
movl %r14d, %edx
callq 0x94dc8
jmp 0x6bc24
movl $0x64, %r13d
movl $0x2710, %edi           # imm = 0x2710
jmp 0x6bba2
movl $0x2710, %r13d          # imm = 0x2710
movl $0xf4240, %edi          # imm = 0xF4240
callq 0x593b5
movl %r13d, %edi
callq 0x593b5
xorl %eax, %eax
negb %r12b
movl $0x0, %ecx
sbbl %ecx, %ecx
orb $0x1, %cl
testb %r15b, %r15b
movzbl %cl, %ecx
cmovel %eax, %ecx
movzbl %cl, %edi
callq 0x591af
leaq 0x2a18ce(%rip), %rax    # 0x30d4a4
movb (%rax), %al
notb %al
andb $0x1, %al
movzbl %al, %edi
callq 0x591af
movl $0x1, %edi
callq 0x591af
xorl %edi, %edi
callq 0x591af
movsd -0x30(%rbp), %xmm0
mulsd 0x9d426(%rip), %xmm0    # 0x109028
cvttsd2si %xmm0, %eax
movzwl %ax, %edi
callq 0x5936c
xorl %edi, %edi
callq 0x5936c
cvttsd2si -0x38(%rbp), %edi
callq 0x593b5
callq 0x5f4ce
testl %r14d, %r14d
je 0x6bc33
movl $0x6, %edi
callq 0x6a306
leaq 0x2a17f2(%rip), %rax    # 0x30d42c
cmpl $0x0, (%rax)
je 0x6bc54
movl %ebx, %edi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6ace6
testl %ebx, %ebx
jne 0x6bac8
movl $0x607, %edi            # imm = 0x607
xorl %esi, %esi
callq 0x5f3f5
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5f4ce