// File: probonopd[P]decodeir/DecodeIR.cpp
// Function: Signal::tryRC5()
void Signal::tryRC5()
//
// RC5         {36k,msb,889}<1,-1|-1,1>(1:1,~F:1:6,T:1,D:5,F:6,^114m)+
// StreamZap  OLD {36k,msb,889}<1,-1|-1,1>(1:1,~F:1:6,T:1,D:6,F:6 // OLD changed to RC5-7F DAR Dec 2010 
// RC5-7F      {36k,msb,889}<1,-1|-1,1>(1:1, D:1:5,T:1,D:5,F:7,^114m)+   NEW DAR Dec 2010
// RC5x        {36k,msb,889}<1,-1|-1,1>(1:1,~S:1:6,T:1,D:5,-4,S:6,F:6,^114m)+
//
{
	if ( nFrameL < 6 )
		return;
	const float HB = 889; 
	if ( nTotDur < 10.*HB )
		return;
	if ( sortOn.min1 < HB * .3 )
		return;
	if ( sortOn.max1 > HB * 2.4 )
		return;
	if (   !framed(20*HB)  //framed(nGap) is: return nGap <= frame, which pFramedEnd
		|| nMaxDur > HB*6.5     // -4 in RC5x with -1 half bit on either side
        || sortBurst.max2 > HB*4.5  // nominal 2,-2
		|| *pFrame < HB*.5 )
	{
		return;
	}

    // Log of tweaks:
    //  8/14/05 reduced m_maxShortPulse 1.35 -> 1.3 to avoid misdecoding pid_0003 as rc5-1-16
    //
	m_minShortPulse = HB * .3 ;
	m_nominalShortPulse = HB * 1 ;
	m_nominalShortGap = HB * 1 ;
	m_maxShortPulse = HB * 1.3 ;
	m_nominalLongPulse = HB * 2 ;
	m_nominalLongGap = HB * 2 ;
	m_maxLongPulse = HB * 2.4 ;

	m_minShortGap = HB * .5 ;
	m_maxShortGap = HB * 1.4 ;
	m_minLongGap = HB * 1.55 ;
	m_maxLongGap = HB * 2.4 ;

    m_negDelta = 99999.;
    m_posDelta = -99999.;
	/** DAR this also works for RC5
	cleanup();
	if (processManchesterAtoms(0, 13, HB, 2*HB, pFrame) ) {
		*pDevice = getMsb(2,5);
		sprintf(pMisc, "T=%d", getMsb(1,1));
		*pOBC = 64*(getMsb(0,1)^1) + getMsb(7,6);
		strcpy(pProtocol, "RC5");
		return;
	}
	**/
	cleanup();
	nBit = 1;
	nState = 1;

	int bRC5x = 0;
	do
	{
		if (nBit >= sizeof(cBits)*8)
			return;
		int sw = phaseBit2();
		if ( sw <= 0 )
		{
			if ( sw == 0 || nBit != 8 )  
			{
				return;
			}
			double x = *pBit + m_extra;
			if ( nState )
			{
				x = pBit[-1] + pBit[0];
			}
			if ( x < HB * 4.5 || x > HB * 6.5 )
				return;
			++pBit;
			if ( x > HB * 5.5 )
			{
				nState = 1;
				cBits[8>>3] |= 1 << (8&7);
			}
			else
			{
				m_extra = *pBit - HB;
				if ( m_extra < -(HB*.7) || m_extra > (HB*.3) )
					return;
				nState = 0;
				++pBit;
			}
			++nBit;
			bRC5x = 1;
		}
        if ( m_posDelta - m_negDelta > 300. )
            return;
	} while (pBit < pFrameEnd);

	if (nBit < 8)
		return;

	makeMsb();

	*pDevice = cBits[0] & 31;
	// sprintf(pMisc, "T=%d U=%d:%d", (cBits[0]>>5)&1, (int)(m_negDelta+2*HB), (int)(m_posDelta+2*HB));
	sprintf(pMisc, "T=%d", (cBits[0]>>5)&1);	// GD Removed confusing U-values

	*pOBC = (64^cBits[0]&64) + getMsb(nBit-6,6);
	if (bRC5x)
	{
		strcpy(pProtocol, "RC5x");
		if (nBit == 20)
		{
			*pOBC &= 63;
			*pSubDevice = (64^cBits[0]&64) + (cBits[1]>>2);
			return;
		}
	}
	else if ( preemptValue >= prRCx )		// GD 2009  Avoids spurious decode of OrtekMCE
	{
		return;
	}
	else
	{
		strcpy(pProtocol, "RC5");
		if (nBit == 14)
		{
			int nHex = (255-cBits[1])&0xFC;
			pHex[0] = nHex;
			pHex[1] = nHex+1;
			pHex[2] = nHex+2;
			return;
		}
		else if (nBit == 15)
		{
			strcpy(pProtocol, "RC5-7F");
			if ((nFreq > 55000) && (nFreq < 59000)) 
			{
				strcat(pProtocol,"-57");
			}
			*pDevice += 64^cBits[0]&64;
			*pOBC = getMsb(8,7);
			pHex[0] = (255-*pOBC);
			return;
		}
	}

	if (nBit)
	{
		sprintf(pProtocol+strlen(pProtocol), "-%d-%d?", (cBits[0]>>6), nBit);
		if (nBit > 8)
		{
			if (nBit <= 16)
			{
				*pOBC = getMsb(8,nBit-8);
				return;
			}
			*pOBC = getMsb(nBit-8,8);
			*pSubDevice = getMsb(8, nBit-16);
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpl $0x6, 0x48(%rax)
jge 0xfb2f
jmp 0x10203
movq -0x30(%rbp), %rax
movss 0xa5ad(%rip), %xmm0     # 0x1a0e8
movss %xmm0, -0xc(%rbp)
movss 0x98(%rax), %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0xaadc(%rip), %xmm0     # 0x1a630
ucomisd %xmm1, %xmm0
jbe 0xfb5f
jmp 0x10203
movq -0x30(%rbp), %rax
movss 0x180(%rax), %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0xaac1(%rip), %xmm0     # 0x1a638
ucomisd %xmm1, %xmm0
jbe 0xfb82
jmp 0x10203
movq -0x30(%rbp), %rax
movss 0x184(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0xaaa6(%rip), %xmm1     # 0x1a640
ucomisd %xmm1, %xmm0
jbe 0xfba5
jmp 0x10203
movq -0x30(%rbp), %rdi
movss 0xa53b(%rip), %xmm0     # 0x1a0ec
callq 0x18840
cmpl $0x0, %eax
je 0xfc15
movq -0x30(%rbp), %rax
movss 0x9c(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0xaa75(%rip), %xmm1     # 0x1a648
ucomisd %xmm1, %xmm0
ja 0xfc15
movq -0x30(%rbp), %rax
movss 0x1e0(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0xaa5f(%rip), %xmm1     # 0x1a650
ucomisd %xmm1, %xmm0
ja 0xfc15
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rax
movss (%rax), %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0xaa49(%rip), %xmm0     # 0x1a658
ucomisd %xmm1, %xmm0
jbe 0xfc1a
jmp 0x10203
movq -0x30(%rbp), %rdi
movss 0xa4ea(%rip), %xmm0     # 0x1a110
movss %xmm0, 0xfc(%rdi)
movss 0xa4b2(%rip), %xmm0     # 0x1a0e8
movss %xmm0, 0x100(%rdi)
movss 0xa4a2(%rip), %xmm0     # 0x1a0e8
movss %xmm0, 0x120(%rdi)
movss 0xa4b6(%rip), %xmm0     # 0x1a10c
movss %xmm0, 0x104(%rdi)
movss 0xa4a2(%rip), %xmm0     # 0x1a108
movss %xmm0, 0x108(%rdi)
movss 0xa492(%rip), %xmm0     # 0x1a108
movss %xmm0, 0x124(%rdi)
movss 0xa472(%rip), %xmm0     # 0x1a0f8
movss %xmm0, 0x10c(%rdi)
movss 0xa46e(%rip), %xmm0     # 0x1a104
movss %xmm0, 0x110(%rdi)
movss 0xa45a(%rip), %xmm0     # 0x1a100
movss %xmm0, 0x114(%rdi)
movss 0xa446(%rip), %xmm0     # 0x1a0fc
movss %xmm0, 0x118(%rdi)
movss 0xa432(%rip), %xmm0     # 0x1a0f8
movss %xmm0, 0x11c(%rdi)
movss 0xa41e(%rip), %xmm0     # 0x1a0f4
movss %xmm0, 0x128(%rdi)
movss 0xa40a(%rip), %xmm0     # 0x1a0f0
movss %xmm0, 0x12c(%rdi)
callq 0x16180
movq -0x30(%rbp), %rax
movl $0x1, 0xc0(%rax)
movl $0x1, 0xc4(%rax)
movl $0x0, -0x10(%rbp)
movq -0x30(%rbp), %rax
movslq 0xc0(%rax), %rax
cmpq $0x80, %rax
jb 0xfd2a
jmp 0x10203
movq -0x30(%rbp), %rdi
callq 0x16aa0
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jg 0xfeb2
cmpl $0x0, -0x14(%rbp)
je 0xfd53
movq -0x30(%rbp), %rax
cmpl $0x8, 0xc0(%rax)
je 0xfd58
jmp 0x10203
movq -0x30(%rbp), %rax
movq 0xc8(%rax), %rcx
movss (%rcx), %xmm0
addss 0x130(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x20(%rbp)
cmpl $0x0, 0xc4(%rax)
je 0xfda5
movq -0x30(%rbp), %rax
movq 0xc8(%rax), %rcx
movss -0x4(%rcx), %xmm0
movq 0xc8(%rax), %rax
addss (%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x20(%rbp)
movsd 0xa8a3(%rip), %xmm0     # 0x1a650
ucomisd -0x20(%rbp), %xmm0
ja 0xfdc7
movsd -0x20(%rbp), %xmm0
movsd 0xa887(%rip), %xmm1     # 0x1a648
ucomisd %xmm1, %xmm0
jbe 0xfdcc
jmp 0x10203
movq -0x30(%rbp), %rax
movq 0xc8(%rax), %rcx
addq $0x4, %rcx
movq %rcx, 0xc8(%rax)
movsd -0x20(%rbp), %xmm0
movsd 0xa871(%rip), %xmm1     # 0x1a660
ucomisd %xmm1, %xmm0
jbe 0xfe18
movq -0x30(%rbp), %rax
movl $0x1, 0xc4(%rax)
movzbl 0xa1(%rax), %ecx
orl $0x1, %ecx
movb %cl, 0xa1(%rax)
jmp 0xfe98
movq -0x30(%rbp), %rax
movq 0xc8(%rax), %rcx
movss (%rcx), %xmm0
movss 0xa2b9(%rip), %xmm1     # 0x1a0e8
subss %xmm1, %xmm0
movss %xmm0, 0x130(%rax)
movss 0x130(%rax), %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0xa819(%rip), %xmm0     # 0x1a668
ucomisd %xmm1, %xmm0
ja 0xfe73
movq -0x30(%rbp), %rax
movss 0x130(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0xa7cb(%rip), %xmm1     # 0x1a638
ucomisd %xmm1, %xmm0
jbe 0xfe78
jmp 0x10203
movq -0x30(%rbp), %rax
movl $0x0, 0xc4(%rax)
movq 0xc8(%rax), %rcx
addq $0x4, %rcx
movq %rcx, 0xc8(%rax)
movq -0x30(%rbp), %rax
movl 0xc0(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0xc0(%rax)
movl $0x1, -0x10(%rbp)
movq -0x30(%rbp), %rax
movss 0x12c(%rax), %xmm0
subss 0x128(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0xa306(%rip), %xmm1     # 0x1a1d8
ucomisd %xmm1, %xmm0
jbe 0xfedd
jmp 0x10203
jmp 0xfedf
movq -0x30(%rbp), %rcx
movq 0xc8(%rcx), %rax
cmpq 0x40(%rcx), %rax
jb 0xfd12
movq -0x30(%rbp), %rax
cmpl $0x8, 0xc0(%rax)
jge 0xff06
jmp 0x10203
movq -0x30(%rbp), %rdi
callq 0x16110
movq -0x30(%rbp), %rax
movzbl 0xa0(%rax), %edx
andl $0x1f, %edx
movq 0x160(%rax), %rcx
movl %edx, (%rcx)
movq 0x150(%rax), %rdi
movzbl 0xa0(%rax), %edx
sarl $0x5, %edx
andl $0x1, %edx
leaq 0xb06b(%rip), %rsi      # 0x1afac
movb $0x0, %al
callq 0x2060
movq -0x30(%rbp), %rdi
movzbl 0xa0(%rdi), %eax
andl $0x40, %eax
xorl $0x40, %eax
movl %eax, -0x34(%rbp)
movl 0xc0(%rdi), %esi
subl $0x6, %esi
movl $0x6, %edx
callq 0x15f50
movl -0x34(%rbp), %ecx
movl %eax, %edx
movq -0x30(%rbp), %rax
addl %edx, %ecx
movq 0x170(%rax), %rax
movl %ecx, (%rax)
cmpl $0x0, -0x10(%rbp)
je 0xffeb
movq -0x30(%rbp), %rax
movq 0x148(%rax), %rdi
leaq 0xae54(%rip), %rsi      # 0x1adef
callq 0x2120
movq -0x30(%rbp), %rax
cmpl $0x14, 0xc0(%rax)
jne 0xffe6
movq -0x30(%rbp), %rax
movq 0x170(%rax), %rcx
movl (%rcx), %edx
andl $0x3f, %edx
movl %edx, (%rcx)
movzbl 0xa0(%rax), %ecx
andl $0x40, %ecx
xorl $0x40, %ecx
movzbl 0xa1(%rax), %edx
sarl $0x2, %edx
addl %edx, %ecx
movq 0x168(%rax), %rax
movl %ecx, (%rax)
jmp 0x10203
jmp 0x1011f
movq -0x30(%rbp), %rax
cmpl $0x3, 0x54(%rax)
jl 0xfffa
jmp 0x10203
movq -0x30(%rbp), %rax
movq 0x148(%rax), %rdi
leaq 0xade8(%rip), %rsi      # 0x1adf4
callq 0x2120
movq -0x30(%rbp), %rax
cmpl $0xe, 0xc0(%rax)
jne 0x1006a
movq -0x30(%rbp), %rax
movzbl 0xa1(%rax), %edx
movl $0xff, %ecx
subl %edx, %ecx
andl $0xfc, %ecx
movl %ecx, -0x24(%rbp)
movl -0x24(%rbp), %edx
movq 0x178(%rax), %rcx
movl %edx, (%rcx)
movl -0x24(%rbp), %edx
addl $0x1, %edx
movq 0x178(%rax), %rcx
movl %edx, 0x4(%rcx)
movl -0x24(%rbp), %ecx
addl $0x2, %ecx
movq 0x178(%rax), %rax
movl %ecx, 0x8(%rax)
jmp 0x10203
movq -0x30(%rbp), %rax
cmpl $0xf, 0xc0(%rax)
jne 0x10119
movq -0x30(%rbp), %rax
movq 0x148(%rax), %rdi
leaq 0xad6b(%rip), %rsi      # 0x1adf8
callq 0x2120
movq -0x30(%rbp), %rax
cmpl $0xd6d8, 0x2c(%rax)     # imm = 0xD6D8
jle 0x100c3
movq -0x30(%rbp), %rax
cmpl $0xe678, 0x2c(%rax)     # imm = 0xE678
jge 0x100c3
movq -0x30(%rbp), %rax
movq 0x148(%rax), %rdi
leaq 0xad41(%rip), %rsi      # 0x1adff
callq 0x2230
movq -0x30(%rbp), %rdi
movzbl 0xa0(%rdi), %ecx
andl $0x40, %ecx
xorl $0x40, %ecx
movq 0x160(%rdi), %rax
addl (%rax), %ecx
movl %ecx, (%rax)
movl $0x8, %esi
movl $0x7, %edx
callq 0x15f50
movl %eax, %edx
movq -0x30(%rbp), %rax
movq 0x170(%rax), %rcx
movl %edx, (%rcx)
movq 0x170(%rax), %rdx
movl $0xff, %ecx
subl (%rdx), %ecx
movq 0x178(%rax), %rax
movl %ecx, (%rax)
jmp 0x10203
jmp 0x1011b
jmp 0x1011d
jmp 0x1011f
movq -0x30(%rbp), %rax
cmpl $0x0, 0xc0(%rax)
je 0x10203
movq -0x30(%rbp), %rax
movq 0x148(%rax), %rcx
movq %rcx, -0x40(%rbp)
movq 0x148(%rax), %rdi
callq 0x20a0
movq -0x40(%rbp), %rdi
movq %rax, %rcx
movq -0x30(%rbp), %rax
addq %rcx, %rdi
movzbl 0xa0(%rax), %edx
sarl $0x6, %edx
movl 0xc0(%rax), %ecx
leaq 0xac93(%rip), %rsi      # 0x1ae03
movb $0x0, %al
callq 0x2060
movq -0x30(%rbp), %rax
cmpl $0x8, 0xc0(%rax)
jle 0x10201
movq -0x30(%rbp), %rax
cmpl $0x10, 0xc0(%rax)
jg 0x101b9
movq -0x30(%rbp), %rdi
movl 0xc0(%rdi), %edx
subl $0x8, %edx
movl $0x8, %esi
callq 0x15f50
movl %eax, %ecx
movq -0x30(%rbp), %rax
movq 0x170(%rax), %rax
movl %ecx, (%rax)
jmp 0x10203
movq -0x30(%rbp), %rdi
movl 0xc0(%rdi), %esi
subl $0x8, %esi
movl $0x8, %edx
callq 0x15f50
movq -0x30(%rbp), %rdi
movl %eax, %ecx
movq 0x170(%rdi), %rax
movl %ecx, (%rax)
movl 0xc0(%rdi), %edx
subl $0x10, %edx
movl $0x8, %esi
callq 0x15f50
movl %eax, %ecx
movq -0x30(%rbp), %rax
movq 0x168(%rax), %rax
movl %ecx, (%rax)
jmp 0x10203
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)