// File: /workspace/github_cpp_makefile_star2_O2/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 $0x2068, %rsp           # imm = 0x2068
movl %edx, %ebx
movq %rsi, %r13
movq %rdi, %r14
callq 0x19a4
movq %r13, %rdi
callq 0x184d
testq %rax, %rax
movl %ebx, 0xc(%rsp)
je 0x338c
leaq 0x20(%rsp), %r15
pushq $0x40
popq %rdx
movq %r15, %rdi
movq %rax, %rsi
callq 0x1080
movq %r15, %rdi
callq 0x1855
leaq 0x3f03(%rip), %rsi      # 0x71d8
pushq $0x5
popq %r12
movq %r15, %rdi
movq %r12, %rdx
callq 0x1150
xorl %ebx, %ebx
testl %eax, %eax
sete %bl
leaq 0x3eeb(%rip), %rsi      # 0x71dd
movq %r15, %rdi
movq %r12, %rdx
callq 0x1150
testl %eax, %eax
pushq $0x2
popq %rbp
cmovnel %ebx, %ebp
leaq 0x3ed6(%rip), %rsi      # 0x71e2
movq %r15, %rdi
movq %r12, %rdx
callq 0x1150
testl %eax, %eax
pushq $0x3
popq %rbx
cmovnel %ebp, %ebx
leaq 0x3ec1(%rip), %rsi      # 0x71e7
movq %r15, %rdi
movq %r12, %rdx
callq 0x1150
testl %eax, %eax
pushq $0x5
popq %rbp
cmovnel %ebx, %ebp
leaq 0x3eac(%rip), %rsi      # 0x71ec
movq %r15, %rdi
movq %r12, %rdx
callq 0x1150
testl %eax, %eax
pushq $0x4
popq %rbx
cmovnel %ebp, %ebx
leaq 0x3e97(%rip), %rsi      # 0x71f1
movq %r15, %rdi
movq %r12, %rdx
callq 0x1150
testl %eax, %eax
pushq $0x7
popq %r12
cmovnel %ebx, %r12d
movl 0xc(%rsp), %ebx
movzwl (%r15), %eax
cmpw $0x242e, %ax            # imm = 0x242E
pushq $0x6
popq %rcx
cmovel %ecx, %r12d
cmpw $0x212e, %ax            # imm = 0x212E
cmovel %ecx, %r12d
jmp 0x338f
xorl %r12d, %r12d
cmpl $0x6, 0x141000(%r14)
jne 0x33a3
cmpl $0x6, %r12d
je 0x344d
movq %r13, 0x10(%rsp)
movb $0x0, 0x145006(%r14)
movw $0x100, 0x145004(%r14)  # imm = 0x100
andl $0x0, 0x141000(%r14)
leaq 0x40008(%r14), %r13
xorl %ebp, %ebp
pushq $0x1
popq %r15
movq %r14, 0x18(%rsp)
movq %r14, %rbx
cmpq $0x100, %rbp            # imm = 0x100
je 0x343a
xorl %r14d, %r14d
cmpq $0x400, %r14            # imm = 0x400
je 0x3427
andl $0x0, (%rbx,%r14)
movq -0x8(%r13,%r14,4), %rdi
testq %rdi, %rdi
je 0x3403
movq %r15, %rsi
callq 0x1140
andq $0x0, -0x8(%r13,%r14,4)
movq (%r13,%r14,4), %rdi
testq %rdi, %rdi
je 0x341b
movq %r15, %rsi
callq 0x1140
andq $0x0, (%r13,%r14,4)
addq $0x4, %r14
jmp 0x33e3
incq %rbp
addq $0x400, %rbx            # imm = 0x400
addq $0x1000, %r13           # imm = 0x1000
jmp 0x33d7
testl %r12d, %r12d
movq 0x18(%rsp), %r14
movl 0xc(%rsp), %ebx
movq 0x10(%rsp), %r13
je 0x34b8
leaq 0x140000(%r14), %rdi
movq %r13, %rsi
callq 0x1110
movl %r12d, 0x141000(%r14)
movq %r13, %rdi
callq 0x182f
testb %al, %al
je 0x3552
movq %r13, %rdi
xorl %esi, %esi
movl $0x1b6, %edx            # imm = 0x1B6
xorl %eax, %eax
callq 0x10c0
testl %eax, %eax
js 0x34c1
movl %eax, %r15d
leal -0x1(%r12), %eax
cmpl $0x6, %eax
ja 0x3509
leaq 0x3e3c(%rip), %rcx      # 0x72d8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movzbl %bl, %edx
movq %r14, %rdi
movl %r15d, %esi
callq 0x3564
jmp 0x354a
leaq 0x3e52(%rip), %rsi      # 0x7311
jmp 0x34e2
leaq 0x3e6f(%rip), %rdx      # 0x7337
leaq 0x60(%rsp), %rbx
movl $0x2000, %esi           # imm = 0x2000
movq %rbx, %rdi
movq %r13, %rcx
xorl %eax, %eax
callq 0x11c0
movq %rbx, %rsi
callq 0x201c
jmp 0x3552
movzbl %bl, %edx
movq %r14, %rdi
movl %r15d, %esi
callq 0x3f88
jmp 0x354a
movzbl %bl, %edx
movq %r14, %rdi
movl %r15d, %esi
callq 0x5500
jmp 0x354a
cmpl $0x6, %r12d
jne 0x354a
movq %r14, %rdi
movl %r15d, %esi
callq 0x5d22
jmp 0x354a
movzbl %bl, %edx
movq %r14, %rdi
movl %r15d, %esi
callq 0x39a8
jmp 0x354a
movzbl %bl, %edx
movq %r14, %rdi
movl %r15d, %esi
callq 0x4d98
jmp 0x354a
movzbl %bl, %edx
movq %r14, %rdi
movl %r15d, %esi
callq 0x4994
movl %r15d, %edi
callq 0x1200
addq $0x2068, %rsp           # imm = 0x2068
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq