// File: 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
movq %rsp, %rbp
subq $0x8120, %rsp           # imm = 0x8120
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rcx
movq %rcx, -0x8118(%rbp)
movq 0xa28c(%rip), %rax      # 0xe7a0
movq %rax, -0x30(%rbp)
movq 0xa289(%rip), %rax      # 0xe7a8
movq %rax, -0x28(%rbp)
movq 0xa286(%rip), %rax      # 0xe7b0
movq %rax, -0x20(%rbp)
movq 0xa283(%rip), %rax      # 0xe7b8
movq %rax, -0x18(%rbp)
movzbl 0x145007(%rcx), %eax
addl $0x1, %eax
movzbl 0x145008(%rcx), %ecx
addl $0x1, %ecx
imull %ecx, %eax
imull $0x186a, %eax, %eax     # imm = 0x186A
addl $0x80000, %eax          # imm = 0x80000
movslq %eax, %rdi
callq 0x1030
movq -0x8118(%rbp), %rcx
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq -0x30(%rbp), %rdx
movq %rdx, (%rax)
movq -0x28(%rbp), %rdx
movq %rdx, 0x8(%rax)
movq -0x20(%rbp), %rdx
movq %rdx, 0x10(%rax)
movl -0x18(%rbp), %edx
movl %edx, 0x18(%rax)
movw -0x14(%rbp), %dx
movw %dx, 0x1c(%rax)
movzbl 0x145007(%rcx), %eax
addl $0x1, %eax
andl $0xff, %eax
movb %al, %dl
movq -0x40(%rbp), %rax
movb %dl, 0x1e(%rax)
movzbl 0x145008(%rcx), %eax
addl $0x1, %eax
andl $0xff, %eax
movb %al, %dl
movq -0x40(%rbp), %rax
movb %dl, 0x1f(%rax)
movq -0x40(%rbp), %rax
movl $0x0, 0x20(%rax)
movzbl 0x145007(%rcx), %eax
addl $0x1, %eax
shll $0x2, %eax
movzbl 0x145008(%rcx), %ecx
addl $0x1, %ecx
imull %ecx, %eax
addl $0x24, %eax
movl %eax, -0xe4(%rbp)
movl $0x0, -0x44(%rbp)
movq -0x8118(%rbp), %rcx
movl -0x44(%rbp), %eax
movzbl 0x145007(%rcx), %ecx
cmpl %ecx, %eax
ja 0x49b2
movl $0x0, -0x48(%rbp)
movq -0x8118(%rbp), %rcx
movl -0x48(%rbp), %eax
movzbl 0x145008(%rcx), %ecx
cmpl %ecx, %eax
ja 0x49a2
movq -0x8118(%rbp), %rsi
movl -0xe4(%rbp), %edx
movq -0x40(%rbp), %rax
movl -0x44(%rbp), %ecx
movzbl 0x145008(%rsi), %esi
addl $0x1, %esi
imull %esi, %ecx
addl -0x48(%rbp), %ecx
movl %ecx, %ecx
movl %edx, 0x24(%rax,%rcx,4)
movq -0x38(%rbp), %rax
movl -0xe4(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x80f8(%rbp)
movl -0xe4(%rbp), %eax
addl $0x2, %eax
movl %eax, -0xe4(%rbp)
movl $0x0, -0xe8(%rbp)
movl $0x0, -0x80fc(%rbp)
movb $0x1, -0x80fd(%rbp)
movq -0x8118(%rbp), %rdi
movl -0x44(%rbp), %eax
movb %al, %dl
movl -0x48(%rbp), %eax
movl -0x80fc(%rbp), %ecx
leaq -0xd8(%rbp), %r8
movzbl %dl, %esi
movzbl %al, %edx
callq 0x2220
testb $0x1, %al
jne 0x46d5
jmp 0x47b7
testb $0x1, -0x80fd(%rbp)
jne 0x46f3
movl -0xb0(%rbp), %eax
cmpl -0x8104(%rbp), %eax
jne 0x46f1
jmp 0x47b7
jmp 0x4706
movl -0xb0(%rbp), %eax
movl %eax, -0x8104(%rbp)
movb $0x0, -0x80fd(%rbp)
movl -0xac(%rbp), %eax
movl %eax, -0x80fc(%rbp)
movq -0xc0(%rbp), %rax
movb (%rax), %dl
movq -0x80f8(%rbp), %rax
movl -0xe8(%rbp), %ecx
movb %dl, 0x2(%rax,%rcx,8)
movq -0xc0(%rbp), %rax
movb 0x1(%rax), %dl
movq -0x80f8(%rbp), %rax
movl -0xe8(%rbp), %ecx
movb %dl, 0x3(%rax,%rcx,8)
movq -0xc0(%rbp), %rax
movb 0x2(%rax), %dl
movq -0x80f8(%rbp), %rax
movl -0xe8(%rbp), %ecx
movb %dl, 0x4(%rax,%rcx,8)
movq -0xc0(%rbp), %rax
movb 0x3(%rax), %dl
movq -0x80f8(%rbp), %rax
movl -0xe8(%rbp), %ecx
movb %dl, 0x5(%rax,%rcx,8)
movl -0xb4(%rbp), %edx
movq -0x80f8(%rbp), %rax
movl -0xe8(%rbp), %ecx
movl %edx, 0x6(%rax,%rcx,8)
movl -0xe8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xe8(%rbp)
movl -0xe4(%rbp), %eax
addl $0x8, %eax
movl %eax, -0xe4(%rbp)
jmp 0x46a5
movl -0xe8(%rbp), %eax
movb %al, %cl
movq -0x80f8(%rbp), %rax
movb %cl, 0x1(%rax)
movq -0x80f8(%rbp), %rax
movb $0x0, (%rax)
movl $0x0, -0x8108(%rbp)
movl -0x8108(%rbp), %eax
cmpl -0xe8(%rbp), %eax
jge 0x4992
movq -0x80f8(%rbp), %rax
movslq -0x8108(%rbp), %rcx
movzbl 0x5(%rax,%rcx,8), %eax
movl %eax, -0x810c(%rbp)
movl -0x810c(%rbp), %eax
andl $0x3, %eax
movl %eax, -0x810c(%rbp)
cmpl $0x0, -0x810c(%rbp)
jne 0x482c
movl $0x80, -0x810c(%rbp)
jmp 0x483f
movl -0x810c(%rbp), %ecx
movl $0x80, %eax
shll %cl, %eax
movl %eax, -0x810c(%rbp)
movq -0x8118(%rbp), %rdi
movl -0x44(%rbp), %eax
movb %al, %dl
movl -0x48(%rbp), %eax
movb %al, %cl
movq -0x80f8(%rbp), %rax
movslq -0x8108(%rbp), %rsi
movb 0x4(%rax,%rsi,8), %al
movq -0x80f8(%rbp), %rsi
movslq -0x8108(%rbp), %r8
movl 0x6(%rsi,%r8,8), %r9d
leaq -0xa0(%rbp), %r8
movzbl %dl, %esi
movzbl %cl, %edx
movzbl %al, %ecx
callq 0x25d0
testb $0x1, %al
jne 0x491d
movl $0x0, -0x8110(%rbp)
movslq -0x8110(%rbp), %rax
cmpq $0x8000, %rax           # imm = 0x8000
jae 0x48cb
movslq -0x8110(%rbp), %rax
movb $0x0, -0x80f0(%rbp,%rax)
movl -0x8110(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8110(%rbp)
jmp 0x489c
movq 0x9c2c(%rip), %rax      # 0xe4fe
movq %rax, -0x80f0(%rbp)
movq 0x9c26(%rip), %rax      # 0xe506
movq %rax, -0x80e8(%rbp)
movq 0x9c20(%rip), %rax      # 0xe50e
movq %rax, -0x80e0(%rbp)
movl 0x9c1b(%rip), %eax      # 0xe516
movl %eax, -0x80d8(%rbp)
movw 0x9c12(%rip), %ax       # 0xe51a
movw %ax, -0x80d4(%rbp)
movb 0x9c07(%rip), %al       # 0xe51c
movb %al, -0x80d2(%rbp)
jmp 0x4934
leaq -0x80f0(%rbp), %rdi
movq -0x68(%rbp), %rsi
movslq -0x810c(%rbp), %rdx
callq 0x10c0
movl -0xe4(%rbp), %edx
movq -0x80f8(%rbp), %rax
movslq -0x8108(%rbp), %rcx
movl %edx, 0x6(%rax,%rcx,8)
movq -0x38(%rbp), %rdi
movl -0xe4(%rbp), %eax
addq %rax, %rdi
leaq -0x80f0(%rbp), %rsi
movslq -0x810c(%rbp), %rdx
callq 0x10c0
movl -0x810c(%rbp), %eax
addl -0xe4(%rbp), %eax
movl %eax, -0xe4(%rbp)
movl -0x8108(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8108(%rbp)
jmp 0x47dd
jmp 0x4994
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x4624
jmp 0x49a4
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x4604
movl -0xc(%rbp), %edi
movq -0x38(%rbp), %rsi
movl -0xe4(%rbp), %eax
movl %eax, %edx
callq 0x1050
movq -0x38(%rbp), %rax
movq %rax, -0x8120(%rbp)
cmpq $0x0, %rax
je 0x49e8
movq -0x8120(%rbp), %rdi
movl $0x1, %esi
callq 0x1130
addq $0x8120, %rsp           # imm = 0x8120
popq %rbp
retq
nopw %cs:(%rax,%rax)