// File: 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
movq %rsp, %rbp
subq $0x80, %rsp
movb %dl, %al
movb %sil, %dl
movq %rdi, -0x10(%rbp)
movb %dl, -0x11(%rbp)
movb %al, -0x12(%rbp)
movl %ecx, -0x18(%rbp)
movq %r8, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x20(%rbp), %rcx
movq $0x0, (%rcx)
movq -0x20(%rbp), %rcx
movq $0x0, 0x8(%rcx)
movq -0x20(%rbp), %rcx
movl $0x0, 0x10(%rcx)
movq -0x20(%rbp), %rcx
movq $0x0, 0x18(%rcx)
movq -0x20(%rbp), %rcx
movl $0x0, 0x20(%rcx)
movq -0x20(%rbp), %rcx
movb $0x0, 0x30(%rcx)
movq -0x20(%rbp), %rcx
movb $0x0, 0x31(%rcx)
movb 0x145006(%rax), %cl
xorb $-0x1, %cl
andb $0x1, %cl
movzbl %cl, %ecx
movl %ecx, -0x48(%rbp)
movzbl -0x11(%rbp), %ecx
movzbl 0x145007(%rax), %edx
movb $0x1, %al
cmpl %edx, %ecx
movb %al, -0x43(%rbp)
jg 0x22cf
movq -0x50(%rbp), %rcx
movzbl -0x12(%rbp), %eax
movzbl 0x145008(%rcx), %ecx
cmpl %ecx, %eax
setg %al
movb %al, -0x43(%rbp)
movq -0x50(%rbp), %rcx
movl -0x48(%rbp), %eax
movb -0x43(%rbp), %dl
andb $0x1, %dl
movzbl %dl, %edx
orl %edx, %eax
movl %eax, -0x58(%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, -0x51(%rbp)
je 0x2339
movq -0x50(%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, -0x51(%rbp)
movl -0x58(%rbp), %eax
movb -0x51(%rbp), %cl
andb $0x1, %cl
movzbl %cl, %ecx
orl %ecx, %eax
cmpl $0x0, %eax
je 0x2355
movb $0x0, -0x1(%rbp)
jmp 0x25ba
movq -0x50(%rbp), %rax
movq %rax, %rcx
addq $0x40000, %rcx          # imm = 0x40000
movzbl -0x11(%rbp), %edx
shlq $0xc, %rdx
addq %rdx, %rcx
movzbl -0x12(%rbp), %edx
shlq $0x4, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
movq -0x20(%rbp), %rdx
movq %rcx, (%rdx)
movq %rcx, -0x28(%rbp)
movq %rax, %rcx
addq $0x40000, %rcx          # imm = 0x40000
movzbl -0x11(%rbp), %edx
shlq $0xc, %rdx
addq %rdx, %rcx
movzbl -0x12(%rbp), %edx
shlq $0x4, %rdx
addq %rdx, %rcx
movq 0x8(%rcx), %rcx
movq -0x20(%rbp), %rdx
movq %rcx, 0x8(%rdx)
movq %rcx, -0x30(%rbp)
movzbl -0x11(%rbp), %ecx
shlq $0xa, %rcx
addq %rcx, %rax
movzbl -0x12(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movq -0x20(%rbp), %rcx
movl %eax, 0x10(%rcx)
movl %eax, -0x34(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
movl -0x34(%rbp), %ecx
addl -0x18(%rbp), %ecx
cmpl %ecx, %eax
jae 0x25b6
movl -0x40(%rbp), %eax
xorl %edx, %edx
divl -0x34(%rbp)
movl %edx, -0x38(%rbp)
movq -0x28(%rbp), %rax
movl -0x38(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0xa1, %eax
jne 0x25a6
movq -0x30(%rbp), %rax
movl -0x38(%rbp), %ecx
cmpb $0x0, (%rax,%rcx)
je 0x25a6
movl -0x38(%rbp), %eax
addl $0x1, %eax
xorl %edx, %edx
divl -0x34(%rbp)
movl %edx, -0x38(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x3c(%rbp)
movq -0x28(%rbp), %rax
movl -0x38(%rbp), %ecx
movzbl (%rax,%rcx), %ecx
xorl %eax, %eax
cmpl $0xa1, %ecx
movb %al, -0x59(%rbp)
jne 0x2459
movq -0x30(%rbp), %rax
movl -0x38(%rbp), %ecx
cmpb $0x0, (%rax,%rcx)
setne %al
movb %al, -0x59(%rbp)
movb -0x59(%rbp), %al
testb $0x1, %al
jne 0x2462
jmp 0x248a
cmpl $0x0, -0x3c(%rbp)
jne 0x2471
movb $0x0, -0x1(%rbp)
jmp 0x25ba
movl -0x38(%rbp), %eax
addl $0x1, %eax
xorl %edx, %edx
divl -0x34(%rbp)
movl %edx, -0x38(%rbp)
movl -0x3c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x2430
movq -0x28(%rbp), %rax
movl -0x38(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0xfe, %eax
je 0x24a1
jmp 0x25a8
movq -0x50(%rbp), %rdi
movl -0x38(%rbp), %eax
addl $0x1, %eax
xorl %edx, %edx
divl -0x34(%rbp)
movl %edx, -0x38(%rbp)
movq -0x20(%rbp), %rax
movb $0x1, 0x30(%rax)
movl -0x40(%rbp), %eax
xorl %edx, %edx
divl -0x34(%rbp)
movq -0x20(%rbp), %rax
movl %edx, 0x24(%rax)
movl -0x38(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x28(%rax)
movl -0x38(%rbp), %eax
addl $0x6, %eax
xorl %edx, %edx
divl -0x34(%rbp)
movq -0x20(%rbp), %rax
movl %edx, 0x2c(%rax)
movq -0x20(%rbp), %rax
movl $0x6, 0x20(%rax)
movq -0x28(%rbp), %rcx
movl -0x38(%rbp), %eax
addq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x28(%rbp), %rsi
movl -0x40(%rbp), %eax
xorl %edx, %edx
divl -0x34(%rbp)
movl %edx, %eax
addq %rax, %rsi
movl -0x38(%rbp), %eax
movl %eax, -0x6c(%rbp)
movl -0x40(%rbp), %eax
xorl %edx, %edx
divl -0x34(%rbp)
movl -0x6c(%rbp), %eax
subl %edx, %eax
addl $0x4, %eax
movl %eax, %eax
movl %eax, %edx
callq 0x1fa0
movw %ax, -0x42(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x68(%rbp)
movl -0x38(%rbp), %eax
addl $0x4, %eax
xorl %edx, %edx
divl -0x34(%rbp)
movq -0x68(%rbp), %rax
movl %edx, %ecx
movzbl (%rax,%rcx), %ecx
movzwl -0x42(%rbp), %edx
sarl $0x8, %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, -0x5a(%rbp)
jne 0x2593
movq -0x28(%rbp), %rax
movq %rax, -0x78(%rbp)
movl -0x38(%rbp), %eax
addl $0x5, %eax
xorl %edx, %edx
divl -0x34(%rbp)
movq -0x78(%rbp), %rax
movl %edx, %ecx
movzbl (%rax,%rcx), %eax
movzwl -0x42(%rbp), %ecx
andl $0xff, %ecx
cmpl %ecx, %eax
sete %al
movb %al, -0x5a(%rbp)
movb -0x5a(%rbp), %cl
andb $0x1, %cl
movq -0x20(%rbp), %rax
movb %cl, 0x31(%rax)
movb $0x1, -0x1(%rbp)
jmp 0x25ba
jmp 0x25a8
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x23d9
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax,%rax)