// File: /workspace/github_cpp_makefile_star2_O1/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 %r15
pushq %r14
pushq %rbx
pushq %rax
movl $0xa, %edi
callq 0xb9c3
movl $0xa, %edi
testl %eax, %eax
je 0x80f3d
callq 0x1ea2c
callq 0x584d5
movl %eax, %ebx
leaq 0x28d3fa(%rip), %rax    # 0x30e2e0
cmpb $0x0, 0x17a12(%rax)
je 0x80ef6
movb $0x0, 0x17a12(%rax)
leaq 0x2b7359(%rip), %r14    # 0x338256
movzwl (%r14), %eax
cmpq $0x6, %rax
ja 0x810a0
leaq 0x9e14a(%rip), %rcx     # 0x11f05c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpl $0x6, %ebx
je 0x810c8
cmpl $0x2, %ebx
jne 0x810e0
movw $0x1, (%r14)
callq 0x58682
jmp 0x81113
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x1e6a0
addb %bl, 0x2ebb6c(%rip)     # 0x36cabe
leaq 0x2b72f9(%rip), %rax    # 0x338252
movzwl (%rax), %ecx
leal 0x1(%rcx), %edx
movw %dx, (%rax)
leaq 0x2efe97(%rip), %rax    # 0x370e00
movb %bl, (%rcx,%rax)
decl 0x2f0eaa(%rip)          # 0x371e1c
jne 0x81135
movw $0x5, (%r14)
jmp 0x81135
movl %ebx, 0x2f0e8f(%rip)    # 0x371e18
addb %bl, 0x2ebb2f(%rip)     # 0x36cabe
movw $0x3, (%r14)
jmp 0x81135
movl %ebx, 0x2f0e80(%rip)    # 0x371e20
movl %ebx, 0x2f0e76(%rip)    # 0x371e1c
addb %bl, 0x2ebb12(%rip)     # 0x36cabe
xorl %eax, %eax
cmpl $0x1, %ebx
adcw $0x4, %ax
movw %ax, (%r14)
jmp 0x81135
movl %ebx, 0x2f0e50(%rip)    # 0x371e14
addb %bl, 0x2ebaf4(%rip)     # 0x36cabe
movw $0x2, (%r14)
jmp 0x81135
movb %bl, 0x2ebae4(%rip)     # 0x36cabf
movw $0x6, (%r14)
jmp 0x81135
movl 0x2f0e2c(%rip), %edx    # 0x371e18
movzbl 0x2efe0d(%rip), %ecx    # 0x370e00
movzbl 0x2efe06(%rip), %r8d    # 0x370e01
movl 0x2f0e12(%rip), %r9d    # 0x371e14
movl 0x2f0e17(%rip), %r10d   # 0x371e20
movzbl 0x2ebaae(%rip), %r11d   # 0x36cabf
movzbl 0x2ebaa5(%rip), %r15d   # 0x36cabe
leaq 0x2bb200(%rip), %rdi    # 0x33c220
leaq 0xa1d21(%rip), %rsi     # 0x122d48
xorl %eax, %eax
pushq %r15
pushq %r11
pushq %rbx
pushq %r10
callq 0x9060
addq $0x20, %rsp
leaq 0x1bd350(%rip), %rax    # 0x23e390
movq (%rax), %rdi
testq %rdi, %rdi
je 0x8105d
leaq 0xa49a8(%rip), %rsi     # 0x1259f7
leaq 0x2bb1ca(%rip), %rdx    # 0x33c220
xorl %eax, %eax
callq 0x9560
cmpl $0x3, %ebx
jne 0x810a0
movb 0x2eba56(%rip), %al     # 0x36cabe
cmpb 0x2eba51(%rip), %al     # 0x36cabf
jne 0x810a0
movl 0x2f0da2(%rip), %edi    # 0x371e18
movl 0x2f0d98(%rip), %esi    # 0x371e14
callq 0x80bd3
movl $0x1, 0x2eac09(%rip)    # 0x36bc94
leaq 0x2b71c0(%rip), %rax    # 0x338252
xorl %ecx, %ecx
movw %cx, (%rax)
movw %cx, (%r14)
jmp 0x81135
leaq 0x2b71ab(%rip), %rax    # 0x338252
xorl %ecx, %ecx
movw %cx, (%rax)
movw %cx, (%r14)
movl $0x0, 0x2f0d46(%rip)    # 0x371e00
movb %cl, 0x2eb9ff(%rip)     # 0x36cabf
movb %cl, 0x2eb9f8(%rip)     # 0x36cabe
jmp 0x81135
leaq 0x1bd2c1(%rip), %rax    # 0x23e390
movq (%rax), %rcx
testq %rcx, %rcx
je 0x81113
leaq 0xa1c60(%rip), %rdi     # 0x122d3e
jmp 0x81104
cmpl $0x15, %ebx
sete %al
leaq 0x1bd2a3(%rip), %rcx    # 0x23e390
movq (%rcx), %rcx
testq %rcx, %rcx
setne %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0x81113
leaq 0xa1c3f(%rip), %rdi     # 0x122d43
movl $0x4, %esi
movl $0x1, %edx
callq 0x9750
leaq 0x2b7138(%rip), %rax    # 0x338252
xorl %ecx, %ecx
movw %cx, (%rax)
leaq 0x2b712e(%rip), %rax    # 0x338254
movw %cx, (%rax)
movb %cl, 0x2eb98f(%rip)     # 0x36cabe
movb %cl, 0x2eb98a(%rip)     # 0x36cabf
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq