// File: /workspace/github_cpp_makefile_star2_O2/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 %r13
pushq %r12
pushq %rbx
pushq %rax
cmpl $0x12, %edi
ja 0xb8d7
movl %edi, %ebx
cmpl $0xa, %edi
sete %cl
movq 0x1f702f(%rip), %rax    # 0x2028d0
cmpq $0x400, %rax            # imm = 0x400
sete %dl
andb %cl, %dl
movl 0x21d67e(%rip), %ecx    # 0x228f30
testl %ecx, %ecx
sete %sil
testb %sil, %dl
jne 0xb8d7
testl %ecx, %ecx
sete %cl
cmpl $0xa, %ebx
sete %dl
cmpq $0x800000, %rax         # imm = 0x800000
sete %al
andb %dl, %al
testb %cl, %al
je 0xb8ec
xorl %r14d, %r14d
movl %r14d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0xa, %ebx
jne 0xb911
cmpq $0x0, 0x21cac7(%rip)    # 0x2283c0
je 0xb911
pushq $0xa
popq %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa95a
movl %ebx, %eax
imulq $0x2260, %rax, %rax     # imm = 0x2260
leaq 0x2ec9bf(%rip), %r12    # 0x2f82e0
cmpl $0x0, 0x2234(%rax,%r12)
je 0xb8d7
addq %rax, %r12
cmpl $0x0, 0x8(%r12)
js 0xb8d7
testb $0x1, 0x2251(%r12)
jne 0xb8d7
movslq 0x2224(%r12), %rax
cmpl 0x2220(%r12), %eax
jl 0xb9ae
andq $0x0, 0x2220(%r12)
leaq 0x1220(%r12), %r14
xorl %r13d, %r13d
xorl %eax, %eax
testl %eax, %eax
jne 0xb9a1
movl 0x8(%r12), %edi
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rsi
callq 0x9570
movq %rax, %r15
testl %r15d, %r15d
cmovlel %r13d, %r15d
movl %r15d, 0x2220(%r12)
callq 0xc067d
movl 0x2220(%r12), %eax
jmp 0xb96a
testl %r15d, %r15d
je 0xba08
movslq 0x2224(%r12), %rax
leal 0x1(%rax), %ecx
movl %ecx, 0x2224(%r12)
movzbl 0x1220(%r12,%rax), %r14d
cmpl $0xd, %r14d
je 0xb9ce
cmpl $0xa, %r14d
jne 0xb9d8
movl $0x1, 0x21d5bc(%rip)    # 0x228f94
cmpb $0x4f, 0x2eadc9(%rip)   # 0x2f67a8
jne 0xb8da
cmpl %ebx, 0x2ec8e5(%rip)    # 0x2f82d0
jne 0xb8da
movl 0x21ca4d(%rip), %edx    # 0x228444
movl %ebx, %edi
movl %r14d, %esi
xorl %ecx, %ecx
callq 0xaad9
jmp 0xb8da
cmpb $0x0, 0x1f9824(%rip)    # 0x205233
je 0xba27
leaq 0xe80e7(%rip), %rsi     # 0xf3aff
movl $0xde, %edi
callq 0xb290
jmp 0xb8d7
orb $0x1, 0x2251(%r12)
incl 0x3155de(%rip)          # 0x321014
jmp 0xb8d7