// File: 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
subq $0x10, %rsp
movl $0xa, %edi
callq 0xcad0
cmpl $0x0, %eax
jne 0xd1636
movl $0xa, %edi
callq 0x2b610
jmp 0xd1ab7
movl $0xa, %edi
callq 0x2bd30
callq 0x79fc0
movb %al, -0x1(%rbp)
leaq 0x29be71(%rip), %rax    # 0x36d4c0
cmpb $0x0, 0x17a12(%rax)
je 0xd1666
leaq 0x29be61(%rip), %rax    # 0x36d4c0
movb $0x0, 0x17a12(%rax)
leaq 0x2c5dc9(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x0, %eax
jne 0xd170d
movzbl -0x1(%rbp), %eax
cmpl $0xff, %eax
jne 0xd1695
leaq 0x2c5dab(%rip), %rax    # 0x397436
movw $0x1, (%rax)
callq 0x7a1d0
leaq 0x2c5d96(%rip), %rax    # 0x397432
movw $0x0, (%rax)
leaq 0x2c5d8c(%rip), %rax    # 0x397434
movw $0x0, (%rax)
movw $0x0, 0x2fa64a(%rip)    # 0x3cbd00
movw $0x0, 0x2fa63f(%rip)    # 0x3cbcfe
movw $0x0, 0x2ff9e8(%rip)    # 0x3d10b0
movw $0x0, 0x2ff9e1(%rip)    # 0x3d10b2
movw $0x0, 0x2fa622(%rip)    # 0x3cbcfc
movl $0x0, 0x2ff9c8(%rip)    # 0x3d10ac
movw $0x0, 0x2fa61b(%rip)    # 0x3cbd08
movw $0x0, 0x2fa610(%rip)    # 0x3cbd06
movw $0x0, 0x2fa605(%rip)    # 0x3cbd04
movw $0x0, 0x2fa5fa(%rip)    # 0x3cbd02
jmp 0xd1ab7
leaq 0x2c5d22(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x1, %eax
jne 0xd1749
movzbl -0x1(%rbp), %eax
cmpl $0x81, %eax
je 0xd172c
jmp 0xd1a95
leaq 0x2c5d03(%rip), %rax    # 0x397436
movw (%rax), %cx
addw $0x1, %cx
leaq 0x2c5cf5(%rip), %rax    # 0x397436
movw %cx, (%rax)
jmp 0xd1ab5
leaq 0x2c5ce6(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x2, %eax
jne 0xd18bf
cmpl $0x8, 0x2ff949(%rip)    # 0x3d10ac
jl 0xd176a
jmp 0xd1a95
movl 0x2ff93c(%rip), %eax    # 0x3d10ac
andl $0x1, %eax
cmpl $0x0, %eax
je 0xd17d3
movzbl -0x1(%rbp), %eax
movzwl %ax, %edx
shll $0x8, %edx
movl 0x2ff924(%rip), %eax    # 0x3d10ac
sarl %eax
movslq %eax, %rcx
leaq 0x2fa56e(%rip), %rax    # 0x3cbd02
movzwl (%rax,%rcx,2), %eax
addl %edx, %eax
movw %ax, %dx
leaq 0x2fa55e(%rip), %rax    # 0x3cbd02
movw %dx, (%rax,%rcx,2)
movl 0x2ff8fe(%rip), %eax    # 0x3d10ac
sarl %eax
movslq %eax, %rcx
leaq 0x2fa548(%rip), %rax    # 0x3cbd02
movzwl (%rax,%rcx,2), %eax
andl $0xffff, %eax           # imm = 0xFFFF
movw %ax, %dx
leaq 0x2fa535(%rip), %rax    # 0x3cbd02
movw %dx, (%rax,%rcx,2)
jmp 0xd17f8
movzbl -0x1(%rbp), %eax
movzwl %ax, %eax
andl $0xff, %eax
movw %ax, %dx
movl 0x2ff8c4(%rip), %eax    # 0x3d10ac
sarl %eax
movslq %eax, %rcx
leaq 0x2fa50e(%rip), %rax    # 0x3cbd02
movw %dx, (%rax,%rcx,2)
movl 0x2ff8ae(%rip), %eax    # 0x3d10ac
addl $0x1, %eax
movl %eax, 0x2ff8a5(%rip)    # 0x3d10ac
cmpl $0x8, %eax
jl 0xd18ba
movzwl 0x2fa4eb(%rip), %eax    # 0x3cbd02
addl $0x81ff, %eax           # imm = 0x81FF
movzwl 0x2fa4e1(%rip), %ecx    # 0x3cbd04
addl %ecx, %eax
movzwl 0x2fa4da(%rip), %ecx    # 0x3cbd06
addl %ecx, %eax
andl $0xffff, %eax           # imm = 0xFFFF
movw %ax, 0x2ff876(%rip)     # 0x3d10b0
movzwl 0x2ff86f(%rip), %eax    # 0x3d10b0
cmpl $0x8000, %eax           # imm = 0x8000
je 0xd185f
movzwl 0x2ff861(%rip), %ecx    # 0x3d10b0
xorl %eax, %eax
subl %ecx, %eax
andl $0xffff, %eax           # imm = 0xFFFF
movw %ax, 0x2ff851(%rip)     # 0x3d10b0
movzwl 0x2ff84a(%rip), %eax    # 0x3d10b0
movzwl 0x2fa49b(%rip), %ecx    # 0x3cbd08
cmpl %ecx, %eax
je 0xd1876
jmp 0xd1a95
movzwl 0x2fa487(%rip), %eax    # 0x3cbd04
shll %eax
movw %ax, 0x2fa47a(%rip)     # 0x3cbd00
movzwl 0x2fa473(%rip), %eax    # 0x3cbd00
cmpl $0x0, %eax
jne 0xd1897
jmp 0xd1a76
leaq 0x2c5b98(%rip), %rax    # 0x397436
movw (%rax), %cx
addw $0x1, %cx
leaq 0x2c5b8a(%rip), %rax    # 0x397436
movw %cx, (%rax)
movw $0x0, 0x2ff7f8(%rip)    # 0x3d10b0
jmp 0xd18ba
jmp 0xd1ab3
leaq 0x2c5b70(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x3, %eax
jne 0xd19bf
leaq 0x2c5b59(%rip), %rax    # 0x397432
movzwl (%rax), %eax
cmpl $0x1000, %eax           # imm = 0x1000
jge 0xd199d
movb -0x1(%rbp), %dl
leaq 0x2c5b41(%rip), %rax    # 0x397432
movw (%rax), %ax
movw %ax, %si
addw $0x1, %si
leaq 0x2c5b30(%rip), %rcx    # 0x397432
movw %si, (%rcx)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0x2c4b1f(%rip), %rax    # 0x396430
movb %dl, (%rax,%rcx)
movzwl 0x2ff797(%rip), %eax    # 0x3d10b2
andl $0x1, %eax
cmpl $0x0, %eax
je 0xd1948
movzbl -0x1(%rbp), %eax
movzwl %ax, %ecx
shll $0x8, %ecx
movzwl 0x2ff77c(%rip), %eax    # 0x3d10b0
addl %eax, %ecx
movzwl 0x2fa3c1(%rip), %eax    # 0x3cbcfe
addl %ecx, %eax
movw %ax, 0x2fa3b8(%rip)     # 0x3cbcfe
jmp 0xd1953
movzbl -0x1(%rbp), %eax
movw %ax, 0x2ff75d(%rip)     # 0x3d10b0
movw 0x2ff758(%rip), %ax     # 0x3d10b2
addw $0x1, %ax
movw %ax, 0x2ff74d(%rip)     # 0x3d10b2
movw 0x2fa394(%rip), %ax     # 0x3cbd00
addw $-0x1, %ax
movw %ax, 0x2fa389(%rip)     # 0x3cbd00
movzwl 0x2fa382(%rip), %eax    # 0x3cbd00
cmpl $0x0, %eax
jne 0xd199b
leaq 0x2c5aac(%rip), %rax    # 0x397436
movw (%rax), %cx
addw $0x1, %cx
leaq 0x2c5a9e(%rip), %rax    # 0x397436
movw %cx, (%rax)
jmp 0xd19ba
leaq 0x1cc7ac(%rip), %rax    # 0x29e150
movl (%rax), %ecx
orl $0x8000, %ecx           # imm = 0x8000
leaq 0x1cc79d(%rip), %rax    # 0x29e150
movl %ecx, (%rax)
jmp 0xd1a95
jmp 0xd1ab1
leaq 0x2c5a70(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x4, %eax
jne 0xd19f6
movzbl -0x1(%rbp), %eax
movw %ax, 0x2fa323(%rip)     # 0x3cbcfc
leaq 0x2c5a56(%rip), %rax    # 0x397436
movw (%rax), %cx
addw $0x1, %cx
leaq 0x2c5a48(%rip), %rax    # 0x397436
movw %cx, (%rax)
jmp 0xd1aaf
leaq 0x2c5a39(%rip), %rax    # 0x397436
movzwl (%rax), %eax
cmpl $0x5, %eax
jne 0xd1a93
movzbl -0x1(%rbp), %eax
movzwl %ax, %ecx
shll $0x8, %ecx
movzwl 0x2fa2e2(%rip), %eax    # 0x3cbcfc
addl %ecx, %eax
movw %ax, 0x2fa2d9(%rip)     # 0x3cbcfc
movzwl 0x2fa2d4(%rip), %eax    # 0x3cbcfe
cmpl $0x8000, %eax           # imm = 0x8000
je 0xd1a48
movzwl 0x2fa2c6(%rip), %ecx    # 0x3cbcfe
xorl %eax, %eax
subl %ecx, %eax
andl $0xffff, %eax           # imm = 0xFFFF
movw %ax, 0x2fa2b6(%rip)     # 0x3cbcfe
movzwl 0x2fa2af(%rip), %eax    # 0x3cbcfe
movzwl 0x2fa2a6(%rip), %ecx    # 0x3cbcfc
cmpl %ecx, %eax
je 0xd1a74
leaq 0x1cc6ef(%rip), %rax    # 0x29e150
movl (%rax), %ecx
orl $0x4000, %ecx           # imm = 0x4000
leaq 0x1cc6e0(%rip), %rax    # 0x29e150
movl %ecx, (%rax)
jmp 0xd1a95
jmp 0xd1a76
leaq 0x2c59b9(%rip), %rax    # 0x397436
movw $0x0, (%rax)
callq 0xd14a0
movl $0x1, 0x2f93e3(%rip)    # 0x3cae74
jmp 0xd1aad
jmp 0xd1a95
leaq 0x2c5996(%rip), %rax    # 0x397432
movw $0x0, (%rax)
leaq 0x2c598e(%rip), %rax    # 0x397436
movw $0x0, (%rax)
jmp 0xd1aaf
jmp 0xd1ab1
jmp 0xd1ab3
jmp 0xd1ab5
jmp 0xd1ab7
addq $0x10, %rsp
popq %rbp
retq
nopl (%rax)