// File: /workspace/github_cpp_makefile_star2_O1/atsidaev[P]trx2x/DiskImage.cpp
// Function: TDiskImage::FindADMark(unsigned char, unsigned char, unsigned int, VGFIND_ADM*)
bool TDiskImage::FindADMark(unsigned char CYL, unsigned char SIDE,
                            unsigned int FromOffset, 
                            VGFIND_ADM *vgfa)
{
   vgfa->TrackPointer = NULL;
   vgfa->ClkPointer = NULL;
   vgfa->TrackLength = 0;
   vgfa->ADMPointer = NULL;
   vgfa->ADMLength = 0;
   vgfa->FoundADM = false;
   vgfa->CRCOK = false;

   if( (!DiskPresent) | 
       ((CYL > MaxTrack)||(SIDE > MaxSide)) |
       ((!FTracksPtr[CYL][SIDE][0])||(!FTracksPtr[CYL][SIDE][1])) )
   {
      return false;           // ERROR: disk not ready
   }

   unsigned char *track = vgfa->TrackPointer = FTracksPtr[CYL][SIDE][0];
   unsigned char *clks = vgfa->ClkPointer = FTracksPtr[CYL][SIDE][1];
   unsigned int tlen = vgfa->TrackLength = FTrackLength[CYL][SIDE];

   unsigned int off, rc;

   unsigned int pos=FromOffset;
   for(; pos < tlen+FromOffset; 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] != 0xFE) continue;

         off = (off+1)%tlen;

         vgfa->FoundADM = true;
         vgfa->MarkedOffsetADM = pos%tlen;
         vgfa->OffsetADM = off;
         vgfa->OffsetEndADM = (off+6)%tlen;
         vgfa->ADMLength = 6;
         vgfa->ADMPointer = track + off;

         unsigned short crc = MakeVGCRC(track+(pos%tlen), (off-(pos%tlen))+4);

         vgfa->CRCOK = (track[(off+4)%tlen]==(crc>>8))&&(track[(off+5)%tlen]==(crc&0xFF));

         return true;
      }
   }

   return false;
}

// Assembly:
movq $0x0, 0x18(%r8)
xorl %eax, %eax
movl %eax, 0x20(%r8)
movw $0x0, 0x30(%r8)
xorps %xmm0, %xmm0
movups %xmm0, (%r8)
movl %eax, 0x10(%r8)
movzbl 0x145006(%rdi), %r9d
xorl $0x1, %r9d
cmpb %sil, 0x145007(%rdi)
setb %al
cmpb %dl, 0x145008(%rdi)
setb %r10b
orb %al, %r10b
movzbl %r10b, %eax
orl %r9d, %eax
movl %esi, %r9d
shll $0xc, %r9d
addq %rdi, %r9
movl %edx, %r10d
shll $0x4, %r10d
addq %r9, %r10
addq $0x40000, %r10          # imm = 0x40000
movq (%r10), %r9
testq %r9, %r9
je 0x1f8d
xorl %r11d, %r11d
cmpq $0x0, 0x8(%r10)
sete %r11b
jmp 0x1f93
movl $0x1, %r11d
orl %eax, %r11d
je 0x1f9f
xorl %esi, %esi
jmp 0x20e5
pushq %rbp
pushq %r14
pushq %rbx
movl %esi, %eax
movl %edx, %edx
movq %r9, (%r8)
movq 0x8(%r10), %rbx
movq %rbx, 0x8(%r8)
shll $0xa, %eax
addq %rax, %rdi
movl (%rdi,%rdx,4), %r10d
movl %r10d, 0x10(%r8)
leal (%r10,%rcx), %ebp
xorl %esi, %esi
cmpl %ecx, %ebp
jbe 0x20e1
movl %ecx, %eax
xorl %edx, %edx
divl %r10d
movl %edx, %r11d
cmpb $-0x5f, (%r9,%r11)
jne 0x2027
cmpb $0x0, (%rbx,%r11)
je 0x2027
movl %edx, %edi
leal 0x1(%rdi), %edx
cmpl %r10d, %edx
cmovel %esi, %edx
movl %r10d, %r14d
movl %edx, %eax
movzbl (%r9,%rax), %edx
cmpl $0xa1, %edx
jne 0x2018
cmpb $0x0, (%rbx,%rax)
je 0x2022
subl $0x1, %r14d
jb 0x202d
incl %eax
xorl %edx, %edx
divl %r10d
jmp 0x1ff4
cmpl $0xfe, %edx
je 0x2034
jmp 0x2027
cmpb $-0x2, %dl
je 0x2034
incl %ecx
cmpl %ebp, %ecx
jne 0x1fce
xorl %esi, %esi
jmp 0x20e1
incl %eax
xorl %edx, %edx
divl %r10d
movl %edx, %ecx
movb $0x1, 0x30(%r8)
movl %edi, 0x24(%r8)
movl %edx, 0x28(%r8)
leal 0x6(%rcx), %eax
xorl %edx, %edx
divl %r10d
movl %edx, 0x2c(%r8)
movl $0x6, 0x20(%r8)
leaq (%r9,%rcx), %rax
movq %rax, 0x18(%r8)
movl %ecx, %eax
subl %edi, %eax
addl $0x4, %eax
je 0x20aa
addq %r9, %r11
movl %eax, %eax
movw $0xffff, %si            # imm = 0xFFFF
xorl %edx, %edx
movzbl (%r11,%rdx), %edi
shll $0x8, %edi
xorl %edi, %esi
movl $0x8, %edi
leal (%rsi,%rsi), %ebx
movl %ebx, %ebp
xorl $0x1021, %ebp           # imm = 0x1021
testw %si, %si
cmovnsl %ebx, %ebp
movl %ebp, %esi
decl %edi
jne 0x2089
incq %rdx
cmpq %rax, %rdx
jb 0x207a
jmp 0x20ae
movw $0xffff, %si            # imm = 0xFFFF
leal 0x4(%rcx), %eax
xorl %edi, %edi
xorl %edx, %edx
divl %r10d
movzbl (%r9,%rdx), %eax
movzwl %si, %edx
shrl $0x8, %edx
cmpw %ax, %dx
jne 0x20da
addl $0x5, %ecx
movl %ecx, %eax
xorl %edx, %edx
divl %r10d
cmpb %sil, (%r9,%rdx)
sete %dil
movb %dil, 0x31(%r8)
movb $0x1, %sil
popq %rbx
popq %r14
popq %rbp
movl %esi, %eax
retq