// File: /workspace/github_cpp_makefile_star2_O2/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
callq 0x6db6
testq %rax, %rax
js 0x5daa
movq %rax, %r15
movq %rax, %rdi
addq $0x200000, %rdi         # imm = 0x200000
callq 0x1030
movq %rax, %r14
movl %ebp, %edi
movq %rax, %rsi
movq %r15, %rdx
callq 0x11a0
testq %rax, %rax
je 0x5d96
cmpq $0x10, %rax
jbe 0x5d96
movb (%r14), %bpl
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 0x5dc7
pushq $0x1
popq %rsi
movq %r14, %rdi
callq 0x1140
leaq 0x15ce(%rip), %rsi      # 0x7376
jmp 0x5db1
leaq 0x15a7(%rip), %rsi      # 0x7358
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x201c
movzwl 0xf(%r14), %eax
xorl %ecx, %ecx
xorl %edx, %edx
cmpq $0xf, %rcx
je 0x5de2
movzbl (%r14,%rcx), %esi
addl %esi, %edx
incq %rcx
jmp 0x5dd0
imull $0x101, %edx, %ecx      # imm = 0x101
addl $0x69, %ecx
cmpw %ax, %cx
je 0x5dfc
leaq 0x18d2(%rip), %rsi      # 0x76c9
callq 0x201c
movb $0x1, 0x145005(%rbx)
cmpb $0x0, 0x145006(%rbx)
jne 0x5e2b
pushq $0x50
popq %rsi
pushq $0x2
popq %rdx
movq %rbx, %rdi
callq 0x6062
movl $0x6, 0x141000(%rbx)
movb $0x1, 0x145006(%rbx)
movb 0xe(%r14), %r15b
pushq $0x9
popq %rcx
leaq 0xf0(%rsp), %r8
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %r9d, %r9d
callq 0x1d64
testb %al, %al
je 0x5e62
movq 0x128(%rsp), %rax
movb 0xe2(%rax), %r13b
cmpb $-0x60, %r13b
jb 0x5e7e
pushq $0x1
popq %rsi
movq %r14, %rdi
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1140
movb %bpl, 0x6b(%rsp)
movb %r15b, 0x69(%rsp)
movzbl %r15b, %esi
movb 0xe1(%rax), %dl
movzbl 0xe4(%rax), %ecx
movq %rcx, 0x80(%rsp)
movb 0xf4(%rax), %cl
movb %cl, 0x6a(%rsp)
movq %rax, 0x88(%rsp)
movzwl 0xe5(%rax), %r12d
addq $0x11, %r14
movzwl %si, %eax
movl %r12d, %ecx
subl %eax, %ecx
movl %ecx, 0x6c(%rsp)
shll $0x8, %esi
movq %rsi, 0x90(%rsp)
xorl %r15d, %r15d
movb %dl, 0x67(%rsp)
movl %edx, %ebp
movb %r13b, 0x68(%rsp)
cmpq %r15, 0x90(%rsp)
je 0x5f78
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 0x1d64
testb %al, %al
je 0x5f4f
movq 0xd0(%rsp), %rdi
leaq (%r14,%r15), %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x10d0
pushq $0xb
popq %rcx
movq %rsp, %rdi
leaq 0x98(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x1adc
decl %r12d
cmpb $0x10, %bpl
movl $0x0, %eax
cmovael %eax, %ebp
sbbb $-0x1, %r13b
addq $0x100, %r15            # imm = 0x100
cmpb $-0x60, %r13b
jb 0x5ee4
movb $-0x60, %r13b
jmp 0x5f7d
movl 0x6c(%rsp), %r12d
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 0x1d64
testb %al, %al
movb 0x6b(%rsp), %bl
je 0x5fff
movl %r14d, %eax
shll $0x4, %eax
leaq 0x98(%rsp), %rsi
movq 0x38(%rsi), %rcx
movzbl %al, %eax
movb %bl, (%rcx,%rax)
movq 0x70(%rsp), %rdx
movq %rdx, 0x1(%rcx,%rax)
movl 0x78(%rsp), %edx
movl %edx, 0x9(%rcx,%rax)
movb 0x69(%rsp), %dl
movb %dl, 0xd(%rcx,%rax)
movb 0x67(%rsp), %dl
movb %dl, 0xe(%rcx,%rax)
movb 0x68(%rsp), %dl
movb %dl, 0xf(%rcx,%rax)
pushq $0xb
popq %rcx
movq %rsp, %rdi
rep  movsq (%rsi), %es:(%rdi)
callq 0x1adc
cmpb $0x1, %bl
sete %al
movb 0x6a(%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)
pushq $0xb
popq %rcx
leaq 0xf0(%rsp), %rsi
movq %rsp, %rdi
rep  movsq (%rsi), %es:(%rdi)
callq 0x1adc
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop