// File: /workspace/github_cpp_makefile_star2_O3/atsidaev[P]trx2x/DiskImage.cpp
// Function: TDiskImage::writeSCL(int)
void TDiskImage::writeSCL(int hfile)
{
   VGFIND_SECTOR vgfs;

   if(!FindSector(0, 0, 9, &vgfs)) 
   {
      cout << "DANGER! Sector 9 on track 0, side 0 not found!" << endl;
      return;
   }
   else if((!vgfs.CRCOK)||(!vgfs.vgfa.CRCOK)) cout << "Warning: sector 9 on track 0, side 0 with BAD CRC!" << endl;
   if(vgfs.SectorLength!=256) cout << "DANGER! Sector 9 on track 0, side 0 is non 256 bytes!" << endl;


   if(vgfs.SectorPointer[0xE7] != 0x10) 
   {
      cout << "Error: source disk image in non TR-DOS format!" << endl;
      return;
   }
   unsigned int FileCount = unsigned(vgfs.SectorPointer[0xE4]);
   if(FileCount > 144) 
   {
      cout << "Error: source disk image contain incorrect sector 9, track 0, side 0!" << endl;
      return;
   }

   unsigned int SideCount = 2;
   if((vgfs.SectorPointer[0xE3] == 0x18)||(vgfs.SectorPointer[0xE3] == 0x19))
      SideCount = 1;
   if((vgfs.SectorPointer[0xE3] != 0x16)&&(vgfs.SectorPointer[0xE3] != 0x17)&&
      (vgfs.SectorPointer[0xE3] != 0x18)&&(vgfs.SectorPointer[0xE3] != 0x19) )
   {
      cout << "Error: source disk image in non TR-DOS format!" << endl;
      return;
   }

   unsigned i,j;
   uint32_t SCLCRC=0;
   unsigned char buf[256*16];
   memcpy(buf, "SINCLAIR", 8);
   buf[8] = (unsigned char)FileCount;
   for(i=0; i < 9; i++) SCLCRC += unsigned(buf[i]);

   write(hfile, buf, 9);

// prepare nullbuf...
   unsigned char nullbuf[256];
   for(i=0; i < 256; i++) nullbuf[i] = '*';
   memcpy(nullbuf, errsect, sizeof(errsect));
   TRDOS_DIR_ELEMENT nulldir;
   memcpy(nulldir.FileName, "WRONGSEC", 8);
   nulldir.Type = 'E';
   nulldir.Start = 0xEEEE;
   nulldir.Length = 0xEEEE;
   nulldir.SecLen = 0;
   nulldir.FirstSec=0x00;
   nulldir.FirstSec=0x00;

   TRDOS_DIR_ELEMENT fileinfo[256];
   for(i=0; i < FileCount; i++)
   {
      if(FindSector(0, 0, ((i*16)/256)+1, &vgfs))
      {
         memcpy( &fileinfo[i], vgfs.SectorPointer+ (i*16)%256, 16);
         if((!vgfs.CRCOK)||(!vgfs.vgfa.CRCOK)) cout << "Warning: sector "<<(((i*16)/256)+1)<<" on track 0, side 0 with BAD CRC!" << endl;
         if(vgfs.SectorLength!=256) cout << "Warning: sector "<<(((i*16)/256)+1)<<" on track 0, side 0 is non TR-DOS!" << endl;
      }
      else 
      {
         memcpy( &fileinfo[i], &nulldir, 16);
         cout << "DANGER! Sector "<<(((i*16)/256)+1)<<" on track 0, side 0 size is non 256 bytes!" << endl;
      }
      memcpy(buf, &fileinfo[i], 16);
      
      write(hfile, buf, 14);
      for(j=0; j < 14; j++) SCLCRC += unsigned(buf[j]);
   }

   unsigned char SEC, TRK;
   for(i=0; i < FileCount; i++)
   {
      SEC = fileinfo[i].FirstSec;
      TRK = fileinfo[i].FirstTrk;

      for(j=0; j < unsigned(fileinfo[i].SecLen); j++)
      {
         if(FindSector(TRK/SideCount, TRK%SideCount, SEC+1, &vgfs))
         {
            memcpy( buf, vgfs.SectorPointer, 256);
            if((!vgfs.CRCOK)||(!vgfs.vgfa.CRCOK)) cout << "Warning: sector "<<(SEC+1)<<" on track "<<(TRK/SideCount)<<", side "<<(TRK%SideCount)<<" with BAD CRC!" << endl;
            if(vgfs.SectorLength!=256) cout << "Warning: sector "<<(SEC+1)<<" on track "<<(TRK/SideCount)<<", side "<<(TRK%SideCount)<<" is non 256 bytes!" << endl;
         }
         else 
         {
            memcpy( buf, nullbuf, 256);
            cout << "DANGER! Sector "<<(SEC+1)<<" on track "<<(TRK/SideCount)<<", side "<<(TRK%SideCount)<<" not found!" << endl;
         }
         write(hfile, buf, 256);
         for(unsigned k=0; k < 256; k++) SCLCRC += unsigned(buf[k]);
         SEC++;
         if(SEC > 15) { SEC=0; TRK++; }
      }
   }
   write(hfile, &SCLCRC, 4);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x21a8, %rsp           # imm = 0x21A8
movl %esi, 0x10(%rsp)
leaq 0x48(%rsp), %r8
movq %rdi, 0x38(%rsp)
xorl %esi, %esi
xorl %edx, %edx
movl $0x9, %ecx
xorl %r9d, %r9d
callq 0x20f6
testb %al, %al
je 0x28b6
cmpb $0x0, 0x99(%rsp)
je 0x27e3
cmpb $0x0, 0x79(%rsp)
jne 0x2825
movq 0x87ee(%rip), %rbx      # 0xafd8
leaq 0x5e08(%rip), %rsi      # 0x85f9
movl $0x32, %edx
movq %rbx, %rdi
callq 0x1170
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x1130
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x10c0
cmpl $0x100, 0x88(%rsp) # imm = 0x100
je 0x2874
movq 0x879f(%rip), %rbx      # 0xafd8
leaq 0x5dec(%rip), %rsi      # 0x862c
movl $0x35, %edx
movq %rbx, %rdi
callq 0x1170
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x1130
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x10c0
movq 0x80(%rsp), %rax
cmpb $0x10, 0xe7(%rax)
jne 0x2edf
movzbl 0xe4(%rax), %ecx
movq %rcx, 0x18(%rsp)
cmpq $0x91, %rcx
jb 0x28c9
movq 0x8733(%rip), %rbx      # 0xafd8
leaq 0x5de5(%rip), %rsi      # 0x8691
movl $0x45, %edx
jmp 0x2ef2
movq 0x871b(%rip), %rbx      # 0xafd8
leaq 0x5d06(%rip), %rsi      # 0x85ca
jmp 0x2eed
movb 0xe3(%rax), %r14b
leal -0x16(%r14), %eax
cmpb $0x4, %al
jae 0x2edf
andb $-0x2, %r14b
movabsq $0x5249414c434e4953, %rax # imm = 0x5249414C434E4953
movq %rax, 0x1a0(%rsp)
movq 0x18(%rsp), %rax
movb %al, 0x1a8(%rsp)
xorl %eax, %eax
xorl %r12d, %r12d
movzbl 0x1a0(%rsp,%rax), %ecx
addl %ecx, %r12d
incq %rax
cmpq $0x9, %rax
jne 0x2903
movl %r12d, 0x2c(%rsp)
leaq 0x1a0(%rsp), %rsi
movl $0x9, %edx
movl 0x10(%rsp), %edi
callq 0x1060
leaq 0xd6(%rsp), %rbx
movl $0xca, %edx
movq %rbx, %rdi
movl $0x2a, %esi
callq 0x10a0
movaps 0x87ed(%rip), %xmm0     # 0xb140
movaps %xmm0, -0x36(%rbx)
movaps 0x87f2(%rip), %xmm0     # 0xb150
movaps %xmm0, -0x26(%rbx)
movdqa 0x87f6(%rip), %xmm0     # 0xb160
movdqa %xmm0, -0x16(%rbx)
movq 0x87f8(%rip), %rax      # 0xb16e
movq %rax, -0x8(%rbx)
cmpb $0x0, 0x18(%rsp)
je 0x2eb7
movq %r14, 0x30(%rsp)
xorl %r13d, %r13d
movq 0x8644(%rip), %r14      # 0xafd8
movl %r13d, %ebp
shrl $0x4, %ebp
incl %ebp
movzbl %bpl, %ecx
movq 0x38(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
leaq 0x48(%rsp), %r8
xorl %r9d, %r9d
callq 0x20f6
movq %r13, %r15
shlq $0x4, %r15
leaq (%rsp,%r15), %rcx
addq $0x11a0, %rcx           # imm = 0x11A0
testb %al, %al
je 0x2a9a
movq 0x80(%rsp), %rax
movl %r13d, %edx
shll $0x4, %edx
movzbl %dl, %edx
movdqu (%rax,%rdx), %xmm0
movdqu %xmm0, (%rcx)
cmpb $0x0, 0x99(%rsp)
je 0x29fb
cmpb $0x0, 0x79(%rsp)
jne 0x2a57
movl $0x10, %edx
movq %r14, %rdi
leaq 0x5a71(%rip), %rsi      # 0x847b
callq 0x1170
movl %ebp, %esi
movq %r14, %rdi
callq 0x10b0
movq %rax, %rbx
movl $0x21, %edx
movq %rax, %rdi
leaq 0x5bdf(%rip), %rsi      # 0x860a
callq 0x1170
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x1130
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x10c0
cmpl $0x100, 0x88(%rsp) # imm = 0x100
je 0x2b14
movl $0x10, %edx
movq %r14, %rdi
leaq 0x5a04(%rip), %rsi      # 0x847b
callq 0x1170
movl %ebp, %esi
movq %r14, %rdi
callq 0x10b0
movq %rax, %rbx
movl $0x22, %edx
movq %rax, %rdi
leaq 0x5c3f(%rip), %rsi      # 0x86d7
jmp 0x2ae8
movabsq $0x434553474e4f5257, %rax # imm = 0x434553474E4F5257
movq %rax, (%rcx)
movb $0x45, 0x8(%rcx)
movl $0xeeeeeeee, 0x9(%rcx)  # imm = 0xEEEEEEEE
movw $0x0, 0xd(%rcx)
movl $0xf, %edx
movq %r14, %rdi
leaq 0x59d8(%rip), %rsi      # 0x849f
callq 0x1170
movl %ebp, %esi
movq %r14, %rdi
callq 0x10b0
movq %rax, %rbx
movl $0x2a, %edx
movq %rax, %rdi
leaq 0x5c12(%rip), %rsi      # 0x86fa
callq 0x1170
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x1130
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x10c0
movdqa 0x11a0(%rsp,%r15), %xmm0
movdqa %xmm0, 0x1a0(%rsp)
movl $0xe, %edx
movl 0x10(%rsp), %edi
leaq 0x1a0(%rsp), %rsi
callq 0x1060
xorl %eax, %eax
movzbl 0x1a0(%rsp,%rax), %ecx
addl %ecx, %r12d
incq %rax
cmpq $0xe, %rax
jne 0x2b3f
incq %r13
cmpq 0x18(%rsp), %r13
jne 0x2994
cmpb $0x0, 0x18(%rsp)
je 0x2eb7
movq 0x30(%rsp), %rcx
cmpb $0x18, %cl
setne 0xf(%rsp)
xorl %eax, %eax
movq %rax, 0x40(%rsp)
shlq $0x4, %rax
movzbl 0x11ad(%rsp,%rax), %edx
movl %edx, 0x24(%rsp)
testl %edx, %edx
je 0x2ea4
addq %rsp, %rax
addq $0x11a0, %rax           # imm = 0x11A0
movb 0xe(%rax), %r13b
movb 0xf(%rax), %r15b
xorl %eax, %eax
movl %eax, 0x28(%rsp)
cmpb $0x18, %cl
setne %al
movzbl %r15b, %ebx
movb 0xf(%rsp), %cl
shrl %cl, %ebx
andb %r15b, %al
movzbl %al, %ebp
movzbl %r13b, %r14d
incl %r14d
movzbl %r14b, %ecx
movq 0x38(%rsp), %rdi
movl %ebx, %esi
movl %ebp, %edx
leaq 0x48(%rsp), %r8
xorl %r9d, %r9d
callq 0x20f6
testb %al, %al
movl %ebp, 0x14(%rsp)
je 0x2d5c
movq 0x80(%rsp), %rsi
movl $0x100, %edx            # imm = 0x100
leaq 0x1a0(%rsp), %rdi
callq 0x10e0
cmpb $0x0, 0x99(%rsp)
je 0x2c21
cmpb $0x0, 0x79(%rsp)
jne 0x2cca
movl $0x10, %edx
movq 0x83ab(%rip), %rbp      # 0xafd8
movq %rbp, %rdi
leaq 0x5844(%rip), %rsi      # 0x847b
callq 0x1170
movq %rbp, %rdi
movl %r14d, %esi
callq 0x11f0
movq %rax, %rbp
movl $0xa, %edx
movq %rax, %rdi
leaq 0x5833(%rip), %rsi      # 0x848c
callq 0x1170
movl %ebx, %esi
movq %rbp, %rdi
callq 0x10b0
movq %rax, %rbp
movl $0x7, %edx
movq %rax, %rdi
leaq 0x581d(%rip), %rsi      # 0x8497
callq 0x1170
movl 0x14(%rsp), %esi
movq %rbp, %rdi
callq 0x10b0
movq %rax, %rbp
movl $0xe, %edx
movq %rax, %rdi
leaq 0x5980(%rip), %rsi      # 0x861d
callq 0x1170
movq (%rbp), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x1130
movsbl %al, %esi
movq %rbp, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x10c0
cmpl $0x100, 0x88(%rsp) # imm = 0x100
je 0x2e1e
movl $0x10, %edx
movq 0x82f1(%rip), %rbp      # 0xafd8
movq %rbp, %rdi
leaq 0x578a(%rip), %rsi      # 0x847b
callq 0x1170
movq %rbp, %rdi
movl %r14d, %esi
callq 0x11f0
movq %rax, %r14
movl $0xa, %edx
movq %rax, %rdi
leaq 0x5779(%rip), %rsi      # 0x848c
callq 0x1170
movl %ebx, %esi
movq %r14, %rdi
callq 0x10b0
movq %rax, %rbx
movl $0x7, %edx
movq %rax, %rdi
leaq 0x5763(%rip), %rsi      # 0x8497
callq 0x1170
movl 0x14(%rsp), %esi
movq %rbx, %rdi
callq 0x10b0
movq %rax, %rbx
movl $0x12, %edx
movq %rax, %rdi
leaq 0x58f8(%rip), %rsi      # 0x864f
jmp 0x2df2
movl $0x100, %edx            # imm = 0x100
leaq 0x1a0(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x10e0
movl $0xf, %edx
movq 0x8256(%rip), %rbp      # 0xafd8
movq %rbp, %rdi
leaq 0x5713(%rip), %rsi      # 0x849f
callq 0x1170
movq %rbp, %rdi
movl %r14d, %esi
callq 0x11f0
movq %rax, %r14
movl $0xa, %edx
movq %rax, %rdi
leaq 0x56de(%rip), %rsi      # 0x848c
callq 0x1170
movl %ebx, %esi
movq %r14, %rdi
callq 0x10b0
movq %rax, %rbx
movl $0x7, %edx
movq %rax, %rdi
leaq 0x56c8(%rip), %rsi      # 0x8497
callq 0x1170
movl 0x14(%rsp), %esi
movq %rbx, %rdi
callq 0x10b0
movq %rax, %rbx
movl $0xb, %edx
movq %rax, %rdi
leaq 0x57fb(%rip), %rsi      # 0x85ed
callq 0x1170
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x1130
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x10c0
movl $0x100, %edx            # imm = 0x100
movl 0x10(%rsp), %edi
leaq 0x1a0(%rsp), %rsi
callq 0x1060
movd %r12d, %xmm0
xorl %eax, %eax
pxor %xmm2, %xmm2
movd 0x1a0(%rsp,%rax), %xmm1
punpcklbw %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1],xmm1[2],xmm2[2],xmm1[3],xmm2[3],xmm1[4],xmm2[4],xmm1[5],xmm2[5],xmm1[6],xmm2[6],xmm1[7],xmm2[7]
punpcklwd %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1],xmm1[2],xmm2[2],xmm1[3],xmm2[3]
paddd %xmm1, %xmm0
addq $0x4, %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x2e3f
pshufd $0xee, %xmm0, %xmm1     # xmm1 = xmm0[2,3,2,3]
paddd %xmm0, %xmm1
pshufd $0x55, %xmm1, %xmm0     # xmm0 = xmm1[1,1,1,1]
paddd %xmm1, %xmm0
movd %xmm0, %r12d
incb %r13b
cmpb $0x10, %r13b
movzbl %r13b, %r13d
movl $0x0, %eax
cmovael %eax, %r13d
sbbb $-0x1, %r15b
movl 0x28(%rsp), %eax
incl %eax
cmpl 0x24(%rsp), %eax
movq 0x30(%rsp), %rcx
jne 0x2bab
movq 0x40(%rsp), %rax
incq %rax
cmpq 0x18(%rsp), %rax
jne 0x2b7b
leaq 0x2c(%rsp), %rsi
movl %r12d, (%rsi)
movl $0x4, %edx
movl 0x10(%rsp), %edi
callq 0x1060
addq $0x21a8, %rsp           # imm = 0x21A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x80f2(%rip), %rbx      # 0xafd8
leaq 0x5775(%rip), %rsi      # 0x8662
movl $0x2e, %edx
movq %rbx, %rdi
callq 0x1170
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x1130
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1040
movq %rax, %rdi
addq $0x21a8, %rsp           # imm = 0x21A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10c0