// File: /workspace/github_cpp_makefile_star2_O3/atsidaev[P]trx2x/DiskImage.cpp
// Function: TDiskImage::formatTRDOS(unsigned int, unsigned int)
void TDiskImage::formatTRDOS(unsigned int Tcount, unsigned int Scount)
{
   MaxTrack = Tcount-1;
   MaxSide = Scount-1;

   unsigned short TotalSecs = Tcount*Scount * 16 - 16;

// форматирование нового диска под TR-DOS (16 x 256bytes sector per track)...
   unsigned int ptrcrc;
   unsigned int r;
   unsigned short vgcrc;
   for(unsigned int trk=0; trk <= unsigned(MaxTrack); trk++)
      for(unsigned int side=0; side <= unsigned(MaxSide); side++)
      {
         FTrackLength[trk][side] = 6250;

         FTracksPtr[trk][side][0] = (unsigned char*)new char[FTrackLength[trk][side]+1024];      // trk img
         FTracksPtr[trk][side][1] = (unsigned char*)new char[FTrackLength[trk][side]+1024];      // clk img

         unsigned int tptr=0;
         for(int sec=0; sec < 16; sec++)
         {
            for(r=0; r < 10; r++)        // Первый пробел
            {
               FTracksPtr[trk][side][0][tptr] = 0x4E;
               FTracksPtr[trk][side][1][tptr++] = 0x00;
            }
            for(r=0; r < 12; r++)        // Синхропромежуток
            {
               FTracksPtr[trk][side][0][tptr] = 0x00;
               FTracksPtr[trk][side][1][tptr++] = 0x00;
            }
            ptrcrc = tptr;
            for(r=0; r < 3; r++)        // Синхроимпульс
            {
               FTracksPtr[trk][side][0][tptr] = 0xA1;
               FTracksPtr[trk][side][1][tptr++] = 0xFF;
            }
            FTracksPtr[trk][side][0][tptr] = 0xFE;   // Метка "Адрес"
            FTracksPtr[trk][side][1][tptr++] = 0x00;

            FTracksPtr[trk][side][0][tptr] = (unsigned char)trk; // cyl
            FTracksPtr[trk][side][1][tptr++] = 0x00;
            FTracksPtr[trk][side][0][tptr] = (unsigned char)0x00; // head (TR always 0)
            FTracksPtr[trk][side][1][tptr++] = 0x00;
            FTracksPtr[trk][side][0][tptr] = (unsigned char)(sec+1); // secN
            FTracksPtr[trk][side][1][tptr++] = 0x00;
            FTracksPtr[trk][side][0][tptr] = (unsigned char)0x01; // len=256b
            FTracksPtr[trk][side][1][tptr++] = 0x00;

            vgcrc = MakeVGCRC(FTracksPtr[trk][side][0]+ptrcrc, tptr-ptrcrc);
            FTracksPtr[trk][side][0][tptr] = (unsigned char)(vgcrc>>8); // VG93 CRC
            FTracksPtr[trk][side][1][tptr++] = 0x00;
            FTracksPtr[trk][side][0][tptr] = (unsigned char)(vgcrc&0xFF);
            FTracksPtr[trk][side][1][tptr++] = 0x00;

            for(r=0; r < 22; r++)        // Второй пробел
            {
               FTracksPtr[trk][side][0][tptr] = 0x4E;
               FTracksPtr[trk][side][1][tptr++] = 0x00;
            }
            for(r=0; r < 12; r++)        // Синхропромежуток
            {
               FTracksPtr[trk][side][0][tptr] = 0x00;
               FTracksPtr[trk][side][1][tptr++] = 0x00;
            }
            ptrcrc = tptr;
            for(r=0; r < 3; r++)        // Синхроимпульс
            {
               FTracksPtr[trk][side][0][tptr] = 0xA1;
               FTracksPtr[trk][side][1][tptr++] = 0xFF;
            }
            FTracksPtr[trk][side][0][tptr] = 0xFB;   // Метка "Данные"
            FTracksPtr[trk][side][1][tptr++] = 0x00;

            for(r=0; r < 256; r++)        // сектор 256байт
            {
               FTracksPtr[trk][side][0][tptr] = 0x00;
               FTracksPtr[trk][side][1][tptr++] = 0x00;
            }
            if( (trk==0)&&(side==0)&&(sec==8) )      // make TR-DOS id
            {
               int ssec = tptr-256;
               FTracksPtr[trk][side][0][ssec+0xE1] = 0x00; // first free SECT
               FTracksPtr[trk][side][0][ssec+0xE2] = 0x01; // first free TRACK
               FTracksPtr[trk][side][0][ssec+0xE3] = 0x16; // 80trk DS
               FTracksPtr[trk][side][0][ssec+0xE4] = 0x00; // file count
               *(unsigned short*)(FTracksPtr[trk][side][0]+ssec+0xE5) 
                                                   = TotalSecs; // free SECS count
               FTracksPtr[trk][side][0][ssec+0xE7] = 0x10; // TR-DOS id
               FTracksPtr[trk][side][0][ssec+0xF4] = 0x00; // deleted file count

               memcpy(FTracksPtr[trk][side][0]+ssec+0xF5, 
                  STR_CREATEDISKNAME"               ",8); // disk name
               FTracksPtr[trk][side][0][ssec+0xFD] = 0x00; // zero
               FTracksPtr[trk][side][0][ssec+0xFE] = 0x00; // zero
               FTracksPtr[trk][side][0][ssec+0xFF] = 0x00; // zero
            }

            vgcrc = MakeVGCRC(FTracksPtr[trk][side][0]+ptrcrc, tptr-ptrcrc);
            FTracksPtr[trk][side][0][tptr] = (unsigned char)(vgcrc>>8); // VG93 CRC
            FTracksPtr[trk][side][1][tptr++] = 0x00;
            FTracksPtr[trk][side][0][tptr] = (unsigned char)(vgcrc&0xFF);
            FTracksPtr[trk][side][1][tptr++] = 0x00;

            for(r=0; r < 60; r++)        // Третий пробел
            {
               FTracksPtr[trk][side][0][tptr] = 0x4E;
               FTracksPtr[trk][side][1][tptr++] = 0x00;
            }
         }
         for(int eoftrk=tptr; eoftrk < 6250; eoftrk++) 
         {
            FTracksPtr[trk][side][0][tptr] = 0x4E;
            FTracksPtr[trk][side][1][tptr++] = 0x00;
         }
      }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
leal -0x1(%rsi), %eax
movb %al, 0x145007(%rdi)
leal -0x1(%rdx), %eax
movb %al, 0x145008(%rdi)
imull %esi, %edx
shll $0x4, %edx
addl $-0x10, %edx
movq %rdx, 0x28(%rsp)
leaq 0x40000(%rdi), %rax
movq %rax, (%rsp)
xorl %r12d, %r12d
xorl %r13d, %r13d
movq %rdi, 0x8(%rsp)
movq %r13, %rax
shlq $0xa, %rax
addq %rdi, %rax
movq %rax, 0x18(%rsp)
movq %r13, %rax
shlq $0xc, %rax
addq (%rsp), %rax
movq %rax, 0x10(%rsp)
xorl %ecx, %ecx
movq 0x18(%rsp), %rax
movl $0x186a, (%rax,%rcx,4)  # imm = 0x186A
movl $0x1c6a, %edi           # imm = 0x1C6A
movq %rcx, %rbp
callq 0x1030
movq %rbp, %rbx
shlq $0x4, %rbx
movq 0x10(%rsp), %r15
leaq (%r15,%rbx), %r14
movq %rax, (%rbx,%r15)
movl $0x1c6a, %edi           # imm = 0x1C6A
callq 0x1030
movq %rax, 0x8(%rbx,%r15)
movq %rbp, 0x20(%rsp)
orq %r13, %rbp
movq %rbp, 0x30(%rsp)
xorl %edx, %edx
xorl %ecx, %ecx
movl %edx, %esi
leal 0x9(%rcx), %r8d
leal 0xd(%rcx), %r10d
leal 0x10(%rcx), %ebx
xorl %edi, %edi
movl %r8d, %eax
movl %r10d, %edx
movl %ebx, %r9d
leal (%rcx,%rdi), %r8d
movq (%r14), %r10
movb $0x4e, (%r10,%r8)
movq 0x8(%r14), %r10
movb $0x0, (%r10,%r8)
incl %edi
leal 0x1(%rax), %r8d
leal 0x1(%rdx), %r10d
leal 0x1(%r9), %ebx
cmpl $0xa, %edi
jne 0x6e9d
addl %edi, %ecx
movl $0xc, %r10d
movl %eax, %edi
movl %edx, %ebx
movl %r9d, %r8d
movq (%r14), %rax
movl %ecx, %edx
movb %r12b, (%rax,%rdx)
movq 0x8(%r14), %rax
incl %ecx
movb %r12b, (%rax,%rdx)
leal 0x1(%rdi), %eax
leal 0x1(%rbx), %edx
leal 0x1(%r8), %r9d
decl %r10d
jne 0x6ed6
movl $0x3, %r15d
movl %ebx, %r9d
movl %r8d, %r10d
leal -0x7(%rdi), %eax
movq (%r14), %rdx
movb $-0x5f, (%rdx,%rax)
movq 0x8(%r14), %rdx
movb $-0x1, (%rdx,%rax)
incl %edi
leal 0x1(%r9), %ebx
leal 0x1(%r10), %r8d
decl %r15d
jne 0x6f05
movq (%r14), %rax
leal -0x7(%rdi), %edx
movb $-0x2, (%rax,%rdx)
movq 0x8(%r14), %rax
leal -0x6(%rdi), %r8d
movb %r12b, (%rax,%rdx)
movq (%r14), %rax
movq %r13, %r11
movb %r13b, (%rax,%r8)
movq 0x8(%r14), %rax
leal -0x5(%rdi), %edx
movb %r12b, (%rax,%r8)
movq (%r14), %rax
movb %r12b, (%rax,%rdx)
movq 0x8(%r14), %rax
leal -0x4(%rdi), %r8d
movb %r12b, (%rax,%rdx)
leal 0x1(%rsi), %edx
movq (%r14), %rax
movb %dl, (%rax,%r8)
movq 0x8(%r14), %rax
leal -0x3(%rdi), %ebx
movb %r12b, (%rax,%r8)
movq (%r14), %rax
movb $0x1, (%rax,%rbx)
movq 0x8(%r14), %rax
movb %r12b, (%rax,%rbx)
movq (%r14), %rbx
movw $0xffff, %ax            # imm = 0xFFFF
cmpl $0x8, %r15d
je 0x6fde
movl %ecx, %ecx
addq %rbx, %rcx
movw $0xffff, %ax            # imm = 0xFFFF
xorl %r8d, %r8d
movzbl (%rcx,%r8), %r15d
shll $0x8, %r15d
xorl %r15d, %eax
movl $0x8, %r15d
leal (%rax,%rax), %ebp
movl %ebp, %r13d
xorl $0x1021, %r13d          # imm = 0x1021
testw %ax, %ax
cmovnsl %ebp, %r13d
movl %r13d, %eax
decl %r15d
jne 0x6fb9
incq %r8
cmpq $0x8, %r8
jne 0x6fa7
leal -0x2(%rdi), %ecx
movl %ecx, %ecx
movb %ah, (%rbx,%rcx)
movq 0x8(%r14), %r8
leal -0x1(%rdi), %ebx
movb %r12b, (%r8,%rcx)
movq (%r14), %rcx
movb %al, (%rcx,%rbx)
movq 0x8(%r14), %rax
movb %r12b, (%rax,%rbx)
movl $0x16, %eax
movl %edi, %r8d
movl %r9d, %ecx
movl %r10d, %ebx
movq (%r14), %rdi
movl %r8d, %r9d
movb $0x4e, (%rdi,%r9)
movq 0x8(%r14), %r10
leal 0x1(%r8), %edi
movb $0x0, (%r10,%r9)
leal 0x1(%rcx), %r9d
leal 0x1(%rbx), %r10d
decl %eax
jne 0x7004
movl $0xc, %edi
movq %r11, %r13
movl %ecx, %r9d
movl %ebx, %eax
incl %r8d
movq (%r14), %rcx
movb %r12b, (%rcx,%r8)
movq 0x8(%r14), %rcx
movb %r12b, (%rcx,%r8)
leal 0x1(%r9), %ecx
leal 0x1(%rax), %ebx
decl %edi
jne 0x7039
incl %r8d
movl $0x3, %r10d
movl $0xfffffffe, %ebx       # imm = 0xFFFFFFFE
movl %ebx, %edi
movl %eax, %ecx
leal -0x1(%r9), %eax
movq (%r14), %rbx
movb $-0x5f, (%rbx,%rax)
movq 0x8(%r14), %rbx
movb $-0x1, (%rbx,%rax)
incl %r9d
leal -0x1(%rdi), %ebx
leal 0x1(%rcx), %eax
decl %r10d
jne 0x7069
movq (%r14), %rax
decl %r9d
movb $-0x5, (%rax,%r9)
movq 0x8(%r14), %rax
movb $0x0, (%rax,%r9)
movl $0x100, %eax            # imm = 0x100
leal -0x2(%rcx), %r9d
movq (%r14), %r10
movb %r12b, (%r10,%r9)
movq 0x8(%r14), %r10
movb %r12b, (%r10,%r9)
decl %edi
incl %ecx
decl %eax
jne 0x70a7
xorl $0x8, %esi
orl 0x30(%rsp), %esi
jne 0x717a
leal -0x3(%rcx), %eax
leal -0x102(%rcx), %esi
movq (%r14), %r9
leal -0x21(%rcx), %r10d
movslq %r10d, %r10
movb %r12b, (%r9,%r10)
movq (%r14), %r9
leal -0x20(%rcx), %r10d
movslq %r10d, %r10
movb $0x1, (%r9,%r10)
movq (%r14), %r9
leal -0x1f(%rcx), %r10d
movslq %r10d, %r10
movb $0x16, (%r9,%r10)
movq (%r14), %r9
leal -0x1e(%rcx), %r10d
movslq %r10d, %r10
movb %r12b, (%r9,%r10)
movq (%r14), %r9
movslq %esi, %rsi
movq 0x28(%rsp), %r10
movw %r10w, 0xe5(%r9,%rsi)
leal -0x1b(%rcx), %r10d
movslq %r10d, %r10
movb $0x10, (%r9,%r10)
movq (%r14), %r9
leal -0xe(%rcx), %r10d
movslq %r10d, %r10
movb %r12b, (%r9,%r10)
movq (%r14), %r9
movabsq $0x2020204b414d585a, %r10 # imm = 0x2020204B414D585A
movq %r10, 0xf5(%r9,%rsi)
movq (%r14), %rsi
leal -0x5(%rcx), %r9d
movslq %r9d, %r9
movb %r12b, (%rsi,%r9)
movq (%r14), %rsi
leal -0x4(%rcx), %r9d
movslq %r9d, %r9
movb %r12b, (%rsi,%r9)
movq (%r14), %rsi
cltq
movb %r12b, (%rsi,%rax)
movq (%r14), %rsi
testl %edi, %edi
je 0x71c9
movl %r8d, %edi
addq %rsi, %rdi
movw $0xffff, %ax            # imm = 0xFFFF
xorl %r8d, %r8d
movzbl (%rdi,%r8), %r9d
shll $0x8, %r9d
xorl %r9d, %eax
movl $0x8, %r9d
leal (%rax,%rax), %r10d
movl %r10d, %ebx
xorl $0x1021, %ebx           # imm = 0x1021
testw %ax, %ax
cmovnsl %r10d, %ebx
movl %ebx, %eax
decl %r9d
jne 0x71a0
incq %r8
cmpq $0x104, %r8             # imm = 0x104
jne 0x718e
jmp 0x71cd
movw $0xffff, %ax            # imm = 0xFFFF
leal -0x2(%rcx), %edi
movl %edi, %edi
movb %ah, (%rsi,%rdi)
movq 0x8(%r14), %rsi
leal -0x1(%rcx), %r8d
movb %r12b, (%rsi,%rdi)
movq (%r14), %rsi
movb %al, (%rsi,%r8)
movq 0x8(%r14), %rax
movb %r12b, (%rax,%r8)
movl $0x3c, %eax
movq (%r14), %rsi
movl %ecx, %edi
movb $0x4e, (%rsi,%rdi)
movq 0x8(%r14), %rsi
incl %ecx
movb $0x0, (%rsi,%rdi)
decl %eax
jne 0x71f5
cmpl $0x10, %edx
jne 0x6e8e
cmpl $0x1869, %ecx           # imm = 0x1869
movq 0x8(%rsp), %rdi
jg 0x7240
movq (%r14), %rax
movl %ecx, %edx
movb $0x4e, (%rax,%rdx)
movq 0x8(%r14), %rax
leal 0x1(%rcx), %esi
movb $0x0, (%rax,%rdx)
cmpl $0x1869, %ecx           # imm = 0x1869
movl %esi, %ecx
jl 0x7222
movq 0x20(%rsp), %rdx
leaq 0x1(%rdx), %rax
movzbl 0x145008(%rdi), %ecx
cmpq %rcx, %rdx
movq %rax, %rcx
jb 0x6e41
leaq 0x1(%r13), %rax
movzbl 0x145007(%rdi), %ecx
cmpq %rcx, %r13
movq %rax, %r13
jb 0x6e20
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq