// File: /workspace/github_cpp_makefile_star2_O1/jkingan[P]ladyheather/heather.cpp
// Function: get_serial_char(unsigned int)
u08 get_serial_char(unsigned port)
{
int flag;
u08 c;

   // return the next byte from a com device
   if(port >= NUM_COM_PORTS) return FALSE;

   if((port == RCVR_PORT) && (rcvr_type == NO_RCVR) && (enable_terminal == 0)) { // alternate ready/not ready each check
      return 0;
   }
if((port == RCVR_PORT) && (rcvr_type == TIDE_RCVR) && (enable_terminal == 0)) {
   return 0;
}


   if((port == RCVR_PORT) && sim_file) {  // get char from a simulation file
      return get_sim_char(port);
   }

   if(com[port].process_com == 0) return 0;  //lfs
   if(com[port].com_fd < 0) return 0;
   if(com[port].com_error & RCV_ERR) return 0;

   if(com[port].next_rcv_byte < com[port].rcv_byte_count) {   // return byte previously fetched by check_incoming_data()
      c = com[port].rcv_buffer[com[port].next_rcv_byte++];
      if((c == 0x0D) || (c == 0x0A)) crlf_seen = 1;

      if((zoom_screen == 'O')  && (port == monitor_port)) {   // send char to "terminal" format monitor screen
         echo_term(monitor_port, c, monitor_hex, 0);
      }
      return c;
   }

   com[port].rcv_byte_count = 0;
   com[port].next_rcv_byte = 0;

   while(com[port].rcv_byte_count == 0) {  // wait until we have a character
      if(1 || (com[port].com_port != 0) || (com[port].usb_port != 0)) {  // zork COM port in use: read a byte from serial port
         flag = read(com[port].com_fd, &com[port].rcv_buffer[0], RCV_BUF_SIZE);
         if(flag < 0) {  // !!!! com error or no serial data available - we should check errno()
            flag = com[port].rcv_byte_count = 0;
         }
         else com[port].rcv_byte_count = flag;
      }

      update_pwm();   // if doing pwm temperature control
   }

   if(flag && com[port].rcv_byte_count) {  // succesful read
      c = com[port].rcv_buffer[com[port].next_rcv_byte++];
      if((c == 0x0D) || (c == 0x0A)) crlf_seen = 1;

      if((zoom_screen == 'O') && (port == monitor_port)) {   // send char to "terminal" format monitor screen
         echo_term(monitor_port, c, monitor_hex, 0);
      }
      return c;
   }

   if(com_error_exit) {
      error_exit(222, "Serial receive error");
   }
   else {
      com[port].com_error |= RCV_ERR;
      ++com_errors;
   }
   return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
cmpl $0x12, %edi
ja 0xbb66
movl %edi, %ebx
cmpl $0xa, %edi
sete %cl
movq 0x20cda0(%rip), %rax    # 0x2188d0
cmpq $0x400, %rax            # imm = 0x400
sete %dl
andb %cl, %dl
movl 0x2333ef(%rip), %ecx    # 0x23ef30
testl %ecx, %ecx
sete %sil
testb %sil, %dl
jne 0xbb66
testl %ecx, %ecx
sete %cl
cmpl $0xa, %ebx
sete %dl
cmpq $0x800000, %rax         # imm = 0x800000
sete %al
andb %dl, %al
testb %cl, %al
je 0xbb75
xorl %r14d, %r14d
movl %r14d, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
cmpl $0xa, %ebx
jne 0xbb96
cmpq $0x0, 0x23283e(%rip)    # 0x23e3c0
je 0xbb96
movl $0xa, %edi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0xaab1
movl %ebx, %eax
imulq $0x2260, %rax, %rax     # imm = 0x2260
leaq 0x30273a(%rip), %r12    # 0x30e2e0
cmpl $0x0, 0x2234(%rax,%r12)
je 0xbb66
addq %rax, %r12
cmpl $0x0, 0x8(%r12)
js 0xbb66
testb $0x1, 0x2251(%r12)
jne 0xbb66
movslq 0x2224(%r12), %rax
cmpl 0x2220(%r12), %eax
jl 0xbc33
movq $0x0, 0x2220(%r12)
leaq 0x1220(%r12), %r14
movl 0x8(%r12), %edi
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rsi
callq 0x9570
movq %rax, %r15
testl %r15d, %r15d
movl $0x0, %eax
cmovgl %r15d, %eax
movl %eax, 0x2220(%r12)
callq 0xd1865
cmpl $0x0, 0x2220(%r12)
je 0xbbed
testl %r15d, %r15d
jle 0xbc8d
movslq 0x2224(%r12), %rax
leal 0x1(%rax), %ecx
movl %ecx, 0x2224(%r12)
movzbl 0x1220(%r12,%rax), %r14d
cmpl $0xd, %r14d
je 0xbc53
cmpl $0xa, %r14d
jne 0xbc5d
movl $0x1, 0x233337(%rip)    # 0x23ef94
cmpb $0x4f, 0x300b44(%rip)   # 0x30c7a8
jne 0xbb69
movl 0x302660(%rip), %edi    # 0x30e2d0
cmpl %ebx, %edi
jne 0xbb69
movl 0x2327c6(%rip), %edx    # 0x23e444
movl %r14d, %esi
xorl %ecx, %ecx
callq 0xac26
jmp 0xbb69
cmpb $0x0, 0x20f59f(%rip)    # 0x21b233
je 0xbcac
leaq 0xfde02(%rip), %rsi     # 0x109a9f
movl $0xde, %edi
callq 0xb425
jmp 0xbb66
orb $0x1, 0x2251(%r12)
incl 0x32b359(%rip)          # 0x337014
jmp 0xbb66