// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x82d8, %rsp           # imm = 0x82D8
movl %esi, %ebx
movq %rdi, 0x10(%rsp)
movabsq $0x3001500004454, %rax  # imm = 0x3001500004454
leaq 0x30(%rsp), %r15
movq %rax, (%r15)
movw $0x200, 0x8(%r15)       # imm = 0x200
pushq $0xa
popq %rsi
movq %r15, %rdi
callq 0x6484
movw %ax, 0xa(%r15)
pushq $0xc
popq %rdx
movl %ebx, %edi
movq %r15, %rsi
callq 0x1050
xorl %eax, %eax
cmpq $0x100, %rax            # imm = 0x100
je 0x2dab
movb $0x2a, 0xd0(%rsp,%rax)
incq %rax
jmp 0x2d96
movq 0x73ac(%rip), %rax      # 0xa15e
leaq 0xd0(%rsp), %rcx
movq %rax, 0x2e(%rcx)
movaps 0x738b(%rip), %xmm0     # 0xa150
movaps %xmm0, 0x20(%rcx)
movaps 0x7370(%rip), %xmm0     # 0xa140
movaps %xmm0, 0x10(%rcx)
movaps 0x7355(%rip), %xmm0     # 0xa130
movaps %xmm0, (%rcx)
xorl %ecx, %ecx
movq 0x10(%rsp), %rax
movzbl 0x145007(%rax), %eax
cmpl %eax, %ecx
ja 0x2fab
xorl %edx, %edx
movl %ecx, 0xc(%rsp)
movzbl %cl, %ebp
movq 0x10(%rsp), %rax
movzbl 0x145008(%rax), %eax
cmpl %eax, %edx
ja 0x2fa0
movb $0x1, %r12b
xorl %r14d, %r14d
movq %rdx, 0x28(%rsp)
movzbl %dl, %r13d
xorl %ecx, %ecx
movq 0x10(%rsp), %rdi
movl %ebp, %esi
movl %r13d, %edx
leaq 0x40(%rsp), %r8
callq 0x1bca
movl %r14d, %r15d
testb %al, %al
je 0x2e6f
movl 0x68(%rsp), %eax
testb $0x1, %r12b
je 0x2e4d
movl %eax, 0x24(%rsp)
jmp 0x2e53
cmpl 0x24(%rsp), %eax
je 0x2e6f
movl 0x6c(%rsp), %ecx
movq 0x58(%rsp), %rax
movb 0x2(%rax), %al
movb %al, 0x1d0(%rsp,%r15)
incl %r14d
xorl %r12d, %r12d
jmp 0x2e22
movl $0xfe, %eax
cmpl %eax, %r14d
cmovael %eax, %r14d
movb %r14b, 0x8(%rsp)
movl 0xc(%rsp), %eax
movb %al, 0x9(%rsp)
movq 0x28(%rsp), %rax
movb %al, 0xa(%rsp)
leaq 0x8(%rsp), %r14
movq %r14, %rdi
pushq $0x3
popq %rsi
callq 0x6484
movb %al, 0xb(%rsp)
movl %ebx, %edi
movq %r14, %rsi
pushq $0x4
popq %rdx
callq 0x1050
movl $0xfe, %eax
cmpq %rax, %r15
cmovaeq %rax, %r15
xorl %r14d, %r14d
cmpq %r14, %r15
je 0x2f94
movzbl 0x1d0(%rsp,%r14), %ecx
movq 0x10(%rsp), %rdi
movl %ebp, %esi
movl %r13d, %edx
leaq 0x78(%rsp), %r8
xorl %r9d, %r9d
callq 0x1d64
testb %al, %al
je 0x2f08
movq 0xb0(%rsp), %rsi
movl 0xb8(%rsp), %r12d
leaq 0x2d1(%rsp), %rdi
jmp 0x2f20
movl 0xb8(%rsp), %r12d
leaq 0x2d1(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
movq %r12, %rdx
callq 0x10d0
movb $0x0, 0x2d0(%rsp)
incl %r12d
leaq 0x2d0(%rsp), %rdi
movl %r12d, %esi
callq 0x6484
movb %al, 0x21(%rsp)
movw %r12w, 0x22(%rsp)
cmpb $0x0, 0xa8(%rsp)
je 0x2f65
movq 0x90(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x1c(%rsp)
movb $0x0, 0x20(%rsp)
movl %ebx, %edi
leaq 0x1c(%rsp), %rsi
pushq $0x8
popq %rdx
callq 0x1050
movzwl %r12w, %edx
movl %ebx, %edi
leaq 0x2d0(%rsp), %rsi
callq 0x1050
incq %r14
jmp 0x2ec1
movq 0x28(%rsp), %rdx
incl %edx
jmp 0x2dfd
movl 0xc(%rsp), %ecx
incl %ecx
jmp 0x2de0
leaq 0x8(%rsp), %rsi
movl $0xff, (%rsi)
pushq $0x4
popq %rdx
movl %ebx, %edi
callq 0x1050
addq $0x82d8, %rsp           # imm = 0x82D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq