// File: /workspace/github_cpp_makefile_star2_O3/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, 0x30(%rsp)
movzbl 0x145007(%rdi), %r14d
movq %rdi, 0x18(%rsp)
movzbl 0x145008(%rdi), %ebx
leaq 0x1(%rbx), %rax
imulq $0x186a, %r14, %rdi     # imm = 0x186A
addq $0x186a, %rdi           # imm = 0x186A
imulq %rax, %rdi
addq $0x80000, %rdi          # imm = 0x80000
callq 0x1030
movl $0x494446, (%rax)       # imm = 0x494446
leal 0x1(%r14), %ecx
movw %cx, 0x4(%rax)
movq %rbx, 0x40(%rsp)
leal 0x1(%rbx), %ecx
movw %cx, 0x6(%rax)
movq %rax, 0x10(%rsp)
movw $0x0, 0xc(%rax)
movl $0xe, %r15d
xorl %r12d, %r12d
xorl %eax, %eax
movq %r14, 0x38(%rsp)
movq %rax, 0x48(%rsp)
movzbl %al, %eax
movl %eax, 0xc(%rsp)
xorl %edx, %edx
movq 0x10(%rsp), %rcx
movl %r15d, %eax
movl %r12d, 0x34(%rsp)
movl %r12d, (%rcx,%rax)
leal 0x4(%r15), %eax
movw $0x0, (%rcx,%rax)
movq %r15, 0x50(%rsp)
addl $0x7, %r15d
movq %rdx, 0x20(%rsp)
movzbl %dl, %ebx
xorl %ebp, %ebp
movq 0x18(%rsp), %rdi
movl 0xc(%rsp), %esi
movl %ebx, %edx
xorl %ecx, %ecx
leaq 0x58(%rsp), %r8
callq 0x1f26
xorl %r12d, %r12d
testb %al, %al
je 0x30f6
movb $0x1, %r14b
xorl %ebp, %ebp
movl 0x80(%rsp), %eax
testb $0x1, %r14b
je 0x302d
movl %eax, 0x28(%rsp)
movq 0x10(%rsp), %r14
jmp 0x303c
cmpl 0x28(%rsp), %eax
movq 0x10(%rsp), %r14
je 0x30f6
movl 0x84(%rsp), %r13d
movl %r15d, %eax
movq 0x70(%rsp), %rcx
movl (%rcx), %ecx
movl %ecx, (%r14,%rax)
shrl $0x10, %ecx
movl 0x7c(%rsp), %r9d
movzbl %cl, %ecx
movq 0x18(%rsp), %rdi
movl 0xc(%rsp), %esi
movl %ebx, %edx
leaq 0x90(%rsp), %r8
callq 0x20f6
testb %al, %al
je 0x30a6
cmpb $-0x8, 0xe2(%rsp)
sete %cl
shlb $0x7, %cl
cmpb $0x0, 0xe1(%rsp)
movl 0xd0(%rsp), %eax
je 0x30af
movl %eax, %edx
shrl $0x7, %edx
andb $0x3f, %dl
orb %cl, %dl
movl %edx, %ecx
jmp 0x30af
movb $0x40, %cl
movl 0xd0(%rsp), %eax
leal 0x4(%r15), %edx
leal 0x5(%r15), %esi
movl %edx, %edx
movb %cl, (%r14,%rdx)
movw %r12w, (%r14,%rsi)
xorl %r14d, %r14d
testb $0x40, %cl
cmovnel %r14d, %eax
addl %eax, %r12d
addl $0x7, %r15d
movq 0x18(%rsp), %rdi
movl 0xc(%rsp), %esi
movl %ebx, %edx
movl %r13d, %ecx
leaq 0x58(%rsp), %r8
callq 0x1f26
incb %bpl
testb %al, %al
jne 0x3015
movq 0x50(%rsp), %rax
addl $0x6, %eax
movq 0x10(%rsp), %rcx
movb %bpl, (%rcx,%rax)
addl 0x34(%rsp), %r12d
movq 0x20(%rsp), %rdx
leal 0x1(%rdx), %eax
cmpl 0x40(%rsp), %edx
movl %eax, %edx
jne 0x2fc5
movq 0x48(%rsp), %rcx
leal 0x1(%rcx), %eax
movq 0x38(%rsp), %r14
cmpl %r14d, %ecx
jne 0x2fb2
movq 0x10(%rsp), %rbx
movw %r15w, 0x8(%rbx)
movl %r15d, %edi
addq %rbx, %rdi
leaq 0x8033(%rip), %rsi      # 0xb180
movl $0x4d, %edx
callq 0x10e0
addl $0x4d, %r15d
movw %r15w, 0xa(%rbx)
xorl %eax, %eax
leaq 0x58(%rsp), %r12
movq %rax, 0x20(%rsp)
movzbl %al, %ebp
xorl %eax, %eax
movl %edx, %r14d
movq %rax, 0x28(%rsp)
movzbl %al, %r13d
movq 0x18(%rsp), %rdi
movl %ebp, %esi
movl %r13d, %edx
xorl %ecx, %ecx
movq %r12, %r8
callq 0x1f26
testb %al, %al
je 0x3242
movb $0x1, %cl
movl %r13d, 0xc(%rsp)
movl 0x80(%rsp), %eax
testb $0x1, %cl
je 0x31b1
movl %eax, %r14d
jmp 0x31ba
cmpl %r14d, %eax
je 0x3242
movl 0x84(%rsp), %ebx
movq 0x70(%rsp), %rax
movzbl 0x2(%rax), %ecx
movl 0x7c(%rsp), %r9d
movq 0x18(%rsp), %rdi
movl %ebp, %esi
movl %r13d, %edx
leaq 0x90(%rsp), %r8
callq 0x20f6
testb %al, %al
je 0x3224
movl %r15d, %edi
addq 0x10(%rsp), %rdi
movq 0xc8(%rsp), %rsi
movl %ebp, %r13d
movl %r14d, %ebp
movq %r12, %r14
movl 0xd0(%rsp), %r12d
movq %r12, %rdx
callq 0x10e0
addl %r12d, %r15d
movq %r14, %r12
movl %ebp, %r14d
movl %r13d, %ebp
movl 0xc(%rsp), %r13d
movq 0x18(%rsp), %rdi
movl %ebp, %esi
movl %r13d, %edx
movl %ebx, %ecx
movq %r12, %r8
callq 0x1f26
xorl %ecx, %ecx
testb %al, %al
jne 0x31a0
movq 0x28(%rsp), %rcx
leal 0x1(%rcx), %eax
cmpl 0x40(%rsp), %ecx
jne 0x3174
movl %r14d, %edx
movq 0x20(%rsp), %rcx
leal 0x1(%rcx), %eax
movq 0x38(%rsp), %r14
cmpl %r14d, %ecx
jne 0x3167
movl %r15d, %edx
movl 0x30(%rsp), %edi
movq 0x10(%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
nop