// File: /workspace/github_cpp_makefile_star2_O1/atsidaev[P]trx2x/DiskImage.cpp
// Function: TDiskImage::writeFDD(int)
void TDiskImage::writeFDD(int hfile)
{
   char fddid[32] = "SPM DISK (c) 1996 MOA v0.1    ";


   unsigned char *ptr = (unsigned char*)new char[(MaxTrack+1)*(MaxSide+1)*6250+524288];

   FDD_MAIN_HEADER *fdd_hdr = (FDD_MAIN_HEADER*)(ptr);

   memcpy(fdd_hdr->ID, fddid, 30);
   fdd_hdr->MaxTracks = (MaxTrack+1)&0xFF;
   fdd_hdr->MaxHeads = (MaxSide+1)&0xFF;
   fdd_hdr->diskIndex = 0;
//   fdd_hdr->DataOffset =




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

   unsigned char *htrkSectorCount;
   unsigned int fddOFF = 36 + 4*(MaxTrack+1)*(MaxSide+1);

   unsigned int SectorCount;
   unsigned int sectOFF;

   char secbuf[32768];

   for(trk=0; trk <= unsigned(MaxTrack); trk++)
      for(side=0; side <= unsigned(MaxSide); side++)
      {
         fdd_hdr->DataOffset[trk*(MaxSide+1)+side] = fddOFF;
         FDD_TRACK_HEADER *trackinfo = (FDD_TRACK_HEADER*)(ptr+fddOFF);
         fddOFF += 2;

         SectorCount = 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;

            trackinfo->sect[SectorCount].trk  = vgfa.ADMPointer[0];
            trackinfo->sect[SectorCount].side = vgfa.ADMPointer[1];
            trackinfo->sect[SectorCount].sect = vgfa.ADMPointer[2];
            trackinfo->sect[SectorCount].size = vgfa.ADMPointer[3];
            trackinfo->sect[SectorCount].SectPos = vgfa.MarkedOffsetADM;  // tmp

            SectorCount++;
            fddOFF += 8;
         }

         trackinfo->SectNum = SectorCount;
         trackinfo->trkType = 0;


         for(int ilsec=0; ilsec < int(SectorCount); ilsec++)
         {
            int SL = trackinfo->sect[ilsec].size;
            SL &= 3;
            if(!SL) SL = 128;
            else SL = 128 << SL;


            if(!FindSector(trk, side, trackinfo->sect[ilsec].sect , &vgfs, trackinfo->sect[ilsec].SectPos))
            {
               for(int i=0; i < sizeof(secbuf); i++) secbuf[i] = 0;
               memcpy(secbuf, "***ERROR: SECTOR NOT FOUND!***", 31);
            }
            else
               memcpy(secbuf, vgfs.SectorPointer, SL);

            trackinfo->sect[ilsec].SectPos = fddOFF;

            memcpy(ptr+fddOFF, secbuf, SL);
            fddOFF += SL;
         }
      }

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x80e8, %rsp           # imm = 0x80E8
movl %esi, 0x18(%rsp)
movzbl 0x145007(%rdi), %ebx
movq %rdi, 0x10(%rsp)
movzbl 0x145008(%rdi), %r14d
movq %r14, %r13
incq %r13
imulq $0x186a, %rbx, %rdi     # imm = 0x186A
addq $0x186a, %rdi           # imm = 0x186A
imulq %r13, %rdi
addq $0x80000, %rdi          # imm = 0x80000
callq 0x1030
movaps 0x4f24(%rip), %xmm0     # 0x87a0
movups %xmm0, (%rax)
movups 0x4f28(%rip), %xmm0     # 0x87ae
movups %xmm0, 0xe(%rax)
leal 0x1(%rbx), %ecx
movb %cl, 0x1e(%rax)
leal 0x1(%r14), %ecx
movb %cl, 0x1f(%rax)
movl $0x0, 0x20(%rax)
leal 0x4(,%rbx,4), %ecx
movq %r13, 0x20(%rsp)
imull %ecx, %r13d
addl $0x24, %r13d
leaq 0x1(%rbx), %rcx
movq %rcx, 0x28(%rsp)
movq %rax, 0x8(%rsp)
addq $0x6, %rax
movq %rax, 0x38(%rsp)
xorl %eax, %eax
movq %rax, %rcx
imulq 0x20(%rsp), %rcx
movq %rcx, 0x40(%rsp)
movq %rax, 0x30(%rsp)
movzbl %al, %eax
movl %eax, (%rsp)
xorl %ebx, %ebx
movq 0x40(%rsp), %rax
addq %rbx, %rax
movq 0x8(%rsp), %rcx
movl %r13d, 0x24(%rcx,%rax,4)
movl %r13d, %r15d
leaq (%rcx,%r15), %r14
movl %r13d, %ebp
addl $0x2, %ebp
movzbl %bl, %edx
xorl %r13d, %r13d
movq 0x10(%rsp), %rdi
movl (%rsp), %esi
xorl %ecx, %ecx
leaq 0x50(%rsp), %r8
callq 0x1f12
movq %rbx, 0x48(%rsp)
movzbl %bl, %ecx
movl %ecx, 0x4(%rsp)
testb %al, %al
je 0x399d
xorl %r13d, %r13d
movb $0x1, %bl
testb $0x1, %bl
je 0x3942
movl 0x78(%rsp), %r12d
xorl %ebx, %ebx
jmp 0x3949
cmpl %r12d, 0x78(%rsp)
je 0x399d
movl 0x7c(%rsp), %ecx
movq 0x68(%rsp), %rax
movb (%rax), %dl
movl %r13d, %esi
movb %dl, 0x2(%r14,%rsi,8)
movb 0x1(%rax), %dl
movb %dl, 0x3(%r14,%rsi,8)
movb 0x2(%rax), %dl
movb %dl, 0x4(%r14,%rsi,8)
movb 0x3(%rax), %al
movb %al, 0x5(%r14,%rsi,8)
movl 0x74(%rsp), %eax
movl %eax, 0x6(%r14,%rsi,8)
incl %r13d
addl $0x8, %ebp
movq 0x10(%rsp), %rdi
movl (%rsp), %esi
movl 0x4(%rsp), %edx
leaq 0x50(%rsp), %r8
callq 0x1f12
testb %al, %al
jne 0x3934
movl %r12d, 0x1c(%rsp)
movb %r13b, 0x1(%r14)
movb $0x0, (%r14)
testl %r13d, %r13d
jle 0x3a71
movl %r13d, %r14d
addq 0x38(%rsp), %r15
xorl %ebx, %ebx
movb -0x1(%r15,%rbx,8), %cl
andb $0x3, %cl
movl $0x80, %r13d
shlq %cl, %r13
movzbl -0x2(%r15,%rbx,8), %ecx
movl (%r15,%rbx,8), %r9d
movq 0x10(%rsp), %rdi
movl (%rsp), %esi
movl 0x4(%rsp), %edx
leaq 0x88(%rsp), %r8
callq 0x20e8
testb %al, %al
je 0x3a12
movq 0xc0(%rsp), %rsi
leaq 0xe0(%rsp), %r12
movq %r12, %rdi
movq %r13, %rdx
callq 0x10e0
jmp 0x3a47
movl $0x8000, %edx           # imm = 0x8000
leaq 0xe0(%rsp), %r12
movq %r12, %rdi
xorl %esi, %esi
callq 0x10a0
movups 0x4ad6(%rip), %xmm0     # 0x8506
movaps %xmm0, 0xe0(%rsp)
movups 0x4ad6(%rip), %xmm0     # 0x8515
movups %xmm0, 0xef(%rsp)
movl %ebp, (%r15,%rbx,8)
movl %ebp, %edi
addq 0x8(%rsp), %rdi
movl %r13d, %edx
movq %r12, %rsi
callq 0x10e0
addl %ebp, %r13d
incq %rbx
movl %r13d, %ebp
cmpq %rbx, %r14
jne 0x39bd
jmp 0x3a74
movl %ebp, %r13d
movq 0x48(%rsp), %rbx
incq %rbx
cmpq 0x20(%rsp), %rbx
movl 0x1c(%rsp), %r12d
jne 0x38e6
movq 0x30(%rsp), %rax
incq %rax
cmpq 0x28(%rsp), %rax
jne 0x38cb
movl %r13d, %edx
movl 0x18(%rsp), %edi
movq 0x8(%rsp), %rbx
movq %rbx, %rsi
callq 0x1060
movl $0x1, %esi
movq %rbx, %rdi
addq $0x80e8, %rsp           # imm = 0x80E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1150
nop