// File: atsidaev[P]trx2x/DiskImage.cpp
// Function: TDiskImage::writeTD0(int)
void TDiskImage::writeTD0(int hfile)
{
   TD0_MAIN_HEADER td0hdr;

   td0hdr.ID[0] = 'T'; td0hdr.ID[1] = 'D'; td0hdr.__t = 0x00;
   td0hdr.__1 = 0x00;
   td0hdr.Ver = 21;
   td0hdr.__2 = 0x00;
   td0hdr.DiskType = 0x03;
   td0hdr.Info = 0x00;
   td0hdr.DataDOS = 0x00;
   td0hdr.ChkdSides = 0x02;
   td0hdr.CRC = TD0CRC((unsigned char*)&td0hdr, 10);
   write(hfile, &td0hdr, sizeof(td0hdr));

//   TD0_INFO_DATA td0inf;

   VGFIND_SECTOR vgfs;
   VGFIND_ADM vgfa;

   TD0_TRACK_HEADER tdtrk;
   TD0_SECT_HEADER tdsect;

// prepare nullbuf...
   unsigned char nullbuf[256];
   for(int i=0; i < 256; i++) nullbuf[i] = '*';
   memcpy(nullbuf, errsect, sizeof(errsect));

   unsigned char secbuf[32768];

   unsigned char sectorsMap[256];

   unsigned int SectorCount;
   for(unsigned int trk=0; trk <= unsigned(MaxTrack); trk++)
      for(unsigned int side=0; side <= unsigned(MaxSide); side++)
      {
         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;

            sectorsMap[SectorCount] = vgfa.ADMPointer[2];

            SectorCount++;
         }
         if(SectorCount > 0xFE) SectorCount = 0xFE;

         tdtrk.SectorCount = (unsigned char)SectorCount;
         tdtrk.Track = (unsigned char)trk;
         tdtrk.Side = (unsigned char)side;
         tdtrk.CRCL = (unsigned char)(TD0CRC((unsigned char*)&tdtrk, 3)&0xFF);

         write(hfile, &tdtrk, sizeof(TD0_TRACK_HEADER));

         for(unsigned int sec=0; sec < SectorCount; sec++)
         {
            if(FindSector(trk, side, sectorsMap[sec], &vgfs))
               memcpy(secbuf+1, vgfs.SectorPointer, vgfs.SectorLength);
            else memcpy(secbuf+1, nullbuf, vgfs.SectorLength);
            secbuf[0] = 0x00;
            tdsect.ADRM[5] = (unsigned char)(TD0CRC(secbuf, vgfs.SectorLength+1)&0xFF);
            tdsect.DataLength = vgfs.SectorLength+1;
            if(vgfs.vgfa.FoundADM) memcpy(tdsect.ADRM, vgfs.vgfa.ADMPointer, 4);
            tdsect.ADRM[4] = 0x00;

            write(hfile, &tdsect, sizeof(TD0_SECT_HEADER));
            write(hfile, secbuf, tdsect.DataLength);
         }
      }

   tdtrk.SectorCount = 0xFF;
   tdtrk.Track = 0x00;
   tdtrk.Side = 0x00;
   tdtrk.CRCL = 0x00;
   write(hfile, &tdtrk, sizeof(TD0_TRACK_HEADER));

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x8300, %rsp           # imm = 0x8300
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x82f8(%rbp)
movb $0x54, -0x18(%rbp)
movb $0x44, -0x17(%rbp)
movb $0x0, -0x16(%rbp)
movb $0x0, -0x15(%rbp)
movb $0x15, -0x14(%rbp)
movb $0x0, -0x13(%rbp)
movb $0x3, -0x12(%rbp)
movb $0x0, -0x11(%rbp)
movb $0x0, -0x10(%rbp)
movb $0x2, -0xf(%rbp)
leaq -0x18(%rbp), %rdi
movl $0xa, %esi
callq 0xc700
movw %ax, -0xe(%rbp)
movl -0xc(%rbp), %edi
leaq -0x18(%rbp), %rsi
movl $0xc, %edx
callq 0x1050
movl $0x0, -0x1c4(%rbp)
cmpl $0x100, -0x1c4(%rbp)    # imm = 0x100
jge 0x41ee
movslq -0x1c4(%rbp), %rax
movb $0x2a, -0x1c0(%rbp,%rax)
movl -0x1c4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c4(%rbp)
jmp 0x41c2
leaq -0x1c0(%rbp), %rdi
leaq 0xcf34(%rip), %rsi      # 0x11130
movl $0x36, %edx
callq 0x10c0
movl $0x0, -0x82d8(%rbp)
movq -0x82f8(%rbp), %rcx
movl -0x82d8(%rbp), %eax
movzbl 0x145007(%rcx), %ecx
cmpl %ecx, %eax
ja 0x44b2
movl $0x0, -0x82dc(%rbp)
movq -0x82f8(%rbp), %rcx
movl -0x82dc(%rbp), %eax
movzbl 0x145008(%rcx), %ecx
cmpl %ecx, %eax
ja 0x449c
movl $0x0, -0x82d4(%rbp)
movl $0x0, -0x82e0(%rbp)
movb $0x1, -0x82e1(%rbp)
movq -0x82f8(%rbp), %rdi
movl -0x82d8(%rbp), %eax
movb %al, %dl
movl -0x82dc(%rbp), %eax
movl -0x82e0(%rbp), %ecx
leaq -0xa8(%rbp), %r8
movzbl %dl, %esi
movzbl %al, %edx
callq 0x2220
testb $0x1, %al
jne 0x42a0
jmp 0x42fc
testb $0x1, -0x82e1(%rbp)
jne 0x42b8
movl -0x80(%rbp), %eax
cmpl -0x82e8(%rbp), %eax
jne 0x42b6
jmp 0x42fc
jmp 0x42c8
movl -0x80(%rbp), %eax
movl %eax, -0x82e8(%rbp)
movb $0x0, -0x82e1(%rbp)
movl -0x7c(%rbp), %eax
movl %eax, -0x82e0(%rbp)
movq -0x90(%rbp), %rax
movb 0x2(%rax), %cl
movl -0x82d4(%rbp), %eax
movb %cl, -0x82d0(%rbp,%rax)
movl -0x82d4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x82d4(%rbp)
jmp 0x426d
cmpl $0xfe, -0x82d4(%rbp)
jbe 0x4312
movl $0xfe, -0x82d4(%rbp)
movl -0x82d4(%rbp), %eax
movb %al, -0xac(%rbp)
movl -0x82d8(%rbp), %eax
movb %al, -0xab(%rbp)
movl -0x82dc(%rbp), %eax
movb %al, -0xaa(%rbp)
leaq -0xac(%rbp), %rdi
movl $0x3, %esi
callq 0xc700
movzwl %ax, %eax
andl $0xff, %eax
movb %al, -0xa9(%rbp)
movl -0xc(%rbp), %edi
leaq -0xac(%rbp), %rsi
movl $0x4, %edx
callq 0x1050
movl $0x0, -0x82ec(%rbp)
movl -0x82ec(%rbp), %eax
cmpl -0x82d4(%rbp), %eax
jae 0x4486
movq -0x82f8(%rbp), %rdi
movl -0x82d8(%rbp), %eax
movb %al, %dl
movl -0x82dc(%rbp), %eax
movb %al, %cl
movl -0x82ec(%rbp), %eax
leaq -0x70(%rbp), %r8
xorl %r9d, %r9d
movzbl %dl, %esi
movzbl %cl, %edx
movzbl -0x82d0(%rbp,%rax), %ecx
callq 0x25d0
testb $0x1, %al
jne 0x43c2
jmp 0x43dd
leaq -0x81d0(%rbp), %rdi
addq $0x1, %rdi
movq -0x38(%rbp), %rsi
movl -0x30(%rbp), %eax
movl %eax, %edx
callq 0x10c0
jmp 0x43f9
leaq -0x81d0(%rbp), %rdi
addq $0x1, %rdi
leaq -0x1c0(%rbp), %rsi
movl -0x30(%rbp), %eax
movl %eax, %edx
callq 0x10c0
movb $0x0, -0x81d0(%rbp)
leaq -0x81d0(%rbp), %rdi
movl -0x30(%rbp), %esi
addl $0x1, %esi
callq 0xc700
movzwl %ax, %eax
andl $0xff, %eax
movb %al, -0xaf(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movw %ax, -0xae(%rbp)
cmpb $0x0, -0x40(%rbp)
je 0x443f
movq -0x58(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xb4(%rbp)
movb $0x0, -0xb0(%rbp)
movl -0xc(%rbp), %edi
leaq -0xb4(%rbp), %rsi
movl $0x8, %edx
callq 0x1050
movl -0xc(%rbp), %edi
leaq -0x81d0(%rbp), %rsi
movzwl -0xae(%rbp), %eax
movl %eax, %edx
callq 0x1050
movl -0x82ec(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x82ec(%rbp)
jmp 0x4373
jmp 0x4488
movl -0x82dc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x82dc(%rbp)
jmp 0x4236
jmp 0x449e
movl -0x82d8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x82d8(%rbp)
jmp 0x4210
movb $-0x1, -0xac(%rbp)
movb $0x0, -0xab(%rbp)
movb $0x0, -0xaa(%rbp)
movb $0x0, -0xa9(%rbp)
movl -0xc(%rbp), %edi
leaq -0xac(%rbp), %rsi
movl $0x4, %edx
callq 0x1050
addq $0x8300, %rsp           # imm = 0x8300
popq %rbp
retq
nopl (%rax,%rax)