// File: /workspace/github_cpp_makefile_star2_O1/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:
cmpl $0x6, 0x48(%rdi)
jl 0xb660
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movq %rdi, %rbx
movss 0x6ffd(%rip), %xmm0     # 0x1261c
ucomiss 0x98(%rdi), %xmm0
ja 0xb656
xorps %xmm0, %xmm0
cvtss2sd 0x180(%rbx), %xmm0
movsd 0x6dfd(%rip), %xmm1     # 0x12438
ucomisd %xmm0, %xmm1
ja 0xb656
xorps %xmm0, %xmm0
cvtss2sd 0x184(%rbx), %xmm0
ucomisd 0x6dec(%rip), %xmm0     # 0x12440
jbe 0xb661
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movss 0x140(%rbx), %xmm0
ucomiss 0x6fb0(%rip), %xmm0     # 0x12620
jb 0xb656
movss 0x9c(%rbx), %xmm0
ucomiss 0x6fa3(%rip), %xmm0     # 0x12624
ja 0xb656
movss 0x1e0(%rbx), %xmm0
ucomiss 0x6f96(%rip), %xmm0     # 0x12628
ja 0xb656
movq 0x38(%rbx), %rax
movss 0x6f8c(%rip), %xmm0     # 0x1262c
ucomiss (%rax), %xmm0
ja 0xb656
movabsq $0x445e40004385599a, %rcx # imm = 0x445E40004385599A
movq %rcx, 0xfc(%rbx)
movabsq $0x44de400044907666, %rcx # imm = 0x44DE400044907666
movq %rcx, 0x104(%rbx)
movl $0x44de4000, 0x124(%rbx) # imm = 0x44DE4000
movaps 0x6978(%rip), %xmm0     # 0x12050
movups %xmm0, 0x10c(%rbx)
movabsq $0x445e40004505599a, %rcx # imm = 0x445E40004505599A
movq %rcx, 0x11c(%rbx)
movabsq $-0x383cb07fb83cb080, %rcx # imm = 0xC7C34F8047C34F80
movq %rcx, 0x128(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0xa0(%rbx)
movq %rax, 0xc8(%rbx)
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0xc0(%rbx)
leaq 0x130(%rbx), %r15
xorl %r14d, %r14d
cmpl $0x7f, 0xc0(%rbx)
ja 0xb656
movq %rbx, %rdi
callq 0x10566
testl %eax, %eax
jle 0xb77a
movss 0x12c(%rbx), %xmm0
subss 0x128(%rbx), %xmm0
ucomiss 0x6d9f(%rip), %xmm0     # 0x124fc
setbe %cl
testb %cl, %cl
je 0xb656
movq 0xc8(%rbx), %rax
cmpq 0x40(%rbx), %rax
jb 0xb72d
jmp 0xb84f
xorl %ecx, %ecx
testl %eax, %eax
je 0xb760
cmpl $0x8, 0xc0(%rbx)
jne 0xb760
movq 0xc8(%rbx), %rax
movss (%rax), %xmm0
leaq -0x4(%rax), %rcx
cmpl $0x0, 0xc4(%rbx)
cmoveq %r15, %rcx
addss (%rcx), %xmm0
xorl %ecx, %ecx
movss 0x6e77(%rip), %xmm1     # 0x12628
ucomiss %xmm0, %xmm1
ja 0xb840
ucomiss 0x6e63(%rip), %xmm0     # 0x12624
ja 0xb840
leaq 0x4(%rax), %rcx
movq %rcx, 0xc8(%rbx)
ucomiss 0x6e5b(%rip), %xmm0     # 0x12630
jbe 0xb7ea
movl $0x1, 0xc4(%rbx)
orb $0x1, 0xa1(%rbx)
jmp 0xb82e
movss (%rcx), %xmm0
addss 0x6e3e(%rip), %xmm0     # 0x12634
movss %xmm0, (%r15)
cvtss2sd %xmm0, %xmm0
xorl %ecx, %ecx
movsd 0x6c3f(%rip), %xmm1     # 0x12448
ucomisd %xmm0, %xmm1
ja 0xb840
ucomisd 0x6c21(%rip), %xmm0     # 0x12438
ja 0xb840
movl $0x0, 0xc4(%rbx)
addq $0x8, %rax
movq %rax, 0xc8(%rbx)
movl $0x9, 0xc0(%rbx)
movb $0x1, %cl
movl $0x1, %r14d
testb %cl, %cl
jne 0xb746
xorl %ecx, %ecx
jmp 0xb760
movl 0xc0(%rbx), %eax
cmpl $0x8, %eax
jl 0xb656
addq $0x7, %rax
shrq $0x3, %rax
addq $0x9f, %rax
movl $0x33000000, %ecx       # imm = 0x33000000
movl $0x55000000, %edx       # imm = 0x55000000
movzbl (%rbx,%rax), %esi
bswapl %esi
movl %esi, %edi
shll $0x4, %edi
shrl $0x4, %esi
andl $0xf000000, %esi        # imm = 0xF000000
orl %edi, %esi
movl %esi, %edi
andl %ecx, %edi
shrl $0x2, %esi
andl %ecx, %esi
leal (%rsi,%rdi,4), %esi
movl %esi, %edi
andl %edx, %edi
shrl %esi
andl %edx, %esi
leal (%rsi,%rdi,2), %esi
shrl $0x18, %esi
movb %sil, (%rbx,%rax)
leaq -0x1(%rax), %rsi
addq $-0x9f, %rax
cmpq $0x1, %rax
movq %rsi, %rax
jg 0xb876
movzbl 0xa0(%rbx), %eax
andl $0x1f, %eax
movq 0x160(%rbx), %rcx
movl %eax, (%rcx)
movq 0x150(%rbx), %rdi
movl 0xa0(%rbx), %edx
shrl $0x5, %edx
andl $0x1, %edx
leaq 0x75b0(%rip), %rsi      # 0x12e9a
xorl %eax, %eax
callq 0x2060
movl 0xa0(%rbx), %r15d
movl 0xc0(%rbx), %esi
notl %r15d
andl $0x40, %r15d
addl $-0x6, %esi
movq %rbx, %rdi
movl $0x6, %edx
callq 0xfdf2
addl %r15d, %eax
movq 0x170(%rbx), %rcx
movl %eax, (%rcx)
testl %r14d, %r14d
je 0xb975
movq 0x148(%rbx), %rax
movb $0x0, 0x4(%rax)
movl $0x78354352, (%rax)     # imm = 0x78354352
cmpl $0x14, 0xc0(%rbx)
jne 0xb9c8
movq 0x170(%rbx), %rax
andl $0x3f, (%rax)
movzbl 0xa0(%rbx), %eax
andl $0x40, %eax
movzbl 0xa1(%rbx), %ecx
shrl $0x2, %ecx
orl %eax, %ecx
xorl $0x40, %ecx
movq 0x168(%rbx), %rax
movl %ecx, (%rax)
jmp 0xb656
cmpl $0x2, 0x54(%rbx)
jg 0xb656
movq 0x148(%rbx), %rax
movl $0x354352, (%rax)       # imm = 0x354352
movl 0xc0(%rbx), %eax
cmpl $0xf, %eax
je 0xba3f
cmpl $0xe, %eax
jne 0xb9c8
movzbl 0xa1(%rbx), %eax
notl %eax
andl $0xfc, %eax
movq 0x178(%rbx), %rcx
movl %eax, (%rcx)
leal 0x1(%rax), %edx
movl %edx, 0x4(%rcx)
orl $0x2, %eax
movl %eax, 0x8(%rcx)
jmp 0xb656
movl 0xc0(%rbx), %r14d
testl %r14d, %r14d
je 0xb656
movq 0x148(%rbx), %r15
movq %r15, %rdi
callq 0x2090
addq %rax, %r15
movzbl 0xa0(%rbx), %edx
shrl $0x6, %edx
leaq 0x7306(%rip), %rsi      # 0x12d01
movq %r15, %rdi
movl %r14d, %ecx
xorl %eax, %eax
callq 0x2060
movl 0xc0(%rbx), %eax
cmpl $0x9, %eax
jl 0xb656
leal -0x8(%rax), %edx
movq 0x170(%rbx), %r14
cmpl $0x10, %eax
jg 0xbaad
movq %rbx, %rdi
movl $0x8, %esi
callq 0xfdf2
movl %eax, (%r14)
jmp 0xb656
movq 0x148(%rbx), %rax
movl $0x46372d, 0x3(%rax)    # imm = 0x46372D
movl $0x2d354352, (%rax)     # imm = 0x2D354352
movl $0xffff2927, %eax       # imm = 0xFFFF2927
addl 0x2c(%rbx), %eax
cmpl $0xf9e, %eax            # imm = 0xF9E
ja 0xba79
movq 0x148(%rbx), %r14
movq %r14, %rdi
callq 0x2090
movl $0x37352d, (%r14,%rax)  # imm = 0x37352D
movl 0xa0(%rbx), %eax
notl %eax
andl $0x40, %eax
movq 0x160(%rbx), %rcx
addl %eax, (%rcx)
movzbl 0xa1(%rbx), %eax
shrl %eax
movq 0x170(%rbx), %rcx
movl %eax, (%rcx)
xorl $0xff, %eax
movq 0x178(%rbx), %rcx
jmp 0xbadc
movq %rbx, %rdi
movl %edx, %esi
movl $0x8, %edx
callq 0xfdf2
movl %eax, (%r14)
movl 0xc0(%rbx), %edx
addl $-0x10, %edx
movq %rbx, %rdi
movl $0x8, %esi
callq 0xfdf2
movq 0x168(%rbx), %rcx
movl %eax, (%rcx)
jmp 0xb656
nop