// File: /workspace/github_cpp_makefile_star2_O1/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, 0x4(%rsp)
movq %rdi, 0x28(%rsp)
movabsq $0x3001500004454, %rax  # imm = 0x3001500004454
movq %rax, 0x34(%rsp)
movw $0x200, 0x3c(%rsp)      # imm = 0x200
xorl %eax, %eax
leaq 0x7c91(%rip), %r13      # 0xb1e0
xorl %ecx, %ecx
movzwl %cx, %ecx
movb 0x34(%rsp,%rax), %dl
xorb %cl, %dl
shrl $0x8, %ecx
movzbl %dl, %edx
xorw (%r13,%rdx,2), %cx
incq %rax
cmpl $0xa, %eax
jne 0x3551
rolw $0x8, %cx
leaq 0x34(%rsp), %rsi
movw %cx, 0xa(%rsi)
movl $0xc, %edx
movl 0x4(%rsp), %edi
callq 0x1060
leaq 0x1d0(%rsp), %r15
movl $0x100, %edx            # imm = 0x100
movq %r15, %rdi
movl $0x2a, %esi
callq 0x10a0
movaps 0x7b96(%rip), %xmm0     # 0xb140
movaps %xmm0, (%r15)
movaps 0x7b9b(%rip), %xmm0     # 0xb150
movaps %xmm0, 0x10(%r15)
movaps 0x7b9f(%rip), %xmm0     # 0xb160
movaps %xmm0, 0x20(%r15)
movq 0x7ba1(%rip), %rax      # 0xb16e
movq %rax, 0x2e(%r15)
xorl %eax, %eax
leaq 0x40(%rsp), %r12
movq 0x28(%rsp), %r15
movq %rax, 0x18(%rsp)
movzbl %al, %eax
movl %eax, 0x8(%rsp)
xorl %eax, %eax
movq %rax, 0x20(%rsp)
movzbl %al, %ebp
xorl %r14d, %r14d
movq %r15, %rdi
movl 0x8(%rsp), %esi
movl %ebp, %edx
xorl %ecx, %ecx
movq %r12, %r8
callq 0x1f12
testb %al, %al
je 0x365b
xorl %r14d, %r14d
movb $0x1, %bl
testb $0x1, %bl
je 0x3623
movl 0x68(%rsp), %eax
movl %eax, 0x14(%rsp)
xorl %ebx, %ebx
jmp 0x362d
movl 0x14(%rsp), %eax
cmpl %eax, 0x68(%rsp)
je 0x365b
movl 0x6c(%rsp), %ecx
movq 0x58(%rsp), %rax
movb 0x2(%rax), %al
movl %r14d, %edx
movb %al, 0xd0(%rsp,%rdx)
incl %r14d
movq %r15, %rdi
movl 0x8(%rsp), %esi
movl %ebp, %edx
movq %r12, %r8
callq 0x1f12
testb %al, %al
jne 0x3612
movl $0xfe, %eax
cmpl %eax, %r14d
movl $0xfe, %ebx
cmovbl %r14d, %ebx
movb %bl, (%rsp)
movq 0x18(%rsp), %rax
movb %al, 0x1(%rsp)
movq 0x20(%rsp), %rax
movb %al, 0x2(%rsp)
xorl %eax, %eax
xorl %ecx, %ecx
movzwl %cx, %ecx
movb (%rsp,%rax), %dl
xorb %cl, %dl
shrl $0x8, %ecx
movzbl %dl, %edx
movzwl (%r13,%rdx,2), %edx
xorl %edx, %ecx
incq %rax
cmpl $0x3, %eax
jne 0x3685
movb %dh, 0x3(%rsp)
movl $0x4, %edx
movl 0x4(%rsp), %edi
movq %rsp, %rsi
callq 0x1060
testl %r14d, %r14d
je 0x37c9
movl %ebx, %r12d
xorl %r14d, %r14d
movzbl 0xd0(%rsp,%r14), %ecx
movq %r15, %rdi
movl 0x8(%rsp), %esi
movl %ebp, %edx
leaq 0x78(%rsp), %r8
xorl %r9d, %r9d
callq 0x20e8
testb %al, %al
je 0x3703
movq 0xb0(%rsp), %rsi
movl 0xb8(%rsp), %edx
leaq 0x2d1(%rsp), %rdi
jmp 0x371a
movl 0xb8(%rsp), %edx
leaq 0x2d1(%rsp), %rdi
leaq 0x1d0(%rsp), %rsi
callq 0x10e0
movb $0x0, 0x2d0(%rsp)
movl 0xb8(%rsp), %ebx
cmpl $-0x1, %ebx
je 0x3763
leal 0x1(%rbx), %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 (%r13,%rsi,2), %esi
xorl %esi, %edx
incq %rcx
cmpl %ecx, %eax
jne 0x373a
shrl $0x8, %esi
jmp 0x3765
xorl %esi, %esi
movb %sil, 0x11(%rsp)
incl %ebx
movw %bx, 0x12(%rsp)
cmpb $0x0, 0xa8(%rsp)
je 0x3789
movq 0x90(%rsp), %rax
movl (%rax), %eax
movl %eax, 0xc(%rsp)
movb $0x0, 0x10(%rsp)
movl $0x8, %edx
movl 0x4(%rsp), %r15d
movl %r15d, %edi
leaq 0xc(%rsp), %rsi
callq 0x1060
movzwl %bx, %edx
movl %r15d, %edi
leaq 0x2d0(%rsp), %rsi
callq 0x1060
incq %r14
cmpq %r12, %r14
movq 0x28(%rsp), %r15
jne 0x36c7
movq 0x20(%rsp), %rdx
leal 0x1(%rdx), %eax
movzbl 0x145008(%r15), %ecx
cmpl %ecx, %edx
leaq 0x40(%rsp), %r12
jb 0x35eb
movq 0x18(%rsp), %rdx
leal 0x1(%rdx), %eax
movzbl 0x145007(%r15), %ecx
cmpl %ecx, %edx
jb 0x35dd
movq %rsp, %rsi
movl $0xff, (%rsi)
movl $0x4, %edx
movl 0x4(%rsp), %edi
callq 0x1060
addq $0x82d8, %rsp           # imm = 0x82D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop