// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq %r8, %rbx
andq $0x0, 0x18(%r8)
andl $0x0, 0x20(%r8)
andw $0x0, 0x30(%r8)
xorps %xmm0, %xmm0
movups %xmm0, (%r8)
andl $0x0, 0x10(%r8)
movzbl 0x145006(%rdi), %r8d
xorl $0x1, %r8d
cmpb %sil, 0x145007(%rdi)
setb %al
cmpb %dl, 0x145008(%rdi)
setb %r9b
orb %al, %r9b
movzbl %r9b, %eax
orl %r8d, %eax
movl %esi, %r8d
shll $0xc, %r8d
addq %rdi, %r8
movl %edx, %r9d
shll $0x4, %r9d
addq %r9, %r8
addq $0x40000, %r8           # imm = 0x40000
movq (%r8), %r15
testq %r15, %r15
je 0x1c4c
xorl %r10d, %r10d
cmpq $0x0, 0x8(%r8)
sete %r10b
jmp 0x1c50
pushq $0x1
popq %r10
xorl %r9d, %r9d
orl %eax, %r10d
jne 0x1d58
movl %esi, %eax
movl %edx, %edx
movq %r15, (%rbx)
movq 0x8(%r8), %r10
movq %r10, 0x8(%rbx)
shll $0xa, %eax
addq %rax, %rdi
movl (%rdi,%rdx,4), %r12d
movl %r12d, 0x10(%rbx)
leal (%r12,%rcx), %edi
cmpl %edi, %ecx
jae 0x1cda
movl %ecx, %eax
xorl %edx, %edx
divl %r12d
movl %edx, %esi
cmpb $-0x5f, (%r15,%rsi)
jne 0x1cd6
cmpb $0x0, (%r10,%rsi)
je 0x1cd6
movl %edx, %r8d
leal 0x1(%r8), %edx
cmpl %r12d, %edx
cmovel %r9d, %edx
movl %r12d, %r11d
movl %edx, %eax
movzbl (%r15,%rax), %edx
cmpl $0xa1, %edx
jne 0x1cce
cmpb $0x0, (%r10,%rax)
je 0x1cd6
subl $0x1, %r11d
jb 0x1cda
incl %eax
xorl %edx, %edx
divl %r12d
jmp 0x1ca9
cmpl $0xfe, %edx
je 0x1cdf
incl %ecx
jmp 0x1c7d
xorl %r9d, %r9d
jmp 0x1d58
addq %r15, %rsi
incl %eax
xorl %ebp, %ebp
xorl %edx, %edx
divl %r12d
movl %edx, %r14d
movb $0x1, 0x30(%rbx)
movl %r8d, 0x24(%rbx)
movl %edx, 0x28(%rbx)
leal 0x6(%r14), %eax
xorl %edx, %edx
divl %r12d
movl %edx, 0x2c(%rbx)
movl $0x6, 0x20(%rbx)
leaq (%r14,%r15), %rax
movq %rax, 0x18(%rbx)
movl %r14d, %edx
subl %r8d, %edx
addl $0x4, %edx
callq 0x1a98
movl %eax, %ecx
movzwl %ax, %esi
leal 0x4(%r14), %eax
xorl %edx, %edx
divl %r12d
movzbl (%r15,%rdx), %eax
shrl $0x8, %esi
cmpw %ax, %si
jne 0x1d51
addl $0x5, %r14d
movl %r14d, %eax
xorl %edx, %edx
divl %r12d
cmpb %cl, (%r15,%rdx)
sete %bpl
movb %bpl, 0x31(%rbx)
movb $0x1, %r9b
movl %r9d, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq