// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r14
pushq %rbx
movq %rdi, %rbx
movl 0x4(%rdi), %edi
leaq 0x120(%rbx), %r14
movq %r14, %rsi
callq 0x2200
movl 0x10(%rbx), %eax
cmpl $0x4aff, %eax           # imm = 0x4AFF
jg 0x5c83
cmpl $0x95f, %eax            # imm = 0x95F
jg 0x5cae
cmpl $0x12c, %eax            # imm = 0x12C
je 0x5ce8
cmpl $0x258, %eax            # imm = 0x258
je 0x5d08
cmpl $0x4b0, %eax            # imm = 0x4B0
jne 0x5d28
movl $0x9, (%rbx)
jmp 0x5d34
cmpl $0x1c1ff, %eax          # imm = 0x1C1FF
jg 0x5ccb
cmpl $0x4b00, %eax           # imm = 0x4B00
je 0x5cf0
cmpl $0x9600, %eax           # imm = 0x9600
je 0x5d10
cmpl $0xe100, %eax           # imm = 0xE100
jne 0x5d28
movl $0x1001, (%rbx)         # imm = 0x1001
jmp 0x5d34
cmpl $0x960, %eax            # imm = 0x960
je 0x5cf8
cmpl $0x12c0, %eax           # imm = 0x12C0
je 0x5d18
cmpl $0x2580, %eax           # imm = 0x2580
jne 0x5d28
movl $0xd, (%rbx)
jmp 0x5d34
cmpl $0x1c200, %eax          # imm = 0x1C200
je 0x5d00
cmpl $0x7a120, %eax          # imm = 0x7A120
je 0x5d20
cmpl $0xf4240, %eax          # imm = 0xF4240
jne 0x5d28
movl $0x1008, (%rbx)         # imm = 0x1008
jmp 0x5d34
movl $0x7, (%rbx)
jmp 0x5d34
movl $0xe, (%rbx)
jmp 0x5d34
movl $0xb, (%rbx)
jmp 0x5d34
movl $0x1002, (%rbx)         # imm = 0x1002
jmp 0x5d34
movl $0x8, (%rbx)
jmp 0x5d34
movl $0xf, (%rbx)
jmp 0x5d34
movl $0xc, (%rbx)
jmp 0x5d34
movl $0x1005, (%rbx)         # imm = 0x1005
jmp 0x5d34
leaq 0x5e4(%rip), %rdi       # 0x6313
callq 0x22e0
movl (%rbx), %esi
movq %r14, %rdi
callq 0x2100
movl (%rbx), %esi
movq %r14, %rdi
callq 0x21f0
movl $0x7ffff60f, %eax       # imm = 0x7FFFF60F
andl 0x128(%rbx), %eax
orl $0x8b0, %eax            # imm = 0x8B0
movl %eax, 0x128(%rbx)
andb $-0x1d, 0x121(%rbx)
andb $-0x1c, 0x12c(%rbx)
andb $-0x2, 0x124(%rbx)
movw $0x0, 0x136(%rbx)
movl 0x4(%rbx), %edi
xorl %esi, %esi
movq %r14, %rdx
callq 0x2240
testl %eax, %eax
jne 0x5daf
movl 0x10(%rbx), %esi
leaq 0x513(%rip), %rdi       # 0x62aa
xorl %eax, %eax
callq 0x2050
movl 0x4(%rbx), %edi
movl $0x2, %esi
popq %rbx
popq %r14
popq %rbp
jmp 0x22c0
leaq 0x4ca(%rip), %rdi       # 0x6280
callq 0x21a0
jmp 0x5d9e
nop