// File: msu-starx[P]starx/serial.cpp
// Function: SerialPort::setPort()
void SerialPort::setPort(void){
    tcgetattr(fileDescriptor, &portSettings); // Gets the current attributes

    switch(baudRate) {
    case 300:
        constBaud = B300;
        break;
    case 600:
        constBaud = B600;
        break;
    case 1200:
        constBaud = B1200;
        break;
    case 2400:
        constBaud = B2400;
        break;
    case 4800:
        constBaud = B4800;
        break;
    case 9600:
        constBaud = B9600;
        break;
    case 19200:
        constBaud = B19200;
        break;
    case 38400:
        constBaud = B38400;
        break;
    case 57600:
        constBaud = B57600;
        break;
    case 115200:
        constBaud = B115200;
        break;
    case 500000:
        constBaud = B500000;
        break;
    case 1000000:
        constBaud = B1000000;
        break;
    default:
        printf("Error! Undefined baud rate!\n");
        break;
    }

    cfsetispeed(&portSettings, constBaud); // Sets read speed
    cfsetospeed(&portSettings, constBaud); // Sets write speed

    portSettings.c_cflag &= ~PARENB; // Disables parity
    portSettings.c_cflag &= ~CSTOPB; // Sets the stop bits amount to 1
    portSettings.c_cflag &= ~CSIZE; // Clears the mask for setting the data size
    portSettings.c_cflag |= CS8; // Set the data bits amount to 8
    portSettings.c_cflag &= ~CRTSCTS; // Disables hardware flow control
    portSettings.c_cflag |= (CREAD | CLOCAL); // Enables receiver and sets local mode
    portSettings.c_iflag &= ~(IXON | IXOFF | IXANY); // Disables software flow control
    portSettings.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // Selects raw input mode
    portSettings.c_oflag &= ~OPOST; // Selects raw output mode

    portSettings.c_cc[VMIN] = 0; // Minimum number of characters to read
    portSettings.c_cc[VTIME] = 0; // Time to wait for every character read
    // If VMIN = 0 and VTIME = 0, read() will be satisfied immediately

    if((tcsetattr(fileDescriptor, TCSANOW, &portSettings)) != 0) { // Sets the new attributes
        perror("\nSet_Port: Error in setting attributes - ");
    }
    else{
        printf("\n| Baud rate = %d bps | Stop bits = 1 | Parity = None |\n", baudRate);
    }

    this->flushPortAll();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rsi
movq %rsi, -0x18(%rbp)
movl 0x4(%rsi), %edi
addq $0x120, %rsi            # imm = 0x120
callq 0x2290
movq -0x18(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0xc(%rbp)
subl $0x12c, %eax            # imm = 0x12C
je 0xb94d
jmp 0xb89a
movl -0xc(%rbp), %eax
subl $0x258, %eax            # imm = 0x258
je 0xb95c
jmp 0xb8aa
movl -0xc(%rbp), %eax
subl $0x4b0, %eax            # imm = 0x4B0
je 0xb96b
jmp 0xb8ba
movl -0xc(%rbp), %eax
subl $0x960, %eax            # imm = 0x960
je 0xb977
jmp 0xb8ca
movl -0xc(%rbp), %eax
subl $0x12c0, %eax           # imm = 0x12C0
je 0xb983
jmp 0xb8da
movl -0xc(%rbp), %eax
subl $0x2580, %eax           # imm = 0x2580
je 0xb98f
jmp 0xb8ea
movl -0xc(%rbp), %eax
subl $0x4b00, %eax           # imm = 0x4B00
je 0xb99b
jmp 0xb8fa
movl -0xc(%rbp), %eax
subl $0x9600, %eax           # imm = 0x9600
je 0xb9a7
jmp 0xb90a
movl -0xc(%rbp), %eax
subl $0xe100, %eax           # imm = 0xE100
je 0xb9b3
jmp 0xb91a
movl -0xc(%rbp), %eax
subl $0x1c200, %eax          # imm = 0x1C200
je 0xb9bf
jmp 0xb92a
movl -0xc(%rbp), %eax
subl $0x7a120, %eax          # imm = 0x7A120
je 0xb9cb
jmp 0xb93a
movl -0xc(%rbp), %eax
subl $0xf4240, %eax          # imm = 0xF4240
je 0xb9d7
jmp 0xb9e3
movq -0x18(%rbp), %rax
movl $0x7, (%rax)
jmp 0xb9f1
movq -0x18(%rbp), %rax
movl $0x8, (%rax)
jmp 0xb9f1
movq -0x18(%rbp), %rax
movl $0x9, (%rax)
jmp 0xb9f1
movq -0x18(%rbp), %rax
movl $0xb, (%rax)
jmp 0xb9f1
movq -0x18(%rbp), %rax
movl $0xc, (%rax)
jmp 0xb9f1
movq -0x18(%rbp), %rax
movl $0xd, (%rax)
jmp 0xb9f1
movq -0x18(%rbp), %rax
movl $0xe, (%rax)
jmp 0xb9f1
movq -0x18(%rbp), %rax
movl $0xf, (%rax)
jmp 0xb9f1
movq -0x18(%rbp), %rax
movl $0x1001, (%rax)         # imm = 0x1001
jmp 0xb9f1
movq -0x18(%rbp), %rax
movl $0x1002, (%rax)         # imm = 0x1002
jmp 0xb9f1
movq -0x18(%rbp), %rax
movl $0x1005, (%rax)         # imm = 0x1005
jmp 0xb9f1
movq -0x18(%rbp), %rax
movl $0x1008, (%rax)         # imm = 0x1008
jmp 0xb9f1
leaq 0x88e(%rip), %rdi       # 0xc278
movb $0x0, %al
callq 0x2050
movq -0x18(%rbp), %rax
movq %rax, %rdi
addq $0x120, %rdi            # imm = 0x120
movl (%rax), %esi
callq 0x2120
movq -0x18(%rbp), %rax
movq %rax, %rdi
addq $0x120, %rdi            # imm = 0x120
movl (%rax), %esi
callq 0x2270
movq -0x18(%rbp), %rdx
movl 0x128(%rdx), %eax
andl $0xfffffeff, %eax       # imm = 0xFFFFFEFF
movl %eax, 0x128(%rdx)
movl 0x128(%rdx), %eax
andl $-0x41, %eax
movl %eax, 0x128(%rdx)
movl 0x128(%rdx), %eax
andl $-0x31, %eax
movl %eax, 0x128(%rdx)
movl 0x128(%rdx), %eax
orl $0x30, %eax
movl %eax, 0x128(%rdx)
movl 0x128(%rdx), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, 0x128(%rdx)
movl 0x128(%rdx), %eax
orl $0x880, %eax            # imm = 0x880
movl %eax, 0x128(%rdx)
movl 0x120(%rdx), %eax
andl $0xffffe3ff, %eax       # imm = 0xFFFFE3FF
movl %eax, 0x120(%rdx)
movl 0x12c(%rdx), %eax
andl $-0x1c, %eax
movl %eax, 0x12c(%rdx)
movl 0x124(%rdx), %eax
andl $-0x2, %eax
movl %eax, 0x124(%rdx)
movb $0x0, 0x137(%rdx)
movb $0x0, 0x136(%rdx)
movl 0x4(%rdx), %edi
addq $0x120, %rdx            # imm = 0x120
xorl %esi, %esi
callq 0x22e0
cmpl $0x0, %eax
je 0xbae0
leaq 0x7bc(%rip), %rdi       # 0xc295
callq 0x21d0
jmp 0xbaf5
movq -0x18(%rbp), %rax
movl 0x10(%rax), %esi
leaq 0x7d1(%rip), %rdi       # 0xc2bf
movb $0x0, %al
callq 0x2050
movq -0x18(%rbp), %rdi
callq 0xbb10
addq $0x20, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)