// File: /workspace/github_cpp_makefile_star2_O2/jkingan[P]ladyheather/heathgps.cpp
// Function: get_moto_message()
void get_moto_message()
{
u08 c;
static int cmd1 = 0;
static int cmd2 = 0;
static int eq_cksum = 0; // ascii Eq message checksum

   // this routine buffers up an incoming Motorola binary message

   if(SERIAL_DATA_AVAILABLE(RCVR_PORT) == 0) {
      check_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(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;
      moto_msg_len = moto_msg_ptr = 0;
      moto_vfy_cksum = 0;
      return;
   }
   else if(tsip_sync == 1) {    // @ had been seen, now check next byte for '@'
      if(c != '@') goto rst_msg;
      ++tsip_sync;
   }
   else if(tsip_sync == 2) {    // get first byte of command
      if((c >= 'A') && (c <= 'Z')) {
         cmd1 = c;
         moto_vfy_cksum ^= c;
         ++tsip_sync;
      }
      else goto rst_msg;
   }
   else if(tsip_sync == 3) {   // next byte of command
      if((tolower(c) >= 'a') && (tolower(c) <= 'z')) {
         cmd2 = c;
         moto_vfy_cksum ^= c;
         moto_msg_len = moto_msg_ptr = find_moto_cmd(cmd1, cmd2);
         if(moto_msg_len < 0) goto rst_msg;       // unknown command
         else if(moto_msg_len == 0) ++tsip_sync;  // no payload bytes
      }
      else goto rst_msg;
      ++tsip_sync;
   }
   else if(tsip_sync == 4) {   // buffer up message bytes
      if(tsip_wptr < MAX_TSIP) {   // add char to the message buffer
         moto_vfy_cksum ^= c;
         tsip_buf[tsip_wptr++] = c;
      }
      else {
         tsip_error |= OVFL_ERROR;
         goto rst_msg;
      }
      if(--moto_msg_ptr <= 0) ++tsip_sync;  // end of message payload
   }
   else if(tsip_sync == 5) {   // get checksum and verify, parse payload
      if((cmd1 == 'E') && (cmd2 == 'q')) {  // BASTARD ASCII message format
         eq_cksum = (c - '0');
         ++tsip_sync;
      }
      else {
         if(c != moto_vfy_cksum) goto rst_msg;
         tsip_sync = 8;  // get 0x0D 0x0A
         if(0) {
            decode_moto_msg(cmd1, cmd2);
            packet_end = 1;
            tsip_sync = 0;
         }
      }
   }
   else if(tsip_sync == 6) {  // second byte of ascii checksum - ascii posn msg
      eq_cksum = (eq_cksum*10) + (c - '0');
      ++tsip_sync;
   }
   else if(tsip_sync == 7) {  // final byte of ascii checksum - ascii posn msg
      eq_cksum = (eq_cksum*10) + (c - '0');
//sprintf(plot_title, "c:%c eq:%d vfy:%d", c, eq_cksum, moto_vfy_cksum);
      if(eq_cksum != moto_vfy_cksum) goto rst_msg;
      tsip_sync = 8;  // get 0x0A 0x0A
      if(0) {
         decode_moto_msg(cmd1, cmd2);
         packet_end = 1;
         tsip_sync = 0;
      }
   }
   else if(tsip_sync == 8) {  // 0x0D
      tsip_sync = 9;
   }
   else if(tsip_sync == 9) {  // 0x0A
      decode_moto_msg(cmd1, cmd2);
      packet_end = 1;
      tsip_sync = 0;
   }
   else {
      rst_msg:
      tsip_wptr = 0;
      tsip_sync = 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 0x8e839
callq 0x1d93f
callq 0x51891
movl %eax, %ebx
leaq 0x269b1c(%rip), %rax    # 0x2f82e0
cmpb $0x0, 0x17a12(%rax)
je 0x8e7d4
movb $0x0, 0x17a12(%rax)
leaq 0x293a7b(%rip), %r14    # 0x322256
movzwl (%r14), %eax
cmpq $0x9, %rax
ja 0x8e9b9
leaq 0x7b068(%rip), %rcx     # 0x109858
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpl $0x40, %ebx
jne 0x8e809
movw $0x1, (%r14)
callq 0x51a2f
leaq 0x293a42(%rip), %rax    # 0x322252
andw $0x0, (%rax)
leaq 0x293a39(%rip), %rax    # 0x322254
andw $0x0, (%rax)
andl $0x0, 0x2c829e(%rip)    # 0x356ac4
andl $0x0, 0x2c829b(%rip)    # 0x356ac8
movb $0x0, 0x2c828d(%rip)    # 0x356ac1
jmp 0x8e9c9
popq %rbx
popq %r14
popq %rbp
jmp 0x1d61b
leaq 0x293a09(%rip), %rcx    # 0x322252
movzwl (%rcx), %eax
cmpq $0xfff, %rax            # imm = 0xFFF
ja 0x8e999
xorb %bl, 0x2c8263(%rip)     # 0x356ac1
leal 0x1(%rax), %edx
movw %dx, (%rcx)
leaq 0x2929e5(%rip), %rcx    # 0x321250
movb %bl, (%rcx,%rax)
movl 0x2c8250(%rip), %eax    # 0x356ac4
leal -0x1(%rax), %ecx
movl %ecx, 0x2c8247(%rip)    # 0x356ac4
cmpl $0x2, %eax
jge 0x8e9c9
movw $0x5, (%r14)
jmp 0x8e9c9
movl 0x2cd629(%rip), %edi    # 0x35bec0
movl 0x2cd627(%rip), %esi    # 0x35bec4
callq 0x8e0fc
movl $0x1, 0x2c73e8(%rip)    # 0x355c94
jmp 0x8e9c4
movl %ebx, %eax
addl $-0x41, %eax
cmpl $0x19, %eax
ja 0x8e9b9
movl %ebx, 0x2cd5fb(%rip)    # 0x35bec0
xorb %bl, 0x2c81f6(%rip)     # 0x356ac1
movw $0x3, (%r14)
jmp 0x8e9c9
movl %ebx, %edi
callq 0x9580
addl $-0x61, %eax
cmpl $0x19, %eax
ja 0x8e9b9
movl %ebx, 0x2cd5d5(%rip)    # 0x35bec4
xorb %bl, 0x2c81cc(%rip)     # 0x356ac1
movl 0x2cd5c5(%rip), %edi    # 0x35bec0
movl %ebx, %esi
callq 0x8db6e
movl %eax, 0x2c81bc(%rip)    # 0x356ac4
movl %eax, 0x2c81ba(%rip)    # 0x356ac8
testl %eax, %eax
js 0x8e9b9
xorl %ecx, %ecx
cmpl $0x1, %eax
adcw $0x4, %cx
movw %cx, (%r14)
jmp 0x8e9c9
imull $0xa, 0x2cd599(%rip), %eax # 0x35bec8
addl %ebx, %eax
addl $-0x30, %eax
movl %eax, 0x2cd58e(%rip)    # 0x35bec8
movzbl 0x2c8180(%rip), %ecx    # 0x356ac1
cmpl %ecx, %eax
je 0x8e9b1
jmp 0x8e9b9
cmpl $0x40, %ebx
jne 0x8e9b9
movw $0x2, (%r14)
jmp 0x8e9c9
cmpl $0x45, 0x2cd565(%rip)   # 0x35bec0
jne 0x8e9a6
cmpl $0x71, 0x2cd560(%rip)   # 0x35bec4
jne 0x8e9a6
addl $-0x30, %ebx
movl %ebx, 0x2cd559(%rip)    # 0x35bec8
movw $0x6, (%r14)
jmp 0x8e9c9
imull $0xa, 0x2cd54a(%rip), %eax # 0x35bec8
addl %ebx, %eax
addl $-0x30, %eax
movl %eax, 0x2cd53f(%rip)    # 0x35bec8
movw $0x7, (%r14)
jmp 0x8e9c9
movw $0x9, (%r14)
jmp 0x8e9c9
leaq 0x19a5d0(%rip), %rax    # 0x228f70
orb $-0x80, 0x1(%rax)
jmp 0x8e9b9
movzbl 0x2c8114(%rip), %eax    # 0x356ac1
cmpl %eax, %ebx
jne 0x8e9b9
movw $0x8, (%r14)
jmp 0x8e9c9
leaq 0x293892(%rip), %rax    # 0x322252
andw $0x0, (%rax)
andw $0x0, (%r14)
popq %rbx
popq %r14
popq %rbp
retq