// File: mattdibi[P]Programmatore-JTAG-per-Xilinx/serial.cpp
// Function: Serial::ReadString(char*, char, unsigned int, unsigned int)
unsigned Serial::ReadString(char *String,char FinalChar,unsigned MaxNbBytes,unsigned TimeOut_ms)
{
   // Se non è specificato un timeout, usa il metodo senza timeout
   if (TimeOut_ms==0)
      return ReadStringNoTimeOut(String,FinalChar,MaxNbBytes);
   unsigned NbBytes=0;
   unsigned ret;

   // Timer la gestione del timeout
   TimeOut Timer;
   long int TimeOutParam;

   // Inizializza il timer
   Timer.InitTimer();

   while (NbBytes<MaxNbBytes)
   {
      // Tempo trascorso
      TimeOutParam=TimeOut_ms-Timer.ElapsedTime_ms();
      if (TimeOutParam>0)
      {
         ret=ReadChar(&String[NbBytes],TimeOutParam);
         if (ret==1)
         {
            if (String[NbBytes]==FinalChar)
            {
               String[NbBytes]='\0';
               return NbBytes;
            }
            NbBytes++;
         }
         else
          throw ErrorReadException();
      }
      if (Timer.ElapsedTime_ms()>TimeOut_ms)
      {
         String[NbBytes]='\0';
         throw TimeoutException();
      }
   }
   throw ErrorBufferFullException();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movb %dl, %al
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movb %al, -0x19(%rbp)
movl %ecx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0x7368
movq -0x50(%rbp), %rdi
movq -0x18(%rbp), %rsi
movb -0x19(%rbp), %al
movl -0x20(%rbp), %ecx
movsbl %al, %edx
callq 0x7210
movl %eax, -0x4(%rbp)
jmp 0x74e6
movl $0x0, -0x28(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x7690
leaq -0x40(%rbp), %rdi
callq 0x7160
movl -0x28(%rbp), %eax
cmpl -0x20(%rbp), %eax
jae 0x74a9
movl -0x24(%rbp), %eax
movq %rax, -0x58(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x7180
movq %rax, %rcx
movq -0x58(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jle 0x744b
movq -0x50(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x28(%rbp), %eax
addq %rax, %rsi
movq -0x48(%rbp), %rax
movl %eax, %edx
callq 0x7040
movl %eax, -0x2c(%rbp)
cmpl $0x1, -0x2c(%rbp)
jne 0x740c
movq -0x18(%rbp), %rax
movl -0x28(%rbp), %ecx
movsbl (%rax,%rcx), %eax
movsbl -0x19(%rbp), %ecx
cmpl %ecx, %eax
jne 0x7401
movq -0x18(%rbp), %rax
movl -0x28(%rbp), %ecx
movb $0x0, (%rax,%rcx)
movl -0x28(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x74e6
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x7449
movl $0x8, %edi
callq 0x3110
movq %rax, %rdi
movq %rdi, -0x60(%rbp)
xorl %esi, %esi
movl $0x8, %edx
callq 0x3130
movq -0x60(%rbp), %rdi
callq 0x77c0
movq -0x60(%rbp), %rdi
leaq 0x683b(%rip), %rsi      # 0xdc78
leaq 0x3bc(%rip), %rdx       # 0x7800
callq 0x3490
jmp 0x744b
leaq -0x40(%rbp), %rdi
callq 0x7180
movl -0x24(%rbp), %ecx
cmpq %rcx, %rax
jbe 0x74a4
movq -0x18(%rbp), %rax
movl -0x28(%rbp), %ecx
movb $0x0, (%rax,%rcx)
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 0x7820
movq -0x68(%rbp), %rdi
leaq 0x6780(%rip), %rsi      # 0xdc18
leaq 0x3c1(%rip), %rdx       # 0x7860
callq 0x3490
jmp 0x7381
movl $0x8, %edi
callq 0x3110
movq %rax, %rdi
movq %rdi, -0x70(%rbp)
xorl %esi, %esi
movl $0x8, %edx
callq 0x3130
movq -0x70(%rbp), %rdi
callq 0x7880
movq -0x70(%rbp), %rdi
leaq 0x67b6(%rip), %rsi      # 0xdc90
leaq 0x3df(%rip), %rdx       # 0x78c0
callq 0x3490
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nop