// File: /workspace/github_cpp_makefile_star2_O1/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, 0x4(%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 0x2167
addq %rcx, %rdi
addq $0x40000, %rdi          # imm = 0x40000
xorl %ecx, %ecx
cmpq $0x0, 0x8(%rdi)
sete %cl
jmp 0x216c
movl $0x1, %ecx
orl %eax, %ecx
je 0x2191
movq $0x0, 0x18(%rbx)
xorl %eax, %eax
movl %eax, 0x20(%rbx)
movw $0x0, 0x30(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
movl %eax, 0x10(%rbx)
jmp 0x2325
movzbl %sil, %r15d
movzbl %dl, %r12d
movq %r14, %rdi
movl %r15d, %esi
movl %r12d, %edx
movl %r9d, %ecx
movq %rbx, %r8
callq 0x1f12
testb %al, %al
je 0x2323
movb $0x1, %r13b
movq (%rbx), %r8
movl 0x28(%rbx), %ecx
leal 0x2(%rcx), %eax
movl 0x10(%rbx), %r9d
xorl %edx, %edx
divl %r9d
movl 0x4(%rsp), %eax
cmpb %al, (%r8,%rdx)
je 0x2208
testb $0x1, %r13b
je 0x21e2
movl 0x2c(%rbx), %ebp
xorl %r13d, %r13d
jmp 0x21eb
cmpl %ebp, 0x2c(%rbx)
je 0x2323
movl 0x2c(%rbx), %ecx
movq %r14, %rdi
movl %r15d, %esi
movl %r12d, %edx
movq %rbx, %r8
callq 0x1f12
testb %al, %al
jne 0x21b8
jmp 0x2323
movl 0x2c(%rbx), %edi
leal (%r9,%r9), %ebp
cmpl %ebp, %edi
jae 0x2323
movq 0x8(%rbx), %r11
movl %edi, %eax
xorl %edx, %edx
divl %r9d
movl %edx, %r10d
cmpb $-0x5f, (%r8,%r10)
jne 0x2233
cmpb $0x0, (%r11,%r10)
jne 0x2240
incl %edi
xorl %eax, %eax
cmpl %edi, %ebp
jne 0x221b
jmp 0x2325
movl %edx, %esi
leal 0x1(%rsi), %eax
xorl %edx, %edx
cmpl %r9d, %eax
cmovnel %eax, %edx
movb (%r8,%rdx), %al
cmpb $-0x5f, %al
jne 0x2280
cmpb $0x0, (%r11,%rdx)
je 0x2280
leal 0x1(%r9), %edi
decl %edi
je 0x2323
incl %edx
movl %edx, %eax
xorl %edx, %edx
divl %r9d
movb (%r8,%rdx), %al
cmpb $-0x5f, %al
jne 0x2280
cmpb $0x0, (%r11,%rdx)
jne 0x2260
movl %eax, %edi
andb $-0x4, %dil
cmpb $-0x8, %dil
jne 0x2323
movb %al, 0x52(%rbx)
incl %edx
movl %edx, %eax
xorl %edx, %edx
divl %r9d
movl %edx, %edi
movb $0x1, 0x50(%rbx)
addl $0x3, %ecx
movl %ecx, %eax
xorl %edx, %edx
divl %r9d
movb (%r8,%rdx), %cl
movl $0x80, %r11d
shll %cl, %r11d
movl %r11d, 0x40(%rbx)
leaq (%rdi,%r8), %rax
movq %rax, 0x38(%rbx)
movl %esi, 0x44(%rbx)
movl %edi, 0x48(%rbx)
leal (%r11,%rdi), %ecx
movl %ecx, %eax
xorl %edx, %edx
divl %r9d
movl %edx, 0x4c(%rbx)
subl %esi, %edi
addl %r11d, %edi
je 0x2334
addq %r8, %r10
movl %edi, %eax
movw $0xffff, %si            # imm = 0xFFFF
xorl %edi, %edi
movzbl (%r10,%rdi), %r11d
shll $0x8, %r11d
xorl %r11d, %esi
movl $0x8, %r11d
leal (%rsi,%rsi), %ebp
movl %ebp, %r14d
xorl $0x1021, %r14d          # imm = 0x1021
testw %si, %si
cmovnsl %ebp, %r14d
movl %r14d, %esi
decl %r11d
jne 0x22fd
incq %rdi
cmpq %rax, %rdi
jb 0x22eb
jmp 0x2338
xorl %eax, %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 (%r8,%rax), %eax
movzwl %si, %edx
shrl $0x8, %edx
cmpw %ax, %dx
jne 0x235c
incl %ecx
movl %ecx, %eax
xorl %edx, %edx
divl %r9d
cmpb %sil, (%r8,%rdx)
sete %al
jmp 0x235e
xorl %eax, %eax
movb %al, 0x51(%rbx)
movb $0x1, %al
jmp 0x2325
nop