// File: jkingan[P]ladyheather/heathgps.cpp
// Function: get_rt17_message()
void get_rt17_message()
{
u08 c;
static int status;
static int msg;
static int len, xlen;

   // This routine buffers up an incoming RT17 message.  When the end of the
   // message is seen, the message is parsed and decoded with decode_rt17_msg()
   // Some RT17 messages are spread out over several packets.  decode_rt17_msg()
   // re-assembles them into one large packet for parsing.

   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 == 0x02) {
         tsip_sync = 1;
         get_sync_time();
      }
      else if(c == 0x06) {  // ack
if(debug_file) fprintf(debug_file, "ack\n");
      }
      else if(c == 0x15) {  // nak
if(debug_file) fprintf(debug_file, "nak\n");
      }
      tsip_wptr = 0;
      tsip_rptr = 0;
      rt17_vfy_cksum = 0;  // the calculated message checksum
      rt17_msg_cksum = 0;  // the checksum in the message
      return;
   } 
   else if(tsip_sync == 1) { // STX has been seen, get status byte
      status = c;
      rt17_vfy_cksum += c;
      tsip_sync = 2;
   }
   else if(tsip_sync == 2) {  // msg type byte
      msg = c;
      rt17_vfy_cksum += c;
      tsip_sync = 3;
   }
   else if(tsip_sync == 3) {  // msg len
      len = xlen = c;
      rt17_vfy_cksum += c;
      tsip_sync = 4;
      if(len == 0) {      // null message
         tsip_sync = 5;
      }
   }
   else if(tsip_sync == 4) {  // message data
      rt17_vfy_cksum += c;
      rt17_buf[tsip_wptr++] = c;
      --len;
      if(len == 0) tsip_sync = 5;
   }
   else if(tsip_sync == 5) {  // checksum
      rt17_msg_cksum = c;
      tsip_sync = 6;
   }
   else if(tsip_sync == 6) {  // end of message
sprintf(out, "msg:%02X:%02X:%02X  status:%02X  len:%02x  etx:%02X  chk:%02X  vfy:%02X", 
msg,rt17_buf[0],rt17_buf[1],  status, xlen, c, rt17_msg_cksum&0xFF, rt17_vfy_cksum&0xFF);
if(debug_file) fprintf(debug_file, "%s\n", out);
      if(c != 0x03) goto rst_msg;  // ETX was expected

      rt17_msg_cksum &= 0xFF;
      rt17_vfy_cksum &= 0xFF;
      if(rt17_vfy_cksum != rt17_msg_cksum) goto rst_msg;  // checksums dont match

      decode_rt17_msg(msg, status);  // message checksum matches, process the message
      packet_end = 1;

      tsip_wptr = 0;     // prepare for next message
      tsip_sync = 0;
   }
   else {     // should never happen, prepare for next message
      rst_msg:
      tsip_wptr = 0;
      tsip_sync = 0;
      rt17_wptr = 0;
      rt17_vfy_cksum = rt17_msg_cksum = 0; // init received message checksum
   }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %rbx
