// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %rax
movq %r8, %rbx
movq %rdi, %r14
movq $0x0, 0x38(%r8)
movl $0x0, 0x40(%r8)
movw $0x0, 0x50(%r8)
movzbl 0x145006(%rdi), %edi
xorl $0x1, %edi
cmpb %sil, 0x145007(%r14)
setb %al
cmpb %dl, 0x145008(%r14)
movl %ecx, %ebp
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 0x2173
addq %rcx, %rdi
addq $0x40000, %rdi          # imm = 0x40000
xorl %ecx, %ecx
cmpq $0x0, 0x8(%rdi)
sete %cl
jmp 0x2178
movl $0x1, %ecx
orl %eax, %ecx
je 0x219d
movq $0x0, 0x18(%rbx)
xorl %edi, %edi
movl %edi, 0x20(%rbx)
movw $0x0, 0x30(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
movl %edi, 0x10(%rbx)
jmp 0x2339
movzbl %sil, %r15d
movzbl %dl, %r12d
movq %r14, %rdi
movl %r15d, %esi
movl %r12d, %edx
movl %r9d, %ecx
movq %rbx, %r8
callq 0x1f26
testb %al, %al
je 0x2337
movb $0x1, %dil
movq (%rbx), %r9
movl 0x28(%rbx), %ecx
leal 0x2(%rcx), %eax
movl 0x10(%rbx), %r10d
xorl %edx, %edx
divl %r10d
cmpb %bpl, (%r9,%rdx)
je 0x2212
movl 0x2c(%rbx), %ecx
movl %ecx, %r13d
testb $0x1, %dil
jne 0x21f3
movl %esi, %r13d
cmpl %esi, %ecx
je 0x2337
movq %r14, %rdi
movl %r15d, %esi
movl %r12d, %edx
movq %rbx, %r8
callq 0x1f26
xorl %edi, %edi
movl %r13d, %esi
testb %al, %al
jne 0x21c4
jmp 0x2339
movl 0x2c(%rbx), %r8d
leal (%r10,%r10), %ebp
cmpl %ebp, %r8d
jae 0x2337
movq 0x8(%rbx), %r14
xorl %edi, %edi
movl %r8d, %eax
xorl %edx, %edx
divl %r10d
movl %edx, %r11d
cmpb $-0x5f, (%r9,%r11)
jne 0x2242
cmpb $0x0, (%r14,%r11)
jne 0x224f
incl %r8d
cmpl %r8d, %ebp
jne 0x2229
jmp 0x2339
movl %edx, %esi
leal 0x1(%rsi), %eax
xorl %edx, %edx
cmpl %r10d, %eax
cmovnel %eax, %edx
movb (%r9,%rdx), %al
cmpb $-0x5f, %al
jne 0x2294
movl %r10d, %r8d
xorl %edi, %edi
subl $0x1, %r8d
setb %al
cmpb $0x0, (%r14,%rdx)
je 0x2339
testb %al, %al
jne 0x2339
incl %edx
movl %edx, %eax
xorl %edx, %edx
divl %r10d
movb (%r9,%rdx), %al
cmpb $-0x5f, %al
je 0x2269
movl %eax, %edi
andb $-0x4, %dil
cmpb $-0x8, %dil
jne 0x2337
movb %al, 0x52(%rbx)
incl %edx
movl %edx, %eax
xorl %edx, %edx
divl %r10d
movl %edx, %edi
movb $0x1, 0x50(%rbx)
addl $0x3, %ecx
movl %ecx, %eax
xorl %edx, %edx
divl %r10d
movb (%r9,%rdx), %cl
movl $0x80, %r8d
shll %cl, %r8d
movl %r8d, 0x40(%rbx)
leaq (%rdi,%r9), %rax
movq %rax, 0x38(%rbx)
movl %esi, 0x44(%rbx)
movl %edi, 0x48(%rbx)
leal (%r8,%rdi), %ecx
movl %ecx, %eax
xorl %edx, %edx
divl %r10d
movl %edx, 0x4c(%rbx)
subl %esi, %edi
addl %r8d, %edi
je 0x234a
addq %r9, %r11
movl %edi, %eax
movw $0xffff, %si            # imm = 0xFFFF
xorl %edi, %edi
movzbl (%r11,%rdi), %r8d
shll $0x8, %r8d
xorl %r8d, %esi
movl $0x8, %r8d
leal (%rsi,%rsi), %ebp
movl %ebp, %r14d
xorl $0x1021, %r14d          # imm = 0x1021
testw %si, %si
cmovnsl %ebp, %r14d
movl %r14d, %esi
decl %r8d
jne 0x2311
incq %rdi
cmpq %rax, %rdi
jb 0x22ff
jmp 0x234e
xorl %edi, %edi
movl %edi, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movw $0xffff, %si            # imm = 0xFFFF
movl %edx, %eax
movzbl (%r9,%rax), %eax
movzwl %si, %edx
shrl $0x8, %edx
cmpw %ax, %dx
jne 0x2372
incl %ecx
movl %ecx, %eax
xorl %edx, %edx
divl %r10d
cmpb %sil, (%r9,%rdx)
sete %al
jmp 0x2374
xorl %eax, %eax
movb %al, 0x51(%rbx)
movb $0x1, %dil
jmp 0x2339