// File: /workspace/github_cpp_makefile_star2_O3/jkingan[P]ladyheather/heathgps.cpp
// Function: get_z12_message()
void get_z12_message()
{
u08 c;
static int bin_len = 0;
static int z12_bin_len = 0;
static int bin_msg = 0;
static unsigned bin_sum = 0;
static unsigned odd=0, even=0, ck=0;
static unsigned msg_sum = 0;
static unsigned dben_low, dben_high;
static int mpc_msg = 0;

   // This routine buffers up an incoming NMEA message.  When the end of the
   // message is seen, the message is parsed and decoded with decode_nmea_msg()


   if(SERIAL_DATA_AVAILABLE(RCVR_PORT) == 0) {
      check_com_timer(RCVR_PORT);
if(in_mpc == 0) {
   get_norcvr_message();
   reset_com_timer(RCVR_PORT);
}
      return;
   }
   else {
      reset_com_timer(RCVR_PORT);
   }

   c = get_com_char();
   if(com[RCVR_PORT].rcv_error) {      // parity/framing/overrun errors
      com[RCVR_PORT].rcv_error = 0;
//!!!!!qqqq    goto rst_msg;
   }

   if(bin_len) {
      if(bin_len == (-2)) {  // DBEN payload length
         dben_low = c;
         ++bin_len;
      }
      else if(bin_len == (-1)) {
         dben_high = c;
         bin_len = (dben_high << 8) | dben_low;
         bin_sum = 0;
      }
      else if(tsip_wptr < MAX_TSIP) {   // add char to the message buffer
         tsip_buf[tsip_wptr++] = c;

         nmea_vfy_cksum ^= c;       // XOR cksum on MPC message

         if(bin_len & 1) odd = c;   // 16-bit sum on other messages
         else            even = c;

         if(bin_len <= 2) {
            if(bin_len == 1) {
               msg_sum = (even * 256) + odd;
            }
         }
         else if(bin_len & 1) {
            ck = ((unsigned) odd);
            ck += (((unsigned) even) << 8);
            bin_sum += ck;
            bin_sum &= 0xFFFF;
         }
      }
      else {
         tsip_error |= OVFL_ERROR;
         goto rst_msg;
      }
      ++bin_msg;
      --bin_len;
      if(bin_len <= 0) goto bin_end;
   }
   else if(tsip_sync == 0) {    // syncing to start of message, search for a '$'
      if(c == '$') {
         tsip_sync = 1;
         get_sync_time();
      }
      tsip_wptr = 0;
      tsip_rptr = 0;
      bin_len = 0;
//    z12_bin_len = 0;
      bin_msg = 0;
      bin_sum = 0;
      msg_sum = 0;
      nmea_vfy_cksum = 0;  // the calculated message checksum
      nmea_msg_cksum = 0;  // the checksum in the message
      return;
   }
   else if(tsip_sync == 1) { // '$' has been seen, now build the message
      if((c == 0x0D) || (c == 0x0A)) goto nmea_end;  // message has no checksum

      if(c == '*') tsip_sync = 2;  // message checksum follows
      else if(tsip_wptr < MAX_TSIP) {   // add char to the message buffer
         nmea_vfy_cksum ^= c;
         tsip_buf[tsip_wptr++] = c;
         tsip_buf[tsip_wptr] = 0;
         if(tsip_wptr == 10) {  // see if Z12 binary data follows
            mpc_msg = 0;
            if     (strstr((char *) tsip_buf, "PBN,")) bin_len = 56;
            else if(strstr((char *) tsip_buf, "ALM,")) bin_len = 70;
            else if(strstr((char *) tsip_buf, "SNV,")) bin_len = 132;
            else if(strstr((char *) tsip_buf, "EPB,")) bin_len = 122;
            else if(strstr((char *) tsip_buf, "RPC,")) bin_len = (-2);  // !!!! has variable sized messages, we ignore this
            else if(strstr((char *) tsip_buf, "MPC,")) {
               bin_len = 95;  // !!!! was 97
               nmea_vfy_cksum = 0x00;
               mpc_msg = 1;
            }
            z12_bin_len = bin_len;
         }
      }
      else {
         tsip_error |= OVFL_ERROR;
         goto rst_msg;
      }
   }
   else if(tsip_sync == 2) {  // getting message checksum chars
      if((c == 0x0D) || (c == 0x0A)) goto nmea_end;

      if     ((c >= '0') && (c <= '9')) nmea_msg_cksum = (nmea_msg_cksum << 4) | (c-'0');
      else if((c >= 'A') && (c <= 'F')) nmea_msg_cksum = (nmea_msg_cksum << 4) | ((c-'A')+10);
      else goto rst_msg;
   }
   else if(tsip_sync == 3) {  // end of message
      nmea_end:
if(debug_file) fprintf(debug_file, "At nmea end:(%s)\n", tsip_buf);
      if((tsip_sync == 2) && (nmea_vfy_cksum != nmea_msg_cksum)) { // checksum error
         tsip_error |= CKSUM_ERROR;
         goto rst_msg;
      }

      bin_end:
      if(bin_msg) {
         if(mpc_msg && (nmea_vfy_cksum == 0)) decode_nmea_msg();
         else if(msg_sum == bin_sum) decode_nmea_msg();
         else if(debug_file) {
            sprintf(out, "Message checksum error: nmea:%02X  msg:%04X vfy:%04X", nmea_vfy_cksum, msg_sum,bin_sum);
            fprintf(debug_file, "%s\n", out);
in_mpc = 0;
//strcpy(debug_text, out);
         }
      }
      else {
         decode_nmea_msg();      // message checksum matches, process the message
      }
      packet_end = 1;

      if(debug_file) {
         if(bin_msg) {
            tsip_buf[10] = 0;
            fprintf(debug_file, "RCVD Z12:%-11.11s (binary len:%d)  bin_sum:%04X  msg_sum:%04X\n", tsip_buf,z12_bin_len, bin_sum, msg_sum);
         }
         else {
            fprintf(debug_file, "RCVD Z12:%s  (ascii len:%d)\n", tsip_buf, z12_bin_len);
         }
         if(tsip_buf[0] == 'G') ;
         else if(tsip_buf[0] == 'P') ;
         else {
            fprintf(debug_file, "BAD MSG %d: msglen:%d  wptr:%d len:%d\n", bin_msg, z12_bin_len, tsip_wptr, bin_len);
         }
      }

      tsip_wptr = 0;     // prepare for next message
      tsip_sync = 0;
      bin_len = 0;
//    z12_bin_len = 0;
      bin_msg = 0;
      bin_sum = 0;
      msg_sum = 0;
      venus_nmea = 0;
   }
   else {     // should never happen, prepare for next message
      rst_msg:
      tsip_wptr = 0;
      tsip_sync = 0;
      bin_len = 0;
//    z12_bin_len = 0;
      bin_msg = 0;
      bin_sum = 0;
      msg_sum = 0;
      nmea_vfy_cksum = nmea_msg_cksum = 0; // init received message checksum
      venus_nmea = 0;
   }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r14
pushq %rbx
movl $0xa, %edi
callq 0xb9ad
movl $0xa, %edi
testl %eax, %eax
je 0x849a0
callq 0x1ea0b
callq 0x584bd
leaq 0x28998f(%rip), %rcx    # 0x30e2e0
cmpb $0x0, 0x17a12(%rcx)
je 0x84961
movb $0x0, 0x17a12(%rcx)
movl 0x2ed4c9(%rip), %ecx    # 0x371e30
testl %ecx, %ecx
je 0x849ff
cmpl $-0x1, %ecx
je 0x849c5
cmpl $-0x2, %ecx
jne 0x84a71
movl %eax, 0x2ed4c9(%rip)    # 0x371e4c
movl 0x2ed4af(%rip), %eax    # 0x371e38
incl %eax
movl %eax, 0x2ed4a7(%rip)    # 0x371e38
movl $0xfffffffe, 0x2ed495(%rip) # imm = 0xFFFFFFFE
jmp 0x84b7b
callq 0x1e66a
cmpl $0x0, 0x2ed480(%rip)    # 0x371e2c
jne 0x84e03
callq 0x8247a
movl $0xa, %edi
popq %rbx
popq %r14
popq %rbp
jmp 0x1ea0b
shll $0x8, %eax
orl 0x2ed47e(%rip), %eax    # 0x371e4c
movl $0x0, 0x2ed464(%rip)    # 0x371e3c
movl %eax, %ecx
movl 0x2ed458(%rip), %eax    # 0x371e38
incl %eax
movl %eax, 0x2ed450(%rip)    # 0x371e38
leal -0x1(%rcx), %edx
movl %edx, 0x2ed43f(%rip)    # 0x371e30
cmpl $0x2, %ecx
jl 0x84b7b
jmp 0x84e03
leaq 0x2b3850(%rip), %rbx    # 0x338256
movzwl (%rbx), %ecx
cmpq $0x3, %rcx
ja 0x84dc3
leaq 0x9a756(%rip), %rdx     # 0x11f170
movslq (%rdx,%rcx,4), %rsi
addq %rdx, %rsi
jmpq *%rsi
cmpl $0x24, %eax
jne 0x84a32
movw $0x1, (%rbx)
callq 0x58672
leaq 0x2b3819(%rip), %rax    # 0x338252
xorl %ecx, %ecx
movw %cx, (%rax)
leaq 0x2b380f(%rip), %rax    # 0x338254
movw %cx, (%rax)
movl %ecx, 0x2ed3e2(%rip)    # 0x371e30
movl %ecx, 0x2ed3e4(%rip)    # 0x371e38
movl %ecx, 0x2ed3e2(%rip)    # 0x371e3c
movl %ecx, 0x2ed3e8(%rip)    # 0x371e48
movb %cl, 0x2e804e(%rip)     # 0x36cab4
movb %cl, 0x2e8049(%rip)     # 0x36cab5
jmp 0x84e03
leaq 0x2b37da(%rip), %rdi    # 0x338252
movzwl (%rdi), %esi
movl $0x8000, %edx           # imm = 0x8000
cmpq $0xfff, %rsi            # imm = 0xFFF
ja 0x84dba
leal 0x1(%rsi), %edx
movw %dx, (%rdi)
leaq 0x2b27b6(%rip), %rdx    # 0x337250
movb %al, (%rdx,%rsi)
xorb %al, 0x2e8011(%rip)     # 0x36cab4
testb $0x1, %cl
jne 0x84abf
movl %eax, 0x2ed396(%rip)    # 0x371e44
cmpl $0x1, %ecx
jne 0x849da
movl 0x2ed383(%rip), %edx    # 0x371e40
jmp 0x84adf
movl %eax, 0x2ed37b(%rip)    # 0x371e40
cmpl $0x2, %ecx
jg 0x84cbb
cmpl $0x1, %ecx
jne 0x849da
movl %eax, %edx
movl 0x2ed365(%rip), %eax    # 0x371e44
shll $0x8, %eax
addl %edx, %eax
movl %eax, 0x2ed35e(%rip)    # 0x371e48
movl 0x2ed348(%rip), %eax    # 0x371e38
incl %eax
movl %eax, 0x2ed340(%rip)    # 0x371e38
decl %ecx
movl %ecx, 0x2ed330(%rip)    # 0x371e30
jmp 0x84b7b
cmpb $0xa, %al
je 0x84b31
movzbl %al, %edx
cmpl $0xd, %edx
je 0x84b31
cmpl $0x2a, %edx
jne 0x84d45
movw $0x2, (%rbx)
jmp 0x84e03
cmpb $0xa, %al
je 0x84b31
movzbl %al, %edx
cmpl $0xd, %edx
jne 0x84d25
leaq 0x1b9858(%rip), %rax    # 0x23e390
movq (%rax), %rdi
testq %rdi, %rdi
je 0x84b58
leaq 0x9e3e8(%rip), %rsi     # 0x122f2f
leaq 0x2b2702(%rip), %rdx    # 0x337250
xorl %eax, %eax
callq 0x9560
movzwl (%rbx), %ecx
cmpw $0x2, %cx
jne 0x84b75
movb 0x2e7f50(%rip), %al     # 0x36cab4
movl $0x4000, %edx           # imm = 0x4000
cmpb 0x2e7f46(%rip), %al     # 0x36cab5
jne 0x84dba
movl 0x2ed2bd(%rip), %eax    # 0x371e38
testl %eax, %eax
je 0x84bab
movb 0x2e7f2f(%rip), %al     # 0x36cab4
testb %al, %al
sete %cl
andb 0x2ed2c0(%rip), %cl     # 0x371e50
cmpb $0x1, %cl
je 0x84bab
movl 0x2ed2ad(%rip), %ecx    # 0x371e48
movl 0x2ed29a(%rip), %r8d    # 0x371e3c
cmpl %r8d, %ecx
jne 0x84c70
callq 0x84f17
leaq 0x1b97d9(%rip), %rbx    # 0x23e390
movq (%rbx), %rdi
movl $0x1, 0x2e70d0(%rip)    # 0x36bc94
testq %rdi, %rdi
je 0x84cf2
cmpl $0x0, 0x2ed264(%rip)    # 0x371e38
je 0x84c05
leaq 0x2b2673(%rip), %rdx    # 0x337250
movb $0x0, 0xa(%rdx)
movl 0x2ed24d(%rip), %ecx    # 0x371e34
movl 0x2ed24e(%rip), %r8d    # 0x371e3c
movl 0x2ed253(%rip), %r9d    # 0x371e48
leaq 0x9e37a(%rip), %rsi     # 0x122f76
xorl %eax, %eax
callq 0x9560
jmp 0x84c20
movl 0x2ed229(%rip), %ecx    # 0x371e34
leaq 0x9e3a3(%rip), %rsi     # 0x122fb5
leaq 0x2b2637(%rip), %rdx    # 0x337250
xorl %eax, %eax
callq 0x9560
leaq 0x2b2629(%rip), %rax    # 0x337250
movzbl (%rax), %eax
cmpl $0x47, %eax
je 0x84cf2
cmpl $0x50, %eax
je 0x84cf2
movq (%rbx), %rdi
movl 0x2ed1f3(%rip), %edx    # 0x371e38
movl 0x2ed1e9(%rip), %ecx    # 0x371e34
leaq 0x2b3600(%rip), %rax    # 0x338252
movzwl (%rax), %r8d
movl 0x2ed1d3(%rip), %r9d    # 0x371e30
leaq 0x9e36e(%rip), %rsi     # 0x122fd2
xorl %eax, %eax
callq 0x9560
jmp 0x84cf2
leaq 0x1b9719(%rip), %r14    # 0x23e390
cmpq $0x0, (%r14)
je 0x84ce8
movzbl %al, %edx
leaq 0x2b7599(%rip), %rbx    # 0x33c220
leaq 0x9e2b3(%rip), %rsi     # 0x122f41
movq %rbx, %rdi
xorl %eax, %eax
callq 0x9060
movq (%r14), %rdi
leaq 0xa0e35(%rip), %rsi     # 0x125ad7
movq %rbx, %rdx
xorl %eax, %eax
callq 0x9560
movl $0x0, 0x2ed176(%rip)    # 0x371e2c
jmp 0x84bb0
movl 0x2ed183(%rip), %edx    # 0x371e44
shll $0x8, %edx
orl %eax, %edx
addl 0x2ed170(%rip), %edx    # 0x371e3c
incl 0x2ed166(%rip)          # 0x371e38
movzwl %dx, %eax
movl %eax, 0x2ed161(%rip)    # 0x371e3c
decl %ecx
movl %ecx, 0x2ed14d(%rip)    # 0x371e30
jmp 0x84e03
movl $0x1, 0x2e6fa2(%rip)    # 0x36bc94
leaq 0x2b3559(%rip), %rax    # 0x338252
xorl %ecx, %ecx
movw %cx, (%rax)
leaq 0x2b3551(%rip), %rax    # 0x338256
movw %cx, (%rax)
movl %ecx, 0x2ed122(%rip)    # 0x371e30
movl %ecx, 0x2ed124(%rip)    # 0x371e38
movl %ecx, 0x2ed122(%rip)    # 0x371e3c
movl %ecx, 0x2ed128(%rip)    # 0x371e48
jmp 0x84dfd
leal -0x30(%rax), %ecx
cmpl $0x9, %ecx
ja 0x84e08
movzbl 0x2e7d7d(%rip), %ecx    # 0x36cab5
shll $0x4, %ecx
addl $0xd0, %eax
jmp 0x84e1f
leaq 0x2b3506(%rip), %rsi    # 0x338252
movzwl (%rsi), %ecx
movl $0x8000, %edx           # imm = 0x8000
cmpq $0xfff, %rcx            # imm = 0xFFF
ja 0x84dba
xorb %al, 0x2e7d51(%rip)     # 0x36cab4
leaq 0x1(%rcx), %rdx
movw %dx, (%rsi)
leaq 0x2b24df(%rip), %rsi    # 0x337250
movb %al, (%rsi,%rcx)
movb $0x0, 0x1(%rsi,%rcx)
cmpw $0xa, %dx
jne 0x84e03
movb $0x0, 0x2ed0c6(%rip)    # 0x371e50
leaq 0x2b24bf(%rip), %rdi    # 0x337250
leaq 0x9e179(%rip), %rsi     # 0x122f11
callq 0x9090
testq %rax, %rax
je 0x84e29
movl $0x38, 0x2ed080(%rip)   # 0x371e30
movl $0x38, %eax
jmp 0x84f0c
leaq 0x1ba1af(%rip), %rax    # 0x23ef70
orl %edx, (%rax)
leaq 0x2b3488(%rip), %rax    # 0x338252
xorl %ecx, %ecx
movw %cx, (%rax)
leaq 0x2b3480(%rip), %rax    # 0x338256
movw %cx, (%rax)
movl %ecx, 0x2ed051(%rip)    # 0x371e30
movl %ecx, 0x2ed053(%rip)    # 0x371e38
movl %ecx, 0x2ed051(%rip)    # 0x371e3c
movl %ecx, 0x2ed057(%rip)    # 0x371e48
movb %cl, 0x2e7cbe(%rip)     # 0x36cab5
movb %cl, 0x2e7cb7(%rip)     # 0x36cab4
movl %ecx, 0x2e7ce1(%rip)    # 0x36cae4
popq %rbx
popq %r14
popq %rbp
retq
leal -0x41(%rax), %ecx
cmpl $0x5, %ecx
ja 0x84dc3
movzbl 0x2e7c9e(%rip), %ecx    # 0x36cab5
shll $0x4, %ecx
addl $0xc9, %eax
orl %ecx, %eax
movb %al, 0x2e7c8e(%rip)     # 0x36cab5
jmp 0x84e03
leaq 0x2b2420(%rip), %rdi    # 0x337250
leaq 0x9e0df(%rip), %rsi     # 0x122f16
callq 0x9090
testq %rax, %rax
je 0x84e55
movl $0x46, 0x2ecfe5(%rip)   # 0x371e30
movl $0x46, %eax
jmp 0x84f0c
leaq 0x2b23f4(%rip), %rdi    # 0x337250
leaq 0x9e0b8(%rip), %rsi     # 0x122f1b
callq 0x9090
testq %rax, %rax
je 0x84e81
movl $0x84, 0x2ecfb9(%rip)   # 0x371e30
movl $0x84, %eax
jmp 0x84f0c
leaq 0x2b23c8(%rip), %rdi    # 0x337250
leaq 0x9e091(%rip), %rsi     # 0x122f20
callq 0x9090
testq %rax, %rax
je 0x84eaa
movl $0x7a, 0x2ecf8d(%rip)   # 0x371e30
movl $0x7a, %eax
jmp 0x84f0c
leaq 0x2b239f(%rip), %rdi    # 0x337250
leaq 0x9e06d(%rip), %rsi     # 0x122f25
callq 0x9090
testq %rax, %rax
je 0x84ed3
movl $0xfffffffe, 0x2ecf64(%rip) # imm = 0xFFFFFFFE
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
jmp 0x84f0c
leaq 0x2b2376(%rip), %rdi    # 0x337250
leaq 0x9e049(%rip), %rsi     # 0x122f2a
callq 0x9090
testq %rax, %rax
je 0x84f0a
movl $0x5f, 0x2ecf3b(%rip)   # 0x371e30
movb $0x0, 0x2e7bb8(%rip)    # 0x36cab4
movb $0x1, 0x2ecf4d(%rip)    # 0x371e50
movl $0x5f, %eax
jmp 0x84f0c
xorl %eax, %eax
movl %eax, 0x2ecf22(%rip)    # 0x371e34
jmp 0x84e03