subq $0x28, %rsp
movl $0xa, %edi
callq 0xcad0
cmpl $0x0, %eax
jne 0xb06a7
movl $0xa, %edi
callq 0x2b610
jmp 0xb0a65
movl $0xa, %edi
callq 0x2bd30
callq 0x79fc0
movb %al, -0x9(%rbp)
leaq 0x2bce00(%rip), %rax    # 0x36d4c0
cmpb $0x0, 0x17a12(%rax)
je 0xb06d7
leaq 0x2bcdf0(%rip), %rax    # 0x36d4c0
movb $0x0, 0x17a12(%rax)
leaq 0x2e6d58(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x0, %eax
jne 0xb0795
movzbl -0x9(%rbp), %eax
cmpl $0x2, %eax
jne 0xb0706
leaq 0x2e6d3c(%rip), %rax    # 0x397436
movw $0x1, (%rax)
callq 0x7a1d0
jmp 0xb076a
movzbl -0x9(%rbp), %eax
cmpl $0x6, %eax
jne 0xb0736
leaq 0x1ece5a(%rip), %rax    # 0x29d570
cmpq $0x0, (%rax)
je 0xb0734
leaq 0x1ece4d(%rip), %rax    # 0x29d570
movq (%rax), %rdi
leaq 0xd5981(%rip), %rsi     # 0x1860ae
movb $0x0, %al
callq 0x9550
jmp 0xb0768
movzbl -0x9(%rbp), %eax
cmpl $0x15, %eax
jne 0xb0766
leaq 0x1ece2a(%rip), %rax    # 0x29d570
cmpq $0x0, (%rax)
je 0xb0764
leaq 0x1ece1d(%rip), %rax    # 0x29d570
movq (%rax), %rdi
leaq 0xd5956(%rip), %rsi     # 0x1860b3
movb $0x0, %al
callq 0x9550
jmp 0xb0766
jmp 0xb0768
jmp 0xb076a
leaq 0x2e6cc1(%rip), %rax    # 0x397432
movw $0x0, (%rax)
leaq 0x2e6cb7(%rip), %rax    # 0x397434
movw $0x0, (%rax)
movb $0x0, 0x31b515(%rip)    # 0x3cbc9e
movb $0x0, 0x31b50f(%rip)    # 0x3cbc9f
jmp 0xb0a65
leaq 0x2e6c9a(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x1, %eax
jne 0xb07d2
movzbl -0x9(%rbp), %eax
movl %eax, 0x320836(%rip)    # 0x3d0fe4
movzbl -0x9(%rbp), %ecx
movzbl 0x31b4e5(%rip), %eax    # 0x3cbc9e
addl %ecx, %eax
movb %al, 0x31b4dd(%rip)     # 0x3cbc9e
leaq 0x2e6c6e(%rip), %rax    # 0x397436
movw $0x2, (%rax)
jmp 0xb0a63
leaq 0x2e6c5d(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x2, %eax
jne 0xb080f
movzbl -0x9(%rbp), %eax
movl %eax, 0x3207fd(%rip)    # 0x3d0fe8
movzbl -0x9(%rbp), %ecx
movzbl 0x31b4a8(%rip), %eax    # 0x3cbc9e
addl %ecx, %eax
movb %al, 0x31b4a0(%rip)     # 0x3cbc9e
leaq 0x2e6c31(%rip), %rax    # 0x397436
movw $0x3, (%rax)
jmp 0xb0a61
leaq 0x2e6c20(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x3, %eax
jne 0xb0867
movzbl -0x9(%rbp), %eax
movl %eax, 0x3207c8(%rip)    # 0x3d0ff0
movl %eax, 0x3207be(%rip)    # 0x3d0fec
movzbl -0x9(%rbp), %ecx
movzbl 0x31b465(%rip), %eax    # 0x3cbc9e
addl %ecx, %eax
movb %al, 0x31b45d(%rip)     # 0x3cbc9e
leaq 0x2e6bee(%rip), %rax    # 0x397436
movw $0x4, (%rax)
cmpl $0x0, 0x320798(%rip)    # 0x3d0fec
jne 0xb0862
leaq 0x2e6bd9(%rip), %rax    # 0x397436
movw $0x5, (%rax)
jmp 0xb0a5f
leaq 0x2e6bc8(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x4, %eax
jne 0xb08df
movzbl -0x9(%rbp), %ecx
movzbl 0x31b41d(%rip), %eax    # 0x3cbc9e
addl %ecx, %eax
movb %al, 0x31b415(%rip)     # 0x3cbc9e
movb -0x9(%rbp), %dl
leaq 0x2e6b9f(%rip), %rax    # 0x397432
movw (%rax), %ax
movw %ax, %si
addw $0x1, %si
leaq 0x2e6b8e(%rip), %rcx    # 0x397432
movw %si, (%rcx)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0x31f71d(%rip), %rax    # 0x3cffd0
movb %dl, (%rax,%rcx)
movl 0x320730(%rip), %eax    # 0x3d0fec
addl $-0x1, %eax
movl %eax, 0x320727(%rip)    # 0x3d0fec
cmpl $0x0, 0x320720(%rip)    # 0x3d0fec
jne 0xb08da
leaq 0x2e6b61(%rip), %rax    # 0x397436
movw $0x5, (%rax)
jmp 0xb0a5d
leaq 0x2e6b50(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x5, %eax
jne 0xb0908
movb -0x9(%rbp), %al
movb %al, 0x31b3a8(%rip)     # 0x3cbc9f
leaq 0x2e6b38(%rip), %rax    # 0x397436
movw $0x6, (%rax)
jmp 0xb0a5b
leaq 0x2e6b27(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x6, %eax
jne 0xb0a27
movl 0x3206c7(%rip), %edx    # 0x3d0fe8
movzbl 0x31f6a8(%rip), %ecx    # 0x3cffd0
movzbl 0x31f6a1(%rip), %r8d    # 0x3cffd1
movl 0x3206ad(%rip), %r9d    # 0x3d0fe4
movl 0x3206b3(%rip), %ebx    # 0x3d0ff0
movzbl -0x9(%rbp), %r11d
movzbl 0x31b355(%rip), %r10d   # 0x3cbc9f
andl $0xff, %r10d
movzbl 0x31b346(%rip), %eax    # 0x3cbc9e
andl $0xff, %eax
leaq 0x2eaa9c(%rip), %rdi    # 0x39b400
leaq 0xd574d(%rip), %rsi     # 0x1860b8
movl %ebx, (%rsp)
movl %r11d, 0x8(%rsp)
movl %r10d, 0x10(%rsp)
movl %eax, 0x18(%rsp)
movb $0x0, %al
callq 0x9060
leaq 0x1ecbe6(%rip), %rax    # 0x29d570
cmpq $0x0, (%rax)
je 0xb09af
leaq 0x1ecbd9(%rip), %rax    # 0x29d570
movq (%rax), %rdi
leaq 0xd845e(%rip), %rsi     # 0x188dff
leaq 0x2eaa58(%rip), %rdx    # 0x39b400
movb $0x0, %al
callq 0x9550
movzbl -0x9(%rbp), %eax
cmpl $0x3, %eax
je 0xb09ba
jmp 0xb0a29
movzbl 0x31b2de(%rip), %eax    # 0x3cbc9f
andl $0xff, %eax
movb %al, 0x31b2d3(%rip)     # 0x3cbc9f
movzbl 0x31b2cb(%rip), %eax    # 0x3cbc9e
andl $0xff, %eax
movb %al, 0x31b2c0(%rip)     # 0x3cbc9e
movzbl 0x31b2b9(%rip), %eax    # 0x3cbc9e
movzbl 0x31b2b3(%rip), %ecx    # 0x3cbc9f
cmpl %ecx, %eax
je 0xb09f2
jmp 0xb0a29
movl 0x3205f0(%rip), %edi    # 0x3d0fe8
movl 0x3205e6(%rip), %esi    # 0x3d0fe4
callq 0xb02e0
movl $0x1, 0x31a467(%rip)    # 0x3cae74
leaq 0x2e6a1e(%rip), %rax    # 0x397432
movw $0x0, (%rax)
leaq 0x2e6a16(%rip), %rax    # 0x397436
movw $0x0, (%rax)
jmp 0xb0a59
jmp 0xb0a29
leaq 0x2e6a02(%rip), %rax    # 0x397432
movw $0x0, (%rax)
leaq 0x2e69fa(%rip), %rax    # 0x397436
movw $0x0, (%rax)
movl $0x0, 0x320585(%rip)    # 0x3d0fd0
movb $0x0, 0x31b24d(%rip)    # 0x3cbc9f
movb $0x0, 0x31b245(%rip)    # 0x3cbc9e
jmp 0xb0a5b
jmp 0xb0a5d
jmp 0xb0a5f
jmp 0xb0a61
jmp 0xb0a63
jmp 0xb0a65
addq $0x28, %rsp
popq %rbx
popq %rbp
retq
nopl (%rax)