// File: probonopd[P]decodeir/DecodeIR.cpp
// Function: Signal::tryTDC()
void Signal::tryTDC()	// GD 2009  Added tryTDC

// TDC-38		{38k,315,msb}<-1,1|1,-1>(1:1,D:5,S:5,F:7,-89m)+
// TDC-56		{56.3k,213,msb}<-1,1|1,-1>(1:1,D:5,S:5,F:7,-89m)+
// V2.39 has revised TDC to allow 7-bit OBC as an example has been found of this.
// In 2.38, F:7 was F:6,~F:1

// OrtekMCE		{38.6k,480}<1,-1|-1,1>(4,-1,D:5,P:2,F:6,C:4,-48m)+
// P is position code:	0 for first frame of repeat sequence
//						1 for all repeats up to last one
//						2 for last repeat
// C is checksum, 3 more than the number of 1 bits in D, P, F together

{
	if ( nFrameL < 8 ) return;

	int HB = *pFrame < 260 ? 213 : *pFrame < 400 ? 315 : 480;
	static int FC = 0;		// count of frames bypassed
	int P = 0;				// position count
	static int FP = 0;	// first P value

	if ( nTotDur < 14.*HB )
		return;
	if ( sortOn.min1 < HB * .3 )
		return;
	if ( sortOn.max2 > HB * 2.4 )
		return;
	if (   !framed(40*HB)
		|| nMaxDur > HB*4.5
        || sortBurst.max2 > HB*4.5  // nominal 2,-2
		|| *pFrame < HB*.5 )
	{
		return;
	}

    minGlitch = HB*.25;
	nMinShort = nMinShort2 = HB*.5;				// Nominally 1
	nMaxShort = nMaxShort2 = nMinLong = HB*1.5;	// 1 vs. 2
	nMaxLong = HB*2.5;							// Nominally 2

	cleanup();
	nBit = 1;
	nState = 1;
	++pBit;

	do
	{	
		if ( ! phaseBit() ) return;
	}	while (pBit < pFrameEnd);

	if ( nBit != 18 ) return;		// wrong number of bits

	if ( HB == 480 )
	{	int chksum = -1;	// checksum is checked with equivalent formula on uncomplemented data
		for ( int ndx = 1; ndx < 14; ndx++ )
		{
			chksum += getLsb(ndx,1);
		}

		if ( chksum != getLsb(14,4) ) return;		// wrong checksum

		setPreempt(prByOrtekMCE);					// avoid RC5, RC6 or Q2 decode of same frame
		P = getLsb(6,2);

		// look to see if there is a following frame, usually with different position code P
		float* pFrameEnd1 = pFrameEnd;					// save for later restoration
		float* pFrameEnd2 = pFrameEnd + 1;
		for ( ; pFrameEnd2 < pMainLimit; pFrameEnd2++ )
		{
			if ( *pFrameEnd2 > 40 * HB ) break;
		};
		pBit = pFrameEnd + 2;
		pFrameEnd = pFrameEnd2;							// temp change for use by phaseBit{}
		nBit++;
		nState = 1;

		while ( pBit < pFrameEnd )
		{	
			if ( ! phaseBit() ) break;
		}	
		pFrameEnd = pFrameEnd1;							// restore original value

		if ( nBit == 36 )
		{
			if (	( ( P ==3 || P == 2 ) &&  
						( getLsb(24,2) == 2 || getLsb(24,2) == 1 ) )	// check valid sequence of position count
				&&	getLsb(1,5) == getLsb(19,5)
				&&	getLsb(8,6) == getLsb(26,6)
				&&	getLsb(14,4) - getLsb(32,4) == (getLsb(6,2) == 3 && getLsb(24,2) < 3) )
			{											
				if ( FP == 0 ) FP = P;					// next frame is valid repeat
				if ( FC == 0 ) setzContext();			// save the context value
				FC++;									// increment count of frames skipped
				return;									// wait for repeat to come round
			}
		}

		P = getLsb(6,2);
		*pDevice = 31 - getLsb(1,5);
		*pOBC = 63 - getLsb(8,6);
		pHex[0] = 0xFC - msb(*pOBC);	// need to encode as comp
		nFrameCount = FC;
		strcpy( pProtocol, "OrtekMCE" );
		nNote_out = 6;
//	changed next line to call "strcpy" (was "sprintf"), per instructions
//	by mathdon, for smoother compile in Unix environments--alex750		
		strcpy( pMisc, FP == 3 && P == 1 ? "" :
			FP == 2 && P == 1 ? (nNote_out = 5, nAuxNote_out = 1, "no start frame") : 
			FP == 1 && P == 1 ? (nNote_out = 0, nAuxNote_out = 6, "only end frame") : 		
			FP == 3 && P == 2 ? (nNote_out = 4, nAuxNote_out = 0, "no end frame") : 
			FP == 2 && P == 2 ? (nNote_out = 1, nAuxNote_out = 2, "no start and end frames") :
			(nNote_out = 0, nAuxNote_out = 5, "only start frame") );
	}
	else
	{
		makeMsb();

//		if ( getMsb(16,1) == getMsb(17,1) ) return;	// GD 2.39 protocol revised to allow 7-bit OBC
		*pDevice = getMsb(1,5);
		*pSubDevice = getMsb(6,5);
		*pOBC = getMsb(11,7);						// GD 2.39 In 2.38 was getMsb(11,6)
		pHex[0] = getMsb(11,7) << 1;
		strcpy( pProtocol, (HB == 213) ? "TDC-56" : "TDC-38"  );
	}
	FC = 0;		// reset static variables
	FP = 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpl $0x8, 0x48(%rax)
jge 0x6752
jmp 0x6f52
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rax
movss 0x138ca(%rip), %xmm0    # 0x1a02c
ucomiss (%rax), %xmm0
jbe 0x6771
movl $0xd5, %eax
movl %eax, -0x34(%rbp)
jmp 0x6798
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rax
movss (%rax), %xmm1
movss 0x138ab(%rip), %xmm0    # 0x1a030
movl $0x1e0, %eax            # imm = 0x1E0
movl $0x13b, %ecx            # imm = 0x13B
ucomiss %xmm1, %xmm0
cmoval %ecx, %eax
movl %eax, -0x34(%rbp)
movq -0x30(%rbp), %rax
movl -0x34(%rbp), %ecx
movl %ecx, -0xc(%rbp)
movl $0x0, -0x10(%rbp)
movss 0x98(%rax), %xmm0
cvtss2sd %xmm0, %xmm1
cvtsi2sdl -0xc(%rbp), %xmm2
movsd 0x13aa6(%rip), %xmm0    # 0x1a268
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x67d1
jmp 0x6f52
movq -0x30(%rbp), %rax
movss 0x180(%rax), %xmm0
cvtss2sd %xmm0, %xmm1
cvtsi2sdl -0xc(%rbp), %xmm0
movsd 0x13a82(%rip), %xmm2    # 0x1a270
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x67fd
jmp 0x6f52
movq -0x30(%rbp), %rax
movss 0x188(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
cvtsi2sdl -0xc(%rbp), %xmm1
movsd 0x13a5e(%rip), %xmm2    # 0x1a278
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x6829
jmp 0x6f52
movq -0x30(%rbp), %rdi
imull $0x28, -0xc(%rbp), %eax
cvtsi2ss %eax, %xmm0
callq 0x18840
cmpl $0x0, %eax
je 0x68b4
movq -0x30(%rbp), %rax
movss 0x9c(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
cvtsi2sdl -0xc(%rbp), %xmm1
movsd 0x13a24(%rip), %xmm2    # 0x1a280
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
ja 0x68b4
movq -0x30(%rbp), %rax
movss 0x1e0(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
cvtsi2sdl -0xc(%rbp), %xmm1
movsd 0x139fd(%rip), %xmm2    # 0x1a280
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
ja 0x68b4
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rax
movss (%rax), %xmm0
cvtss2sd %xmm0, %xmm1
cvtsi2sdl -0xc(%rbp), %xmm0
movsd 0x13916(%rip), %xmm2    # 0x1a1c0
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x68b9
jmp 0x6f52
movq -0x30(%rbp), %rdi
cvtsi2sdl -0xc(%rbp), %xmm0
movsd 0x139ce(%rip), %xmm1    # 0x1a298
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xe0(%rdi)
cvtsi2sdl -0xc(%rbp), %xmm0
movsd 0x138d9(%rip), %xmm1    # 0x1a1c0
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xf4(%rdi)
movss %xmm0, 0xe4(%rdi)
cvtsi2sdl -0xc(%rbp), %xmm0
movsd 0x13984(%rip), %xmm1    # 0x1a290
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xec(%rdi)
movss %xmm0, 0xf8(%rdi)
movss %xmm0, 0xe8(%rdi)
cvtsi2sdl -0xc(%rbp), %xmm0
movsd 0x1394f(%rip), %xmm1    # 0x1a288
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xf0(%rdi)
callq 0x16180
movq -0x30(%rbp), %rax
movl $0x1, 0xc0(%rax)
movl $0x1, 0xc4(%rax)
movq 0xc8(%rax), %rcx
addq $0x4, %rcx
movq %rcx, 0xc8(%rax)
movq -0x30(%rbp), %rdi
callq 0x17050
cmpl $0x0, %eax
jne 0x698b
jmp 0x6f52
jmp 0x698d
movq -0x30(%rbp), %rcx
movq 0xc8(%rcx), %rax
cmpq 0x40(%rcx), %rax
jb 0x6978
movq -0x30(%rbp), %rax
cmpl $0x12, 0xc0(%rax)
je 0x69b0
jmp 0x6f52
cmpl $0x1e0, -0xc(%rbp)      # imm = 0x1E0
jne 0x6e6d
movl $0xffffffff, -0x14(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x18(%rbp)
cmpl $0xe, -0x18(%rbp)
jge 0x69f3
movq -0x30(%rbp), %rdi
movl -0x18(%rbp), %esi
movl $0x1, %edx
callq 0x16030
addl -0x14(%rbp), %eax
movl %eax, -0x14(%rbp)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x69cb
movq -0x30(%rbp), %rdi
movl -0x14(%rbp), %eax
movl %eax, -0x38(%rbp)
movl $0xe, %esi
movl $0x4, %edx
callq 0x16030
movl %eax, %ecx
movl -0x38(%rbp), %eax
cmpl %ecx, %eax
je 0x6a1a
jmp 0x6f52
movq -0x30(%rbp), %rdi
movl $0x3, %esi
callq 0x2810
movq -0x30(%rbp), %rdi
movl $0x6, %esi
movl $0x2, %edx
callq 0x16030
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, -0x10(%rbp)
movq 0x40(%rax), %rcx
movq %rcx, -0x20(%rbp)
movq 0x40(%rax), %rax
addq $0x4, %rax
movq %rax, -0x28(%rbp)
movq -0x30(%rbp), %rcx
movq -0x28(%rbp), %rax
cmpq 0x10(%rcx), %rax
jae 0x6a8d
movq -0x28(%rbp), %rax
movss (%rax), %xmm0
imull $0x28, -0xc(%rbp), %eax
cvtsi2ss %eax, %xmm1
ucomiss %xmm1, %xmm0
jbe 0x6a7d
jmp 0x6a8d
jmp 0x6a7f
movq -0x28(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x28(%rbp)
jmp 0x6a58
movq -0x30(%rbp), %rax
movq 0x40(%rax), %rcx
addq $0x8, %rcx
movq %rcx, 0xc8(%rax)
movq -0x28(%rbp), %rcx
movq %rcx, 0x40(%rax)
movl 0xc0(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0xc0(%rax)
movl $0x1, 0xc4(%rax)
movq -0x30(%rbp), %rcx
movq 0xc8(%rcx), %rax
cmpq 0x40(%rcx), %rax
jae 0x6ae4
movq -0x30(%rbp), %rdi
callq 0x17050
cmpl $0x0, %eax
jne 0x6ae2
jmp 0x6ae4
jmp 0x6ac1
movq -0x30(%rbp), %rax
movq -0x20(%rbp), %rcx
movq %rcx, 0x40(%rax)
cmpl $0x24, 0xc0(%rax)
jne 0x6c65
cmpl $0x3, -0x10(%rbp)
je 0x6b0d
cmpl $0x2, -0x10(%rbp)
jne 0x6c63
movq -0x30(%rbp), %rdi
movl $0x18, %esi
movl $0x2, %edx
callq 0x16030
cmpl $0x2, %eax
je 0x6b41
movq -0x30(%rbp), %rdi
movl $0x18, %esi
movl $0x2, %edx
callq 0x16030
cmpl $0x1, %eax
jne 0x6c63
movq -0x30(%rbp), %rdi
movl $0x1, %esi
movl $0x5, %edx
callq 0x16030
movq -0x30(%rbp), %rdi
movl %eax, -0x3c(%rbp)
movl $0x13, %esi
movl $0x5, %edx
callq 0x16030
movl %eax, %ecx
movl -0x3c(%rbp), %eax
cmpl %ecx, %eax
jne 0x6c63
movq -0x30(%rbp), %rdi
movl $0x8, %esi
movl $0x6, %edx
callq 0x16030
movq -0x30(%rbp), %rdi
movl %eax, -0x40(%rbp)
movl $0x1a, %esi
movl $0x6, %edx
callq 0x16030
movl %eax, %ecx
movl -0x40(%rbp), %eax
cmpl %ecx, %eax
jne 0x6c63
movq -0x30(%rbp), %rdi
movl $0xe, %esi
movl $0x4, %edx
callq 0x16030
movq -0x30(%rbp), %rdi
movl %eax, -0x4c(%rbp)
movl $0x20, %esi
movl $0x4, %edx
callq 0x16030
movq -0x30(%rbp), %rdi
movl %eax, %ecx
movl -0x4c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x48(%rbp)
movl $0x6, %esi
movl $0x2, %edx
callq 0x16030
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x3, %ecx
movb %al, -0x41(%rbp)
jne 0x6c1b
movq -0x30(%rbp), %rdi
movl $0x18, %esi
movl $0x2, %edx
callq 0x16030
cmpl $0x3, %eax
setb %al
movb %al, -0x41(%rbp)
movl -0x48(%rbp), %eax
movb -0x41(%rbp), %cl
andb $0x1, %cl
movzbl %cl, %ecx
cmpl %ecx, %eax
jne 0x6c63
cmpl $0x0, 0x186c6(%rip)     # 0x1f2f8
jne 0x6c3d
movl -0x10(%rbp), %eax
movl %eax, 0x186bb(%rip)     # 0x1f2f8
cmpl $0x0, 0x186b0(%rip)     # 0x1f2f4
jne 0x6c4f
movq -0x30(%rbp), %rdi
callq 0x2860
movl 0x1869f(%rip), %eax     # 0x1f2f4
addl $0x1, %eax
movl %eax, 0x18696(%rip)     # 0x1f2f4
jmp 0x6f52
jmp 0x6c65
movq -0x30(%rbp), %rdi
movl $0x6, %esi
movl $0x2, %edx
callq 0x16030
movq -0x30(%rbp), %rdi
movl %eax, -0x10(%rbp)
movl $0x1, %esi
movl $0x5, %edx
callq 0x16030
movq -0x30(%rbp), %rdi
movl $0x1f, %ecx
subl %eax, %ecx
movq 0x160(%rdi), %rax
movl %ecx, (%rax)
movl $0x8, %esi
movl $0x6, %edx
callq 0x16030
movq -0x30(%rbp), %rdi
movl $0x3f, %ecx
subl %eax, %ecx
movq 0x170(%rdi), %rax
movl %ecx, (%rax)
movq 0x170(%rdi), %rax
movl (%rax), %esi
movl $0x8, %edx
callq 0x15e70
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl $0xfc, %edx
subl %ecx, %edx
movq 0x178(%rax), %rcx
movl %edx, (%rcx)
movl 0x18600(%rip), %ecx     # 0x1f2f4
movl %ecx, 0x60(%rax)
movq 0x148(%rax), %rdi
leaq 0x14124(%rip), %rsi     # 0x1ae29
callq 0x2120
movq -0x30(%rbp), %rax
movl $0x6, 0x88(%rax)
movq 0x150(%rax), %rax
movq %rax, -0x58(%rbp)
cmpl $0x3, 0x185ce(%rip)     # 0x1f2f8
jne 0x6d42
cmpl $0x1, -0x10(%rbp)
jne 0x6d42
leaq 0x144fa(%rip), %rax     # 0x1b233
movq %rax, -0x60(%rbp)
jmp 0x6e5b
cmpl $0x2, 0x185af(%rip)     # 0x1f2f8
jne 0x6d79
cmpl $0x1, -0x10(%rbp)
jne 0x6d79
movq -0x30(%rbp), %rax
movl $0x5, 0x88(%rax)
movl $0x1, 0x8c(%rax)
leaq 0x140c2(%rip), %rax     # 0x1ae32
movq %rax, -0x68(%rbp)
jmp 0x6e53
cmpl $0x1, 0x18578(%rip)     # 0x1f2f8
jne 0x6db0
cmpl $0x1, -0x10(%rbp)
jne 0x6db0
movq -0x30(%rbp), %rax
movl $0x0, 0x88(%rax)
movl $0x6, 0x8c(%rax)
leaq 0x1409a(%rip), %rax     # 0x1ae41
movq %rax, -0x70(%rbp)
jmp 0x6e4b
cmpl $0x3, 0x18541(%rip)     # 0x1f2f8
jne 0x6de4
cmpl $0x2, -0x10(%rbp)
jne 0x6de4
movq -0x30(%rbp), %rax
movl $0x4, 0x88(%rax)
movl $0x0, 0x8c(%rax)
leaq 0x14109(%rip), %rax     # 0x1aee7
movq %rax, -0x78(%rbp)
jmp 0x6e43
cmpl $0x2, 0x1850d(%rip)     # 0x1f2f8
jne 0x6e18
cmpl $0x2, -0x10(%rbp)
jne 0x6e18
movq -0x30(%rbp), %rax
movl $0x1, 0x88(%rax)
movl $0x2, 0x8c(%rax)
leaq 0x1403e(%rip), %rax     # 0x1ae50
movq %rax, -0x80(%rbp)
jmp 0x6e3b
movq -0x30(%rbp), %rax
movl $0x0, 0x88(%rax)
movl $0x5, 0x8c(%rax)
leaq 0x14031(%rip), %rax     # 0x1ae68
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x58(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x2120
jmp 0x6f3e
movq -0x30(%rbp), %rdi
callq 0x16110
movq -0x30(%rbp), %rdi
movl $0x1, %esi
movl $0x5, %edx
callq 0x15f50
movq -0x30(%rbp), %rdi
movl %eax, %ecx
movq 0x160(%rdi), %rax
movl %ecx, (%rax)
movl $0x6, %esi
movl $0x5, %edx
callq 0x15f50
movq -0x30(%rbp), %rdi
movl %eax, %ecx
movq 0x168(%rdi), %rax
movl %ecx, (%rax)
movl $0xb, %esi
movl $0x7, %edx
callq 0x15f50
movq -0x30(%rbp), %rdi
movl %eax, %ecx
movq 0x170(%rdi), %rax
movl %ecx, (%rax)
movl $0xb, %esi
movl $0x7, %edx
callq 0x15f50
movl %eax, %edx
movq -0x30(%rbp), %rax
shll %edx
movq 0x178(%rax), %rcx
movl %edx, (%rcx)
movq 0x148(%rax), %rax
movq %rax, -0x88(%rbp)
cmpl $0xd5, -0xc(%rbp)
jne 0x6f1b
leaq 0x13f67(%rip), %rax     # 0x1ae79
movq %rax, -0x90(%rbp)
jmp 0x6f2b
leaq 0x13f5e(%rip), %rax     # 0x1ae80
movq %rax, -0x90(%rbp)
jmp 0x6f2b
movq -0x88(%rbp), %rdi
movq -0x90(%rbp), %rsi
callq 0x2120
movl $0x0, 0x183ac(%rip)     # 0x1f2f4
movl $0x0, 0x183a6(%rip)     # 0x1f2f8
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax,%rax)