// File: /workspace/github_cpp_makefile_star2_O1/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, %ebp
leaq 0x38(%rsp), %r8
movq %rdi, 0x28(%rsp)
xorl %esi, %esi
xorl %edx, %edx
movl $0x9, %ecx
xorl %r9d, %r9d
callq 0x20e8
testb %al, %al
je 0x2898
cmpb $0x0, 0x89(%rsp)
je 0x27cb
cmpb $0x0, 0x69(%rsp)
jne 0x280d
movq 0x8806(%rip), %rbx      # 0xafd8
leaq 0x5dc4(%rip), %rsi      # 0x859d
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, 0x78(%rsp)      # imm = 0x100
je 0x2859
movq 0x87ba(%rip), %rbx      # 0xafd8
leaq 0x5dab(%rip), %rsi      # 0x85d0
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 0x70(%rsp), %rax
cmpb $0x10, 0xe7(%rax)
jne 0x2e94
movzbl 0xe4(%rax), %ecx
movq %rcx, 0x10(%rsp)
cmpq $0x91, %rcx
jb 0x28ab
movq 0x8751(%rip), %rbx      # 0xafd8
leaq 0x5da7(%rip), %rsi      # 0x8635
movl $0x45, %edx
jmp 0x2ea7
movq 0x8739(%rip), %rbx      # 0xafd8
leaq 0x5cc8(%rip), %rsi      # 0x856e
jmp 0x2ea2
movb 0xe3(%rax), %cl
leal -0x16(%rcx), %eax
cmpb $0x4, %al
jae 0x2e94
andb $-0x2, %cl
movq %rcx, 0x20(%rsp)
movl $0x0, 0xc(%rsp)
movabsq $0x5249414c434e4953, %rax # imm = 0x5249414C434E4953
movq %rax, 0x190(%rsp)
movq 0x10(%rsp), %rax
movb %al, 0x198(%rsp)
xorl %eax, %eax
xorl %r12d, %r12d
movzbl 0x190(%rsp,%rax), %ecx
addl %ecx, %r12d
incq %rax
cmpq $0x9, %rax
jne 0x28ef
movl %r12d, 0xc(%rsp)
leaq 0x190(%rsp), %rsi
movl $0x9, %edx
movl %ebp, %edi
callq 0x1060
leaq 0x90(%rsp), %rbx
movl $0x100, %edx            # imm = 0x100
movq %rbx, %rdi
movl $0x2a, %esi
callq 0x10a0
movaps 0x8803(%rip), %xmm0     # 0xb140
movaps %xmm0, (%rbx)
movaps 0x8809(%rip), %xmm0     # 0xb150
movaps %xmm0, 0x10(%rbx)
movaps 0x880e(%rip), %xmm0     # 0xb160
movaps %xmm0, 0x20(%rbx)
movq 0x8811(%rip), %rax      # 0xb16e
movq %rax, 0x2e(%rbx)
cmpb $0x0, 0x10(%rsp)
movl %ebp, 0x8(%rsp)
je 0x2b3f
xorl %r13d, %r13d
movq 0x865e(%rip), %r14      # 0xafd8
movl %r13d, %ebp
shrl $0x4, %ebp
incl %ebp
movzbl %bpl, %ecx
movq 0x28(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
leaq 0x38(%rsp), %r8
xorl %r9d, %r9d
callq 0x20e8
movq %r13, %r15
shlq $0x4, %r15
leaq (%rsp,%r15), %rcx
addq $0x1190, %rcx           # imm = 0x1190
testb %al, %al
je 0x2a78
movq 0x70(%rsp), %rax
movl %r13d, %edx
shll $0x4, %edx
movzbl %dl, %edx
movups (%rax,%rdx), %xmm0
movups %xmm0, (%rcx)
cmpb $0x0, 0x89(%rsp)
je 0x29dc
cmpb $0x0, 0x69(%rsp)
jne 0x2a38
movl $0x10, %edx
movq %r14, %rdi
leaq 0x5a34(%rip), %rsi      # 0x841f
callq 0x1170
movl %ebp, %esi
movq %r14, %rdi
callq 0x10b0
movq %rax, %rbx
movl $0x21, %edx
movq %rax, %rdi
leaq 0x5ba2(%rip), %rsi      # 0x85ae
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, 0x78(%rsp)      # imm = 0x100
je 0x2af2
movl $0x10, %edx
movq %r14, %rdi
leaq 0x59ca(%rip), %rsi      # 0x841f
callq 0x1170
movl %ebp, %esi
movq %r14, %rdi
callq 0x10b0
movq %rax, %rbx
movl $0x22, %edx
movq %rax, %rdi
leaq 0x5c05(%rip), %rsi      # 0x867b
jmp 0x2ac6
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 0x599e(%rip), %rsi      # 0x8443
callq 0x1170
movl %ebp, %esi
movq %r14, %rdi
callq 0x10b0
movq %rax, %rbx
movl $0x2a, %edx
movq %rax, %rdi
leaq 0x5bd8(%rip), %rsi      # 0x869e
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
movaps 0x1190(%rsp,%r15), %xmm0
movaps %xmm0, 0x190(%rsp)
movl $0xe, %edx
movl 0x8(%rsp), %ebp
movl %ebp, %edi
leaq 0x190(%rsp), %rsi
callq 0x1060
xorl %eax, %eax
movzbl 0x190(%rsp,%rax), %ecx
addl %ecx, %r12d
incq %rax
cmpq $0xe, %rax
jne 0x2b1d
incq %r13
cmpq 0x10(%rsp), %r13
jne 0x297a
movl %r12d, 0xc(%rsp)
cmpb $0x0, 0x10(%rsp)
je 0x2e6e
movq 0x20(%rsp), %rcx
cmpb $0x18, %cl
setne 0x6(%rsp)
xorl %eax, %eax
movq %rax, 0x30(%rsp)
shlq $0x4, %rax
movzbl 0x119d(%rsp,%rax), %edx
movl %edx, 0x18(%rsp)
testl %edx, %edx
je 0x2e5b
addq %rsp, %rax
addq $0x1190, %rax           # imm = 0x1190
movb 0xe(%rax), %r13b
movb 0xf(%rax), %dl
xorl %eax, %eax
movl %eax, 0x1c(%rsp)
cmpb $0x18, %cl
setne %al
movzbl %dl, %ebx
movb 0x6(%rsp), %cl
shrl %cl, %ebx
movb %dl, 0x7(%rsp)
andb %dl, %al
movzbl %al, %r15d
movzbl %r13b, %r14d
incl %r14d
movzbl %r14b, %ecx
movq 0x28(%rsp), %rdi
movl %ebx, %esi
movl %r15d, %edx
leaq 0x38(%rsp), %r8
xorl %r9d, %r9d
callq 0x20e8
testb %al, %al
je 0x2d3a
movq 0x70(%rsp), %rsi
movl $0x100, %edx            # imm = 0x100
leaq 0x190(%rsp), %rdi
callq 0x10e0
cmpb $0x0, 0x89(%rsp)
je 0x2c00
cmpb $0x0, 0x69(%rsp)
jne 0x2cac
movl $0x10, %edx
movq 0x83cc(%rip), %rbp      # 0xafd8
movq %rbp, %rdi
leaq 0x5809(%rip), %rsi      # 0x841f
callq 0x1170
movq %rbp, %rdi
movl %r14d, %esi
callq 0x11f0
movq %rax, %rbp
movl $0xa, %edx
movq %rax, %rdi
leaq 0x57f8(%rip), %rsi      # 0x8430
callq 0x1170
movl %ebx, %esi
movq %rbp, %rdi
callq 0x10b0
movq %rax, %rbp
movl $0x7, %edx
movq %rax, %rdi
leaq 0x57e2(%rip), %rsi      # 0x843b
callq 0x1170
movl %r15d, %esi
movq %rbp, %rdi
callq 0x10b0
movq %rax, %rbp
movl $0xe, %edx
movq %rax, %rdi
leaq 0x5946(%rip), %rsi      # 0x85c1
callq 0x1170
movq (%rbp), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x1130
movsbl %al, %esi
movq %rbp, %rdi
movl 0x8(%rsp), %ebp
callq 0x1040
movq %rax, %rdi
callq 0x10c0
cmpl $0x100, 0x78(%rsp)      # imm = 0x100
je 0x2dff
movl $0x10, %edx
movq 0x8312(%rip), %rbp      # 0xafd8
movq %rbp, %rdi
leaq 0x574f(%rip), %rsi      # 0x841f
callq 0x1170
movq %rbp, %rdi
movl %r14d, %esi
callq 0x11f0
movq %rax, %r14
movl $0xa, %edx
movq %rax, %rdi
leaq 0x573e(%rip), %rsi      # 0x8430
callq 0x1170
movl %ebx, %esi
movq %r14, %rdi
callq 0x10b0
movq %rax, %rbx
movl $0x7, %edx
movq %rax, %rdi
leaq 0x5728(%rip), %rsi      # 0x843b
callq 0x1170
movl %r15d, %esi
movq %rbx, %rdi
callq 0x10b0
movq %rax, %rbx
movl $0x12, %edx
movq %rax, %rdi
leaq 0x58be(%rip), %rsi      # 0x85f3
jmp 0x2dcf
movl $0x100, %edx            # imm = 0x100
leaq 0x190(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x10e0
movl $0xf, %edx
movq 0x8278(%rip), %rbp      # 0xafd8
movq %rbp, %rdi
leaq 0x56d9(%rip), %rsi      # 0x8443
callq 0x1170
movq %rbp, %rdi
movl %r14d, %esi
callq 0x11f0
movq %rax, %r14
movl $0xa, %edx
movq %rax, %rdi
leaq 0x56a4(%rip), %rsi      # 0x8430
callq 0x1170
movl %ebx, %esi
movq %r14, %rdi
callq 0x10b0
movq %rax, %rbx
movl $0x7, %edx
movq %rax, %rdi
leaq 0x568e(%rip), %rsi      # 0x843b
callq 0x1170
movl %r15d, %esi
movq %rbx, %rdi
callq 0x10b0
movq %rax, %rbx
movl $0xb, %edx
movq %rax, %rdi
leaq 0x57c2(%rip), %rsi      # 0x8591
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 0x8(%rsp), %ebp
movl $0x100, %edx            # imm = 0x100
movl %ebp, %edi
leaq 0x190(%rsp), %rsi
callq 0x1060
xorl %eax, %eax
movzbl 0x190(%rsp,%rax), %ecx
addl %ecx, %r12d
incq %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x2e15
incb %r13b
cmpb $0x10, %r13b
movzbl %r13b, %r13d
movl $0x0, %eax
cmovael %eax, %r13d
movb 0x7(%rsp), %dl
sbbb $-0x1, %dl
movl 0x1c(%rsp), %eax
incl %eax
cmpl 0x18(%rsp), %eax
movq 0x20(%rsp), %rcx
jne 0x2b8d
movq 0x30(%rsp), %rax
incq %rax
cmpq 0x10(%rsp), %rax
jne 0x2b5e
leaq 0xc(%rsp), %rsi
movl %r12d, (%rsi)
movl $0x4, %edx
movl %ebp, %edi
callq 0x1060
addq $0x2198, %rsp           # imm = 0x2198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x813d(%rip), %rbx      # 0xafd8
leaq 0x5764(%rip), %rsi      # 0x8606
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 $0x2198, %rsp           # imm = 0x2198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10c0
nop