// File: /workspace/github_cpp_makefile_star2_O3/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 0xb9ad
movl $0xa, %edi
testl %eax, %eax
je 0x9a00c
callq 0x1ea0b
callq 0x584bd
leaq 0x27436c(%rip), %rcx    # 0x30e2e0
cmpb $0x0, 0x17a12(%rcx)
je 0x99f84
movb $0x0, 0x17a12(%rcx)
leaq 0x29e2cb(%rip), %rcx    # 0x338256
movzwl (%rcx), %edx
cmpq $0x5, %rdx
ja 0x9a100
leaq 0x8593d(%rip), %rsi     # 0x11f8dc
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
cmpl $0xff, %eax
jne 0x99fb9
movw $0x1, (%rcx)
callq 0x58672
leaq 0x29e292(%rip), %rax    # 0x338252
xorl %ecx, %ecx
movw %cx, (%rax)
leaq 0x29e288(%rip), %rax    # 0x338254
movw %cx, (%rax)
movw %cx, 0x2d2b4a(%rip)     # 0x36cb20
movw %cx, 0x2d2b41(%rip)     # 0x36cb1e
movw %cx, 0x2d7ed8(%rip)     # 0x371ebc
movw %cx, 0x2d7ed3(%rip)     # 0x371ebe
movw %cx, 0x2d2b2a(%rip)     # 0x36cb1c
movl $0x0, 0x2d7ebc(%rip)    # 0x371eb8
movq $0x0, 0x2d2b21(%rip) # 0x36cb28
jmp 0x9a10f
popq %rbp
jmp 0x1e66a
movw %ax, 0x2d2b03(%rip)     # 0x36cb1c
movw $0x5, (%rcx)
jmp 0x9a10f
movl 0x2d7e8f(%rip), %edx    # 0x371eb8
cmpl $0x7, %edx
jg 0x9a100
movl %edx, %esi
sarl %esi
movslq %esi, %rsi
leaq 0x2d2ae8(%rip), %rdi    # 0x36cb28
testb $0x1, %dl
jne 0x9a111
movw %ax, (%rdi,%rsi,2)
jmp 0x9a118
leaq 0x29e1f9(%rip), %rsi    # 0x338252
movzwl (%rsi), %edx
movl $0x8000, %edi           # imm = 0x8000
cmpq $0xfff, %rdx            # imm = 0xFFF
ja 0x9a0f7
leal 0x1(%rdx), %edi
movw %di, (%rsi)
leaq 0x29d1d5(%rip), %rsi    # 0x337250
movb %al, (%rsi,%rdx)
movzwl 0x2d7e39(%rip), %edx    # 0x371ebe
testb $0x1, %dl
jne 0x9a177
movw %ax, 0x2d7e27(%rip)     # 0x371ebc
jmp 0x9a188
cmpl $0x81, %eax
jne 0x9a100
movw $0x2, (%rcx)
jmp 0x9a10f
shll $0x8, %eax
addw 0x2d2a6a(%rip), %ax     # 0x36cb1c
movw %ax, 0x2d2a63(%rip)     # 0x36cb1c
movzwl 0x2d2a5e(%rip), %edx    # 0x36cb1e
movw $0x8000, %si            # imm = 0x8000
cmpl $0x8000, %edx           # imm = 0x8000
je 0x9a0d7
negl %edx
movw %dx, 0x2d2a49(%rip)     # 0x36cb1e
movl %edx, %esi
movl $0x4000, %edi           # imm = 0x4000
cmpw %ax, %si
jne 0x9a0f7
movw $0x0, (%rcx)
callq 0x99e0c
movl $0x1, 0x2d1b9f(%rip)    # 0x36bc94
jmp 0x9a10f
leaq 0x1a4e72(%rip), %rax    # 0x23ef70
orl %edi, (%rax)
leaq 0x29e14b(%rip), %rax    # 0x338252
xorl %edx, %edx
movw %dx, (%rax)
movw %dx, (%rcx)
popq %rbp
retq
shll $0x8, %eax
addw %ax, (%rdi,%rsi,2)
leal 0x1(%rdx), %eax
movl %eax, 0x2d7d97(%rip)    # 0x371eb8
cmpl $0x7, %edx
jne 0x9a10f
movzwl 0x2d29fd(%rip), %eax    # 0x36cb2a
movzwl 0x2d29f4(%rip), %edx    # 0x36cb28
addw %ax, %dx
addw 0x2d29ee(%rip), %dx     # 0x36cb2c
movl $0x7e01, %esi           # imm = 0x7E01
subl %edx, %esi
movw %si, 0x2d7d70(%rip)     # 0x371ebc
cmpw 0x2d29db(%rip), %si     # 0x36cb2e
jne 0x9a100
addl %eax, %eax
movw %ax, 0x2d29c2(%rip)     # 0x36cb20
testw %ax, %ax
je 0x9a0e1
movw $0x3, (%rcx)
movw $0x0, 0x2d7d47(%rip)    # 0x371ebc
jmp 0x9a10f
shll $0x8, %eax
addw 0x2d7d3b(%rip), %ax     # 0x371ebc
addw %ax, 0x2d2996(%rip)     # 0x36cb1e
leal 0x1(%rdx), %eax
movw %ax, 0x2d7d2c(%rip)     # 0x371ebe
decw 0x2d2987(%rip)          # 0x36cb20
jne 0x9a10f
movw $0x4, (%rcx)
jmp 0x9a10f