// File: /workspace/github_cpp_makefile_star2_O1/atsidaev[P]trx2x/DiskImage.cpp
// Function: TDiskImage::readSCL(int, bool)
void TDiskImage::readSCL(int hfile, bool readonly)
{
   long fsize = filelength(hfile);
   if(fsize < 0)
   {
      ShowError(ERR_GETLEN);
      return;
   }
   unsigned char *ptr = (unsigned char*)new char[fsize+1024*2048];
   if(!ptr)
   {
      ShowError(ERR_NOMEM);
      return;
   }
   unsigned long rsize = read(hfile, ptr, fsize);

   if(!rsize)
   {
      delete ptr;
      ShowError(ERR_CORRUPT);
      return;
   }
   if(rsize < 9+4)      // header
   {
      delete ptr;
      ShowError(ERR_CORRUPT);
      return;
   }
   if(memcmp(ptr, "SINCLAIR", 8) != 0) 
   {
      delete ptr;
      ShowError(ERR_FORMAT" SCL!");
      return;
   }

   unsigned int FileCount = ptr[8];
   if(rsize < 9+4+FileCount*14)
   {
      delete ptr;
      ShowError(ERR_CORRUPT);
      return;
   }

   TRDOS_DIR_ELEMENT *fileinfo[256];

   unsigned short FilesTotalSecs = 0;

// checking for corrupt + SCL-CRC + FileDIRS parse
   uint32_t SCLCRC = 0;
   uint32_t sclOFF = 0;
   unsigned int i, j;
   for(i=0; i < 9; i++) SCLCRC += unsigned(ptr[sclOFF++]);

   for(i=0; i < FileCount; i++)
   {
      fileinfo[i] = (TRDOS_DIR_ELEMENT*)(ptr+sclOFF);
      for(j=0; j < 14; j++) SCLCRC += unsigned(ptr[sclOFF++]);
   }
   for(i=0; i < FileCount; i++)
   {
      unsigned SL = unsigned(fileinfo[i]->SecLen)*256;
      FilesTotalSecs += fileinfo[i]->SecLen;
      if(rsize < sclOFF+4+SL)
      {
         delete ptr;
         ShowError(ERR_CORRUPT);
         return;
      }
      for(j=0; j < SL; j++) SCLCRC += unsigned(ptr[sclOFF++]);
   }

   if( *((uint32_t*)(ptr+sclOFF)) != SCLCRC )
      ShowError(ERR_FILECRC" SCL!");


   if(FilesTotalSecs < 2544) FilesTotalSecs = 2544;
   else
   {
      int cyls = ((16+FilesTotalSecs) / (16*2)) + ( ((16+FilesTotalSecs) % (16*2))?  1:0);
      FilesTotalSecs = (cyls * 16*2)-16;
   }

   formatTRDOS((FilesTotalSecs+16)/(16*2), 2);
   ReadOnly = true;
   FType = DIT_SCL;
   DiskPresent = true;

   sclOFF = 9+14*FileCount;

   bool BOOTADD=true;

   unsigned char SEC=0, TRK=1;
   unsigned short FreeSEC = 2544;
   unsigned char _FileCount = 0;
   unsigned char _DelFileCount = 0;

   TRDOS_DIR_ELEMENT trdosde;
   VGFIND_SECTOR vgfs;
   char sbuf[512];

   for(i=0; i < FileCount; i++)
   {
      if(TRK >= (MaxTrack+1)*(MaxSide+1)) break;      // disk full ?

      memcpy(sbuf, fileinfo[i]->FileName, 9);
      if(sbuf[0] == 0x01) _DelFileCount++;
      _FileCount++; 
      make_uppercase(sbuf);
      if(!memcmp(sbuf, "BOOT    B", 9)) BOOTADD = false;

      memcpy(&trdosde, fileinfo[i], 14);

      trdosde.FirstSec = SEC;
      trdosde.FirstTrk = TRK;

      unsigned int dirsec = ((i*16)/256)+1;
      if(FindSector(0, 0, dirsec, &vgfs))         // DIR ELEMENT write
      {
         memcpy(vgfs.SectorPointer + ((i*16)%256), &trdosde, 16);
         ApplySectorCRC(vgfs);
      }

      for(j=0; j < unsigned(trdosde.SecLen); j++)
      {
         if(FindSector(TRK/2, TRK%2, SEC+1, &vgfs))       // SECTOR write
         {
            memcpy(vgfs.SectorPointer, ptr+sclOFF, 256);
            ApplySectorCRC(vgfs);
         }
         sclOFF+=256;
         SEC++;
         FreeSEC--;
         if(SEC > 15) { SEC=0; TRK++; }

         if(TRK >= (MaxTrack+1)*(MaxSide+1)) break;      // disk full ?
      }
   }
   if(!AddBOOT) BOOTADD = false;

   if(BOOTADD&&(unsigned(_FileCount)+unsigned(_DelFileCount) < 127))
   {
      unsigned int dirsec = ((_FileCount*16)/256)+1;
      if(FindSector(0, 0, dirsec, &vgfs))         // DIR ELEMENT write
      {
         memcpy(vgfs.SectorPointer + ((i*16)%256), sbootdir, 16);
         ApplySectorCRC(vgfs);
      }
      _FileCount++;

      memcpy(&trdosde, sbootdir, 16);

      unsigned char _bSEC=trdosde.FirstSec+1;
      unsigned int _bOFF=0;

      for(j=0; j < unsigned(trdosde.SecLen); j++)
      {
         if(FindSector(0, 0, _bSEC++, &vgfs))       // SECTOR write
         {
            memcpy(vgfs.SectorPointer, sbootimage+_bOFF, 256);
            ApplySectorCRC(vgfs);
         }
         _bOFF+=256;
         if(_bSEC > 16) break;
      }
   }

   if(FindSector(0, 0, 9, &vgfs))         // update disk info
   {
      vgfs.SectorPointer[0xE1] = SEC;
      vgfs.SectorPointer[0xE2] = TRK;
      vgfs.SectorPointer[0xE4] = _FileCount;
      vgfs.SectorPointer[0xF4] = _DelFileCount;
      *((unsigned short*)(vgfs.SectorPointer+0xE5)) = FreeSEC;
      ApplySectorCRC(vgfs);
   }

   delete ptr;
   ReadOnly = readonly;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb08, %rsp            # imm = 0xB08
movl %edx, %ebx
movl %esi, %ebp
movq %rdi, %r14
movl %esi, %edi
xorl %esi, %esi
movl $0x1, %edx
callq 0x1210
movq %rax, %r15
movl %ebp, %edi
xorl %esi, %esi
movl $0x2, %edx
callq 0x1210
movq %rax, %r12
movl %ebp, %edi
movq %r15, %rsi
xorl %edx, %edx
callq 0x1210
testq %r12, %r12
js 0x432c
movq %r12, %rdi
addq $0x200000, %rdi         # imm = 0x200000
callq 0x1030
movq %rax, %r15
movl %ebp, %edi
movq %rax, %rsi
movq %r12, %rdx
callq 0x11b0
testq %rax, %rax
je 0x4335
movq %r15, 0x78(%rsp)
cmpq $0xc, %rax
ja 0x434b
movl $0x1, %esi
movq 0x78(%rsp), %rdi
jmp 0x433d
leaq 0x4025(%rip), %rsi      # 0x8358
jmp 0x4370
movl $0x1, %esi
movq %r15, %rdi
callq 0x1150
leaq 0x402d(%rip), %rsi      # 0x8376
jmp 0x4370
movabsq $0x5249414c434e4953, %rcx # imm = 0x5249414C434E4953
movq 0x78(%rsp), %rdi
cmpq %rcx, (%rdi)
je 0x4386
movl $0x1, %esi
callq 0x1150
leaq 0x41be(%rip), %rsi      # 0x852e
addq $0xb08, %rsp            # imm = 0xB08
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x23fa
movb 0x8(%rdi), %r8b
movzbl %r8b, %r9d
movl %r9d, %r13d
shll $0x4, %r13d
subq %r9, %r13
subq %r9, %r13
leaq 0xd(%r13), %rcx
cmpq %rcx, %rax
jae 0x43ab
movl $0x1, %esi
jmp 0x433d
movl %ebx, 0x90(%rsp)
xorl %edx, %edx
xorl %ebx, %ebx
movzbl (%rdi,%rdx), %ecx
incq %rdx
addl %ecx, %ebx
cmpq $0x9, %rdx
jne 0x43b6
testb %r8b, %r8b
je 0x4405
leaq 0x9(%rdi), %r10
movl $0x9, %r15d
xorl %ecx, %ecx
leaq (%rdi,%r15), %rdx
movq %rdx, 0x300(%rsp,%rcx,8)
xorl %edx, %edx
movzbl (%r10,%rdx), %esi
addl %esi, %ebx
incq %rdx
cmpl $0xe, %edx
jne 0x43e4
addq $0xe, %r15
incq %rcx
addq $0xe, %r10
cmpq %r9, %rcx
jne 0x43d6
jmp 0x440b
movl $0x9, %r15d
movq %r8, 0x60(%rsp)
testb %r8b, %r8b
movq %r9, 0xa0(%rsp)
je 0x44d1
xorl %r12d, %r12d
xorl %ebp, %ebp
movq %rax, 0x88(%rsp)
movq 0x300(%rsp,%r12,8), %rcx
movzbl 0xd(%rcx), %r10d
movl %r10d, %r8d
shll $0x8, %r8d
leal (%r15,%r8), %r11d
addl $0x4, %r11d
cmpq %r11, %rax
jae 0x449b
movl $0x1, %esi
movq 0x78(%rsp), %rdi
movl %r10d, 0x80(%rsp)
movq %r11, 0x70(%rsp)
callq 0x1150
leaq 0x3f04(%rip), %rsi      # 0x8376
callq 0x23fa
movq 0x70(%rsp), %r11
movl 0x80(%rsp), %r10d
movq 0x88(%rsp), %rax
movq 0xa0(%rsp), %r9
movq 0x78(%rsp), %rdi
jmp 0x44b7
movl %r8d, %ecx
movl %r15d, %edx
testb %r10b, %r10b
je 0x44b7
movl %edx, %esi
incl %edx
movzbl (%rdi,%rsi), %esi
addl %esi, %ebx
decl %ecx
jne 0x44a6
addl %r8d, %r15d
cmpq %r11, %rax
jb 0x4972
addl %r10d, %ebp
incq %r12
cmpq %r9, %r12
jne 0x442e
jmp 0x44d3
xorl %ebp, %ebp
movl %r15d, %eax
cmpl %ebx, (%rdi,%rax)
je 0x44e7
leaq 0x4069(%rip), %rsi      # 0x854b
callq 0x23fa
movzwl %bp, %eax
movl $0x50, %esi
cmpl $0x9f0, %eax            # imm = 0x9F0
jb 0x451c
leal 0x10(%rax), %ecx
xorl %edx, %edx
testb $0x1f, %cl
setne %dl
shll $0x5, %edx
addl %edx, %eax
addl $0x10, %eax
andl $0xffe0, %eax           # imm = 0xFFE0
addl $0xfff0, %eax           # imm = 0xFFF0
movzwl %ax, %esi
addl $0x10, %esi
shrl $0x5, %esi
movq %r14, %rdi
movl $0x2, %edx
callq 0x6e1a
movw $0x101, 0x145005(%r14)  # imm = 0x101
movl $0x2, 0x141000(%r14)
movq $0x0, 0x98(%rsp)
cmpb $0x0, 0x60(%rsp)
je 0x4792
addl $0x9, %r13d
movb $0x1, %bl
movw $0x9f0, %ax             # imm = 0x9F0
movl %eax, 0x6c(%rsp)
xorl %edi, %edi
xorl %r12d, %r12d
xorl %ebp, %ebp
movb $0x1, %sil
xorl %r8d, %r8d
movzbl %bl, %eax
movzbl 0x145007(%r14), %ecx
incl %ecx
movzbl 0x145008(%r14), %edx
incl %edx
imull %ecx, %edx
cmpl %eax, %edx
jbe 0x47af
movq 0x300(%rsp,%rdi,8), %r15
movq (%r15), %rax
movq %rax, 0x100(%rsp)
movb 0x8(%r15), %cl
movb %cl, 0x108(%rsp)
cmpb $0x1, %al
sete 0x5f(%rsp)
testb %al, %al
je 0x45d9
leaq 0x101(%rsp), %rcx
leal -0x61(%rax), %edx
cmpb $0x19, %dl
ja 0x45d0
addb $-0x20, %al
movb %al, -0x1(%rcx)
movb (%rcx), %al
incq %rcx
testb %al, %al
jne 0x45c3
movl %r8d, 0x94(%rsp)
movq %r12, 0x70(%rsp)
movq 0x100(%rsp), %rax
movabsq $0x20202020544f4f42, %rcx # imm = 0x20202020544F4F42
xorq %rcx, %rax
movzbl 0x108(%rsp), %ecx
xorq $0x42, %rcx
orq %rax, %rcx
movzbl %sil, %esi
movl $0x0, %eax
cmovel %eax, %esi
movl %esi, 0x88(%rsp)
movb 0xd(%r15), %r12b
movq %rdi, 0x80(%rsp)
movl %edi, %eax
shrl $0x4, %eax
incb %al
movzbl %al, %ecx
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
leaq 0xa8(%rsp), %r8
xorl %r9d, %r9d
callq 0x20e8
testb %al, %al
je 0x46a2
movq 0xe0(%rsp), %rax
movq 0x80(%rsp), %rcx
shll $0x4, %ecx
movzbl %cl, %ecx
movq (%r15), %rdx
movl 0x8(%r15), %esi
movb 0xc(%r15), %dil
movb %dil, 0xc(%rax,%rcx)
movl %esi, 0x8(%rax,%rcx)
movq %rdx, (%rax,%rcx)
movb %r12b, 0xd(%rax,%rcx)
movb %bpl, 0xe(%rax,%rcx)
movb %bl, 0xf(%rax,%rcx)
movl $0xb, %ecx
movq %rsp, %rdi
leaq 0xa8(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x1e28
testb %r12b, %r12b
je 0x4758
movzbl %r12b, %r15d
xorl %r12d, %r12d
movl %ebx, %eax
shrb %al
movl %ebx, %ecx
andb $0x1, %cl
incb %bpl
movzbl %al, %esi
movzbl %cl, %edx
movzbl %bpl, %ebp
movq %r14, %rdi
movl %ebp, %ecx
leaq 0xa8(%rsp), %r8
xorl %r9d, %r9d
callq 0x20e8
testb %al, %al
je 0x4713
movq 0xe0(%rsp), %rdi
movl %r13d, %esi
addq 0x78(%rsp), %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x10e0
movl $0xb, %ecx
movq %rsp, %rdi
leaq 0xa8(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x1e28
addl $0x100, %r13d           # imm = 0x100
cmpb $0x10, %bpl
movl $0x0, %eax
cmovael %eax, %ebp
sbbb $-0x1, %bl
movzbl %bl, %eax
movzbl 0x145007(%r14), %ecx
incl %ecx
movzbl 0x145008(%r14), %edx
incl %edx
imull %ecx, %edx
incl %r12d
cmpl %eax, %edx
jbe 0x4753
cmpl %r15d, %r12d
jb 0x46b2
subl %r12d, 0x6c(%rsp)
movq 0x70(%rsp), %r12
addb 0x5f(%rsp), %r12b
movl 0x94(%rsp), %r8d
incl %r8d
movq 0x80(%rsp), %rdi
incq %rdi
movq 0xa0(%rsp), %rcx
cmpq %rcx, %rdi
movl 0x88(%rsp), %esi
jne 0x4570
jmp 0x47b9
movw $0x9f0, %ax             # imm = 0x9F0
movl %eax, 0x6c(%rsp)
movb $0x1, %sil
xorl %ebp, %ebp
movb $0x1, %bl
movq $0x0, 0x60(%rsp)
xorl %r12d, %r12d
jmp 0x47c7
movl %edi, %ecx
movl %r8d, %eax
movq %rax, 0x60(%rsp)
shll $0x4, %ecx
movzbl %cl, %eax
movq %rax, 0x98(%rsp)
cmpb $0x1, 0x145009(%r14)
jne 0x48ef
testb $0x1, %sil
je 0x48ef
movq 0x60(%rsp), %rdx
movzbl %dl, %eax
movzbl %r12b, %ecx
addl %eax, %ecx
cmpl $0x7e, %ecx
ja 0x48ef
movl %edx, %eax
shrb $0x4, %al
incb %al
movzbl %al, %ecx
leaq 0xa8(%rsp), %r8
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %r9d, %r9d
callq 0x20e8
testb %al, %al
je 0x484a
leaq 0xa8(%rsp), %rsi
movq 0x38(%rsi), %rax
movaps 0x69a2(%rip), %xmm0     # 0xb1d0
movq 0x98(%rsp), %rcx
movups %xmm0, (%rax,%rcx)
movl $0xb, %ecx
movq %rsp, %rdi
rep  movsq (%rsi), %es:(%rdi)
callq 0x1e28
movq 0x60(%rsp), %rax
incb %al
movq %rax, 0x60(%rsp)
movzbl 0x697f(%rip), %r15d     # 0xb1dd
testq %r15, %r15
je 0x48ef
movq %r12, 0x70(%rsp)
movb 0x696b(%rip), %r13b     # 0xb1de
addb $0x2, %r13b
shll $0x8, %r15d
xorl %r12d, %r12d
leal -0x1(%r13), %eax
movzbl %al, %ecx
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
leaq 0xa8(%rsp), %r8
xorl %r9d, %r9d
callq 0x20e8
testb %al, %al
je 0x48d5
movq 0xe0(%rsp), %rdi
leaq 0x6d42(%rip), %rax      # 0xb5f1
leaq (%rax,%r12), %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x10e0
movl $0xb, %ecx
movq %rsp, %rdi
leaq 0xa8(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x1e28
cmpb $0x10, %r13b
ja 0x48ea
addq $0x100, %r12            # imm = 0x100
incb %r13b
cmpl %r12d, %r15d
jne 0x487e
movq 0x70(%rsp), %r12
leaq 0xa8(%rsp), %r8
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
movl $0x9, %ecx
xorl %r9d, %r9d
callq 0x20e8
testb %al, %al
je 0x4955
leaq 0xa8(%rsp), %rsi
movq 0x38(%rsi), %rax
movb %bpl, 0xe1(%rax)
movb %bl, 0xe2(%rax)
movq 0x60(%rsp), %rcx
movb %cl, 0xe4(%rax)
movb %r12b, 0xf4(%rax)
movl 0x6c(%rsp), %ecx
movw %cx, 0xe5(%rax)
movl $0xb, %ecx
movq %rsp, %rdi
rep  movsq (%rsi), %es:(%rdi)
callq 0x1e28
movl $0x1, %esi
movq 0x78(%rsp), %rdi
callq 0x1150
movl 0x90(%rsp), %eax
movb %al, 0x145005(%r14)
addq $0xb08, %rsp            # imm = 0xB08
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq