// File: /workspace/github_cpp_makefile_star2_O2/atsidaev[P]trx2x/DiskImage.cpp
// Function: TDiskImage::FindSector(unsigned char, unsigned char, unsigned char, VGFIND_SECTOR*, unsigned int)
bool TDiskImage::FindSector(unsigned char CYL, unsigned char SIDE,
                            unsigned char SECT, 
                            VGFIND_SECTOR *vgfs, unsigned int FromOffset)
{
   vgfs->SectorPointer = NULL;
   vgfs->SectorLength = 0;
   vgfs->FoundDATA = false;
   vgfs->CRCOK = false;

   if( (!DiskPresent) | 
       ((CYL > MaxTrack)||(SIDE > MaxSide)) |
       ((!FTracksPtr[CYL][SIDE][0])||(!FTracksPtr[CYL][SIDE][1])) )
   {
      vgfs->vgfa.TrackPointer = NULL;
      vgfs->vgfa.ClkPointer = NULL;
      vgfs->vgfa.TrackLength = 0;
      vgfs->vgfa.ADMPointer = NULL;
      vgfs->vgfa.ADMLength = 0;
      vgfs->vgfa.FoundADM = false;
      vgfs->vgfa.CRCOK = false;
      return false;           // ERROR: disk not ready
   }

   unsigned int TrackOffset = FromOffset;

   bool FirstFind = true;
   unsigned int FirstPos;

// Поиск адресной метки требуемого сектора...
   bool ADFOUND = false;
   for(;;)
   {
      if(!FindADMark(CYL, SIDE, TrackOffset, &(vgfs->vgfa) ))
         return false;          // ERROR: No ADMARK found on track

      if(vgfs->vgfa.TrackPointer[(vgfs->vgfa.OffsetADM+2)%vgfs->vgfa.TrackLength]==SECT)
      {
         ADFOUND = true;
         break;
      }

      if(!FirstFind) 
      {
         if(vgfs->vgfa.OffsetEndADM == FirstPos) break;
      }
      else
      {
         FirstPos = vgfs->vgfa.OffsetEndADM;
         FirstFind = false;
      }

      TrackOffset = vgfs->vgfa.OffsetEndADM;
   };

   if(!ADFOUND) return false;

// ADRMARK нужного найден, поиск массива данных...

   unsigned char *track = vgfs->vgfa.TrackPointer;
   unsigned char *clks  = vgfs->vgfa.ClkPointer;
   unsigned int tlen    = vgfs->vgfa.TrackLength;

   unsigned int pos     = vgfs->vgfa.OffsetEndADM;

   unsigned int off, rc;

   for(; pos < tlen*2; pos++)
   {
      off = pos%tlen;
      if( (track[off]==0xA1)&&(clks[off]) ) // fnd Mark
      {
         off = (off+1)%tlen;

         rc = tlen;
         while( (track[off]==0xA1)&&(clks[off]) ) // repeat Mark
         {
            if(!rc) return false;       // ERROR: MFM marks all disk
            off = (off+1)%tlen;
            rc--;
         }

         if((track[off] < 0xF8)||(track[off] > 0xFB))
         {
            break;                      // ERROR: data array not found
         }
         vgfs->DataMarker = track[off];

         off = (off+1)%tlen;

         vgfs->FoundDATA = true;

         unsigned char SL = vgfs->vgfa.TrackPointer[(vgfs->vgfa.OffsetADM+3)%vgfs->vgfa.TrackLength];
         vgfs->SectorLength = 128;
         if(SL) vgfs->SectorLength <<= SL;
         vgfs->SectorPointer = track+off;

         vgfs->MarkedOffsetSector = pos%tlen;
         vgfs->OffsetSector = off;
         vgfs->OffsetEndSector = (off+vgfs->SectorLength)%tlen;

         unsigned short crc = MakeVGCRC(track+(pos%tlen), (off-(pos%tlen))+vgfs->SectorLength);
         vgfs->CRCOK = (track[(off+vgfs->SectorLength)%tlen]==(crc>>8))&&(track[(off+vgfs->SectorLength+1)%tlen]==(crc&0xFF));

         return true;                         // OK read
      }
   }

   return false;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r8, %rbx
andq $0x0, 0x38(%r8)
andl $0x0, 0x40(%r8)
movq %rdi, %r14
andw $0x0, 0x50(%r8)
movzbl 0x145006(%rdi), %edi
xorl $0x1, %edi
cmpb %sil, 0x145007(%r14)
setb %al
cmpb %dl, 0x145008(%r14)
movl %ecx, 0x14(%rsp)
setb %cl
orb %al, %cl
movzbl %cl, %eax
orl %edi, %eax
movl %esi, %ecx
shll $0xc, %ecx
addq %r14, %rcx
movl %edx, %edi
shll $0x4, %edi
cmpq $0x0, 0x40000(%rdi,%rcx)
je 0x1ddf
addq %rcx, %rdi
addq $0x40000, %rdi          # imm = 0x40000
xorl %ecx, %ecx
cmpq $0x0, 0x8(%rdi)
sete %cl
jmp 0x1de2
pushq $0x1
popq %rcx
orl %eax, %ecx
je 0x1e13
andq $0x0, 0x18(%rbx)
andl $0x0, 0x20(%rbx)
andw $0x0, 0x30(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
andl $0x0, 0x10(%rbx)
xorl %r15d, %r15d
movl %r15d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x1, %r15b
movzbl %sil, %eax
movl %eax, 0x10(%rsp)
movzbl %dl, %r13d
movq %r14, %rdi
movl 0x10(%rsp), %esi
movl %r13d, %edx
movl %r9d, %ecx
movq %rbx, %r8
callq 0x1bca
testb %al, %al
je 0x1dfe
movq (%rbx), %rbp
movl 0x28(%rbx), %ecx
leal 0x2(%rcx), %eax
movl 0x10(%rbx), %r12d
xorl %edx, %edx
divl %r12d
movl 0x14(%rsp), %eax
cmpb %al, (%rbp,%rdx)
je 0x1e77
movl 0x2c(%rbx), %r9d
testb $0x1, %r15b
jne 0x1e6d
xorl %r15d, %r15d
cmpl 0xc(%rsp), %r9d
jne 0x1e22
jmp 0x1e01
xorl %r15d, %r15d
movl %r9d, 0xc(%rsp)
jmp 0x1e22
movq 0x8(%rbx), %r9
movl 0x2c(%rbx), %r8d
leal (%r12,%r12), %r10d
cmpl %r10d, %r8d
jae 0x1dfe
movl %r8d, %eax
xorl %edx, %edx
divl %r12d
movl %edx, %esi
cmpb $-0x5f, (%rbp,%rsi)
jne 0x1ea4
cmpb $0x0, (%r9,%rsi)
jne 0x1ea9
incl %r8d
jmp 0x1e83
movl %edx, %edi
addq %rbp, %rsi
leal 0x1(%rdi), %edx
xorl %eax, %eax
cmpl %r12d, %edx
cmovel %eax, %edx
movl %r12d, %r8d
xorl %r15d, %r15d
movl %edx, %eax
movb (%rbp,%rax), %dl
cmpb $-0x5f, %dl
jne 0x1eed
subl $0x1, %r8d
setb %dl
cmpb $0x0, (%r9,%rax)
je 0x1e01
testb %dl, %dl
jne 0x1e01
incl %eax
xorl %edx, %edx
divl %r12d
jmp 0x1ebf
movl %edx, %r8d
andb $-0x4, %r8b
cmpb $-0x8, %r8b
jne 0x1dfe
movb %dl, 0x52(%rbx)
incl %eax
xorl %r13d, %r13d
xorl %edx, %edx
divl %r12d
movl %edx, %r8d
movb $0x1, 0x50(%rbx)
addl $0x3, %ecx
movl %ecx, %eax
xorl %edx, %edx
divl %r12d
movb (%rbp,%rdx), %cl
movl $0x80, %r9d
shll %cl, %r9d
movl %r9d, 0x40(%rbx)
leaq (%r8,%rbp), %rax
movq %rax, 0x38(%rbx)
movl %edi, 0x44(%rbx)
movl %r8d, 0x48(%rbx)
leal (%r9,%r8), %r15d
movl %r15d, %eax
xorl %edx, %edx
divl %r12d
movl %edx, %r14d
movl %edx, 0x4c(%rbx)
subl %edi, %r8d
addl %r9d, %r8d
movq %r8, %rdx
callq 0x1a98
movl %eax, %ecx
movzwl %ax, %eax
movzbl (%rbp,%r14), %edx
shrl $0x8, %eax
cmpw %dx, %ax
jne 0x1f82
incl %r15d
movl %r15d, %eax
xorl %edx, %edx
divl %r12d
cmpb %cl, (%rbp,%rdx)
sete %r13b
movb %r13b, 0x51(%rbx)
movb $0x1, %r15b
jmp 0x1e01