// File: /workspace/github_cpp_makefile_star2_O2/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 $0xd8, %rsp
movl %esi, 0x20(%rsp)
movzbl 0x145007(%rdi), %ebx
movq %rdi, 0x18(%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
incl %ebx
movq %rbx, 0x30(%rsp)
movw %bx, 0x4(%rax)
incl %r14d
movq %r14, 0x38(%rsp)
movw %r14w, 0x6(%rax)
movq %rax, 0x8(%rsp)
andw $0x0, 0xc(%rax)
pushq $0xe
popq %r15
xorl %eax, %eax
xorl %edx, %edx
cmpl 0x30(%rsp), %eax
je 0x29ad
xorl %esi, %esi
movzbl %al, %ecx
movl %ecx, 0x4(%rsp)
movl %eax, 0x24(%rsp)
cmpl 0x38(%rsp), %esi
je 0x29a6
movl %r15d, %eax
movq 0x8(%rsp), %rcx
movl %edx, 0x2c(%rsp)
movl %edx, (%rcx,%rax)
leal 0x4(%r15), %eax
andw $0x0, (%rcx,%rax)
leal 0x6(%r15), %eax
movq %rax, 0x40(%rsp)
addl $0xc, %r15d
movb $0x1, %bpl
movl $0x0, (%rsp)
movl %esi, 0x28(%rsp)
movzbl %sil, %ebx
movl %r15d, %r12d
xorl %r14d, %r14d
xorl %r13d, %r13d
leal -0x5(%r12), %r15d
movq 0x18(%rsp), %rdi
movl 0x4(%rsp), %esi
movl %ebx, %edx
movl %r13d, %ecx
leaq 0x48(%rsp), %r8
callq 0x1bca
testb %al, %al
je 0x2980
movl 0x70(%rsp), %eax
testb $0x1, %bpl
je 0x28cf
movl %eax, 0x14(%rsp)
jmp 0x28d9
cmpl 0x14(%rsp), %eax
je 0x2980
movl 0x74(%rsp), %r13d
movl %r15d, %eax
movq 0x60(%rsp), %rcx
movl (%rcx), %ecx
movq 0x8(%rsp), %rdx
movl %ecx, (%rdx,%rax)
shrl $0x10, %ecx
movl 0x6c(%rsp), %r9d
movzbl %cl, %ecx
movq 0x18(%rsp), %rdi
movl 0x4(%rsp), %esi
movl %ebx, %edx
leaq 0x80(%rsp), %r8
callq 0x1d64
testb %al, %al
je 0x2944
cmpb $-0x8, 0xd2(%rsp)
sete %cl
shlb $0x7, %cl
cmpb $0x0, 0xd1(%rsp)
movl 0xc0(%rsp), %eax
je 0x294d
movl %eax, %edx
shrl $0x7, %edx
andb $0x3f, %dl
orb %cl, %dl
movl %edx, %ecx
jmp 0x294d
movb $0x40, %cl
movl 0xc0(%rsp), %eax
leal -0x1(%r12), %edx
movl %edx, %edx
movq 0x8(%rsp), %rsi
movb %cl, (%rsi,%rdx)
xorl %ebp, %ebp
testb $0x40, %cl
movl %r12d, %ecx
cmovnel %ebp, %eax
movw %r14w, (%rsi,%rcx)
addl %eax, %r14d
addl $0x7, %r12d
movl (%rsp), %eax
incb %al
movl %eax, (%rsp)
jmp 0x289a
movq 0x8(%rsp), %rax
movq 0x40(%rsp), %rcx
movl (%rsp), %edx
movb %dl, (%rax,%rcx)
movl 0x2c(%rsp), %edx
addl %r14d, %edx
movl 0x28(%rsp), %esi
incl %esi
movl 0x24(%rsp), %eax
jmp 0x2850
incl %eax
jmp 0x2839
movq 0x8(%rsp), %rbx
movw %r15w, 0x8(%rbx)
movl %r15d, %edi
addq %rbx, %rdi
leaq 0x77ac(%rip), %rsi      # 0xa170
pushq $0x4d
popq %rdx
callq 0x10d0
addl $0x4d, %r15d
movw %r15w, 0xa(%rbx)
xorl %eax, %eax
cmpl 0x30(%rsp), %eax
je 0x2aa6
xorl %ecx, %ecx
movl %eax, 0x14(%rsp)
movzbl %al, %ebx
cmpl 0x38(%rsp), %ecx
je 0x2a9b
movb $0x1, %r12b
xorl %r13d, %r13d
movl %ecx, 0x4(%rsp)
movzbl %cl, %ebp
movq 0x18(%rsp), %r14
movq %r14, %rdi
movl %ebx, %esi
movl %ebp, %edx
movl %r13d, %ecx
leaq 0x48(%rsp), %r8
callq 0x1bca
testb %al, %al
je 0x2a90
movl 0x70(%rsp), %eax
testb $0x1, %r12b
je 0x2a2d
movl %eax, (%rsp)
jmp 0x2a32
cmpl (%rsp), %eax
je 0x2a90
movl 0x74(%rsp), %r13d
movq 0x60(%rsp), %rax
movzbl 0x2(%rax), %ecx
movl 0x6c(%rsp), %r9d
movq 0x18(%rsp), %r14
movq %r14, %rdi
movl %ebx, %esi
movl %ebp, %edx
leaq 0x80(%rsp), %r8
callq 0x1d64
xorl %r12d, %r12d
testb %al, %al
je 0x2a06
movl %r15d, %edi
addq 0x8(%rsp), %rdi
movq 0xb8(%rsp), %rsi
movl 0xc0(%rsp), %r14d
movq %r14, %rdx
callq 0x10d0
addl %r14d, %r15d
xorl %r12d, %r12d
jmp 0x2a01
movl 0x4(%rsp), %ecx
incl %ecx
jmp 0x29ea
movl 0x14(%rsp), %eax
incl %eax
jmp 0x29d7
movl %r15d, %edx
movl 0x20(%rsp), %edi
movq 0x8(%rsp), %rbx
movq %rbx, %rsi
callq 0x1050
pushq $0x1
popq %rsi
movq %rbx, %rdi
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1140