// File: 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
subq $0x10, %rsp
movl $0xa, %edi
callq 0xcad0
cmpl $0x0, %eax
jne 0xd5966
movl $0xa, %edi
callq 0x2b610
jmp 0xd5d7b
movl $0xa, %edi
callq 0x2bd30
callq 0x79fc0
movb %al, -0x1(%rbp)
leaq 0x297b41(%rip), %rax    # 0x36d4c0
cmpb $0x0, 0x17a12(%rax)
je 0xd5996
leaq 0x297b31(%rip), %rax    # 0x36d4c0
movb $0x0, 0x17a12(%rax)
leaq 0x2c1a99(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x0, %eax
jne 0xd59f7
movzbl -0x1(%rbp), %eax
cmpl $0x40, %eax
jne 0xd59bf
leaq 0x2c1a81(%rip), %rax    # 0x397436
movw $0x1, (%rax)
callq 0x7a1d0
leaq 0x2c1a6c(%rip), %rax    # 0x397432
movw $0x0, (%rax)
leaq 0x2c1a62(%rip), %rax    # 0x397434
movw $0x0, (%rax)
movl $0x0, 0x2f62c3(%rip)    # 0x3cbca4
movl $0x0, 0x2f62bd(%rip)    # 0x3cbca8
movb $0x0, 0x2f62af(%rip)    # 0x3cbca1
jmp 0xd5d7b
leaq 0x2c1a38(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x1, %eax
jne 0xd5a31
movzbl -0x1(%rbp), %eax
cmpl $0x40, %eax
je 0xd5a14
jmp 0xd5d51
leaq 0x2c1a1b(%rip), %rax    # 0x397436
movw (%rax), %cx
addw $0x1, %cx
leaq 0x2c1a0d(%rip), %rax    # 0x397436
movw %cx, (%rax)
jmp 0xd5d79
leaq 0x2c19fe(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x2, %eax
jne 0xd5a93
movzbl -0x1(%rbp), %eax
cmpl $0x41, %eax
jl 0xd5a89
movzbl -0x1(%rbp), %eax
cmpl $0x5a, %eax
jg 0xd5a89
movzbl -0x1(%rbp), %eax
movl %eax, 0x2fb658(%rip)    # 0x3d10b4
movzbl -0x1(%rbp), %ecx
movzbl 0x2f623a(%rip), %eax    # 0x3cbca1
xorl %ecx, %eax
movb %al, 0x2f6232(%rip)     # 0x3cbca1
leaq 0x2c19c0(%rip), %rax    # 0x397436
movw (%rax), %cx
addw $0x1, %cx
leaq 0x2c19b2(%rip), %rax    # 0x397436
movw %cx, (%rax)
jmp 0xd5a8e
jmp 0xd5d51
jmp 0xd5d77
leaq 0x2c199c(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x3, %eax
jne 0xd5b51
movzbl -0x1(%rbp), %edi
callq 0x9570
cmpl $0x61, %eax
jl 0xd5b2f
movzbl -0x1(%rbp), %edi
callq 0x9570
cmpl $0x7a, %eax
jg 0xd5b2f
movzbl -0x1(%rbp), %eax
movl %eax, 0x2fb5ec(%rip)    # 0x3d10b8
movzbl -0x1(%rbp), %ecx
movzbl 0x2f61ca(%rip), %eax    # 0x3cbca1
xorl %ecx, %eax
movb %al, 0x2f61c2(%rip)     # 0x3cbca1
movl 0x2fb5cf(%rip), %edi    # 0x3d10b4
movl 0x2fb5cd(%rip), %esi    # 0x3d10b8
callq 0xd4770
movl %eax, 0x2f61ae(%rip)    # 0x3cbca4
movl %eax, 0x2f61ac(%rip)    # 0x3cbca8
cmpl $0x0, 0x2f61a5(%rip)    # 0x3cbca8
jge 0xd5b0a
jmp 0xd5d51
cmpl $0x0, 0x2f6197(%rip)    # 0x3cbca8
jne 0xd5b2b
leaq 0x2c191c(%rip), %rax    # 0x397436
movw (%rax), %cx
addw $0x1, %cx
leaq 0x2c190e(%rip), %rax    # 0x397436
movw %cx, (%rax)
jmp 0xd5b2d
jmp 0xd5b34
jmp 0xd5d51
leaq 0x2c18fb(%rip), %rax    # 0x397436
movw (%rax), %cx
addw $0x1, %cx
leaq 0x2c18ed(%rip), %rax    # 0x397436
movw %cx, (%rax)
jmp 0xd5d75
leaq 0x2c18de(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x4, %eax
jne 0xd5c05
leaq 0x2c18c7(%rip), %rax    # 0x397432
movzwl (%rax), %eax
cmpl $0x1000, %eax           # imm = 0x1000
jge 0xd5bb7
movzbl -0x1(%rbp), %ecx
movzbl 0x2f6121(%rip), %eax    # 0x3cbca1
xorl %ecx, %eax
movb %al, 0x2f6119(%rip)     # 0x3cbca1
movb -0x1(%rbp), %dl
leaq 0x2c18a0(%rip), %rax    # 0x397432
movw (%rax), %ax
movw %ax, %si
addw $0x1, %si
leaq 0x2c188f(%rip), %rcx    # 0x397432
movw %si, (%rcx)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0x2c087e(%rip), %rax    # 0x396430
movb %dl, (%rax,%rcx)
jmp 0xd5bd4
leaq 0x1c8592(%rip), %rax    # 0x29e150
movl (%rax), %ecx
orl $0x8000, %ecx           # imm = 0x8000
leaq 0x1c8583(%rip), %rax    # 0x29e150
movl %ecx, (%rax)
jmp 0xd5d51
movl 0x2f60ca(%rip), %eax    # 0x3cbca4
addl $-0x1, %eax
movl %eax, 0x2f60c1(%rip)    # 0x3cbca4
cmpl $0x0, %eax
jg 0xd5c00
leaq 0x2c1847(%rip), %rax    # 0x397436
movw (%rax), %cx
addw $0x1, %cx
leaq 0x2c1839(%rip), %rax    # 0x397436
movw %cx, (%rax)
jmp 0xd5d73
leaq 0x2c182a(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x5, %eax
jne 0xd5c72
cmpl $0x45, 0x2fb499(%rip)   # 0x3d10b4
jne 0xd5c4d
cmpl $0x71, 0x2fb494(%rip)   # 0x3d10b8
jne 0xd5c4d
movzbl -0x1(%rbp), %eax
subl $0x30, %eax
movl %eax, 0x2fb489(%rip)    # 0x3d10bc
leaq 0x2c17fc(%rip), %rax    # 0x397436
movw (%rax), %cx
addw $0x1, %cx
leaq 0x2c17ee(%rip), %rax    # 0x397436
movw %cx, (%rax)
jmp 0xd5c6d
movzbl -0x1(%rbp), %eax
movzbl 0x2f6049(%rip), %ecx    # 0x3cbca1
cmpl %ecx, %eax
je 0xd5c61
jmp 0xd5d51
leaq 0x2c17ce(%rip), %rax    # 0x397436
movw $0x8, (%rax)
jmp 0xd5d71
leaq 0x2c17bd(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x6, %eax
jne 0xd5cb4
imull $0xa, 0x2fb434(%rip), %eax # 0x3d10bc
movzbl -0x1(%rbp), %ecx
subl $0x30, %ecx
addl %ecx, %eax
movl %eax, 0x2fb425(%rip)    # 0x3d10bc
leaq 0x2c1798(%rip), %rax    # 0x397436
movw (%rax), %cx
addw $0x1, %cx
leaq 0x2c178a(%rip), %rax    # 0x397436
movw %cx, (%rax)
jmp 0xd5d6f
leaq 0x2c177b(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x7, %eax
jne 0xd5cfa
imull $0xa, 0x2fb3f2(%rip), %eax # 0x3d10bc
movzbl -0x1(%rbp), %ecx
subl $0x30, %ecx
addl %ecx, %eax
movl %eax, 0x2fb3e3(%rip)    # 0x3d10bc
movl 0x2fb3dd(%rip), %eax    # 0x3d10bc
movzbl 0x2f5fbb(%rip), %ecx    # 0x3cbca1
cmpl %ecx, %eax
je 0xd5cec
jmp 0xd5d51
leaq 0x2c1743(%rip), %rax    # 0x397436
movw $0x8, (%rax)
jmp 0xd5d6d
leaq 0x2c1735(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x8, %eax
jne 0xd5d17
leaq 0x2c1726(%rip), %rax    # 0x397436
movw $0x9, (%rax)
jmp 0xd5d6b
leaq 0x2c1718(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x9, %eax
jne 0xd5d4f
movl 0x2fb388(%rip), %edi    # 0x3d10b4
movl 0x2fb386(%rip), %esi    # 0x3d10b8
callq 0xd5040
movl $0x1, 0x2f5133(%rip)    # 0x3cae74
leaq 0x2c16ee(%rip), %rax    # 0x397436
movw $0x0, (%rax)
jmp 0xd5d69
jmp 0xd5d51
leaq 0x2c16da(%rip), %rax    # 0x397432
movw $0x0, (%rax)
leaq 0x2c16d2(%rip), %rax    # 0x397436
movw $0x0, (%rax)
jmp 0xd5d6b
jmp 0xd5d6d
jmp 0xd5d6f
jmp 0xd5d71
jmp 0xd5d73
jmp 0xd5d75
jmp 0xd5d77
jmp 0xd5d79
jmp 0xd5d7b
addq $0x10, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)