// File: /workspace/github_cpp_makefile_star2_O1/atsidaev[P]trx2x/DiskImage.cpp
// Function: TDiskImage::readHOB(int)
void TDiskImage::readHOB(int hfile)
{
   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 < 17)      // header
   {
      delete ptr;
      ShowError(ERR_CORRUPT);
      return;
   }

   TRDOS_DIR_ELEMENT dired;
   memcpy(&dired, ptr, 14);

   unsigned short hobRealCRC = *((uint16_t*)(ptr+0x0F));
   unsigned int DataLength = *((uint16_t*)(ptr+0x0D));

   if(rsize < 17+(DataLength&0xFF00))
   {
      delete ptr;
      ShowError(ERR_CORRUPT);
      return;
   }

   unsigned int i;
   uint16_t CRC=0;
   for(i=0; i < 15; i++) CRC = CRC + ptr[i];
   CRC *= 257;
   CRC += 105;          // сумма чисел от 0 до 14

   if(CRC != hobRealCRC)
      ShowError(ERR_FILECRC" HOBETA!");




   ReadOnly = true;
   if(!DiskPresent)
   {
      formatTRDOS(80, 2);
      FType = DIT_HOB;
      DiskPresent = true;
   }

// --- read file...
   dired.SecLen = ptr[0x0E];            // число секторов файла

   VGFIND_SECTOR vgfs9;
   if(!FindSector(0, 0, 9, &vgfs9)) { delete ptr; return; }

   dired.FirstSec = vgfs9.SectorPointer[0xE1];
   dired.FirstTrk = vgfs9.SectorPointer[0xE2];

   VGFIND_SECTOR vgfs;
                                           
   unsigned char SEC = dired.FirstSec, TRK = dired.FirstTrk;
   unsigned short FreeSEC = *((unsigned short*)(vgfs9.SectorPointer+0xE5));
   unsigned char FileCount = vgfs9.SectorPointer[0xE4];
   unsigned char DelFileCount = vgfs9.SectorPointer[0xF4];

   if(TRK >= 160)       // disk full ? 
   {
      delete ptr;
      return;
   }

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

      if(TRK >= 160) break;     // disk full?
   }

   if(FindSector(0, 0, ((FileCount*16)/256)+1, &vgfs))
   {
      memcpy(vgfs.SectorPointer + ((FileCount*16)%256), &dired, 16);
      ApplySectorCRC(vgfs);
   }

   if(dired.FileName[0] == 0x01) DelFileCount++;
   FileCount++;

   vgfs9.SectorPointer[0xE1] = SEC;
   vgfs9.SectorPointer[0xE2] = TRK;
   *((unsigned short*)(vgfs9.SectorPointer+0xE5)) = FreeSEC;
   vgfs9.SectorPointer[0xE4] = FileCount;
   vgfs9.SectorPointer[0xF4] = DelFileCount;
   ApplySectorCRC(vgfs9);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movl %esi, %ebp
