// File: 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
movq %rsp, %rbp
subq $0x20a0, %rsp           # imm = 0x20A0
movb %dl, %al
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
andb $0x1, %al
movb %al, -0x11(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x2088(%rbp)
callq 0x1e70
movl $0x0, -0x18(%rbp)
movq -0x10(%rbp), %rdi
callq 0x1ab0
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
je 0x4b48
leaq -0x60(%rbp), %rdi
movq -0x68(%rbp), %rsi
movl $0x40, %edx
callq 0x1080
leaq -0x60(%rbp), %rdi
callq 0x1ad0
leaq -0x60(%rbp), %rdi
leaq 0x974d(%rip), %rsi      # 0xe1bc
callq 0x1190
cmpl $0x0, %eax
jne 0x4a80
movl $0x1, -0x18(%rbp)
leaq -0x60(%rbp), %rdi
leaq 0x9736(%rip), %rsi      # 0xe1c1
callq 0x1190
cmpl $0x0, %eax
jne 0x4a9c
movl $0x2, -0x18(%rbp)
leaq -0x60(%rbp), %rdi
leaq 0x971f(%rip), %rsi      # 0xe1c6
callq 0x1190
cmpl $0x0, %eax
jne 0x4ab8
movl $0x3, -0x18(%rbp)
leaq -0x60(%rbp), %rdi
leaq 0x9708(%rip), %rsi      # 0xe1cb
callq 0x1190
cmpl $0x0, %eax
jne 0x4ad4
movl $0x5, -0x18(%rbp)
leaq -0x60(%rbp), %rdi
leaq 0x96f1(%rip), %rsi      # 0xe1d0
callq 0x1190
cmpl $0x0, %eax
jne 0x4af0
movl $0x4, -0x18(%rbp)
leaq -0x60(%rbp), %rdi
leaq 0x96da(%rip), %rsi      # 0xe1d5
callq 0x1190
cmpl $0x0, %eax
jne 0x4b0c
movl $0x7, -0x18(%rbp)
leaq -0x60(%rbp), %rax
movw (%rax), %ax
subw $0x242e, %ax            # imm = 0x242E
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x4b29
movl $0x6, -0x18(%rbp)
leaq -0x60(%rbp), %rax
movw (%rax), %ax
subw $0x212e, %ax            # imm = 0x212E
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x4b46
movl $0x6, -0x18(%rbp)
jmp 0x4b48
movq -0x2088(%rbp), %rax
cmpl $0x6, 0x141000(%rax)
jne 0x4b62
cmpl $0x6, -0x18(%rbp)
je 0x4d1b
movq -0x2088(%rbp), %rax
movb $0x1, 0x145005(%rax)
movb $0x0, 0x145006(%rax)
movb $0x0, 0x145004(%rax)
movl $0x0, 0x141000(%rax)
movl $0x0, -0x6c(%rbp)
cmpl $0x100, -0x6c(%rbp)     # imm = 0x100
jge 0x4d19
movl $0x0, -0x70(%rbp)
cmpl $0x100, -0x70(%rbp)     # imm = 0x100
jge 0x4d09
movq -0x2088(%rbp), %rax
movslq -0x6c(%rbp), %rdx
shlq $0xa, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movslq -0x70(%rbp), %rdx
movl $0x0, (%rcx,%rdx,4)
addq $0x40000, %rax          # imm = 0x40000
movslq -0x6c(%rbp), %rcx
shlq $0xc, %rcx
addq %rcx, %rax
movslq -0x70(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
cmpq $0x0, (%rax)
je 0x4c38
movq -0x2088(%rbp), %rax
addq $0x40000, %rax          # imm = 0x40000
movslq -0x6c(%rbp), %rcx
shlq $0xc, %rcx
addq %rcx, %rax
movslq -0x70(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq %rax, -0x2090(%rbp)
cmpq $0x0, %rax
je 0x4c36
movq -0x2090(%rbp), %rdi
movl $0x1, %esi
callq 0x1130
jmp 0x4c38
movq -0x2088(%rbp), %rax
movq %rax, %rcx
addq $0x40000, %rcx          # imm = 0x40000
movslq -0x6c(%rbp), %rdx
shlq $0xc, %rdx
addq %rdx, %rcx
movslq -0x70(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movq $0x0, (%rcx)
addq $0x40000, %rax          # imm = 0x40000
movslq -0x6c(%rbp), %rcx
shlq $0xc, %rcx
addq %rcx, %rax
movslq -0x70(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
cmpq $0x0, 0x8(%rax)
je 0x4cd0
movq -0x2088(%rbp), %rax
addq $0x40000, %rax          # imm = 0x40000
movslq -0x6c(%rbp), %rcx
shlq $0xc, %rcx
addq %rcx, %rax
movslq -0x70(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, -0x2098(%rbp)
cmpq $0x0, %rax
je 0x4cce
movq -0x2098(%rbp), %rdi
movl $0x1, %esi
callq 0x1130
jmp 0x4cd0
movq -0x2088(%rbp), %rax
addq $0x40000, %rax          # imm = 0x40000
movslq -0x6c(%rbp), %rcx
shlq $0xc, %rcx
addq %rcx, %rax
movslq -0x70(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq $0x0, 0x8(%rax)
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x4ba3
jmp 0x4d0b
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x4b8f
jmp 0x4d1b
cmpl $0x0, -0x18(%rbp)
jne 0x4d39
movq -0x2088(%rbp), %rdi
leaq 0x95ae(%rip), %rsi      # 0xe2dd
callq 0x2c30
jmp 0x4e8e
movq -0x2088(%rbp), %rdi
addq $0x140000, %rdi         # imm = 0x140000
movq -0x10(%rbp), %rsi
callq 0x1100
movq -0x2088(%rbp), %rax
movl -0x18(%rbp), %ecx
movl %ecx, 0x141000(%rax)
movq -0x10(%rbp), %rdi
callq 0x1a80
testb $0x1, %al
jne 0x4d72
jmp 0x4e8e
movq -0x10(%rbp), %rdi
xorl %esi, %esi
movl $0x1b6, %edx            # imm = 0x1B6
movb $0x0, %al
callq 0x10b0
movl %eax, -0x74(%rbp)
cmpl $0x0, -0x74(%rbp)
jge 0x4dc3
leaq -0x2080(%rbp), %rdi
movq -0x10(%rbp), %rcx
movl $0x2000, %esi           # imm = 0x2000
leaq 0x955f(%rip), %rdx      # 0xe303
movb $0x0, %al
callq 0x11c0
movq -0x2088(%rbp), %rdi
leaq -0x2080(%rbp), %rsi
callq 0x2c30
jmp 0x4e8e
cmpl $0x1, -0x18(%rbp)
jne 0x4de0
movq -0x2088(%rbp), %rdi
movl -0x74(%rbp), %esi
movb -0x11(%rbp), %al
andb $0x1, %al
movzbl %al, %edx
callq 0x4ea0
cmpl $0x2, -0x18(%rbp)
jne 0x4dfd
movq -0x2088(%rbp), %rdi
movl -0x74(%rbp), %esi
movb -0x11(%rbp), %al
andb $0x1, %al
movzbl %al, %edx
callq 0x59b0
cmpl $0x3, -0x18(%rbp)
jne 0x4e1a
movq -0x2088(%rbp), %rdi
movl -0x74(%rbp), %esi
movb -0x11(%rbp), %al
andb $0x1, %al
movzbl %al, %edx
callq 0x65e0
cmpl $0x5, -0x18(%rbp)
jne 0x4e37
movq -0x2088(%rbp), %rdi
movl -0x74(%rbp), %esi
movb -0x11(%rbp), %al
andb $0x1, %al
movzbl %al, %edx
callq 0x8010
cmpl $0x4, -0x18(%rbp)
jne 0x4e54
movq -0x2088(%rbp), %rdi
movl -0x74(%rbp), %esi
movb -0x11(%rbp), %al
andb $0x1, %al
movzbl %al, %edx
callq 0x88b0
cmpl $0x7, -0x18(%rbp)
jne 0x4e71
movq -0x2088(%rbp), %rdi
movl -0x74(%rbp), %esi
movb -0x11(%rbp), %al
andb $0x1, %al
movzbl %al, %edx
callq 0x9c70
cmpl $0x6, -0x18(%rbp)
jne 0x4e86
movq -0x2088(%rbp), %rdi
movl -0x74(%rbp), %esi
callq 0xb470
movl -0x74(%rbp), %edi
callq 0x1200
addq $0x20a0, %rsp           # imm = 0x20A0
popq %rbp
retq
nopw (%rax,%rax)