// File: /workspace/github_cpp_makefile_star2_O2/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, 0x1c(%rsp)
movzbl 0x145007(%rdi), %ebx
movq %rdi, 0x30(%rsp)
movzbl 0x145008(%rdi), %r14d
movq %r14, %r15
incq %r15
imulq $0x186a, %rbx, %rdi     # imm = 0x186A
addq $0x186a, %rdi           # imm = 0x186A
imulq %r15, %rdi
addq $0x80000, %rdi          # imm = 0x80000
callq 0x1030
movaps 0x477a(%rip), %xmm0     # 0x77a0
movups %xmm0, (%rax)
movups 0x477e(%rip), %xmm0     # 0x77ae
movups %xmm0, 0xe(%rax)
leal 0x1(%rbx), %ecx
movb %cl, 0x1e(%rax)
leal 0x1(%r14), %ecx
movb %cl, 0x1f(%rax)
movq %rax, 0x10(%rsp)
andl $0x0, 0x20(%rax)
leal 0x4(,%rbx,4), %eax
movq %r15, 0x28(%rsp)
movl %r15d, %ebp
imull %eax, %ebp
addl $0x24, %ebp
leaq 0x1(%rbx), %rax
movq %rax, 0x38(%rsp)
xorl %eax, %eax
cmpq 0x38(%rsp), %rax
je 0x3251
movq %rax, %rcx
imulq 0x28(%rsp), %rcx
movq %rcx, 0x40(%rsp)
movzbl %al, %r14d
xorl %edx, %edx
movq %rax, 0x48(%rsp)
cmpq 0x28(%rsp), %rdx
je 0x3249
movq 0x40(%rsp), %rax
addq %rdx, %rax
movq 0x10(%rsp), %rcx
movl %ebp, 0x24(%rcx,%rax,4)
movl %ebp, %eax
leaq (%rcx,%rax), %rbx
addl $0x2, %ebp
leaq (%rcx,%rax), %r15
addq $0x2, %r15
movb $0x1, %r12b
xorl %r13d, %r13d
movq %rdx, 0x50(%rsp)
movzbl %dl, %eax
movl %eax, 0xc(%rsp)
xorl %ecx, %ecx
movq 0x30(%rsp), %rdi
movl %r14d, %esi
movl 0xc(%rsp), %edx
leaq 0x60(%rsp), %r8
callq 0x1bca
testb %al, %al
je 0x3146
movl 0x88(%rsp), %eax
testb $0x1, %r12b
je 0x30fc
movl %eax, 0x20(%rsp)
jmp 0x3102
cmpl 0x20(%rsp), %eax
je 0x3146
movl 0x8c(%rsp), %ecx
movq 0x78(%rsp), %rax
movb (%rax), %dl
movl %r13d, %esi
movb %dl, (%r15,%rsi,8)
movb 0x1(%rax), %dl
movb %dl, 0x1(%r15,%rsi,8)
movb 0x2(%rax), %dl
movb %dl, 0x2(%r15,%rsi,8)
movb 0x3(%rax), %al
movb %al, 0x3(%r15,%rsi,8)
movl 0x84(%rsp), %eax
movl %eax, 0x4(%r15,%rsi,8)
incl %r13d
addl $0x8, %ebp
xorl %r12d, %r12d
jmp 0x30cf
movb %r13b, 0x1(%rbx)
movb $0x0, (%rbx)
testl %r13d, %r13d
movl $0x0, %eax
cmovlel %eax, %r13d
xorl %ebx, %ebx
movq %r13, 0x58(%rsp)
cmpq %r13, %rbx
je 0x3237
movb 0x3(%r15,%rbx,8), %cl
andb $0x3, %cl
movl $0x80, %r13d
shlq %cl, %r13
movzbl 0x2(%r15,%rbx,8), %ecx
movl 0x4(%r15,%rbx,8), %r9d
movq 0x30(%rsp), %rdi
movl %r14d, %esi
movl 0xc(%rsp), %edx
leaq 0x98(%rsp), %r8
callq 0x1d64
testb %al, %al
je 0x31d3
movq 0xd0(%rsp), %rsi
movl %r13d, %r12d
movl %ebp, 0x24(%rsp)
movl %r14d, %ebp
leaq 0xf0(%rsp), %r14
movq %r14, %rdi
movq %r12, %rdx
callq 0x10d0
movq %r14, %rsi
movl %ebp, %r14d
movl 0x24(%rsp), %ebp
jmp 0x3213
xorl %eax, %eax
leaq 0xf0(%rsp), %rsi
cmpq $0x8000, %rax           # imm = 0x8000
je 0x31f2
movb $0x0, 0xf0(%rsp,%rax)
incq %rax
jmp 0x31dd
movups 0x431c(%rip), %xmm0     # 0x7515
movups %xmm0, 0xff(%rsp)
movups 0x42fe(%rip), %xmm0     # 0x7506
movaps %xmm0, 0xf0(%rsp)
movq %r13, %r12
movl %ebp, 0x4(%r15,%rbx,8)
movl %ebp, %edi
addq 0x10(%rsp), %rdi
movq %r12, %rdx
callq 0x10d0
addl %r13d, %ebp
incq %rbx
movq 0x58(%rsp), %r13
jmp 0x3160
movq 0x50(%rsp), %rdx
incq %rdx
movq 0x48(%rsp), %rax
jmp 0x308e
incq %rax
jmp 0x306a
movl %ebp, %edx
movl 0x1c(%rsp), %edi
movq 0x10(%rsp), %rbx
movq %rbx, %rsi
callq 0x1050
pushq $0x1
popq %rsi
movq %rbx, %rdi
addq $0x80f8, %rsp           # imm = 0x80F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1140