movq %rdi, %rbx
movl %esi, %edi
xorl %esi, %esi
movl $0x1, %edx
callq 0x1210
movq %rax, %r14
movl %ebp, %edi
xorl %esi, %esi
movl $0x2, %edx
callq 0x1210
movq %rax, %r15
movl %ebp, %edi
movq %r14, %rsi
xorl %edx, %edx
callq 0x1210
testq %r15, %r15
js 0x6b4c
movq %r15, %rdi
addq $0x200000, %rdi         # imm = 0x200000
callq 0x1030
movq %rax, %r14
movl %ebp, %edi
movq %rax, %rsi
movq %r15, %rdx
callq 0x11b0
testq %rax, %rax
je 0x6b36
cmpq $0x10, %rax
jbe 0x6b36
movb (%r14), %r15b
movl 0x9(%r14), %ecx
movl %ecx, 0x78(%rsp)
movq 0x1(%r14), %rcx
movq %rcx, 0x70(%rsp)
movzbl 0xe(%r14), %ecx
shll $0x8, %ecx
orq $0x11, %rcx
cmpq %rcx, %rax
jae 0x6b69
movl $0x1, %esi
movq %r14, %rdi
callq 0x1150
leaq 0x182c(%rip), %rsi      # 0x8376
jmp 0x6b53
leaq 0x1805(%rip), %rsi      # 0x8358
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x23fa
movzwl 0xf(%r14), %eax
xorl %edx, %edx
xorl %ecx, %ecx
movzbl (%r14,%rdx), %esi
addl %esi, %ecx
incq %rdx
cmpq $0xf, %rdx
jne 0x6b72
movl %ecx, %edx
shll $0x8, %edx
addl %edx, %ecx
addl $0x69, %ecx
cmpw %ax, %cx
je 0x6b9d
leaq 0x1b31(%rip), %rsi      # 0x86c9
callq 0x23fa
movb $0x1, 0x145005(%rbx)
cmpb $0x0, 0x145006(%rbx)
jne 0x6bd0
movq %rbx, %rdi
movl $0x50, %esi
movl $0x2, %edx
callq 0x6e1a
movl $0x6, 0x141000(%rbx)
movb $0x1, 0x145006(%rbx)
movb 0xe(%r14), %bpl
leaq 0xf0(%rsp), %r8
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
movl $0x9, %ecx
xorl %r9d, %r9d
callq 0x20e8
testb %al, %al
je 0x6c09
movq 0x128(%rsp), %rax
movb 0xe2(%rax), %r13b
cmpb $-0x60, %r13b
jb 0x6c27
movl $0x1, %esi
movq %r14, %rdi
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1150
movb 0xe1(%rax), %dl
movzbl 0xe4(%rax), %ecx
movq %rcx, 0x80(%rsp)
movb 0xf4(%rax), %cl
movb %cl, 0x6b(%rsp)
movq %rax, 0x88(%rsp)
movzwl 0xe5(%rax), %r12d
testb %bpl, %bpl
movb %bpl, 0x6a(%rsp)
movb %r13b, 0x69(%rsp)
movb %dl, 0x68(%rsp)
je 0x6d32
movb %r15b, 0x67(%rsp)
movzbl %bpl, %ecx
addq $0x11, %r14
movzwl %cx, %eax
movl %r12d, %esi
subl %eax, %esi
movl %esi, 0x6c(%rsp)
shll $0x8, %ecx
movq %rcx, 0x90(%rsp)
decl %r12d
xorl %r15d, %r15d
movl %edx, %ebp
movl %r13d, %eax
shrb %al
movl %r13d, %ecx
andb $0x1, %cl
incb %bpl
movzbl %al, %esi
movzbl %cl, %edx
movzbl %bpl, %ebp
movq %rbx, %rdi
movl %ebp, %ecx
leaq 0x98(%rsp), %r8
xorl %r9d, %r9d
callq 0x20e8
testb %al, %al
je 0x6cf8
movq 0xd0(%rsp), %rdi
leaq (%r14,%r15), %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x10e0
movl $0xb, %ecx
movq %rsp, %rdi
leaq 0x98(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x1e28
cmpb $0x10, %bpl
movl $0x0, %eax
cmovael %eax, %ebp
sbbb $-0x1, %r13b
cmpb $-0x60, %r13b
jae 0x6d2b
addq $0x100, %r15            # imm = 0x100
decl %r12d
cmpq %r15, 0x90(%rsp)
jne 0x6c99
movl 0x6c(%rsp), %r12d
movb 0x67(%rsp), %r15b
jmp 0x6d34
movl %edx, %ebp
movq 0x80(%rsp), %r14
movl %r14d, %eax
shrb $0x4, %al
incb %al
movzbl %al, %ecx
leaq 0x98(%rsp), %r8
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %r9d, %r9d
callq 0x20e8
testb %al, %al
je 0x6db5
movl %r14d, %eax
shll $0x4, %eax
leaq 0x98(%rsp), %rsi
movq 0x38(%rsi), %rcx
movzbl %al, %eax
movb %r15b, (%rcx,%rax)
movq 0x70(%rsp), %rdx
movq %rdx, 0x1(%rcx,%rax)
movl 0x78(%rsp), %edx
movl %edx, 0x9(%rcx,%rax)
movb 0x6a(%rsp), %dl
movb %dl, 0xd(%rcx,%rax)
movb 0x68(%rsp), %dl
movb %dl, 0xe(%rcx,%rax)
movb 0x69(%rsp), %dl
movb %dl, 0xf(%rcx,%rax)
movl $0xb, %ecx
movq %rsp, %rdi
rep  movsq (%rsi), %es:(%rdi)
callq 0x1e28
cmpb $0x1, %r15b
sete %al
movb 0x6b(%rsp), %cl
addb %al, %cl
incb %r14b
movq 0x88(%rsp), %rax
movb %bpl, 0xe1(%rax)
movb %r13b, 0xe2(%rax)
movw %r12w, 0xe5(%rax)
movb %r14b, 0xe4(%rax)
movb %cl, 0xf4(%rax)
leaq 0xf0(%rsp), %rsi
movl $0xb, %ecx
movq %rsp, %rdi
rep  movsq (%rsi), %es:(%rdi)
callq 0x1e28
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq