// File: /workspace/github_cpp_makefile_star2_O3/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, 0x8(%rsp)
movq %rdi, %r13
movabsq $0x3001500004454, %rax  # imm = 0x3001500004454
movq %rax, 0x28(%rsp)
movw $0x200, 0x30(%rsp)      # imm = 0x200
xorl %eax, %eax
leaq 0x7c55(%rip), %r12      # 0xb1e0
xorl %ecx, %ecx
movzwl %cx, %ecx
movb 0x28(%rsp,%rax), %dl
xorb %cl, %dl
shrl $0x8, %ecx
movzbl %dl, %edx
xorw (%r12,%rdx,2), %cx
incq %rax
cmpl $0xa, %eax
jne 0x358d
rolw $0x8, %cx
leaq 0x28(%rsp), %rsi
movw %cx, 0xa(%rsi)
movl $0xc, %edx
movl 0x8(%rsp), %edi
callq 0x1060
leaq 0x206(%rsp), %r15
movl $0xca, %edx
movq %r15, %rdi
movl $0x2a, %esi
callq 0x10a0
movaps 0x7b5b(%rip), %xmm0     # 0xb140
movaps %xmm0, -0x36(%r15)
movaps 0x7b5f(%rip), %xmm0     # 0xb150
movaps %xmm0, -0x26(%r15)
movaps 0x7b63(%rip), %xmm0     # 0xb160
movaps %xmm0, -0x16(%r15)
movq 0x7b65(%rip), %rax      # 0xb16e
movq %rax, -0x8(%r15)
xorl %r15d, %r15d
leaq 0x40(%rsp), %r14
movzbl %r15b, %eax
movl %eax, 0xc(%rsp)
xorl %eax, %eax
movq %r15, 0x38(%rsp)
movq %rax, 0x20(%rsp)
movzbl %al, %edx
xorl %ebx, %ebx
movq %r13, %rdi
movl 0xc(%rsp), %esi
movl %edx, 0x10(%rsp)
xorl %ecx, %ecx
movq %r14, %r8
callq 0x1f26
testb %al, %al
je 0x368c
xorl %ebx, %ebx
movb $0x1, %cl
movl 0x68(%rsp), %eax
testb $0x1, %cl
je 0x3658
movl %eax, %ebp
jmp 0x365c
cmpl %ebp, %eax
je 0x368c
movl 0x6c(%rsp), %ecx
movq 0x58(%rsp), %rax
movb 0x2(%rax), %al
movl %ebx, %edx
movb %al, 0xd0(%rsp,%rdx)
incl %ebx
movq %r13, %rdi
movl 0xc(%rsp), %esi
movl 0x10(%rsp), %edx
movq %r14, %r8
callq 0x1f26
xorl %ecx, %ecx
testb %al, %al
jne 0x364b
movl %ebp, 0x1c(%rsp)
movl $0xfe, %eax
cmpl %eax, %ebx
movl $0xfe, %r14d
cmovbl %ebx, %r14d
movb %r14b, 0x4(%rsp)
movb %r15b, 0x5(%rsp)
movq 0x20(%rsp), %rax
movb %al, 0x6(%rsp)
xorl %eax, %eax
xorl %ecx, %ecx
movzwl %cx, %ecx
movb 0x4(%rsp,%rax), %dl
xorb %cl, %dl
shrl $0x8, %ecx
movzbl %dl, %edx
movzwl (%r12,%rdx,2), %edx
xorl %edx, %ecx
incq %rax
cmpl $0x3, %eax
jne 0x36b8
movb %dh, 0x7(%rsp)
movl $0x4, %edx
movl 0x8(%rsp), %edi
leaq 0x4(%rsp), %rsi
callq 0x1060
testl %ebx, %ebx
je 0x3800
movl %r14d, %r14d
xorl %ebx, %ebx
movzbl 0xd0(%rsp,%rbx), %ecx
movq %r13, %rbp
movq %r13, %rdi
movl 0xc(%rsp), %esi
movl 0x10(%rsp), %edx
leaq 0x78(%rsp), %r8
xorl %r9d, %r9d
callq 0x20f6
testb %al, %al
je 0x373b
movq 0xb0(%rsp), %rsi
movl 0xb8(%rsp), %r13d
leaq 0x2d1(%rsp), %rdi
jmp 0x3753
movl 0xb8(%rsp), %r13d
leaq 0x2d1(%rsp), %rdi
leaq 0x1d0(%rsp), %rsi
movq %r13, %rdx
callq 0x10e0
movb $0x0, 0x2d0(%rsp)
cmpl $-0x1, %r13d
je 0x3799
leal 0x1(%r13), %eax
xorl %ecx, %ecx
xorl %edx, %edx
movzwl %dx, %edx
movb 0x2d0(%rsp,%rcx), %sil
xorb %dl, %sil
shrl $0x8, %edx
movzbl %sil, %esi
movzwl (%r12,%rsi,2), %esi
xorl %esi, %edx
incq %rcx
cmpl %ecx, %eax
jne 0x3771
shrl $0x8, %esi
jmp 0x379b
xorl %esi, %esi
movb %sil, 0x19(%rsp)
incl %r13d
movw %r13w, 0x1a(%rsp)
cmpb $0x0, 0xa8(%rsp)
je 0x37c1
movq 0x90(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x14(%rsp)
movb $0x0, 0x18(%rsp)
movl $0x8, %edx
movl 0x8(%rsp), %r15d
movl %r15d, %edi
leaq 0x14(%rsp), %rsi
callq 0x1060
movzwl %r13w, %edx
movl %r15d, %edi
leaq 0x2d0(%rsp), %rsi
callq 0x1060
incq %rbx
cmpq %r14, %rbx
movq %rbp, %r13
jne 0x36fa
movq 0x20(%rsp), %rdx
leal 0x1(%rdx), %eax
movzbl 0x145008(%r13), %ecx
cmpl %ecx, %edx
leaq 0x40(%rsp), %r14
movq 0x38(%rsp), %r15
movl 0x1c(%rsp), %ebp
jb 0x3624
leal 0x1(%r15), %eax
movzbl 0x145007(%r13), %ecx
cmpl %ecx, %r15d
movl %eax, %r15d
jb 0x3615
leaq 0x4(%rsp), %rsi
movl $0xff, (%rsi)
movl $0x4, %edx
movl 0x8(%rsp), %edi
callq 0x1060
addq $0x82d8, %rsp           # imm = 0x82D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop