// File: /workspace/github_cpp_makefile_star2_O1/jkingan[P]ladyheather/heathgps.cpp
// Function: get_zod_message()
void get_zod_message()
{
static int hdr_ptr = 0;
u08 c;
static u16 cksum;
static u16 byte_count;
static int row = 3;

   // This routine buffers up an incoming Venus message.  When the end of the
   // message is seen, the message is parsed and decoded with parse_zodiac_msg()

   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;
   }


//if(tsip_sync == 0) fprintf(debug_file, "\n");
//if(debug_file) fprintf(debug_file, "%02X ", c);

   if(tsip_sync == 0) {         // syncing to start of message, search for a 0xFF
      if(c == 0xFF) {
         tsip_sync = 1;
         get_sync_time();
      }

      tsip_wptr = 0;
      tsip_rptr = 0;
      zodiac_rx_len = 0;
      zodiac_rx_cksum = 0;
      cksum = 0;
      byte_count = 0;
      zodiac_vfy_cksum = 0;
      hdr_ptr = 0;
      zodiac_hdr[0] = zodiac_hdr[1] = zodiac_hdr[2] = zodiac_hdr[3] = 0;
      return;
   }
   else if(tsip_sync == 1) { // 0xFF has been seen, now checking next byte
      if(c != 0x81) goto rst_msg;
      ++tsip_sync;           // 0xFF 0x81 seen... so get the message header
   }
   else if(tsip_sync == 2) { // header packet
      if(hdr_ptr >= 8) goto rst_msg;   // should never happen

      if(hdr_ptr & 1) {  // high byte
         zodiac_hdr[hdr_ptr>>1] += (((u16) c) * 256);
         zodiac_hdr[hdr_ptr>>1] &= 0xFFFF;
      }
      else {  // low byte
         zodiac_hdr[hdr_ptr>>1] = ((u16) c) & 0xFF;
      }

      if(++hdr_ptr >= 8) {  // end of header
         cksum = (0x81FF + zodiac_hdr[0] + zodiac_hdr[1] + zodiac_hdr[2]) & 0xFFFF;   
         if(cksum != 0x8000) cksum = (0 - cksum) & 0xFFFF;
         if(cksum != zodiac_hdr[3]) goto rst_msg;  // bad checksum;
         zodiac_rx_len = (zodiac_hdr[1]*2);
         if(zodiac_rx_len == 0) goto got_it;  // message has no payload, go parse it
         else {
            ++tsip_sync;
            cksum = 0;
//if(debug_file) fprintf(debug_file, "  [%d]  ", byte_count);
         }
      }
   }
   else if(tsip_sync == 3) {   // get Zodiac message payload
      if(tsip_wptr < MAX_TSIP) {  // so add it to the message buffer
         tsip_buf[tsip_wptr++] = c;
         if(byte_count & 1) zodiac_rx_cksum += ((((u16) c) * 256) + cksum);
         else               cksum = (u16) c;    // low byte of word
         ++byte_count;
         --zodiac_rx_len;
         if(zodiac_rx_len == 0) ++tsip_sync;  // message payload processed, now get checksums
      }
      else {
         tsip_error |= OVFL_ERROR;
         goto rst_msg;
      }
   }
   else if(tsip_sync == 4) {  // get message checksum low byte
      zodiac_vfy_cksum = (u16) c;   // low byte of message checksum
      ++tsip_sync;
   }
   else if(tsip_sync == 5) {  // message checksum, end of venus message
      zodiac_vfy_cksum += ((u16) c) * 256;

      if(zodiac_rx_cksum != 0x8000) zodiac_rx_cksum = (0 - zodiac_rx_cksum) & 0xFFFF;
//sprintf(out, "id:%-4d  len:%-4d  flags:%04X  vfy:%04X  rx:%04X  bc:%-4d  dif:%04X", 
//zodiac_hdr[0], zodiac_hdr[1], zodiac_hdr[2], zodiac_vfy_cksum, zodiac_rx_cksum, byte_count, (zodiac_rx_cksum-zodiac_vfy_cksum)&0xFFFF);
//vidstr(row,3, YELLOW, out);
//if(row++>30) row = 0;
//refresh_page();
//if(debug_file) fprintf(debug_file, "  (v:%04X calc:%04X)\n", zodiac_vfy_cksum, zodiac_rx_cksum);
      if(zodiac_rx_cksum != zodiac_vfy_cksum) { // checksum error
         tsip_error |= CKSUM_ERROR;
         goto rst_msg;
      }

      got_it:
      tsip_sync = 0;
      decode_zod_message();  // message checksum matches, process the message
      packet_end = 1;
   }
   else {     // should never happen
      rst_msg:
      tsip_wptr = 0;
      tsip_sync = 0;
   }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movl $0xa, %edi
callq 0xb9c3
movl $0xa, %edi
testl %eax, %eax
je 0x99e5d
callq 0x1ea2c
callq 0x584d5
leaq 0x27451b(%rip), %rcx    # 0x30e2e0
cmpb $0x0, 0x17a12(%rcx)
je 0x99dd5
movb $0x0, 0x17a12(%rcx)
leaq 0x29e47a(%rip), %rcx    # 0x338256
movzwl (%rcx), %edx
cmpq $0x5, %rdx
ja 0x99f53
leaq 0x85a14(%rip), %rsi     # 0x11f804
movslq (%rsi,%rdx,4), %rdi
addq %rsi, %rdi
jmpq *%rdi
cmpl $0xff, %eax
jne 0x99e0a
movw $0x1, (%rcx)
callq 0x58682
leaq 0x29e441(%rip), %rax    # 0x338252
xorl %ecx, %ecx
movw %cx, (%rax)
leaq 0x29e437(%rip), %rax    # 0x338254
movw %cx, (%rax)
movw %cx, 0x2d2cf9(%rip)     # 0x36cb20
movw %cx, 0x2d2cf0(%rip)     # 0x36cb1e
movw %cx, 0x2d8087(%rip)     # 0x371ebc
movw %cx, 0x2d8082(%rip)     # 0x371ebe
movw %cx, 0x2d2cd9(%rip)     # 0x36cb1c
movl $0x0, 0x2d806b(%rip)    # 0x371eb8
movq $0x0, 0x2d2cd0(%rip) # 0x36cb28
jmp 0x99ff9
popq %rbp
jmp 0x1e6a0
movw %ax, 0x2d2cb2(%rip)     # 0x36cb1c
jmp 0x99ff3
movl 0x2d8043(%rip), %esi    # 0x371eb8
cmpl $0x7, %esi
jg 0x99f53
movl %esi, %edi
sarl %edi
movslq %edi, %rdi
leaq 0x2d2c9c(%rip), %r8     # 0x36cb28
testb $0x1, %sil
jne 0x99f67
movw %ax, (%r8,%rdi,2)
jmp 0x99f6f
leaq 0x29e3ab(%rip), %rdi    # 0x338252
movzwl (%rdi), %esi
movl $0x8000, %r8d           # imm = 0x8000
cmpq $0xfff, %rsi            # imm = 0xFFF
ja 0x99f49
leal 0x1(%rsi), %r8d
movw %r8w, (%rdi)
leaq 0x29d384(%rip), %rdi    # 0x337250
movb %al, (%rdi,%rsi)
movzwl 0x2d7fe8(%rip), %esi    # 0x371ebe
testb $0x1, %sil
jne 0x99fcf
movw %ax, 0x2d7fd5(%rip)     # 0x371ebc
jmp 0x99fe0
cmpl $0x81, %eax
jne 0x99f53
jmp 0x99ff3
shll $0x8, %eax
addw 0x2d2c1a(%rip), %ax     # 0x36cb1c
movw %ax, 0x2d2c13(%rip)     # 0x36cb1c
movzwl 0x2d2c0e(%rip), %edx    # 0x36cb1e
cmpl $0x8000, %edx           # imm = 0x8000
je 0x99f21
negl %edx
movw %dx, 0x2d2bfd(%rip)     # 0x36cb1e
movl $0x4000, %r8d           # imm = 0x4000
cmpw %ax, 0x2d2bf0(%rip)     # 0x36cb1e
jne 0x99f49
movw $0x0, (%rcx)
callq 0x99c5d
movl $0x1, 0x2d1d50(%rip)    # 0x36bc94
jmp 0x99ff9
leaq 0x1a5020(%rip), %rax    # 0x23ef70
orl %r8d, (%rax)
leaq 0x29e2f8(%rip), %rax    # 0x338252
xorl %edx, %edx
movw %dx, (%rax)
movw %dx, (%rcx)
jmp 0x99ff9
shll $0x8, %eax
addw %ax, (%r8,%rdi,2)
leal 0x1(%rsi), %eax
movl %eax, 0x2d7f40(%rip)    # 0x371eb8
cmpl $0x7, %esi
jne 0x99ff9
movzwl 0x2d2ba6(%rip), %eax    # 0x36cb2a
movzwl 0x2d2b9d(%rip), %esi    # 0x36cb28
addw %ax, %si
addw 0x2d2b97(%rip), %si     # 0x36cb2c
movl $0x7e01, %edi           # imm = 0x7E01
subl %esi, %edi
movw %di, 0x2d7f19(%rip)     # 0x371ebc
cmpw 0x2d2b84(%rip), %di     # 0x36cb2e
jne 0x99f53
addl %eax, %eax
movw %ax, 0x2d2b6b(%rip)     # 0x36cb20
testw %ax, %ax
je 0x99f30
leal 0x1(%rdx), %eax
movw %ax, (%rcx)
movw $0x0, 0x2d7eef(%rip)    # 0x371ebc
jmp 0x99ff9
shll $0x8, %eax
addw 0x2d7ee3(%rip), %ax     # 0x371ebc
addw %ax, 0x2d2b3e(%rip)     # 0x36cb1e
leal 0x1(%rsi), %eax
movw %ax, 0x2d7ed4(%rip)     # 0x371ebe
decw 0x2d2b2f(%rip)          # 0x36cb20
jne 0x99ff9
leal 0x1(%rdx), %eax
movw %ax, (%rcx)
popq %rbp
retq