// File: mattdibi[P]Programmatore-JTAG-per-Xilinx/serial.cpp
// Function: Serial::Open(char const*, unsigned int)
void Serial::Open(const char *Device,const unsigned Bauds)
{
   // Struttura per memorizzare i parametri della connessione
   struct termios options;

   // Apre la connessione in lettura/scrittura
   fd = open(Device, O_RDWR | O_NOCTTY | O_NDELAY);
   if (fd == -1)
   {
      // Se non è possibile aprire la connessione, lancia un'eccezione
      throw ErrorOpeningException();
   }

   // Impostazione dei flag per la seriale
   fcntl(fd, F_SETFL, FNDELAY);

   // Impostazione del puntatore della struttura per le opzioni
   tcgetattr(fd, &options);

   // Azzeramento la struttura options
   bzero(&options, sizeof(options));

   // Impostazione la velocità della seriale
   speed_t Speed;
   switch (Bauds)
   {
      case 110:
         Speed=B110;
         break;
      case 300:
         Speed=B300;
         break;
      case 600:
         Speed=B600;
         break;
      case 1200:
         Speed=B1200;
         break;
      case 2400:
         Speed=B2400;
         break;
      case 4800:
         Speed=B4800;
         break;
      case 9600:
         Speed=B9600;
         break;
      case 19200:
         Speed=B19200;
         break;
      case 38400:
         Speed=B38400;
         break;
      case 57600:
         Speed=B57600;
         break;
      case 115200:
         Speed=B115200;
         break;
      default:
         throw ErrorSpeedException();
   }
   cfsetispeed(&options, Speed);
   cfsetospeed(&options, Speed);

   // Impostazione dei flag
   options.c_cflag |= ( CLOCAL | CREAD |  CS8);
   options.c_iflag |= ( IGNPAR | IGNBRK );
   options.c_cc[VTIME]=0;
   options.c_cc[VMIN]=0;
   tcsetattr(fd, TCSANOW, &options);
   return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rdi
movl $0x902, %esi            # imm = 0x902
movb $0x0, %al
callq 0x31f0
movl %eax, %ecx
movq -0x60(%rbp), %rax
movl %ecx, (%rax)
cmpl $-0x1, (%rax)
jne 0x6d08
movl $0x8, %edi
callq 0x3110
movq %rax, %rdi
movq %rdi, -0x68(%rbp)
xorl %esi, %esi
movl $0x8, %edx
callq 0x3130
movq -0x68(%rbp), %rdi
callq 0x76a0
movq -0x68(%rbp), %rdi
leaq 0x6f34(%rip), %rsi      # 0xdc30
leaq 0x9dd(%rip), %rdx       # 0x76e0
callq 0x3490
movq -0x60(%rbp), %rax
movl (%rax), %edi
xorl %eax, %eax
movl $0x4, %esi
movl $0x800, %edx            # imm = 0x800
callq 0x3140
movq -0x60(%rbp), %rax
movl (%rax), %edi
leaq -0x50(%rbp), %rsi
callq 0x3320
xorps %xmm0, %xmm0
movups %xmm0, -0x24(%rbp)
movaps %xmm0, -0x30(%rbp)
movaps %xmm0, -0x40(%rbp)
movaps %xmm0, -0x50(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x6c(%rbp)
subl $0x6e, %eax
je 0x6dc6
jmp 0x6d4e
movl -0x6c(%rbp), %eax
subl $0x12c, %eax            # imm = 0x12C
je 0x6dd2
jmp 0x6d5a
movl -0x6c(%rbp), %eax
subl $0x258, %eax            # imm = 0x258
je 0x6dde
jmp 0x6d66
movl -0x6c(%rbp), %eax
subl $0x4b0, %eax            # imm = 0x4B0
je 0x6dea
jmp 0x6d72
movl -0x6c(%rbp), %eax
subl $0x960, %eax            # imm = 0x960
je 0x6df3
jmp 0x6d7e
movl -0x6c(%rbp), %eax
subl $0x12c0, %eax           # imm = 0x12C0
je 0x6dfc
jmp 0x6d8a
movl -0x6c(%rbp), %eax
subl $0x2580, %eax           # imm = 0x2580
je 0x6e05
jmp 0x6d96
movl -0x6c(%rbp), %eax
subl $0x4b00, %eax           # imm = 0x4B00
je 0x6e0e
jmp 0x6da2
movl -0x6c(%rbp), %eax
subl $0x9600, %eax           # imm = 0x9600
je 0x6e17
jmp 0x6dae
movl -0x6c(%rbp), %eax
subl $0xe100, %eax           # imm = 0xE100
je 0x6e20
jmp 0x6dba
movl -0x6c(%rbp), %eax
subl $0x1c200, %eax          # imm = 0x1C200
je 0x6e29
jmp 0x6e32
movl $0x3, -0x54(%rbp)
jmp 0x6e6f
movl $0x7, -0x54(%rbp)
jmp 0x6e6f
movl $0x8, -0x54(%rbp)
jmp 0x6e6f
movl $0x9, -0x54(%rbp)
jmp 0x6e6f
movl $0xb, -0x54(%rbp)
jmp 0x6e6f
movl $0xc, -0x54(%rbp)
jmp 0x6e6f
movl $0xd, -0x54(%rbp)
jmp 0x6e6f
movl $0xe, -0x54(%rbp)
jmp 0x6e6f
movl $0xf, -0x54(%rbp)
jmp 0x6e6f
movl $0x1001, -0x54(%rbp)    # imm = 0x1001
jmp 0x6e6f
movl $0x1002, -0x54(%rbp)    # imm = 0x1002
jmp 0x6e6f
movl $0x8, %edi
callq 0x3110
movq %rax, %rdi
movq %rdi, -0x78(%rbp)
xorl %esi, %esi
movl $0x8, %edx
callq 0x3130
movq -0x78(%rbp), %rdi
callq 0x7700
movq -0x78(%rbp), %rdi
leaq 0x6de5(%rip), %rsi      # 0xdc48
leaq 0x8d6(%rip), %rdx       # 0x7740
callq 0x3490
movl -0x54(%rbp), %esi
leaq -0x50(%rbp), %rdi
callq 0x3170
movl -0x54(%rbp), %esi
leaq -0x50(%rbp), %rdi
callq 0x3300
movq -0x60(%rbp), %rax
movl -0x48(%rbp), %ecx
orl $0x8b0, %ecx            # imm = 0x8B0
movl %ecx, -0x48(%rbp)
movl -0x50(%rbp), %ecx
orl $0x5, %ecx
movl %ecx, -0x50(%rbp)
movb $0x0, -0x3a(%rbp)
movb $0x0, -0x39(%rbp)
movl (%rax), %edi
xorl %esi, %esi
leaq -0x50(%rbp), %rdx
callq 0x3360
addq $0x80, %rsp
popq %rbp
retq
nop