// File: /workspace/github_cpp_makefile_star2_O1/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 0xb9c3
movl $0xa, %edi
testl %eax, %eax
je 0x84a89
callq 0x1ea2c
callq 0x584d5
leaq 0x289893(%rip), %rcx    # 0x30e2e0
cmpb $0x0, 0x17a12(%rcx)
je 0x84a5d
movb $0x0, 0x17a12(%rcx)
movl 0x2ed3cd(%rip), %ecx    # 0x371e30
testl %ecx, %ecx
je 0x84b7b
cmpl $-0x1, %ecx
je 0x84aae
cmpl $-0x2, %ecx
jne 0x84bed
movl %eax, 0x2ed3cd(%rip)    # 0x371e4c
incl %ecx
movl %ecx, 0x2ed3a9(%rip)    # 0x371e30
jmp 0x84ac7
callq 0x1e6a0
cmpl $0x0, 0x2ed397(%rip)    # 0x371e2c
jne 0x84e27
callq 0x82578
movl $0xa, %edi
popq %rbx
popq %r14
popq %rbp
jmp 0x1ea2c
shll $0x8, %eax
orl 0x2ed395(%rip), %eax    # 0x371e4c
movl %eax, 0x2ed373(%rip)    # 0x371e30
movl $0x0, 0x2ed375(%rip)    # 0x371e3c
incl 0x2ed36b(%rip)          # 0x371e38
movl 0x2ed35d(%rip), %eax    # 0x371e30
leal -0x1(%rax), %ecx
movl %ecx, 0x2ed354(%rip)    # 0x371e30
cmpl $0x2, %eax
jge 0x84e27
cmpl $0x0, 0x2ed34c(%rip)    # 0x371e38
je 0x84b1a
movb 0x2e7fc0(%rip), %al     # 0x36cab4
testb %al, %al
sete %cl
andb 0x2ed351(%rip), %cl     # 0x371e50
cmpb $0x1, %cl
je 0x84b1a
movl 0x2ed33e(%rip), %ecx    # 0x371e48
movl 0x2ed32b(%rip), %r8d    # 0x371e3c
cmpl %r8d, %ecx
jne 0x84d1f
callq 0x84fb4
movl $0x1, 0x2e716b(%rip)    # 0x36bc94
leaq 0x1b9860(%rip), %rbx    # 0x23e390
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x84cc0
cmpl $0x0, 0x2ed2f5(%rip)    # 0x371e38
je 0x84c62
leaq 0x2b2700(%rip), %rdx    # 0x337250
movb $0x0, 0xa(%rdx)
movl 0x2ed2da(%rip), %ecx    # 0x371e34
movl 0x2ed2db(%rip), %r8d    # 0x371e3c
movl 0x2ed2e0(%rip), %r9d    # 0x371e48
leaq 0x9e327(%rip), %rsi     # 0x122e96
xorl %eax, %eax
callq 0x9560
jmp 0x84c7d
leaq 0x2b36d4(%rip), %rbx    # 0x338256
movzwl (%rbx), %ecx
cmpq $0x3, %rcx
ja 0x84de7
leaq 0x9a51a(%rip), %rdx     # 0x11f0b0
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
cmpl $0x24, %eax
jne 0x84bae
movw $0x1, (%rbx)
callq 0x58682
leaq 0x2b369d(%rip), %rax    # 0x338252
xorl %ecx, %ecx
movw %cx, (%rax)
leaq 0x2b3693(%rip), %rax    # 0x338254
movw %cx, (%rax)
movl %ecx, 0x2ed266(%rip)    # 0x371e30
movl %ecx, 0x2ed268(%rip)    # 0x371e38
movl %ecx, 0x2ed266(%rip)    # 0x371e3c
movl %ecx, 0x2ed26c(%rip)    # 0x371e48
movb %cl, 0x2e7ed2(%rip)     # 0x36cab4
movb %cl, 0x2e7ecd(%rip)     # 0x36cab5
jmp 0x84e27
leaq 0x2b365e(%rip), %rdi    # 0x338252
movzwl (%rdi), %esi
movl $0x8000, %edx           # imm = 0x8000
cmpq $0xfff, %rsi            # imm = 0xFFF
ja 0x84dde
leal 0x1(%rsi), %edx
movw %dx, (%rdi)
leaq 0x2b263a(%rip), %rdx    # 0x337250
xorb %al, 0x2e7e98(%rip)     # 0x36cab4
movb %al, (%rdx,%rsi)
testb $0x1, %cl
leaq 0x2ed21b(%rip), %rdx    # 0x371e44
leaq 0x2ed210(%rip), %rsi    # 0x371e40
cmoveq %rdx, %rsi
movl %eax, (%rsi)
cmpl $0x2, %ecx
jg 0x84cf3
cmpl $0x1, %ecx
jne 0x84ac7
movl 0x2ed1f6(%rip), %eax    # 0x371e44
shll $0x8, %eax
addl 0x2ed1e9(%rip), %eax    # 0x371e40
movl %eax, 0x2ed1eb(%rip)    # 0x371e48
jmp 0x84ac7
movl 0x2ed1cc(%rip), %ecx    # 0x371e34
leaq 0x9e266(%rip), %rsi     # 0x122ed5
leaq 0x2b25da(%rip), %rdx    # 0x337250
xorl %eax, %eax
callq 0x9560
leaq 0x2b25cc(%rip), %rax    # 0x337250
movzbl (%rax), %eax
cmpl $0x47, %eax
je 0x84cc0
cmpl $0x50, %eax
je 0x84cc0
movq (%rbx), %rdi
movl 0x2ed19e(%rip), %edx    # 0x371e38
movl 0x2ed194(%rip), %ecx    # 0x371e34
leaq 0x2b35ab(%rip), %rax    # 0x338252
movzwl (%rax), %r8d
movl 0x2ed17e(%rip), %r9d    # 0x371e30
leaq 0x9e239(%rip), %rsi     # 0x122ef2
xorl %eax, %eax
callq 0x9560
leaq 0x2b358b(%rip), %rax    # 0x338252
xorl %ecx, %ecx
movw %cx, (%rax)
leaq 0x2b3583(%rip), %rax    # 0x338256
movw %cx, (%rax)
movl %ecx, 0x2ed154(%rip)    # 0x371e30
movl %ecx, 0x2ed156(%rip)    # 0x371e38
movl %ecx, 0x2ed154(%rip)    # 0x371e3c
movl %ecx, 0x2ed15a(%rip)    # 0x371e48
jmp 0x84e21
testb $0x1, %cl
je 0x84ac7
movl 0x2ed142(%rip), %eax    # 0x371e44
shll $0x8, %eax
addl 0x2ed135(%rip), %eax    # 0x371e40
addl 0x2ed12b(%rip), %eax    # 0x371e3c
movzwl %ax, %eax
movl %eax, 0x2ed122(%rip)    # 0x371e3c
jmp 0x84ac7
leaq 0x1b966a(%rip), %r14    # 0x23e390
cmpq $0x0, (%r14)
je 0x84b1f
movzbl %al, %edx
leaq 0x2b74e6(%rip), %rbx    # 0x33c220
leaq 0x9e120(%rip), %rsi     # 0x122e61
movq %rbx, %rdi
xorl %eax, %eax
callq 0x9060
movq (%r14), %rdi
leaq 0xa0ca2(%rip), %rsi     # 0x1259f7
movq %rbx, %rdx
xorl %eax, %eax
callq 0x9560
movl $0x0, 0x2ed0c3(%rip)    # 0x371e2c
jmp 0x84b1f
cmpl $0xa, %eax
je 0x84d99
cmpl $0xd, %eax
je 0x84d99
cmpl $0x2a, %eax
jne 0x84e4c
movw $0x2, (%rbx)
jmp 0x84e27
cmpl $0xa, %eax
je 0x84d99
cmpl $0xd, %eax
jne 0x84e2c
leaq 0x1b95f0(%rip), %rax    # 0x23e390
movq (%rax), %rdi
testq %rdi, %rdi
je 0x84dbd
leaq 0x9e0a0(%rip), %rsi     # 0x122e4f
leaq 0x2b249a(%rip), %rdx    # 0x337250
xorl %eax, %eax
callq 0x9560
cmpw $0x2, (%rbx)
jne 0x84ae5
movb 0x2e7ce7(%rip), %al     # 0x36cab4
movl $0x4000, %edx           # imm = 0x4000
cmpb 0x2e7cdd(%rip), %al     # 0x36cab5
je 0x84ae5
leaq 0x1ba18b(%rip), %rax    # 0x23ef70
orl %edx, (%rax)
leaq 0x2b3464(%rip), %rax    # 0x338252
xorl %ecx, %ecx
movw %cx, (%rax)
leaq 0x2b345c(%rip), %rax    # 0x338256
movw %cx, (%rax)
movl %ecx, 0x2ed02d(%rip)    # 0x371e30
movl %ecx, 0x2ed02f(%rip)    # 0x371e38
movl %ecx, 0x2ed02d(%rip)    # 0x371e3c
movl %ecx, 0x2ed033(%rip)    # 0x371e48
movb %cl, 0x2e7c9a(%rip)     # 0x36cab5
movb %cl, 0x2e7c93(%rip)     # 0x36cab4
movl %ecx, 0x2e7cbd(%rip)    # 0x36cae4
popq %rbx
popq %r14
popq %rbp
retq
leal -0x30(%rax), %ecx
cmpl $0x9, %ecx
ja 0x84eb8
movzbl 0x2e7c76(%rip), %ecx    # 0x36cab5
shll $0x4, %ecx
addl $0xd0, %eax
jmp 0x84ed3
leaq 0x2b33ff(%rip), %rsi    # 0x338252
movzwl (%rsi), %ecx
movl $0x8000, %edx           # imm = 0x8000
cmpq $0xfff, %rcx            # imm = 0xFFF
ja 0x84dde
xorb %al, 0x2e7c46(%rip)     # 0x36cab4
leaq 0x1(%rcx), %rdx
movw %dx, (%rsi)
leaq 0x2b23d4(%rip), %rsi    # 0x337250
movb %al, (%rsi,%rcx)
movb $0x0, 0x1(%rsi,%rcx)
cmpw $0xa, %dx
jne 0x84e27
movb $0x0, 0x2ecfbf(%rip)    # 0x371e50
leaq 0x2b23b8(%rip), %rdi    # 0x337250
leaq 0x9df92(%rip), %rsi     # 0x122e31
callq 0x9090
testq %rax, %rax
je 0x84ee0
movl $0x38, 0x2ecf7d(%rip)   # 0x371e30
jmp 0x84fa3
leal -0x41(%rax), %ecx
cmpl $0x5, %ecx
ja 0x84de7
movzbl 0x2e7bea(%rip), %ecx    # 0x36cab5
shll $0x4, %ecx
addl $0xc9, %eax
orl %ecx, %eax
movb %al, 0x2e7bda(%rip)     # 0x36cab5
jmp 0x84e27
leaq 0x2b2369(%rip), %rdi    # 0x337250
leaq 0x9df48(%rip), %rsi     # 0x122e36
callq 0x9090
testq %rax, %rax
je 0x84f07
movl $0x46, 0x2ecf2e(%rip)   # 0x371e30
jmp 0x84fa3
leaq 0x2b2342(%rip), %rdi    # 0x337250
leaq 0x9df26(%rip), %rsi     # 0x122e3b
callq 0x9090
testq %rax, %rax
je 0x84f2b
movl $0x84, 0x2ecf07(%rip)   # 0x371e30
jmp 0x84fa3
leaq 0x2b231e(%rip), %rdi    # 0x337250
leaq 0x9df07(%rip), %rsi     # 0x122e40
callq 0x9090
testq %rax, %rax
je 0x84f4f
movl $0x7a, 0x2ecee3(%rip)   # 0x371e30
jmp 0x84fa3
leaq 0x2b22fa(%rip), %rdi    # 0x337250
leaq 0x9dee8(%rip), %rsi     # 0x122e45
callq 0x9090
testq %rax, %rax
je 0x84f73
movl $0xfffffffe, 0x2ecebf(%rip) # imm = 0xFFFFFFFE
jmp 0x84fa3
leaq 0x2b22d6(%rip), %rdi    # 0x337250
leaq 0x9dec9(%rip), %rsi     # 0x122e4a
callq 0x9090
testq %rax, %rax
je 0x84fa3
movl $0x5f, 0x2ece9b(%rip)   # 0x371e30
movb $0x0, 0x2e7b18(%rip)    # 0x36cab4
movb $0x1, 0x2ecead(%rip)    # 0x371e50
movl 0x2ece87(%rip), %eax    # 0x371e30
movl %eax, 0x2ece85(%rip)    # 0x371e34
jmp 0x84e27