// File: /workspace/github_cpp_makefile_star2_O3/atsidaev[P]trx2x/DiskImage.cpp
// Function: TDiskImage::readTRD(int, bool)
void TDiskImage::readTRD(int hfile, bool readonly)
{
   long fsize = filelength(hfile);
   if(fsize < 0)
   {
      ShowError(ERR_GETLEN);
      return;
   }

   unsigned char *ptr = (unsigned char*)new char[fsize+1024*2048];
   if(!ptr)
   {
      ShowError(ERR_NOMEM);
      return;
   }

   unsigned long rsize = read(hfile, ptr, fsize);

   if(!rsize)
   {
      delete ptr;
      ShowError(ERR_CORRUPT);
      return;
   }

   if(rsize % (256*16*2))
   {
      delete ptr;
      ShowError(ERR_UNKFORMAT);
      return;
   }
   MaxSide = 1;
   MaxTrack = (rsize / (256*16*2)) - 1;


// форматирование нового диска под TR-DOS...

   unsigned short vgcrc;
   for(unsigned int trk=0; trk <= unsigned(MaxTrack); trk++)
      for(unsigned int side=0; side <= unsigned(MaxSide); side++)
      {
         FTrackLength[trk][side] = 6250;

         FTracksPtr[trk][side][0] = (unsigned char*)new char[FTrackLength[trk][side]+1024];      // trk img
         FTracksPtr[trk][side][1] = (unsigned char*)new char[FTrackLength[trk][side]+1024];      // clk img


         unsigned int r;
         unsigned int tptr=0;
         unsigned int ptrcrc;

         for(int sec=0; sec < 16; sec++)
         {
            for(r=0; r < 10; r++)        // Первый пробел
            {
               FTracksPtr[trk][side][0][tptr] = 0x4E;
               FTracksPtr[trk][side][1][tptr++] = 0x00;
            }
            for(r=0; r < 12; r++)        // Синхропромежуток
            {
               FTracksPtr[trk][side][0][tptr] = 0x00;
               FTracksPtr[trk][side][1][tptr++] = 0x00;
            }
            ptrcrc = tptr;
            for(r=0; r < 3; r++)        // Синхроимпульс
            {
               FTracksPtr[trk][side][0][tptr] = 0xA1;
               FTracksPtr[trk][side][1][tptr++] = 0xFF;
            }
            FTracksPtr[trk][side][0][tptr] = 0xFE;   // Метка "Адрес"
            FTracksPtr[trk][side][1][tptr++] = 0x00;

            FTracksPtr[trk][side][0][tptr] = (unsigned char)trk; // cyl
            FTracksPtr[trk][side][1][tptr++] = 0x00;
            FTracksPtr[trk][side][0][tptr] = (unsigned char)0x00; // head (TR always 0)
            FTracksPtr[trk][side][1][tptr++] = 0x00;
            FTracksPtr[trk][side][0][tptr] = (unsigned char)(sec+1); // secN
            FTracksPtr[trk][side][1][tptr++] = 0x00;
            FTracksPtr[trk][side][0][tptr] = (unsigned char)0x01; // len=256b
            FTracksPtr[trk][side][1][tptr++] = 0x00;

            vgcrc = MakeVGCRC(FTracksPtr[trk][side][0]+ptrcrc, tptr-ptrcrc);
            FTracksPtr[trk][side][0][tptr] = (unsigned char)(vgcrc>>8); // VG93 CRC
            FTracksPtr[trk][side][1][tptr++] = 0x00;
            FTracksPtr[trk][side][0][tptr] = (unsigned char)(vgcrc&0xFF);
            FTracksPtr[trk][side][1][tptr++] = 0x00;

            for(r=0; r < 22; r++)        // Второй пробел
            {
               FTracksPtr[trk][side][0][tptr] = 0x4E;
               FTracksPtr[trk][side][1][tptr++] = 0x00;
            }
            for(r=0; r < 12; r++)        // Синхропромежуток
            {
               FTracksPtr[trk][side][0][tptr] = 0x00;
               FTracksPtr[trk][side][1][tptr++] = 0x00;
            }
            ptrcrc = tptr;
            for(r=0; r < 3; r++)        // Синхроимпульс
            {
               FTracksPtr[trk][side][0][tptr] = 0xA1;
               FTracksPtr[trk][side][1][tptr++] = 0xFF;
            }
            FTracksPtr[trk][side][0][tptr] = 0xFB;   // Метка "Данные"
            FTracksPtr[trk][side][1][tptr++] = 0x00;

            for(r=0; r < 256; r++)        // сектор 256байт
            {
               FTracksPtr[trk][side][0][tptr] = ptr[(trk*2+side)*4096+sec*256+r];
               FTracksPtr[trk][side][1][tptr++] = 0x00;
            }
            vgcrc = MakeVGCRC(FTracksPtr[trk][side][0]+ptrcrc, tptr-ptrcrc);
            FTracksPtr[trk][side][0][tptr] = (unsigned char)(vgcrc>>8); // VG93 CRC
            FTracksPtr[trk][side][1][tptr++] = 0x00;
            FTracksPtr[trk][side][0][tptr] = (unsigned char)(vgcrc&0xFF);
            FTracksPtr[trk][side][1][tptr++] = 0x00;

            for(r=0; r < 60; r++)        // Третий пробел
            {
               FTracksPtr[trk][side][0][tptr] = 0x4E;
               FTracksPtr[trk][side][1][tptr++] = 0x00;
            }
         }
         for(int eoftrk=tptr; eoftrk < 6250; eoftrk++) 
         {
            FTracksPtr[trk][side][0][tptr] = 0x4E;
            FTracksPtr[trk][side][1][tptr++] = 0x00;
         }
      }

   delete ptr;
   ReadOnly = readonly;
   FType = DIT_TRD;
   DiskPresent = true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %edx, %ebp
movl %esi, %r15d
movq %rdi, %rbx
movl %esi, %edi
xorl %esi, %esi
movl $0x1, %edx
callq 0x1210
movq %rax, %r14
movl %r15d, %edi
xorl %esi, %esi
movl $0x2, %edx
callq 0x1210
movq %rax, %r12
movl %r15d, %edi
movq %r14, %rsi
xorl %edx, %edx
callq 0x1210
testq %r12, %r12
js 0x3ebd
movq %r12, %rdi
addq $0x200000, %rdi         # imm = 0x200000
callq 0x1030
movq %rax, %r14
movl %r15d, %edi
movq %rax, %rsi
movq %r12, %rdx
callq 0x11b0
testq %rax, %rax
je 0x3ec6
movq %rax, %rcx
shlq $0x33, %rcx
je 0x3eed
movl $0x1, %esi
movq %r14, %rdi
callq 0x1150
leaq 0x44b2(%rip), %rsi      # 0x836d
jmp 0x3eda
leaq 0x44f0(%rip), %rsi      # 0x83b4
jmp 0x3eda
movl $0x1, %esi
movq %r14, %rdi
callq 0x1150
leaq 0x44f8(%rip), %rsi      # 0x83d2
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2410
movl %ebp, 0x4(%rsp)
movb $0x1, 0x145008(%rbx)
shrl $0xd, %eax
decb %al
movb %al, 0x145007(%rbx)
leaq 0x40000(%rbx), %rax
movq %rax, 0x10(%rsp)
xorl %r12d, %r12d
movq %r14, 0x8(%rsp)
xorl %ebp, %ebp
movq %rbx, 0x20(%rsp)
movq %rbp, %rax
shlq $0xa, %rax
addq %rbx, %rax
movq %rax, 0x30(%rsp)
movq %rbp, %rax
shlq $0xc, %rax
addq 0x10(%rsp), %rax
movq %rax, 0x28(%rsp)
movq %r14, 0x18(%rsp)
movq %r14, %r13
xorl %r14d, %r14d
movq 0x30(%rsp), %rax
movl $0x186a, (%rax,%r14,4)  # imm = 0x186A
movl $0x1c6a, %edi           # imm = 0x1C6A
callq 0x1030
movq %r14, 0x38(%rsp)
movq %r14, %rbx
shlq $0x4, %rbx
movq 0x28(%rsp), %r15
leaq (%r15,%rbx), %r14
movq %rax, (%rbx,%r15)
movl $0x1c6a, %edi           # imm = 0x1C6A
callq 0x1030
movq %rax, 0x8(%rbx,%r15)
xorl %r10d, %r10d
movq %r13, 0x40(%rsp)
movq %r13, %rcx
xorl %edx, %edx
leal 0x9(%rdx), %r9d
leal 0xd(%rdx), %r11d
leal 0x10(%rdx), %ebx
xorl %esi, %esi
movl %r9d, %eax
movl %r11d, %edi
movl %ebx, %r8d
leal (%rdx,%rsi), %r9d
movq (%r14), %r11
movb $0x4e, (%r11,%r9)
movq 0x8(%r14), %r11
movb $0x0, (%r11,%r9)
incl %esi
leal 0x1(%rax), %r9d
leal 0x1(%rdi), %r11d
leal 0x1(%r8), %ebx
cmpl $0xa, %esi
jne 0x3fa2
addl %esi, %edx
movl $0xc, %r9d
movl %eax, %esi
movl %edi, %r11d
movl %r8d, %ebx
movq (%r14), %rax
movl %edx, %edi
movb %r12b, (%rax,%rdi)
movq 0x8(%r14), %rax
incl %edx
movb %r12b, (%rax,%rdi)
leal 0x1(%rsi), %eax
leal 0x1(%r11), %edi
leal 0x1(%rbx), %r8d
decl %r9d
jne 0x3fdb
movl $0x3, %r9d
movl %r11d, %edi
movl %ebx, %r8d
leal -0x7(%rsi), %eax
movq (%r14), %r11
movb $-0x5f, (%r11,%rax)
movq 0x8(%r14), %r11
movb $-0x1, (%r11,%rax)
incl %esi
leal 0x1(%rdi), %r11d
leal 0x1(%r8), %ebx
decl %r9d
jne 0x400c
movq (%r14), %rax
leal -0x7(%rsi), %r11d
movb $-0x2, (%rax,%r11)
movq 0x8(%r14), %rax
leal -0x6(%rsi), %ebx
movb %r12b, (%rax,%r11)
movq (%r14), %rax
movb %bpl, (%rax,%rbx)
movq 0x8(%r14), %rax
leal -0x5(%rsi), %r11d
movb %r12b, (%rax,%rbx)
movq (%r14), %rax
movb %r12b, (%rax,%r11)
movq 0x8(%r14), %rax
leal -0x4(%rsi), %ebx
movb %r12b, (%rax,%r11)
incq %r10
movq (%r14), %rax
movb %r10b, (%rax,%rbx)
movq 0x8(%r14), %rax
leal -0x3(%rsi), %r11d
movb %r12b, (%rax,%rbx)
movq (%r14), %rax
movb $0x1, (%rax,%r11)
movq 0x8(%r14), %rax
movb %r12b, (%rax,%r11)
movq (%r14), %rbx
movw $0xffff, %ax            # imm = 0xFFFF
cmpl $0x8, %r9d
je 0x40e8
movl %edx, %edx
addq %rbx, %rdx
movw $0xffff, %ax            # imm = 0xFFFF
xorl %r9d, %r9d
movzbl (%rdx,%r9), %r11d
shll $0x8, %r11d
xorl %r11d, %eax
movl $0x8, %r11d
leal (%rax,%rax), %r15d
movl %r15d, %r13d
xorl $0x1021, %r13d          # imm = 0x1021
testw %ax, %ax
cmovnsl %r15d, %r13d
movl %r13d, %eax
decl %r11d
jne 0x40c2
incq %r9
cmpq $0x8, %r9
jne 0x40b0
leal -0x2(%rsi), %edx
movl %edx, %edx
movb %ah, (%rbx,%rdx)
movq 0x8(%r14), %r9
leal -0x1(%rsi), %r11d
movb %r12b, (%r9,%rdx)
movq (%r14), %rdx
movb %al, (%rdx,%r11)
movq 0x8(%r14), %rax
movb %r12b, (%rax,%r11)
movl $0x16, %r11d
movl %esi, %r9d
movl %edi, %eax
movl %r8d, %edx
movq (%r14), %rsi
movl %r9d, %edi
movb $0x4e, (%rsi,%rdi)
movq 0x8(%r14), %r8
leal 0x1(%r9), %esi
movb $0x0, (%r8,%rdi)
leal 0x1(%rax), %edi
leal 0x1(%rdx), %r8d
decl %r11d
jne 0x4111
movl $0xc, %r8d
movl %eax, %esi
movl %edx, %edi
incl %r9d
movq (%r14), %rax
movb %r12b, (%rax,%r9)
movq 0x8(%r14), %rax
movb %r12b, (%rax,%r9)
leal 0x1(%rsi), %eax
leal 0x1(%rdi), %edx
decl %r8d
jne 0x4142
incl %r9d
movl $0x3, %r8d
movl $0xfffffffe, %r11d      # imm = 0xFFFFFFFE
movl %r11d, %eax
movl %edi, %edx
leal -0x1(%rsi), %edi
movq (%r14), %r11
movb $-0x5f, (%r11,%rdi)
movq 0x8(%r14), %r11
movb $-0x1, (%r11,%rdi)
incl %esi
leal -0x1(%rax), %r11d
leal 0x1(%rdx), %edi
decl %r8d
jne 0x4172
movq (%r14), %rdi
decl %esi
movb $-0x5, (%rdi,%rsi)
movq 0x8(%r14), %rdi
movb $0x0, (%rdi,%rsi)
xorl %esi, %esi
leal -0x2(%rdx), %edi
movb (%rcx,%rsi), %r8b
movq (%r14), %r11
movb %r8b, (%r11,%rdi)
movq 0x8(%r14), %r8
movb $0x0, (%r8,%rdi)
incq %rsi
decl %eax
incl %edx
cmpq $0x100, %rsi            # imm = 0x100
jne 0x41ac
movq (%r14), %rsi
testl %eax, %eax
je 0x4222
movl %r9d, %edi
addq %rsi, %rdi
movw $0xffff, %ax            # imm = 0xFFFF
xorl %r8d, %r8d
movzbl (%rdi,%r8), %r9d
shll $0x8, %r9d
xorl %r9d, %eax
movl $0x8, %r9d
leal (%rax,%rax), %r11d
movl %r11d, %ebx
xorl $0x1021, %ebx           # imm = 0x1021
testw %ax, %ax
cmovnsl %r11d, %ebx
movl %ebx, %eax
decl %r9d
jne 0x41f9
incq %r8
cmpq $0x104, %r8             # imm = 0x104
jne 0x41e7
jmp 0x4226
movw $0xffff, %ax            # imm = 0xFFFF
leal -0x2(%rdx), %edi
movl %edi, %edi
movb %ah, (%rsi,%rdi)
movq 0x8(%r14), %rsi
leal -0x1(%rdx), %r8d
movb %r12b, (%rsi,%rdi)
movq (%r14), %rsi
movb %al, (%rsi,%r8)
movq 0x8(%r14), %rax
movb %r12b, (%rax,%r8)
movl $0x3c, %eax
movq (%r14), %rsi
movl %edx, %edi
movb $0x4e, (%rsi,%rdi)
movq 0x8(%r14), %rsi
incl %edx
movb $0x0, (%rsi,%rdi)
decl %eax
jne 0x424e
addq $0x100, %rcx            # imm = 0x100
cmpq $0x10, %r10
jne 0x3f95
cmpl $0x1869, %edx           # imm = 0x1869
movq 0x20(%rsp), %rbx
jg 0x42a1
movq (%r14), %rax
movl %edx, %ecx
movb $0x4e, (%rax,%rcx)
movq 0x8(%r14), %rax
leal 0x1(%rdx), %esi
movb $0x0, (%rax,%rcx)
cmpl $0x1869, %edx           # imm = 0x1869
movl %esi, %edx
jl 0x4283
movq 0x38(%rsp), %rdx
leaq 0x1(%rdx), %r14
movzbl 0x145008(%rbx), %ecx
movq 0x40(%rsp), %r13
addq $0x1000, %r13           # imm = 0x1000
cmpq %rcx, %rdx
jb 0x3f49
leaq 0x1(%rbp), %rax
movzbl 0x145007(%rbx), %ecx
movq 0x18(%rsp), %r14
addq $0x2000, %r14           # imm = 0x2000
cmpq %rcx, %rbp
movq %rax, %rbp
jb 0x3f1e
movl $0x1, %esi
movq 0x8(%rsp), %rdi
callq 0x1150
movl 0x4(%rsp), %eax
movb %al, 0x145005(%rbx)
movl $0x1, 0x141000(%rbx)
movb $0x1, 0x145006(%rbx)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq