// File: /workspace/github_cpp_makefile_star2_O3/jkingan[P]ladyheather/heathgps.cpp
// Function: set_rcvr_baud(int, int, int, int)
int set_rcvr_baud(int baud, int data_bits, int parity, int stop_bits)
{
int i;

   // future command to set receiver serial port settings
   // !!!!! data_bits, parity, stop_bits not currently implemented
   //       We currentlu always set 8:N:1
   // returns: 0=baud set  1=improper rate   2=baud change not supported

   if(read_only) return 2;

   i = (-1);
   if     (baud == 150) return 1;   // don't allow non-standard values
   else if(baud == 300)    i = 0;   
   else if(baud == 600)    i = 1;   
   else if(baud == 1200)   i = 2;  
   else if(baud == 2400)   i = 3;  
   else if(baud == 4800)   i = 4;  
   else if(baud == 9600)   i = 5;  
   else if(baud == 19200)  i = 6; 
   else if(baud == 38400)  i = 7; 
   else if(baud == 57600)  i = 8; 
   else if(baud == 115200) i = 9; 
   else if(baud == 230400) {
      if(0 && (rcvr_type == VENUS_RCVR)) i = 10;
      else return 1;
   }
   else return 1;

   if(rcvr_type == ESIP_RCVR) {
      sprintf(out, "PERDCFG,UART1,%d", baud);
      send_esip_cmd(out);
      com[RCVR_PORT].baud_rate = baud;
   }
   else if(0 && (rcvr_type == NVS_RCVR)) {
      if(i < 4) return 1;
      send_nvs_start(0x0B);
      send_byte(0);    // current port
      send_dword(baud);
      send_byte(0);    // current protocol
      send_nvs_end();
      com[RCVR_PORT].baud_rate = baud;
   }
   else if(1 && (rcvr_type == SIRF_RCVR)) {
      if(i < 2) return 1;
      send_sirf_start(SET_SIRF_BINARY, 8+1);
      send_dword(baud);
      send_byte(8);
      send_byte(1);
      send_byte(0);
      send_byte(0);
      send_sirf_end();
      com[RCVR_PORT].baud_rate = baud;
   }
   else if(rcvr_type == TM4_RCVR) {  // time port baud rate
      i -= 2;
      if(i < 0) return 1;
      sprintf(out, "#10,%d", i);
      send_tm4_cmd(out);
      return 0;   // does not change control serial port baud rate !!!
   }
   else if(0 && (rcvr_type == TSIP_RCVR)) {
      if((baud >= 9600) && (baud <= 115200)) {
         set_trimble_protocol(baud, 8, NO_PAR, 1, 2,2);
         com[RCVR_PORT].baud_rate = baud;
      }
      else return 1;
   }
   else if(rcvr_type == UBX_RCVR) {
      rcvr_type = NMEA_RCVR;
      sprintf(out, "PUBX,41,1,0003,0001,%d,0", com[RCVR_PORT].baud_rate);  // 9600 bps ublox binary output mode (port 1)
      send_nmea_cmd(out);
      rcvr_type = UBX_RCVR;
      com[RCVR_PORT].baud_rate = baud;
   }
   else if(rcvr_type == VENUS_RCVR) {
      i -= 4;
      if(i < 0) return 1;
      send_venus_start(SET_VENUS_BAUD);
      send_byte(0);  // com1
      send_byte(i); 
      send_venus_save(1);
      com[RCVR_PORT].baud_rate = baud;
   }
   else if(rcvr_type == Z12_RCVR) {
      com[RCVR_PORT].baud_rate = baud;
      sprintf(out, "PASHS,SPD,A,%d", i);
      send_z12_cmd(out);
      com[RCVR_PORT].baud_rate = baud;
   }
   else if(0 && (rcvr_type == ZODIAC_RCVR)) {
      if(i == 9) i += 2;
      else       i += 1;
      if(i == 10) return 1;   // we don't allow 76800 baud

      send_zod_start(ZOD_SET_BAUD, 20, 0);
      send_zod_seq();

      send_word(0x0001);  // port 1

      send_word(1);       // 8 bits
      send_word(0);       // 1 stop
      send_word(0);       // no parity
      send_word(i);       // baud
      send_word(0);
      send_word(0);

      send_word(1);       // 8 bits
      send_word(0);       // 1 stop
      send_word(0);       // no parity
      send_word(i);       // baud
      send_word(0);
      send_word(0);

      send_zod_end();

      com[RCVR_PORT].baud_rate = baud;
   }
   else return 2;

   Sleep(COM_INIT_SLEEP);
   init_com(RCVR_PORT, 150);
   return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
leaq 0x2d78e9(%rip), %rax    # 0x33701a
movl $0x2, %r14d
cmpb $0x0, (%rax)
jne 0x5fa0b
movl %edi, %ebx
cmpl $0x257f, %edi           # imm = 0x257F
jg 0x5f783
movb $0x1, %al
cmpl $0x4af, %ebx            # imm = 0x4AF
jle 0x5f7af
cmpl $0x4b0, %ebx            # imm = 0x4B0
je 0x5f7f4
cmpl $0x960, %ebx            # imm = 0x960
je 0x5f804
cmpl $0x12c0, %ebx           # imm = 0x12C0
jne 0x5fa17
movl $0x4, %r15d
jmp 0x5f812
cmpl $0x95ff, %ebx           # imm = 0x95FF
jle 0x5f7d0
cmpl $0x9600, %ebx           # imm = 0x9600
je 0x5f7fc
cmpl $0xe100, %ebx           # imm = 0xE100
je 0x5f80c
cmpl $0x1c200, %ebx          # imm = 0x1C200
jne 0x5fa17
movl $0x9, %r15d
jmp 0x5f812
xorl %r15d, %r15d
cmpl $0x12c, %ebx            # imm = 0x12C
je 0x5f7cc
cmpl $0x258, %ebx            # imm = 0x258
jne 0x5fa17
movl $0x1, %r15d
movb $0x1, %cl
jmp 0x5f816
cmpl $0x2580, %ebx           # imm = 0x2580
je 0x5f7ec
cmpl $0x4b00, %ebx           # imm = 0x4B00
jne 0x5fa17
movl $0x6, %r15d
jmp 0x5f812
movl $0x5, %r15d
jmp 0x5f812
movl $0x2, %r15d
jmp 0x5f814
movl $0x7, %r15d
jmp 0x5f812
movl $0x3, %r15d
jmp 0x5f814
movl $0x8, %r15d
xorl %eax, %eax
xorl %ecx, %ecx
leaq 0x1b90b3(%rip), %r12    # 0x2188d0
movq (%r12), %rdx
cmpq $0x7fffffff, %rdx       # imm = 0x7FFFFFFF
jg 0x5f88b
cmpq $0x4, %rdx
je 0x5f8e6
cmpq $0x40, %rdx
je 0x5f965
cmpq $0x80, %rdx
jne 0x5fa0b
movl $0x1, %r14d
testb %al, %al
jne 0x5fa0b
movl $0x5, %edi
movl $0x4, %esi
callq 0x5fb3c
xorl %edi, %edi
callq 0x590f7
addb $-0x4, %r15b
movzbl %r15b, %edi
callq 0x590f7
movl $0x1, %edi
callq 0x5fc00
jmp 0x5f9e2
movabsq $0x400000000, %rax      # imm = 0x400000000
cmpq %rax, %rdx
je 0x5f928
movabsq $0x100000000, %rax      # imm = 0x100000000
cmpq %rax, %rdx
je 0x5f9b2
movl $0x80000000, %eax       # imm = 0x80000000
cmpq %rax, %rdx
jne 0x5fa0b
leaq 0x2dc95a(%rip), %r14    # 0x33c220
leaq 0xc0c54(%rip), %rsi     # 0x120521
movq %r14, %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0x9060
movq %r14, %rdi
callq 0x5ee9b
jmp 0x5f9e2
movq $0x2, (%r12)
leaq 0x2ae9eb(%rip), %rax    # 0x30e2e0
movl 0x159d0(%rax), %edx
leaq 0x2dc91e(%rip), %r14    # 0x33c220
leaq 0xc0c30(%rip), %rsi     # 0x120539
movq %r14, %rdi
xorl %eax, %eax
callq 0x9060
movq %r14, %rdi
callq 0x5f0e5
movq $0x4, (%r12)
jmp 0x5f9e2
movl $0x1, %r14d
testb %cl, %cl
jne 0x5fa0b
addl $-0x2, %r15d
leaq 0x2dc8df(%rip), %rbx    # 0x33c220
leaq 0xc0bea(%rip), %rsi     # 0x120532
xorl %r14d, %r14d
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0x9060
movq %rbx, %rdi
callq 0x5f170
jmp 0x5fa0b
movl $0x1, %r14d
testb %cl, %cl
jne 0x5fa0b
movl $0x86, %edi
movl $0x9, %esi
callq 0x5fa1f
movl %ebx, %edi
callq 0x592eb
movl $0x8, %edi
callq 0x590f7
movl $0x1, %edi
callq 0x590f7
xorl %edi, %edi
callq 0x590f7
xorl %edi, %edi
callq 0x590f7
callq 0x5fac3
jmp 0x5f9e2
leaq 0x2ae927(%rip), %rax    # 0x30e2e0
movl %ebx, 0x159d0(%rax)
leaq 0x2dc85a(%rip), %r14    # 0x33c220
leaq 0xc0b85(%rip), %rsi     # 0x120552
movq %r14, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0x9060
movq %r14, %rdi
callq 0x5f2c8
leaq 0x2ae8f7(%rip), %rax    # 0x30e2e0
movl %ebx, 0x159d0(%rax)
movl $0xc8, %edi
callq 0xafe8
movl $0xa, %edi
movl $0x96, %esi
callq 0xb49e
xorl %r14d, %r14d
movl %r14d, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %r14d
jmp 0x5fa0b