// File: /workspace/github_cpp_makefile_star2_O3/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 $0x80f8, %rsp           # imm = 0x80F8
movl %esi, 0x20(%rsp)
movq %rdi, %r12
movzbl 0x145007(%rdi), %r14d
movzbl 0x145008(%rdi), %ebx
movq %rbx, %r15
incq %r15
imulq $0x186a, %r14, %rdi     # imm = 0x186A
addq $0x186a, %rdi           # imm = 0x186A
imulq %r15, %rdi
addq $0x80000, %rdi          # imm = 0x80000
callq 0x1030
movaps 0x4f44(%rip), %xmm0     # 0x8800
movups %xmm0, (%rax)
movups 0x4f48(%rip), %xmm0     # 0x880e
movups %xmm0, 0xe(%rax)
leal 0x1(%r14), %ecx
movb %cl, 0x1e(%rax)
leal 0x1(%rbx), %ecx
movb %cl, 0x1f(%rax)
movl $0x0, 0x20(%rax)
leal 0x4(,%r14,4), %ecx
movl %r15d, %ebx
imull %ecx, %ebx
addl $0x24, %ebx
leaq 0x1(%r14), %rcx
movq %rcx, 0x40(%rsp)
movq %rax, 0x18(%rsp)
addq $0x6, %rax
movq %rax, 0x50(%rsp)
xorl %eax, %eax
movq %r12, 0x38(%rsp)
movq %r15, 0x10(%rsp)
movq %rax, %r14
imulq %r15, %r14
movq %rax, 0x48(%rsp)
movzbl %al, %eax
movl %eax, 0x8(%rsp)
xorl %ebp, %ebp
movq %r14, 0x28(%rsp)
leaq (%r14,%rbp), %rax
movq 0x18(%rsp), %rcx
movl %ebx, 0x24(%rcx,%rax,4)
movl %ebx, %r15d
leaq (%rcx,%r15), %r13
addl $0x2, %ebx
movzbl %bpl, %edx
movq %r12, %rdi
movl 0x8(%rsp), %esi
xorl %ecx, %ecx
leaq 0x60(%rsp), %r8
callq 0x1f26
testb %al, %al
je 0x3ae0
xorl %r14d, %r14d
movb $0x1, %cl
movq %rbp, 0x30(%rsp)
movzbl %bpl, %eax
movl %eax, 0xc(%rsp)
movl 0x24(%rsp), %ebp
movl 0x88(%rsp), %eax
testb $0x1, %cl
je 0x3988
movl %eax, %ebp
jmp 0x398c
cmpl %ebp, %eax
je 0x39e7
movl 0x8c(%rsp), %ecx
movq 0x78(%rsp), %rax
movb (%rax), %dl
movl %r14d, %esi
movb %dl, 0x2(%r13,%rsi,8)
movb 0x1(%rax), %dl
movb %dl, 0x3(%r13,%rsi,8)
movb 0x2(%rax), %dl
movb %dl, 0x4(%r13,%rsi,8)
movb 0x3(%rax), %al
movb %al, 0x5(%r13,%rsi,8)
movl 0x84(%rsp), %eax
movl %eax, 0x6(%r13,%rsi,8)
incl %r14d
addl $0x8, %ebx
movq %r12, %rdi
movl 0x8(%rsp), %esi
movl 0xc(%rsp), %edx
leaq 0x60(%rsp), %r8
callq 0x1f26
xorl %ecx, %ecx
testb %al, %al
jne 0x3978
movb %r14b, 0x1(%r13)
movb $0x0, (%r13)
testl %r14d, %r14d
movl %ebp, 0x24(%rsp)
jle 0x3acf
movl %r14d, %eax
movq %rax, 0x58(%rsp)
addq 0x50(%rsp), %r15
xorl %r12d, %r12d
movb -0x1(%r15,%r12,8), %cl
andb $0x3, %cl
movl $0x80, %r13d
shlq %cl, %r13
movzbl -0x2(%r15,%r12,8), %ecx
movl (%r15,%r12,8), %r9d
movq 0x38(%rsp), %rdi
movl 0x8(%rsp), %esi
movl 0xc(%rsp), %edx
leaq 0x98(%rsp), %r8
callq 0x20f6
testb %al, %al
je 0x3a66
movq 0xd0(%rsp), %rsi
movl %r13d, %r14d
leaq 0xf0(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rdx
callq 0x10e0
jmp 0x3aa3
movl $0x7fe1, %edx           # imm = 0x7FE1
leaq 0x10f(%rsp), %rdi
xorl %esi, %esi
callq 0x10a0
movups 0x4ae1(%rip), %xmm0     # 0x8562
movaps %xmm0, 0xf0(%rsp)
movups 0x4ae1(%rip), %xmm0     # 0x8571
movups %xmm0, 0xff(%rsp)
movq %r13, %r14
leaq 0xf0(%rsp), %rbp
movl %ebx, (%r15,%r12,8)
movl %ebx, %edi
addq 0x18(%rsp), %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x10e0
addl %r13d, %ebx
incq %r12
cmpq %r12, 0x58(%rsp)
jne 0x3a0d
movq 0x38(%rsp), %r12
movq 0x10(%rsp), %r15
movq 0x28(%rsp), %r14
movq 0x30(%rsp), %rbp
jmp 0x3aec
movw $0x0, (%r13)
movq 0x10(%rsp), %r15
incq %rbp
cmpq %r15, %rbp
jne 0x392c
movq 0x48(%rsp), %rax
incq %rax
cmpq 0x40(%rsp), %rax
jne 0x3912
movl %ebx, %edx
movl 0x20(%rsp), %edi
movq 0x18(%rsp), %rbx
movq %rbx, %rsi
callq 0x1060
movl $0x1, %esi
movq %rbx, %rdi
addq $0x80f8, %rsp           # imm = 0x80F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1150