// File: /workspace/github_cpp_makefile_star2_O2/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:
cmpl $0x8, 0x48(%rdi)
jl 0x4e3d
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq 0x38(%rdi), %rax
movss (%rax), %xmm3
movss 0xc7b9(%rip), %xmm0     # 0x11518
ucomiss %xmm3, %xmm0
movl $0x13b, %ecx            # imm = 0x13B
movl $0x1e0, %edx            # imm = 0x1E0
cmoval %ecx, %edx
movss 0xc7a5(%rip), %xmm0     # 0x1151c
ucomiss %xmm3, %xmm0
movl $0xd5, %r14d
cmovbel %edx, %r14d
cvtss2sd 0x98(%rdi), %xmm0
imull $0xe, %r14d, %ecx
cvtsi2sd %ecx, %xmm1
ucomisd %xmm0, %xmm1
ja 0x4e2f
cvtsi2sd %r14d, %xmm0
cvtss2sd 0x180(%rbx), %xmm1
movsd 0xc3a5(%rip), %xmm2     # 0x11158
mulsd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
ja 0x4e2f
cvtss2sd 0x188(%rbx), %xmm1
movsd 0xc393(%rip), %xmm2     # 0x11160
mulsd %xmm0, %xmm2
ucomisd %xmm2, %xmm1
ja 0x4e2f
imull $0x28, %r14d, %ecx
cvtsi2ss %ecx, %xmm2
movss 0x140(%rbx), %xmm1
movss %xmm2, -0x30(%rbp)
ucomiss %xmm2, %xmm1
jb 0x4e2f
cvtss2sd 0x9c(%rbx), %xmm2
movsd 0xc367(%rip), %xmm1     # 0x11168
mulsd %xmm0, %xmm1
ucomisd %xmm1, %xmm2
ja 0x4e2f
cvtss2sd 0x1e0(%rbx), %xmm2
ucomisd %xmm1, %xmm2
ja 0x4e2f
cvtss2sd %xmm3, %xmm2
movsd 0xc2ab(%rip), %xmm1     # 0x110d0
mulsd %xmm0, %xmm1
ucomisd %xmm2, %xmm1
jbe 0x4e3e
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movsd 0xc32a(%rip), %xmm2     # 0x11170
mulsd %xmm0, %xmm2
cvtsd2ss %xmm2, %xmm2
movss %xmm2, 0xe0(%rbx)
cvtsd2ss %xmm1, %xmm1
movss %xmm1, 0xf4(%rbx)
movsd 0xc30e(%rip), %xmm2     # 0x11178
mulsd %xmm0, %xmm2
cvtsd2ss %xmm2, %xmm2
movss %xmm1, 0xe4(%rbx)
movss %xmm2, 0xec(%rbx)
movss %xmm2, 0xf8(%rbx)
movss %xmm2, 0xe8(%rbx)
mulsd 0xc2e6(%rip), %xmm0     # 0x11180
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xf0(%rbx)
leaq 0xa0(%rbx), %r12
xorps %xmm0, %xmm0
movups %xmm0, 0xa0(%rbx)
movabsq $0x100000001, %rcx      # imm = 0x100000001
movq %rcx, 0xc0(%rbx)
addq $0x4, %rax
movq %rax, 0xc8(%rbx)
movss %xmm3, -0x2c(%rbp)
movq %rbx, %rdi
callq 0xf4f4
testl %eax, %eax
je 0x4e2f
movq 0xc8(%rbx), %rax
cmpq 0x40(%rbx), %rax
jb 0x4ed8
cmpl $0x12, 0xc0(%rbx)
jne 0x4e2f
cmpl $0x1e0, %r14d           # imm = 0x1E0
jne 0x4f36
pushq $0x1
popq %r14
pushq $-0x1
popq %r13
movl %r14d, %r15d
cmpl $0xe, %r15d
je 0x4fd6
movq %rbx, %rdi
movl %r15d, %esi
movl %r14d, %edx
callq 0xebd6
addl %eax, %r13d
incl %r15d
jmp 0x4f16
movq %rbx, %rdi
callq 0xec52
movl 0xa0(%rbx), %eax
shrl $0x2, %eax
andl $0x1f, %eax
movq 0x160(%rbx), %rcx
movl %eax, (%rcx)
pushq $0x6
popq %rsi
pushq $0x5
popq %rdx
movq %rbx, %rdi
callq 0xeb6a
movq 0x168(%rbx), %rcx
movl %eax, (%rcx)
pushq $0xb
popq %r14
pushq $0x7
popq %r15
movq %rbx, %rdi
movl %r14d, %esi
movl %r15d, %edx
callq 0xeb6a
movq 0x170(%rbx), %rcx
movl %eax, (%rcx)
movq %rbx, %rdi
movl %r14d, %esi
movl %r15d, %edx
callq 0xeb6a
addl %eax, %eax
movss 0xc57b(%rip), %xmm0     # 0x1151c
ucomiss -0x2c(%rbp), %xmm0
movq 0x178(%rbx), %rcx
movl %eax, (%rcx)
movq 0x148(%rbx), %rax
leaq 0xcdb2(%rip), %rcx      # 0x11d6e
leaq 0xcdb2(%rip), %rdx      # 0x11d75
cmovaq %rcx, %rdx
movl (%rdx), %ecx
movl 0x3(%rdx), %edx
movl %ecx, (%rax)
movl %edx, 0x3(%rax)
jmp 0x5292
pushq $0xe
popq %rsi
pushq $0x4
popq %rdx
movq %rbx, %rdi
callq 0xebd6
cmpl %eax, %r13d
jne 0x4e2f
pushq $0x3
popq %rsi
movq %rbx, %rdi
callq 0x264a
movzbl 0xa0(%rbx), %r15d
movl %r15d, %r14d
shrl $0x6, %r14d
movq 0x10(%rbx), %rax
movq 0x40(%rbx), %r13
leaq 0x4(%r13), %rdx
movss -0x30(%rbp), %xmm1
movq %rdx, %rcx
cmpq %rax, %rdx
jae 0x502d
movss (%rcx), %xmm0
leaq 0x4(%rcx), %rdx
ucomiss %xmm1, %xmm0
jbe 0x5018
leaq 0x8(%r13), %rax
movq %rax, 0xc8(%rbx)
movq %rcx, 0x40(%rbx)
incl 0xc0(%rbx)
movl $0x1, 0xc4(%rbx)
movq 0xc8(%rbx), %rax
cmpq 0x40(%rbx), %rax
jae 0x5065
movq %rbx, %rdi
callq 0xf4f4
testl %eax, %eax
jne 0x504c
movq %r13, 0x40(%rbx)
testb %r15b, %r15b
jns 0x5125
cmpl $0x24, 0xc0(%rbx)
jne 0x5125
movzbl 0xa3(%rbx), %eax
movl %eax, %ecx
andl $0x3, %ecx
decl %ecx
movb 0xa0(%rbx), %r15b
cmpl $0x1, %ecx
ja 0x5129
movzbl %r15b, %r12d
movl %r12d, %ecx
shrl %ecx
andl $0x1f, %ecx
movzbl 0xa2(%rbx), %edx
shrl $0x3, %edx
cmpl %edx, %ecx
jne 0x5129
movzbl 0xa1(%rbx), %ecx
andl $0x3f, %ecx
shrl $0x2, %eax
cmpl %ecx, %eax
jne 0x5129
pushq $0xe
popq %rsi
pushq $0x4
popq %rdx
movq %rbx, %rdi
callq 0xebd6
movzbl 0xa4(%rbx), %ecx
andl $0xf, %ecx
subl %ecx, %eax
notl %r12d
xorl %ecx, %ecx
testb $-0x40, %r12b
sete %cl
cmpl %ecx, %eax
jne 0x5129
cmpl $0x0, 0x1020f(%rip)     # 0x15308
jne 0x5102
movl %r14d, 0x10206(%rip)    # 0x15308
movl 0x101fb(%rip), %r14d    # 0x15304
testl %r14d, %r14d
jne 0x5116
movq %rbx, %rdi
callq 0x266a
incl %r14d
movl %r14d, 0x101e4(%rip)    # 0x15304
jmp 0x4e2f
movb (%r12), %r15b
movzbl %r15b, %ecx
movl %ecx, %eax
shrl $0x6, %eax
notl %ecx
shrl %ecx
andl $0x1f, %ecx
movq 0x160(%rbx), %rdx
movl %ecx, (%rdx)
movzbl 0xa1(%rbx), %ecx
notl %ecx
andl $0x3f, %ecx
movq 0x170(%rbx), %rdx
movl %ecx, (%rdx)
bswapl %ecx
movl %ecx, %edx
andl $0xc000000, %edx        # imm = 0xC000000
movl %ecx, %esi
shll $0x4, %esi
shrl $0x4, %ecx
andl $0x3000000, %ecx        # imm = 0x3000000
orl %esi, %ecx
shll $0x2, %ecx
leal (%rcx,%rdx,4), %ecx
movl %ecx, %edx
andl $0x54000000, %edx       # imm = 0x54000000
shrl %ecx
andl $0x55555540, %ecx       # imm = 0x55555540
leal (%rcx,%rdx,2), %ecx
shrl $0x18, %ecx
xorl $0xfc, %ecx
movq 0x178(%rbx), %rdx
movl %ecx, (%rdx)
movl 0x10162(%rip), %ecx     # 0x15304
movl %ecx, 0x60(%rbx)
movq 0x148(%rbx), %rcx
movabsq $0x45434d6b6574724f, %rdx # imm = 0x45434D6B6574724F
movq %rdx, (%rcx)
movb $0x0, 0x8(%rcx)
movl $0x6, 0x88(%rbx)
movq 0x150(%rbx), %rdi
movl 0x10134(%rip), %esi     # 0x15308
movl %esi, %ecx
xorl $0x3, %ecx
movl %eax, %r8d
xorl $0x1, %r8d
movl %r8d, %edx
orl %ecx, %edx
jne 0x51f3
leaq 0xcf17(%rip), %rsi      # 0x12105
jmp 0x528d
movl %esi, %edx
xorl $0x2, %edx
movl %r8d, %r9d
orl %edx, %r9d
jne 0x521a
movabsq $0x100000005, %rax      # imm = 0x100000005
movq %rax, 0x88(%rbx)
leaq 0xcb0f(%rip), %rsi      # 0x11d27
jmp 0x528d
xorl $0x1, %esi
orl %esi, %r8d
jne 0x523c
movabsq $0x600000000, %rax      # imm = 0x600000000
movq %rax, 0x88(%rbx)
leaq 0xcafc(%rip), %rsi      # 0x11d36
jmp 0x528d
xorl $0x2, %eax
orl %eax, %ecx
jne 0x5257
movq $0x4, 0x88(%rbx)
leaq 0xcb87(%rip), %rsi      # 0x11ddc
jmp 0x528d
orl %edx, %eax
jne 0x5275
movabsq $0x200000001, %rax      # imm = 0x200000001
movq %rax, 0x88(%rbx)
leaq 0xcad2(%rip), %rsi      # 0x11d45
jmp 0x528d
movabsq $0x500000000, %rax      # imm = 0x500000000
movq %rax, 0x88(%rbx)
leaq 0xcad0(%rip), %rsi      # 0x11d5d
callq 0x2120
andl $0x0, 0x1006b(%rip)     # 0x15304
andl $0x0, 0x10068(%rip)     # 0x15308
jmp 0x4e2f
nop