// File: 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
movq %rsp, %rbp
subq $0xb0, %rsp
movb %cl, %al
movb %dl, %cl
movb %sil, %dl
movq %rdi, -0x10(%rbp)
movb %dl, -0x11(%rbp)
movb %cl, -0x12(%rbp)
movb %al, -0x13(%rbp)
movq %r8, -0x20(%rbp)
movl %r9d, -0x24(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x20(%rbp), %rcx
movq $0x0, 0x38(%rcx)
movq -0x20(%rbp), %rcx
movl $0x0, 0x40(%rcx)
movq -0x20(%rbp), %rcx
movb $0x0, 0x50(%rcx)
movq -0x20(%rbp), %rcx
movb $0x0, 0x51(%rcx)
movb 0x145006(%rax), %cl
xorb $-0x1, %cl
andb $0x1, %cl
movzbl %cl, %ecx
movl %ecx, -0x64(%rbp)
movzbl -0x11(%rbp), %ecx
movzbl 0x145007(%rax), %edx
movb $0x1, %al
cmpl %edx, %ecx
movb %al, -0x5d(%rbp)
jg 0x2663
movq -0x70(%rbp), %rcx
movzbl -0x12(%rbp), %eax
movzbl 0x145008(%rcx), %ecx
cmpl %ecx, %eax
setg %al
movb %al, -0x5d(%rbp)
movq -0x70(%rbp), %rcx
movl -0x64(%rbp), %eax
movb -0x5d(%rbp), %dl
andb $0x1, %dl
movzbl %dl, %edx
orl %edx, %eax
movl %eax, -0x78(%rbp)
addq $0x40000, %rcx          # imm = 0x40000
movzbl -0x11(%rbp), %eax
shlq $0xc, %rax
addq %rax, %rcx
movzbl -0x12(%rbp), %eax
shlq $0x4, %rax
addq %rax, %rcx
movb $0x1, %al
cmpq $0x0, (%rcx)
movb %al, -0x71(%rbp)
je 0x26cd
movq -0x70(%rbp), %rax
addq $0x40000, %rax          # imm = 0x40000
movzbl -0x11(%rbp), %ecx
shlq $0xc, %rcx
addq %rcx, %rax
movzbl -0x12(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
cmpq $0x0, 0x8(%rax)
setne %al
xorb $-0x1, %al
movb %al, -0x71(%rbp)
movl -0x78(%rbp), %eax
movb -0x71(%rbp), %cl
andb $0x1, %cl
movzbl %cl, %ecx
orl %ecx, %eax
cmpl $0x0, %eax
je 0x2732
movq -0x20(%rbp), %rax
movq $0x0, (%rax)
movq -0x20(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x10(%rax)
movq -0x20(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x20(%rax)
movq -0x20(%rbp), %rax
movb $0x0, 0x30(%rax)
movq -0x20(%rbp), %rax
movb $0x0, 0x31(%rax)
movb $0x0, -0x1(%rbp)
jmp 0x2a8e
movl -0x24(%rbp), %eax
movl %eax, -0x28(%rbp)
movb $0x1, -0x29(%rbp)
movb $0x0, -0x31(%rbp)
movq -0x70(%rbp), %rdi
movb -0x11(%rbp), %dl
movb -0x12(%rbp), %al
movl -0x28(%rbp), %ecx
movq -0x20(%rbp), %r8
movzbl %dl, %esi
movzbl %al, %edx
callq 0x2220
testb $0x1, %al
jne 0x2769
movb $0x0, -0x1(%rbp)
jmp 0x2a8e
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x80(%rbp)
movq -0x20(%rbp), %rax
movl 0x28(%rax), %eax
addl $0x2, %eax
movq -0x20(%rbp), %rcx
xorl %edx, %edx
divl 0x10(%rcx)
movq -0x80(%rbp), %rax
movl %edx, %ecx
movzbl (%rax,%rcx), %eax
movzbl -0x13(%rbp), %ecx
cmpl %ecx, %eax
jne 0x279f
movb $0x1, -0x31(%rbp)
jmp 0x27d2
testb $0x1, -0x29(%rbp)
jne 0x27b5
movq -0x20(%rbp), %rax
movl 0x2c(%rax), %eax
cmpl -0x30(%rbp), %eax
jne 0x27b3
jmp 0x27d2
jmp 0x27c3
movq -0x20(%rbp), %rax
movl 0x2c(%rax), %eax
movl %eax, -0x30(%rbp)
movb $0x0, -0x29(%rbp)
movq -0x20(%rbp), %rax
movl 0x2c(%rax), %eax
movl %eax, -0x28(%rbp)
jmp 0x2740
testb $0x1, -0x31(%rbp)
jne 0x27e1
movb $0x0, -0x1(%rbp)
jmp 0x2a8e
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x20(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x4c(%rbp)
movq -0x20(%rbp), %rax
movl 0x2c(%rax), %eax
movl %eax, -0x50(%rbp)
movl -0x50(%rbp), %eax
movl -0x4c(%rbp), %ecx
shll %ecx
cmpl %ecx, %eax
jae 0x2a8a
movl -0x50(%rbp), %eax
xorl %edx, %edx
divl -0x4c(%rbp)
movl %edx, -0x54(%rbp)
movq -0x40(%rbp), %rax
movl -0x54(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0xa1, %eax
jne 0x2a7a
movq -0x48(%rbp), %rax
movl -0x54(%rbp), %ecx
cmpb $0x0, (%rax,%rcx)
je 0x2a7a
movl -0x54(%rbp), %eax
addl $0x1, %eax
xorl %edx, %edx
divl -0x4c(%rbp)
movl %edx, -0x54(%rbp)
movl -0x4c(%rbp), %eax
movl %eax, -0x58(%rbp)
movq -0x40(%rbp), %rax
movl -0x54(%rbp), %ecx
movzbl (%rax,%rcx), %ecx
xorl %eax, %eax
cmpl $0xa1, %ecx
movb %al, -0x81(%rbp)
jne 0x2891
movq -0x48(%rbp), %rax
movl -0x54(%rbp), %ecx
cmpb $0x0, (%rax,%rcx)
setne %al
movb %al, -0x81(%rbp)
movb -0x81(%rbp), %al
testb $0x1, %al
jne 0x289d
jmp 0x28c5
cmpl $0x0, -0x58(%rbp)
jne 0x28ac
movb $0x0, -0x1(%rbp)
jmp 0x2a8e
movl -0x54(%rbp), %eax
addl $0x1, %eax
xorl %edx, %edx
divl -0x4c(%rbp)
movl %edx, -0x54(%rbp)
movl -0x58(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x2862
movq -0x40(%rbp), %rax
movl -0x54(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0xf8, %eax
jl 0x28e9
movq -0x40(%rbp), %rax
movl -0x54(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0xfb, %eax
jle 0x28ee
jmp 0x2a8a
movq -0x40(%rbp), %rax
movl -0x54(%rbp), %ecx
movb (%rax,%rcx), %cl
movq -0x20(%rbp), %rax
movb %cl, 0x52(%rax)
movl -0x54(%rbp), %eax
addl $0x1, %eax
xorl %edx, %edx
divl -0x4c(%rbp)
movl %edx, -0x54(%rbp)
movq -0x20(%rbp), %rax
movb $0x1, 0x50(%rax)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x20(%rbp), %rax
movl 0x28(%rax), %eax
addl $0x3, %eax
movq -0x20(%rbp), %rcx
xorl %edx, %edx
divl 0x10(%rcx)
movq -0x90(%rbp), %rax
movl %edx, %ecx
movb (%rax,%rcx), %al
movb %al, -0x59(%rbp)
movq -0x20(%rbp), %rax
movl $0x80, 0x40(%rax)
cmpb $0x0, -0x59(%rbp)
je 0x2968
movzbl -0x59(%rbp), %ecx
movq -0x20(%rbp), %rax
movl 0x40(%rax), %edx
shll %cl, %edx
movl %edx, %ecx
movl %ecx, 0x40(%rax)
movq -0x70(%rbp), %rdi
movq -0x40(%rbp), %rcx
movl -0x54(%rbp), %eax
addq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x38(%rax)
movl -0x50(%rbp), %eax
xorl %edx, %edx
divl -0x4c(%rbp)
movq -0x20(%rbp), %rax
movl %edx, 0x44(%rax)
movl -0x54(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x48(%rax)
movl -0x54(%rbp), %eax
movq -0x20(%rbp), %rcx
addl 0x40(%rcx), %eax
xorl %edx, %edx
divl -0x4c(%rbp)
movq -0x20(%rbp), %rax
movl %edx, 0x4c(%rax)
movq -0x40(%rbp), %rsi
movl -0x50(%rbp), %eax
xorl %edx, %edx
divl -0x4c(%rbp)
movl %edx, %eax
addq %rax, %rsi
movl -0x54(%rbp), %eax
movl %eax, -0xa4(%rbp)
movl -0x50(%rbp), %eax
xorl %edx, %edx
divl -0x4c(%rbp)
movl -0xa4(%rbp), %eax
subl %edx, %eax
movq -0x20(%rbp), %rcx
addl 0x40(%rcx), %eax
movl %eax, %eax
movl %eax, %edx
callq 0x1fa0
movw %ax, -0x5c(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0xa0(%rbp)
movl -0x54(%rbp), %eax
movq -0x20(%rbp), %rcx
addl 0x40(%rcx), %eax
xorl %edx, %edx
divl -0x4c(%rbp)
movq -0xa0(%rbp), %rax
movl %edx, %ecx
movzbl (%rax,%rcx), %ecx
movzwl -0x5c(%rbp), %edx
sarl $0x8, %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, -0x91(%rbp)
jne 0x2a64
movq -0x40(%rbp), %rax
movq %rax, -0xb0(%rbp)
movl -0x54(%rbp), %eax
movq -0x20(%rbp), %rcx
addl 0x40(%rcx), %eax
addl $0x1, %eax
xorl %edx, %edx
divl -0x4c(%rbp)
movq -0xb0(%rbp), %rax
movl %edx, %ecx
movzbl (%rax,%rcx), %eax
movzwl -0x5c(%rbp), %ecx
andl $0xff, %ecx
cmpl %ecx, %eax
sete %al
movb %al, -0x91(%rbp)
movb -0x91(%rbp), %cl
andb $0x1, %cl
movq -0x20(%rbp), %rax
movb %cl, 0x51(%rax)
movb $0x1, -0x1(%rbp)
jmp 0x2a8e
jmp 0x2a7c
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x280c
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0xb0, %rsp
popq %rbp
retq
nopl (%rax)