// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movl %edx, %ebx
movl %esi, %r15d
movq %rdi, (%rsp)
movl %esi, %edi
callq 0x6db6
testq %rax, %rax
js 0x35cc
movq %rax, %r12
movq %rax, %rdi
addq $0x200000, %rdi         # imm = 0x200000
callq 0x1030
movq %rax, %r14
movl %r15d, %edi
movq %rax, %rsi
movq %r12, %rdx
callq 0x11a0
testq %rax, %rax
je 0x35d5
movq %rax, %rcx
shlq $0x33, %rcx
je 0x35fa
pushq $0x1
popq %rsi
movq %r14, %rdi
callq 0x1140
leaq 0x3d47(%rip), %rsi      # 0x7311
jmp 0x35e7
leaq 0x3d85(%rip), %rsi      # 0x7358
jmp 0x35e7
pushq $0x1
popq %rsi
movq %r14, %rdi
callq 0x1140
leaq 0x3d8f(%rip), %rsi      # 0x7376
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x201c
movl %ebx, 0xc(%rsp)
movq (%rsp), %rcx
movb $0x1, 0x145008(%rcx)
shrl $0xd, %eax
decb %al
movb %al, 0x145007(%rcx)
addq $0x40000, %rcx          # imm = 0x40000
movq %rcx, 0x28(%rsp)
movb $0x1, %cl
xorl %r13d, %r13d
movq %r14, 0x20(%rsp)
movq %r14, 0x10(%rsp)
xorl %ebp, %ebp
movzbl %al, %eax
cmpq %rax, %rbp
ja 0x396c
movq %rbp, %rax
shlq $0xa, %rax
addq (%rsp), %rax
movq %rax, 0x38(%rsp)
movq %rbp, %rax
shlq $0xc, %rax
addq 0x28(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x10(%rsp), %rax
movq %rax, 0x18(%rsp)
xorl %ebx, %ebx
movq %rbp, 0x48(%rsp)
movzbl %cl, %eax
cmpq %rax, %rbx
ja 0x3951
movq 0x38(%rsp), %rax
movl $0x186a, (%rax,%rbx,4)  # imm = 0x186A
movl $0x1c6a, %edi           # imm = 0x1C6A
callq 0x1030
movq %rbx, 0x40(%rsp)
shlq $0x4, %rbx
movq 0x30(%rsp), %r14
leaq (%r14,%rbx), %r12
movq %rax, (%rbx,%r14)
movl $0x1c6a, %edi           # imm = 0x1C6A
callq 0x1030
movq %rax, 0x8(%rbx,%r14)
xorl %r8d, %r8d
movq 0x18(%rsp), %r14
xorl %eax, %eax
cmpq $0x10, %r8
je 0x3913
leal 0xa(%rax), %ebx
xorl %ecx, %ecx
leal (%rax,%rcx), %r15d
cmpl $0xa, %ecx
je 0x36f2
movq (%r12), %rdx
movl %r15d, %esi
movb $0x4e, (%rdx,%rsi)
movq 0x8(%r12), %rdx
movb $0x0, (%rdx,%rsi)
incl %ecx
incl %ebx
jmp 0x36cf
pushq $0xc
popq %rax
movl %r15d, %esi
subl $0x1, %eax
jb 0x3715
movq (%r12), %rcx
movb %r13b, (%rcx,%rsi)
movq 0x8(%r12), %rcx
incl %r15d
movb %r13b, (%rcx,%rsi)
incl %ebx
jmp 0x36f5
movq %r14, 0x58(%rsp)
xorl %r14d, %r14d
pushq $0x5
popq %rdx
leal (%r15,%r14), %eax
movq (%r12), %rcx
cmpl $0x3, %r14d
je 0x3745
movb $-0x5f, (%rcx,%rax)
movq 0x8(%r12), %rcx
movb $-0x1, (%rcx,%rax)
incl %r14d
incq %rdx
incl %ebx
jmp 0x3720
movb $-0x2, (%rcx,%rax)
movq 0x8(%r12), %rcx
movb %r13b, (%rcx,%rax)
movq (%r12), %rax
leal (%r15,%r14), %ecx
incl %ecx
movb %bpl, (%rax,%rcx)
movq 0x8(%r12), %rax
leal 0x2(%r15,%r14), %edi
movb %r13b, (%rax,%rcx)
movq (%r12), %rax
movb %r13b, (%rax,%rdi)
movq 0x8(%r12), %rax
leal (%r15,%r14), %ecx
addl $0x3, %ecx
movb %r13b, (%rax,%rdi)
incq %r8
movq (%r12), %rax
movq %r8, 0x60(%rsp)
movb %r8b, (%rax,%rcx)
movq 0x8(%r12), %rax
leal 0x4(%r15,%r14), %edi
movb %r13b, (%rax,%rcx)
movq (%r12), %rax
movb $0x1, (%rax,%rdi)
movq 0x8(%r12), %rax
leal 0x5(%r15,%r14), %ecx
movq %rcx, 0x50(%rsp)
movb %r13b, (%rax,%rdi)
movq (%r12), %rbp
addq %rbp, %rsi
callq 0x1a98
movq 0x50(%rsp), %rsi
movb %ah, (%rbp,%rsi)
movq 0x8(%r12), %rcx
leal 0x6(%r15,%r14), %edx
movb %r13b, (%rcx,%rsi)
movq (%r12), %rcx
movb %al, (%rcx,%rdx)
movq 0x8(%r12), %rax
leal 0x7(%r15,%r14), %ecx
movb %r13b, (%rax,%rdx)
pushq $0x16
popq %rax
movq 0x58(%rsp), %r14
subl $0x1, %eax
jb 0x381d
movq (%r12), %rdx
movl %ecx, %esi
movb $0x4e, (%rdx,%rsi)
movq 0x8(%r12), %rdx
incl %ecx
movb $0x0, (%rdx,%rsi)
incl %ebx
jmp 0x37ff
xorl %edx, %edx
leal (%rcx,%rdx), %eax
cmpl $0xc, %edx
je 0x3840
movq (%r12), %rsi
movl %eax, %eax
movb %r13b, (%rsi,%rax)
movq 0x8(%r12), %rsi
movb %r13b, (%rsi,%rax)
incl %edx
incl %ebx
jmp 0x381f
pushq $0x1
popq %rdx
pushq $0x3
popq %rcx
movl %eax, %esi
movq (%r12), %r8
movl %esi, %edi
subl $0x1, %ecx
jb 0x386c
movb $-0x5f, (%r8,%rdi)
movq 0x8(%r12), %r8
leal 0x1(%rdi), %esi
movb $-0x1, (%r8,%rdi)
incl %ebx
incq %rdx
jmp 0x3848
movb $-0x5, (%r8,%rdi)
movq 0x8(%r12), %rcx
movb $0x0, (%rcx,%rdi)
xorl %ecx, %ecx
leal -0x2(%rbx), %ebp
cmpq $0x100, %rcx            # imm = 0x100
je 0x38ab
movb (%r14,%rcx), %sil
movq (%r12), %rdi
movl %ebp, %r8d
movb %sil, (%rdi,%r8)
movq 0x8(%r12), %rsi
movb $0x0, (%rsi,%r8)
incq %rcx
incl %ebx
incq %rdx
jmp 0x387c
movq (%r12), %rcx
movl %eax, %esi
addq %rcx, %rsi
movq %rcx, %r15
callq 0x1a98
movq %r15, %rcx
movb %ah, (%rcx,%rbp)
movq 0x8(%r12), %rdx
leal -0x1(%rbx), %esi
xorl %ecx, %ecx
movb %cl, (%rdx,%rbp)
movq (%r12), %rdx
movb %al, (%rdx,%rsi)
movq 0x8(%r12), %rax
movb %cl, (%rax,%rsi)
movq 0x48(%rsp), %rbp
movq 0x60(%rsp), %r8
leal (%rbx,%rcx), %eax
cmpl $0x3c, %ecx
je 0x3907
movq (%r12), %rdx
movl %eax, %eax
movb $0x4e, (%rdx,%rax)
movq 0x8(%r12), %rdx
movb $0x0, (%rdx,%rax)
incl %ecx
jmp 0x38e8
addq $0x100, %r14            # imm = 0x100
jmp 0x36c0
movq 0x40(%rsp), %rbx
cmpl $0x186a, %eax           # imm = 0x186A
jge 0x3936
movq (%r12), %rcx
movl %eax, %edx
movb $0x4e, (%rcx,%rdx)
movq 0x8(%r12), %rcx
incl %eax
movb $0x0, (%rcx,%rdx)
jmp 0x3918
incq %rbx
movq (%rsp), %rax
movb 0x145008(%rax), %cl
addq $0x1000, 0x18(%rsp)     # imm = 0x1000
jmp 0x366f
incq %rbp
movq (%rsp), %rax
movb 0x145007(%rax), %al
addq $0x2000, 0x10(%rsp)     # imm = 0x2000
jmp 0x3631
pushq $0x1
popq %rsi
movq 0x20(%rsp), %rdi
callq 0x1140
movq (%rsp), %rax
movl 0xc(%rsp), %ecx
movb %cl, 0x145005(%rax)
movl $0x1, 0x141000(%rax)
movb $0x1, 0x145006(%rax)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop