// File: /workspace/github_cpp_makefile_star2_O3/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, %ebp
movq %rsi, %r14
movq %rdi, %rbx
callq 0x1d0a
movq %r14, %rdi
movl $0x2e, %esi
callq 0x1160
testq %rax, %rax
movl %ebp, 0xc(%rsp)
je 0x3c56
leaq 0x20(%rsp), %r15
movl $0x40, %edx
movq %r15, %rdi
movq %rax, %rsi
callq 0x1090
movb (%r15), %al
testb %al, %al
je 0x3bab
leaq 0x21(%rsp), %rcx
leal -0x61(%rax), %edx
cmpb $0x19, %dl
ja 0x3ba2
addb $-0x20, %al
movb %al, -0x1(%rcx)
movb (%rcx), %al
incq %rcx
testb %al, %al
jne 0x3b95
movl 0x20(%rsp), %eax
movl %eax, %edx
xorl $0x4452542e, %edx       # imm = 0x4452542E
movzbl 0x24(%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, %r15d
cmovnel %esi, %r15d
movzwl 0x20(%rsp), %eax
cmpl $0x242e, %eax           # imm = 0x242E
sete %cl
cmpl $0x212e, %eax           # imm = 0x212E
sete %r13b
orb %cl, %r13b
movl $0x6, %ebp
cmovnel %ebp, %r15d
cmpl $0x6, 0x141000(%rbx)
sete %al
movb $0x1, %r12b
testb %r13b, %al
je 0x3c5c
jmp 0x3d08
xorl %r13d, %r13d
xorl %r15d, %r15d
movq %r14, 0x10(%rsp)
movb $0x0, 0x145006(%rbx)
movw $0x100, 0x145004(%rbx)  # imm = 0x100
movl $0x0, 0x141000(%rbx)
leaq 0x40008(%rbx), %r12
xorl %ebp, %ebp
movq %rbx, 0x18(%rsp)
xorl %r14d, %r14d
movl $0x0, (%rbx,%r14)
movq -0x8(%r12,%r14,4), %rdi
testq %rdi, %rdi
je 0x3ca8
movl $0x1, %esi
callq 0x1150
movq $0x0, -0x8(%r12,%r14,4)
movq (%r12,%r14,4), %rdi
testq %rdi, %rdi
je 0x3cc4
movl $0x1, %esi
callq 0x1150
movq $0x0, (%r12,%r14,4)
addq $0x4, %r14
cmpq $0x400, %r14            # imm = 0x400
jne 0x3c8c
incq %rbp
addq $0x400, %rbx            # imm = 0x400
addq $0x1000, %r12           # imm = 0x1000
cmpq $0x100, %rbp            # imm = 0x100
jne 0x3c89
movl %r15d, %ebp
movl %r13d, %r12d
testl %r15d, %r15d
movq 0x18(%rsp), %rbx
movq 0x10(%rsp), %r14
je 0x3d7a
leaq 0x140000(%rbx), %rdi
movq %r14, %rsi
callq 0x1120
movl %ebp, 0x141000(%rbx)
leaq 0x60(%rsp), %rsi
movq %r14, %rdi
callq 0x1110
testl %eax, %eax
jne 0x3e1b
movq %r14, %rdi
xorl %esi, %esi
movl $0x1b6, %edx            # imm = 0x1B6
xorl %eax, %eax
callq 0x10d0
testl %eax, %eax
js 0x3d83
movl %eax, %r15d
decl %ebp
cmpl $0x6, %ebp
ja 0x3e03
leaq 0x457c(%rip), %rax      # 0x82d8
movslq (%rax,%rbp,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movzbl 0xc(%rsp), %edx
movq %rbx, %rdi
movl %r15d, %esi
callq 0x3e2e
jmp 0x3e03
leaq 0x45ec(%rip), %rsi      # 0x836d
jmp 0x3da4
leaq 0x4609(%rip), %rdx      # 0x8393
leaq 0x60(%rsp), %rbx
movl $0x2000, %esi           # imm = 0x2000
movq %rbx, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x11d0
movq %rbx, %rsi
callq 0x2410
jmp 0x3e1b
movzbl 0xc(%rsp), %edx
movq %rbx, %rdi
movl %r15d, %esi
callq 0x498a
jmp 0x3e03
movzbl 0xc(%rsp), %edx
movq %rbx, %rdi
movl %r15d, %esi
callq 0x6154
jmp 0x3e03
movzbl 0xc(%rsp), %edx
movq %rbx, %rdi
movl %r15d, %esi
callq 0x4322
jmp 0x3e03
movzbl 0xc(%rsp), %edx
movq %rbx, %rdi
movl %r15d, %esi
callq 0x58e4
jmp 0x3e03
movzbl 0xc(%rsp), %edx
movq %rbx, %rdi
movl %r15d, %esi
callq 0x545e
testb %r12b, %r12b
je 0x3e13
movq %rbx, %rdi
movl %r15d, %esi
callq 0x6a5a
movl %r15d, %edi
callq 0x1220
addq $0x2068, %rsp           # imm = 0x2068
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop