// File: /workspace/github_cpp_makefile_star2_O1/atsidaev[P]trx2x/DiskImage.cpp
// Function: TDiskImage::Open(char*, bool)
void TDiskImage::Open(char *filename, bool ROnly)
{
   FlushImage();

   TDiskImageType typ = DIT_UNK;

   char ext[MAX_EXT];
   const char* ext_pos = get_extension(filename);
   if (ext_pos) {
      strncpy(ext, ext_pos, MAX_EXT);
      make_uppercase(ext);
      
      if(!strcmp(ext, ".TRD")) typ = DIT_TRD;
      if(!strcmp(ext, ".SCL")) typ = DIT_SCL;
      if(!strcmp(ext, ".FDI")) typ = DIT_FDI;
      if(!strcmp(ext, ".UDI")) typ = DIT_UDI;
      if(!strcmp(ext, ".TD0")) typ = DIT_TD0;
      if(!strcmp(ext, ".FDD")) typ = DIT_FDD;
      if(!memcmp(ext, ".$", 2)) typ = DIT_HOB;
      if(!memcmp(ext, ".!", 2)) typ = DIT_HOB;
   }

   if(! ((FType==DIT_HOB)&&(typ==DIT_HOB)) )     // if not hobeta clear disk...
   {
      ReadOnly = true;
      DiskPresent = false;
      Changed = false;
      FType = DIT_UNK;

      for(int t=0; t < 256; t++)
         for(int s=0; s < 256; s++)
         {
            FTrackLength[t][s] = 0;
            if(FTracksPtr[t][s][0]) delete FTracksPtr[t][s][0];
            FTracksPtr[t][s][0] = NULL;
            if(FTracksPtr[t][s][1]) delete FTracksPtr[t][s][1];
            FTracksPtr[t][s][1] = NULL;
         }
   }

   if(typ == DIT_UNK)
   {
      ShowError(ERR_UNKFORMAT);
      return;
   }
   strcpy((char*)FFileName, filename);
   FType = typ;
                                                                        
   if(!file_exists(filename)) // file not present - create needed
   {
/*
      if(IDYES != MessageBox(GetForegroundWindow(), STR_CREATE,"Create disk image", MB_ICONQUESTION | MB_YESNO)) return;
      if(typ==DIT_HOB) { ShowError(ERR_BADCREATE);  return; }

      formatTRDOS(80, 2);

      ReadOnly = ROnly;
      FType = typ;
      DiskPresent = true;

      int hf = open((char*)FFileName, O_CREAT | O_RDWR | O_TRUNC | O_BINARY, _S_IREAD|_S_IWRITE);
      if(FType==DIT_TRD) writeTRD(hf);
      if(FType==DIT_SCL) writeSCL(hf);
      if(FType==DIT_FDI) writeFDI(hf);
      if(FType==DIT_UDI) writeUDI(hf);
      if(FType==DIT_TD0) writeTD0(hf);
      if(FType==DIT_FDD) writeFDD(hf);
      close(hf);
*/
      return;
   }

   int hfile = open(filename, O_RDONLY | O_BINARY, 0666);

   if (hfile < 0) {
       char sbuf[8192];
       snprintf(sbuf, sizeof(sbuf), ERR_OPEN" %s", filename);
       ShowError(sbuf);
       return;
   }

   if(typ==DIT_TRD) readTRD(hfile, ROnly);
   if(typ==DIT_SCL) readSCL(hfile, ROnly);
   if(typ==DIT_FDI) readFDI(hfile, ROnly);
   if(typ==DIT_UDI) readUDI(hfile, ROnly);
   if(typ==DIT_TD0) readTD0(hfile, ROnly);
   if(typ==DIT_FDD) readFDD(hfile, ROnly);
   if(typ==DIT_HOB) readHOB(hfile);

   close(hfile);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2058, %rsp           # imm = 0x2058
movl %edx, 0x4(%rsp)
movq %rsi, %rbx
movq %rdi, %rbp
callq 0x1d02
movq %rbx, %rdi
movl $0x2e, %esi
callq 0x1160
testq %rax, %rax
movq %rbx, 0x8(%rsp)
je 0x3bd4
leaq 0x10(%rsp), %r15
movl $0x40, %edx
movq %r15, %rdi
movq %rax, %rsi
callq 0x1090
movb (%r15), %al
testb %al, %al
je 0x3b44
leaq 0x11(%rsp), %rcx
leal -0x61(%rax), %edx
cmpb $0x19, %dl
ja 0x3b3b
addb $-0x20, %al
movb %al, -0x1(%rcx)
movb (%rcx), %al
incq %rcx
testb %al, %al
jne 0x3b2e
movl 0x10(%rsp), %eax
movl %eax, %edx
xorl $0x4452542e, %edx       # imm = 0x4452542E
movzbl 0x14(%rsp), %ecx
xorl %esi, %esi
orl %ecx, %edx
sete %sil
movl %eax, %edx
xorl $0x4c43532e, %edx       # imm = 0x4C43532E
orl %ecx, %edx
movl $0x2, %edx
cmovnel %esi, %edx
movl %eax, %esi
xorl $0x4944462e, %esi       # imm = 0x4944462E
orl %ecx, %esi
movl $0x3, %esi
cmovnel %edx, %esi
movl %eax, %edx
xorl $0x4944552e, %edx       # imm = 0x4944552E
orl %ecx, %edx
movl $0x5, %edx
cmovnel %esi, %edx
movl %eax, %esi
xorl $0x3044542e, %esi       # imm = 0x3044542E
orl %ecx, %esi
movl $0x4, %esi
cmovnel %edx, %esi
xorl $0x4444462e, %eax       # imm = 0x4444462E
orl %ecx, %eax
movl $0x7, %r12d
cmovnel %esi, %r12d
movzwl 0x10(%rsp), %eax
cmpl $0x242e, %eax           # imm = 0x242E
movl $0x6, %ecx
cmovel %ecx, %r12d
cmpl $0x212e, %eax           # imm = 0x212E
cmovel %ecx, %r12d
jmp 0x3bd7
xorl %r12d, %r12d
cmpl $0x6, 0x141000(%rbp)
jne 0x3bea
cmpl $0x6, %r12d
je 0x3c7b
movb $0x0, 0x145006(%rbp)
movw $0x100, 0x145004(%rbp)  # imm = 0x100
movl $0x0, 0x141000(%rbp)
leaq 0x40008(%rbp), %r15
xorl %r13d, %r13d
movq %rbp, %rbx
xorl %r14d, %r14d
movl $0x0, (%rbx,%r14)
movq -0x8(%r15,%r14,4), %rdi
testq %rdi, %rdi
je 0x3c30
movl $0x1, %esi
callq 0x1150
movq $0x0, -0x8(%r15,%r14,4)
movq (%r15,%r14,4), %rdi
testq %rdi, %rdi
je 0x3c4c
movl $0x1, %esi
callq 0x1150
movq $0x0, (%r15,%r14,4)
addq $0x4, %r14
cmpq $0x400, %r14            # imm = 0x400
jne 0x3c14
incq %r13
addq $0x400, %rbx            # imm = 0x400
addq $0x1000, %r15           # imm = 0x1000
cmpq $0x100, %r13            # imm = 0x100
jne 0x3c11
testl %r12d, %r12d
je 0x3cfb
leaq 0x140000(%rbp), %rdi
movq 0x8(%rsp), %r14
movq %r14, %rsi
callq 0x1120
movl %r12d, 0x141000(%rbp)
leaq 0x50(%rsp), %rsi
movq %r14, %rdi
callq 0x1110
testl %eax, %eax
jne 0x3da0
movq %r14, %rdi
xorl %esi, %esi
movl $0x1b6, %edx            # imm = 0x1B6
xorl %eax, %eax
callq 0x10d0
testl %eax, %eax
js 0x3d0c
movl %eax, %r15d
leal -0x1(%r12), %eax
cmpl $0x6, %eax
ja 0x3d87
leaq 0x45fb(%rip), %rcx      # 0x82d8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movzbl 0x4(%rsp), %edx
movq %rbp, %rdi
movl %r15d, %esi
callq 0x3db2
jmp 0x3d87
leaq 0x460f(%rip), %rsi      # 0x8311
callq 0x23fa
jmp 0x3da0
leaq 0x4624(%rip), %rdx      # 0x8337
leaq 0x50(%rsp), %rbx
movl $0x2000, %esi           # imm = 0x2000
movq %rbx, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x11d0
movq %rbx, %rsi
jmp 0x3d02
movzbl 0x4(%rsp), %edx
movq %rbp, %rdi
movl %r15d, %esi
callq 0x4984
jmp 0x3d87
movzbl 0x4(%rsp), %edx
movq %rbp, %rdi
movl %r15d, %esi
callq 0x61a6
jmp 0x3d87
movzbl 0x4(%rsp), %edx
movq %rbp, %rdi
movl %r15d, %esi
callq 0x42a6
jmp 0x3d87
movzbl 0x4(%rsp), %edx
movq %rbp, %rdi
movl %r15d, %esi
callq 0x5928
jmp 0x3d87
movzbl 0x4(%rsp), %edx
movq %rbp, %rdi
movl %r15d, %esi
callq 0x5446
cmpl $0x6, %r12d
jne 0x3d98
movq %rbp, %rdi
movl %r15d, %esi
callq 0x6a9e
movl %r15d, %edi
callq 0x1220
addq $0x2058, %rsp           # imm = 0x2058
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq