// File: /workspace/github_cpp_makefile_star2_O2/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 $0x2198, %rsp           # imm = 0x2198
movl %esi, 0x4(%rsp)
pushq $0x9
popq %rcx
leaq 0x38(%rsp), %r8
movq %rdi, 0x28(%rsp)
xorl %esi, %esi
xorl %edx, %edx
xorl %r9d, %r9d
callq 0x1d64
testb %al, %al
je 0x2318
cmpb $0x0, 0x89(%rsp)
je 0x22a1
cmpb $0x0, 0x69(%rsp)
jne 0x22bc
movq 0x7d30(%rip), %rdi      # 0x9fd8
leaq 0x52ee(%rip), %rsi      # 0x759d
callq 0x1120
movq %rax, %rdi
callq 0x1070
cmpl $0x100, 0x78(%rsp)      # imm = 0x100
je 0x22e1
movq 0x7d0b(%rip), %rdi      # 0x9fd8
leaq 0x52fc(%rip), %rsi      # 0x75d0
callq 0x1120
movq %rax, %rdi
callq 0x1070
movq 0x70(%rsp), %rax
cmpb $0x10, 0xe7(%rax)
jne 0x2379
movzbl 0xe4(%rax), %ecx
movq %rcx, 0x8(%rsp)
cmpq $0x91, %rcx
jb 0x2328
movq 0x7cc9(%rip), %rdi      # 0x9fd8
leaq 0x531f(%rip), %rsi      # 0x7635
jmp 0x2387
movq 0x7cb9(%rip), %rdi      # 0x9fd8
leaq 0x5248(%rip), %rsi      # 0x756e
jmp 0x2387
movb 0xe3(%rax), %cl
leal -0x16(%rcx), %eax
cmpb $0x4, %al
jae 0x2379
andb $-0x2, %cl
movq %rcx, 0x20(%rsp)
andl $0x0, 0x1c(%rsp)
movabsq $0x5249414c434e4953, %rax # imm = 0x5249414C434E4953
movq %rax, 0x190(%rsp)
movq 0x8(%rsp), %rax
movb %al, 0x198(%rsp)
xorl %eax, %eax
xorl %ebp, %ebp
cmpq $0x9, %rax
je 0x23a5
movzbl 0x190(%rsp,%rax), %ecx
addl %ecx, %ebp
incq %rax
jmp 0x2364
movq 0x7c58(%rip), %rdi      # 0x9fd8
leaq 0x527f(%rip), %rsi      # 0x7606
callq 0x1120
movq %rax, %rdi
addq $0x2198, %rsp           # imm = 0x2198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1070
leaq 0x190(%rsp), %rsi
pushq $0x9
popq %rdx
movl 0x4(%rsp), %edi
callq 0x1050
xorl %eax, %eax
cmpq $0x100, %rax            # imm = 0x100
je 0x23d0
movb $0x2a, 0x90(%rsp,%rax)
incq %rax
jmp 0x23bb
movq 0x7d87(%rip), %rax      # 0xa15e
movq %rax, 0xbe(%rsp)
movaps 0x7d6a(%rip), %xmm0     # 0xa150
movaps %xmm0, 0xb0(%rsp)
movaps 0x7d4b(%rip), %xmm0     # 0xa140
movaps %xmm0, 0xa0(%rsp)
movaps 0x7d2c(%rip), %xmm0     # 0xa130
movaps %xmm0, 0x90(%rsp)
xorl %ebx, %ebx
pushq $0xe
popq %r13
cmpq 0x8(%rsp), %rbx
je 0x2567
movl %ebx, %r14d
shrl $0x4, %r14d
incl %r14d
movzbl %r14b, %ecx
movq 0x28(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
leaq 0x38(%rsp), %r8
xorl %r9d, %r9d
callq 0x1d64
movq %rbx, %r12
shlq $0x4, %r12
leaq (%rsp,%r12), %rcx
addq $0x1190, %rcx           # imm = 0x1190
movl %r14d, %r15d
testb %al, %al
je 0x24ce
movq 0x70(%rsp), %rax
movl %ebx, %edx
shll $0x4, %edx
movzbl %dl, %edx
movups (%rax,%rdx), %xmm0
movups %xmm0, (%rcx)
cmpb $0x0, 0x89(%rsp)
je 0x247f
cmpb $0x0, 0x69(%rsp)
jne 0x24b4
movq 0x7b52(%rip), %rdi      # 0x9fd8
leaq 0x4f92(%rip), %rsi      # 0x741f
callq 0x1120
movq %rax, %rdi
movq %r15, %rsi
callq 0x10a0
movq %rax, %rdi
leaq 0x5107(%rip), %rsi      # 0x75ae
callq 0x1120
movq %rax, %rdi
callq 0x1070
cmpl $0x100, 0x78(%rsp)      # imm = 0x100
leaq 0x4f5c(%rip), %rsi      # 0x741f
leaq 0x51b1(%rip), %r14      # 0x767b
jne 0x24f9
jmp 0x2523
movabsq $0x434553474e4f5257, %rax # imm = 0x434553474E4F5257
movq %rax, (%rcx)
movb $0x45, 0x8(%rcx)
movl $0xeeeeeeee, 0x9(%rcx)  # imm = 0xEEEEEEEE
andw $0x0, 0xd(%rcx)
leaq 0x4f51(%rip), %rsi      # 0x7443
leaq 0x51a5(%rip), %r14      # 0x769e
movq 0x7ad8(%rip), %rdi      # 0x9fd8
callq 0x1120
movq %rax, %rdi
movq %r15, %rsi
callq 0x10a0
movq %rax, %rdi
movq %r14, %rsi
callq 0x1120
movq %rax, %rdi
callq 0x1070
movaps 0x1190(%rsp,%r12), %xmm0
movaps %xmm0, 0x190(%rsp)
movl 0x4(%rsp), %edi
leaq 0x190(%rsp), %rsi
movq %r13, %rdx
callq 0x1050
xorl %eax, %eax
cmpq $0xe, %rax
je 0x255f
movzbl 0x190(%rsp,%rax), %ecx
addl %ecx, %ebp
incq %rax
jmp 0x254a
incq %rbx
jmp 0x2412
movq 0x20(%rsp), %rcx
cmpb $0x18, %cl
setne 0x2(%rsp)
xorl %eax, %eax
cmpq 0x8(%rsp), %rax
je 0x2798
movq %rax, 0x30(%rsp)
shlq $0x4, %rax
movb 0x119e(%rsp,%rax), %r12b
movb 0x119f(%rsp,%rax), %dl
movzbl 0x119d(%rsp,%rax), %eax
movl %eax, 0x14(%rsp)
xorl %eax, %eax
cmpl 0x14(%rsp), %eax
je 0x278b
movl %eax, 0x18(%rsp)
cmpb $0x18, %cl
setne %al
movzbl %dl, %ebx
movb 0x2(%rsp), %cl
shrl %cl, %ebx
movb %dl, 0x3(%rsp)
andb %dl, %al
movzbl %al, %r14d
movzbl %r12b, %r15d
incl %r15d
movzbl %r15b, %ecx
movq 0x28(%rsp), %rdi
movl %ebx, %esi
movl %r14d, %edx
leaq 0x38(%rsp), %r8
xorl %r9d, %r9d
callq 0x1d64
movl %ebx, %ebx
movl %r14d, %r13d
testb %al, %al
je 0x26ab
movq 0x70(%rsp), %rsi
movl $0x100, %edx            # imm = 0x100
leaq 0x190(%rsp), %rdi
callq 0x10d0
cmpb $0x0, 0x89(%rsp)
je 0x2625
cmpb $0x0, 0x69(%rsp)
jne 0x268e
movq 0x79ac(%rip), %rdi      # 0x9fd8
leaq 0x4dec(%rip), %rsi      # 0x741f
callq 0x1120
movq %rax, %rdi
movl %r15d, %esi
callq 0x11d0
movq %rax, %rdi
leaq 0x4de3(%rip), %rsi      # 0x7430
callq 0x1120
movq %rax, %rdi
movq %rbx, %rsi
callq 0x10a0
movq %rax, %rdi
leaq 0x4dd4(%rip), %rsi      # 0x743b
callq 0x1120
movq %rax, %rdi
movq %r13, %rsi
callq 0x10a0
movq %rax, %rdi
leaq 0x4f40(%rip), %rsi      # 0x75c1
callq 0x1120
movq %rax, %rdi
callq 0x1070
cmpl $0x100, 0x78(%rsp)      # imm = 0x100
leaq 0x4d82(%rip), %rsi      # 0x741f
leaq 0x4f4f(%rip), %r14      # 0x75f3
jne 0x26d3
jmp 0x2731
movl $0x100, %edx            # imm = 0x100
leaq 0x190(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x10d0
leaq 0x4d77(%rip), %rsi      # 0x7443
leaq 0x4ebe(%rip), %r14      # 0x7591
movq 0x78fe(%rip), %rdi      # 0x9fd8
callq 0x1120
movq %rax, %rdi
movl %r15d, %esi
callq 0x11d0
movq %rax, %rdi
leaq 0x4d3c(%rip), %rsi      # 0x7430
callq 0x1120
movq %rax, %rdi
movq %rbx, %rsi
callq 0x10a0
movq %rax, %rdi
leaq 0x4d2d(%rip), %rsi      # 0x743b
callq 0x1120
movq %rax, %rdi
movq %r13, %rsi
callq 0x10a0
movq %rax, %rdi
movq %r14, %rsi
callq 0x1120
movq %rax, %rdi
callq 0x1070
movl $0x100, %edx            # imm = 0x100
movl 0x4(%rsp), %edi
leaq 0x190(%rsp), %rsi
callq 0x1050
xorl %eax, %eax
movb 0x3(%rsp), %dl
cmpq $0x100, %rax            # imm = 0x100
je 0x2764
movzbl 0x190(%rsp,%rax), %ecx
addl %ecx, %ebp
incq %rax
jmp 0x274d
incb %r12b
cmpb $0x10, %r12b
movzbl %r12b, %r12d
movl $0x0, %eax
cmovael %eax, %r12d
sbbb $-0x1, %dl
movl 0x18(%rsp), %eax
incl %eax
movq 0x20(%rsp), %rcx
jmp 0x25a7
movq 0x30(%rsp), %rax
incq %rax
jmp 0x2576
leaq 0x1c(%rsp), %rsi
movl %ebp, (%rsi)
pushq $0x4
popq %rdx
movl 0x4(%rsp), %edi
callq 0x1050
addq $0x2198, %rsp           # imm = 0x2198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop