// File: 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
movq %rsp, %rbp
subq $0x2b0, %rsp            # imm = 0x2B0
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x218(%rbp)
movl -0xc(%rbp), %edi
callq 0xd6a0
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jge 0xb4b8
movq -0x218(%rbp), %rdi
leaq 0x2e90(%rip), %rsi      # 0xe33e
callq 0x2c30
jmp 0xbacc
movq -0x18(%rbp), %rdi
addq $0x200000, %rdi         # imm = 0x200000
callq 0x1030
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0xb4eb
movq -0x218(%rbp), %rdi
leaq 0x2e7b(%rip), %rsi      # 0xe35c
callq 0x2c30
jmp 0xbacc
movl -0xc(%rbp), %edi
movq -0x20(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x11a0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0xb540
movq -0x20(%rbp), %rax
movq %rax, -0x220(%rbp)
cmpq $0x0, %rax
je 0xb528
movq -0x220(%rbp), %rdi
movl $0x1, %esi
callq 0x1130
movq -0x218(%rbp), %rdi
leaq 0x2e38(%rip), %rsi      # 0xe36e
callq 0x2c30
jmp 0xbacc
cmpq $0x11, -0x28(%rbp)
jae 0xb581
movq -0x20(%rbp), %rax
movq %rax, -0x228(%rbp)
cmpq $0x0, %rax
je 0xb569
movq -0x228(%rbp), %rdi
movl $0x1, %esi
callq 0x1130
movq -0x218(%rbp), %rdi
leaq 0x2df7(%rip), %rsi      # 0xe36e
callq 0x2c30
jmp 0xbacc
movq -0x20(%rbp), %rax
movq (%rax), %rcx
movq %rcx, -0x38(%rbp)
movl 0x8(%rax), %ecx
movl %ecx, -0x30(%rbp)
movw 0xc(%rax), %ax
movw %ax, -0x2c(%rbp)
movq -0x20(%rbp), %rax
movw 0xf(%rax), %ax
movw %ax, -0x3a(%rbp)
movq -0x20(%rbp), %rax
movzwl 0xd(%rax), %eax
movl %eax, -0x40(%rbp)
movq -0x28(%rbp), %rax
movl -0x40(%rbp), %ecx
andl $0xff00, %ecx           # imm = 0xFF00
addl $0x11, %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jae 0xb602
movq -0x20(%rbp), %rax
movq %rax, -0x230(%rbp)
cmpq $0x0, %rax
je 0xb5ea
movq -0x230(%rbp), %rdi
movl $0x1, %esi
callq 0x1130
movq -0x218(%rbp), %rdi
leaq 0x2d76(%rip), %rsi      # 0xe36e
callq 0x2c30
jmp 0xbacc
movw $0x0, -0x46(%rbp)
movl $0x0, -0x44(%rbp)
cmpl $0xf, -0x44(%rbp)
jae 0xb635
movzwl -0x46(%rbp), %eax
movq -0x20(%rbp), %rcx
movl -0x44(%rbp), %edx
movzbl (%rcx,%rdx), %ecx
addl %ecx, %eax
movw %ax, -0x46(%rbp)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0xb60f
movzwl -0x46(%rbp), %eax
imull $0x101, %eax, %eax      # imm = 0x101
movw %ax, -0x46(%rbp)
movzwl -0x46(%rbp), %eax
addl $0x69, %eax
movw %ax, -0x46(%rbp)
movzwl -0x46(%rbp), %eax
movzwl -0x3a(%rbp), %ecx
cmpl %ecx, %eax
je 0xb66d
movq -0x218(%rbp), %rdi
leaq 0x3062(%rip), %rsi      # 0xe6ca
callq 0x2c30
movq -0x218(%rbp), %rax
movb $0x1, 0x145005(%rax)
testb $0x1, 0x145006(%rax)
jne 0xb6b2
movq -0x218(%rbp), %rdi
movl $0x50, %esi
movl $0x2, %edx
callq 0xbae0
movq -0x218(%rbp), %rax
movl $0x6, 0x141000(%rax)
movb $0x1, 0x145006(%rax)
movq -0x218(%rbp), %rdi
movq -0x20(%rbp), %rax
movb 0xe(%rax), %al
movb %al, -0x2b(%rbp)
xorl %r9d, %r9d
movl $0x9, %ecx
leaq -0xa0(%rbp), %r8
movl %r9d, %esi
movl %r9d, %edx
callq 0x25d0
testb $0x1, %al
jne 0xb708
movq -0x20(%rbp), %rax
movq %rax, -0x238(%rbp)
cmpq $0x0, %rax
je 0xb703
movq -0x238(%rbp), %rdi
movl $0x1, %esi
callq 0x1130
jmp 0xbacc
movq -0x68(%rbp), %rax
movb 0xe1(%rax), %al
movb %al, -0x2a(%rbp)
movq -0x68(%rbp), %rax
movb 0xe2(%rax), %al
movb %al, -0x29(%rbp)
movb -0x2a(%rbp), %al
movb %al, -0xf9(%rbp)
movb -0x29(%rbp), %al
movb %al, -0xfa(%rbp)
movq -0x68(%rbp), %rax
movw 0xe5(%rax), %ax
movw %ax, -0xfc(%rbp)
movq -0x68(%rbp), %rax
movb 0xe4(%rax), %al
movb %al, -0xfd(%rbp)
movq -0x68(%rbp), %rax
movb 0xf4(%rax), %al
movb %al, -0xfe(%rbp)
movzbl -0xfa(%rbp), %eax
cmpl $0xa0, %eax
jl 0xb79b
movq -0x20(%rbp), %rax
movq %rax, -0x240(%rbp)
cmpq $0x0, %rax
je 0xb796
movq -0x240(%rbp), %rdi
movl $0x1, %esi
callq 0x1130
jmp 0xbacc
movl $0x0, -0x104(%rbp)
movl -0x104(%rbp), %eax
movzbl -0x2b(%rbp), %ecx
cmpl %ecx, %eax
jae 0xb90d
movq -0x218(%rbp), %rdi
movzbl -0xfa(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movb %al, -0x241(%rbp)
movzbl -0xfa(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %edx, %eax
movb -0x241(%rbp), %dl
movb %al, %cl
movzbl -0xf9(%rbp), %eax
addl $0x1, %eax
leaq -0xf8(%rbp), %r8
xorl %r9d, %r9d
movzbl %dl, %esi
movzbl %cl, %edx
movzbl %al, %ecx
callq 0x25d0
testb $0x1, %al
jne 0xb817
jmp 0xb8a6
movq -0xc0(%rbp), %rdi
movq -0x20(%rbp), %rsi
addq $0x11, %rsi
movl -0x104(%rbp), %eax
shll $0x8, %eax
movl %eax, %eax
addq %rax, %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x10c0
leaq -0x160(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
movl $0x58, %edx
callq 0x10c0
movq -0x218(%rbp), %rdi
movq -0x110(%rbp), %rcx
movq %rsp, %rax
movq %rcx, 0x50(%rax)
movups -0x120(%rbp), %xmm0
movups %xmm0, 0x40(%rax)
movups -0x160(%rbp), %xmm0
movups -0x150(%rbp), %xmm1
movups -0x140(%rbp), %xmm2
movups -0x130(%rbp), %xmm3
movups %xmm3, 0x30(%rax)
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x2040
movb -0xf9(%rbp), %al
addb $0x1, %al
movb %al, -0xf9(%rbp)
movw -0xfc(%rbp), %ax
addw $-0x1, %ax
movw %ax, -0xfc(%rbp)
movzbl -0xf9(%rbp), %eax
cmpl $0xf, %eax
jle 0xb8e7
movb $0x0, -0xf9(%rbp)
movb -0xfa(%rbp), %al
addb $0x1, %al
movb %al, -0xfa(%rbp)
movzbl -0xfa(%rbp), %eax
cmpl $0xa0, %eax
jl 0xb8f7
jmp 0xb90d
jmp 0xb8f9
movl -0x104(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x104(%rbp)
jmp 0xb7a5
movq -0x218(%rbp), %rdi
movzbl -0xfd(%rbp), %eax
shll $0x4, %eax
movl $0x100, %ecx            # imm = 0x100
cltd
idivl %ecx
addl $0x1, %eax
xorl %r9d, %r9d
leaq -0xf8(%rbp), %r8
movl %r9d, %esi
movl %r9d, %edx
movzbl %al, %ecx
callq 0x25d0
testb $0x1, %al
jne 0xb94a
jmp 0xb9ed
movq -0xc0(%rbp), %rax
movq %rax, -0x250(%rbp)
movzbl -0xfd(%rbp), %eax
shll $0x4, %eax
movl $0x100, %ecx            # imm = 0x100
cltd
idivl %ecx
movq -0x250(%rbp), %rax
movslq %edx, %rcx
movq -0x38(%rbp), %rdx
movq %rdx, (%rax,%rcx)
movq -0x30(%rbp), %rdx
movq %rdx, 0x8(%rax,%rcx)
leaq -0x1b8(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
movl $0x58, %edx
callq 0x10c0
movq -0x218(%rbp), %rdi
movq -0x168(%rbp), %rcx
movq %rsp, %rax
movq %rcx, 0x50(%rax)
movups -0x178(%rbp), %xmm0
movups %xmm0, 0x40(%rax)
movups -0x1b8(%rbp), %xmm0
movups -0x1a8(%rbp), %xmm1
movups -0x198(%rbp), %xmm2
movups -0x188(%rbp), %xmm3
movups %xmm3, 0x30(%rax)
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x2040
movsbl -0x38(%rbp), %eax
cmpl $0x1, %eax
jne 0xba04
movb -0xfe(%rbp), %al
addb $0x1, %al
movb %al, -0xfe(%rbp)
movb -0xfd(%rbp), %al
addb $0x1, %al
movb %al, -0xfd(%rbp)
movb -0xf9(%rbp), %cl
movq -0x68(%rbp), %rax
movb %cl, 0xe1(%rax)
movb -0xfa(%rbp), %cl
movq -0x68(%rbp), %rax
movb %cl, 0xe2(%rax)
movw -0xfc(%rbp), %cx
movq -0x68(%rbp), %rax
movw %cx, 0xe5(%rax)
movb -0xfd(%rbp), %cl
movq -0x68(%rbp), %rax
movb %cl, 0xe4(%rax)
movb -0xfe(%rbp), %cl
movq -0x68(%rbp), %rax
movb %cl, 0xf4(%rax)
leaq -0x210(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
movl $0x58, %edx
callq 0x10c0
movq -0x218(%rbp), %rdi
movq -0x1c0(%rbp), %rcx
movq %rsp, %rax
movq %rcx, 0x50(%rax)
movups -0x1d0(%rbp), %xmm0
movups %xmm0, 0x40(%rax)
movups -0x210(%rbp), %xmm0
movups -0x200(%rbp), %xmm1
movups -0x1f0(%rbp), %xmm2
movups -0x1e0(%rbp), %xmm3
movups %xmm3, 0x30(%rax)
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x2040
addq $0x2b0, %rsp            # imm = 0x2B0
popq %rbp
retq
nopw %cs:(%rax,%rax)