// File: /workspace/github_cpp_makefile_star2_O2/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
pushq $0xa
popq %rdi
callq 0xb771
pushq $0xa
popq %rdi
testl %eax, %eax
je 0x78d59
callq 0x1d93f
callq 0x51891
leaq 0x27f5c5(%rip), %rcx    # 0x2f82e0
cmpb $0x0, 0x17a12(%rcx)
je 0x78d2b
movb $0x0, 0x17a12(%rcx)
movl 0x2e30ff(%rip), %ecx    # 0x35be30
testl %ecx, %ecx
je 0x78db9
cmpl $-0x1, %ecx
je 0x78d7c
cmpl $-0x2, %ecx
jne 0x78e34
movl %eax, 0x2e30ff(%rip)    # 0x35be4c
orl $-0x1, 0x2e30dc(%rip)   # 0x35be30
pushq $-0x1
popq %rcx
jmp 0x78d94
callq 0x1d61b
cmpl $0x0, 0x2e30c7(%rip)    # 0x35be2c
jne 0x791a3
callq 0x76a3e
pushq $0xa
popq %rdi
popq %rbx
popq %r14
popq %rbp
jmp 0x1d93f
shll $0x8, %eax
orl 0x2e30c7(%rip), %eax    # 0x35be4c
movl %eax, 0x2e30a5(%rip)    # 0x35be30
andl $0x0, 0x2e30aa(%rip)    # 0x35be3c
movl %eax, %ecx
movl 0x2e309e(%rip), %eax    # 0x35be38
incl %eax
movl %eax, 0x2e3096(%rip)    # 0x35be38
leal -0x1(%rcx), %edx
movl %edx, 0x2e3085(%rip)    # 0x35be30
cmpl $0x2, %ecx
jl 0x78f27
jmp 0x791a3
leaq 0x2a9496(%rip), %rbx    # 0x322256
movzwl (%rbx), %ecx
cmpq $0x3, %rcx
ja 0x7915c
leaq 0x902e0(%rip), %rdx     # 0x1090b4
movslq (%rdx,%rcx,4), %rsi
addq %rdx, %rsi
movzbl %al, %edx
jmpq *%rsi
cmpl $0x24, %eax
jne 0x78def
movw $0x1, (%rbx)
callq 0x51a2f
leaq 0x2a945c(%rip), %rax    # 0x322252
andw $0x0, (%rax)
leaq 0x2a9453(%rip), %rax    # 0x322254
andw $0x0, (%rax)
andl $0x0, 0x2e3024(%rip)    # 0x35be30
andl $0x0, 0x2e3025(%rip)    # 0x35be38
andl $0x0, 0x2e3022(%rip)    # 0x35be3c
andl $0x0, 0x2e3027(%rip)    # 0x35be48
xorl %eax, %eax
movb %al, 0x2ddc8b(%rip)     # 0x356ab4
movb %al, 0x2ddc86(%rip)     # 0x356ab5
jmp 0x791a3
leaq 0x2a9417(%rip), %rdi    # 0x322252
movzwl (%rdi), %esi
movl $0x8000, %edx           # imm = 0x8000
cmpq $0xfff, %rsi            # imm = 0xFFF
ja 0x79153
leal 0x1(%rsi), %edx
movw %dx, (%rdi)
leaq 0x2a83f3(%rip), %rdx    # 0x321250
movb %al, (%rdx,%rsi)
xorb %al, 0x2ddc4e(%rip)     # 0x356ab4
testb $0x1, %cl
jne 0x78e82
movl %eax, 0x2e2fd3(%rip)    # 0x35be44
cmpl $0x1, %ecx
jne 0x78d94
movl 0x2e2fc0(%rip), %ecx    # 0x35be40
jmp 0x78ea2
movl %eax, 0x2e2fb8(%rip)    # 0x35be40
cmpl $0x2, %ecx
jg 0x79061
cmpl $0x1, %ecx
jne 0x78d94
movl %eax, %ecx
movl 0x2e2fa2(%rip), %eax    # 0x35be44
shll $0x8, %eax
addl %ecx, %eax
movl %eax, 0x2e2f9b(%rip)    # 0x35be48
pushq $0x1
jmp 0x78d56
cmpb $0xa, %al
je 0x78edd
cmpl $0xd, %edx
je 0x78edd
cmpl $0x2a, %edx
jne 0x790e1
movw $0x2, (%rbx)
jmp 0x791a3
cmpb $0xa, %al
je 0x78edd
cmpl $0xd, %edx
jne 0x790c1
leaq 0x1af4ac(%rip), %rax    # 0x228390
movq (%rax), %rdi
testq %rdi, %rdi
je 0x78f04
leaq 0x93f84(%rip), %rsi     # 0x10ce77
leaq 0x2a8356(%rip), %rdx    # 0x321250
xorl %eax, %eax
callq 0x9560
movzwl (%rbx), %ecx
cmpw $0x2, %cx
jne 0x78f21
movb 0x2ddba4(%rip), %al     # 0x356ab4
movl $0x4000, %edx           # imm = 0x4000
cmpb 0x2ddb9a(%rip), %al     # 0x356ab5
jne 0x79153
movl 0x2e2f11(%rip), %eax    # 0x35be38
testl %eax, %eax
je 0x78f57
movb 0x2ddb83(%rip), %al     # 0x356ab4
testb %al, %al
sete %cl
andb 0x2e2f14(%rip), %cl     # 0x35be50
cmpb $0x1, %cl
je 0x78f57
movl 0x2e2f01(%rip), %ecx    # 0x35be48
movl 0x2e2eee(%rip), %r8d    # 0x35be3c
cmpl %r8d, %ecx
jne 0x79019
callq 0x792a9
leaq 0x1af42d(%rip), %rbx    # 0x228390
movq (%rbx), %rdi
movl $0x1, 0x2dcd24(%rip)    # 0x355c94
testq %rdi, %rdi
je 0x7908a
cmpl $0x0, 0x2e2eb8(%rip)    # 0x35be38
je 0x78fb1
leaq 0x2a82c7(%rip), %rdx    # 0x321250
movb $0x0, 0xa(%rdx)
movl 0x2e2ea1(%rip), %ecx    # 0x35be34
movl 0x2e2ea2(%rip), %r8d    # 0x35be3c
movl 0x2e2ea7(%rip), %r9d    # 0x35be48
leaq 0x93f16(%rip), %rsi     # 0x10cebe
xorl %eax, %eax
callq 0x9560
jmp 0x78fcc
movl 0x2e2e7d(%rip), %ecx    # 0x35be34
leaq 0x93f3f(%rip), %rsi     # 0x10cefd
leaq 0x2a828b(%rip), %rdx    # 0x321250
xorl %eax, %eax
callq 0x9560
leaq 0x2a827d(%rip), %rax    # 0x321250
movzbl (%rax), %eax
cmpl $0x47, %eax
je 0x7908a
cmpl $0x50, %eax
je 0x7908a
movq (%rbx), %rdi
movl 0x2e2e47(%rip), %edx    # 0x35be38
movl 0x2e2e3d(%rip), %ecx    # 0x35be34
leaq 0x2a9254(%rip), %rax    # 0x322252
movzwl (%rax), %r8d
movl 0x2e2e27(%rip), %r9d    # 0x35be30
leaq 0x93f0a(%rip), %rsi     # 0x10cf1a
xorl %eax, %eax
callq 0x9560
jmp 0x7908a
leaq 0x1af370(%rip), %r14    # 0x228390
cmpq $0x0, (%r14)
je 0x79080
movzbl %al, %edx
leaq 0x2ad1f0(%rip), %rbx    # 0x326220
leaq 0x93e52(%rip), %rsi     # 0x10ce89
movq %rbx, %rdi
xorl %eax, %eax
callq 0x9060
movq (%r14), %rdi
leaq 0x969d4(%rip), %rsi     # 0x10fa1f
movq %rbx, %rdx
xorl %eax, %eax
callq 0x9560
andl $0x0, 0x2e2dd0(%rip)    # 0x35be2c
jmp 0x78f5c
movl 0x2e2ddd(%rip), %edx    # 0x35be44
shll $0x8, %edx
orl %eax, %edx
addl 0x2e2dca(%rip), %edx    # 0x35be3c
movzwl %dx, %eax
movl %eax, 0x2e2dc1(%rip)    # 0x35be3c
jmp 0x78d94
movl $0x1, 0x2dcc0a(%rip)    # 0x355c94
leaq 0x2a91c1(%rip), %rax    # 0x322252
andw $0x0, (%rax)
leaq 0x2a91ba(%rip), %rax    # 0x322256
andw $0x0, (%rax)
andl $0x0, 0x2e2d89(%rip)    # 0x35be30
andl $0x0, 0x2e2d8a(%rip)    # 0x35be38
andl $0x0, 0x2e2d87(%rip)    # 0x35be3c
andl $0x0, 0x2e2d8c(%rip)    # 0x35be48
jmp 0x7919c
leal -0x30(%rax), %ecx
cmpl $0x9, %ecx
ja 0x791a8
movzbl 0x2dd9e1(%rip), %ecx    # 0x356ab5
shll $0x4, %ecx
addl $0xd0, %eax
jmp 0x791bf
leaq 0x2a916a(%rip), %rsi    # 0x322252
movzwl (%rsi), %ecx
movl $0x8000, %edx           # imm = 0x8000
cmpq $0xfff, %rcx            # imm = 0xFFF
ja 0x79153
xorb %al, 0x2dd9b5(%rip)     # 0x356ab4
leaq 0x1(%rcx), %rdx
movw %dx, (%rsi)
leaq 0x2a8143(%rip), %rsi    # 0x321250
movb %al, (%rsi,%rcx)
movb $0x0, 0x1(%rsi,%rcx)
cmpw $0xa, %dx
jne 0x791a3
movb $0x0, 0x2e2d2a(%rip)    # 0x35be50
leaq 0x2a8123(%rip), %rdi    # 0x321250
leaq 0x93d25(%rip), %rsi     # 0x10ce59
callq 0x9090
testq %rax, %rax
je 0x791c9
movl $0x38, 0x2e2ce4(%rip)   # 0x35be30
pushq $0x38
jmp 0x791ed
leaq 0x1afe16(%rip), %rax    # 0x228f70
orl %edx, (%rax)
leaq 0x2a90ef(%rip), %rax    # 0x322252
andw $0x0, (%rax)
leaq 0x2a90e8(%rip), %rax    # 0x322256
andw $0x0, (%rax)
andl $0x0, 0x2e2cb7(%rip)    # 0x35be30
andl $0x0, 0x2e2cb8(%rip)    # 0x35be38
andl $0x0, 0x2e2cb5(%rip)    # 0x35be3c
andl $0x0, 0x2e2cba(%rip)    # 0x35be48
xorl %eax, %eax
movb %al, 0x2dd91f(%rip)     # 0x356ab5
movb %al, 0x2dd918(%rip)     # 0x356ab4
andl $0x0, 0x2dd941(%rip)    # 0x356ae4
popq %rbx
popq %r14
popq %rbp
retq
leal -0x41(%rax), %ecx
cmpl $0x5, %ecx
ja 0x7915c
movzbl 0x2dd8fe(%rip), %ecx    # 0x356ab5
shll $0x4, %ecx
addl $0xc9, %eax
orl %ecx, %eax
movb %al, 0x2dd8ee(%rip)     # 0x356ab5
jmp 0x791a3
leaq 0x2a8080(%rip), %rdi    # 0x321250
leaq 0x93c87(%rip), %rsi     # 0x10ce5e
callq 0x9090
testq %rax, %rax
je 0x791f6
movl $0x46, 0x2e2c45(%rip)   # 0x35be30
pushq $0x46
popq %rax
movl %eax, 0x2e2c40(%rip)    # 0x35be34
jmp 0x791a3
leaq 0x2a8053(%rip), %rdi    # 0x321250
leaq 0x93c5f(%rip), %rsi     # 0x10ce63
callq 0x9090
testq %rax, %rax
je 0x7921f
movl $0x84, 0x2e2c18(%rip)   # 0x35be30
movl $0x84, %eax
jmp 0x791ee
leaq 0x2a802a(%rip), %rdi    # 0x321250
leaq 0x93c3b(%rip), %rsi     # 0x10ce68
callq 0x9090
testq %rax, %rax
je 0x79245
movl $0x7a, 0x2e2bef(%rip)   # 0x35be30
pushq $0x7a
jmp 0x791ed
leaq 0x2a8004(%rip), %rdi    # 0x321250
leaq 0x93c1a(%rip), %rsi     # 0x10ce6d
callq 0x9090
testq %rax, %rax
je 0x7926b
movl $0xfffffffe, 0x2e2bc9(%rip) # imm = 0xFFFFFFFE
pushq $-0x2
jmp 0x791ed
leaq 0x2a7fde(%rip), %rdi    # 0x321250
leaq 0x93bf9(%rip), %rsi     # 0x10ce72
callq 0x9090
testq %rax, %rax
je 0x792a2
movl $0x5f, 0x2e2ba3(%rip)   # 0x35be30
movb $0x0, 0x2dd820(%rip)    # 0x356ab4
movb $0x1, 0x2e2bb5(%rip)    # 0x35be50
pushq $0x5f
jmp 0x791ed
xorl %eax, %eax
jmp 0x791ee