// File: 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
movq %rsp, %rbp
subq $0x21b0, %rsp           # imm = 0x21B0
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x21a0(%rbp)
xorl %r9d, %r9d
movl $0x9, %ecx
leaq -0x68(%rbp), %r8
movl %r9d, %esi
movl %r9d, %edx
callq 0x25d0
testb $0x1, %al
jne 0x2fef
movq 0xe009(%rip), %rdi      # 0x10fd8
leaq 0xb590(%rip), %rsi      # 0xe566
callq 0x1110
movq %rax, %rdi
movq 0xdfe3(%rip), %rsi      # 0x10fc8
callq 0x1140
jmp 0x3795
cmpb $0x0, -0x17(%rbp)
je 0x2ffb
cmpb $0x0, -0x37(%rbp)
jne 0x301d
movq 0xdfd6(%rip), %rdi      # 0x10fd8
leaq 0xb58c(%rip), %rsi      # 0xe595
callq 0x1110
movq %rax, %rdi
movq 0xdfb0(%rip), %rsi      # 0x10fc8
callq 0x1140
jmp 0x301f
cmpl $0x100, -0x28(%rbp)     # imm = 0x100
je 0x304a
movq 0xdfa9(%rip), %rdi      # 0x10fd8
leaq 0xb592(%rip), %rsi      # 0xe5c8
callq 0x1110
movq %rax, %rdi
movq 0xdf83(%rip), %rsi      # 0x10fc8
callq 0x1140
movq -0x30(%rbp), %rax
movzbl 0xe7(%rax), %eax
cmpl $0x10, %eax
je 0x3081
movq 0xdf77(%rip), %rdi      # 0x10fd8
leaq 0xb596(%rip), %rsi      # 0xe5fe
callq 0x1110
movq %rax, %rdi
movq 0xdf51(%rip), %rsi      # 0x10fc8
callq 0x1140
jmp 0x3795
movq -0x30(%rbp), %rax
movzbl 0xe4(%rax), %eax
movl %eax, -0x6c(%rbp)
cmpl $0x90, -0x6c(%rbp)
jbe 0x30bf
movq 0xdf39(%rip), %rdi      # 0x10fd8
leaq 0xb587(%rip), %rsi      # 0xe62d
callq 0x1110
movq %rax, %rdi
movq 0xdf13(%rip), %rsi      # 0x10fc8
callq 0x1140
jmp 0x3795
movl $0x2, -0x70(%rbp)
movq -0x30(%rbp), %rax
movzbl 0xe3(%rax), %eax
cmpl $0x18, %eax
je 0x30e6
movq -0x30(%rbp), %rax
movzbl 0xe3(%rax), %eax
cmpl $0x19, %eax
jne 0x30ed
movl $0x1, -0x70(%rbp)
movq -0x30(%rbp), %rax
movzbl 0xe3(%rax), %eax
cmpl $0x16, %eax
je 0x3154
movq -0x30(%rbp), %rax
movzbl 0xe3(%rax), %eax
cmpl $0x17, %eax
je 0x3154
movq -0x30(%rbp), %rax
movzbl 0xe3(%rax), %eax
cmpl $0x18, %eax
je 0x3154
movq -0x30(%rbp), %rax
movzbl 0xe3(%rax), %eax
cmpl $0x19, %eax
je 0x3154
movq 0xdea4(%rip), %rdi      # 0x10fd8
leaq 0xb4c3(%rip), %rsi      # 0xe5fe
callq 0x1110
movq %rax, %rdi
movq 0xde7e(%rip), %rsi      # 0x10fc8
callq 0x1140
jmp 0x3795
movl $0x0, -0x7c(%rbp)
movq 0xb3bb(%rip), %rax      # 0xe51d
movq %rax, -0x1080(%rbp)
movl -0x6c(%rbp), %eax
movb %al, -0x1078(%rbp)
movl $0x0, -0x74(%rbp)
cmpl $0x9, -0x74(%rbp)
jae 0x319b
movl -0x74(%rbp), %eax
movzbl -0x1080(%rbp,%rax), %eax
addl -0x7c(%rbp), %eax
movl %eax, -0x7c(%rbp)
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x3179
movl -0xc(%rbp), %edi
leaq -0x1080(%rbp), %rsi
movl $0x9, %edx
callq 0x1050
movl $0x0, -0x74(%rbp)
cmpl $0x100, -0x74(%rbp)     # imm = 0x100
jae 0x31d5
movl -0x74(%rbp), %eax
movb $0x2a, -0x1180(%rbp,%rax)
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x31b6
leaq -0x1180(%rbp), %rdi
leaq 0xdf4d(%rip), %rsi      # 0x11130
movl $0x36, %edx
callq 0x10c0
movq 0xb47f(%rip), %rax      # 0xe673
movq %rax, -0x1190(%rbp)
movb $0x45, -0x1188(%rbp)
movw $0xeeee, -0x1187(%rbp)  # imm = 0xEEEE
movw $0xeeee, -0x1185(%rbp)  # imm = 0xEEEE
movb $0x0, -0x1183(%rbp)
movb $0x0, -0x1182(%rbp)
movb $0x0, -0x1182(%rbp)
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl -0x6c(%rbp), %eax
jae 0x3425
movq -0x21a0(%rbp), %rdi
movl -0x74(%rbp), %eax
shll $0x4, %eax
shrl $0x8, %eax
addl $0x1, %eax
xorl %r9d, %r9d
leaq -0x68(%rbp), %r8
movl %r9d, %esi
movl %r9d, %edx
movzbl %al, %ecx
callq 0x25d0
testb $0x1, %al
jne 0x326d
jmp 0x3343
movl -0x74(%rbp), %eax
movl %eax, %ecx
leaq -0x2190(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
movq -0x30(%rbp), %rcx
movl -0x74(%rbp), %edx
shll $0x4, %edx
andl $0xff, %edx
movl %edx, %edx
movq (%rcx,%rdx), %rsi
movq %rsi, (%rax)
movq 0x8(%rcx,%rdx), %rcx
movq %rcx, 0x8(%rax)
cmpb $0x0, -0x17(%rbp)
je 0x32ae
cmpb $0x0, -0x37(%rbp)
jne 0x32f3
movq 0xdd23(%rip), %rdi      # 0x10fd8
leaq 0xb15b(%rip), %rsi      # 0xe417
callq 0x1110
movq %rax, %rdi
movl -0x74(%rbp), %esi
shll $0x4, %esi
shrl $0x8, %esi
addl $0x1, %esi
callq 0x1170
movq %rax, %rdi
leaq 0xb2c7(%rip), %rsi      # 0xe5a6
callq 0x1110
movq %rax, %rdi
movq 0xdcda(%rip), %rsi      # 0x10fc8
callq 0x1140
cmpl $0x100, -0x28(%rbp)     # imm = 0x100
je 0x3341
movq 0xdcd5(%rip), %rdi      # 0x10fd8
leaq 0xb10d(%rip), %rsi      # 0xe417
callq 0x1110
movq %rax, %rdi
movl -0x74(%rbp), %esi
shll $0x4, %esi
shrl $0x8, %esi
addl $0x1, %esi
callq 0x1170
movq %rax, %rdi
leaq 0xb34f(%rip), %rsi      # 0xe67c
callq 0x1110
movq %rax, %rdi
movq 0xdc8c(%rip), %rsi      # 0x10fc8
callq 0x1140
jmp 0x33b0
movl -0x74(%rbp), %eax
movl %eax, %ecx
leaq -0x2190(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
movq -0x1190(%rbp), %rcx
movq %rcx, (%rax)
movq -0x1188(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq 0xdc66(%rip), %rdi      # 0x10fd8
leaq 0xb0c2(%rip), %rsi      # 0xe43b
callq 0x1110
movq %rax, %rdi
movl -0x74(%rbp), %esi
shll $0x4, %esi
shrl $0x8, %esi
addl $0x1, %esi
callq 0x1170
movq %rax, %rdi
leaq 0xb303(%rip), %rsi      # 0xe69f
callq 0x1110
movq %rax, %rdi
movq 0xdc1d(%rip), %rsi      # 0x10fc8
callq 0x1140
movl -0x74(%rbp), %eax
movl %eax, %ecx
leaq -0x2190(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, -0x1080(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x1078(%rbp)
movl -0xc(%rbp), %edi
leaq -0x1080(%rbp), %rsi
movl $0xe, %edx
callq 0x1050
movl $0x0, -0x78(%rbp)
cmpl $0xe, -0x78(%rbp)
jae 0x3415
movl -0x78(%rbp), %eax
movzbl -0x1080(%rbp,%rax), %eax
addl -0x7c(%rbp), %eax
movl %eax, -0x7c(%rbp)
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x33f3
jmp 0x3417
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x3230
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl -0x6c(%rbp), %eax
jae 0x3784
movl -0x74(%rbp), %eax
movl %eax, %ecx
leaq -0x2190(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
movb 0xe(%rax), %al
movb %al, -0x2191(%rbp)
movl -0x74(%rbp), %eax
movl %eax, %ecx
leaq -0x2190(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
movb 0xf(%rax), %al
movb %al, -0x2192(%rbp)
movl $0x0, -0x78(%rbp)
movl -0x78(%rbp), %eax
movl -0x74(%rbp), %ecx
movl %ecx, %edx
leaq -0x2190(%rbp), %rcx
shlq $0x4, %rdx
addq %rdx, %rcx
movzbl 0xd(%rcx), %ecx
cmpl %ecx, %eax
jae 0x3774
movq -0x21a0(%rbp), %rdi
movzbl -0x2192(%rbp), %eax
xorl %edx, %edx
divl -0x70(%rbp)
movb %al, -0x21a1(%rbp)
movzbl -0x2192(%rbp), %eax
xorl %edx, %edx
divl -0x70(%rbp)
movl %edx, %eax
movb -0x21a1(%rbp), %dl
movb %al, %cl
movzbl -0x2191(%rbp), %eax
addl $0x1, %eax
leaq -0x68(%rbp), %r8
xorl %r9d, %r9d
movzbl %dl, %esi
movzbl %cl, %edx
movzbl %al, %ecx
callq 0x25d0
testb $0x1, %al
jne 0x34f0
jmp 0x3641
leaq -0x1080(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x10c0
cmpb $0x0, -0x17(%rbp)
je 0x3515
cmpb $0x0, -0x37(%rbp)
jne 0x35a2
movq 0xdabc(%rip), %rdi      # 0x10fd8
leaq 0xaef4(%rip), %rsi      # 0xe417
callq 0x1110
movq %rax, %rdi
movzbl -0x2191(%rbp), %esi
addl $0x1, %esi
callq 0x11d0
movq %rax, %rdi
leaq 0xaee4(%rip), %rsi      # 0xe428
callq 0x1110
movq %rax, %rdi
movzbl -0x2192(%rbp), %eax
xorl %edx, %edx
divl -0x70(%rbp)
movl %eax, %esi
callq 0x1170
movq %rax, %rdi
leaq 0xaeca(%rip), %rsi      # 0xe433
callq 0x1110
movq %rax, %rdi
movzbl -0x2192(%rbp), %eax
xorl %edx, %edx
divl -0x70(%rbp)
movl %edx, %esi
callq 0x1170
movq %rax, %rdi
leaq 0xb02b(%rip), %rsi      # 0xe5b9
callq 0x1110
movq %rax, %rdi
movq 0xda2b(%rip), %rsi      # 0x10fc8
callq 0x1140
cmpl $0x100, -0x28(%rbp)     # imm = 0x100
je 0x363c
movq 0xda22(%rip), %rdi      # 0x10fd8
leaq 0xae5a(%rip), %rsi      # 0xe417
callq 0x1110
movq %rax, %rdi
movzbl -0x2191(%rbp), %esi
addl $0x1, %esi
callq 0x11d0
movq %rax, %rdi
leaq 0xae4a(%rip), %rsi      # 0xe428
callq 0x1110
movq %rax, %rdi
movzbl -0x2192(%rbp), %eax
xorl %edx, %edx
divl -0x70(%rbp)
movl %eax, %esi
callq 0x1170
movq %rax, %rdi
leaq 0xae30(%rip), %rsi      # 0xe433
callq 0x1110
movq %rax, %rdi
movzbl -0x2192(%rbp), %eax
xorl %edx, %edx
divl -0x70(%rbp)
movl %edx, %esi
callq 0x1170
movq %rax, %rdi
leaq 0xafc3(%rip), %rsi      # 0xe5eb
callq 0x1110
movq %rax, %rdi
movq 0xd991(%rip), %rsi      # 0x10fc8
callq 0x1140
jmp 0x36e6
leaq -0x1080(%rbp), %rdi
leaq -0x1180(%rbp), %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x10c0
movq 0xd978(%rip), %rdi      # 0x10fd8
leaq 0xadd4(%rip), %rsi      # 0xe43b
callq 0x1110
movq %rax, %rdi
movzbl -0x2191(%rbp), %esi
addl $0x1, %esi
callq 0x11d0
movq %rax, %rdi
leaq 0xada0(%rip), %rsi      # 0xe428
callq 0x1110
movq %rax, %rdi
movzbl -0x2192(%rbp), %eax
xorl %edx, %edx
divl -0x70(%rbp)
movl %eax, %esi
callq 0x1170
movq %rax, %rdi
leaq 0xad86(%rip), %rsi      # 0xe433
callq 0x1110
movq %rax, %rdi
movzbl -0x2192(%rbp), %eax
xorl %edx, %edx
divl -0x70(%rbp)
movl %edx, %esi
callq 0x1170
movq %rax, %rdi
leaq 0xaeb7(%rip), %rsi      # 0xe589
callq 0x1110
movq %rax, %rdi
movq 0xd8e7(%rip), %rsi      # 0x10fc8
callq 0x1140
movl -0xc(%rbp), %edi
leaq -0x1080(%rbp), %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x1050
movl $0x0, -0x2198(%rbp)
cmpl $0x100, -0x2198(%rbp)   # imm = 0x100
jae 0x3735
movl -0x2198(%rbp), %eax
movzbl -0x1080(%rbp,%rax), %eax
addl -0x7c(%rbp), %eax
movl %eax, -0x7c(%rbp)
movl -0x2198(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2198(%rbp)
jmp 0x3704
movb -0x2191(%rbp), %al
addb $0x1, %al
movb %al, -0x2191(%rbp)
movzbl -0x2191(%rbp), %eax
cmpl $0xf, %eax
jle 0x3764
movb $0x0, -0x2191(%rbp)
movb -0x2192(%rbp), %al
addb $0x1, %al
movb %al, -0x2192(%rbp)
jmp 0x3766
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x3477
jmp 0x3776
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x342c
movl -0xc(%rbp), %edi
leaq -0x7c(%rbp), %rsi
movl $0x4, %edx
callq 0x1050
addq $0x21b0, %rsp           # imm = 0x21B0
popq %rbp
retq
nop