// File: /workspace/github_cpp_makefile_star2_O2/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 $0x58, %rsp
leal -0x1(%rsi), %ecx
movb %cl, 0x145007(%rdi)
leal -0x1(%rdx), %eax
movb %al, 0x145008(%rdi)
imull %esi, %edx
shll $0x4, %edx
addl $-0x10, %edx
movq %rdx, 0x38(%rsp)
movq %rdi, (%rsp)
leaq 0x40000(%rdi), %rdx
movq %rdx, 0x18(%rsp)
xorl %r12d, %r12d
xorl %edi, %edi
movzbl %cl, %ecx
cmpq %rcx, %rdi
ja 0x6475
movq %rdi, %rcx
shlq $0xa, %rcx
addq (%rsp), %rcx
movq %rcx, 0x28(%rsp)
movq %rdi, %rcx
shlq $0xc, %rcx
addq 0x18(%rsp), %rcx
movq %rcx, 0x20(%rsp)
xorl %r15d, %r15d
movq %rdi, 0x10(%rsp)
movzbl %al, %ecx
cmpq %rcx, %r15
ja 0x6463
movq 0x28(%rsp), %rax
movl $0x186a, (%rax,%r15,4)  # imm = 0x186A
movl $0x1c6a, %edi           # imm = 0x1C6A
callq 0x1030
movq %r15, %rbx
shlq $0x4, %rbx
movq 0x20(%rsp), %r14
leaq (%r14,%rbx), %r13
movq %rax, (%rbx,%r14)
movl $0x1c6a, %edi           # imm = 0x1C6A
callq 0x1030
movq 0x10(%rsp), %rdi
movq %rax, 0x8(%rbx,%r14)
movq %r15, 0x30(%rsp)
orq %rdi, %r15
movq %r15, 0x40(%rsp)
xorl %eax, %eax
xorl %r8d, %r8d
cmpl $0x10, %r8d
je 0x6445
leal 0xa(%rax), %r14d
xorl %ecx, %ecx
leal (%rax,%rcx), %r15d
cmpl $0xa, %ecx
je 0x616a
movq (%r13), %rdx
movl %r15d, %esi
movb $0x4e, (%rdx,%rsi)
movq 0x8(%r13), %rdx
movb $0x0, (%rdx,%rsi)
incl %ecx
incl %r14d
jmp 0x6147
pushq $0xc
popq %rax
movl %r15d, %esi
subl $0x1, %eax
jb 0x618d
movq (%r13), %rcx
movb %r12b, (%rcx,%rsi)
movq 0x8(%r13), %rcx
incl %r15d
movb %r12b, (%rcx,%rsi)
incl %r14d
jmp 0x616d
xorl %ebp, %ebp
pushq $0x5
popq %rdx
leal (%r15,%rbp), %eax
movq (%r13), %rcx
cmpl $0x3, %ebp
je 0x61b5
movb $-0x5f, (%rcx,%rax)
movq 0x8(%r13), %rcx
movb $-0x1, (%rcx,%rax)
incl %ebp
incq %rdx
incl %r14d
jmp 0x6192
movb $-0x2, (%rcx,%rax)
movq 0x8(%r13), %rcx
movb %r12b, (%rcx,%rax)
movq (%r13), %rax
leal (%r15,%rbp), %ecx
incl %ecx
movb %dil, (%rax,%rcx)
movq 0x8(%r13), %rax
leal 0x2(%r15,%rbp), %edi
movb %r12b, (%rax,%rcx)
movq (%r13), %rax
movb %r12b, (%rax,%rdi)
movq 0x8(%r13), %rax
leal 0x3(%r15,%rbp), %ecx
movb %r12b, (%rax,%rdi)
movq %r8, 0x50(%rsp)
leal 0x1(%r8), %edi
movq (%r13), %rax
movl %edi, 0xc(%rsp)
movb %dil, (%rax,%rcx)
movq 0x8(%r13), %rax
leal 0x4(%r15,%rbp), %edi
movb %r12b, (%rax,%rcx)
movq (%r13), %rax
movb $0x1, (%rax,%rdi)
movq 0x8(%r13), %rax
leal 0x5(%r15,%rbp), %ecx
movq %rcx, 0x48(%rsp)
movb %r12b, (%rax,%rdi)
movq (%r13), %rbx
addq %rbx, %rsi
callq 0x1a98
movq 0x48(%rsp), %rsi
movb %ah, (%rbx,%rsi)
movq 0x8(%r13), %rcx
leal 0x6(%r15,%rbp), %edx
movb %r12b, (%rcx,%rsi)
movq (%r13), %rcx
movb %al, (%rcx,%rdx)
movq 0x8(%r13), %rax
leal 0x7(%r15,%rbp), %ecx
movb %r12b, (%rax,%rdx)
pushq $0x16
popq %rax
subl $0x1, %eax
jb 0x6283
movq (%r13), %rdx
movl %ecx, %esi
movb $0x4e, (%rdx,%rsi)
movq 0x8(%r13), %rdx
incl %ecx
movb $0x0, (%rdx,%rsi)
incl %r14d
jmp 0x6265
xorl %edx, %edx
leal (%rcx,%rdx), %eax
cmpl $0xc, %edx
je 0x62a6
movq (%r13), %rsi
movl %eax, %eax
movb %r12b, (%rsi,%rax)
movq 0x8(%r13), %rsi
movb %r12b, (%rsi,%rax)
incl %edx
incl %r14d
jmp 0x6285
pushq $0x1
popq %rdx
pushq $0x3
popq %rcx
movl %eax, %esi
movq (%r13), %r8
movl %esi, %edi
subl $0x1, %ecx
jb 0x62d2
movb $-0x5f, (%r8,%rdi)
movq 0x8(%r13), %r8
leal 0x1(%rdi), %esi
movb $-0x1, (%r8,%rdi)
incl %r14d
incq %rdx
jmp 0x62ae
movb $-0x5, (%r8,%rdi)
movq 0x8(%r13), %rcx
movb $0x0, (%rcx,%rdi)
movl $0x100, %ecx            # imm = 0x100
movq 0x50(%rsp), %r8
leal -0x2(%r14), %ebp
subl $0x1, %ecx
jb 0x630c
movq (%r13), %rsi
movl %ebp, %edi
movb %r12b, (%rsi,%rdi)
movq 0x8(%r13), %rsi
movb %r12b, (%rsi,%rdi)
incl %r14d
incq %rdx
jmp 0x62e9
xorl $0x8, %r8d
orl 0x40(%rsp), %r8d
jne 0x63d3
leal -0x3(%r14), %ecx
leal -0x102(%r14), %esi
movq (%r13), %rdi
leal -0x21(%r14), %r8d
movslq %r8d, %r8
movb %r12b, (%rdi,%r8)
movq (%r13), %rdi
leal -0x20(%r14), %r8d
movslq %r8d, %r8
movb $0x1, (%rdi,%r8)
movq (%r13), %rdi
leal -0x1f(%r14), %r8d
movslq %r8d, %r8
movb $0x16, (%rdi,%r8)
movq (%r13), %rdi
leal -0x1e(%r14), %r8d
movslq %r8d, %r8
movb %r12b, (%rdi,%r8)
movq (%r13), %rdi
movslq %esi, %rsi
movq 0x38(%rsp), %r8
movw %r8w, 0xe5(%rdi,%rsi)
leal -0x1b(%r14), %r8d
movslq %r8d, %r8
movb $0x10, (%rdi,%r8)
movq (%r13), %rdi
leal -0xe(%r14), %r8d
movslq %r8d, %r8
movb %r12b, (%rdi,%r8)
movq (%r13), %rdi
movabsq $0x2020204b414d585a, %r8 # imm = 0x2020204B414D585A
movq %r8, 0xf5(%rdi,%rsi)
movq (%r13), %rsi
leal -0x5(%r14), %edi
movslq %edi, %rdi
movb %r12b, (%rsi,%rdi)
movq (%r13), %rsi
leal -0x4(%r14), %edi
movslq %edi, %rdi
movb %r12b, (%rsi,%rdi)
movq (%r13), %rsi
movslq %ecx, %rcx
movb %r12b, (%rsi,%rcx)
movq (%r13), %rbx
movl %eax, %esi
addq %rbx, %rsi
callq 0x1a98
movl %ebp, %edx
movb %ah, (%rbx,%rdx)
movq 0x8(%r13), %rsi
leal -0x1(%r14), %edi
xorl %ecx, %ecx
movb %cl, (%rsi,%rdx)
movq (%r13), %rdx
movb %al, (%rdx,%rdi)
movq 0x8(%r13), %rax
movb %cl, (%rax,%rdi)
movq 0x10(%rsp), %rdi
leal (%r14,%rcx), %eax
cmpl $0x3c, %ecx
je 0x6425
movq (%r13), %rdx
movl %eax, %eax
movb $0x4e, (%rdx,%rax)
movq 0x8(%r13), %rdx
movb $0x0, (%rdx,%rax)
incl %ecx
jmp 0x6406
movl 0xc(%rsp), %ecx
movl %ecx, %r8d
jmp 0x6137
movq (%r13), %rcx
movl %eax, %edx
movb $0x4e, (%rcx,%rdx)
movq 0x8(%r13), %rcx
incl %eax
movb $0x0, (%rcx,%rdx)
cmpl $0x186a, %eax           # imm = 0x186A
jl 0x6431
movq 0x30(%rsp), %r15
incq %r15
movq (%rsp), %rax
movb 0x145008(%rax), %al
jmp 0x60da
incq %rdi
movq (%rsp), %rcx
movb 0x145007(%rcx), %cl
jmp 0x60a5
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq