// File: 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
subq $0x20, %rsp
movl %edi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movl %ecx, -0x14(%rbp)
leaq 0x31269f(%rip), %rax    # 0x3961fa
cmpb $0x0, (%rax)
je 0x83b6c
movl $0x2, -0x4(%rbp)
jmp 0x83f00
movl $0xffffffff, -0x18(%rbp) # imm = 0xFFFFFFFF
cmpl $0x96, -0x8(%rbp)
jne 0x83b88
movl $0x1, -0x4(%rbp)
jmp 0x83f00
cmpl $0x12c, -0x8(%rbp)      # imm = 0x12C
jne 0x83b9d
movl $0x0, -0x18(%rbp)
jmp 0x83c7e
cmpl $0x258, -0x8(%rbp)      # imm = 0x258
jne 0x83bb2
movl $0x1, -0x18(%rbp)
jmp 0x83c7c
cmpl $0x4b0, -0x8(%rbp)      # imm = 0x4B0
jne 0x83bc7
movl $0x2, -0x18(%rbp)
jmp 0x83c7a
cmpl $0x960, -0x8(%rbp)      # imm = 0x960
jne 0x83bdc
movl $0x3, -0x18(%rbp)
jmp 0x83c78
cmpl $0x12c0, -0x8(%rbp)     # imm = 0x12C0
jne 0x83bf1
movl $0x4, -0x18(%rbp)
jmp 0x83c76
cmpl $0x2580, -0x8(%rbp)     # imm = 0x2580
jne 0x83c03
movl $0x5, -0x18(%rbp)
jmp 0x83c74
cmpl $0x4b00, -0x8(%rbp)     # imm = 0x4B00
jne 0x83c15
movl $0x6, -0x18(%rbp)
jmp 0x83c72
cmpl $0x9600, -0x8(%rbp)     # imm = 0x9600
jne 0x83c27
movl $0x7, -0x18(%rbp)
jmp 0x83c70
cmpl $0xe100, -0x8(%rbp)     # imm = 0xE100
jne 0x83c39
movl $0x8, -0x18(%rbp)
jmp 0x83c6e
cmpl $0x1c200, -0x8(%rbp)    # imm = 0x1C200
jne 0x83c4b
movl $0x9, -0x18(%rbp)
jmp 0x83c6c
cmpl $0x38400, -0x8(%rbp)    # imm = 0x38400
jne 0x83c60
movl $0x1, -0x4(%rbp)
jmp 0x83f00
movl $0x1, -0x4(%rbp)
jmp 0x83f00
jmp 0x83c6e
jmp 0x83c70
jmp 0x83c72
jmp 0x83c74
jmp 0x83c76
jmp 0x83c78
jmp 0x83c7a
jmp 0x83c7c
jmp 0x83c7e
jmp 0x83c80
movl $0x80000000, %ecx       # imm = 0x80000000
leaq 0x1f3e24(%rip), %rax    # 0x277ab0
cmpq %rcx, (%rax)
jne 0x83cca
movl -0x8(%rbp), %edx
leaq 0x317765(%rip), %rdi    # 0x39b400
leaq 0xffaff(%rip), %rsi     # 0x1837a1
movb $0x0, %al
callq 0x9060
leaq 0x317750(%rip), %rdi    # 0x39b400
callq 0x830d0
movl -0x8(%rbp), %ecx
leaq 0x2e9801(%rip), %rax    # 0x36d4c0
movl %ecx, 0x159d0(%rax)
jmp 0x83ee0
leaq 0x1f3ddf(%rip), %rax    # 0x277ab0
cmpq $0x40, (%rax)
jne 0x83d3c
cmpl $0x2, -0x18(%rbp)
jge 0x83ce9
movl $0x1, -0x4(%rbp)
jmp 0x83f00
movl $0x86, %edi
movl $0x9, %esi
callq 0x83f10
movl -0x8(%rbp), %edi
callq 0x7b520
movl $0x8, %edi
callq 0x7b210
movl $0x1, %edi
callq 0x7b210
xorl %edi, %edi
callq 0x7b210
xorl %edi, %edi
callq 0x7b210
callq 0x83fe0
movl -0x8(%rbp), %ecx
leaq 0x2e978f(%rip), %rax    # 0x36d4c0
movl %ecx, 0x159d0(%rax)
jmp 0x83ede
movabsq $0x400000000, %rcx      # imm = 0x400000000
leaq 0x1f3d63(%rip), %rax    # 0x277ab0
cmpq %rcx, (%rax)
jne 0x83d9d
movl -0x18(%rbp), %eax
subl $0x2, %eax
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jge 0x83d6d
movl $0x1, -0x4(%rbp)
jmp 0x83f00
movl -0x18(%rbp), %edx
leaq 0x317689(%rip), %rdi    # 0x39b400
leaq 0xffa34(%rip), %rsi     # 0x1837b2
movb $0x0, %al
callq 0x9060
leaq 0x317674(%rip), %rdi    # 0x39b400
callq 0x83490
movl $0x0, -0x4(%rbp)
jmp 0x83f00
leaq 0x1f3d0c(%rip), %rax    # 0x277ab0
cmpq $0x4, (%rax)
jne 0x83e09
leaq 0x1f3cff(%rip), %rax    # 0x277ab0
movq $0x2, (%rax)
leaq 0x2e9701(%rip), %rax    # 0x36d4c0
movl 0x159d0(%rax), %edx
leaq 0x317634(%rip), %rdi    # 0x39b400
leaq 0xff9e6(%rip), %rsi     # 0x1837b9
movb $0x0, %al
callq 0x9060
leaq 0x31761f(%rip), %rdi    # 0x39b400
callq 0x833e0
leaq 0x1f3cc3(%rip), %rax    # 0x277ab0
movq $0x4, (%rax)
movl -0x8(%rbp), %ecx
leaq 0x2e96c2(%rip), %rax    # 0x36d4c0
movl %ecx, 0x159d0(%rax)
jmp 0x83eda
leaq 0x1f3ca0(%rip), %rax    # 0x277ab0
cmpq $0x80, (%rax)
jne 0x83e71
movl -0x18(%rbp), %eax
subl $0x4, %eax
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jge 0x83e34
movl $0x1, -0x4(%rbp)
jmp 0x83f00
movl $0x5, %edi
movl $0x4, %esi
callq 0x84080
xorl %edi, %edi
callq 0x7b210
movl -0x18(%rbp), %eax
movzbl %al, %edi
callq 0x7b210
movl $0x1, %edi
callq 0x84130
movl -0x8(%rbp), %ecx
leaq 0x2e9657(%rip), %rax    # 0x36d4c0
movl %ecx, 0x159d0(%rax)
jmp 0x83ed8
movabsq $0x100000000, %rcx      # imm = 0x100000000
leaq 0x1f3c2e(%rip), %rax    # 0x277ab0
cmpq %rcx, (%rax)
jne 0x83ecd
movl -0x8(%rbp), %ecx
leaq 0x2e962f(%rip), %rax    # 0x36d4c0
movl %ecx, 0x159d0(%rax)
movl -0x18(%rbp), %edx
leaq 0x31755f(%rip), %rdi    # 0x39b400
leaq 0xff92a(%rip), %rsi     # 0x1837d2
movb $0x0, %al
callq 0x9060
leaq 0x31754a(%rip), %rdi    # 0x39b400
callq 0x83630
movl -0x8(%rbp), %ecx
leaq 0x2e95fb(%rip), %rax    # 0x36d4c0
movl %ecx, 0x159d0(%rax)
jmp 0x83ed6
movl $0x2, -0x4(%rbp)
jmp 0x83f00
jmp 0x83ed8
jmp 0x83eda
jmp 0x83edc
jmp 0x83ede
jmp 0x83ee0
movl $0xc8, %edi
callq 0xb8b0
movl $0xa, %edi
movl $0x96, %esi
callq 0xc0e0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x20, %rsp
popq %rbp
retq
nopl (%rax)