// File: /workspace/github_cpp_makefile_star2_O1/atsidaev[P]trx2x/DiskImage.cpp
// Function: TDiskImage::writeFDI(int)
void TDiskImage::writeFDI(int hfile)
{
   unsigned char *ptr = (unsigned char*)new char[(MaxTrack+1)*(MaxSide+1)*6250+524288];
   unsigned short *headptr = (unsigned short*)(ptr+4);
   memcpy(ptr, "FDI", 3);
   ptr[3]  = 0x00;                                      // write enabled
   headptr[0] = ((unsigned short)MaxTrack) + 1;         // Cyl count
   headptr[1] = ((unsigned short)MaxSide) + 1;          // Head count
   headptr[4] = 0;                                      // extension length

   unsigned int trk, side;
   VGFIND_SECTOR vgfs;
   VGFIND_ADM vgfa;

   unsigned char *htrkSectorCount;
   unsigned int fdiOFF = 0x0E;
   unsigned int trackOFF = 0;
   unsigned int SectorCount;
   unsigned int sectOFF;

   for(trk=0; trk <= unsigned(MaxTrack); trk++)
      for(side=0; side <= unsigned(MaxSide); side++)
      {
         *((uint32_t*)(ptr+fdiOFF)) = trackOFF;
         fdiOFF += 4;
         *((uint16_t*)(ptr+fdiOFF)) = 0;  // Всегда содержит 0 (резерв)
         fdiOFF += 2;
         htrkSectorCount = ptr+fdiOFF;
         fdiOFF++;
         SectorCount=0;
         sectOFF = 0;

         unsigned int TrackPtr=0;
         bool FirstFindAD = true;
         unsigned int FirstOffset;
         for(;;)
         {
            if(!FindADMark(trk, side, TrackPtr, &vgfa)) break;
            if(!FirstFindAD) 
            {
               if(vgfa.OffsetADM == FirstOffset) break;
            }
            else
            {
               FirstOffset = vgfa.OffsetADM;
               FirstFindAD = false;
            }
            TrackPtr = vgfa.OffsetEndADM;

            SectorCount++;

            memcpy(ptr+fdiOFF, vgfa.ADMPointer, 4);
            fdiOFF+=4;
            unsigned char flags=0x00;
            if(!FindSector(trk, side, vgfa.ADMPointer[2], &vgfs, vgfa.MarkedOffsetADM))
               flags |= 0x40;
            else
            {
               if(vgfs.DataMarker==0xF8) flags |= 0x80;
               if(vgfs.CRCOK) flags |= (vgfs.SectorLength>>7)&0x3F;
            }
            ptr[fdiOFF++] = flags;
            *((unsigned short*)(ptr+fdiOFF)) = sectOFF;
            fdiOFF+=2;

            if(!(flags&0x40)) sectOFF += vgfs.SectorLength;
         }
         *htrkSectorCount = (unsigned char)SectorCount;
         trackOFF += sectOFF;
      }
   headptr[2] = fdiOFF;                                 // Text offset
   memcpy(ptr+fdiOFF, fdicomment, sizeof(fdicomment));
   fdiOFF += sizeof(fdicomment);

   headptr[3] = fdiOFF;                                 // Data offset

   for(trk=0; trk <= unsigned(MaxTrack); trk++)
      for(side=0; side <= unsigned(MaxSide); side++)
      {
         unsigned int TrackPtr=0;
         bool FirstFindAD = true;
         unsigned int FirstOffset;
         for(;;)
         {
            if(!FindADMark(trk, side, TrackPtr, &vgfa)) break;
            if(!FirstFindAD) 
            {
               if(vgfa.OffsetADM == FirstOffset) break;
            }
            else
            {
               FirstOffset = vgfa.OffsetADM;
               FirstFindAD = false;
            }
            TrackPtr = vgfa.OffsetEndADM;

            if(FindSector(trk, side, vgfa.ADMPointer[2], &vgfs, vgfa.MarkedOffsetADM))
            {
               memcpy(ptr+fdiOFF, vgfs.SectorPointer, vgfs.SectorLength);
               fdiOFF+=vgfs.SectorLength;
            }
         }
      }

   write(hfile, ptr, fdiOFF);
   delete ptr;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movl %esi, 0x3c(%rsp)
movzbl 0x145007(%rdi), %ebx
movq %rdi, 0x10(%rsp)
movzbl 0x145008(%rdi), %r14d
leaq 0x1(%r14), %rax
imulq $0x186a, %rbx, %rdi     # imm = 0x186A
addq $0x186a, %rdi           # imm = 0x186A
imulq %rax, %rdi
addq $0x80000, %rdi          # imm = 0x80000
callq 0x1030
movl $0x494446, (%rax)       # imm = 0x494446
movq %rbx, 0x40(%rsp)
leal 0x1(%rbx), %ecx
movw %cx, 0x4(%rax)
movq %r14, 0x48(%rsp)
leal 0x1(%r14), %ecx
movq %rax, %r14
movw %cx, 0x6(%rax)
movw $0x0, 0xc(%rax)
movl $0xe, %r15d
xorl %ecx, %ecx
xorl %eax, %eax
movq %r14, 0x18(%rsp)
movq %rax, 0x50(%rsp)
movzbl %al, %eax
movl %eax, 0x8(%rsp)
movl %r15d, %esi
xorl %edx, %edx
movl %esi, %eax
movl %ecx, 0x30(%rsp)
movl %ecx, (%r14,%rax)
leal 0x4(%rsi), %eax
movw $0x0, (%r14,%rax)
movq %rsi, 0x20(%rsp)
leal 0x7(%rsi), %r15d
movq %rdx, 0x28(%rsp)
movzbl %dl, %ebx
xorl %r12d, %r12d
movq 0x10(%rsp), %rdi
movl 0x8(%rsp), %esi
movl %ebx, %edx
xorl %ecx, %ecx
leaq 0x58(%rsp), %r8
callq 0x1f12
xorl %ebp, %ebp
testb %al, %al
je 0x30ac
movb $0x1, %r14b
xorl %r12d, %r12d
testb $0x1, %r14b
je 0x2fe0
movl 0x80(%rsp), %eax
movl %eax, 0xc(%rsp)
xorl %r14d, %r14d
jmp 0x2ff1
movl 0xc(%rsp), %eax
cmpl %eax, 0x80(%rsp)
je 0x30a7
movl 0x84(%rsp), %r13d
movl %r15d, %eax
movq 0x70(%rsp), %rcx
movl (%rcx), %edx
movq 0x18(%rsp), %rsi
movl %edx, (%rsi,%rax)
movzbl 0x2(%rcx), %ecx
movl 0x7c(%rsp), %r9d
movq 0x10(%rsp), %rdi
movl 0x8(%rsp), %esi
movl %ebx, %edx
leaq 0x90(%rsp), %r8
callq 0x20e8
movb $0x40, %cl
testb %al, %al
je 0x305a
cmpb $-0x8, 0xe2(%rsp)
sete %cl
shlb $0x7, %cl
cmpb $0x0, 0xe1(%rsp)
je 0x305a
movl 0xd0(%rsp), %eax
shrl $0x7, %eax
andb $0x3f, %al
orb %cl, %al
movl %eax, %ecx
leal 0x4(%r15), %eax
leal 0x5(%r15), %edx
movl %eax, %eax
movq 0x18(%rsp), %rsi
movb %cl, (%rsi,%rax)
movw %bp, (%rsi,%rdx)
xorl %eax, %eax
testb $0x40, %cl
jne 0x307e
movl 0xd0(%rsp), %eax
addl %eax, %ebp
addl $0x7, %r15d
movq 0x10(%rsp), %rdi
movl 0x8(%rsp), %esi
movl %ebx, %edx
movl %r13d, %ecx
leaq 0x58(%rsp), %r8
callq 0x1f12
incl %r12d
testb %al, %al
jne 0x2fca
movq 0x18(%rsp), %r14
movq 0x20(%rsp), %rax
addl $0x6, %eax
movb %r12b, (%r14,%rax)
movl 0x30(%rsp), %ecx
addl %ebp, %ecx
movq 0x28(%rsp), %rdx
leal 0x1(%rdx), %eax
movl %r15d, %esi
cmpl 0x48(%rsp), %edx
movl %eax, %edx
jne 0x2f7b
movq 0x50(%rsp), %rdx
leal 0x1(%rdx), %eax
cmpl 0x40(%rsp), %edx
jne 0x2f6a
movw %r15w, 0x8(%r14)
movl %r15d, %edi
addq %r14, %rdi
leaq 0x8087(%rip), %rsi      # 0xb180
movl $0x4d, %edx
callq 0x10e0
addl $0x4d, %r15d
movw %r15w, 0xa(%r14)
xorl %eax, %eax
leaq 0x58(%rsp), %r12
leaq 0x90(%rsp), %rbx
movq %rax, 0x20(%rsp)
movzbl %al, %ebp
xorl %r13d, %r13d
movl %ebp, 0x28(%rsp)
movzbl %r13b, %edx
movq 0x10(%rsp), %rdi
movl %ebp, %esi
movl %edx, 0x8(%rsp)
xorl %ecx, %ecx
movq %r12, %r8
callq 0x1f12
testb %al, %al
je 0x3206
movb $0x1, %al
movl %eax, 0xc(%rsp)
movq %r13, 0x30(%rsp)
testb $0x1, 0xc(%rsp)
je 0x316f
movl 0x80(%rsp), %r14d
movl $0x0, 0xc(%rsp)
jmp 0x317d
cmpl %r14d, 0x80(%rsp)
je 0x3206
movl 0x84(%rsp), %r13d
movq 0x70(%rsp), %rax
movzbl 0x2(%rax), %ecx
movl 0x7c(%rsp), %r9d
movq 0x10(%rsp), %rdi
movl %ebp, %esi
movl 0x8(%rsp), %edx
movq %rbx, %r8
callq 0x20e8
testb %al, %al
je 0x31e3
movl %r15d, %edi
addq 0x18(%rsp), %rdi
movq 0xc8(%rsp), %rsi
movl %r14d, %ebp
movq %rbx, %r14
movq %r12, %rbx
movl 0xd0(%rsp), %r12d
movq %r12, %rdx
callq 0x10e0
addl %r12d, %r15d
movq %rbx, %r12
movq %r14, %rbx
movl %ebp, %r14d
movl 0x28(%rsp), %ebp
movq 0x10(%rsp), %rdi
movl %ebp, %esi
movl 0x8(%rsp), %edx
movl %r13d, %ecx
movq %r12, %r8
callq 0x1f12
testb %al, %al
movq 0x30(%rsp), %r13
jne 0x3156
leal 0x1(%r13), %eax
cmpl 0x48(%rsp), %r13d
movl %eax, %r13d
jne 0x312a
movq 0x20(%rsp), %rcx
leal 0x1(%rcx), %eax
cmpl 0x40(%rsp), %ecx
jne 0x311b
movl %r15d, %edx
movl 0x3c(%rsp), %edi
movq 0x18(%rsp), %rbx
movq %rbx, %rsi
callq 0x1060
movl $0x1, %esi
movq %rbx, %rdi
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1150